This is an automated email from the ASF dual-hosted git repository.
kou pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-adbc.git
The following commit(s) were added to refs/heads/main by this push:
new ded5eec feat(glib): add gadbc_connection_get_table_schema() (#576)
ded5eec is described below
commit ded5eec7927c8c022a09364bf57bd59e4d35d7f3
Author: Sutou Kouhei <[email protected]>
AuthorDate: Fri Apr 7 11:18:09 2023 +0900
feat(glib): add gadbc_connection_get_table_schema() (#576)
Fixes #549.
---
glib/adbc-glib/connection.c | 37 +++++++++++++++++++++++++++++++++++
glib/adbc-glib/connection.h | 4 ++++
glib/test/{run.rb => helper.rb} | 43 +++++++++++++++++++++++++----------------
glib/test/run.rb | 2 +-
glib/test/test-connection.rb | 21 ++++++++++++++++++++
glib/test/test-statement.rb | 31 +++++++++--------------------
6 files changed, 98 insertions(+), 40 deletions(-)
diff --git a/glib/adbc-glib/connection.c b/glib/adbc-glib/connection.c
index ebf4b61..6500cc3 100644
--- a/glib/adbc-glib/connection.c
+++ b/glib/adbc-glib/connection.c
@@ -241,6 +241,43 @@ gpointer gadbc_connection_get_info(GADBCConnection*
connection, guint32* info_co
}
}
+/**
+ * gadbc_connection_get_table_schema:
+ * @connection: A #GADBCConnection.
+ * @catalog: (nullable): A catalog or %NULL if not applicable.
+ * @db_schema: (nullable): A database schema or %NULL if not applicable.
+ * @table_name: A table name.
+ * @error: (nullable): Return location for a #GError or %NULL.
+ *
+ * Get the Apache Arrow schema of a table.
+ *
+ * Returns: The result set as `struct ArrowSchema *`. It should
+ * be freed with the `ArrowSchema::release` callback then
+ * g_free() when no longer needed.
+ *
+ * Since: 0.4.0
+ */
+gpointer gadbc_connection_get_table_schema(GADBCConnection* connection,
+ const gchar* catalog, const gchar*
db_schema,
+ const gchar* table_name, GError**
error) {
+ const gchar* context = "[adbc][connection][get-table-schema]";
+ struct AdbcConnection* adbc_connection =
+ gadbc_connection_get_raw(connection, context, error);
+ if (!adbc_connection) {
+ return NULL;
+ }
+ struct ArrowSchema* array_schema = g_new0(struct ArrowSchema, 1);
+ struct AdbcError adbc_error = {};
+ AdbcStatusCode status_code = AdbcConnectionGetTableSchema(
+ adbc_connection, catalog, db_schema, table_name, array_schema,
&adbc_error);
+ if (gadbc_error_check(error, status_code, &adbc_error, context)) {
+ return array_schema;
+ } else {
+ g_free(array_schema);
+ return NULL;
+ }
+}
+
/**
* gadbc_connection_get_table_types:
* @connection: A #GADBCConnection.
diff --git a/glib/adbc-glib/connection.h b/glib/adbc-glib/connection.h
index a5c3868..f8b9105 100644
--- a/glib/adbc-glib/connection.h
+++ b/glib/adbc-glib/connection.h
@@ -72,6 +72,10 @@ GADBC_AVAILABLE_IN_0_4
gpointer gadbc_connection_get_info(GADBCConnection* connection, guint32*
info_codes,
gsize n_info_codes, GError** error);
GADBC_AVAILABLE_IN_0_4
+gpointer gadbc_connection_get_table_schema(GADBCConnection* connection,
+ const gchar* catalog, const gchar*
db_schema,
+ const gchar* table_name, GError**
error);
+GADBC_AVAILABLE_IN_0_4
gpointer gadbc_connection_get_table_types(GADBCConnection* connection,
GError** error);
G_END_DECLS
diff --git a/glib/test/run.rb b/glib/test/helper.rb
old mode 100755
new mode 100644
similarity index 51%
copy from glib/test/run.rb
copy to glib/test/helper.rb
index 5fd87d7..f207060
--- a/glib/test/run.rb
+++ b/glib/test/helper.rb
@@ -1,5 +1,3 @@
-#!/usr/bin/env ruby
-#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
@@ -17,20 +15,31 @@
# specific language governing permissions and limitations
# under the License.
-require "pathname"
-require "test-unit"
-
-(ENV["ADBC_DLL_PATH"] || "").split(File::PATH_SEPARATOR).each do |path|
- RubyInstaller::Runtime.add_dll_directory(path)
-end
-
-base_dir = Pathname(__dir__).parent
-test_dir = base_dir + "test"
-
-require "gi"
-
-ADBC = GI.load("ADBC")
-
require "arrow"
-exit(Test::Unit::AutoRunner.run(true, test_dir.to_s))
+module Helper
+ def execute_statement(statement, need_result: true)
+ _, c_abi_array_stream, n_rows_affected = statement.execute(need_result)
+ begin
+ if need_result
+ reader = Arrow::RecordBatchReader.import(c_abi_array_stream)
+ table = reader.read_all
+ yield(table, n_rows_affected) if block_given?
+ else
+ yield(n_rows_affected) if block_given?
+ end
+ ensure
+ GLib.free(c_abi_array_stream) if need_result
+ end
+ end
+
+ def execute_sql(connection, sql, need_result: true, &block)
+ statement = ADBC::Statement.new(connection)
+ begin
+ statement.set_sql_query(sql)
+ execute_statement(statement, need_result: need_result, &block)
+ ensure
+ statement.release
+ end
+ end
+end
diff --git a/glib/test/run.rb b/glib/test/run.rb
index 5fd87d7..352c34f 100755
--- a/glib/test/run.rb
+++ b/glib/test/run.rb
@@ -31,6 +31,6 @@ require "gi"
ADBC = GI.load("ADBC")
-require "arrow"
+require_relative "helper"
exit(Test::Unit::AutoRunner.run(true, test_dir.to_s))
diff --git a/glib/test/test-connection.rb b/glib/test/test-connection.rb
index 130f2a0..cea32c9 100644
--- a/glib/test/test-connection.rb
+++ b/glib/test/test-connection.rb
@@ -16,6 +16,8 @@
# under the License.
class ConnectionTest < Test::Unit::TestCase
+ include Helper
+
def setup
@database = ADBC::Database.new
@database.set_option("driver", "adbc_driver_sqlite")
@@ -86,6 +88,25 @@ class ConnectionTest < Test::Unit::TestCase
end
end
+ def test_table_schema
+ execute_sql(@connection,
+ "CREATE TABLE data (number int, string text)",
+ need_result: false)
+ execute_sql(@connection,
+ "INSERT INTO data VALUES (1, 'hello')",
+ need_result: false)
+
+ c_abi_schema = @connection.get_table_schema(nil, nil, "data")
+ begin
+ schema = Arrow::Schema.import(c_abi_schema)
+ assert_equal(Arrow::Schema.new(number: :int64,
+ string: :string),
+ schema)
+ ensure
+ GLib.free(c_abi_schema)
+ end
+ end
+
def test_table_types
c_abi_array_stream = @connection.table_types
begin
diff --git a/glib/test/test-statement.rb b/glib/test/test-statement.rb
index 8daf803..b1b72da 100644
--- a/glib/test/test-statement.rb
+++ b/glib/test/test-statement.rb
@@ -16,6 +16,8 @@
# under the License.
class StatementTest < Test::Unit::TestCase
+ include Helper
+
def setup
@database = ADBC::Database.new
@database.set_option("driver", "adbc_driver_sqlite")
@@ -30,24 +32,9 @@ class StatementTest < Test::Unit::TestCase
@statement.release
end
- def execute_statement(need_result: true)
- _, c_abi_array_stream, n_rows_affected = @statement.execute(need_result)
- begin
- if need_result
- reader = Arrow::RecordBatchReader.import(c_abi_array_stream)
- table = reader.read_all
- yield(table, n_rows_affected) if block_given?
- else
- yield(n_rows_affected) if block_given?
- end
- ensure
- GLib.free(c_abi_array_stream) if need_result
- end
- end
-
def test_execute
@statement.set_sql_query("SELECT 1")
- execute_statement do |table, _n_rows_affected|
+ execute_statement(@statement) do |table, _n_rows_affected|
assert_equal(Arrow::Table.new("1" => Arrow::Int64Array.new([1])),
table)
end
@@ -55,7 +42,7 @@ class StatementTest < Test::Unit::TestCase
def test_bind
@statement.set_sql_query("CREATE TABLE data (number int)")
- execute_statement
+ execute_statement(@statement, need_result: false)
record_batch =
Arrow::RecordBatch.new(number: Arrow::Int64Array.new([10, 20, 30]))
@@ -65,7 +52,7 @@ class StatementTest < Test::Unit::TestCase
_, c_abi_array, c_abi_schema = record_batch.export
begin
@statement.bind(c_abi_array, c_abi_schema)
- execute_statement(need_result: false) do |n_rows_affected|
+ execute_statement(@statement, need_result: false) do |n_rows_affected|
assert_equal(3, n_rows_affected)
end
ensure
@@ -77,7 +64,7 @@ class StatementTest < Test::Unit::TestCase
end
@statement.set_sql_query("SELECT * FROM data")
- execute_statement do |table, _n_rows_affected|
+ execute_statement(@statement) do |table, _n_rows_affected|
assert_equal(record_batch.to_table,
table)
end
@@ -85,7 +72,7 @@ class StatementTest < Test::Unit::TestCase
def test_bind_stream
@statement.set_sql_query("CREATE TABLE data (number int)")
- execute_statement
+ execute_statement(@statement, need_result: false)
record_batch =
Arrow::RecordBatch.new(number: Arrow::Int64Array.new([10, 20, 30]))
@@ -96,7 +83,7 @@ class StatementTest < Test::Unit::TestCase
c_abi_array_stream = reader.export
begin
@statement.bind_stream(c_abi_array_stream)
- execute_statement(need_result: false) do |n_rows_affected|
+ execute_statement(@statement, need_result: false) do |n_rows_affected|
assert_equal(3, n_rows_affected)
end
ensure
@@ -104,7 +91,7 @@ class StatementTest < Test::Unit::TestCase
end
@statement.set_sql_query("SELECT * FROM data")
- execute_statement do |table, _n_rows_affected|
+ execute_statement(@statement) do |table, _n_rows_affected|
assert_equal(record_batch.to_table,
table)
end