duckdb 2.0.0.0 → 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 +28 -0
- data/ext/duckdb/aggregate_function.c +42 -80
- data/ext/duckdb/aggregate_function_set.c +2 -2
- data/ext/duckdb/appender.c +46 -134
- 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 +20 -56
- data/ext/duckdb/{conveter.c → converter.c} +37 -80
- 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/function_executor.c +214 -248
- data/ext/duckdb/function_executor.h +10 -0
- data/ext/duckdb/instance_cache.c +3 -2
- data/ext/duckdb/logical_type.c +90 -172
- data/ext/duckdb/memory_helper.c +17 -4
- data/ext/duckdb/pending_result.c +3 -2
- data/ext/duckdb/prepared_statement.c +39 -140
- data/ext/duckdb/result.c +0 -491
- data/ext/duckdb/result.h +0 -1
- data/ext/duckdb/ruby-duckdb.h +2 -1
- data/ext/duckdb/scalar_function.c +13 -71
- 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 +72 -82
- 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/util.c +53 -2
- 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} +94 -7
- data/ext/duckdb/vector_writer.h +53 -0
- data/lib/duckdb/appender.rb +27 -31
- data/lib/duckdb/data_chunk.rb +4 -89
- data/lib/duckdb/interval.rb +2 -28
- data/lib/duckdb/prepared_statement.rb +19 -39
- data/lib/duckdb/ruby_type_inference.rb +91 -0
- data/lib/duckdb/scalar_function_set.rb +1 -2
- data/lib/duckdb/version.rb +1 -1
- data/lib/duckdb.rb +1 -0
- metadata +7 -4
- data/ext/duckdb/function_vector.h +0 -27
|
@@ -10,6 +10,7 @@ static VALUE table_function_init_info_set_max_threads(VALUE self, VALUE max_thre
|
|
|
10
10
|
static VALUE table_function_init_info_column_count(VALUE self);
|
|
11
11
|
static VALUE table_function_init_info_column_index(VALUE self, VALUE index);
|
|
12
12
|
static VALUE table_function_init_info_bind_data(VALUE self);
|
|
13
|
+
static VALUE table_function_init_info_set_init_data(VALUE self, VALUE data);
|
|
13
14
|
|
|
14
15
|
static const rb_data_type_t init_info_data_type = {
|
|
15
16
|
"DuckDB/TableFunctionInitInfo",
|
|
@@ -23,8 +24,8 @@ static void deallocate(void *ctx) {
|
|
|
23
24
|
}
|
|
24
25
|
|
|
25
26
|
static VALUE allocate(VALUE klass) {
|
|
26
|
-
rubyDuckDBInitInfo *ctx
|
|
27
|
-
return
|
|
27
|
+
rubyDuckDBInitInfo *ctx;
|
|
28
|
+
return TypedData_Make_Struct(klass, rubyDuckDBInitInfo, &init_info_data_type, ctx);
|
|
28
29
|
}
|
|
29
30
|
|
|
30
31
|
static size_t memsize(const void *p) {
|
|
@@ -37,6 +38,21 @@ rubyDuckDBInitInfo *rbduckdb_get_struct_init_info(VALUE obj) {
|
|
|
37
38
|
return ctx;
|
|
38
39
|
}
|
|
39
40
|
|
|
41
|
+
/*
|
|
42
|
+
* Returns the struct for +self+, raising DuckDB::Error if the underlying
|
|
43
|
+
* DuckDB info is no longer valid (i.e. the object is used after the init
|
|
44
|
+
* callback has returned).
|
|
45
|
+
*/
|
|
46
|
+
static rubyDuckDBInitInfo *get_valid_init_info(VALUE self) {
|
|
47
|
+
rubyDuckDBInitInfo *ctx;
|
|
48
|
+
|
|
49
|
+
TypedData_Get_Struct(self, rubyDuckDBInitInfo, &init_info_data_type, ctx);
|
|
50
|
+
if (ctx->info == NULL) {
|
|
51
|
+
rb_raise(eDuckDBError, "DuckDB::TableFunction::InitInfo can only be used inside the init callback");
|
|
52
|
+
}
|
|
53
|
+
return ctx;
|
|
54
|
+
}
|
|
55
|
+
|
|
40
56
|
/*
|
|
41
57
|
* call-seq:
|
|
42
58
|
* init_info.set_error(error_message) -> self
|
|
@@ -50,7 +66,7 @@ static VALUE table_function_init_info_set_error(VALUE self, VALUE error) {
|
|
|
50
66
|
rubyDuckDBInitInfo *ctx;
|
|
51
67
|
const char *error_msg;
|
|
52
68
|
|
|
53
|
-
|
|
69
|
+
ctx = get_valid_init_info(self);
|
|
54
70
|
|
|
55
71
|
error_msg = StringValueCStr(error);
|
|
56
72
|
duckdb_init_set_error(ctx->info, error_msg);
|
|
@@ -72,7 +88,7 @@ static VALUE table_function_init_info_set_error(VALUE self, VALUE error) {
|
|
|
72
88
|
static VALUE table_function_init_info_set_max_threads(VALUE self, VALUE max_threads) {
|
|
73
89
|
rubyDuckDBInitInfo *ctx;
|
|
74
90
|
|
|
75
|
-
|
|
91
|
+
ctx = get_valid_init_info(self);
|
|
76
92
|
|
|
77
93
|
duckdb_init_set_max_threads(ctx->info, NUM2ULL(max_threads));
|
|
78
94
|
|
|
@@ -83,16 +99,22 @@ static VALUE table_function_init_info_set_max_threads(VALUE self, VALUE max_thre
|
|
|
83
99
|
* call-seq:
|
|
84
100
|
* init_info.column_count -> Integer
|
|
85
101
|
*
|
|
86
|
-
* Returns the number of
|
|
87
|
-
*
|
|
88
|
-
*
|
|
102
|
+
* Returns the number of columns DuckDB needs from this scan, including
|
|
103
|
+
* columns used only in filters (e.g. +score+ in
|
|
104
|
+
* <code>SELECT name FROM f() WHERE score > 0</code>). A column selected more
|
|
105
|
+
* than once is counted once, and a query that references no column
|
|
106
|
+
* (e.g. <code>SELECT count(*)</code>) still reports one column.
|
|
107
|
+
* With DuckDB::TableFunction#supports_projection_pushdown= set to +true+,
|
|
108
|
+
* the output chunk passed to the execute callback holds exactly these
|
|
109
|
+
* columns. Otherwise the output chunk still holds every result column added
|
|
110
|
+
* in the bind callback.
|
|
89
111
|
*
|
|
90
112
|
* init_info.column_count # => 2
|
|
91
113
|
*/
|
|
92
114
|
static VALUE table_function_init_info_column_count(VALUE self) {
|
|
93
115
|
rubyDuckDBInitInfo *ctx;
|
|
94
116
|
|
|
95
|
-
|
|
117
|
+
ctx = get_valid_init_info(self);
|
|
96
118
|
|
|
97
119
|
return ULL2NUM(duckdb_init_get_column_count(ctx->info));
|
|
98
120
|
}
|
|
@@ -102,16 +124,16 @@ static VALUE table_function_init_info_column_count(VALUE self) {
|
|
|
102
124
|
* init_info.column_index(index) -> Integer
|
|
103
125
|
*
|
|
104
126
|
* Returns the column index of the projected result column at +index+
|
|
105
|
-
* (0 <= +index+ < column_count).
|
|
106
|
-
* columns
|
|
107
|
-
* +index
|
|
127
|
+
* (0 <= +index+ < column_count), i.e. the position of that column among the
|
|
128
|
+
* columns added in the bind callback. With projection pushdown enabled, the
|
|
129
|
+
* +index+-th vector of the output chunk corresponds to this column.
|
|
108
130
|
*
|
|
109
131
|
* init_info.column_index(0) # => 0
|
|
110
132
|
*/
|
|
111
133
|
static VALUE table_function_init_info_column_index(VALUE self, VALUE index) {
|
|
112
134
|
rubyDuckDBInitInfo *ctx;
|
|
113
135
|
|
|
114
|
-
|
|
136
|
+
ctx = get_valid_init_info(self);
|
|
115
137
|
|
|
116
138
|
return ULL2NUM(duckdb_init_get_column_index(ctx->info, NUM2ULL(index)));
|
|
117
139
|
}
|
|
@@ -128,11 +150,47 @@ static VALUE table_function_init_info_column_index(VALUE self, VALUE index) {
|
|
|
128
150
|
static VALUE table_function_init_info_bind_data(VALUE self) {
|
|
129
151
|
rubyDuckDBInitInfo *ctx;
|
|
130
152
|
|
|
131
|
-
|
|
153
|
+
ctx = get_valid_init_info(self);
|
|
132
154
|
|
|
133
155
|
return rbduckdb_function_data_lookup(duckdb_init_get_bind_data(ctx->info));
|
|
134
156
|
}
|
|
135
157
|
|
|
158
|
+
/*
|
|
159
|
+
* call-seq:
|
|
160
|
+
* init_info.init_data = data
|
|
161
|
+
*
|
|
162
|
+
* Stores an arbitrary Ruby object as the table function's global init data.
|
|
163
|
+
* The same object can be retrieved during execution with
|
|
164
|
+
* DuckDB::TableFunction::FunctionInfo#init_data, and is kept alive until
|
|
165
|
+
* DuckDB frees the init data at the end of the scan. Useful for keeping scan
|
|
166
|
+
* state (e.g. a cursor or remaining rows) across execute calls.
|
|
167
|
+
*
|
|
168
|
+
* The init data is shared by every thread that executes the table function.
|
|
169
|
+
* When DuckDB::TableFunction::InitInfo#max_threads= is greater than 1, the
|
|
170
|
+
* execute callback may run on several threads at once, so guard any mutation
|
|
171
|
+
* of the shared object (e.g. with a Mutex).
|
|
172
|
+
*
|
|
173
|
+
* Must be called inside the init callback; using the InitInfo object after
|
|
174
|
+
* the callback has returned raises DuckDB::Error.
|
|
175
|
+
*
|
|
176
|
+
* init_info.init_data = { offset: 0, lock: Mutex.new }
|
|
177
|
+
*/
|
|
178
|
+
static VALUE table_function_init_info_set_init_data(VALUE self, VALUE data) {
|
|
179
|
+
rubyDuckDBInitInfo *ctx;
|
|
180
|
+
void *handle;
|
|
181
|
+
|
|
182
|
+
ctx = get_valid_init_info(self);
|
|
183
|
+
|
|
184
|
+
if (ctx->init_data_handle != NULL) {
|
|
185
|
+
rbduckdb_function_data_release(ctx->init_data_handle);
|
|
186
|
+
}
|
|
187
|
+
handle = rbduckdb_function_data_register(data);
|
|
188
|
+
ctx->init_data_handle = handle;
|
|
189
|
+
duckdb_init_set_init_data(ctx->info, handle, rbduckdb_function_data_destroy);
|
|
190
|
+
|
|
191
|
+
return self;
|
|
192
|
+
}
|
|
193
|
+
|
|
136
194
|
void rbduckdb_init_table_function_init_info(void) {
|
|
137
195
|
#if 0
|
|
138
196
|
VALUE mDuckDB = rb_define_module("DuckDB");
|
|
@@ -146,4 +204,5 @@ void rbduckdb_init_table_function_init_info(void) {
|
|
|
146
204
|
rb_define_method(cDuckDBTableFunctionInitInfo, "column_count", table_function_init_info_column_count, 0);
|
|
147
205
|
rb_define_method(cDuckDBTableFunctionInitInfo, "column_index", table_function_init_info_column_index, 1);
|
|
148
206
|
rb_define_method(cDuckDBTableFunctionInitInfo, "bind_data", table_function_init_info_bind_data, 0);
|
|
207
|
+
rb_define_method(cDuckDBTableFunctionInitInfo, "init_data=", table_function_init_info_set_init_data, 1);
|
|
149
208
|
}
|
data/ext/duckdb/util.c
CHANGED
|
@@ -1,5 +1,38 @@
|
|
|
1
1
|
#include "ruby-duckdb.h"
|
|
2
2
|
|
|
3
|
+
#define MICROS_PER_DAY 86400000000LL
|
|
4
|
+
|
|
5
|
+
/*
|
|
6
|
+
* Mirrors DuckDB's Date::IsValid. duckdb_to_date throws a C++ exception
|
|
7
|
+
* for dates it rejects, which would abort the process.
|
|
8
|
+
*/
|
|
9
|
+
static int is_valid_date(int32_t year, int32_t month, int32_t day) {
|
|
10
|
+
static const int32_t days_in_month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
|
|
11
|
+
int32_t max_day;
|
|
12
|
+
|
|
13
|
+
if (month < 1 || month > 12 || day < 1) {
|
|
14
|
+
return 0;
|
|
15
|
+
}
|
|
16
|
+
if (year < -5877641 || (year == -5877641 && (month < 6 || (month == 6 && day < 25)))) {
|
|
17
|
+
return 0;
|
|
18
|
+
}
|
|
19
|
+
if (year > 5881580 || (year == 5881580 && (month > 7 || (month == 7 && day > 10)))) {
|
|
20
|
+
return 0;
|
|
21
|
+
}
|
|
22
|
+
max_day = days_in_month[month];
|
|
23
|
+
if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)) {
|
|
24
|
+
max_day = 29;
|
|
25
|
+
}
|
|
26
|
+
return day <= max_day;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
static duckdb_date to_duckdb_date(duckdb_date_struct dt_struct) {
|
|
30
|
+
if (!is_valid_date(dt_struct.year, dt_struct.month, dt_struct.day)) {
|
|
31
|
+
rb_raise(rb_eRangeError, "date out of range: %d-%02d-%02d", dt_struct.year, dt_struct.month, dt_struct.day);
|
|
32
|
+
}
|
|
33
|
+
return duckdb_to_date(dt_struct);
|
|
34
|
+
}
|
|
35
|
+
|
|
3
36
|
duckdb_date rbduckdb_to_duckdb_date_from_value(VALUE year, VALUE month, VALUE day) {
|
|
4
37
|
duckdb_date_struct dt_struct;
|
|
5
38
|
|
|
@@ -7,7 +40,7 @@ duckdb_date rbduckdb_to_duckdb_date_from_value(VALUE year, VALUE month, VALUE da
|
|
|
7
40
|
dt_struct.month = NUM2INT(month);
|
|
8
41
|
dt_struct.day = NUM2INT(day);
|
|
9
42
|
|
|
10
|
-
return
|
|
43
|
+
return to_duckdb_date(dt_struct);
|
|
11
44
|
}
|
|
12
45
|
|
|
13
46
|
duckdb_time rbduckdb_to_duckdb_time_from_value(VALUE hour, VALUE min, VALUE sec, VALUE micros) {
|
|
@@ -47,6 +80,9 @@ duckdb_timestamp rbduckdb_to_duckdb_timestamp_from_time_value(VALUE time_obj) {
|
|
|
47
80
|
|
|
48
81
|
duckdb_timestamp rbduckdb_to_duckdb_timestamp_from_value(VALUE year, VALUE month, VALUE day, VALUE hour, VALUE min, VALUE sec, VALUE micros) {
|
|
49
82
|
duckdb_timestamp_struct timestamp_st;
|
|
83
|
+
duckdb_timestamp result;
|
|
84
|
+
int64_t days;
|
|
85
|
+
int64_t time_micros;
|
|
50
86
|
|
|
51
87
|
timestamp_st.date.year = NUM2INT(year);
|
|
52
88
|
timestamp_st.date.month = NUM2INT(month);
|
|
@@ -56,7 +92,22 @@ duckdb_timestamp rbduckdb_to_duckdb_timestamp_from_value(VALUE year, VALUE month
|
|
|
56
92
|
timestamp_st.time.sec = NUM2INT(sec);
|
|
57
93
|
timestamp_st.time.micros = NUM2INT(micros);
|
|
58
94
|
|
|
59
|
-
|
|
95
|
+
/* Mirrors DuckDB's Timestamp::TryFromDatetime; duckdb_to_timestamp throws on failure. */
|
|
96
|
+
days = to_duckdb_date(timestamp_st.date).days;
|
|
97
|
+
time_micros = duckdb_to_time(timestamp_st.time).micros;
|
|
98
|
+
if (days > INT64_MAX / MICROS_PER_DAY || days < INT64_MIN / MICROS_PER_DAY) {
|
|
99
|
+
rb_raise(rb_eRangeError, "timestamp out of range");
|
|
100
|
+
}
|
|
101
|
+
result.micros = days * MICROS_PER_DAY;
|
|
102
|
+
if ((time_micros > 0 && result.micros > INT64_MAX - time_micros) ||
|
|
103
|
+
(time_micros < 0 && result.micros < INT64_MIN - time_micros)) {
|
|
104
|
+
rb_raise(rb_eRangeError, "timestamp out of range");
|
|
105
|
+
}
|
|
106
|
+
result.micros += time_micros;
|
|
107
|
+
if (!duckdb_is_finite_timestamp(result)) {
|
|
108
|
+
rb_raise(rb_eRangeError, "timestamp out of range");
|
|
109
|
+
}
|
|
110
|
+
return result;
|
|
60
111
|
}
|
|
61
112
|
|
|
62
113
|
void rbduckdb_to_duckdb_interval_from_value(duckdb_interval* interval, VALUE months, VALUE days, VALUE micros) {
|
data/ext/duckdb/value.c
CHANGED
|
@@ -73,8 +73,8 @@ static void deallocate(void * ctx) {
|
|
|
73
73
|
}
|
|
74
74
|
|
|
75
75
|
static VALUE allocate(VALUE klass) {
|
|
76
|
-
rubyDuckDBValue *ctx
|
|
77
|
-
return
|
|
76
|
+
rubyDuckDBValue *ctx;
|
|
77
|
+
return TypedData_Make_Struct(klass, rubyDuckDBValue, &value_data_type, ctx);
|
|
78
78
|
}
|
|
79
79
|
|
|
80
80
|
static size_t memsize(const void *p) {
|
|
@@ -561,14 +561,17 @@ rubyDuckDBValue *rbduckdb_get_struct_value(VALUE obj) {
|
|
|
561
561
|
|
|
562
562
|
/*
|
|
563
563
|
* Converts a duckdb_value to Ruby by referencing it into a one-element
|
|
564
|
-
* vector and reusing the vector
|
|
565
|
-
* same Ruby object a query result produces for
|
|
566
|
-
*
|
|
564
|
+
* vector and reusing the vector reader (rbduckdb_vector_value_at), so
|
|
565
|
+
* Value#to_ruby returns the same Ruby object a query result produces for
|
|
566
|
+
* that type.
|
|
567
567
|
*/
|
|
568
568
|
static VALUE to_ruby_via_vector(duckdb_logical_type logical_type, duckdb_value val) {
|
|
569
569
|
struct vector_ref_arg arg;
|
|
570
570
|
|
|
571
571
|
arg.vec = duckdb_create_vector(logical_type, 1);
|
|
572
|
+
if (arg.vec == NULL) {
|
|
573
|
+
rb_raise(eDuckDBError, "failed to create a vector to convert the value");
|
|
574
|
+
}
|
|
572
575
|
arg.logical_type = logical_type;
|
|
573
576
|
duckdb_vector_reference_value(arg.vec, val);
|
|
574
577
|
return rb_ensure(convert_referenced_value, (VALUE)&arg, destroy_referencing_vector, (VALUE)&arg);
|
|
@@ -601,167 +604,18 @@ VALUE rbduckdb_duckdb_value_to_ruby_and_destroy(duckdb_value val) {
|
|
|
601
604
|
}
|
|
602
605
|
|
|
603
606
|
VALUE rbduckdb_duckdb_value_to_ruby(duckdb_value val) {
|
|
604
|
-
duckdb_logical_type
|
|
605
|
-
duckdb_type type_id;
|
|
606
|
-
VALUE result;
|
|
607
|
-
VALUE elem;
|
|
608
|
-
VALUE key;
|
|
609
|
-
char *str;
|
|
610
|
-
idx_t i;
|
|
611
|
-
idx_t size;
|
|
607
|
+
duckdb_logical_type type;
|
|
612
608
|
|
|
613
609
|
if (duckdb_is_null_value(val)) {
|
|
614
610
|
return Qnil;
|
|
615
611
|
}
|
|
616
612
|
|
|
617
613
|
/* logical_type from duckdb_get_value_type is borrowed and must not be destroyed */
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
switch (type_id) {
|
|
622
|
-
case DUCKDB_TYPE_BOOLEAN:
|
|
623
|
-
result = duckdb_get_bool(val) ? Qtrue : Qfalse;
|
|
624
|
-
break;
|
|
625
|
-
case DUCKDB_TYPE_TINYINT:
|
|
626
|
-
result = INT2FIX(duckdb_get_int8(val));
|
|
627
|
-
break;
|
|
628
|
-
case DUCKDB_TYPE_SMALLINT:
|
|
629
|
-
result = INT2FIX(duckdb_get_int16(val));
|
|
630
|
-
break;
|
|
631
|
-
case DUCKDB_TYPE_INTEGER:
|
|
632
|
-
result = INT2NUM(duckdb_get_int32(val));
|
|
633
|
-
break;
|
|
634
|
-
case DUCKDB_TYPE_BIGINT:
|
|
635
|
-
result = LL2NUM(duckdb_get_int64(val));
|
|
636
|
-
break;
|
|
637
|
-
case DUCKDB_TYPE_HUGEINT:
|
|
638
|
-
result = rbduckdb_hugeint_to_ruby(duckdb_get_hugeint(val));
|
|
639
|
-
break;
|
|
640
|
-
case DUCKDB_TYPE_UHUGEINT:
|
|
641
|
-
result = rbduckdb_uhugeint_to_ruby(duckdb_get_uhugeint(val));
|
|
642
|
-
break;
|
|
643
|
-
case DUCKDB_TYPE_UTINYINT:
|
|
644
|
-
result = INT2FIX(duckdb_get_uint8(val));
|
|
645
|
-
break;
|
|
646
|
-
case DUCKDB_TYPE_USMALLINT:
|
|
647
|
-
result = INT2FIX(duckdb_get_uint16(val));
|
|
648
|
-
break;
|
|
649
|
-
case DUCKDB_TYPE_UINTEGER:
|
|
650
|
-
result = UINT2NUM(duckdb_get_uint32(val));
|
|
651
|
-
break;
|
|
652
|
-
case DUCKDB_TYPE_UBIGINT:
|
|
653
|
-
result = ULL2NUM(duckdb_get_uint64(val));
|
|
654
|
-
break;
|
|
655
|
-
case DUCKDB_TYPE_FLOAT:
|
|
656
|
-
result = DBL2NUM(duckdb_get_float(val));
|
|
657
|
-
break;
|
|
658
|
-
case DUCKDB_TYPE_DOUBLE:
|
|
659
|
-
result = DBL2NUM(duckdb_get_double(val));
|
|
660
|
-
break;
|
|
661
|
-
case DUCKDB_TYPE_TIMESTAMP:
|
|
662
|
-
result = rbduckdb_timestamp_to_ruby(duckdb_get_timestamp(val));
|
|
663
|
-
break;
|
|
664
|
-
case DUCKDB_TYPE_DATE:
|
|
665
|
-
result = rbduckdb_date_to_ruby(duckdb_get_date(val));
|
|
666
|
-
break;
|
|
667
|
-
case DUCKDB_TYPE_TIME:
|
|
668
|
-
result = rbduckdb_time_to_ruby(duckdb_get_time(val));
|
|
669
|
-
break;
|
|
670
|
-
case DUCKDB_TYPE_INTERVAL:
|
|
671
|
-
result = rbduckdb_interval_to_ruby(duckdb_get_interval(val));
|
|
672
|
-
break;
|
|
673
|
-
case DUCKDB_TYPE_TIMESTAMP_S:
|
|
674
|
-
result = rbduckdb_timestamp_s_to_ruby(duckdb_get_timestamp_s(val));
|
|
675
|
-
break;
|
|
676
|
-
case DUCKDB_TYPE_TIMESTAMP_MS:
|
|
677
|
-
result = rbduckdb_timestamp_ms_to_ruby(duckdb_get_timestamp_ms(val));
|
|
678
|
-
break;
|
|
679
|
-
case DUCKDB_TYPE_TIMESTAMP_NS:
|
|
680
|
-
result = rbduckdb_timestamp_ns_to_ruby(duckdb_get_timestamp_ns(val));
|
|
681
|
-
break;
|
|
682
|
-
case DUCKDB_TYPE_TIME_NS:
|
|
683
|
-
result = rbduckdb_time_ns_to_ruby(duckdb_get_time_ns(val));
|
|
684
|
-
break;
|
|
685
|
-
case DUCKDB_TYPE_TIME_TZ:
|
|
686
|
-
result = rbduckdb_time_tz_to_ruby(duckdb_get_time_tz(val));
|
|
687
|
-
break;
|
|
688
|
-
case DUCKDB_TYPE_TIMESTAMP_TZ:
|
|
689
|
-
result = rbduckdb_timestamp_tz_to_ruby(duckdb_get_timestamp_tz(val));
|
|
690
|
-
break;
|
|
691
|
-
case DUCKDB_TYPE_VARCHAR:
|
|
692
|
-
str = duckdb_get_varchar(val);
|
|
693
|
-
result = rb_str_new_cstr(str);
|
|
694
|
-
duckdb_free(str);
|
|
695
|
-
break;
|
|
696
|
-
case DUCKDB_TYPE_UUID:
|
|
697
|
-
result = rbduckdb_uuid_uhugeint_to_ruby(duckdb_get_uuid(val));
|
|
698
|
-
break;
|
|
699
|
-
case DUCKDB_TYPE_ENUM:
|
|
700
|
-
str = duckdb_enum_dictionary_value(logical_type, duckdb_get_enum_value(val));
|
|
701
|
-
if (str == NULL) {
|
|
702
|
-
result = Qnil;
|
|
703
|
-
break;
|
|
704
|
-
}
|
|
705
|
-
result = rb_utf8_str_new_cstr(str);
|
|
706
|
-
duckdb_free(str);
|
|
707
|
-
break;
|
|
708
|
-
case DUCKDB_TYPE_LIST:
|
|
709
|
-
size = duckdb_get_list_size(val);
|
|
710
|
-
result = rb_ary_new_capa(size);
|
|
711
|
-
for (i = 0; i < size; i++) {
|
|
712
|
-
elem = rbduckdb_duckdb_value_to_ruby_and_destroy(duckdb_get_list_child(val, i));
|
|
713
|
-
rb_ary_push(result, elem);
|
|
714
|
-
}
|
|
715
|
-
break;
|
|
716
|
-
case DUCKDB_TYPE_STRUCT:
|
|
717
|
-
size = duckdb_struct_type_child_count(logical_type);
|
|
718
|
-
result = rb_hash_new();
|
|
719
|
-
for (i = 0; i < size; i++) {
|
|
720
|
-
str = duckdb_struct_type_child_name(logical_type, i);
|
|
721
|
-
key = ID2SYM(rb_intern(str));
|
|
722
|
-
duckdb_free(str);
|
|
723
|
-
elem = rbduckdb_duckdb_value_to_ruby_and_destroy(duckdb_get_struct_child(val, i));
|
|
724
|
-
rb_hash_aset(result, key, elem);
|
|
725
|
-
}
|
|
726
|
-
break;
|
|
727
|
-
case DUCKDB_TYPE_MAP:
|
|
728
|
-
size = duckdb_get_map_size(val);
|
|
729
|
-
result = rb_hash_new();
|
|
730
|
-
for (i = 0; i < size; i++) {
|
|
731
|
-
key = rbduckdb_duckdb_value_to_ruby_and_destroy(duckdb_get_map_key(val, i));
|
|
732
|
-
elem = rbduckdb_duckdb_value_to_ruby_and_destroy(duckdb_get_map_value(val, i));
|
|
733
|
-
rb_hash_aset(result, key, elem);
|
|
734
|
-
}
|
|
735
|
-
break;
|
|
736
|
-
case DUCKDB_TYPE_ARRAY:
|
|
737
|
-
case DUCKDB_TYPE_UNION:
|
|
738
|
-
case DUCKDB_TYPE_BIT:
|
|
739
|
-
case DUCKDB_TYPE_DECIMAL:
|
|
740
|
-
result = to_ruby_via_vector(logical_type, val);
|
|
741
|
-
break;
|
|
742
|
-
case DUCKDB_TYPE_BLOB: {
|
|
743
|
-
duckdb_blob blob = duckdb_get_blob(val);
|
|
744
|
-
|
|
745
|
-
result = rb_str_new((const char *)blob.data, blob.size);
|
|
746
|
-
duckdb_free(blob.data);
|
|
747
|
-
break;
|
|
748
|
-
}
|
|
749
|
-
case DUCKDB_TYPE_BIGNUM: {
|
|
750
|
-
duckdb_bignum bignum = duckdb_get_bignum(val);
|
|
751
|
-
|
|
752
|
-
result = rb_integer_unpack(bignum.data, bignum.size, 1, 0, INTEGER_PACK_BIG_ENDIAN);
|
|
753
|
-
duckdb_free(bignum.data);
|
|
754
|
-
if (bignum.is_negative) {
|
|
755
|
-
result = rb_funcall(result, rb_intern("-@"), 0);
|
|
756
|
-
}
|
|
757
|
-
break;
|
|
758
|
-
}
|
|
759
|
-
default:
|
|
760
|
-
result = Qnil;
|
|
761
|
-
break;
|
|
614
|
+
type = duckdb_get_value_type(val);
|
|
615
|
+
if (!rbduckdb_vector_reader_supports(duckdb_get_type_id(type))) {
|
|
616
|
+
return Qnil;
|
|
762
617
|
}
|
|
763
|
-
|
|
764
|
-
return result;
|
|
618
|
+
return to_ruby_via_vector(type, val);
|
|
765
619
|
}
|
|
766
620
|
|
|
767
621
|
/*
|
data/ext/duckdb/vector.c
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
VALUE cDuckDBVector;
|
|
4
4
|
|
|
5
|
+
static void mark(void *ctx);
|
|
6
|
+
static void compact(void *ctx);
|
|
5
7
|
static void deallocate(void *ctx);
|
|
6
8
|
static VALUE allocate(VALUE klass);
|
|
7
9
|
static size_t memsize(const void *p);
|
|
@@ -14,27 +16,60 @@ static VALUE vector_set_validity(VALUE self, VALUE index, VALUE valid);
|
|
|
14
16
|
|
|
15
17
|
static const rb_data_type_t vector_data_type = {
|
|
16
18
|
"DuckDB/Vector",
|
|
17
|
-
{
|
|
19
|
+
{mark, deallocate, memsize, compact},
|
|
18
20
|
0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
|
|
19
21
|
};
|
|
20
22
|
|
|
23
|
+
static void mark(void *ctx) {
|
|
24
|
+
rubyDuckDBVector *p = (rubyDuckDBVector *)ctx;
|
|
25
|
+
rb_gc_mark_movable(p->data_chunk);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
static void compact(void *ctx) {
|
|
29
|
+
rubyDuckDBVector *p = (rubyDuckDBVector *)ctx;
|
|
30
|
+
p->data_chunk = rb_gc_location(p->data_chunk);
|
|
31
|
+
}
|
|
32
|
+
|
|
21
33
|
static void deallocate(void *ctx) {
|
|
22
34
|
rubyDuckDBVector *p = (rubyDuckDBVector *)ctx;
|
|
23
35
|
xfree(p);
|
|
24
36
|
}
|
|
25
37
|
|
|
26
38
|
static VALUE allocate(VALUE klass) {
|
|
27
|
-
rubyDuckDBVector *ctx
|
|
28
|
-
|
|
39
|
+
rubyDuckDBVector *ctx;
|
|
40
|
+
VALUE obj = TypedData_Make_Struct(klass, rubyDuckDBVector, &vector_data_type, ctx);
|
|
41
|
+
ctx->data_chunk = Qnil;
|
|
42
|
+
return obj;
|
|
29
43
|
}
|
|
30
44
|
|
|
31
45
|
static size_t memsize(const void *p) {
|
|
32
46
|
return sizeof(rubyDuckDBVector);
|
|
33
47
|
}
|
|
34
48
|
|
|
35
|
-
|
|
49
|
+
VALUE rbduckdb_vector_new(VALUE data_chunk, uint64_t data_chunk_generation, duckdb_vector vector) {
|
|
36
50
|
rubyDuckDBVector *ctx;
|
|
51
|
+
VALUE obj = allocate(cDuckDBVector);
|
|
52
|
+
|
|
37
53
|
TypedData_Get_Struct(obj, rubyDuckDBVector, &vector_data_type, ctx);
|
|
54
|
+
RB_OBJ_WRITE(obj, &ctx->data_chunk, data_chunk);
|
|
55
|
+
ctx->data_chunk_generation = data_chunk_generation;
|
|
56
|
+
ctx->vector = vector;
|
|
57
|
+
return obj;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/*
|
|
61
|
+
* Returns the struct for +self+, raising DuckDB::Error if the vector is no
|
|
62
|
+
* longer usable: its DataChunk was the output of a TableFunction execute
|
|
63
|
+
* callback that has returned, or the DataChunk was re-initialized.
|
|
64
|
+
*/
|
|
65
|
+
static rubyDuckDBVector *get_valid_vector(VALUE self) {
|
|
66
|
+
rubyDuckDBVector *ctx;
|
|
67
|
+
|
|
68
|
+
TypedData_Get_Struct(self, rubyDuckDBVector, &vector_data_type, ctx);
|
|
69
|
+
if (ctx->vector == NULL || NIL_P(ctx->data_chunk) ||
|
|
70
|
+
!rbduckdb_data_chunk_is_current(ctx->data_chunk, ctx->data_chunk_generation)) {
|
|
71
|
+
rb_raise(eDuckDBError, "DuckDB::Vector is no longer valid (its DuckDB::DataChunk is gone or was re-initialized)");
|
|
72
|
+
}
|
|
38
73
|
return ctx;
|
|
39
74
|
}
|
|
40
75
|
|
|
@@ -51,7 +86,7 @@ static VALUE vector_get_data(VALUE self) {
|
|
|
51
86
|
rubyDuckDBVector *ctx;
|
|
52
87
|
void *data;
|
|
53
88
|
|
|
54
|
-
|
|
89
|
+
ctx = get_valid_vector(self);
|
|
55
90
|
|
|
56
91
|
data = duckdb_vector_get_data(ctx->vector);
|
|
57
92
|
|
|
@@ -71,7 +106,7 @@ static VALUE vector_get_validity(VALUE self) {
|
|
|
71
106
|
rubyDuckDBVector *ctx;
|
|
72
107
|
uint64_t *validity;
|
|
73
108
|
|
|
74
|
-
|
|
109
|
+
ctx = get_valid_vector(self);
|
|
75
110
|
|
|
76
111
|
validity = duckdb_vector_get_validity(ctx->vector);
|
|
77
112
|
|
|
@@ -95,10 +130,10 @@ static VALUE vector_assign_string_element(VALUE self, VALUE index, VALUE str) {
|
|
|
95
130
|
idx_t idx;
|
|
96
131
|
const char *string_val;
|
|
97
132
|
|
|
98
|
-
|
|
99
|
-
|
|
133
|
+
/* Convert arguments first: to_int/to_str may run Ruby code. */
|
|
100
134
|
idx = NUM2ULL(index);
|
|
101
135
|
string_val = StringValueCStr(str);
|
|
136
|
+
ctx = get_valid_vector(self);
|
|
102
137
|
|
|
103
138
|
duckdb_vector_assign_string_element(ctx->vector, idx, string_val);
|
|
104
139
|
|
|
@@ -120,11 +155,11 @@ static VALUE vector_assign_string_element_len(VALUE self, VALUE index, VALUE str
|
|
|
120
155
|
const char *string_val;
|
|
121
156
|
idx_t str_len;
|
|
122
157
|
|
|
123
|
-
|
|
124
|
-
|
|
158
|
+
/* Convert arguments first: to_int/to_str may run Ruby code. */
|
|
125
159
|
idx = NUM2ULL(index);
|
|
126
160
|
string_val = StringValuePtr(str);
|
|
127
161
|
str_len = RSTRING_LEN(str);
|
|
162
|
+
ctx = get_valid_vector(self);
|
|
128
163
|
|
|
129
164
|
duckdb_vector_assign_string_element_len(ctx->vector, idx, string_val, str_len);
|
|
130
165
|
|
|
@@ -145,7 +180,7 @@ static VALUE vector_logical_type(VALUE self) {
|
|
|
145
180
|
rubyDuckDBVector *ctx;
|
|
146
181
|
duckdb_logical_type logical_type;
|
|
147
182
|
|
|
148
|
-
|
|
183
|
+
ctx = get_valid_vector(self);
|
|
149
184
|
|
|
150
185
|
logical_type = duckdb_vector_get_column_type(ctx->vector);
|
|
151
186
|
|
|
@@ -166,9 +201,9 @@ static VALUE vector_set_validity(VALUE self, VALUE index, VALUE valid) {
|
|
|
166
201
|
idx_t idx;
|
|
167
202
|
uint64_t *validity;
|
|
168
203
|
|
|
169
|
-
|
|
170
|
-
|
|
204
|
+
/* Convert arguments first: to_int may run Ruby code. */
|
|
171
205
|
idx = NUM2ULL(index);
|
|
206
|
+
ctx = get_valid_vector(self);
|
|
172
207
|
|
|
173
208
|
if (RTEST(valid)) {
|
|
174
209
|
// Setting to valid - ensure validity mask exists and set bit
|
data/ext/duckdb/vector.h
CHANGED
|
@@ -3,11 +3,15 @@
|
|
|
3
3
|
|
|
4
4
|
struct _rubyDuckDBVector {
|
|
5
5
|
duckdb_vector vector;
|
|
6
|
+
/* The DuckDB::DataChunk this vector belongs to (keeps it alive). */
|
|
7
|
+
VALUE data_chunk;
|
|
8
|
+
/* The chunk's generation when this vector was obtained. */
|
|
9
|
+
uint64_t data_chunk_generation;
|
|
6
10
|
};
|
|
7
11
|
|
|
8
12
|
typedef struct _rubyDuckDBVector rubyDuckDBVector;
|
|
9
13
|
|
|
10
|
-
|
|
14
|
+
VALUE rbduckdb_vector_new(VALUE data_chunk, uint64_t data_chunk_generation, duckdb_vector vector);
|
|
11
15
|
void rbduckdb_init_vector(void);
|
|
12
16
|
|
|
13
17
|
#endif
|