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
data/ext/duckdb/result.c CHANGED
@@ -6,29 +6,6 @@ struct chunk_arg {
6
6
  duckdb_logical_type *types;
7
7
  };
8
8
 
9
- struct owned_type_arg {
10
- duckdb_vector vector;
11
- duckdb_logical_type type;
12
- idx_t bgn;
13
- idx_t end;
14
- };
15
-
16
- struct struct_fields_arg {
17
- duckdb_logical_type ty;
18
- duckdb_vector vector;
19
- idx_t row_idx;
20
- duckdb_logical_type child_type;
21
- };
22
-
23
- struct map_entries_arg {
24
- duckdb_vector key_vector;
25
- duckdb_vector value_vector;
26
- duckdb_logical_type key_type;
27
- duckdb_logical_type value_type;
28
- idx_t bgn;
29
- idx_t end;
30
- };
31
-
32
9
  static VALUE cDuckDBResult;
33
10
 
34
11
  static void deallocate(void *ctx);
@@ -47,38 +24,6 @@ static VALUE result__return_type(VALUE oDuckDBResult);
47
24
  static VALUE result__statement_type(VALUE oDuckDBResult);
48
25
  static VALUE result__enum_internal_type(VALUE oDuckDBResult, VALUE col_idx);
49
26
 
