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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +23 -0
- data/ext/duckdb/aggregate_function.c +2 -2
- data/ext/duckdb/aggregate_function_set.c +2 -2
- data/ext/duckdb/appender.c +9 -10
- data/ext/duckdb/arrow_array_stream.c +2 -2
- data/ext/duckdb/arrow_import.c +2 -2
- data/ext/duckdb/client_context.c +2 -2
- data/ext/duckdb/column.c +2 -2
- data/ext/duckdb/config.c +2 -2
- data/ext/duckdb/connection.c +3 -2
- data/ext/duckdb/{conveter.c → converter.c} +0 -6
- data/ext/duckdb/converter.h +0 -1
- data/ext/duckdb/data_chunk.c +171 -26
- data/ext/duckdb/data_chunk.h +12 -0
- data/ext/duckdb/database.c +2 -2
- data/ext/duckdb/expression.c +2 -2
- data/ext/duckdb/extracted_statements.c +3 -3
- data/ext/duckdb/instance_cache.c +3 -2
- data/ext/duckdb/logical_type.c +2 -2
- data/ext/duckdb/memory_helper.c +17 -4
- data/ext/duckdb/pending_result.c +3 -2
- data/ext/duckdb/prepared_statement.c +2 -2
- data/ext/duckdb/result.c +0 -422
- data/ext/duckdb/result.h +0 -1
- data/ext/duckdb/ruby-duckdb.h +2 -1
- data/ext/duckdb/scalar_function.c +4 -2
- data/ext/duckdb/scalar_function_bind_info.c +37 -8
- data/ext/duckdb/scalar_function_bind_info.h +1 -0
- data/ext/duckdb/scalar_function_set.c +2 -2
- data/ext/duckdb/table_description.c +2 -2
- data/ext/duckdb/table_function.c +41 -2
- data/ext/duckdb/table_function_bind_info.c +30 -15
- data/ext/duckdb/table_function_function_info.c +38 -4
- data/ext/duckdb/table_function_init_info.c +72 -13
- data/ext/duckdb/table_function_init_info.h +1 -0
- data/ext/duckdb/value.c +13 -159
- data/ext/duckdb/vector.c +48 -13
- data/ext/duckdb/vector.h +5 -1
- data/ext/duckdb/vector_reader.c +544 -0
- data/ext/duckdb/vector_reader.h +7 -0
- data/ext/duckdb/{function_vector.c → vector_writer.c} +68 -7
- data/ext/duckdb/{function_vector.h → vector_writer.h} +14 -6
- data/lib/duckdb/appender.rb +27 -31
- data/lib/duckdb/data_chunk.rb +4 -89
- data/lib/duckdb/prepared_statement.rb +14 -39
- data/lib/duckdb/ruby_type_inference.rb +91 -0
- data/lib/duckdb/version.rb +1 -1
- data/lib/duckdb.rb +1 -0
- metadata +7 -4
data/ext/duckdb/pending_result.c
CHANGED
|
@@ -55,9 +55,10 @@ static void *destroy_pending_without_gvl(void *ctx) {
|
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
static VALUE allocate(VALUE klass) {
|
|
58
|
-
rubyDuckDBPendingResult *ctx
|
|
58
|
+
rubyDuckDBPendingResult *ctx;
|
|
59
|
+
VALUE obj = TypedData_Make_Struct(klass, rubyDuckDBPendingResult, &pending_result_data_type, ctx);
|
|
59
60
|
ctx->state = DUCKDB_PENDING_RESULT_NOT_READY;
|
|
60
|
-
return
|
|
61
|
+
return obj;
|
|
61
62
|
}
|
|
62
63
|
|
|
63
64
|
static size_t memsize(const void *p) {
|
|
@@ -73,8 +73,8 @@ static void deallocate(void *ctx) {
|
|
|
73
73
|
}
|
|
74
74
|
|
|
75
75
|
static VALUE allocate(VALUE klass) {
|
|
76
|
-
rubyDuckDBPreparedStatement *ctx
|
|
77
|
-
return
|
|
76
|
+
rubyDuckDBPreparedStatement *ctx;
|
|
77
|
+
return TypedData_Make_Struct(klass, rubyDuckDBPreparedStatement, &prepared_statement_data_type, ctx);
|
|
78
78
|
}
|
|
79
79
|
|
|
80
80
|
static size_t memsize(const void *p) {
|
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,28 +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_blob(void* vector_data, idx_t row_idx);
|
|
51
|
-
static VALUE vector_varchar(void* vector_data, idx_t row_idx);
|
|
52
|
-
static VALUE vector_decimal(duckdb_logical_type ty, void* vector_data, idx_t row_idx);
|
|
53
|
-
|
|
54
|
-
static VALUE vector_enum(duckdb_logical_type ty, void* vector_data, idx_t row_idx);
|
|
55
|
-
static VALUE vector_array(duckdb_logical_type ty, duckdb_vector vector, idx_t row_idx);
|
|
56
|
-
static VALUE vector_list(duckdb_logical_type ty, duckdb_vector vector, void* vector_data, idx_t row_idx);
|
|
57
|
-
static VALUE vector_map(duckdb_logical_type ty, duckdb_vector vector, void* vector_data, idx_t row_idx);
|
|
58
|
-
static VALUE vector_struct(duckdb_logical_type ty, duckdb_vector vector, idx_t row_idx);
|
|
59
|
-
static VALUE vector_union(duckdb_logical_type ty, duckdb_vector vector, void* vector_data, idx_t row_idx);
|
|
60
|
-
static VALUE vector_bit(void* vector_data, idx_t row_idx);
|
|
61
|
-
static VALUE vector_time_tz(void* vector_data, idx_t row_idx);
|
|
62
|
-
static VALUE vector_timestamp_tz(void* vector_data, idx_t row_idx);
|
|
63
|
-
static VALUE vector_uuid(void* vector_data, idx_t row_idx);
|
|
64
|
-
static VALUE convert_values_in_range(VALUE varg);
|
|
65
|
-
static VALUE destroy_owned_type(VALUE varg);
|
|
66
|
-
static VALUE vector_values_with_owned_type(duckdb_vector vector, duckdb_logical_type type, idx_t bgn, idx_t end);
|
|
67
|
-
static VALUE convert_map_entries(VALUE varg);
|
|
68
|
-
static VALUE destroy_map_types(VALUE varg);
|
|
69
|
-
static VALUE convert_struct_fields(VALUE varg);
|
|
70
|
-
static VALUE destroy_struct_child_type(VALUE varg);
|
|
71
|
-
|
|
72
27
|
/*
|
|
73
28
|
* No RUBY_TYPED_FREE_IMMEDIATELY: deallocate can block, and must not run
|
|
74
29
|
* during GC sweep: a streaming result holds a ClientContext reference; dropping the last
|
|
@@ -301,383 +256,6 @@ VALUE rbduckdb_create_result(void) {
|
|
|
301
256
|
return allocate(cDuckDBResult);
|
|
302
257
|
}
|
|
303
258
|
|
|
304
|
-
static VALUE vector_blob(void* vector_data, idx_t row_idx) {
|
|
305
|
-
duckdb_string_t s = (((duckdb_string_t *)vector_data)[row_idx]);
|
|
306
|
-
return rb_str_new(duckdb_string_t_data(&s), duckdb_string_t_length(s));
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
static VALUE vector_varchar(void* vector_data, idx_t row_idx) {
|
|
310
|
-
duckdb_string_t s = (((duckdb_string_t *)vector_data)[row_idx]);
|
|
311
|
-
return rb_utf8_str_new(duckdb_string_t_data(&s), duckdb_string_t_length(s));
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
static VALUE vector_decimal(duckdb_logical_type ty, void* vector_data, idx_t row_idx) {
|
|
315
|
-
VALUE width = INT2FIX(duckdb_decimal_width(ty));
|
|
316
|
-
VALUE scale = INT2FIX(duckdb_decimal_scale(ty));
|
|
317
|
-
duckdb_type type = duckdb_decimal_internal_type(ty);
|
|
318
|
-
duckdb_hugeint value;
|
|
319
|
-
VALUE upper = Qnil;
|
|
320
|
-
VALUE lower = Qnil;
|
|
321
|
-
|
|
322
|
-
value.upper = 0;
|
|
323
|
-
value.lower = 0;
|
|
324
|
-
|
|
325
|
-
switch(type) {
|
|
326
|
-
case DUCKDB_TYPE_HUGEINT:
|
|
327
|
-
value = ((duckdb_hugeint *) vector_data)[row_idx];
|
|
328
|
-
upper = LL2NUM(value.upper);
|
|
329
|
-
lower = ULL2NUM(value.lower);
|
|
330
|
-
break;
|
|
331
|
-
case DUCKDB_TYPE_SMALLINT:
|
|
332
|
-
upper = INT2FIX(((int16_t *) vector_data)[row_idx]);
|
|
333
|
-
break;
|
|
334
|
-
case DUCKDB_TYPE_INTEGER:
|
|
335
|
-
upper = INT2NUM(((int32_t *) vector_data)[row_idx]);
|
|
336
|
-
break;
|
|
337
|
-
case DUCKDB_TYPE_BIGINT:
|
|
338
|
-
upper = LL2NUM(((int64_t *) vector_data)[row_idx]);
|
|
339
|
-
break;
|
|
340
|
-
default:
|
|
341
|
-
rb_warn("Unknown decimal internal type %d", type);
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
return rb_funcall(mDuckDBConverter, id__to_decimal_from_hugeint, 4,
|
|
345
|
-
width,
|
|
346
|
-
scale,
|
|
347
|
-
upper,
|
|
348
|
-
lower
|
|
349
|
-
);
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
static VALUE vector_enum(duckdb_logical_type ty, void* vector_data, idx_t row_idx) {
|
|
353
|
-
duckdb_type type = duckdb_enum_internal_type(ty);
|
|
354
|
-
uint8_t index;
|
|
355
|
-
char *p;
|
|
356
|
-
VALUE value = Qnil;
|
|
357
|
-
|
|
358
|
-
switch(type) {
|
|
359
|
-
case DUCKDB_TYPE_UTINYINT:
|
|
360
|
-
index = ((uint8_t *) vector_data)[row_idx];
|
|
361
|
-
p = duckdb_enum_dictionary_value(ty, index);
|
|
362
|
-
if (p) {
|
|
363
|
-
value = rb_utf8_str_new_cstr(p);
|
|
364
|
-
duckdb_free(p);
|
|
365
|
-
}
|
|
366
|
-
break;
|
|
367
|
-
default:
|
|
368
|
-
rb_warn("Unknown enum internal type %d", type);
|
|
369
|
-
}
|
|
370
|
-
return value;
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
static VALUE vector_array(duckdb_logical_type ty, duckdb_vector vector, idx_t row_idx) {
|
|
374
|
-
idx_t size = duckdb_array_type_array_size(ty);
|
|
375
|
-
idx_t bgn = row_idx * size;
|
|
376
|
-
duckdb_vector child = duckdb_array_vector_get_child(vector);
|
|
377
|
-
|
|
378
|
-
return vector_values_with_owned_type(child, duckdb_array_type_child_type(ty), bgn, bgn + size);
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
VALUE rbduckdb_vector_value_at(duckdb_vector vector, duckdb_logical_type element_type, idx_t index) {
|
|
382
|
-
uint64_t *validity;
|
|
383
|
-
duckdb_type type_id;
|
|
384
|
-
void* vector_data;
|
|
385
|
-
VALUE obj = Qnil;
|
|
386
|
-
|
|
387
|
-
validity = duckdb_vector_get_validity(vector);
|
|
388
|
-
if (!duckdb_validity_row_is_valid(validity, index)) {
|
|
389
|
-
return Qnil;
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
type_id = duckdb_get_type_id(element_type);
|
|
393
|
-
vector_data = duckdb_vector_get_data(vector);
|
|
394
|
-
|
|
395
|
-
switch(type_id) {
|
|
396
|
-
case DUCKDB_TYPE_INVALID:
|
|
397
|
-
obj = Qnil;
|
|
398
|
-
break;
|
|
399
|
-
case DUCKDB_TYPE_BOOLEAN:
|
|
400
|
-
obj = (((bool*) vector_data)[index]) ? Qtrue : Qfalse;
|
|
401
|
-
break;
|
|
402
|
-
case DUCKDB_TYPE_TINYINT:
|
|
403
|
-
obj = INT2FIX(((int8_t *) vector_data)[index]);
|
|
404
|
-
break;
|
|
405
|
-
case DUCKDB_TYPE_SMALLINT:
|
|
406
|
-
obj = INT2FIX(((int16_t *) vector_data)[index]);
|
|
407
|
-
break;
|
|
408
|
-
case DUCKDB_TYPE_INTEGER:
|
|
409
|
-
obj = INT2NUM(((int32_t *) vector_data)[index]);
|
|
410
|
-
break;
|
|
411
|
-
case DUCKDB_TYPE_BIGINT:
|
|
412
|
-
obj = LL2NUM(((int64_t *) vector_data)[index]);
|
|
413
|
-
break;
|
|
414
|
-
case DUCKDB_TYPE_UTINYINT:
|
|
415
|
-
obj = INT2FIX(((uint8_t *) vector_data)[index]);
|
|
416
|
-
break;
|
|
417
|
-
case DUCKDB_TYPE_USMALLINT:
|
|
418
|
-
obj = INT2FIX(((uint16_t *) vector_data)[index]);
|
|
419
|
-
break;
|
|
420
|
-
case DUCKDB_TYPE_UINTEGER:
|
|
421
|
-
obj = UINT2NUM(((uint32_t *) vector_data)[index]);
|
|
422
|
-
break;
|
|
423
|
-
case DUCKDB_TYPE_UBIGINT:
|
|
424
|
-
obj = ULL2NUM(((uint64_t *) vector_data)[index]);
|
|
425
|
-
break;
|
|
426
|
-
case DUCKDB_TYPE_FLOAT:
|
|
427
|
-
obj = DBL2NUM((((float *) vector_data)[index]));
|
|
428
|
-
break;
|
|
429
|
-
case DUCKDB_TYPE_DOUBLE:
|
|
430
|
-
obj = DBL2NUM((((double *) vector_data)[index]));
|
|
431
|
-
break;
|
|
432
|
-
case DUCKDB_TYPE_TIMESTAMP:
|
|
433
|
-
obj = rbduckdb_timestamp_to_ruby(((duckdb_timestamp *)vector_data)[index]);
|
|
434
|
-
break;
|
|
435
|
-
case DUCKDB_TYPE_DATE:
|
|
436
|
-
obj = rbduckdb_date_to_ruby(((duckdb_date *)vector_data)[index]);
|
|
437
|
-
break;
|
|
438
|
-
case DUCKDB_TYPE_TIME:
|
|
439
|
-
obj = rbduckdb_time_to_ruby(((duckdb_time *)vector_data)[index]);
|
|
440
|
-
break;
|
|
441
|
-
case DUCKDB_TYPE_INTERVAL:
|
|
442
|
-
obj = rbduckdb_interval_to_ruby(((duckdb_interval *)vector_data)[index]);
|
|
443
|
-
break;
|
|
444
|
-
case DUCKDB_TYPE_HUGEINT:
|
|
445
|
-
obj = rbduckdb_hugeint_to_ruby(((duckdb_hugeint *)vector_data)[index]);
|
|
446
|
-
break;
|
|
447
|
-
case DUCKDB_TYPE_UHUGEINT:
|
|
448
|
-
obj = rbduckdb_uhugeint_to_ruby(((duckdb_uhugeint *)vector_data)[index]);
|
|
449
|
-
break;
|
|
450
|
-
case DUCKDB_TYPE_VARCHAR:
|
|
451
|
-
obj = vector_varchar(vector_data, index);
|
|
452
|
-
break;
|
|
453
|
-
case DUCKDB_TYPE_BLOB:
|
|
454
|
-
obj = vector_blob(vector_data, index);
|
|
455
|
-
break;
|
|
456
|
-
case DUCKDB_TYPE_DECIMAL:
|
|
457
|
-
obj = vector_decimal(element_type, vector_data, index);
|
|
458
|
-
break;
|
|
459
|
-
case DUCKDB_TYPE_TIMESTAMP_S:
|
|
460
|
-
obj = rbduckdb_timestamp_s_to_ruby(((duckdb_timestamp_s *)vector_data)[index]);
|
|
461
|
-
break;
|
|
462
|
-
case DUCKDB_TYPE_TIMESTAMP_MS:
|
|
463
|
-
obj = rbduckdb_timestamp_ms_to_ruby(((duckdb_timestamp_ms *)vector_data)[index]);
|
|
464
|
-
break;
|
|
465
|
-
case DUCKDB_TYPE_TIMESTAMP_NS:
|
|
466
|
-
obj = rbduckdb_timestamp_ns_to_ruby(((duckdb_timestamp_ns *)vector_data)[index]);
|
|
467
|
-
break;
|
|
468
|
-
case DUCKDB_TYPE_TIME_NS:
|
|
469
|
-
obj = rbduckdb_time_ns_to_ruby(((duckdb_time_ns *)vector_data)[index]);
|
|
470
|
-
break;
|
|
471
|
-
case DUCKDB_TYPE_ENUM:
|
|
472
|
-
obj = vector_enum(element_type, vector_data, index);
|
|
473
|
-
break;
|
|
474
|
-
case DUCKDB_TYPE_LIST:
|
|
475
|
-
obj = vector_list(element_type, vector, vector_data, index);
|
|
476
|
-
break;
|
|
477
|
-
case DUCKDB_TYPE_STRUCT:
|
|
478
|
-
obj = vector_struct(element_type, vector, index);
|
|
479
|
-
break;
|
|
480
|
-
case DUCKDB_TYPE_MAP:
|
|
481
|
-
obj = vector_map(element_type, vector, vector_data, index);
|
|
482
|
-
break;
|
|
483
|
-
case DUCKDB_TYPE_ARRAY:
|
|
484
|
-
obj = vector_array(element_type, vector, index);
|
|
485
|
-
break;
|
|
486
|
-
case DUCKDB_TYPE_UUID:
|
|
487
|
-
obj = vector_uuid(vector_data, index);
|
|
488
|
-
break;
|
|
489
|
-
case DUCKDB_TYPE_UNION:
|
|
490
|
-
obj = vector_union(element_type, vector, vector_data, index);
|
|
491
|
-
break;
|
|
492
|
-
case DUCKDB_TYPE_BIT:
|
|
493
|
-
obj = vector_bit(vector_data, index);
|
|
494
|
-
break;
|
|
495
|
-
case DUCKDB_TYPE_TIME_TZ:
|
|
496
|
-
obj = vector_time_tz(vector_data, index);
|
|
497
|
-
break;
|
|
498
|
-
case DUCKDB_TYPE_TIMESTAMP_TZ:
|
|
499
|
-
obj = vector_timestamp_tz(vector_data, index);
|
|
500
|
-
break;
|
|
501
|
-
default:
|
|
502
|
-
rb_warn("Unknown type %d", type_id);
|
|
503
|
-
obj = Qnil;
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
return obj;
|
|
507
|
-
}
|
|
508
|
-
|
|
509
|
-
static VALUE vector_list(duckdb_logical_type ty, duckdb_vector vector, void * vector_data, idx_t row_idx) {
|
|
510
|
-
duckdb_list_entry list_entry = ((duckdb_list_entry *)vector_data)[row_idx];
|
|
511
|
-
idx_t bgn = list_entry.offset;
|
|
512
|
-
duckdb_vector child = duckdb_list_vector_get_child(vector);
|
|
513
|
-
|
|
514
|
-
return vector_values_with_owned_type(child, duckdb_list_type_child_type(ty), bgn, bgn + list_entry.length);
|
|
515
|
-
}
|
|
516
|
-
|
|
517
|
-
static VALUE vector_map(duckdb_logical_type ty, duckdb_vector vector, void* vector_data, idx_t row_idx) {
|
|
518
|
-
struct map_entries_arg arg;
|
|
519
|
-
duckdb_list_entry list_entry = ((duckdb_list_entry *)vector_data)[row_idx];
|
|
520
|
-
duckdb_vector child = duckdb_list_vector_get_child(vector);
|
|
521
|
-
|
|
522
|
-
arg.key_vector = duckdb_struct_vector_get_child(child, 0);
|
|
523
|
-
arg.value_vector = duckdb_struct_vector_get_child(child, 1);
|
|
524
|
-
arg.key_type = duckdb_map_type_key_type(ty);
|
|
525
|
-
arg.value_type = duckdb_map_type_value_type(ty);
|
|
526
|
-
arg.bgn = list_entry.offset;
|
|
527
|
-
arg.end = list_entry.offset + list_entry.length;
|
|
528
|
-
|
|
529
|
-
return rb_ensure(convert_map_entries, (VALUE)&arg, destroy_map_types, (VALUE)&arg);
|
|
530
|
-
}
|
|
531
|
-
|
|
532
|
-
static VALUE convert_map_entries(VALUE varg) {
|
|
533
|
-
struct map_entries_arg *arg = (struct map_entries_arg *)varg;
|
|
534
|
-
VALUE hash = rb_hash_new();
|
|
535
|
-
VALUE key;
|
|
536
|
-
VALUE value;
|
|
537
|
-
|
|
538
|
-
for (idx_t i = arg->bgn; i < arg->end; ++i) {
|
|
539
|
-
key = rbduckdb_vector_value_at(arg->key_vector, arg->key_type, i);
|
|
540
|
-
value = rbduckdb_vector_value_at(arg->value_vector, arg->value_type, i);
|
|
541
|
-
rb_hash_aset(hash, key, value);
|
|
542
|
-
}
|
|
543
|
-
return hash;
|
|
544
|
-
}
|
|
545
|
-
|
|
546
|
-
static VALUE destroy_map_types(VALUE varg) {
|
|
547
|
-
struct map_entries_arg *arg = (struct map_entries_arg *)varg;
|
|
548
|
-
|
|
549
|
-
duckdb_destroy_logical_type(&(arg->key_type));
|
|
550
|
-
duckdb_destroy_logical_type(&(arg->value_type));
|
|
551
|
-
return Qnil;
|
|
552
|
-
}
|
|
553
|
-
|
|
554
|
-
static VALUE vector_struct(duckdb_logical_type ty, duckdb_vector vector, idx_t row_idx) {
|
|
555
|
-
struct struct_fields_arg arg = {ty, vector, row_idx, NULL};
|
|
556
|
-
|
|
557
|
-
return rb_ensure(convert_struct_fields, (VALUE)&arg, destroy_struct_child_type, (VALUE)&arg);
|
|
558
|
-
}
|
|
559
|
-
|
|
560
|
-
static VALUE convert_struct_fields(VALUE varg) {
|
|
561
|
-
struct struct_fields_arg *arg = (struct struct_fields_arg *)varg;
|
|
562
|
-
VALUE hash = rb_hash_new();
|
|
563
|
-
VALUE value = Qnil;
|
|
564
|
-
VALUE key = Qnil;
|
|
565
|
-
duckdb_vector child;
|
|
566
|
-
char *p;
|
|
567
|
-
|
|
568
|
-
idx_t child_count = duckdb_struct_type_child_count(arg->ty);
|
|
569
|
-
|
|
570
|
-
for (idx_t i = 0; i < child_count; ++i) {
|
|
571
|
-
p = duckdb_struct_type_child_name(arg->ty, i);
|
|
572
|
-
if (p) {
|
|
573
|
-
key = ID2SYM(rb_intern_const(p));
|
|
574
|
-
duckdb_free(p);
|
|
575
|
-
child = duckdb_struct_vector_get_child(arg->vector, i);
|
|
576
|
-
arg->child_type = duckdb_struct_type_child_type(arg->ty, i);
|
|
577
|
-
value = rbduckdb_vector_value_at(child, arg->child_type, arg->row_idx);
|
|
578
|
-
duckdb_destroy_logical_type(&(arg->child_type));
|
|
579
|
-
rb_hash_aset(hash, key, value);
|
|
580
|
-
}
|
|
581
|
-
}
|
|
582
|
-
|
|
583
|
-
return hash;
|
|
584
|
-
}
|
|
585
|
-
|
|
586
|
-
static VALUE destroy_struct_child_type(VALUE varg) {
|
|
587
|
-
struct struct_fields_arg *arg = (struct struct_fields_arg *)varg;
|
|
588
|
-
|
|
589
|
-
duckdb_destroy_logical_type(&(arg->child_type));
|
|
590
|
-
return Qnil;
|
|
591
|
-
}
|
|
592
|
-
|
|
593
|
-
static VALUE vector_union(duckdb_logical_type ty, duckdb_vector vector, void* vector_data, idx_t row_idx){
|
|
594
|
-
duckdb_vector type_vector = duckdb_struct_vector_get_child(vector, 0);
|
|
595
|
-
void *data = duckdb_vector_get_data(type_vector);
|
|
596
|
-
uint8_t index = ((int8_t *)data)[row_idx];
|
|
597
|
-
|
|
598
|
-
duckdb_vector member_vector = duckdb_struct_vector_get_child(vector, index + 1);
|
|
599
|
-
VALUE values = vector_values_with_owned_type(member_vector, duckdb_union_type_member_type(ty, index), row_idx, row_idx + 1);
|
|
600
|
-
|
|
601
|
-
return rb_ary_entry(values, 0);
|
|
602
|
-
}
|
|
603
|
-
|
|
604
|
-
static VALUE convert_values_in_range(VALUE varg) {
|
|
605
|
-
struct owned_type_arg *arg = (struct owned_type_arg *)varg;
|
|
606
|
-
VALUE ary = rb_ary_new2(arg->end - arg->bgn);
|
|
607
|
-
|
|
608
|
-
for (idx_t i = arg->bgn; i < arg->end; ++i) {
|
|
609
|
-
rb_ary_store(ary, i - arg->bgn, rbduckdb_vector_value_at(arg->vector, arg->type, i));
|
|
610
|
-
}
|
|
611
|
-
return ary;
|
|
612
|
-
}
|
|
613
|
-
|
|
614
|
-
static VALUE destroy_owned_type(VALUE varg) {
|
|
615
|
-
struct owned_type_arg *arg = (struct owned_type_arg *)varg;
|
|
616
|
-
|
|
617
|
-
duckdb_destroy_logical_type(&(arg->type));
|
|
618
|
-
return Qnil;
|
|
619
|
-
}
|
|
620
|
-
|
|
621
|
-
static VALUE vector_values_with_owned_type(duckdb_vector vector, duckdb_logical_type type, idx_t bgn, idx_t end) {
|
|
622
|
-
struct owned_type_arg arg = {vector, type, bgn, end};
|
|
623
|
-
|
|
624
|
-
return rb_ensure(convert_values_in_range, (VALUE)&arg, destroy_owned_type, (VALUE)&arg);
|
|
625
|
-
}
|
|
626
|
-
|
|
627
|
-
static VALUE str_concat_byte(VALUE str, unsigned char byte, int offset) {
|
|
628
|
-
char x[8];
|
|
629
|
-
char *p = x;
|
|
630
|
-
for (int j = 7; j >=0; j--) {
|
|
631
|
-
if (byte % 2 == 1) {
|
|
632
|
-
x[j] = '1';
|
|
633
|
-
} else {
|
|
634
|
-
x[j] = '0';
|
|
635
|
-
}
|
|
636
|
-
byte = (byte >> 1);
|
|
637
|
-
}
|
|
638
|
-
if (offset > 0 && offset < 8) {
|
|
639
|
-
p = x + offset;
|
|
640
|
-
}
|
|
641
|
-
return rb_str_cat(str, p, 8 - offset);
|
|
642
|
-
}
|
|
643
|
-
|
|
644
|
-
static VALUE bytes_to_string(const char *bytes, uint32_t length, int offset) {
|
|
645
|
-
VALUE str = rb_str_new_literal("");
|
|
646
|
-
str = str_concat_byte(str, bytes[0], offset);
|
|
647
|
-
for (uint32_t i = 1; i < length; i++) {
|
|
648
|
-
str = str_concat_byte(str, bytes[i], 0);
|
|
649
|
-
}
|
|
650
|
-
return str;
|
|
651
|
-
}
|
|
652
|
-
|
|
653
|
-
static VALUE vector_bit(void* vector_data, idx_t row_idx) {
|
|
654
|
-
duckdb_string_t s = (((duckdb_string_t *)vector_data)[row_idx]);
|
|
655
|
-
const char *p = duckdb_string_t_data(&s);
|
|
656
|
-
uint32_t length = duckdb_string_t_length(s);
|
|
657
|
-
int offset;
|
|
658
|
-
VALUE str = Qnil;
|
|
659
|
-
|
|
660
|
-
if (length < 2 || (unsigned char)p[0] > 7) {
|
|
661
|
-
rb_raise(eDuckDBError, "invalid BIT value");
|
|
662
|
-
}
|
|
663
|
-
offset = (unsigned char)p[0];
|
|
664
|
-
str = bytes_to_string(p + 1, length - 1, offset);
|
|
665
|
-
return str;
|
|
666
|
-
}
|
|
667
|
-
|
|
668
|
-
static VALUE vector_time_tz(void* vector_data, idx_t row_idx) {
|
|
669
|
-
return rbduckdb_time_tz_to_ruby(((duckdb_time_tz *)vector_data)[row_idx]);
|
|
670
|
-
}
|
|
671
|
-
|
|
672
|
-
static VALUE vector_timestamp_tz(void* vector_data, idx_t row_idx) {
|
|
673
|
-
duckdb_timestamp ts = {(int64_t)((duckdb_time_tz *)vector_data)[row_idx].bits};
|
|
674
|
-
return rbduckdb_timestamp_tz_to_ruby(ts);
|
|
675
|
-
}
|
|
676
|
-
|
|
677
|
-
static VALUE vector_uuid(void* vector_data, idx_t row_idx) {
|
|
678
|
-
return rbduckdb_uuid_to_ruby(((duckdb_hugeint *)vector_data)[row_idx]);
|
|
679
|
-
}
|
|
680
|
-
|
|
681
259
|
/*
|
|
682
260
|
* call-seq:
|
|
683
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
|
data/ext/duckdb/ruby-duckdb.h
CHANGED
|
@@ -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 "./
|
|
39
|
+
#include "./vector_writer.h"
|
|
39
40
|
#include "./scalar_function.h"
|
|
40
41
|
#include "./scalar_function_set.h"
|
|
41
42
|
#include "./aggregate_function.h"
|
|
@@ -106,8 +106,8 @@ static void compact(void *ctx) {
|
|
|
106
106
|
}
|
|
107
107
|
|
|
108
108
|
static VALUE allocate(VALUE klass) {
|
|
109
|
-
rubyDuckDBScalarFunction *ctx
|
|
110
|
-
return
|
|
109
|
+
rubyDuckDBScalarFunction *ctx;
|
|
110
|
+
return TypedData_Make_Struct(klass, rubyDuckDBScalarFunction, &scalar_function_data_type, ctx);
|
|
111
111
|
}
|
|
112
112
|
|
|
113
113
|
static size_t memsize(const void *p) {
|
|
@@ -379,6 +379,8 @@ static void execute_bind_callback_protected(void *user_data) {
|
|
|
379
379
|
arg.bind_info_obj = rbduckdb_scalar_function_bind_info_new(darg->info);
|
|
380
380
|
|
|
381
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);
|
|
382
384
|
if (exception_state) {
|
|
383
385
|
VALUE msg = rbduckdb_pending_error_message();
|
|
384
386
|
duckdb_scalar_function_bind_set_error(darg->info, StringValueCStr(msg));
|
|
@@ -22,8 +22,8 @@ static void deallocate(void *ctx) {
|
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
static VALUE allocate(VALUE klass) {
|
|
25
|
-
rubyDuckDBScalarFunctionBindInfo *ctx
|
|
26
|
-
return
|
|
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
|
-
|
|
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
|
-
|
|
67
|
-
|
|
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
|
-
|
|
81
|
-
|
|
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
|
-
|
|
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
|
|
37
|
-
VALUE obj =
|
|
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
|
|
36
|
-
return
|
|
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) {
|