Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(glib): add Vala VAPI for GADBC #1152

Merged
merged 8 commits into from
Nov 14, 2023
Merged

feat(glib): add Vala VAPI for GADBC #1152

merged 8 commits into from
Nov 14, 2023

Conversation

esodan
Copy link
Contributor

@esodan esodan commented Oct 3, 2023

Vala VAPI file is generated for current GADBC 1.0 API version.

It is disable by default, but recommended to be enable when a development package is distributed.

This commit force a 1.0 API versioning, replacing the old one where 0 were used for GIR API version (gir_version), so now shows '1.0' instead of '0.0'.

@esodan esodan requested a review from kou as a code owner October 3, 2023 01:34
@github-actions
Copy link

github-actions bot commented Oct 3, 2023

⚠️ Please follow the Conventional Commits format in CONTRIBUTING.md for PR titles.

Copy link
Member

@kou kou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also add a simple example like https://github.com/apache/arrow/blob/main/c_glib/example/vala/build.vala ?
Or do you want me to do it?

glib/adbc-glib/meson.build Outdated Show resolved Hide resolved
glib/adbc-glib/meson.build Outdated Show resolved Hide resolved
glib/adbc-glib/meson.build Outdated Show resolved Hide resolved
glib/adbc-glib/meson.build Outdated Show resolved Hide resolved
glib/adbc-glib/meson.build Outdated Show resolved Hide resolved
glib/meson_options.txt Show resolved Hide resolved
@kou
Copy link
Member

kou commented Oct 3, 2023

Could you also read the auto-generated comment: #1152 (comment)

@esodan esodan force-pushed the main branch 2 times, most recently from edb9c77 to 46da11d Compare October 4, 2023 23:15
@esodan
Copy link
Contributor Author

esodan commented Oct 4, 2023

⚠️ Please follow the Conventional Commits format in CONTRIBUTING.md for PR titles.

Should be done now.

@esodan
Copy link
Contributor Author

esodan commented Oct 4, 2023

Could you also add a simple example like https://github.com/apache/arrow/blob/main/c_glib/example/vala/build.vala ? Or do you want me to do it?

I've added a C and Vala example for SQLite

@lidavidm lidavidm changed the title Add support to create Vala VAPI for GADBC feat(glib): add Vala VAPI for GADBC Oct 5, 2023
glib/adbc-glib/meson.build Outdated Show resolved Hide resolved
glib/example/README.md Outdated Show resolved Hide resolved
glib/example/sqlite.c Outdated Show resolved Hide resolved
glib/example/sqlite.c Outdated Show resolved Hide resolved
glib/example/sqlite.c Outdated Show resolved Hide resolved
glib/example/vala/README.md Outdated Show resolved Hide resolved
glib/example/vala/README.md Outdated Show resolved Hide resolved
glib/example/vala/sqlite.vala Outdated Show resolved Hide resolved
glib/example/vala/sqlite.vala Outdated Show resolved Hide resolved
glib/meson.build Outdated Show resolved Hide resolved
@esodan
Copy link
Contributor Author

esodan commented Oct 8, 2023

I would like to add an example on how to access a data from a SELECT statement, could you guide me on how can I do that?

I have a problem: GArrow.Statement.execute() returns an out pointer (void*) but it seams to be a ArrowArrayStream which seams doesn't exists in the GLib/API, it is like an interface to be implemented, but no one in the API seems to be used to provide this functionality.

I think is required a GArrowArrayStream as a GInterface to be implemented by a GArrowSimpleStream that can be used to bind an ArrowArrayStream returns from execute().

By adding the above interface should be easy to bind to Vala and simple to use from GLIb/C API I think.

This interface could be defined to contain the methods: get_schema() and get_next(); don't think release() is usable, as it will be a GObject and g_object_unref(), calling release() internally, can be used.

GArrowSimpleStream could be a simple object that can be used to provide a GArrowArrayStream interface, and constructed from an ArrowArrayStream to access the functionality.

Also, this will help others to write C or even, Vala or any other supported GObject Introspection languages, drivers to databases or sources using Arrow format.

@kou
Copy link
Member

kou commented Oct 12, 2023

Sure. We can use garrow_record_batch_reader_import() in Arrow GLib to read data from ArrowArrayStream:

diff --git a/glib/example/meson.build b/glib/example/meson.build
index bc365d72..01932312 100644
--- a/glib/example/meson.build
+++ b/glib/example/meson.build
@@ -18,7 +18,7 @@
 # under the License.
 
 executable('sqlite', 'sqlite.c',
-           dependencies: [adbc_glib],
+           dependencies: [adbc_glib, dependency('arrow-glib')],
            link_language: 'c')
 
 install_data('README.md',
diff --git a/glib/example/sqlite.c b/glib/example/sqlite.c
index 254c8ab5..3d2bd282 100644
--- a/glib/example/sqlite.c
+++ b/glib/example/sqlite.c
@@ -19,6 +19,8 @@
 
 #include <stdlib.h>
 
+#include <arrow-glib/arrow-glib.h>
+
 #include <adbc-glib/adbc-glib.h>
 
 int main(int argc, char** argv) {
@@ -68,6 +70,77 @@ int main(int argc, char** argv) {
     return EXIT_FAILURE;
   }
 
+  GADBCStatement *statement = gadbc_statement_new(conn, &error);
+  if (!statement) {
+    g_print("Error initializing a statement: %s", error->message);
+    g_object_unref(conn);
+    g_object_unref(database);
+    return EXIT_FAILURE;
+  }
+  if (!gadbc_statement_set_sql_query(statement,
+                                     "select sqlite_version() as version",
+                                     &error)) {
+    g_print("Error setting a query: %s", error->message);
+    g_object_unref(statement);
+    g_object_unref(conn);
+    g_object_unref(database);
+    return EXIT_FAILURE;
+  }
+  gpointer c_abi_array_stream;
+  gint64 n_rows_affected;
+  if (!gadbc_statement_execute(statement,
+                               TRUE,
+                               &c_abi_array_stream,
+                               &n_rows_affected,
+                               &error)) {
+    g_print("Error executing a query: %s", error->message);
+    g_error_free(error);
+    g_object_unref(statement);
+    g_object_unref(conn);
+    g_object_unref(database);
+    return EXIT_FAILURE;
+  }
+  GArrowRecordBatchReader *reader =
+    garrow_record_batch_reader_import(c_abi_array_stream, &error);
+  g_free(c_abi_array_stream);
+  if (!reader) {
+    g_print("Error importing a result: %s", error->message);
+    g_error_free(error);
+    g_object_unref(statement);
+    g_object_unref(conn);
+    g_object_unref(database);
+    return EXIT_FAILURE;
+  }
+  GArrowTable *table = garrow_record_batch_reader_read_all(reader, &error);
+  g_object_unref(reader);
+  if (!table) {
+    g_print("Error reading a result: %s", error->message);
+    g_error_free(error);
+    g_object_unref(statement);
+    g_object_unref(conn);
+    g_object_unref(database);
+    return EXIT_FAILURE;
+  }
+  gchar *table_content = garrow_table_to_string(table, &error);
+  g_object_unref(table);
+  if (!table_content) {
+    g_print("Error stringify a result: %s", error->message);
+    g_error_free(error);
+    g_object_unref(statement);
+    g_object_unref(conn);
+    g_object_unref(database);
+    return EXIT_FAILURE;
+  }
+  g_print("Result:\n%s\n", table_content);
+  g_free(table_content);
+  gadbc_statement_release(statement, &error);
+  if (error) {
+    g_print("Error releasing a statement: %s", error->message);
+    g_error_free(error);
+    error = NULL;
+  }
+  g_object_unref(statement);
+
   g_object_unref(conn);
   g_object_unref(database);

@kou
Copy link
Member

kou commented Oct 12, 2023

For Vala:

diff --git a/glib/example/meson.build b/glib/example/meson.build
index bc365d72..76a07079 100644
--- a/glib/example/meson.build
+++ b/glib/example/meson.build
@@ -17,8 +17,10 @@
 # specific language governing permissions and limitations
 # under the License.
 
+arrow_glib = dependency('arrow-glib')
+
 executable('sqlite', 'sqlite.c',
-           dependencies: [adbc_glib],
+           dependencies: [adbc_glib, arrow_glib],
            link_language: 'c')
 
 install_data('README.md',
diff --git a/glib/example/vala/meson.build b/glib/example/vala/meson.build
index f5debcd7..731e0439 100644
--- a/glib/example/vala/meson.build
+++ b/glib/example/vala/meson.build
@@ -25,10 +25,11 @@ if generate_vapi
     ],
     'dependencies': [
       adbc_glib_vapi,
-      dependency('gobject-2.0'),
+      arrow_glib,
     ],
     'vala_args': [
       '--pkg', 'posix',
+      '--vapidir', arrow_glib.get_variable('vapidir'),
     ],
   }
   executable('sqlite', 'sqlite.vala',
diff --git a/glib/example/vala/sqlite.vala b/glib/example/vala/sqlite.vala
index dedb42a3..8d7d2068 100644
--- a/glib/example/vala/sqlite.vala
+++ b/glib/example/vala/sqlite.vala
@@ -29,10 +29,18 @@ int main (string[] args) {
       GLib.message ("Connection to database initialized...");
       try {
         var stm = new GADBC.Statement (conn);
-        string sql = "CREATE TABLE IF NOT EXISTS test (id INTEGER, name TEXT)";
+        string sql = "SELECT sqlite_version() AS version";
         stm.set_sql_query (sql);
-        stm.execute (false, null, null);
-      GLib.message ("Statement executed: %s", sql);
+        void *c_abi_array_stream = null;
+        stm.execute (true, out c_abi_array_stream, null);
+        try {
+            GLib.message ("Statement executed: %s", sql);
+            var reader = GArrow.RecordBatchReader.import (c_abi_array_stream);
+            var table = reader.read_all ();
+            GLib.message ("Executed result: %s", table.to_string ());
+        } finally {
+            GLib.free (c_abi_array_stream);
+        }
       }
       catch (GLib.Error e) {
         GLib.message ("Error executing statement: %s", e.message);

@esodan
Copy link
Contributor Author

esodan commented Oct 16, 2023

Now all suggestions and examples are in place.

glib/example/README.md Outdated Show resolved Hide resolved
glib/example/sqlite.c Outdated Show resolved Hide resolved
glib/example/sqlite.c Outdated Show resolved Hide resolved
glib/example/sqlite.c Outdated Show resolved Hide resolved
glib/example/sqlite.c Outdated Show resolved Hide resolved
glib/example/vala/README.md Outdated Show resolved Hide resolved
esodan and others added 2 commits November 13, 2023 23:55
Vala VAPI file is generated for current GADBC 1.0 API version.

It is disable by default, but recommended to be enable when
a development package is distributed.

This commit force a 1.0 API versioning, replacing the old one
where 0 were used for GIR API version (gir_version), so now
shows '1.0' instead of '0.0'.

Also examples are provided for C and Vala.
* Enable Vala-Lint
* Use gadbc-1.0 for Vala package name
* Require Apache Arrow GLib only when -Dexample=true or -Dvapi=true
* Simplify examples
Copy link
Member

@kou kou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

@kou kou merged commit da364d6 into apache:main Nov 14, 2023
49 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants