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.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +28 -0
  3. data/ext/duckdb/aggregate_function.c +42 -80
  4. data/ext/duckdb/aggregate_function_set.c +2 -2
  5. data/ext/duckdb/appender.c +46 -134
  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 +20 -56
  12. data/ext/duckdb/{conveter.c → converter.c} +37 -80
  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/function_executor.c +214 -248
  20. data/ext/duckdb/function_executor.h +10 -0
  21. data/ext/duckdb/instance_cache.c +3 -2
  22. data/ext/duckdb/logical_type.c +90 -172
  23. data/ext/duckdb/memory_helper.c +17 -4
  24. data/ext/duckdb/pending_result.c +3 -2
  25. data/ext/duckdb/prepared_statement.c +39 -140
  26. data/ext/duckdb/result.c +0 -491
  27. data/ext/duckdb/result.h +0 -1
  28. data/ext/duckdb/ruby-duckdb.h +2 -1
  29. data/ext/duckdb/scalar_function.c +13 -71
  30. data/ext/duckdb/scalar_function_bind_info.c +37 -8
  31. data/ext/duckdb/scalar_function_bind_info.h +1 -0
  32. data/ext/duckdb/scalar_function_set.c +2 -2
  33. data/ext/duckdb/table_description.c +2 -2
  34. data/ext/duckdb/table_function.c +72 -82
  35. data/ext/duckdb/table_function_bind_info.c +30 -15
  36. data/ext/duckdb/table_function_function_info.c +38 -4
  37. data/ext/duckdb/table_function_init_info.c +72 -13
  38. data/ext/duckdb/table_function_init_info.h +1 -0
  39. data/ext/duckdb/util.c +53 -2
  40. data/ext/duckdb/value.c +13 -159
  41. data/ext/duckdb/vector.c +48 -13
  42. data/ext/duckdb/vector.h +5 -1
  43. data/ext/duckdb/vector_reader.c +544 -0
  44. data/ext/duckdb/vector_reader.h +7 -0
  45. data/ext/duckdb/{function_vector.c → vector_writer.c} +94 -7
  46. data/ext/duckdb/vector_writer.h +53 -0
  47. data/lib/duckdb/appender.rb +27 -31
  48. data/lib/duckdb/data_chunk.rb +4 -89
  49. data/lib/duckdb/interval.rb +2 -28
  50. data/lib/duckdb/prepared_statement.rb +19 -39
  51. data/lib/duckdb/ruby_type_inference.rb +91 -0
  52. data/lib/duckdb/scalar_function_set.rb +1 -2
  53. data/lib/duckdb/version.rb +1 -1
  54. data/lib/duckdb.rb +1 -0
  55. metadata +7 -4
  56. data/ext/duckdb/function_vector.h +0 -27
@@ -22,8 +22,8 @@ static void deallocate(void *ctx) {
22
22
  }
23
23
 
24
24
  static VALUE allocate(VALUE klass) {
25
- rubyDuckDBScalarFunctionBindInfo *ctx = xcalloc((size_t)1, sizeof(rubyDuckDBScalarFunctionBindInfo));
26
- return TypedData_Wrap_Struct(klass, &scalar_function_bind_info_data_type, ctx);
25
+ rubyDuckDBScalarFunctionBindInfo *ctx;
26
+ return TypedData_Make_Struct(klass, rubyDuckDBScalarFunctionBindInfo, &scalar_function_bind_info_data_type, ctx);
27
27
  }
28
28
 
29
29
  static size_t memsize(const void *p) {
@@ -38,6 +38,30 @@ VALUE rbduckdb_scalar_function_bind_info_new(duckdb_bind_info bind_info) {
38
38
  return obj;
39
39
  }
40
40
 
41
+ /*
42
+ * Marks +obj+ as no longer usable. Called after the bind callback returns,
43
+ * because the duckdb_bind_info is only valid while the callback runs.
44
+ */
45
+ void rbduckdb_scalar_function_bind_info_invalidate(VALUE obj) {
46
+ rubyDuckDBScalarFunctionBindInfo *ctx;
47
+ TypedData_Get_Struct(obj, rubyDuckDBScalarFunctionBindInfo, &scalar_function_bind_info_data_type, ctx);
48
+ ctx->bind_info = NULL;
49
+ }
50
+
51
+ /*
52
+ * Returns the struct for +self+, raising DuckDB::Error if the underlying
53
+ * DuckDB info is no longer valid (i.e. the object is used after the bind
54
+ * callback has returned).
55
+ */
56
+ static rubyDuckDBScalarFunctionBindInfo *get_valid_bind_info(VALUE self) {
57
+ rubyDuckDBScalarFunctionBindInfo *ctx;
58
+ TypedData_Get_Struct(self, rubyDuckDBScalarFunctionBindInfo, &scalar_function_bind_info_data_type, ctx);
59
+ if (ctx->bind_info == NULL) {
60
+ rb_raise(eDuckDBError, "DuckDB::ScalarFunction::BindInfo can only be used inside the bind callback");
61
+ }
62
+ return ctx;
63
+ }
64
+
41
65
  /*
42
66
  * call-seq:
43
67
  * bind_info.argument_count -> Integer
@@ -48,7 +72,7 @@ VALUE rbduckdb_scalar_function_bind_info_new(duckdb_bind_info bind_info) {
48
72
  */
49
73
  static VALUE scalar_function_bind_info_argument_count(VALUE self) {
50
74
  rubyDuckDBScalarFunctionBindInfo *ctx;
51
- TypedData_Get_Struct(self, rubyDuckDBScalarFunctionBindInfo, &scalar_function_bind_info_data_type, ctx);
75
+ ctx = get_valid_bind_info(self);
52
76
  return ULL2NUM(duckdb_scalar_function_bind_get_argument_count(ctx->bind_info));
53
77
  }
54
78
 
@@ -63,8 +87,10 @@ static VALUE scalar_function_bind_info_argument_count(VALUE self) {
63
87
  */
64
88
  static VALUE scalar_function_bind_info_set_error(VALUE self, VALUE error) {
65
89
  rubyDuckDBScalarFunctionBindInfo *ctx;
66
- TypedData_Get_Struct(self, rubyDuckDBScalarFunctionBindInfo, &scalar_function_bind_info_data_type, ctx);
67
- duckdb_scalar_function_bind_set_error(ctx->bind_info, StringValueCStr(error));
90
+ /* Convert arguments first: to_str may run Ruby code. */
91
+ const char *message = StringValueCStr(error);
92
+ ctx = get_valid_bind_info(self);
93
+ duckdb_scalar_function_bind_set_error(ctx->bind_info, message);
68
94
  return self;
69
95
  }
70
96
 
@@ -77,8 +103,11 @@ static VALUE scalar_function_bind_info_set_error(VALUE self, VALUE error) {
77
103
  */
78
104
  static VALUE scalar_function_bind_info__get_argument(VALUE self, VALUE index) {
79
105
  rubyDuckDBScalarFunctionBindInfo *ctx;
80
- TypedData_Get_Struct(self, rubyDuckDBScalarFunctionBindInfo, &scalar_function_bind_info_data_type, ctx);
81
- duckdb_expression expr = duckdb_scalar_function_bind_get_argument(ctx->bind_info, (idx_t)NUM2ULL(index));
106
+ /* Convert arguments first: to_int may run Ruby code. */
107
+ idx_t idx = (idx_t)NUM2ULL(index);
108
+ duckdb_expression expr;
109
+ ctx = get_valid_bind_info(self);
110
+ expr = duckdb_scalar_function_bind_get_argument(ctx->bind_info, idx);
82
111
  return rbduckdb_expression_new(expr);
83
112
  }
84
113
 
@@ -91,7 +120,7 @@ static VALUE scalar_function_bind_info__get_argument(VALUE self, VALUE index) {
91
120
  static VALUE scalar_function_bind_info_client_context(VALUE self) {
92
121
  rubyDuckDBScalarFunctionBindInfo *ctx;
93
122
  duckdb_client_context client_context;
94
- TypedData_Get_Struct(self, rubyDuckDBScalarFunctionBindInfo, &scalar_function_bind_info_data_type, ctx);
123
+ ctx = get_valid_bind_info(self);
95
124
  duckdb_scalar_function_get_client_context(ctx->bind_info, &client_context);
96
125
  return rbduckdb_client_context_new(client_context);
97
126
  }
@@ -9,5 +9,6 @@ typedef struct _rubyDuckDBScalarFunctionBindInfo rubyDuckDBScalarFunctionBindInf
9
9
 
10
10
  void rbduckdb_init_scalar_function_bind_info(void);
11
11
  VALUE rbduckdb_scalar_function_bind_info_new(duckdb_bind_info bind_info);
12
+ void rbduckdb_scalar_function_bind_info_invalidate(VALUE obj);
12
13
 
13
14
  #endif
@@ -33,8 +33,8 @@ static void compact(void *ctx) {
33
33
  }
34
34
 
35
35
  static VALUE allocate(VALUE klass) {
36
- rubyDuckDBScalarFunctionSet *ctx = xcalloc((size_t)1, sizeof(rubyDuckDBScalarFunctionSet));
37
- VALUE obj = TypedData_Wrap_Struct(klass, &scalar_function_set_data_type, ctx);
36
+ rubyDuckDBScalarFunctionSet *ctx;
37
+ VALUE obj = TypedData_Make_Struct(klass, rubyDuckDBScalarFunctionSet, &scalar_function_set_data_type, ctx);
38
38
  RB_OBJ_WRITE(obj, &ctx->functions, rb_ary_new());
39
39
  return obj;
40
40
  }
@@ -32,8 +32,8 @@ static void deallocate(void *ctx) {
32
32
  }
33
33
 
34
34
  static VALUE allocate(VALUE klass) {
35
- rubyDuckDBTableDescription *ctx = xcalloc((size_t)1, sizeof(rubyDuckDBTableDescription));
36
- return TypedData_Wrap_Struct(klass, &table_description_data_type, ctx);
35
+ rubyDuckDBTableDescription *ctx;
36
+ return TypedData_Make_Struct(klass, rubyDuckDBTableDescription, &table_description_data_type, ctx);
37
37
  }
38
38
 
39
39
  static size_t memsize(const void *p) {
@@ -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);
@@ -22,8 +23,6 @@ static void table_function_init_callback(duckdb_init_info info);
22
23
  static VALUE table_function_execute(VALUE self);
23
24
  static void table_function_execute_callback(duckdb_function_info info, duckdb_data_chunk output);
24
25
  #ifdef HAVE_DUCKDB_H_GE_V1_5_0
25
- /* Thread detection (declared in function_executor.c); used to skip the proxy on Ruby threads. */
26
- extern int ruby_native_thread_p(void);
27
26
  static void table_function_local_init_callback(duckdb_init_info info);
28
27
  #endif
29
28
 
@@ -71,8 +70,8 @@ static void compact(void *ctx) {
71
70
  }
72
71
 
73
72
  static VALUE allocate(VALUE klass) {
74
- rubyDuckDBTableFunction *ctx = xcalloc((size_t)1, sizeof(rubyDuckDBTableFunction));
75
- 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);
76
75
  }
77
76
 
78
77
  static size_t memsize(const void *p) {
@@ -187,21 +186,38 @@ static VALUE table_function_add_named_parameter(VALUE self, VALUE name, VALUE lo
187
186
 
188
187
  /*
189
188
  * call-seq:
190
- * table_function.bind { |bind_info| ... } -> self
189
+ * table_function.supports_projection_pushdown = bool -> bool
191
190
  *
192
- * Sets the bind callback for the table function.
193
- * The callback is called when the function is used in a query.
191
+ * Sets whether the table function supports projection pushdown.
194
192
  *
195
- * table_function.bind do |bind_info|
196
- * bind_info.add_result_column('id', DuckDB::LogicalType::BIGINT)
197
- * bind_info.add_result_column('name', DuckDB::LogicalType::VARCHAR)
198
- * end
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
199
201
  */
200
- static VALUE table_function_bind(VALUE self) {
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
+
216
+ static rubyDuckDBTableFunction *table_function_get_settable_ctx(VALUE self, const char *block_msg) {
201
217
  rubyDuckDBTableFunction *ctx;
202
218
 
203
219
  if (!rb_block_given_p()) {
204
- rb_raise(rb_eArgError, "block is required");
220
+ rb_raise(rb_eArgError, "%s", block_msg);
205
221
  }
206
222
 
207
223
  TypedData_Get_Struct(self, rubyDuckDBTableFunction, &table_function_data_type, ctx);
@@ -210,6 +226,29 @@ static VALUE table_function_bind(VALUE self) {
210
226
  rb_raise(eDuckDBError, "Table function is destroyed");
211
227
  }
212
228
 
229
+ return ctx;
230
+ }
231
+
232
+ static VALUE call_proc_with_one_arg(VALUE arg) {
233
+ VALUE *args = (VALUE *)arg;
234
+ return rb_funcall(args[0], rb_intern("call"), 1, args[1]);
235
+ }
236
+
237
+ /*
238
+ * call-seq:
239
+ * table_function.bind { |bind_info| ... } -> self
240
+ *
241
+ * Sets the bind callback for the table function.
242
+ * The callback is called when the function is used in a query.
243
+ *
244
+ * table_function.bind do |bind_info|
245
+ * bind_info.add_result_column('id', DuckDB::LogicalType::BIGINT)
246
+ * bind_info.add_result_column('name', DuckDB::LogicalType::VARCHAR)
247
+ * end
248
+ */
249
+ static VALUE table_function_bind(VALUE self) {
250
+ rubyDuckDBTableFunction *ctx = table_function_get_settable_ctx(self, "block is required");
251
+
213
252
  RB_OBJ_WRITE(self, &ctx->bind_proc, rb_block_proc());
214
253
 
215
254
  duckdb_table_function_set_bind(ctx->table_function, table_function_bind_callback);
@@ -219,11 +258,6 @@ static VALUE table_function_bind(VALUE self) {
219
258
  return self;
220
259
  }
221
260
 
222
- static VALUE call_bind_proc(VALUE arg) {
223
- VALUE *args = (VALUE *)arg;
224
- return rb_funcall(args[0], rb_intern("call"), 1, args[1]);
225
- }
226
-
227
261
  struct bind_dispatch_arg {
228
262
  rubyDuckDBTableFunction *ctx;
229
263
  duckdb_bind_info info;
@@ -240,7 +274,9 @@ static void execute_bind_callback_protected(void *user_data) {
240
274
  bind_info_ctx->bind_info = darg->info;
241
275
 
242
276
  VALUE call_args[2] = { darg->ctx->bind_proc, bind_info_obj };
243
- rb_protect(call_bind_proc, (VALUE)call_args, &state);
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;
244
280
 
245
281
  if (state) {
246
282
  VALUE msg = rbduckdb_pending_error_message();
@@ -274,17 +310,7 @@ static void table_function_bind_callback(duckdb_bind_info info) {
274
310
  * end
275
311
  */
276
312
  static VALUE table_function_init(VALUE self) {
277
- rubyDuckDBTableFunction *ctx;
278
-
279
- if (!rb_block_given_p()) {
280
- rb_raise(rb_eArgError, "block is required for init");
281
- }
282
-
283
- TypedData_Get_Struct(self, rubyDuckDBTableFunction, &table_function_data_type, ctx);
284
-
285
- if (!ctx->table_function) {
286
- rb_raise(eDuckDBError, "Table function is destroyed");
287
- }
313
+ rubyDuckDBTableFunction *ctx = table_function_get_settable_ctx(self, "block is required for init");
288
314
 
289
315
  RB_OBJ_WRITE(self, &ctx->init_proc, rb_block_proc());
290
316
  duckdb_table_function_set_init(ctx->table_function, table_function_init_callback);
@@ -294,11 +320,6 @@ static VALUE table_function_init(VALUE self) {
294
320
  return self;
295
321
  }
296
322
 
297
- static VALUE call_init_proc(VALUE args_val) {
298
- VALUE *args = (VALUE *)args_val;
299
- return rb_funcall(args[0], rb_intern("call"), 1, args[1]);
300
- }
301
-
302
323
  struct init_dispatch_arg {
303
324
  rubyDuckDBTableFunction *ctx;
304
325
  duckdb_init_info info;
@@ -315,7 +336,9 @@ static void execute_init_callback_protected(void *user_data) {
315
336
  init_info_ctx->info = darg->info;
316
337
 
317
338
  VALUE call_args[2] = { darg->ctx->init_proc, init_info_obj };
318
- rb_protect(call_init_proc, (VALUE)call_args, &state);
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;
319
342
 
320
343
  if (state) {
321
344
  VALUE msg = rbduckdb_pending_error_message();
@@ -351,13 +374,7 @@ static void table_function_init_callback(duckdb_init_info info) {
351
374
  * end
352
375
  */
353
376
  static VALUE table_function_execute(VALUE self) {
354
- rubyDuckDBTableFunction *ctx;
355
-
356
- if (!rb_block_given_p()) {
357
- rb_raise(rb_eArgError, "block is required for execute");
358
- }
359
-
360
- TypedData_Get_Struct(self, rubyDuckDBTableFunction, &table_function_data_type, ctx);
377
+ rubyDuckDBTableFunction *ctx = table_function_get_settable_ctx(self, "block is required for execute");
361
378
 
362
379
  RB_OBJ_WRITE(self, &ctx->execute_proc, rb_block_proc());
363
380
  duckdb_table_function_set_function(ctx->table_function, table_function_execute_callback);
@@ -400,6 +417,10 @@ static void execute_execute_callback_protected(void *user_data) {
400
417
 
401
418
  VALUE call_args[3] = { darg->ctx->execute_proc, func_info_obj, data_chunk_obj };
402
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);
403
424
 
404
425
  if (state) {
405
426
  VALUE msg = rbduckdb_pending_error_message();
@@ -431,48 +452,16 @@ static void table_function_execute_callback(duckdb_function_info info, duckdb_da
431
452
  /*
432
453
  * Per-worker init for the execute path (DuckDB >= 1.5.0).
433
454
  *
434
- * DuckDB calls this once on each worker thread that will run the execute
435
- * callback. We create a per-worker proxy (allocating its Ruby thread under the
436
- * GVL via the global executor, since this runs on a non-Ruby thread) and store
437
- * it as thread-local init data. The execute callback then dispatches through it
438
- * instead of the shared global executor, so workers run callbacks concurrently.
439
- * DuckDB invokes rbduckdb_worker_proxy_destroy when the local state is freed.
455
+ * Runs once on each worker thread that will execute the callback. Creates the
456
+ * worker's proxy and stores it as thread-local init data, so the execute callback
457
+ * dispatches through it rather than the shared global executor. DuckDB calls
458
+ * rbduckdb_worker_proxy_destroy when the local state is freed.
440
459
  */
441
- struct create_proxy_callback_arg {
442
- struct worker_proxy *proxy;
443
- };
444
-
445
- static VALUE create_proxy_callback(VALUE varg) {
446
- struct create_proxy_callback_arg *arg = (struct create_proxy_callback_arg *)varg;
447
- arg->proxy = rbduckdb_worker_proxy_create();
448
- return Qnil;
449
- }
450
-
451
- /*
452
- * rbduckdb_worker_proxy_create may raise (NoMemError, Thread.new failure).
453
- * Swallow it: the proxy stays NULL, local_init sets no state, and the execute
454
- * callback falls back to the global executor.
455
- */
456
- static void create_proxy_callback_protected(void *user_data) {
457
- int exception_state;
458
-
459
- rb_protect(create_proxy_callback, (VALUE)user_data, &exception_state);
460
- if (exception_state) {
461
- rb_set_errinfo(Qnil);
462
- }
463
- }
464
-
465
460
  static void table_function_local_init_callback(duckdb_init_info info) {
466
- struct create_proxy_callback_arg arg;
467
-
468
- /* A Ruby calling thread runs the callback inline (Case 1/2); no proxy needed. */
469
- if (ruby_native_thread_p()) return;
470
-
471
- arg.proxy = NULL;
472
- rbduckdb_function_executor_dispatch(create_proxy_callback_protected, &arg);
461
+ struct worker_proxy *proxy = rbduckdb_worker_proxy_create_for_current_thread();
473
462
 
474
- if (arg.proxy != NULL) {
475
- duckdb_init_set_init_data(info, arg.proxy, rbduckdb_worker_proxy_destroy);
463
+ if (proxy != NULL) {
464
+ duckdb_init_set_init_data(info, proxy, rbduckdb_worker_proxy_destroy);
476
465
  }
477
466
  }
478
467
  #endif
@@ -495,6 +484,7 @@ void rbduckdb_init_table_function(void) {
495
484
  rb_define_method(cDuckDBTableFunction, "name=", table_function_set_name, 1);
496
485
  rb_define_method(cDuckDBTableFunction, "add_parameter", table_function_add_parameter, 1);
497
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);
498
488
  rb_define_method(cDuckDBTableFunction, "bind", table_function_bind, 0);
499
489
  rb_define_method(cDuckDBTableFunction, "init", table_function_init, 0);
500
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
  }