Example - SQL#

This section shows examples that execute a simple SQL in multiple ways.

Raw C API from C#

The following code shows how to execute a simple SQL with raw C API in C. Here are points:

 1/*
 2 * Copyright 2022 Sutou Kouhei <kou@clear-code.com>
 3 *
 4 * Licensed under the Apache License, Version 2.0 (the "License");
 5 * you may not use this file except in compliance with the License.
 6 * You may obtain a copy of the License at
 7 *
 8 *     http://www.apache.org/licenses/LICENSE-2.0
 9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 */
15
16/*
17 * How to build:
18 *   $ cc -o sql sql.c $(pkg-config --cflags --libs datafusion)
19 *
20 * How to run:
21 *   $ ./sql
22 *   +----------+
23 *   | Int64(1) |
24 *   +----------+
25 *   | 1        |
26 *   +----------+
27 */
28
29#include <datafusion.h>
30
31#include <stdio.h>
32#include <stdlib.h>
33
34int
35main(void)
36{
37  DFSessionContext *context = df_session_context_new();
38  DFError *error = NULL;
39  DFDataFrame *data_frame = df_session_context_sql(context, "SELECT 1;", &error);
40  if (error) {
41    printf("failed to run SQL: %s\n", df_error_get_message(error));
42    df_error_free(error);
43    df_session_context_free(context);
44    return EXIT_FAILURE;
45  }
46  df_data_frame_show(data_frame, &error);
47  if (error) {
48    printf("failed to show data frame: %s\n", df_error_get_message(error));
49    df_error_free(error);
50  }
51  df_data_frame_free(data_frame);
52  df_session_context_free(context);
53  return EXIT_SUCCESS;
54}

GLib API from C#

The following code shows how to execute a simple SQL with GLib API in C. Here are points:

 1/*
 2 * Copyright 2022 Sutou Kouhei <kou@clear-code.com>
 3 *
 4 * Licensed under the Apache License, Version 2.0 (the "License");
 5 * you may not use this file except in compliance with the License.
 6 * You may obtain a copy of the License at
 7 *
 8 *     http://www.apache.org/licenses/LICENSE-2.0
 9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 */
15
16/*
17 * How to build:
18 *   $ cc -o sql-glib sql-glib.c $(pkg-config --cflags --libs datafusion-glib)
19 *
20 * How to run:
21 *   $ ./sql-glib
22 *   +----------+
23 *   | Int64(1) |
24 *   +----------+
25 *   | 1        |
26 *   +----------+
27 */
28
29#include <datafusion-glib/datafusion-glib.h>
30
31#include <stdlib.h>
32
33int
34main(void)
35{
36  GDFSessionContext *context = gdf_session_context_new();
37  GError *error = NULL;
38  GDFDataFrame *data_frame = gdf_session_context_sql(context,
39                                                     "SELECT 1;",
40                                                     &error);
41  if (error) {
42    g_print("failed to run SQL: %s\n", error->message);
43    g_error_free(error);
44    g_object_unref(context);
45    return EXIT_FAILURE;
46  }
47  gdf_data_frame_show(data_frame, &error);
48  if (error) {
49    g_print("failed to show data frame: %s\n", error->message);
50    g_error_free(error);
51  }
52  g_object_unref(data_frame);
53  g_object_unref(context);
54  return EXIT_SUCCESS;
55}

Raw C API from Python#

The following code shows how to execute a simple SQL with raw C API in Python. You can use a FFI library such as ctypes to use raw C API from Python. Here are points:

  • You need to open DataFusion C’s shared library explicitly but shared library name depends on platform. For example, it’s libdatafusion.so on Linux. ctypes.util.find_library() may help you.

  • You need to open DataFusion C’s shared library by ctypes.CDLL().

  • You need to prepare signature of raw C API’s functions that you need to use before you call them.

  • See Raw C API from C how to use raw C API.

 1#!/usr/bin/env python3
 2#
 3# Copyright 2022 Sutou Kouhei <kou@clear-code.com>
 4#
 5# Licensed under the Apache License, Version 2.0 (the "License");
 6# you may not use this file except in compliance with the License.
 7# You may obtain a copy of the License at
 8#
 9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# How to run:
