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
@@ -54,8 +54,8 @@ static void deallocate(void *ctx) {
54
54
  }
55
55
 
56
56
  static VALUE allocate(VALUE klass) {
57
- rubyDuckDBArrowArrayStream *ctx = xcalloc((size_t)1, sizeof(rubyDuckDBArrowArrayStream));
58
- return TypedData_Wrap_Struct(klass, &arrow_array_stream_data_type, ctx);
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) {
@@ -39,8 +39,8 @@ static void deallocate(void *ctx) {
39
39
  }
40
40
 
41
41
  static VALUE allocate(VALUE klass) {
42
- rubyDuckDBArrowConvertedSchema *ctx = xcalloc((size_t)1, sizeof(rubyDuckDBArrowConvertedSchema));
43
- return TypedData_Wrap_Struct(klass, &arrow_converted_schema_data_type, ctx);
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) {
@@ -24,8 +24,8 @@ static void deallocate(void *ctx) {
24
24
  }
25
25
 
26
26
  static VALUE allocate(VALUE klass) {
27
- rubyDuckDBClientContext *ctx = xcalloc((size_t)1, sizeof(rubyDuckDBClientContext));
28
- return TypedData_Wrap_Struct(klass, &client_context_data_type, ctx);
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 = xcalloc((size_t)1, sizeof(rubyDuckDBColumn));
26
- return TypedData_Wrap_Struct(klass, &column_data_type, ctx);
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 = xcalloc((size_t)1, sizeof(rubyDuckDBConfig));
28
- return TypedData_Wrap_Struct(klass, &config_data_type, ctx);
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) {
@@ -49,9 +49,10 @@ static void compact(void *ctx) {
49
49
  }
50
50
 
51
51
  static VALUE allocate(VALUE klass) {
52
- rubyDuckDBConnection *ctx = xcalloc((size_t)1, sizeof(rubyDuckDBConnection));
52
+ rubyDuckDBConnection *ctx;
53
+ VALUE obj = TypedData_Make_Struct(klass, rubyDuckDBConnection, &connection_data_type, ctx);
53
54
  ctx->database = Qnil;
54
- return TypedData_Wrap_Struct(klass, &connection_data_type, ctx);
55
+ return obj;
55
56
  }
56
57
 
57
58
  /* Anchors obj to this connection's database so it outlives the connection. */
@@ -251,6 +252,17 @@ static VALUE connection__get_table_names(VALUE self, VALUE query, VALUE qualifie
251
252
  return rbduckdb_value_new(table_names);
252
253
  }
253
254
 
255
+ static VALUE connection_check_registered(VALUE self, duckdb_state state, VALUE obj, const char *kind) {
256
+ if (state == DuckDBError) {
257
+ rb_raise(eDuckDBError, "Failed to register %s", kind);
258
+ }
259
+
260
+ /* Keep reference alive as long as the database can call it */
261
+ connection_retain_registered(self, obj);
262
+
263
+ return self;
264
+ }
265
+
254
266
  /* :nodoc: */
255
267
  static VALUE connection__register_logical_type(VALUE self, VALUE logical_type) {
256
268
  rubyDuckDBConnection *ctxcon;
@@ -261,15 +273,7 @@ static VALUE connection__register_logical_type(VALUE self, VALUE logical_type) {
261
273
  ctxlt = rbduckdb_get_struct_logical_type(logical_type);
262
274
 
263
275
  state = duckdb_register_logical_type(ctxcon->con, ctxlt->logical_type, NULL);
264
-
265
- if (state == DuckDBError) {
266
- rb_raise(eDuckDBError, "Failed to register logical type");
267
- }
268
-
269
- /* Keep reference alive as long as the database can call it */
270
- connection_retain_registered(self, logical_type);
271
-
272
- return self;
276
+ return connection_check_registered(self, state, logical_type, "logical type");
273
277
  }
274
278
 
275
279
  /* :nodoc: */
@@ -282,15 +286,7 @@ static VALUE connection__register_scalar_function(VALUE self, VALUE scalar_funct
282
286
  ctxsf = rbduckdb_get_struct_scalar_function(scalar_function);
283
287
 
284
288
  state = duckdb_register_scalar_function(ctxcon->con, ctxsf->scalar_function);
285
-
286
- if (state == DuckDBError) {
287
- rb_raise(eDuckDBError, "Failed to register scalar function");
288
- }
289
-
290
- /* Keep reference alive as long as the database can call it */
291
- connection_retain_registered(self, scalar_function);
292
-
293
- return self;
289
+ return connection_check_registered(self, state, scalar_function, "scalar function");
294
290
  }
295
291
 
296
292
  /* :nodoc: */
@@ -303,15 +299,7 @@ static VALUE connection__register_scalar_function_set(VALUE self, VALUE scalar_f
303
299
  ctxsfs = rbduckdb_get_struct_scalar_function_set(scalar_function_set);
304
300
 
305
301
  state = duckdb_register_scalar_function_set(ctxcon->con, ctxsfs->scalar_function_set);
306
-
307
- if (state == DuckDBError) {
308
- rb_raise(eDuckDBError, "Failed to register scalar function set");
309
- }
310
-
311
- /* Keep reference alive as long as the database can call it */
312
- connection_retain_registered(self, scalar_function_set);
313
-
314
- return self;
302
+ return connection_check_registered(self, state, scalar_function_set, "scalar function set");
315
303
  }
316
304
 
317
305
  /* :nodoc: */
@@ -324,15 +312,7 @@ static VALUE connection__register_aggregate_function(VALUE self, VALUE aggregate
324
312
  ctxaf = rbduckdb_get_struct_aggregate_function(aggregate_function);
325
313
 
326
314
  state = duckdb_register_aggregate_function(ctxcon->con, ctxaf->aggregate_function);
327
-
328
- if (state == DuckDBError) {
329
- rb_raise(eDuckDBError, "Failed to register aggregate function");
330
- }
331
-
332
- /* Keep reference alive as long as the database can call it */
333
- connection_retain_registered(self, aggregate_function);
334
-
335
- return self;
315
+ return connection_check_registered(self, state, aggregate_function, "aggregate function");
336
316
  }
337
317
 
338
318
  /* :nodoc: */
@@ -345,15 +325,7 @@ static VALUE connection__register_aggregate_function_set(VALUE self, VALUE aggre
345
325
  ctxafs = rbduckdb_get_struct_aggregate_function_set(aggregate_function_set);
346
326
 
347
327
  state = duckdb_register_aggregate_function_set(ctxcon->con, ctxafs->aggregate_function_set);
348
-
349
- if (state == DuckDBError) {
350
- rb_raise(eDuckDBError, "Failed to register aggregate function set");
351
- }
352
-
353
- /* Keep reference alive as long as the database can call it */
354
- connection_retain_registered(self, aggregate_function_set);
355
-
356
- return self;
328
+ return connection_check_registered(self, state, aggregate_function_set, "aggregate function set");
357
329
  }
358
330
 
359
331
  static VALUE connection__register_table_function(VALUE self, VALUE table_function) {
@@ -365,15 +337,7 @@ static VALUE connection__register_table_function(VALUE self, VALUE table_functio
365
337
  ctxtf = rbduckdb_get_struct_table_function(table_function);
366
338
 
367
339
  state = duckdb_register_table_function(ctxcon->con, ctxtf->table_function);
368
-
369
- if (state == DuckDBError) {
370
- rb_raise(eDuckDBError, "Failed to register table function");
371
- }
372
-
373
- /* Keep reference alive as long as the database can call it */
374
- connection_retain_registered(self, table_function);
375
-
376
- return self;
340
+ return connection_check_registered(self, state, table_function, "table function");
377
341
  }
378
342
 
379
343
  void rbduckdb_init_connection(void) {
@@ -17,49 +17,8 @@ ID id__to_time_from_duckdb_timestamp_tz;
17
17
  ID id__to_infinity;
18
18
  ID id__decimal_to_unscaled;
19
19
 
20
- static VALUE infinite_date_value(duckdb_date date) {
21
- if (duckdb_is_finite_date(date) == false) {
22
- return rb_funcall(mDuckDBConverter, id__to_infinity, 1,
23
- INT2NUM(date.days)
24
- );
25
- }
26
- return Qnil;
27
- }
28
-
29
- static VALUE infinite_timestamp_value(duckdb_timestamp timestamp) {
30
- if (duckdb_is_finite_timestamp(timestamp) == false) {
31
- return rb_funcall(mDuckDBConverter, id__to_infinity, 1,
32
- LL2NUM(timestamp.micros)
33
- );
34
- }
35
- return Qnil;
36
- }
37
-
38
- static VALUE infinite_timestamp_s_value(duckdb_timestamp_s timestamp_s) {
39
- if (duckdb_is_finite_timestamp_s(timestamp_s) == false) {
40
- return rb_funcall(mDuckDBConverter, id__to_infinity, 1,
41
- LL2NUM(timestamp_s.seconds)
42
- );
43
- }
44
- return Qnil;
45
- }
46
-
47
- static VALUE infinite_timestamp_ms_value(duckdb_timestamp_ms timestamp_ms) {
48
- if (duckdb_is_finite_timestamp_ms(timestamp_ms) == false) {
49
- return rb_funcall(mDuckDBConverter, id__to_infinity, 1,
50
- LL2NUM(timestamp_ms.millis)
51
- );
52
- }
53
- return Qnil;
54
- }
55
-
56
- static VALUE infinite_timestamp_ns_value(duckdb_timestamp_ns timestamp_ns) {
57
- if (duckdb_is_finite_timestamp_ns(timestamp_ns) == false) {
58
- return rb_funcall(mDuckDBConverter, id__to_infinity, 1,
59
- LL2NUM(timestamp_ns.nanos)
60
- );
61
- }
62
- return Qnil;
20
+ static VALUE ruby_infinity(VALUE raw) {
21
+ return rb_funcall(mDuckDBConverter, id__to_infinity, 1, raw);
63
22
  }
64
23
 
65
24
  #define DUCKDB_UUID_SIGN_BIT 0x8000000000000000ULL
@@ -136,12 +95,6 @@ VALUE rbduckdb_uuid_to_ruby(duckdb_hugeint h) {
136
95
  return rb_utf8_str_new(buf, 36);
137
96
  }
138
97
 
139
- VALUE rbduckdb_uuid_uhugeint_to_ruby(duckdb_uhugeint h) {
140
- char buf[36];
141
- uuid_to_str(h.upper, h.lower, buf);
142
- return rb_utf8_str_new(buf, 36);
143
- }
144
-
145
98
  // Returns the nibble value (0-15) for a hex character, or -1 if invalid.
146
99
  static int hex_nibble(unsigned char c)
147
100
  {
@@ -256,9 +209,8 @@ VALUE rbduckdb_uhugeint_to_ruby(duckdb_uhugeint h) {
256
209
  }
257
210
 
258
211
  VALUE rbduckdb_timestamp_s_to_ruby(duckdb_timestamp_s ts) {
259
- VALUE obj = infinite_timestamp_s_value(ts);
260
- if (obj != Qnil) {
261
- return obj;
212
+ if (!duckdb_is_finite_timestamp_s(ts)) {
213
+ return ruby_infinity(LL2NUM(ts.seconds));
262
214
  }
263
215
  return rb_funcall(mDuckDBConverter, id__to_time_from_duckdb_timestamp_s, 1,
264
216
  LL2NUM(ts.seconds)
@@ -266,9 +218,8 @@ VALUE rbduckdb_timestamp_s_to_ruby(duckdb_timestamp_s ts) {
266
218
  }
267
219
 
268
220
  VALUE rbduckdb_timestamp_ms_to_ruby(duckdb_timestamp_ms ts) {
269
- VALUE obj = infinite_timestamp_ms_value(ts);
270
- if (obj != Qnil) {
271
- return obj;
221
+ if (!duckdb_is_finite_timestamp_ms(ts)) {
222
+ return ruby_infinity(LL2NUM(ts.millis));
272
223
  }
273
224
  return rb_funcall(mDuckDBConverter, id__to_time_from_duckdb_timestamp_ms, 1,
274
225
  LL2NUM(ts.millis)
@@ -276,9 +227,8 @@ VALUE rbduckdb_timestamp_ms_to_ruby(duckdb_timestamp_ms ts) {
276
227
  }
277
228
 
278
229
  VALUE rbduckdb_timestamp_ns_to_ruby(duckdb_timestamp_ns ts) {
279
- VALUE obj = infinite_timestamp_ns_value(ts);
280
- if (obj != Qnil) {
281
- return obj;
230
+ if (!duckdb_is_finite_timestamp_ns(ts)) {
231
+ return ruby_infinity(LL2NUM(ts.nanos));
282
232
  }
283
233
  return rb_funcall(mDuckDBConverter, id__to_time_from_duckdb_timestamp_ns, 1,
284
234
  LL2NUM(ts.nanos)
@@ -319,35 +269,42 @@ VALUE rbduckdb_time_to_ruby(duckdb_time t) {
319
269
  }
320
270
 
321
271
  VALUE rbduckdb_date_to_ruby(duckdb_date date) {
322
- VALUE obj = infinite_date_value(date);
272
+ duckdb_date_struct date_st;
323
273
 
324
- if (obj == Qnil) {
325
- duckdb_date_struct date_st = duckdb_from_date(date);
326
- obj = rb_funcall(mDuckDBConverter, id__to_date, 3,
327
- INT2FIX(date_st.year),
328
- INT2FIX(date_st.month),
329
- INT2FIX(date_st.day)
330
- );
274
+ if (!duckdb_is_finite_date(date)) {
275
+ return ruby_infinity(INT2NUM(date.days));
331
276
  }
332
- return obj;
277
+ date_st = duckdb_from_date(date);
278
+ return rb_funcall(mDuckDBConverter, id__to_date, 3,
279
+ INT2FIX(date_st.year),
280
+ INT2FIX(date_st.month),
281
+ INT2FIX(date_st.day)
282
+ );
333
283
  }
334
284
 
335
285
  VALUE rbduckdb_timestamp_to_ruby(duckdb_timestamp ts) {
336
- VALUE obj = infinite_timestamp_value(ts);
286
+ duckdb_timestamp_struct data_st;
337
287
 
338
- if (obj == Qnil) {
339
- duckdb_timestamp_struct data_st = duckdb_from_timestamp(ts);
340
- obj = rb_funcall(mDuckDBConverter, id__to_time, 7,
341
- INT2FIX(data_st.date.year),
342
- INT2FIX(data_st.date.month),
343
- INT2FIX(data_st.date.day),
344
- INT2FIX(data_st.time.hour),
345
- INT2FIX(data_st.time.min),
346
- INT2FIX(data_st.time.sec),
347
- INT2NUM(data_st.time.micros)
348
- );
288
+ if (!duckdb_is_finite_timestamp(ts)) {
289
+ return ruby_infinity(LL2NUM(ts.micros));
349
290
  }
350
- return obj;
291
+ /*
292
+ * duckdb_from_timestamp throws a C++ exception below this value
293
+ * (days * MICROS_PER_DAY overflows int64), which would abort the process.
294
+ */
295
+ if (ts.micros < -9223372022400000000LL) {
296
+ rb_raise(rb_eRangeError, "timestamp out of range: %lld microseconds", (long long)ts.micros);
297
+ }
298
+ data_st = duckdb_from_timestamp(ts);
299
+ return rb_funcall(mDuckDBConverter, id__to_time, 7,
300
+ INT2FIX(data_st.date.year),
301
+ INT2FIX(data_st.date.month),
302
+ INT2FIX(data_st.date.day),
303
+ INT2FIX(data_st.time.hour),
304
+ INT2FIX(data_st.time.min),
305
+ INT2FIX(data_st.time.sec),
306
+ INT2NUM(data_st.time.micros)
307
+ );
351
308
  }
352
309
 
353
310
  void rbduckdb_init_converter(void) {
@@ -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);
@@ -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 = xcalloc((size_t)1, sizeof(rubyDuckDBDataChunk));
34
- return TypedData_Wrap_Struct(klass, &data_chunk_data_type, ctx);
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
- return sizeof(rubyDuckDBDataChunk);
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->owned && ctx->data_chunk) {
74
- duckdb_destroy_data_chunk(&(ctx->data_chunk));
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 < RARRAY_LEN(logical_types); i++) {
82
- VALUE logical_type = rb_ary_entry(logical_types, i);
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
- TypedData_Get_Struct(self, rubyDuckDBDataChunk, &data_chunk_data_type, ctx);
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
- TypedData_Get_Struct(self, rubyDuckDBDataChunk, &data_chunk_data_type, ctx);
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
- TypedData_Get_Struct(self, rubyDuckDBDataChunk, &data_chunk_data_type, ctx);
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
- // Create Vector wrapper
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
- TypedData_Get_Struct(self, rubyDuckDBDataChunk, &data_chunk_data_type, ctx);
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
  }