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.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +23 -0
  3. data/ext/duckdb/aggregate_function.c +2 -2
  4. data/ext/duckdb/aggregate_function_set.c +2 -2
  5. data/ext/duckdb/appender.c +9 -10
  6. data/ext/duckdb/arrow_array_stream.c +2 -2
  7. data/ext/duckdb/arrow_import.c +2 -2
  8. data/ext/duckdb/client_context.c +2 -2
  9. data/ext/duckdb/column.c +2 -2
  10. data/ext/duckdb/config.c +2 -2
  11. data/ext/duckdb/connection.c +3 -2
  12. data/ext/duckdb/{conveter.c → converter.c} +0 -6
  13. data/ext/duckdb/converter.h +0 -1
  14. data/ext/duckdb/data_chunk.c +171 -26
  15. data/ext/duckdb/data_chunk.h +12 -0
  16. data/ext/duckdb/database.c +2 -2
  17. data/ext/duckdb/expression.c +2 -2
  18. data/ext/duckdb/extracted_statements.c +3 -3
  19. data/ext/duckdb/instance_cache.c +3 -2
  20. data/ext/duckdb/logical_type.c +2 -2
  21. data/ext/duckdb/memory_helper.c +17 -4
  22. data/ext/duckdb/pending_result.c +3 -2
  23. data/ext/duckdb/prepared_statement.c +2 -2
  24. data/ext/duckdb/result.c +0 -422
  25. data/ext/duckdb/result.h +0 -1
  26. data/ext/duckdb/ruby-duckdb.h +2 -1
  27. data/ext/duckdb/scalar_function.c +4 -2
  28. data/ext/duckdb/scalar_function_bind_info.c +37 -8
  29. data/ext/duckdb/scalar_function_bind_info.h +1 -0
  30. data/ext/duckdb/scalar_function_set.c +2 -2
  31. data/ext/duckdb/table_description.c +2 -2
  32. data/ext/duckdb/table_function.c +41 -2
  33. data/ext/duckdb/table_function_bind_info.c +30 -15
  34. data/ext/duckdb/table_function_function_info.c +38 -4
  35. data/ext/duckdb/table_function_init_info.c +72 -13
  36. data/ext/duckdb/table_function_init_info.h +1 -0
  37. data/ext/duckdb/value.c +13 -159
  38. data/ext/duckdb/vector.c +48 -13
  39. data/ext/duckdb/vector.h +5 -1
  40. data/ext/duckdb/vector_reader.c +544 -0
  41. data/ext/duckdb/vector_reader.h +7 -0
  42. data/ext/duckdb/{function_vector.c → vector_writer.c} +68 -7
  43. data/ext/duckdb/{function_vector.h → vector_writer.h} +14 -6
  44. data/lib/duckdb/appender.rb +27 -31
  45. data/lib/duckdb/data_chunk.rb +4 -89
  46. data/lib/duckdb/prepared_statement.rb +14 -39
  47. data/lib/duckdb/ruby_type_inference.rb +91 -0
  48. data/lib/duckdb/version.rb +1 -1
  49. data/lib/duckdb.rb +1 -0
  50. metadata +7 -4
@@ -15,6 +15,7 @@ static VALUE table_function_initialize(VALUE self);
15
15
  static VALUE table_function_set_name(VALUE self, VALUE name);
16
16
  static VALUE table_function_add_parameter(VALUE self, VALUE logical_type);
17
17
  static VALUE table_function_add_named_parameter(VALUE self, VALUE name, VALUE logical_type);
18
+ static VALUE table_function_set_supports_projection_pushdown(VALUE self, VALUE pushdown);
18
19
  static VALUE table_function_bind(VALUE self);
19
20
  static void table_function_bind_callback(duckdb_bind_info info);
20
21
  static VALUE table_function_init(VALUE self);
@@ -69,8 +70,8 @@ static void compact(void *ctx) {
69
70
  }
70
71
 
71
72
  static VALUE allocate(VALUE klass) {
72
- rubyDuckDBTableFunction *ctx = xcalloc((size_t)1, sizeof(rubyDuckDBTableFunction));
73
- return TypedData_Wrap_Struct(klass, &table_function_data_type, ctx);
73
+ rubyDuckDBTableFunction *ctx;
74
+ return TypedData_Make_Struct(klass, rubyDuckDBTableFunction, &table_function_data_type, ctx);
74
75
  }
75
76
 
76
77
  static size_t memsize(const void *p) {
@@ -183,6 +184,35 @@ static VALUE table_function_add_named_parameter(VALUE self, VALUE name, VALUE lo
183
184
  return self;
184
185
  }
185
186
 
187
+ /*
188
+ * call-seq:
189
+ * table_function.supports_projection_pushdown = bool -> bool
190
+ *
191
+ * Sets whether the table function supports projection pushdown.
192
+ *
193
+ * When +true+, DuckDB passes only the columns referenced by the query to the
194
+ * table function. The output chunk given to the execute callback then holds
195
+ * just those columns, in the order reported by
196
+ * DuckDB::TableFunction::InitInfo#column_count and
197
+ * DuckDB::TableFunction::InitInfo#column_index.
198
+ * When +false+ (the default), the output chunk holds every result column.
199
+ *
200
+ * tf.supports_projection_pushdown = true
201
+ */
202
+ static VALUE table_function_set_supports_projection_pushdown(VALUE self, VALUE pushdown) {
203
+ rubyDuckDBTableFunction *ctx;
204
+
205
+ TypedData_Get_Struct(self, rubyDuckDBTableFunction, &table_function_data_type, ctx);
206
+
207
+ if (!ctx->table_function) {
208
+ rb_raise(eDuckDBError, "Table function is destroyed");
209
+ }
210
+
211
+ duckdb_table_function_supports_projection_pushdown(ctx->table_function, RTEST(pushdown));
212
+
213
+ return pushdown;
214
+ }
215
+
186
216
  static rubyDuckDBTableFunction *table_function_get_settable_ctx(VALUE self, const char *block_msg) {
187
217
  rubyDuckDBTableFunction *ctx;
188
218
 
@@ -245,6 +275,8 @@ static void execute_bind_callback_protected(void *user_data) {
245
275
 
246
276
  VALUE call_args[2] = { darg->ctx->bind_proc, bind_info_obj };
247
277
  rb_protect(call_proc_with_one_arg, (VALUE)call_args, &state);
278
+ /* duckdb_bind_info is only valid during the callback. */
279
+ bind_info_ctx->bind_info = NULL;
248
280
 
249
281
  if (state) {
250
282
  VALUE msg = rbduckdb_pending_error_message();
@@ -305,6 +337,8 @@ static void execute_init_callback_protected(void *user_data) {
305
337
 
306
338
  VALUE call_args[2] = { darg->ctx->init_proc, init_info_obj };
307
339
  rb_protect(call_proc_with_one_arg, (VALUE)call_args, &state);
340
+ /* duckdb_init_info is only valid during the callback. */
341
+ init_info_ctx->info = NULL;
308
342
 
309
343
  if (state) {
310
344
  VALUE msg = rbduckdb_pending_error_message();
@@ -383,6 +417,10 @@ static void execute_execute_callback_protected(void *user_data) {
383
417
 
384
418
  VALUE call_args[3] = { darg->ctx->execute_proc, func_info_obj, data_chunk_obj };
385
419
  rb_protect(call_execute_proc, (VALUE)call_args, &state);
420
+ /* duckdb_function_info is only valid during the callback. */
421
+ func_info_ctx->info = NULL;
422
+ /* The output chunk is borrowed from DuckDB and only valid during the callback. */
423
+ rbduckdb_data_chunk_invalidate(data_chunk_obj);
386
424
 
387
425
  if (state) {
388
426
  VALUE msg = rbduckdb_pending_error_message();
@@ -446,6 +484,7 @@ void rbduckdb_init_table_function(void) {
446
484
  rb_define_method(cDuckDBTableFunction, "name=", table_function_set_name, 1);
447
485
  rb_define_method(cDuckDBTableFunction, "add_parameter", table_function_add_parameter, 1);
448
486
  rb_define_method(cDuckDBTableFunction, "add_named_parameter", table_function_add_named_parameter, 2);
487
+ rb_define_method(cDuckDBTableFunction, "supports_projection_pushdown=", table_function_set_supports_projection_pushdown, 1);
449
488
  rb_define_method(cDuckDBTableFunction, "bind", table_function_bind, 0);
450
489
  rb_define_method(cDuckDBTableFunction, "init", table_function_init, 0);
451
490
  rb_define_method(cDuckDBTableFunction, "execute", table_function_execute, 0);
@@ -25,8 +25,8 @@ static void deallocate(void *ctx) {
25
25
  }
26
26
 
27
27
  static VALUE allocate(VALUE klass) {
28
- rubyDuckDBBindInfo *ctx = xcalloc((size_t)1, sizeof(rubyDuckDBBindInfo));
29
- return TypedData_Wrap_Struct(klass, &bind_info_data_type, ctx);
28
+ rubyDuckDBBindInfo *ctx;
29
+ return TypedData_Make_Struct(klass, rubyDuckDBBindInfo, &bind_info_data_type, ctx);
30
30
  }
31
31
 
32
32
  static size_t memsize(const void *p) {
@@ -39,6 +39,21 @@ rubyDuckDBBindInfo *rbduckdb_get_struct_bind_info(VALUE obj) {
39
39
  return ctx;
40
40
  }
41
41
 
42
+ /*
43
+ * Returns the struct for +self+, raising DuckDB::Error if the underlying
44
+ * DuckDB info is no longer valid (i.e. the object is used after the bind
45
+ * callback has returned).
46
+ */
47
+ static rubyDuckDBBindInfo *get_valid_bind_info(VALUE self) {
48
+ rubyDuckDBBindInfo *ctx;
49
+
50
+ TypedData_Get_Struct(self, rubyDuckDBBindInfo, &bind_info_data_type, ctx);
51
+ if (ctx->bind_info == NULL) {
52
+ rb_raise(eDuckDBError, "DuckDB::TableFunction::BindInfo can only be used inside the bind callback");
53
+ }
54
+ return ctx;
55
+ }
56
+
42
57
  /*
43
58
  * Converts a duckdb_value to a Ruby VALUE.
44
59
  * Handles basic types: BIGINT, INTEGER, VARCHAR, DOUBLE, BOOLEAN.
@@ -57,7 +72,7 @@ static VALUE table_function_bind_info_parameter_count(VALUE self) {
57
72
  rubyDuckDBBindInfo *ctx;
58
73
  idx_t count;
59
74
 
60
- TypedData_Get_Struct(self, rubyDuckDBBindInfo, &bind_info_data_type, ctx);
75
+ ctx = get_valid_bind_info(self);
61
76
 
62
77
  count = duckdb_bind_get_parameter_count(ctx->bind_info);
63
78
 
@@ -77,9 +92,9 @@ static VALUE table_function_bind_info_get_parameter(VALUE self, VALUE index) {
77
92
  idx_t idx;
78
93
  duckdb_value param_value;
79
94
 
80
- TypedData_Get_Struct(self, rubyDuckDBBindInfo, &bind_info_data_type, ctx);
81
-
95
+ /* Convert arguments first: to_int/to_str may run Ruby code. */
82
96
  idx = NUM2ULL(index);
97
+ ctx = get_valid_bind_info(self);
83
98
  param_value = duckdb_bind_get_parameter(ctx->bind_info, idx);
84
99
 
85
100
  return rbduckdb_duckdb_value_to_ruby_and_destroy(param_value);
@@ -98,9 +113,9 @@ static VALUE table_function_bind_info_get_named_parameter(VALUE self, VALUE name
98
113
  const char *param_name;
99
114
  duckdb_value param_value;
100
115
 
101
- TypedData_Get_Struct(self, rubyDuckDBBindInfo, &bind_info_data_type, ctx);
102
-
116
+ /* Convert arguments first: to_int/to_str may run Ruby code. */
103
117
  param_name = StringValueCStr(name);
118
+ ctx = get_valid_bind_info(self);
104
119
  param_value = duckdb_bind_get_named_parameter(ctx->bind_info, param_name);
105
120
 
106
121
  // If parameter not found, return nil
@@ -125,10 +140,10 @@ static VALUE table_function_bind_info__add_result_column(VALUE self, VALUE colum
125
140
  rubyDuckDBLogicalType *ctx_logical_type;
126
141
  const char *col_name;
127
142
 
128
- TypedData_Get_Struct(self, rubyDuckDBBindInfo, &bind_info_data_type, ctx);
129
- ctx_logical_type = rbduckdb_get_struct_logical_type(logical_type);
130
-
143
+ /* Convert arguments first: to_int/to_str may run Ruby code. */
131
144
  col_name = StringValueCStr(column_name);
145
+ ctx_logical_type = rbduckdb_get_struct_logical_type(logical_type);
146
+ ctx = get_valid_bind_info(self);
132
147
  duckdb_bind_add_result_column(ctx->bind_info, col_name, ctx_logical_type->logical_type);
133
148
 
134
149
  return self;
@@ -148,10 +163,10 @@ static VALUE table_function_bind_info_set_cardinality(VALUE self, VALUE cardinal
148
163
  idx_t card;
149
164
  bool exact;
150
165
 
151
- TypedData_Get_Struct(self, rubyDuckDBBindInfo, &bind_info_data_type, ctx);
152
-
166
+ /* Convert arguments first: to_int/to_str may run Ruby code. */
153
167
  card = NUM2ULL(cardinality);
154
168
  exact = RTEST(is_exact);
169
+ ctx = get_valid_bind_info(self);
155
170
 
156
171
  duckdb_bind_set_cardinality(ctx->bind_info, card, exact);
157
172
 
@@ -172,7 +187,7 @@ static VALUE table_function_bind_info_set_bind_data(VALUE self, VALUE data) {
172
187
  rubyDuckDBBindInfo *ctx;
173
188
  void *handle;
174
189
 
175
- TypedData_Get_Struct(self, rubyDuckDBBindInfo, &bind_info_data_type, ctx);
190
+ ctx = get_valid_bind_info(self);
176
191
 
177
192
  if (ctx->bind_data_handle != NULL) {
178
193
  rbduckdb_function_data_release(ctx->bind_data_handle);
@@ -196,9 +211,9 @@ static VALUE table_function_bind_info_set_error(VALUE self, VALUE error) {
196
211
  rubyDuckDBBindInfo *ctx;
197
212
  const char *error_msg;
198
213
 
199
- TypedData_Get_Struct(self, rubyDuckDBBindInfo, &bind_info_data_type, ctx);
200
-
214
+ /* Convert arguments first: to_int/to_str may run Ruby code. */
201
215
  error_msg = StringValueCStr(error);
216
+ ctx = get_valid_bind_info(self);
202
217
  duckdb_bind_set_error(ctx->bind_info, error_msg);
203
218
 
204
219
  return self;
@@ -6,6 +6,7 @@ static void deallocate(void *ctx);
6
6
  static VALUE allocate(VALUE klass);
7
7
  static size_t memsize(const void *p);
8
8
  static VALUE table_function_function_info_bind_data(VALUE self);
9
+ static VALUE table_function_function_info_init_data(VALUE self);
9
10
  static VALUE table_function_function_info_set_error(VALUE self, VALUE error);
10
11
 
11
12
  static const rb_data_type_t function_info_data_type = {
@@ -20,8 +21,8 @@ static void deallocate(void *ctx) {
20
21
  }
21
22
 
22
23
  static VALUE allocate(VALUE klass) {
23
- rubyDuckDBFunctionInfo *ctx = xcalloc((size_t)1, sizeof(rubyDuckDBFunctionInfo));
24
- return TypedData_Wrap_Struct(klass, &function_info_data_type, ctx);
24
+ rubyDuckDBFunctionInfo *ctx;
25
+ return TypedData_Make_Struct(klass, rubyDuckDBFunctionInfo, &function_info_data_type, ctx);
25
26
  }
26
27
 
27
28
  static size_t memsize(const void *p) {
@@ -34,6 +35,21 @@ rubyDuckDBFunctionInfo *rbduckdb_get_struct_function_info(VALUE obj) {
34
35
  return ctx;
35
36
  }
36
37
 
38
+ /*
39
+ * Returns the struct for +self+, raising DuckDB::Error if the underlying
40
+ * DuckDB info is no longer valid (i.e. the object is used after the execute
41
+ * callback has returned).
42
+ */
43
+ static rubyDuckDBFunctionInfo *get_valid_function_info(VALUE self) {
44
+ rubyDuckDBFunctionInfo *ctx;
45
+
46
+ TypedData_Get_Struct(self, rubyDuckDBFunctionInfo, &function_info_data_type, ctx);
47
+ if (ctx->info == NULL) {
48
+ rb_raise(eDuckDBError, "DuckDB::TableFunction::FunctionInfo can only be used inside the execute callback");
49
+ }
50
+ return ctx;
51
+ }
52
+
37
53
  /*
38
54
  * call-seq:
39
55
  * function_info.bind_data -> object or nil
@@ -47,13 +63,30 @@ static VALUE table_function_function_info_bind_data(VALUE self) {
47
63
  rubyDuckDBFunctionInfo *ctx;
48
64
  void *bind_data;
49
65
 
50
- TypedData_Get_Struct(self, rubyDuckDBFunctionInfo, &function_info_data_type, ctx);
66
+ ctx = get_valid_function_info(self);
51
67
 
52
68
  bind_data = duckdb_function_get_bind_data(ctx->info);
53
69
 
54
70
  return rbduckdb_function_data_lookup(bind_data);
55
71
  }
56
72
 
73
+ /*
74
+ * call-seq:
75
+ * function_info.init_data -> object or nil
76
+ *
77
+ * Returns the object stored during the init phase with
78
+ * DuckDB::TableFunction::InitInfo#init_data=, or nil if none was set.
79
+ *
80
+ * state = function_info.init_data
81
+ */
82
+ static VALUE table_function_function_info_init_data(VALUE self) {
83
+ rubyDuckDBFunctionInfo *ctx;
84
+
85
+ ctx = get_valid_function_info(self);
86
+
87
+ return rbduckdb_function_data_lookup(duckdb_function_get_init_data(ctx->info));
88
+ }
89
+
57
90
  /*
58
91
  * call-seq:
59
92
  * function_info.set_error(error_message) -> self
@@ -67,7 +100,7 @@ static VALUE table_function_function_info_set_error(VALUE self, VALUE error) {
67
100
  rubyDuckDBFunctionInfo *ctx;
68
101
  const char *error_msg;
69
102
 
70
- TypedData_Get_Struct(self, rubyDuckDBFunctionInfo, &function_info_data_type, ctx);
103
+ ctx = get_valid_function_info(self);
71
104
 
72
105
  error_msg = StringValueCStr(error);
73
106
  duckdb_function_set_error(ctx->info, error_msg);
@@ -83,5 +116,6 @@ void rbduckdb_init_table_function_function_info(void) {
83
116
  rb_define_alloc_func(cDuckDBTableFunctionFunctionInfo, allocate);
84
117
 
85
118
  rb_define_method(cDuckDBTableFunctionFunctionInfo, "bind_data", table_function_function_info_bind_data, 0);
119
+ rb_define_method(cDuckDBTableFunctionFunctionInfo, "init_data", table_function_function_info_init_data, 0);
86
120
  rb_define_method(cDuckDBTableFunctionFunctionInfo, "set_error", table_function_function_info_set_error, 1);
87
121
  }
@@ -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 = xcalloc((size_t)1, sizeof(rubyDuckDBInitInfo));
27
- return TypedData_Wrap_Struct(klass, &init_info_data_type, ctx);
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
- TypedData_Get_Struct(self, rubyDuckDBInitInfo, &init_info_data_type, ctx);
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
- TypedData_Get_Struct(self, rubyDuckDBInitInfo, &init_info_data_type, ctx);
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 projected result columns for this scan.
87
- * Without projection pushdown this equals the number of result columns
88
- * added in the bind callback.
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
- TypedData_Get_Struct(self, rubyDuckDBInitInfo, &init_info_data_type, ctx);
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). Without projection pushdown the projected
106
- * columns mirror the columns added in the bind callback, so this returns
107
- * +index+ itself.
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
- TypedData_Get_Struct(self, rubyDuckDBInitInfo, &init_info_data_type, ctx);
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
- TypedData_Get_Struct(self, rubyDuckDBInitInfo, &init_info_data_type, ctx);
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
  }
@@ -3,6 +3,7 @@
3
3
 
4
4
  struct _rubyDuckDBInitInfo {
5
5
  duckdb_init_info info;
6
+ void *init_data_handle;
6
7
  };
7
8
 
8
9
  typedef struct _rubyDuckDBInitInfo rubyDuckDBInitInfo;
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 = xcalloc((size_t)1, sizeof(rubyDuckDBValue));
77
- return TypedData_Wrap_Struct(klass, &value_data_type, ctx);
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 read path, so Value#to_ruby returns the
565
- * same Ruby object a query result produces for that type. Used for types
566
- * the C API has no direct duckdb_get_* accessors for.
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 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
- logical_type = duckdb_get_value_type(val);
619
- type_id = duckdb_get_type_id(logical_type);
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
  /*