50
- static VALUE vector_date(void *vector_data, idx_t row_idx);
51
- static VALUE vector_timestamp(void* vector_data, idx_t row_idx);
52
- static VALUE vector_time(void* vector_data, idx_t row_idx);
53
- static VALUE vector_interval(void* vector_data, idx_t row_idx);
54
- static VALUE vector_blob(void* vector_data, idx_t row_idx);
55
- static VALUE vector_varchar(void* vector_data, idx_t row_idx);
56
- static VALUE vector_hugeint(void* vector_data, idx_t row_idx);
57
- static VALUE vector_uhugeint(void* vector_data, idx_t row_idx);
58
- static VALUE vector_decimal(duckdb_logical_type ty, void* vector_data, idx_t row_idx);
59
-
60
- static VALUE vector_timestamp_s(void* vector_data, idx_t row_idx);
61
- static VALUE vector_timestamp_ms(void* vector_data, idx_t row_idx);
62
- static VALUE vector_timestamp_ns(void* vector_data, idx_t row_idx);
63
- static VALUE vector_time_ns(void* vector_data, idx_t row_idx);
64
- static VALUE vector_enum(duckdb_logical_type ty, void* vector_data, idx_t row_idx);
65
- static VALUE vector_array(duckdb_logical_type ty, duckdb_vector vector, idx_t row_idx);
66
- static VALUE vector_list(duckdb_logical_type ty, duckdb_vector vector, void* vector_data, idx_t row_idx);
67
- static VALUE vector_map(duckdb_logical_type ty, duckdb_vector vector, void* vector_data, idx_t row_idx);
68
- static VALUE vector_struct(duckdb_logical_type ty, duckdb_vector vector, idx_t row_idx);
69
- static VALUE vector_union(duckdb_logical_type ty, duckdb_vector vector, void* vector_data, idx_t row_idx);
70
- static VALUE vector_bit(void* vector_data, idx_t row_idx);
71
- static VALUE vector_time_tz(void* vector_data, idx_t row_idx);
72
- static VALUE vector_timestamp_tz(void* vector_data, idx_t row_idx);
73
- static VALUE vector_uuid(void* vector_data, idx_t row_idx);
74
- static VALUE convert_values_in_range(VALUE varg);
75
- static VALUE destroy_owned_type(VALUE varg);
76
- static VALUE vector_values_with_owned_type(duckdb_vector vector, duckdb_logical_type type, idx_t bgn, idx_t end);
77
- static VALUE convert_map_entries(VALUE varg);
78
- static VALUE destroy_map_types(VALUE varg);
79
- static VALUE convert_struct_fields(VALUE varg);
80
- static VALUE destroy_struct_child_type(VALUE varg);
81
-
82
27
  /*
83
28
  * No RUBY_TYPED_FREE_IMMEDIATELY: deallocate can block, and must not run
84
29
  * during GC sweep: a streaming result holds a ClientContext reference; dropping the last
@@ -311,442 +256,6 @@ VALUE rbduckdb_create_result(void) {
311
256
  return allocate(cDuckDBResult);
312
257
  }
313
258
 
314
- static VALUE vector_date(void *vector_data, idx_t row_idx) {
315
- return rbduckdb_date_to_ruby(((duckdb_date *)vector_data)[row_idx]);
316
- }
317
-
318
- static VALUE vector_timestamp(void* vector_data, idx_t row_idx) {
319
- return rbduckdb_timestamp_to_ruby(((duckdb_timestamp *)vector_data)[row_idx]);
320
- }
321
-
322
- static VALUE vector_time(void* vector_data, idx_t row_idx) {
323
- return rbduckdb_time_to_ruby(((duckdb_time *)vector_data)[row_idx]);
324
- }
325
-
326
- static VALUE vector_interval(void* vector_data, idx_t row_idx) {
327
- return rbduckdb_interval_to_ruby(((duckdb_interval *)vector_data)[row_idx]);
328
- }
329
-
330
- static VALUE vector_blob(void* vector_data, idx_t row_idx) {
331
- duckdb_string_t s = (((duckdb_string_t *)vector_data)[row_idx]);
332
- if(duckdb_string_is_inlined(s)) {
333
- return rb_str_new(s.value.inlined.inlined, s.value.inlined.length);
334
- } else {
335
- return rb_str_new(s.value.pointer.ptr, s.value.pointer.length);
336
- }
337
- }
338
-
339
- static VALUE vector_varchar(void* vector_data, idx_t row_idx) {
340
- duckdb_string_t s = (((duckdb_string_t *)vector_data)[row_idx]);
341
- if(duckdb_string_is_inlined(s)) {
342
- return rb_utf8_str_new(s.value.inlined.inlined, s.value.inlined.length);
343
- } else {
344
- return rb_utf8_str_new(s.value.pointer.ptr, s.value.pointer.length);
345
- }
346
- }
347
-
348
- static VALUE vector_hugeint(void* vector_data, idx_t row_idx) {
349
- return rbduckdb_hugeint_to_ruby(((duckdb_hugeint *)vector_data)[row_idx]);
350
- }
351
-
352
- static VALUE vector_uhugeint(void* vector_data, idx_t row_idx) {
353
- return rbduckdb_uhugeint_to_ruby(((duckdb_uhugeint *)vector_data)[row_idx]);
354
- }
355
-
356
- static VALUE vector_decimal(duckdb_logical_type ty, void* vector_data, idx_t row_idx) {
357
- VALUE width = INT2FIX(duckdb_decimal_width(ty));
358
- VALUE scale = INT2FIX(duckdb_decimal_scale(ty));
359
- duckdb_type type = duckdb_decimal_internal_type(ty);
360
- duckdb_hugeint value;
361
- VALUE upper = Qnil;
362
- VALUE lower = Qnil;
363
-
364
- value.upper = 0;
365
- value.lower = 0;
366
-
367
- switch(type) {
368
- case DUCKDB_TYPE_HUGEINT:
369
- value = ((duckdb_hugeint *) vector_data)[row_idx];
370
- upper = LL2NUM(value.upper);
371
- lower = ULL2NUM(value.lower);
372
- break;
373
- case DUCKDB_TYPE_SMALLINT:
374
- upper = INT2FIX(((int16_t *) vector_data)[row_idx]);
375
- break;
376
- case DUCKDB_TYPE_INTEGER:
377
- upper = INT2NUM(((int32_t *) vector_data)[row_idx]);
378
- break;
379
- case DUCKDB_TYPE_BIGINT:
380
- upper = LL2NUM(((int64_t *) vector_data)[row_idx]);
381
- break;
382
- default:
383
- rb_warn("Unknown decimal internal type %d", type);
384
- }
385
-
386
- return rb_funcall(mDuckDBConverter, id__to_decimal_from_hugeint, 4,
387
- width,
388
- scale,
389
- upper,
390
- lower
391
- );
392
- }
393
-
394
-
395
-
396
- static VALUE vector_timestamp_s(void* vector_data, idx_t row_idx) {
397
- return rbduckdb_timestamp_s_to_ruby(((duckdb_timestamp_s *)vector_data)[row_idx]);
398
- }
399
-
400
-
401
- static VALUE vector_timestamp_ms(void* vector_data, idx_t row_idx) {
402
- return rbduckdb_timestamp_ms_to_ruby(((duckdb_timestamp_ms *)vector_data)[row_idx]);
403
- }
404
-
405
-
406
- static VALUE vector_timestamp_ns(void* vector_data, idx_t row_idx) {
407
- return rbduckdb_timestamp_ns_to_ruby(((duckdb_timestamp_ns *)vector_data)[row_idx]);
408
- }
409
-
410
- static VALUE vector_time_ns(void* vector_data, idx_t row_idx) {
411
- return rbduckdb_time_ns_to_ruby(((duckdb_time_ns *)vector_data)[row_idx]);
412
- }
413
-
414
- static VALUE vector_enum(duckdb_logical_type ty, void* vector_data, idx_t row_idx) {
415
- duckdb_type type = duckdb_enum_internal_type(ty);
416
- uint8_t index;
417
- char *p;
418
- VALUE value = Qnil;
419
-
420
- switch(type) {
421
- case DUCKDB_TYPE_UTINYINT:
422
- index = ((uint8_t *) vector_data)[row_idx];
423
- p = duckdb_enum_dictionary_value(ty, index);
424
- if (p) {
425
- value = rb_utf8_str_new_cstr(p);
426
- duckdb_free(p);
427
- }
428
- break;
429
- default:
430
- rb_warn("Unknown enum internal type %d", type);
431
- }
432
- return value;
433
- }
434
-
435
- static VALUE vector_array(duckdb_logical_type ty, duckdb_vector vector, idx_t row_idx) {
436
- idx_t size = duckdb_array_type_array_size(ty);
437
- idx_t bgn = row_idx * size;
438
- duckdb_vector child = duckdb_array_vector_get_child(vector);
439
-
440
- return vector_values_with_owned_type(child, duckdb_array_type_child_type(ty), bgn, bgn + size);
441
- }
442
-
443
- VALUE rbduckdb_vector_value_at(duckdb_vector vector, duckdb_logical_type element_type, idx_t index) {
444
- uint64_t *validity;
445
- duckdb_type type_id;
446
- void* vector_data;
447
- VALUE obj = Qnil;
448
-
449
- validity = duckdb_vector_get_validity(vector);
450
- if (!duckdb_validity_row_is_valid(validity, index)) {
451
- return Qnil;
452
- }
453
-
454
- type_id = duckdb_get_type_id(element_type);
455
- vector_data = duckdb_vector_get_data(vector);
456
-
457
- switch(type_id) {
458
- case DUCKDB_TYPE_INVALID:
459
- obj = Qnil;
460
- break;
461
- case DUCKDB_TYPE_BOOLEAN:
462
- obj = (((bool*) vector_data)[index]) ? Qtrue : Qfalse;
463
- break;
464
- case DUCKDB_TYPE_TINYINT:
465
- obj = INT2FIX(((int8_t *) vector_data)[index]);
466
- break;
467
- case DUCKDB_TYPE_SMALLINT:
468
- obj = INT2FIX(((int16_t *) vector_data)[index]);
469
- break;
470
- case DUCKDB_TYPE_INTEGER:
471
- obj = INT2NUM(((int32_t *) vector_data)[index]);
472
- break;
473
- case DUCKDB_TYPE_BIGINT:
474
- obj = LL2NUM(((int64_t *) vector_data)[index]);
475
- break;
476
- case DUCKDB_TYPE_UTINYINT:
477
- obj = INT2FIX(((uint8_t *) vector_data)[index]);
478
- break;
479
- case DUCKDB_TYPE_USMALLINT:
480
- obj = INT2FIX(((uint16_t *) vector_data)[index]);
481
- break;
482
- case DUCKDB_TYPE_UINTEGER:
483
- obj = UINT2NUM(((uint32_t *) vector_data)[index]);
484
- break;
485
- case DUCKDB_TYPE_UBIGINT:
486
- obj = ULL2NUM(((uint64_t *) vector_data)[index]);
487
- break;
488
- case DUCKDB_TYPE_FLOAT:
489
- obj = DBL2NUM((((float *) vector_data)[index]));
490
- break;
491
- case DUCKDB_TYPE_DOUBLE:
492
- obj = DBL2NUM((((double *) vector_data)[index]));
493
- break;
494
- case DUCKDB_TYPE_TIMESTAMP:
495
- obj = vector_timestamp(vector_data, index);
496
- break;
497
- case DUCKDB_TYPE_DATE:
498
- obj = vector_date(vector_data, index);
499
- break;
500
- case DUCKDB_TYPE_TIME:
501
- obj = vector_time(vector_data, index);
502
- break;
503
- case DUCKDB_TYPE_INTERVAL:
504
- obj = vector_interval(vector_data, index);
505
- break;
506
- case DUCKDB_TYPE_HUGEINT:
507
- obj = vector_hugeint(vector_data, index);
508
- break;
509
- case DUCKDB_TYPE_UHUGEINT:
510
- obj = vector_uhugeint(vector_data, index);
511
- break;
512
- case DUCKDB_TYPE_VARCHAR:
513
- obj = vector_varchar(vector_data, index);
514
- break;
515
- case DUCKDB_TYPE_BLOB:
516
- obj = vector_blob(vector_data, index);
517
- break;
518
- case DUCKDB_TYPE_DECIMAL:
519
- obj = vector_decimal(element_type, vector_data, index);
520
- break;
521
- case DUCKDB_TYPE_TIMESTAMP_S:
522
- obj = vector_timestamp_s(vector_data, index);
523
- break;
524
- case DUCKDB_TYPE_TIMESTAMP_MS:
525
- obj = vector_timestamp_ms(vector_data, index);
526
- break;
527
- case DUCKDB_TYPE_TIMESTAMP_NS:
528
- obj = vector_timestamp_ns(vector_data, index);
529
- break;
530
- case DUCKDB_TYPE_TIME_NS:
531
- obj = vector_time_ns(vector_data, index);
532
- break;
533
- case DUCKDB_TYPE_ENUM:
534
- obj = vector_enum(element_type, vector_data, index);
535
- break;
536
- case DUCKDB_TYPE_LIST:
537
- obj = vector_list(element_type, vector, vector_data, index);
538
- break;
539
- case DUCKDB_TYPE_STRUCT:
540
- obj = vector_struct(element_type, vector, index);
541
- break;
542
- case DUCKDB_TYPE_MAP:
543
- obj = vector_map(element_type, vector, vector_data, index);
544
- break;
545
- case DUCKDB_TYPE_ARRAY:
546
- obj = vector_array(element_type, vector, index);
547
- break;
548
- case DUCKDB_TYPE_UUID:
549
- obj = vector_uuid(vector_data, index);
550
- break;
551
- case DUCKDB_TYPE_UNION:
552
- obj = vector_union(element_type, vector, vector_data, index);
553
- break;
554
- case DUCKDB_TYPE_BIT:
555
- obj = vector_bit(vector_data, index);
556
- break;
557
- case DUCKDB_TYPE_TIME_TZ:
558
- obj = vector_time_tz(vector_data, index);
559
- break;
560
- case DUCKDB_TYPE_TIMESTAMP_TZ:
561
- obj = vector_timestamp_tz(vector_data, index);
562
- break;
563
- default:
564
- rb_warn("Unknown type %d", type_id);
565
- obj = Qnil;
566
- }
567
-
568
- return obj;
569
- }
570
-
571
- static VALUE vector_list(duckdb_logical_type ty, duckdb_vector vector, void * vector_data, idx_t row_idx) {
572
- duckdb_list_entry list_entry = ((duckdb_list_entry *)vector_data)[row_idx];
573
- idx_t bgn = list_entry.offset;
574
- duckdb_vector child = duckdb_list_vector_get_child(vector);
575
-
576
- return vector_values_with_owned_type(child, duckdb_list_type_child_type(ty), bgn, bgn + list_entry.length);
577
- }
578
-
579
- static VALUE vector_map(duckdb_logical_type ty, duckdb_vector vector, void* vector_data, idx_t row_idx) {
580
- struct map_entries_arg arg;
581
- duckdb_list_entry list_entry = ((duckdb_list_entry *)vector_data)[row_idx];
582
- duckdb_vector child = duckdb_list_vector_get_child(vector);
583
-
584
- arg.key_vector = duckdb_struct_vector_get_child(child, 0);
585
- arg.value_vector = duckdb_struct_vector_get_child(child, 1);
586
- arg.key_type = duckdb_map_type_key_type(ty);
587
- arg.value_type = duckdb_map_type_value_type(ty);
588
- arg.bgn = list_entry.offset;
589
- arg.end = list_entry.offset + list_entry.length;
590
-
591
- return rb_ensure(convert_map_entries, (VALUE)&arg, destroy_map_types, (VALUE)&arg);
592
- }
593
-
594
- static VALUE convert_map_entries(VALUE varg) {
595
- struct map_entries_arg *arg = (struct map_entries_arg *)varg;
596
- VALUE hash = rb_hash_new();
597
- VALUE key;
598
- VALUE value;
599
-
600
- for (idx_t i = arg->bgn; i < arg->end; ++i) {
601
- key = rbduckdb_vector_value_at(arg->key_vector, arg->key_type, i);
602
- value = rbduckdb_vector_value_at(arg->value_vector, arg->value_type, i);
603
- rb_hash_aset(hash, key, value);
604
- }
605
- return hash;
606
- }
607
-
608
- static VALUE destroy_map_types(VALUE varg) {
609
- struct map_entries_arg *arg = (struct map_entries_arg *)varg;
610
-
611
- duckdb_destroy_logical_type(&(arg->key_type));
612
- duckdb_destroy_logical_type(&(arg->value_type));
613
- return Qnil;
614
- }
615
-
616
- static VALUE vector_struct(duckdb_logical_type ty, duckdb_vector vector, idx_t row_idx) {
617
- struct struct_fields_arg arg = {ty, vector, row_idx, NULL};
618
-
619
- return rb_ensure(convert_struct_fields, (VALUE)&arg, destroy_struct_child_type, (VALUE)&arg);
620
- }
621
-
622
- static VALUE convert_struct_fields(VALUE varg) {
623
- struct struct_fields_arg *arg = (struct struct_fields_arg *)varg;
624
- VALUE hash = rb_hash_new();
625
- VALUE value = Qnil;
626
- VALUE key = Qnil;
627
- duckdb_vector child;
628
- char *p;
629
-
630
- idx_t child_count = duckdb_struct_type_child_count(arg->ty);
631
-
632
- for (idx_t i = 0; i < child_count; ++i) {
633
- p = duckdb_struct_type_child_name(arg->ty, i);
634
- if (p) {
635
- key = ID2SYM(rb_intern_const(p));
636
- duckdb_free(p);
637
- child = duckdb_struct_vector_get_child(arg->vector, i);
638
- arg->child_type = duckdb_struct_type_child_type(arg->ty, i);
639
- value = rbduckdb_vector_value_at(child, arg->child_type, arg->row_idx);
640
- duckdb_destroy_logical_type(&(arg->child_type));
641
- rb_hash_aset(hash, key, value);
642
- }
643
- }
644
-
645
- return hash;
646
- }
647
-
648
- static VALUE destroy_struct_child_type(VALUE varg) {
649
- struct struct_fields_arg *arg = (struct struct_fields_arg *)varg;
650
-
651
- duckdb_destroy_logical_type(&(arg->child_type));
652
- return Qnil;
653
- }
654
-
655
- static VALUE vector_union(duckdb_logical_type ty, duckdb_vector vector, void* vector_data, idx_t row_idx){
656
- duckdb_vector type_vector = duckdb_struct_vector_get_child(vector, 0);
657
- void *data = duckdb_vector_get_data(type_vector);
658
- uint8_t index = ((int8_t *)data)[row_idx];
659
-
660
- duckdb_vector member_vector = duckdb_struct_vector_get_child(vector, index + 1);
661
- VALUE values = vector_values_with_owned_type(member_vector, duckdb_union_type_member_type(ty, index), row_idx, row_idx + 1);
662
-
663
- return rb_ary_entry(values, 0);
664
- }
665
-
666
- static VALUE convert_values_in_range(VALUE varg) {
667
- struct owned_type_arg *arg = (struct owned_type_arg *)varg;
668
- VALUE ary = rb_ary_new2(arg->end - arg->bgn);
669
-
670
- for (idx_t i = arg->bgn; i < arg->end; ++i) {
671
- rb_ary_store(ary, i - arg->bgn, rbduckdb_vector_value_at(arg->vector, arg->type, i));
672
- }
673
- return ary;
674
- }
675
-
676
- static VALUE destroy_owned_type(VALUE varg) {
677
- struct owned_type_arg *arg = (struct owned_type_arg *)varg;
678
-
679
- duckdb_destroy_logical_type(&(arg->type));
680
- return Qnil;
681
- }
682
-
683
- static VALUE vector_values_with_owned_type(duckdb_vector vector, duckdb_logical_type type, idx_t bgn, idx_t end) {
684
- struct owned_type_arg arg = {vector, type, bgn, end};
685
-
686
- return rb_ensure(convert_values_in_range, (VALUE)&arg, destroy_owned_type, (VALUE)&arg);
687
- }
688
-
689
- static VALUE str_concat_byte(VALUE str, unsigned char byte, int offset) {
690
- char x[8];
691
- char *p = x;
692
- for (int j = 7; j >=0; j--) {
693
- if (byte % 2 == 1) {
694
- x[j] = '1';
695
- } else {
696
- x[j] = '0';
697
- }
698
- byte = (byte >> 1);
699
- }
700
- if (offset > 0 && offset < 8) {
701
- p = x + offset;
702
- }
703
- return rb_str_cat(str, p, 8 - offset);
704
- }
705
-
706
- static VALUE bytes_to_string(char *bytes, uint32_t length, int offset) {
707
- VALUE str = rb_str_new_literal("");
708
- str = str_concat_byte(str, bytes[0], offset);
709
- for (uint32_t i = 1; i < length; i++) {
710
- str = str_concat_byte(str, bytes[i], 0);
711
- }
712
- return str;
713
- }
714
-
715
- static VALUE vector_bit(void* vector_data, idx_t row_idx) {
716
- duckdb_string_t s = (((duckdb_string_t *)vector_data)[row_idx]);
717
- char *p;
718
- int offset;
719
- uint32_t length;
720
- VALUE str = Qnil;
721
-
722
- if(duckdb_string_is_inlined(s)) {
723
- length = s.value.inlined.length;
724
- p = s.value.inlined.inlined;
725
- } else {
726
- length = s.value.pointer.length;
727
- p = s.value.pointer.ptr;
728
- }
729
- if (length < 2 || (unsigned char)p[0] > 7) {
730
- rb_raise(eDuckDBError, "invalid BIT value");
731
- }
732
- offset = (unsigned char)p[0];
733
- str = bytes_to_string(p + 1, length - 1, offset);
734
- return str;
735
- }
736
-
737
- static VALUE vector_time_tz(void* vector_data, idx_t row_idx) {
738
- return rbduckdb_time_tz_to_ruby(((duckdb_time_tz *)vector_data)[row_idx]);
739
- }
740
-
741
- static VALUE vector_timestamp_tz(void* vector_data, idx_t row_idx) {
742
- duckdb_timestamp ts = {(int64_t)((duckdb_time_tz *)vector_data)[row_idx].bits};
743
- return rbduckdb_timestamp_tz_to_ruby(ts);
744
- }
745
-
746
- static VALUE vector_uuid(void* vector_data, idx_t row_idx) {
747
- return rbduckdb_uuid_to_ruby(((duckdb_hugeint *)vector_data)[row_idx]);
748
- }
749
-
750
259
  /*
751
260
  * call-seq:
752
261
  * result.arrow_c_stream -> DuckDB::ArrowArrayStream
data/ext/duckdb/result.h CHANGED
@@ -23,6 +23,5 @@ void rbduckdb_result_ref(rubyDuckDBResult *ctx);
23
23
  void rbduckdb_result_unref(rubyDuckDBResult *ctx);
24
24
  void rbduckdb_init_result(void);
25
25
  VALUE rbduckdb_create_result(void);
26
- VALUE rbduckdb_vector_value_at(duckdb_vector vector, duckdb_logical_type element_type, idx_t index);
27
26
 
28
27
  #endif
@@ -21,6 +21,7 @@
21
21
  #include "./database.h"
22
22
  #include "./connection.h"
23
23
  #include "./result.h"
24
+ #include "./vector_reader.h"
24
25
  #include "./column.h"
25
26
  #include "./logical_type.h"
26
27
  #include "./prepared_statement.h"
@@ -35,7 +36,7 @@
35
36
  #include "./instance_cache.h"
36
37
  #include "./value.h"
37
38
  #include "./function_executor.h"
38
- #include "./function_vector.h"
39
+ #include "./vector_writer.h"
39
40
  #include "./scalar_function.h"
40
41
  #include "./scalar_function_set.h"
41
42
  #include "./aggregate_function.h"
@@ -18,11 +18,6 @@ static VALUE scalar_function__set_bind(VALUE self);
18
18
  static void scalar_function_callback(duckdb_function_info info, duckdb_data_chunk input, duckdb_vector output);
19
19
  static void scalar_function_bind_callback(duckdb_bind_info info);
20
20
  #ifdef HAVE_DUCKDB_H_GE_V1_5_0
21
- /*
22
- * Thread detection functions (available since Ruby 2.3).
23
- * Used to skip the proxy on Ruby threads.
24
- */
25
- extern int ruby_native_thread_p(void);
26
21
  static void scalar_function_init_callback(duckdb_init_info info);
