Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ All notable changes to this project will be documented in this file.
- add `DuckDB::ScalarFunction::BindInfo#set_error(message)` to report an error early at planning time (wraps `duckdb_scalar_function_bind_set_error`).
- add `DuckDB::ScalarFunction::BindInfo#get_argument(index)` to return the expression at the given argument index as a `DuckDB::Expression` object (wraps `duckdb_scalar_function_bind_get_argument`). Raises `ArgumentError` for out-of-range index.
- add `DuckDB::Expression#foldable?` to check whether an expression can be folded to a constant at query planning time (wraps `duckdb_expression_is_foldable`). Returns `true` for literals and constant arithmetic, `false` for column references and non-deterministic functions.
- add `DuckDB::LogicalType.create_map` to create a map logical type.

## Breaking changes
- rename `DuckDB::BindInfo` to `DuckDB::TableFunction::BindInfo`. `DuckDB::BindInfo` still works but emits a deprecation warning.
Expand Down
21 changes: 21 additions & 0 deletions ext/duckdb/logical_type.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ static VALUE duckdb_logical_type__get_alias(VALUE self);
static VALUE duckdb_logical_type__set_alias(VALUE self, VALUE aname);
static VALUE duckdb_logical_type_s_create_array_type(VALUE klass, VALUE child, VALUE array_size);
static VALUE duckdb_logical_type_s_create_list_type(VALUE klass, VALUE child);
static VALUE duckdb_logical_type_s_create_map_type(VALUE klass, VALUE key, VALUE value);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Align the new map factory with C-extension conventions.

The new entrypoint uses a non-rbduckdb_ symbol name and raises directly via rb_raise. Please align with the extension conventions (prefixed symbol + error helper from error.c).

Suggested rename diff (symbol prefix consistency)
-static VALUE duckdb_logical_type_s_create_map_type(VALUE klass, VALUE key, VALUE value);
+static VALUE rbduckdb_logical_type_s_create_map_type(VALUE klass, VALUE key, VALUE value);
...
-static VALUE duckdb_logical_type_s_create_map_type(VALUE klass, VALUE key, VALUE value) {
+static VALUE rbduckdb_logical_type_s_create_map_type(VALUE klass, VALUE key, VALUE value) {
...
-    rb_define_private_method(rb_singleton_class(cDuckDBLogicalType), "_create_map_type",
-                             duckdb_logical_type_s_create_map_type, 2);
+    rb_define_private_method(rb_singleton_class(cDuckDBLogicalType), "_create_map_type",
+                             rbduckdb_logical_type_s_create_map_type, 2);

As per coding guidelines ext/duckdb/**/*.c: “C symbols must be prefixed with rbduckdb_ to avoid namespace conflicts” and “Use rb_raise for errors in C extension, wrapped in error.c helpers”.

Also applies to: 481-488, 535-536

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@ext/duckdb/logical_type.c` at line 28, Rename the public symbol to follow the
extension convention (prefix with rbduckdb_)—e.g., change
duckdb_logical_type_s_create_map_type to
rbduckdb_logical_type_s_create_map_type—and replace direct rb_raise calls in
this entrypoint with the error helper exported from error.c (use that helper to
raise/format errors consistently). Also audit the other
non-prefixed/error-raising entrypoints referenced in the review (the other
logical-type factory functions near the same changes) and apply the same rename
+ error-helper replacement so all logical_type factories use rbduckdb_ prefixes
and the centralized error helper.

static VALUE initialize(VALUE self, VALUE type_id_arg);

static const rb_data_type_t logical_type_data_type = {
Expand Down Expand Up @@ -471,6 +472,24 @@ static VALUE duckdb_logical_type_s_create_list_type(VALUE klass, VALUE child) {
return rbduckdb_create_logical_type(new_type);
}

/*
* call-seq:
* DuckDB::LogicalType._create_map_type(key_type, value_type) -> DuckDB::LogicalType
*
* Return a map logical type from the given key and value logical types.
*/
static VALUE duckdb_logical_type_s_create_map_type(VALUE klass, VALUE key, VALUE value) {
rubyDuckDBLogicalType *key_ctx = get_struct_logical_type(key);
rubyDuckDBLogicalType *value_ctx = get_struct_logical_type(value);
duckdb_logical_type new_type = duckdb_create_map_type(key_ctx->logical_type, value_ctx->logical_type);

if (!new_type) {
rb_raise(eDuckDBError, "Failed to create map type");
}

return rbduckdb_create_logical_type(new_type);
}

VALUE rbduckdb_create_logical_type(duckdb_logical_type logical_type) {
VALUE obj;
rubyDuckDBLogicalType *ctx;
Expand Down Expand Up @@ -513,6 +532,8 @@ void rbduckdb_init_duckdb_logical_type(void) {
duckdb_logical_type_s_create_array_type, 2);
rb_define_private_method(rb_singleton_class(cDuckDBLogicalType), "_create_list_type",
duckdb_logical_type_s_create_list_type, 1);
rb_define_private_method(rb_singleton_class(cDuckDBLogicalType), "_create_map_type",
duckdb_logical_type_s_create_map_type, 2);

rb_define_method(cDuckDBLogicalType, "initialize", initialize, 1);
}
15 changes: 15 additions & 0 deletions lib/duckdb/logical_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,21 @@ def create_list(type)
_create_list_type(LogicalType.resolve(type))
end

# Creates a map logical type with the given key and value types.
#
# The +key_type+ and +value_type+ arguments can be symbols or
# DuckDB::LogicalType instances.
#
# require 'duckdb'
#
# map_type = DuckDB::LogicalType.create_map(:integer, :varchar)
# map_type.type #=> :map
# map_type.key_type.type #=> :integer
# map_type.value_type.type #=> :varchar
def create_map(key_type, value_type)
_create_map_type(LogicalType.resolve(key_type), LogicalType.resolve(value_type))
end

private

def raise_resolve_error(symbol)
Expand Down
24 changes: 24 additions & 0 deletions test/duckdb_test/logical_type_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,30 @@ def test_s_create_list_with_invalid_arg
assert_raises(DuckDB::Error) { DuckDB::LogicalType.create_list(:nonexistent) }
end

def test_s_create_map_with_logical_type
map_type = DuckDB::LogicalType.create_map(DuckDB::LogicalType::INTEGER, DuckDB::LogicalType::VARCHAR)

assert_equal(:map, map_type.type)
assert_equal(:integer, map_type.key_type.type)
assert_equal(:varchar, map_type.value_type.type)
end

def test_s_create_map_with_symbol
map_type = DuckDB::LogicalType.create_map(:integer, :varchar)

assert_equal(:map, map_type.type)
assert_equal(:integer, map_type.key_type.type)
assert_equal(:varchar, map_type.value_type.type)
end

def test_s_create_map_with_invalid_key_type
assert_raises(DuckDB::Error) { DuckDB::LogicalType.create_map(:nonexistent, :varchar) }
end

def test_s_create_map_with_invalid_value_type
assert_raises(DuckDB::Error) { DuckDB::LogicalType.create_map(:integer, :nonexistent) }
end

def test_new_with_primitive_like_complex_type
# DUCKDB_TYPE_BIT = 29
bit_type = DuckDB::LogicalType.new(29)
Expand Down
Loading