Skip to content

Commit

Permalink
Add support for DuckDB 1.1.0 (#4)
Browse files Browse the repository at this point in the history
This fixes a number of errors due to changes in DuckDB and ruby-duckdb
between 0.5.0 and 1.1.0.
  • Loading branch information
stenlarsson authored Oct 10, 2024
1 parent 13cb5ce commit 3b01aee
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 21 deletions.
20 changes: 11 additions & 9 deletions ext/arrow-duckdb/arrow-duckdb-registration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,11 @@ namespace {
}

arrow::compute::Expression
convert_filters(std::unordered_map<
idx_t,
std::unique_ptr<duckdb::TableFilter>
> &filters,
convert_filters(duckdb::TableFilterSet *filter_set,
std::unordered_map<idx_t, std::string> &column_names)
{
std::vector<arrow::compute::Expression> expressions;
for (auto it = filters.begin(); it != filters.end(); ++it) {
for (auto it = filter_set->filters.begin(); it != filter_set->filters.end(); ++it) {
expressions.emplace_back(
std::move(convert_filter(it->second.get(), column_names[it->first])));
}
Expand All @@ -192,7 +189,7 @@ namespace {
if (have_filter) {
ARROW_RETURN_NOT_OK(
scanner_builder->Filter(
convert_filters(parameters.filters->filters,
convert_filters(parameters.filters,
parameters.projected_columns.projection_map)));
}
if (!parameters.projected_columns.columns.empty()) {
Expand All @@ -202,7 +199,7 @@ namespace {
}
ARROW_ASSIGN_OR_RAISE(auto scanner, scanner_builder->Finish());
ARROW_ASSIGN_OR_RAISE(auto reader, scanner->ToRecordBatchReader());
auto stream_wrapper = duckdb::make_unique<duckdb::ArrowArrayStreamWrapper>();
auto stream_wrapper = duckdb::make_uniq<duckdb::ArrowArrayStreamWrapper>();
ARROW_RETURN_NOT_OK(
arrow::ExportRecordBatchReader(reader,
&(stream_wrapper->arrow_array_stream)));
Expand All @@ -227,8 +224,13 @@ namespace {
{
auto garrow_table = GARROW_TABLE(reinterpret_cast<gpointer>(data));
auto arrow_table = garrow_table_get_raw(garrow_table);
arrow::ExportSchema(*(arrow_table->schema()),
reinterpret_cast<ArrowSchema *>(&schema));
auto export_schema_status = arrow::ExportSchema(*(arrow_table->schema()),
reinterpret_cast<ArrowSchema *>(&schema));
if (!export_schema_status.ok()) {
throw std::runtime_error(
std::string("[arrow][get_schema] failed to export schema: ") +
export_schema_status.ToString());
}
}
}

Expand Down
18 changes: 8 additions & 10 deletions ext/arrow-duckdb/arrow-duckdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

#include <rbgobject.h>

extern "C" {
#include <ruby-duckdb.h>
}

#include "arrow-duckdb-registration.hpp"

Expand Down Expand Up @@ -99,7 +101,7 @@ namespace {
result_ensure_gschema(Result *result)
{
ArrowSchema c_abi_schema;
duckdb_arrow_schema schema = &c_abi_schema;
auto schema = reinterpret_cast<duckdb_arrow_schema>(&c_abi_schema);
auto state = duckdb_query_arrow_schema(result->arrow, &schema);
if (state == DuckDBError) {
free(result->error_message);
Expand All @@ -121,7 +123,7 @@ namespace {
result_fetch_internal(VALUE self, Result *result)
{
ArrowArray c_abi_array = {};
duckdb_arrow_array array = &c_abi_array;
auto array = reinterpret_cast<duckdb_arrow_array>(&c_abi_array);
auto state = duckdb_query_arrow_array(result->arrow, &array);
if (state == DuckDBError) {
free(result->error_message);
Expand Down Expand Up @@ -219,8 +221,7 @@ namespace {
VALUE
query_sql_arrow(VALUE self, VALUE sql)
{
rubyDuckDBConnection *ctx;
Data_Get_Struct(self, rubyDuckDBConnection, ctx);
auto ctx = get_struct_connection(self);

if (!(ctx->con)) {
rb_raise(eDuckDBError, "Database connection closed");
Expand Down Expand Up @@ -253,8 +254,7 @@ namespace {
VALUE
query_unregister_arrow(VALUE self, VALUE name)
{
rubyDuckDBConnection *ctx;
Data_Get_Struct(self, rubyDuckDBConnection, ctx);
auto ctx = get_struct_connection(self);

if (!(ctx->con)) {
rb_raise(eDuckDBError, "Database connection closed");
Expand Down Expand Up @@ -293,8 +293,7 @@ namespace {
VALUE
query_register_arrow(VALUE self, VALUE name, VALUE arrow_table)
{
rubyDuckDBConnection *ctx;
Data_Get_Struct(self, rubyDuckDBConnection, ctx);
auto ctx = get_struct_connection(self);

if (!(ctx->con)) {
rb_raise(eDuckDBError, "Database connection closed");
Expand Down Expand Up @@ -329,8 +328,7 @@ namespace {
VALUE
prepared_statement_execute_arrow(VALUE self)
{
rubyDuckDBPreparedStatement *ctx;
Data_Get_Struct(self, rubyDuckDBPreparedStatement, ctx);
auto ctx = get_struct_prepared_statement(self);

ID id_new;
CONST_ID(id_new, "new");
Expand Down
2 changes: 1 addition & 1 deletion test/test-connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def setup
result = @connection.query("SELECT * FROM users WHERE name = ?",
'alice',
output: :arrow)
assert_equal([Arrow::RecordBatch.new("string" => ["alice"])],
assert_equal([Arrow::RecordBatch.new("name" => ["alice"])],
result.to_a)
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/test-prepared-statement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def setup

test("#execute_arrow") do
@prepared_statement.bind(1, "alice")
assert_equal([Arrow::RecordBatch.new("string" => ["alice"])],
assert_equal([Arrow::RecordBatch.new("name" => ["alice"])],
@prepared_statement.execute_arrow.to_a)
end
end

0 comments on commit 3b01aee

Please sign in to comment.