duckdb 2.0.0.1 → 2.0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +23 -0
- data/ext/duckdb/aggregate_function.c +2 -2
- data/ext/duckdb/aggregate_function_set.c +2 -2
- data/ext/duckdb/appender.c +9 -10
- data/ext/duckdb/arrow_array_stream.c +2 -2
- data/ext/duckdb/arrow_import.c +2 -2
- data/ext/duckdb/client_context.c +2 -2
- data/ext/duckdb/column.c +2 -2
- data/ext/duckdb/config.c +2 -2
- data/ext/duckdb/connection.c +3 -2
- data/ext/duckdb/{conveter.c → converter.c} +0 -6
- data/ext/duckdb/converter.h +0 -1
- data/ext/duckdb/data_chunk.c +171 -26
- data/ext/duckdb/data_chunk.h +12 -0
- data/ext/duckdb/database.c +2 -2
- data/ext/duckdb/expression.c +2 -2
- data/ext/duckdb/extracted_statements.c +3 -3
- data/ext/duckdb/instance_cache.c +3 -2
- data/ext/duckdb/logical_type.c +2 -2
- data/ext/duckdb/memory_helper.c +17 -4
- data/ext/duckdb/pending_result.c +3 -2
- data/ext/duckdb/prepared_statement.c +2 -2
- data/ext/duckdb/result.c +0 -422
- data/ext/duckdb/result.h +0 -1
- data/ext/duckdb/ruby-duckdb.h +2 -1
- data/ext/duckdb/scalar_function.c +4 -2
- data/ext/duckdb/scalar_function_bind_info.c +37 -8
- data/ext/duckdb/scalar_function_bind_info.h +1 -0
- data/ext/duckdb/scalar_function_set.c +2 -2
- data/ext/duckdb/table_description.c +2 -2
- data/ext/duckdb/table_function.c +41 -2
- data/ext/duckdb/table_function_bind_info.c +30 -15
- data/ext/duckdb/table_function_function_info.c +38 -4
- data/ext/duckdb/table_function_init_info.c +72 -13
- data/ext/duckdb/table_function_init_info.h +1 -0
- data/ext/duckdb/value.c +13 -159
- data/ext/duckdb/vector.c +48 -13
- data/ext/duckdb/vector.h +5 -1
- data/ext/duckdb/vector_reader.c +544 -0
- data/ext/duckdb/vector_reader.h +7 -0
- data/ext/duckdb/{function_vector.c → vector_writer.c} +68 -7
- data/ext/duckdb/{function_vector.h → vector_writer.h} +14 -6
- data/lib/duckdb/appender.rb +27 -31
- data/lib/duckdb/data_chunk.rb +4 -89
- data/lib/duckdb/prepared_statement.rb +14 -39
- data/lib/duckdb/ruby_type_inference.rb +91 -0
- data/lib/duckdb/version.rb +1 -1
- data/lib/duckdb.rb +1 -0
- metadata +7 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 41eff0b516d375e179f86a89b6ac4d50491a6d0058dd5fe99a402ad530ea933b
|
|
4
|
+
data.tar.gz: 7fc4aeb658f10976472668fbc9376d73225ec248b2c4a303f31c3b411c00913c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: af4a05a9eaae6a15cc76d8c22a30a6d51e366f285db5a0255c659ff7b18af3e8db14bf5f4457cda09826d3f50e61b48ecbeaa950dcf38e27340494fc18b966c6
|
|
7
|
+
data.tar.gz: 8eaa3d9d8c5377c24353bb568b2d1925871e350e2cacff76f1afb7cc2535e2231858708d84419ddb92e0914eeb4a77fe0905ecd5c9eb70bbe9ef8dcf6631c210
|
data/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,29 @@ All notable changes to this project will be documented in this file.
|
|
|
4
4
|
|
|
5
5
|
# Unreleased
|
|
6
6
|
|
|
7
|
+
# 2.0.1.0 - 2026-11-27
|
|
8
|
+
- bump up DuckDB 2.0.1 on CI.
|
|
9
|
+
- bump Ruby to 3.3.14, 3.4.13 and 4.0.10 on CI.
|
|
10
|
+
- drop x64-mingw32 (MSVCRT) Ruby from Windows CI and Gemfile.lock platforms.
|
|
11
|
+
- fix use-after-free when `DuckDB::TableFunction::BindInfo`, `DuckDB::ScalarFunction::BindInfo`, or the `DuckDB::DataChunk` (and its `DuckDB::Vector`s) passed to `TableFunction#execute` is kept and used after its callback returns. Such use now raises `DuckDB::Error`. A `DuckDB::Vector` also keeps its `DataChunk` alive and raises `DuckDB::Error` once the chunk is re-initialized (issue #1559).
|
|
12
|
+
- add `DuckDB::TableFunction::InitInfo#init_data=` to store an arbitrary Ruby object as a custom table function's global init data (issue #1554).
|
|
13
|
+
- add `DuckDB::TableFunction::FunctionInfo#init_data` to retrieve, during execution, the object stored by `InitInfo#init_data=` (issue #1555).
|
|
14
|
+
- add `DuckDB::TableFunction#supports_projection_pushdown=` to enable projection pushdown. When enabled, the output chunk passed to the execute callback holds only the columns referenced by the query, as reported by `InitInfo#column_count` / `InitInfo#column_index` (issue #1539).
|
|
15
|
+
- fix BIGNUM values in query results (and scalar/aggregate function arguments) being returned as `nil` with an "Unknown type" warning; they are now converted to `Integer` (issue #1545).
|
|
16
|
+
- `DuckDB::Value#to_ruby` (and table function parameters / `Expression#fold`) now returns VARCHAR values as UTF-8 `String`, the same as query results (issue #1545).
|
|
17
|
+
- fix ENUM values with more than 255 members being returned as `nil` in query results, function arguments and `DuckDB::Value#to_ruby` (issue #1545).
|
|
18
|
+
- STRUCT field names containing non-ASCII characters are now returned as UTF-8 `Symbol`s in query results and `DuckDB::Value#to_ruby` (issue #1545).
|
|
19
|
+
- internal: move the DuckDB vector → Ruby conversion out of `result.c` into `vector_reader.c`, make `DuckDB::Value#to_ruby` use it for every type, and rename `conveter.c` to `converter.c` (issue #1545).
|
|
20
|
+
- `DuckDB::DataChunk#set_value` (TableFunction output, `Appender#append_data_chunk`) now uses the same C writer as ScalarFunction/AggregateFunction return values, so it accepts the same types: HUGEINT, UHUGEINT, UUID, DECIMAL, TIME, TIME_NS, TIME_TZ, INTERVAL and TIMESTAMP_S/MS/NS are newly supported. It is also about 4x faster and raises `IndexError` for an out-of-range column or row index instead of writing out of bounds (issue #1543).
|
|
21
|
+
- ScalarFunction/AggregateFunction return values for a DATE column accept any object that responds to `year`, `month` and `day`, not only `Date` (issue #1543).
|
|
22
|
+
- fix TIMESTAMP_NS return values of ScalarFunction dropping the sub-microsecond part of the `Time` (issue #1543).
|
|
23
|
+
- internal: rename `function_vector.c` to `vector_writer.c` (issue #1543).
|
|
24
|
+
- internal: move the "untyped Ruby object -> DuckDB type" decision tables of `PreparedStatement#bind` and `Appender#append` into one private module, `DuckDB::RubyTypeInference` (`Appender#append` keeps a fast path for Integer/String), with their current differences marked. No behaviour change (issue #1544).
|
|
25
|
+
|
|
26
|
+
## Breaking Changes
|
|
27
|
+
- `DuckDB::DataChunk#set_value` for a TIMESTAMP column now stores the wall-clock components of the `Time` as-is (like `bind_timestamp`, `Appender#append_timestamp` and ScalarFunction), instead of converting it to the local time zone first. `Time.utc(2024, 1, 1, 0, 0, 0)` is stored as `2024-01-01 00:00:00` regardless of `TZ` (issue #1543).
|
|
28
|
+
- ScalarFunction/AggregateFunction return values for a TIMESTAMP_TZ column now honor the `Time`'s `utc_offset` (store the instant, like `DataChunk#set_value` and `bind_timestamp_tz`). Previously the wall-clock components were stored as if they were UTC (issue #1543).
|
|
29
|
+
|
|
7
30
|
# 2.0.0.1 - 2026-10-25
|
|
8
31
|
- `DuckDB::PreparedStatement#bind_decimal` (and `#bind` with a `BigDecimal`) raises `ArgumentError` when the DECIMAL width is out of range (1..38), instead of aborting the process or binding a wrong value.
|
|
9
32
|
- raise `RangeError` instead of aborting the process when a result contains a TIMESTAMP value before `-9223372022400000000` microseconds (near INT64_MIN).
|
|
@@ -103,8 +103,8 @@ static void compact(void *ctx) {
|
|
|
103
103
|
}
|
|
104
104
|
|
|
105
105
|
static VALUE allocate(VALUE klass) {
|
|
106
|
-
rubyDuckDBAggregateFunction *ctx
|
|
107
|
-
return
|
|
106
|
+
rubyDuckDBAggregateFunction *ctx;
|
|
107
|
+
return TypedData_Make_Struct(klass, rubyDuckDBAggregateFunction, &aggregate_function_data_type, ctx);
|
|
108
108
|
}
|
|
109
109
|
|
|
110
110
|
static size_t memsize(const void *p) {
|
|
@@ -33,8 +33,8 @@ static void compact(void *ctx) {
|
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
static VALUE allocate(VALUE klass) {
|
|
36
|
-
rubyDuckDBAggregateFunctionSet *ctx
|
|
37
|
-
VALUE obj =
|
|
36
|
+
rubyDuckDBAggregateFunctionSet *ctx;
|
|
37
|
+
VALUE obj = TypedData_Make_Struct(klass, rubyDuckDBAggregateFunctionSet, &aggregate_function_set_data_type, ctx);
|
|
38
38
|
RB_OBJ_WRITE(obj, &ctx->functions, rb_ary_new());
|
|
39
39
|
return obj;
|
|
40
40
|
}
|
data/ext/duckdb/appender.c
CHANGED
|
@@ -74,8 +74,8 @@ static void deallocate(void * ctx) {
|
|
|
74
74
|
}
|
|
75
75
|
|
|
76
76
|
static VALUE allocate(VALUE klass) {
|
|
77
|
-
rubyDuckDBAppender *ctx
|
|
78
|
-
return
|
|
77
|
+
rubyDuckDBAppender *ctx;
|
|
78
|
+
return TypedData_Make_Struct(klass, rubyDuckDBAppender, &appender_data_type, ctx);
|
|
79
79
|
}
|
|
80
80
|
|
|
81
81
|
static duckdb_appender appender_of(VALUE self) {
|
|
@@ -439,20 +439,19 @@ static VALUE appender__append_value(VALUE self, VALUE val) {
|
|
|
439
439
|
|
|
440
440
|
/* :nodoc: */
|
|
441
441
|
static VALUE appender__append_data_chunk(VALUE self, VALUE chunk) {
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
chunk_ctx = rbduckdb_get_struct_data_chunk(chunk);
|
|
442
|
+
duckdb_data_chunk data_chunk = rbduckdb_get_valid_data_chunk(chunk);
|
|
445
443
|
|
|
446
|
-
return state_to_rbool(duckdb_append_data_chunk(appender_of(self),
|
|
444
|
+
return state_to_rbool(duckdb_append_data_chunk(appender_of(self), data_chunk));
|
|
447
445
|
}
|
|
448
446
|
|
|
449
447
|
/* :nodoc: */
|
|
450
448
|
static VALUE appender__append_default_to_chunk(VALUE self, VALUE chunk, VALUE col, VALUE row) {
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
449
|
+
/* Convert arguments first: to_int may run Ruby code. */
|
|
450
|
+
idx_t col_idx = NUM2ULL(col);
|
|
451
|
+
idx_t row_idx = NUM2ULL(row);
|
|
452
|
+
duckdb_data_chunk data_chunk = rbduckdb_get_valid_data_chunk(chunk);
|
|
454
453
|
|
|
455
|
-
return state_to_rbool(duckdb_append_default_to_chunk(appender_of(self),
|
|
454
|
+
return state_to_rbool(duckdb_append_default_to_chunk(appender_of(self), data_chunk, col_idx, row_idx));
|
|
456
455
|
}
|
|
457
456
|
|
|
458
457
|
/* :nodoc: */
|
|
@@ -54,8 +54,8 @@ static void deallocate(void *ctx) {
|
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
static VALUE allocate(VALUE klass) {
|
|
57
|
-
rubyDuckDBArrowArrayStream *ctx
|
|
58
|
-
return
|
|
57
|
+
rubyDuckDBArrowArrayStream *ctx;
|
|
58
|
+
return TypedData_Make_Struct(klass, rubyDuckDBArrowArrayStream, &arrow_array_stream_data_type, ctx);
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
static size_t memsize(const void *p) {
|
data/ext/duckdb/arrow_import.c
CHANGED
|
@@ -39,8 +39,8 @@ static void deallocate(void *ctx) {
|
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
static VALUE allocate(VALUE klass) {
|
|
42
|
-
rubyDuckDBArrowConvertedSchema *ctx
|
|
43
|
-
return
|
|
42
|
+
rubyDuckDBArrowConvertedSchema *ctx;
|
|
43
|
+
return TypedData_Make_Struct(klass, rubyDuckDBArrowConvertedSchema, &arrow_converted_schema_data_type, ctx);
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
static size_t memsize(const void *p) {
|
data/ext/duckdb/client_context.c
CHANGED
|
@@ -24,8 +24,8 @@ static void deallocate(void *ctx) {
|
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
static VALUE allocate(VALUE klass) {
|
|
27
|
-
rubyDuckDBClientContext *ctx
|
|
28
|
-
return
|
|
27
|
+
rubyDuckDBClientContext *ctx;
|
|
28
|
+
return TypedData_Make_Struct(klass, rubyDuckDBClientContext, &client_context_data_type, ctx);
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
static size_t memsize(const void *p) {
|
data/ext/duckdb/column.c
CHANGED
|
@@ -22,8 +22,8 @@ static void deallocate(void *ctx) {
|
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
static VALUE allocate(VALUE klass) {
|
|
25
|
-
rubyDuckDBColumn *ctx
|
|
26
|
-
return
|
|
25
|
+
rubyDuckDBColumn *ctx;
|
|
26
|
+
return TypedData_Make_Struct(klass, rubyDuckDBColumn, &column_data_type, ctx);
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
static size_t memsize(const void *p) {
|
data/ext/duckdb/config.c
CHANGED
|
@@ -24,8 +24,8 @@ static void deallocate(void * ctx) {
|
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
static VALUE allocate(VALUE klass) {
|
|
27
|
-
rubyDuckDBConfig *ctx
|
|
28
|
-
return
|
|
27
|
+
rubyDuckDBConfig *ctx;
|
|
28
|
+
return TypedData_Make_Struct(klass, rubyDuckDBConfig, &config_data_type, ctx);
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
static size_t memsize(const void *p) {
|
data/ext/duckdb/connection.c
CHANGED
|
@@ -49,9 +49,10 @@ static void compact(void *ctx) {
|
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
static VALUE allocate(VALUE klass) {
|
|
52
|
-
rubyDuckDBConnection *ctx
|
|
52
|
+
rubyDuckDBConnection *ctx;
|
|
53
|
+
VALUE obj = TypedData_Make_Struct(klass, rubyDuckDBConnection, &connection_data_type, ctx);
|
|
53
54
|
ctx->database = Qnil;
|
|
54
|
-
return
|
|
55
|
+
return obj;
|
|
55
56
|
}
|
|
56
57
|
|
|
57
58
|
/* Anchors obj to this connection's database so it outlives the connection. */
|
|
@@ -95,12 +95,6 @@ VALUE rbduckdb_uuid_to_ruby(duckdb_hugeint h) {
|
|
|
95
95
|
return rb_utf8_str_new(buf, 36);
|
|
96
96
|
}
|
|
97
97
|
|
|
98
|
-
VALUE rbduckdb_uuid_uhugeint_to_ruby(duckdb_uhugeint h) {
|
|
99
|
-
char buf[36];
|
|
100
|
-
uuid_to_str(h.upper, h.lower, buf);
|
|
101
|
-
return rb_utf8_str_new(buf, 36);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
98
|
// Returns the nibble value (0-15) for a hex character, or -1 if invalid.
|
|
105
99
|
static int hex_nibble(unsigned char c)
|
|
106
100
|
{
|
data/ext/duckdb/converter.h
CHANGED
|
@@ -17,7 +17,6 @@ extern ID id__to_infinity;
|
|
|
17
17
|
extern ID id__decimal_to_unscaled;
|
|
18
18
|
|
|
19
19
|
VALUE rbduckdb_uuid_to_ruby(duckdb_hugeint h);
|
|
20
|
-
VALUE rbduckdb_uuid_uhugeint_to_ruby(duckdb_uhugeint h);
|
|
21
20
|
void rbduckdb_uuid_str_to_hugeint(VALUE uuid_str, duckdb_hugeint *out);
|
|
22
21
|
void rbduckdb_uuid_str_to_uhugeint(VALUE uuid_str, duckdb_uhugeint *out);
|
|
23
22
|
VALUE rbduckdb_interval_to_ruby(duckdb_interval i);
|
data/ext/duckdb/data_chunk.c
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
#include "ruby-duckdb.h"
|
|
2
2
|
|
|
3
3
|
VALUE cDuckDBDataChunk;
|
|
4
|
-
extern VALUE cDuckDBVector;
|
|
5
4
|
|
|
6
5
|
static void deallocate(void *ctx);
|
|
7
6
|
static VALUE allocate(VALUE klass);
|
|
@@ -12,6 +11,9 @@ static VALUE data_chunk_size(VALUE self);
|
|
|
12
11
|
static VALUE data_chunk_set_size(VALUE self, VALUE size);
|
|
13
12
|
static VALUE data_chunk_get_vector(VALUE self, VALUE col_idx);
|
|
14
13
|
static VALUE data_chunk__reset(VALUE self);
|
|
14
|
+
static VALUE data_chunk_set_value(VALUE self, VALUE col_idx, VALUE row_idx, VALUE value);
|
|
15
|
+
static void free_column_types(rubyDuckDBDataChunk *ctx);
|
|
16
|
+
static duckdb_logical_type column_type(rubyDuckDBDataChunk *ctx, idx_t col_idx);
|
|
15
17
|
|
|
16
18
|
static const rb_data_type_t data_chunk_data_type = {
|
|
17
19
|
"DuckDB/DataChunk",
|
|
@@ -19,9 +21,26 @@ static const rb_data_type_t data_chunk_data_type = {
|
|
|
19
21
|
0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
|
|
20
22
|
};
|
|
21
23
|
|
|
24
|
+
static void free_column_types(rubyDuckDBDataChunk *ctx) {
|
|
25
|
+
idx_t i;
|
|
26
|
+
|
|
27
|
+
if (ctx->column_types == NULL) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
for (i = 0; i < ctx->column_types_count; i++) {
|
|
31
|
+
if (ctx->column_types[i] != NULL) {
|
|
32
|
+
duckdb_destroy_logical_type(&(ctx->column_types[i]));
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
xfree(ctx->column_types);
|
|
36
|
+
ctx->column_types = NULL;
|
|
37
|
+
ctx->column_types_count = 0;
|
|
38
|
+
}
|
|
39
|
+
|
|
22
40
|
static void deallocate(void *ctx) {
|
|
23
41
|
rubyDuckDBDataChunk *p = (rubyDuckDBDataChunk *)ctx;
|
|
24
42
|
|
|
43
|
+
free_column_types(p);
|
|
25
44
|
if (p->owned && p->data_chunk) {
|
|
26
45
|
duckdb_destroy_data_chunk(&(p->data_chunk));
|
|
27
46
|
}
|
|
@@ -30,12 +49,13 @@ static void deallocate(void *ctx) {
|
|
|
30
49
|
}
|
|
31
50
|
|
|
32
51
|
static VALUE allocate(VALUE klass) {
|
|
33
|
-
rubyDuckDBDataChunk *ctx
|
|
34
|
-
return
|
|
52
|
+
rubyDuckDBDataChunk *ctx;
|
|
53
|
+
return TypedData_Make_Struct(klass, rubyDuckDBDataChunk, &data_chunk_data_type, ctx);
|
|
35
54
|
}
|
|
36
55
|
|
|
37
56
|
static size_t memsize(const void *p) {
|
|
38
|
-
|
|
57
|
+
const rubyDuckDBDataChunk *ctx = p;
|
|
58
|
+
return sizeof(rubyDuckDBDataChunk) + ctx->column_types_count * sizeof(duckdb_logical_type);
|
|
39
59
|
}
|
|
40
60
|
|
|
41
61
|
rubyDuckDBDataChunk *rbduckdb_get_struct_data_chunk(VALUE obj) {
|
|
@@ -44,6 +64,61 @@ rubyDuckDBDataChunk *rbduckdb_get_struct_data_chunk(VALUE obj) {
|
|
|
44
64
|
return ctx;
|
|
45
65
|
}
|
|
46
66
|
|
|
67
|
+
/*
|
|
68
|
+
* Returns the struct for +self+, raising DuckDB::Error if it does not hold a
|
|
69
|
+
* usable chunk: either it was never initialized, or it was the output chunk
|
|
70
|
+
* of a TableFunction execute callback that has already returned.
|
|
71
|
+
*/
|
|
72
|
+
static rubyDuckDBDataChunk *get_valid_data_chunk(VALUE self) {
|
|
73
|
+
rubyDuckDBDataChunk *ctx;
|
|
74
|
+
|
|
75
|
+
TypedData_Get_Struct(self, rubyDuckDBDataChunk, &data_chunk_data_type, ctx);
|
|
76
|
+
if (ctx->data_chunk == NULL) {
|
|
77
|
+
rb_raise(eDuckDBError, "DuckDB::DataChunk is not available (it is not initialized, or it was used after the table function execute callback returned)");
|
|
78
|
+
}
|
|
79
|
+
return ctx;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
duckdb_data_chunk rbduckdb_get_valid_data_chunk(VALUE obj) {
|
|
83
|
+
return get_valid_data_chunk(obj)->data_chunk;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/*
|
|
87
|
+
* Releases the chunk held by +ctx+ (destroying it only if owned) and bumps
|
|
88
|
+
* the generation so that Vectors obtained from it become invalid.
|
|
89
|
+
*/
|
|
90
|
+
static void detach_data_chunk(rubyDuckDBDataChunk *ctx) {
|
|
91
|
+
free_column_types(ctx);
|
|
92
|
+
if (ctx->owned && ctx->data_chunk) {
|
|
93
|
+
duckdb_destroy_data_chunk(&(ctx->data_chunk));
|
|
94
|
+
}
|
|
95
|
+
ctx->data_chunk = NULL;
|
|
96
|
+
ctx->owned = false;
|
|
97
|
+
ctx->generation++;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/*
|
|
101
|
+
* Detaches +obj+ from its DuckDB chunk. Used for the output chunk passed to a
|
|
102
|
+
* TableFunction execute callback, which DuckDB owns and which is only valid
|
|
103
|
+
* while the callback runs. Vectors obtained from +obj+ become invalid too.
|
|
104
|
+
*/
|
|
105
|
+
void rbduckdb_data_chunk_invalidate(VALUE obj) {
|
|
106
|
+
rubyDuckDBDataChunk *ctx;
|
|
107
|
+
|
|
108
|
+
TypedData_Get_Struct(obj, rubyDuckDBDataChunk, &data_chunk_data_type, ctx);
|
|
109
|
+
detach_data_chunk(ctx);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/*
|
|
113
|
+
* Returns true if +obj+ still holds the same chunk it held at +generation+.
|
|
114
|
+
*/
|
|
115
|
+
bool rbduckdb_data_chunk_is_current(VALUE obj, uint64_t generation) {
|
|
116
|
+
rubyDuckDBDataChunk *ctx;
|
|
117
|
+
|
|
118
|
+
TypedData_Get_Struct(obj, rubyDuckDBDataChunk, &data_chunk_data_type, ctx);
|
|
119
|
+
return ctx->data_chunk != NULL && ctx->generation == generation;
|
|
120
|
+
}
|
|
121
|
+
|
|
47
122
|
VALUE rbduckdb_create_data_chunk(duckdb_data_chunk chunk, bool owned) {
|
|
48
123
|
VALUE obj = allocate(cDuckDBDataChunk);
|
|
49
124
|
rubyDuckDBDataChunk *ctx;
|
|
@@ -70,18 +145,23 @@ static VALUE data_chunk_initialize(int argc, VALUE *argv, VALUE self) {
|
|
|
70
145
|
|
|
71
146
|
Check_Type(logical_types, T_ARRAY);
|
|
72
147
|
|
|
73
|
-
if (ctx->
|
|
74
|
-
|
|
75
|
-
ctx->owned = false;
|
|
148
|
+
if (ctx->data_chunk && !ctx->owned) {
|
|
149
|
+
rb_raise(eDuckDBError, "cannot re-initialize a DuckDB::DataChunk owned by DuckDB");
|
|
76
150
|
}
|
|
77
151
|
|
|
152
|
+
/* Validate every element before touching the current chunk or allocating. */
|
|
153
|
+
for (i = 0; i < RARRAY_LEN(logical_types); i++) {
|
|
154
|
+
rbduckdb_get_struct_logical_type(rb_ary_entry(logical_types, i));
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/* Vectors from the previous chunk must not be used any more. */
|
|
158
|
+
detach_data_chunk(ctx);
|
|
159
|
+
|
|
78
160
|
column_count = (idx_t)RARRAY_LEN(logical_types);
|
|
79
161
|
types = ALLOC_N(duckdb_logical_type, column_count);
|
|
80
162
|
|
|
81
|
-
for (i = 0; i <
|
|
82
|
-
|
|
83
|
-
rubyDuckDBLogicalType *logical_type_ctx = rbduckdb_get_struct_logical_type(logical_type);
|
|
84
|
-
types[i] = logical_type_ctx->logical_type;
|
|
163
|
+
for (i = 0; i < (long)column_count; i++) {
|
|
164
|
+
types[i] = rbduckdb_get_struct_logical_type(rb_ary_entry(logical_types, i))->logical_type;
|
|
85
165
|
}
|
|
86
166
|
|
|
87
167
|
ctx->data_chunk = duckdb_create_data_chunk(types, column_count);
|
|
@@ -108,7 +188,7 @@ static VALUE data_chunk_column_count(VALUE self) {
|
|
|
108
188
|
rubyDuckDBDataChunk *ctx;
|
|
109
189
|
idx_t count;
|
|
110
190
|
|
|
111
|
-
|
|
191
|
+
ctx = get_valid_data_chunk(self);
|
|
112
192
|
|
|
113
193
|
count = duckdb_data_chunk_get_column_count(ctx->data_chunk);
|
|
114
194
|
|
|
@@ -127,7 +207,7 @@ static VALUE data_chunk_size(VALUE self) {
|
|
|
127
207
|
rubyDuckDBDataChunk *ctx;
|
|
128
208
|
idx_t size;
|
|
129
209
|
|
|
130
|
-
|
|
210
|
+
ctx = get_valid_data_chunk(self);
|
|
131
211
|
|
|
132
212
|
size = duckdb_data_chunk_get_size(ctx->data_chunk);
|
|
133
213
|
|
|
@@ -146,9 +226,9 @@ static VALUE data_chunk_set_size(VALUE self, VALUE size) {
|
|
|
146
226
|
rubyDuckDBDataChunk *ctx;
|
|
147
227
|
idx_t sz;
|
|
148
228
|
|
|
149
|
-
|
|
150
|
-
|
|
229
|
+
/* Convert arguments first: to_int may run Ruby code. */
|
|
151
230
|
sz = NUM2ULL(size);
|
|
231
|
+
ctx = get_valid_data_chunk(self);
|
|
152
232
|
duckdb_data_chunk_set_size(ctx->data_chunk, sz);
|
|
153
233
|
|
|
154
234
|
return size;
|
|
@@ -161,25 +241,27 @@ static VALUE data_chunk_set_size(VALUE self, VALUE size) {
|
|
|
161
241
|
* Gets the vector at the specified column index.
|
|
162
242
|
*
|
|
163
243
|
* vector = data_chunk.get_vector(0)
|
|
244
|
+
*
|
|
245
|
+
* Raises IndexError if +col_idx+ is out of range. The returned vector raises
|
|
246
|
+
* DuckDB::Error once the data chunk is no longer valid.
|
|
164
247
|
*/
|
|
165
248
|
static VALUE data_chunk_get_vector(VALUE self, VALUE col_idx) {
|
|
166
249
|
rubyDuckDBDataChunk *ctx;
|
|
167
250
|
idx_t idx;
|
|
168
251
|
duckdb_vector vector;
|
|
169
|
-
VALUE vector_obj;
|
|
170
|
-
rubyDuckDBVector *vector_ctx;
|
|
171
|
-
|
|
172
|
-
TypedData_Get_Struct(self, rubyDuckDBDataChunk, &data_chunk_data_type, ctx);
|
|
173
252
|
|
|
253
|
+
/* Convert arguments first: to_int may run Ruby code. */
|
|
254
|
+
if (NUM2LL(col_idx) < 0) {
|
|
255
|
+
rb_raise(rb_eIndexError, "column index %"PRIsVALUE" out of range", rb_inspect(col_idx));
|
|
256
|
+
}
|
|
174
257
|
idx = NUM2ULL(col_idx);
|
|
258
|
+
ctx = get_valid_data_chunk(self);
|
|
259
|
+
if (idx >= duckdb_data_chunk_get_column_count(ctx->data_chunk)) {
|
|
260
|
+
rb_raise(rb_eIndexError, "column index %llu out of range", (unsigned long long)idx);
|
|
261
|
+
}
|
|
175
262
|
vector = duckdb_data_chunk_get_vector(ctx->data_chunk, idx);
|
|
176
263
|
|
|
177
|
-
|
|
178
|
-
vector_obj = rb_class_new_instance(0, NULL, cDuckDBVector);
|
|
179
|
-
vector_ctx = rbduckdb_get_struct_vector(vector_obj);
|
|
180
|
-
vector_ctx->vector = vector;
|
|
181
|
-
|
|
182
|
-
return vector_obj;
|
|
264
|
+
return rbduckdb_vector_new(self, ctx->generation, vector);
|
|
183
265
|
}
|
|
184
266
|
|
|
185
267
|
/*
|
|
@@ -193,13 +275,75 @@ static VALUE data_chunk_get_vector(VALUE self, VALUE col_idx) {
|
|
|
193
275
|
static VALUE data_chunk__reset(VALUE self) {
|
|
194
276
|
rubyDuckDBDataChunk *ctx;
|
|
195
277
|
|
|
196
|
-
|
|
278
|
+
ctx = get_valid_data_chunk(self);
|
|
197
279
|
|
|
198
280
|
duckdb_data_chunk_reset(ctx->data_chunk);
|
|
199
281
|
|
|
200
282
|
return self;
|
|
201
283
|
}
|
|
202
284
|
|
|
285
|
+
static duckdb_logical_type column_type(rubyDuckDBDataChunk *ctx, idx_t col_idx) {
|
|
286
|
+
idx_t column_count;
|
|
287
|
+
|
|
288
|
+
if (ctx->column_types == NULL) {
|
|
289
|
+
column_count = duckdb_data_chunk_get_column_count(ctx->data_chunk);
|
|
290
|
+
ctx->column_types = ALLOC_N(duckdb_logical_type, column_count);
|
|
291
|
+
MEMZERO(ctx->column_types, duckdb_logical_type, column_count);
|
|
292
|
+
ctx->column_types_count = column_count;
|
|
293
|
+
}
|
|
294
|
+
if (ctx->column_types[col_idx] == NULL) {
|
|
295
|
+
ctx->column_types[col_idx] = duckdb_vector_get_column_type(duckdb_data_chunk_get_vector(ctx->data_chunk, col_idx));
|
|
296
|
+
}
|
|
297
|
+
return ctx->column_types[col_idx];
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/*
|
|
301
|
+
* call-seq:
|
|
302
|
+
* data_chunk.set_value(col_idx, row_idx, value) -> value
|
|
303
|
+
*
|
|
304
|
+
* Sets a value at the specified column and row index.
|
|
305
|
+
* The value is converted according to the column's logical type, the
|
|
306
|
+
* same way a ScalarFunction return value is converted. +nil+ sets NULL.
|
|
307
|
+
*
|
|
308
|
+
* output.set_value(0, 0, 42) # column 0, row 0
|
|
309
|
+
* output.set_value(1, 0, 'Alice') # column 1, row 0
|
|
310
|
+
* output.set_value(0, 1, nil) # NULL
|
|
311
|
+
*
|
|
312
|
+
* Raises IndexError if +col_idx+ or +row_idx+ is out of range, and
|
|
313
|
+
* ArgumentError if the column type is not supported.
|
|
314
|
+
*/
|
|
315
|
+
static VALUE data_chunk_set_value(VALUE self, VALUE col_idx, VALUE row_idx, VALUE value) {
|
|
316
|
+
rubyDuckDBDataChunk *ctx;
|
|
317
|
+
idx_t col;
|
|
318
|
+
idx_t row;
|
|
319
|
+
duckdb_vector vector;
|
|
320
|
+
duckdb_logical_type type;
|
|
321
|
+
|
|
322
|
+
/* Convert arguments first: to_int may run Ruby code. */
|
|
323
|
+
if (NUM2LL(col_idx) < 0) {
|
|
324
|
+
rb_raise(rb_eIndexError, "column index %"PRIsVALUE" out of range", rb_inspect(col_idx));
|
|
325
|
+
}
|
|
326
|
+
if (NUM2LL(row_idx) < 0) {
|
|
327
|
+
rb_raise(rb_eIndexError, "row index %"PRIsVALUE" out of range", rb_inspect(row_idx));
|
|
328
|
+
}
|
|
329
|
+
col = NUM2ULL(col_idx);
|
|
330
|
+
row = NUM2ULL(row_idx);
|
|
331
|
+
ctx = get_valid_data_chunk(self);
|
|
332
|
+
if (col >= duckdb_data_chunk_get_column_count(ctx->data_chunk)) {
|
|
333
|
+
rb_raise(rb_eIndexError, "column index %llu out of range", (unsigned long long)col);
|
|
334
|
+
}
|
|
335
|
+
if (row >= duckdb_vector_size()) {
|
|
336
|
+
rb_raise(rb_eIndexError, "row index %llu out of range (vector size is %llu)",
|
|
337
|
+
(unsigned long long)row, (unsigned long long)duckdb_vector_size());
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
vector = duckdb_data_chunk_get_vector(ctx->data_chunk, col);
|
|
341
|
+
type = column_type(ctx, col);
|
|
342
|
+
rbduckdb_vector_set_value_at(vector, type, row, value);
|
|
343
|
+
|
|
344
|
+
return value;
|
|
345
|
+
}
|
|
346
|
+
|
|
203
347
|
void rbduckdb_init_data_chunk(void) {
|
|
204
348
|
#if 0
|
|
205
349
|
VALUE mDuckDB = rb_define_module("DuckDB");
|
|
@@ -212,5 +356,6 @@ void rbduckdb_init_data_chunk(void) {
|
|
|
212
356
|
rb_define_method(cDuckDBDataChunk, "size", data_chunk_size, 0);
|
|
213
357
|
rb_define_method(cDuckDBDataChunk, "size=", data_chunk_set_size, 1);
|
|
214
358
|
rb_define_method(cDuckDBDataChunk, "get_vector", data_chunk_get_vector, 1);
|
|
359
|
+
rb_define_method(cDuckDBDataChunk, "set_value", data_chunk_set_value, 3);
|
|
215
360
|
rb_define_private_method(cDuckDBDataChunk, "_reset", data_chunk__reset, 0);
|
|
216
361
|
}
|
data/ext/duckdb/data_chunk.h
CHANGED
|
@@ -4,12 +4,24 @@
|
|
|
4
4
|
struct _rubyDuckDBDataChunk {
|
|
5
5
|
duckdb_data_chunk data_chunk;
|
|
6
6
|
bool owned;
|
|
7
|
+
/* Lazily loaded column types for set_value; destroyed with the chunk. */
|
|
8
|
+
duckdb_logical_type *column_types;
|
|
9
|
+
idx_t column_types_count;
|
|
10
|
+
/*
|
|
11
|
+
* Bumped whenever data_chunk stops pointing at the chunk it pointed at
|
|
12
|
+
* before (invalidated or re-initialized), so that Vectors obtained from
|
|
13
|
+
* the old chunk can tell they are stale.
|
|
14
|
+
*/
|
|
15
|
+
uint64_t generation;
|
|
7
16
|
};
|
|
8
17
|
|
|
9
18
|
typedef struct _rubyDuckDBDataChunk rubyDuckDBDataChunk;
|
|
10
19
|
|
|
11
20
|
rubyDuckDBDataChunk *rbduckdb_get_struct_data_chunk(VALUE obj);
|
|
12
21
|
VALUE rbduckdb_create_data_chunk(duckdb_data_chunk chunk, bool owned);
|
|
22
|
+
duckdb_data_chunk rbduckdb_get_valid_data_chunk(VALUE obj);
|
|
23
|
+
void rbduckdb_data_chunk_invalidate(VALUE obj);
|
|
24
|
+
bool rbduckdb_data_chunk_is_current(VALUE obj, uint64_t generation);
|
|
13
25
|
void rbduckdb_init_data_chunk(void);
|
|
14
26
|
|
|
15
27
|
#endif
|
data/ext/duckdb/database.c
CHANGED
|
@@ -72,8 +72,8 @@ static size_t memsize(const void *p) {
|
|
|
72
72
|
}
|
|
73
73
|
|
|
74
74
|
static VALUE allocate(VALUE klass) {
|
|
75
|
-
rubyDuckDB *ctx
|
|
76
|
-
VALUE obj =
|
|
75
|
+
rubyDuckDB *ctx;
|
|
76
|
+
VALUE obj = TypedData_Make_Struct(klass, rubyDuckDB, &database_data_type, ctx);
|
|
77
77
|
ctx->cached_path = Qnil;
|
|
78
78
|
ctx->cache_wrappers = Qnil;
|
|
79
79
|
RB_OBJ_WRITE(obj, &ctx->registered_functions, rb_ary_new());
|
data/ext/duckdb/expression.c
CHANGED
|
@@ -25,8 +25,8 @@ static void deallocate(void *ctx) {
|
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
static VALUE allocate(VALUE klass) {
|
|
28
|
-
rubyDuckDBExpression *ctx
|
|
29
|
-
return
|
|
28
|
+
rubyDuckDBExpression *ctx;
|
|
29
|
+
return TypedData_Make_Struct(klass, rubyDuckDBExpression, &expression_data_type, ctx);
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
static size_t memsize(const void *p) {
|
|
@@ -29,10 +29,10 @@ static void deallocate(void *ctx) {
|
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
static VALUE allocate(VALUE klass) {
|
|
32
|
-
rubyDuckDBExtractedStatements *ctx
|
|
32
|
+
rubyDuckDBExtractedStatements *ctx;
|
|
33
|
+
VALUE obj = TypedData_Make_Struct(klass, rubyDuckDBExtractedStatements, &extract_statements_data_type, ctx);
|
|
33
34
|
ctx->num_statements = 0;
|
|
34
|
-
|
|
35
|
-
return TypedData_Wrap_Struct(klass, &extract_statements_data_type, ctx);
|
|
35
|
+
return obj;
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
static size_t memsize(const void *p) {
|
data/ext/duckdb/instance_cache.c
CHANGED
|
@@ -45,9 +45,10 @@ static size_t memsize(const void *p) {
|
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
static VALUE allocate(VALUE klass) {
|
|
48
|
-
rubyDuckDBInstanceCache *ctx
|
|
48
|
+
rubyDuckDBInstanceCache *ctx;
|
|
49
|
+
VALUE obj = TypedData_Make_Struct(klass, rubyDuckDBInstanceCache, &instance_cache_data_type, ctx);
|
|
49
50
|
ctx->wrappers = Qnil;
|
|
50
|
-
return
|
|
51
|
+
return obj;
|
|
51
52
|
}
|
|
52
53
|
|
|
53
54
|
/*
|
data/ext/duckdb/logical_type.c
CHANGED
|
@@ -55,8 +55,8 @@ rubyDuckDBLogicalType *rbduckdb_get_struct_logical_type(VALUE obj) {
|
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
static VALUE allocate(VALUE klass) {
|
|
58
|
-
rubyDuckDBLogicalType *ctx
|
|
59
|
-
return
|
|
58
|
+
rubyDuckDBLogicalType *ctx;
|
|
59
|
+
return TypedData_Make_Struct(klass, rubyDuckDBLogicalType, &logical_type_data_type, ctx);
|
|
60
60
|
}
|
|
61
61
|
|
|
62
62
|
static duckdb_logical_type logical_type_of(VALUE self) {
|
data/ext/duckdb/memory_helper.c
CHANGED
|
@@ -239,7 +239,9 @@ static VALUE memory_helper_s_write_float(VALUE self, VALUE ptr, VALUE index, VAL
|
|
|
239
239
|
* DuckDB::MemoryHelper.write_timestamp(ptr, index, value) -> nil
|
|
240
240
|
*
|
|
241
241
|
* Writes a DuckDB timestamp to raw memory.
|
|
242
|
-
* +value+ must be a Ruby Time object.
|
|
242
|
+
* +value+ must be a Ruby Time object. It is converted to the local time zone
|
|
243
|
+
* first, unlike DuckDB::DataChunk#set_value, which stores the wall-clock
|
|
244
|
+
* components of the Time as-is.
|
|
243
245
|
*
|
|
244
246
|
* ptr = vector.get_data
|
|
245
247
|
* DuckDB::MemoryHelper.write_timestamp(ptr, 0, Time.new(2024, 3, 15, 10, 30, 45))
|
|
@@ -274,7 +276,7 @@ static VALUE memory_helper_s_write_timestamp(VALUE self, VALUE ptr, VALUE index,
|
|
|
274
276
|
* DuckDB::MemoryHelper.write_timestamp_tz(ptr, 0, Time.new(2024, 3, 15, 10, 30, 45, '+00:00'))
|
|
275
277
|
*/
|
|
276
278
|
static VALUE memory_helper_s_write_timestamp_tz(VALUE self, VALUE ptr, VALUE index, VALUE value) {
|
|
277
|
-
|
|
279
|
+
duckdb_timestamp *data;
|
|
278
280
|
idx_t idx;
|
|
279
281
|
int64_t secs;
|
|
280
282
|
int32_t usecs;
|
|
@@ -285,12 +287,12 @@ static VALUE memory_helper_s_write_timestamp_tz(VALUE self, VALUE ptr, VALUE ind
|
|
|
285
287
|
rb_raise(rb_eTypeError, "Expected Time object for TIMESTAMP_TZ");
|
|
286
288
|
}
|
|
287
289
|
|
|
288
|
-
data = (
|
|
290
|
+
data = (duckdb_timestamp *)NUM2ULL(ptr);
|
|
289
291
|
idx = (idx_t)NUM2ULL(index);
|
|
290
292
|
|
|
291
293
|
secs = NUM2LL(rb_funcall(value, rb_intern("to_i"), 0));
|
|
292
294
|
usecs = NUM2INT(rb_funcall(value, rb_intern("usec"), 0));
|
|
293
|
-
data[idx].
|
|
295
|
+
data[idx].micros = secs * 1000000LL + usecs;
|
|
294
296
|
|
|
295
297
|
return Qnil;
|
|
296
298
|
}
|
|
@@ -312,6 +314,17 @@ static VALUE memory_helper_s_write_date(VALUE self, VALUE ptr, VALUE index, VALU
|
|
|
312
314
|
}
|
|
313
315
|
|
|
314
316
|
void rbduckdb_init_memory_helper(void) {
|
|
317
|
+
#if 0
|
|
318
|
+
VALUE mDuckDB = rb_define_module("DuckDB");
|
|
319
|
+
#endif
|
|
320
|
+
/*
|
|
321
|
+
* DuckDB::MemoryHelper writes primitive values directly to raw vector
|
|
322
|
+
* memory (Vector#get_data). It is a low-level escape hatch: it does no
|
|
323
|
+
* bounds checking and is not used by ruby-duckdb itself.
|
|
324
|
+
* Prefer DuckDB::DataChunk#set_value, which converts values by the
|
|
325
|
+
* column's logical type the same way ScalarFunction return values are
|
|
326
|
+
* converted.
|
|
327
|
+
*/
|
|
315
328
|
mDuckDBMemoryHelper = rb_define_module_under(mDuckDB, "MemoryHelper");
|
|
316
329
|
|
|
317
330
|
rb_define_singleton_method(mDuckDBMemoryHelper, "write_bigint", memory_helper_s_write_bigint, 3);
|