27
22
  #endif
28
23
 
@@ -111,8 +106,8 @@ static void compact(void *ctx) {
111
106
  }
112
107
 
113
108
  static VALUE allocate(VALUE klass) {
114
- rubyDuckDBScalarFunction *ctx = xcalloc((size_t)1, sizeof(rubyDuckDBScalarFunction));
115
- return TypedData_Wrap_Struct(klass, &scalar_function_data_type, ctx);
109
+ rubyDuckDBScalarFunction *ctx;
110
+ return TypedData_Make_Struct(klass, rubyDuckDBScalarFunction, &scalar_function_data_type, ctx);
116
111
  }
117
112
 
118
113
  static size_t memsize(const void *p) {
@@ -251,15 +246,8 @@ static VALUE process_rows(VALUE varg) {
251
246
  * a GC, and the earlier columns' objects must stay reachable. */
252
247
  VALUE args = rb_ary_new_capa((long)arg->col_count);
253
248
 
254
- /* Allocate arrays to hold input vectors and their types */
255
- arg->input_vectors = ALLOC_N(duckdb_vector, arg->col_count);
256
- arg->input_types = ALLOC_N(duckdb_logical_type, arg->col_count);
257
-
258
249
  /* Get all input vectors and their types */
259
- for (j = 0; j < arg->col_count; j++) {
260
- arg->input_vectors[j] = duckdb_data_chunk_get_vector(arg->input, j);
261
- arg->input_types[j] = duckdb_vector_get_column_type(arg->input_vectors[j]);
262
- }
250
+ rbduckdb_input_columns_load(arg->input, arg->col_count, &arg->input_vectors, &arg->input_types);
263
251
 
264
252
  /* Process each row */
265
253
  for (i = 0; i < arg->row_count; i++) {
@@ -282,24 +270,10 @@ static VALUE process_rows(VALUE varg) {
282
270
 
283
271
  static VALUE cleanup_callback(VALUE varg) {
284
272
  struct callback_arg *arg = (struct callback_arg *)varg;
285
- idx_t j;
286
273
 
287
- /* Destroy all logical types */
288
- if (arg->input_types != NULL) {
289
- for (j = 0; j < arg->col_count; j++) {
290
- duckdb_destroy_logical_type(&arg->input_types[j]);
291
- }
292
- }
274
+ rbduckdb_input_columns_free(arg->input_vectors, arg->input_types, arg->col_count);
293
275
  duckdb_destroy_logical_type(&arg->output_type);
294
276
 
295
- /* Free allocated memory */
296
- if (arg->input_types != NULL) {
297
- xfree(arg->input_types);
298
- }
299
- if (arg->input_vectors != NULL) {
300
- xfree(arg->input_vectors);
301
- }
302
-
303
277
  return Qnil;
304
278
  }
305
279
 
@@ -307,50 +281,16 @@ static VALUE cleanup_callback(VALUE varg) {
307
281
  /*
308
282
  * Per-worker init for the execute path (DuckDB >= 1.5.0).
309
283
  *
310
- * DuckDB calls this once on each worker thread that will run the execute
311
- * callback. We create a per-worker proxy (allocating its Ruby thread under the
312
- * GVL via the global executor, since this runs on a non-Ruby thread) and store
313
- * it as per-worker state. The execute callback then dispatches through it
314
- * instead of the shared global executor, so workers run callbacks concurrently.
315
- * DuckDB invokes rbduckdb_worker_proxy_destroy when the state is freed.
316
- */
317
- struct create_proxy_callback_arg {
318
- struct worker_proxy *proxy;
319
- };
320
-
321
- static VALUE create_proxy_callback(VALUE varg) {
322
- struct create_proxy_callback_arg *arg = (struct create_proxy_callback_arg *)varg;
323
- arg->proxy = rbduckdb_worker_proxy_create();
324
- return Qnil;
325
- }
326
-
327
- /*
328
- * rbduckdb_worker_proxy_create may raise (NoMemError, Thread.new failure),
329
- * and the executor runs callbacks unprotected — a raise would longjmp past
330
- * its done-signaling and block the waiting DuckDB worker forever. Swallow
331
- * the exception instead: the proxy stays NULL, init sets no state, and the
332
- * execute callback falls back to the global executor.
284
+ * Runs once on each worker thread that will execute the callback. Creates the
285
+ * worker's proxy and stores it as per-worker state, so the execute callback
286
+ * dispatches through it rather than the shared global executor. DuckDB calls
287
+ * rbduckdb_worker_proxy_destroy when the state is freed.
333
288
  */
334
- static void create_proxy_callback_protected(void *user_data) {
335
- int exception_state;
336
-
337
- rb_protect(create_proxy_callback, (VALUE)user_data, &exception_state);
338
- if (exception_state) {
339
- rb_set_errinfo(Qnil);
340
- }
341
- }
342
-
343
289
  static void scalar_function_init_callback(duckdb_init_info info) {
344
- struct create_proxy_callback_arg arg;
345
-
346
- /* A Ruby calling thread runs the callback inline (Case 1/2); no proxy needed. */
347
- if (ruby_native_thread_p()) return;
348
-
349
- arg.proxy = NULL;
350
- rbduckdb_function_executor_dispatch(create_proxy_callback_protected, &arg);
290
+ struct worker_proxy *proxy = rbduckdb_worker_proxy_create_for_current_thread();
351
291
 
352
- if (arg.proxy != NULL) {
353
- duckdb_scalar_function_init_set_state(info, arg.proxy, rbduckdb_worker_proxy_destroy);
292
+ if (proxy != NULL) {
293
+ duckdb_scalar_function_init_set_state(info, proxy, rbduckdb_worker_proxy_destroy);
354
294
  }
355
295
  }
356
296
  #endif
@@ -439,6 +379,8 @@ static void execute_bind_callback_protected(void *user_data) {
439
379
  arg.bind_info_obj = rbduckdb_scalar_function_bind_info_new(darg->info);
440
380
 
441
381
  rb_protect(call_bind_proc, (VALUE)&arg, &exception_state);
382
+ /* duckdb_bind_info is only valid during the callback. */
383
+ rbduckdb_scalar_function_bind_info_invalidate(arg.bind_info_obj);
442
384
  if (exception_state) {
443
385
  VALUE msg = rbduckdb_pending_error_message();
444
386
  duckdb_scalar_function_bind_set_error(darg->info, StringValueCStr(msg));