18#   $ ./sql.py
19#   +----------+
20#   | Int64(1) |
21#   +----------+
22#   | 1        |
23#   +----------+
24
25import ctypes
26import ctypes.util
27
28datafusion_so_path = ctypes.util.find_library('datafusion')
29if datafusion_so_path is None:
30    import sys
31    if sys.platform == 'darwin':
32        datafusion_so_path = 'libdatafusion.dylib'
33    elif sys.platform == 'win32':
34        datafusion_so_path = 'datafusion.dll'
35    else:
36        datafusion_so_path = 'libdatafusion.so'
37datafusion = ctypes.CDLL(datafusion_so_path)
38
39datafusion.df_error_free.argtypes = [ctypes.c_void_p]
40datafusion.df_error_free.restype = None
41datafusion.df_error_get_message.argtypes = [ctypes.c_void_p]
42datafusion.df_error_get_message.restype = ctypes.c_char_p
43
44datafusion.df_session_context_new.argtypes = []
45datafusion.df_session_context_new.restype = ctypes.c_void_p
46datafusion.df_session_context_free.argtypes = [ctypes.c_void_p]
47datafusion.df_session_context_free.restype = None
48datafusion.df_session_context_sql.argtypes = \
49    [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_void_p]
50datafusion.df_session_context_sql.restype = ctypes.c_void_p
51
52datafusion.df_data_frame_free.argtypes = [ctypes.c_void_p]
53datafusion.df_data_frame_free.restype = None
54datafusion.df_data_frame_show.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
55datafusion.df_data_frame_show.restype = None
56
57context = datafusion.df_session_context_new()
58try:
59    error = (ctypes.c_void_p * 1)()
60    try:
61        data_frame = datafusion.df_session_context_sql(
62            context, b'SELECT 1;', ctypes.pointer(error))
63        if error[0] is not None:
64            message = datafusion.df_error_get_message(error[0])
65            print(f'failed to run SQL: {message.decode()}')
66            exit(1)
67        try:
68            datafusion.df_data_frame_show(data_frame, ctypes.pointer(error));
69            if error[0] is not None:
70                message = datafusion.df_error_get_message(error[0])
71                print('failed to show data frame: {message.decode()}')
72                exit(1)
73        finally:
74            datafusion.df_data_frame_free(data_frame)
75    finally:
76        if error[0] is not None:
77            datafusion.df_error_free(error[0])
78finally:
79    datafusion.df_session_context_free(context)

GLib API from Python#

The following code shows how to execute a simple SQL with GLib API in Python. You can use PyGObject to use GLib API from Python. Here are points:

  • You can use DataFusion as GObject Introspection namespace.

  • See GLib API from C how to use GLib API.

 1#!/usr/bin/env python3
 2#
 3# Copyright 2022 Sutou Kouhei <kou@clear-code.com>
 4#
 5# Licensed under the Apache License, Version 2.0 (the "License");
 6# you may not use this file except in compliance with the License.
 7# You may obtain a copy of the License at
 8#
 9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# How to run:
18#   $ ./sql-glib.py
19#   +----------+
20#   | Int64(1) |
21#   +----------+
22#   | 1        |
23#   +----------+
24
25import gi
26from gi.repository import DataFusion
27
28context = DataFusion.SessionContext.new()
29data_frame = context.sql("SELECT 1;", )
30data_frame.show()

Raw C API from Ruby#

The following code shows how to execute a simple SQL with raw C API in Ruby. You can use a FFI library such as Fiddle to use raw C API from Ruby. Here are points:

  • You need to open DataFusion C’s shared library explicitly but shared library name depends on platform. For example, it’s libdatafusion.so on Linux. RbConfig::CONFIG["SOEXT"] may help you.

  • You need to open DataFusion C’s shared library by Fiddle::Importer.dlload.

  • You need to register raw C API’s functions that you need to use explicitly before you call them.

  • See Raw C API from C how to use raw C API.

 1#!/usr/bin/env ruby
 2#
 3# Copyright 2022 Sutou Kouhei <kou@clear-code.com>
 4#
 5# Licensed under the Apache License, Version 2.0 (the "License");
 6# you may not use this file except in compliance with the License.
 7# You may obtain a copy of the License at
 8#
 9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# How to run:
18#   $ ./sql.rb
19#   +----------+
20#   | Int64(1) |
21#   +----------+
22#   | 1        |
23#   +----------+
24
25require "rbconfig"
26require "fiddle/import"
27
28module DataFusion
29  extend Fiddle::Importer
30
31  ext = RbConfig::CONFIG["SOEXT"]
32  if /mingw|mswin/.match?(RUBY_PLATFORM)
33    prefix = ""
34  else
35    prefix = "lib"
36  end
37  dlload "#{prefix}datafusion.#{ext}"
38
39  typealias "DFError *", "void *"
40  extern "void df_error_free(DFError *error)"
41  extern "const char *df_error_get_message(DFError *error)"
42
43  typealias "DFDataFrame *", "void *"
44  extern "void df_data_frame_free(DFDataFrame *data_frame)"
45  extern "void df_data_frame_show(DFDataFrame *data_frame, DFError **error)"
46
47  typealias "DFSessionContext *", "void *"
48  extern "DFSessionContext *df_session_context_new(void)"
49  extern "void df_session_context_free(DFSessionContext *ctx)"
50  extern "DFDataFrame *df_session_context_sql(DFSessionContext *ctx, const char *sql, DFError **error)"
51end
52
53begin
54  context = DataFusion.df_session_context_new
55  Fiddle::Pointer.malloc(Fiddle::SIZEOF_VOIDP, Fiddle::RUBY_FREE) do |error|
56    begin
57      data_frame = DataFusion.df_session_context_sql(context, "SELECT 1;", error)
58      unless error.ptr.null?
59        message = DataFusion.df_error_get_message(error.ptr)
60        puts("failed to run SQL: #{message}")
61        exit(false)
62      end
63      begin
64        DataFusion.df_data_frame_show(data_frame, error)
65        unless error.ptr.null?
66          message = DataFusion.df_error_get_message(error.ptr)
67          puts("failed to show data frame: #{message}")
68          exit(false)
69        end
70      ensure
71        DataFusion.df_data_frame_free(data_frame)
72      end
73    ensure
74      DataFusion.df_error_free(error.ptr) unless error.ptr.null?
75    end
76  end
77ensure
78  DataFusion.df_session_context_free(context)
79end

GLib API from Ruby#

The following code shows how to execute a simple SQL with GLib API in Ruby. You can use gobject-introspection gem or red-datafusion gem to use GLib API from Ruby. The following example uses gobject-introspection gem but you should use red-datafusion gem in production. red-datafusion gem is built on top of the gobject-introspection gem and adds many useful APIs. Here are points:

  • You can use DataFusion as GObject Introspection namespace.

  • See GLib API from C how to use GLib API.

 1#!/usr/bin/env ruby
 2#
 3# Copyright 2022 Sutou Kouhei <kou@clear-code.com>
 4#
 5# Licensed under the Apache License, Version 2.0 (the "License");
 6# you may not use this file except in compliance with the License.
 7# You may obtain a copy of the License at
 8#
 9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# How to run:
18#   $ ./sql-glib.rb
19#   +----------+
20#   | Int64(1) |
21#   +----------+
22#   | 1        |
23#   +----------+
24
25require "gi"
26
27DataFusion = GI.load("DataFusion")
28
29context = DataFusion::SessionContext.new
30data_frame = context.sql("SELECT 1;")
31data_frame.show

GLib API from Vala#

The following code shows how to execute a simple SQL with GLib API in Vala. You need to add -Dvapi=true when you run meson setup to enable Vala support. Here are points:

  • You can use datafusion-glib as Vala package name.

  • You can get a directory where Vala API file for datafusion-glib package exists by pkg-config --variable=vapidir datafusion-glib.

  • See GLib API from C how to use GLib API.

 1// Copyright 2022 Sutou Kouhei <kou@clear-code.com>
 2//
 3// Licensed under the Apache License, Version 2.0 (the "License");
 4// you may not use this file except in compliance with the License.
 5// You may obtain a copy of the License at
 6//
 7//     http://www.apache.org/licenses/LICENSE-2.0
 8//
 9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// How to build:
16//   $ valac \
17//       --vapidir $(pkg-config --variable=vapidir datafusion-glib) \
18//       --pkg datafusion-glib \
19//       --pkg posix \
20//       sql-glib.vala
21//
22// How to run:
23//   $ ./sql-glib
24//   +----------+
25//   | Int64(1) |
26//   +----------+
27//   | 1        |
28//   +----------+
29
30int main(string[] args) {
31    var context = new GDF.SessionContext();
32    GDF.DataFrame data_frame;
33    try {
34        data_frame = context.sql("SELECT 1;");
35    } catch (Error error) {
36        stderr.printf("failed to run SQL: %s\n", error.message);
37        return Posix.EXIT_FAILURE;
38    }
39    try {
40        data_frame.show();
41    } catch (Error error) {
42        stderr.printf("failed to show data frame: %s\n", error.message);
43        return Posix.EXIT_FAILURE;
44    }
45    return Posix.EXIT_SUCCESS;
46}