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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +28 -0
- data/ext/duckdb/aggregate_function.c +42 -80
- data/ext/duckdb/aggregate_function_set.c +2 -2
- data/ext/duckdb/appender.c +46 -134
- 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 +20 -56
- data/ext/duckdb/{conveter.c → converter.c} +37 -80
- 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/function_executor.c +214 -248
- data/ext/duckdb/function_executor.h +10 -0
- data/ext/duckdb/instance_cache.c +3 -2
- data/ext/duckdb/logical_type.c +90 -172
- data/ext/duckdb/memory_helper.c +17 -4
- data/ext/duckdb/pending_result.c +3 -2
- data/ext/duckdb/prepared_statement.c +39 -140
- data/ext/duckdb/result.c +0 -491
- data/ext/duckdb/result.h +0 -1
- data/ext/duckdb/ruby-duckdb.h +2 -1
- data/ext/duckdb/scalar_function.c +13 -71
- 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 +72 -82
- 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/util.c +53 -2
- 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} +94 -7
- data/ext/duckdb/vector_writer.h +53 -0
- data/lib/duckdb/appender.rb +27 -31
- data/lib/duckdb/data_chunk.rb +4 -89
- data/lib/duckdb/interval.rb +2 -28
- data/lib/duckdb/prepared_statement.rb +19 -39
- data/lib/duckdb/ruby_type_inference.rb +91 -0
- data/lib/duckdb/scalar_function_set.rb +1 -2
- data/lib/duckdb/version.rb +1 -1
- data/lib/duckdb.rb +1 -0
- metadata +7 -4
- data/ext/duckdb/function_vector.h +0 -27
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
#ifndef RUBY_DUCKDB_VECTOR_WRITER_H
|
|
2
|
+
#define RUBY_DUCKDB_VECTOR_WRITER_H
|
|
3
|
+
|
|
4
|
+
/*
|
|
5
|
+
* Ruby value -> DuckDB vector cell writer.
|
|
6
|
+
*
|
|
7
|
+
* Converts a Ruby VALUE to the appropriate DuckDB type and writes it
|
|
8
|
+
* into a vector at the given index. This is the single place that
|
|
9
|
+
* decides how a Ruby object is written into a vector: ScalarFunction
|
|
10
|
+
* and AggregateFunction output, DataChunk#set_value (TableFunction
|
|
11
|
+
* output, Appender#append_data_chunk) all go through it.
|
|
12
|
+
*
|
|
13
|
+
* TIMESTAMP (and TIMESTAMP_S/MS/NS) stores the wall-clock components of
|
|
14
|
+
* the Time as-is, like PreparedStatement#bind_timestamp and
|
|
15
|
+
* Appender#append_timestamp. TIMESTAMP_TZ stores the instant
|
|
16
|
+
* (microseconds since the Unix epoch), so the Time's utc_offset is
|
|
17
|
+
* taken into account.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/*
|
|
21
|
+
* Write `value` (a Ruby VALUE) into `vector` at position `index`.
|
|
22
|
+
*
|
|
23
|
+
* `element_type` is the logical type of the vector and determines
|
|
24
|
+
* which conversion is applied. If `value` is Qnil the row is marked
|
|
25
|
+
* invalid (NULL).
|
|
26
|
+
*
|
|
27
|
+
* Raises ArgumentError for unsupported types - callers that cannot
|
|
28
|
+
* tolerate longjmp should wrap the call in rb_protect.
|
|
29
|
+
*/
|
|
30
|
+
void rbduckdb_vector_set_value_at(duckdb_vector vector,
|
|
31
|
+
duckdb_logical_type element_type,
|
|
32
|
+
idx_t index,
|
|
33
|
+
VALUE value);
|
|
34
|
+
|
|
35
|
+
/*
|
|
36
|
+
* Load the vector and logical type of each of the `n` columns of `input`.
|
|
37
|
+
* Allocates both arrays with ALLOC_N; release them with
|
|
38
|
+
* rbduckdb_input_columns_free.
|
|
39
|
+
*/
|
|
40
|
+
void rbduckdb_input_columns_load(duckdb_data_chunk input,
|
|
41
|
+
idx_t n,
|
|
42
|
+
duckdb_vector **vectors,
|
|
43
|
+
duckdb_logical_type **types);
|
|
44
|
+
|
|
45
|
+
/*
|
|
46
|
+
* Destroy the `n` logical types and free both arrays. NULL arrays are
|
|
47
|
+
* tolerated so it is safe to call when the load never happened.
|
|
48
|
+
*/
|
|
49
|
+
void rbduckdb_input_columns_free(duckdb_vector *vectors,
|
|
50
|
+
duckdb_logical_type *types,
|
|
51
|
+
idx_t n);
|
|
52
|
+
|
|
53
|
+
#endif
|
data/lib/duckdb/appender.rb
CHANGED
|
@@ -755,25 +755,25 @@ module DuckDB
|
|
|
755
755
|
# appender.append(1)
|
|
756
756
|
# appender.append('Alice')
|
|
757
757
|
# appender.end_row
|
|
758
|
-
#
|
|
759
|
-
|
|
758
|
+
# The Ruby type -> DuckDB type decision is made by RubyTypeInference,
|
|
759
|
+
# which also holds PreparedStatement#bind's table. Integer and String
|
|
760
|
+
# are the most common values in a bulk load, so they are matched here
|
|
761
|
+
# first, without the extra call; the rules are the same as
|
|
762
|
+
# RubyTypeInference.for_append (ruby_type_inference_test.rb and
|
|
763
|
+
# appender_test.rb check both).
|
|
764
|
+
def append(value) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength
|
|
760
765
|
case value
|
|
761
|
-
when
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
when
|
|
768
|
-
|
|
769
|
-
when
|
|
770
|
-
|
|
771
|
-
when
|
|
772
|
-
append_timestamp(value)
|
|
773
|
-
when Date
|
|
774
|
-
append_date(value)
|
|
775
|
-
when DuckDB::Interval
|
|
776
|
-
append_interval(value)
|
|
766
|
+
when Integer then return append_integer_value(value)
|
|
767
|
+
when String then return append_string_value(value)
|
|
768
|
+
end
|
|
769
|
+
|
|
770
|
+
case RubyTypeInference.for_append(value)
|
|
771
|
+
when :null then append_null
|
|
772
|
+
when :double then append_double(value)
|
|
773
|
+
when :boolean then append_bool(value)
|
|
774
|
+
when :timestamp then append_timestamp(value)
|
|
775
|
+
when :date then append_date(value)
|
|
776
|
+
when :interval then append_interval(value)
|
|
777
777
|
else
|
|
778
778
|
raise(DuckDB::Error, "not supported type #{value} (#{value.class})")
|
|
779
779
|
end
|
|
@@ -811,25 +811,21 @@ module DuckDB
|
|
|
811
811
|
raise DuckDB::Error, message || default_message
|
|
812
812
|
end
|
|
813
813
|
|
|
814
|
-
def blob?(value) # :nodoc:
|
|
815
|
-
value.instance_of?(DuckDB::Blob) || value.encoding == Encoding::BINARY
|
|
816
|
-
end
|
|
817
|
-
|
|
818
814
|
def append_integer_value(value) # :nodoc:
|
|
819
815
|
case value
|
|
820
|
-
when RANGE_INT16
|
|
821
|
-
|
|
822
|
-
when
|
|
823
|
-
|
|
824
|
-
when RANGE_INT64
|
|
825
|
-
append_int64(value)
|
|
826
|
-
else
|
|
827
|
-
append_hugeint(value)
|
|
816
|
+
when RANGE_INT16 then append_int16(value)
|
|
817
|
+
when RANGE_INT32 then append_int32(value)
|
|
818
|
+
when RANGE_INT64 then append_int64(value)
|
|
819
|
+
else append_hugeint(value)
|
|
828
820
|
end
|
|
829
821
|
end
|
|
830
822
|
|
|
831
823
|
def append_string_value(value) # :nodoc:
|
|
832
|
-
|
|
824
|
+
if value.instance_of?(DuckDB::Blob) || value.encoding == Encoding::BINARY
|
|
825
|
+
append_blob(value)
|
|
826
|
+
else
|
|
827
|
+
append_varchar(value)
|
|
828
|
+
end
|
|
833
829
|
end
|
|
834
830
|
|
|
835
831
|
def to_time(value) # :nodoc:
|
data/lib/duckdb/data_chunk.rb
CHANGED
|
@@ -24,76 +24,11 @@ module DuckDB
|
|
|
24
24
|
# end
|
|
25
25
|
#
|
|
26
26
|
class DataChunk
|
|
27
|
-
# Most methods are defined in C extension (ext/duckdb/data_chunk.c)
|
|
28
|
-
|
|
29
|
-
#
|
|
30
|
-
# Sets a value at the specified column and row index.
|
|
31
|
-
# Type conversion is automatic based on the column's logical type.
|
|
32
|
-
#
|
|
33
|
-
# @param col_idx [Integer] Column index (0-based)
|
|
34
|
-
# @param row_idx [Integer] Row index (0-based)
|
|
35
|
-
# @param value [Object] Value to set (Integer, Float, String, Time, Date, nil)
|
|
36
|
-
# @return [Object] The value that was set
|
|
37
|
-
#
|
|
38
|
-
# @example Set integer value
|
|
39
|
-
# output.set_value(0, 0, 42)
|
|
40
|
-
#
|
|
41
|
-
# @example Set string value
|
|
42
|
-
# output.set_value(1, 0, 'hello')
|
|
27
|
+
# Most methods are defined in C extension (ext/duckdb/data_chunk.c).
|
|
43
28
|
#
|
|
44
|
-
#
|
|
45
|
-
#
|
|
46
|
-
#
|
|
47
|
-
# rubocop:disable-next Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
|
|
48
|
-
def set_value(col_idx, row_idx, value)
|
|
49
|
-
vector = cached_vector(col_idx)
|
|
50
|
-
type_id = cached_type_id(col_idx, vector)
|
|
51
|
-
|
|
52
|
-
# Handle NULL
|
|
53
|
-
if value.nil?
|
|
54
|
-
vector.set_validity(row_idx, false)
|
|
55
|
-
return value
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
case type_id
|
|
59
|
-
when :boolean
|
|
60
|
-
MemoryHelper.write_boolean(cached_data(col_idx, vector), row_idx, value)
|
|
61
|
-
when :tinyint
|
|
62
|
-
MemoryHelper.write_tinyint(cached_data(col_idx, vector), row_idx, value)
|
|
63
|
-
when :smallint
|
|
64
|
-
MemoryHelper.write_smallint(cached_data(col_idx, vector), row_idx, value)
|
|
65
|
-
when :integer
|
|
66
|
-
MemoryHelper.write_integer(cached_data(col_idx, vector), row_idx, value)
|
|
67
|
-
when :bigint
|
|
68
|
-
MemoryHelper.write_bigint(cached_data(col_idx, vector), row_idx, value)
|
|
69
|
-
when :utinyint
|
|
70
|
-
MemoryHelper.write_utinyint(cached_data(col_idx, vector), row_idx, value)
|
|
71
|
-
when :usmallint
|
|
72
|
-
MemoryHelper.write_usmallint(cached_data(col_idx, vector), row_idx, value)
|
|
73
|
-
when :uinteger
|
|
74
|
-
MemoryHelper.write_uinteger(cached_data(col_idx, vector), row_idx, value)
|
|
75
|
-
when :ubigint
|
|
76
|
-
MemoryHelper.write_ubigint(cached_data(col_idx, vector), row_idx, value)
|
|
77
|
-
when :float
|
|
78
|
-
MemoryHelper.write_float(cached_data(col_idx, vector), row_idx, value)
|
|
79
|
-
when :double
|
|
80
|
-
MemoryHelper.write_double(cached_data(col_idx, vector), row_idx, value)
|
|
81
|
-
when :varchar
|
|
82
|
-
vector.assign_string_element(row_idx, value.to_s)
|
|
83
|
-
when :blob
|
|
84
|
-
vector.assign_string_element_len(row_idx, value.to_s)
|
|
85
|
-
when :timestamp
|
|
86
|
-
MemoryHelper.write_timestamp(cached_data(col_idx, vector), row_idx, value)
|
|
87
|
-
when :timestamp_tz
|
|
88
|
-
MemoryHelper.write_timestamp_tz(cached_data(col_idx, vector), row_idx, value)
|
|
89
|
-
when :date
|
|
90
|
-
MemoryHelper.write_date(cached_data(col_idx, vector), row_idx, value)
|
|
91
|
-
else
|
|
92
|
-
raise ArgumentError, "Unsupported type for DataChunk#set_value: #{type_id} for value `#{value.inspect}`"
|
|
93
|
-
end
|
|
94
|
-
|
|
95
|
-
value
|
|
96
|
-
end
|
|
29
|
+
# DataChunk#set_value(col_idx, row_idx, value) converts +value+ by the
|
|
30
|
+
# column's logical type with the same writer ScalarFunction uses for its
|
|
31
|
+
# return values, so both accept the same types with identical results.
|
|
97
32
|
|
|
98
33
|
#
|
|
99
34
|
# Resets the data chunk so it can be reused for another batch of rows.
|
|
@@ -102,27 +37,7 @@ module DuckDB
|
|
|
102
37
|
#
|
|
103
38
|
def reset
|
|
104
39
|
_reset
|
|
105
|
-
# duckdb_data_chunk_reset may invalidate previously returned data pointers,
|
|
106
|
-
# so drop the cache; vector/type caches remain valid across resets.
|
|
107
|
-
@data_cache = nil
|
|
108
40
|
self
|
|
109
41
|
end
|
|
110
|
-
|
|
111
|
-
private
|
|
112
|
-
|
|
113
|
-
def cached_vector(col_idx)
|
|
114
|
-
@vector_cache ||= {}
|
|
115
|
-
@vector_cache[col_idx] ||= get_vector(col_idx)
|
|
116
|
-
end
|
|
117
|
-
|
|
118
|
-
def cached_type_id(col_idx, vector)
|
|
119
|
-
@type_id_cache ||= {}
|
|
120
|
-
@type_id_cache[col_idx] ||= vector.logical_type.type
|
|
121
|
-
end
|
|
122
|
-
|
|
123
|
-
def cached_data(col_idx, vector)
|
|
124
|
-
@data_cache ||= {}
|
|
125
|
-
@data_cache[col_idx] ||= vector.get_data
|
|
126
|
-
end
|
|
127
42
|
end
|
|
128
43
|
end
|
data/lib/duckdb/interval.rb
CHANGED
|
@@ -103,12 +103,10 @@ module DuckDB
|
|
|
103
103
|
|
|
104
104
|
def matched_to_i(matched) # :nodoc:
|
|
105
105
|
sign = to_sign(matched)
|
|
106
|
-
sec =
|
|
106
|
+
year, month, day, hour, min, sec = %i[year month day hour min sec].map { |k| matched[k].to_i }
|
|
107
107
|
usec = to_usec(matched)
|
|
108
108
|
usec *= -1 if sec.negative?
|
|
109
|
-
value = [
|
|
110
|
-
to_year(matched), to_month(matched), to_day(matched), to_hour(matched), to_min(matched), sec, usec
|
|
111
|
-
]
|
|
109
|
+
value = [year, month, day, hour, min, sec, usec]
|
|
112
110
|
sign.positive? ? value : value.map { |v| v * sign }
|
|
113
111
|
end
|
|
114
112
|
|
|
@@ -116,30 +114,6 @@ module DuckDB
|
|
|
116
114
|
matched[:negativ] == '-' ? -1 : 1
|
|
117
115
|
end
|
|
118
116
|
|
|
119
|
-
def to_year(matched) # :nodoc:
|
|
120
|
-
matched[:year].to_i
|
|
121
|
-
end
|
|
122
|
-
|
|
123
|
-
def to_month(matched) # :nodoc:
|
|
124
|
-
matched[:month].to_i
|
|
125
|
-
end
|
|
126
|
-
|
|
127
|
-
def to_day(matched) # :nodoc:
|
|
128
|
-
matched[:day].to_i
|
|
129
|
-
end
|
|
130
|
-
|
|
131
|
-
def to_hour(matched) # :nodoc:
|
|
132
|
-
matched[:hour].to_i
|
|
133
|
-
end
|
|
134
|
-
|
|
135
|
-
def to_min(matched) # :nodoc:
|
|
136
|
-
matched[:min].to_i
|
|
137
|
-
end
|
|
138
|
-
|
|
139
|
-
def to_sec(matched) # :nodoc:
|
|
140
|
-
matched[:sec].to_i
|
|
141
|
-
end
|
|
142
|
-
|
|
143
117
|
def to_usec(matched) # :nodoc:
|
|
144
118
|
matched[:usec].to_s.ljust(6, '0')[0, 6].to_i
|
|
145
119
|
end
|
|
@@ -332,6 +332,11 @@ module DuckDB
|
|
|
332
332
|
decimal = _parse_deciaml(value)
|
|
333
333
|
lower, upper = decimal_to_hugeint(decimal)
|
|
334
334
|
width = _decimal_width(decimal)
|
|
335
|
+
# _bind_decimal narrows width to uint8, and DuckDB aborts on a mismatched width.
|
|
336
|
+
unless RANGE_DECIMAL_WIDTH.cover?(width)
|
|
337
|
+
raise ArgumentError, "value out of range for DECIMAL width (#{RANGE_DECIMAL_WIDTH})"
|
|
338
|
+
end
|
|
339
|
+
|
|
335
340
|
_bind_decimal(index, lower, upper, width, decimal.scale)
|
|
336
341
|
end
|
|
337
342
|
|
|
@@ -379,55 +384,30 @@ module DuckDB
|
|
|
379
384
|
|
|
380
385
|
private
|
|
381
386
|
|
|
382
|
-
#
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
when
|
|
388
|
-
|
|
389
|
-
when
|
|
390
|
-
|
|
391
|
-
when
|
|
392
|
-
|
|
393
|
-
when
|
|
394
|
-
|
|
395
|
-
when
|
|
396
|
-
bind_varchar(index, value.strftime('%Y-%m-%d %H:%M:%S.%N'))
|
|
397
|
-
when Date
|
|
398
|
-
bind_varchar(index, value.strftime('%Y-%m-%d'))
|
|
399
|
-
when BigDecimal
|
|
400
|
-
bind_decimal(index, value)
|
|
387
|
+
# The Ruby type -> DuckDB type decision is made by RubyTypeInference,
|
|
388
|
+
# which also holds Appender#append's table.
|
|
389
|
+
def bind_with_index(index, value) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
|
|
390
|
+
case RubyTypeInference.for_bind(value)
|
|
391
|
+
when :null then bind_null(index)
|
|
392
|
+
when :double then bind_double(index, value)
|
|
393
|
+
when :bigint then bind_int64(index, value)
|
|
394
|
+
when :integer_as_varchar then bind_varchar(index, value.to_s)
|
|
395
|
+
when :blob then bind_blob(index, value)
|
|
396
|
+
when :varchar then bind_varchar(index, value)
|
|
397
|
+
when :boolean then bind_bool(index, value)
|
|
398
|
+
when :time_as_varchar then bind_varchar(index, value.strftime('%Y-%m-%d %H:%M:%S.%N'))
|
|
399
|
+
when :date_as_varchar then bind_varchar(index, value.strftime('%Y-%m-%d'))
|
|
400
|
+
when :decimal then bind_decimal(index, value)
|
|
401
401
|
else
|
|
402
402
|
raise(DuckDB::Error, "not supported type `#{value}` (#{value.class})")
|
|
403
403
|
end
|
|
404
404
|
end
|
|
405
405
|
|
|
406
|
-
def bind_integer_value(index, value)
|
|
407
|
-
if RANGE_INT64.cover?(value)
|
|
408
|
-
bind_int64(index, value)
|
|
409
|
-
else
|
|
410
|
-
bind_varchar(index, value.to_s)
|
|
411
|
-
end
|
|
412
|
-
end
|
|
413
|
-
|
|
414
|
-
def bind_string_value(index, value)
|
|
415
|
-
if blob?(value)
|
|
416
|
-
bind_blob(index, value)
|
|
417
|
-
else
|
|
418
|
-
bind_varchar(index, value)
|
|
419
|
-
end
|
|
420
|
-
end
|
|
421
|
-
|
|
422
406
|
def bind_with_name(name, value)
|
|
423
407
|
raise DuckDB::Error, 'not supported binding with name' unless respond_to?(:bind_parameter_index)
|
|
424
408
|
|
|
425
409
|
i = bind_parameter_index(name)
|
|
426
410
|
bind_with_index(i, value)
|
|
427
411
|
end
|
|
428
|
-
|
|
429
|
-
def blob?(value)
|
|
430
|
-
value.instance_of?(DuckDB::Blob) || value.encoding == Encoding::BINARY
|
|
431
|
-
end
|
|
432
412
|
end
|
|
433
413
|
end
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'bigdecimal'
|
|
4
|
+
require 'date'
|
|
5
|
+
|
|
6
|
+
module DuckDB
|
|
7
|
+
# Decides which DuckDB type an untyped Ruby object becomes when it is
|
|
8
|
+
# passed to PreparedStatement#bind (#for_bind) or Appender#append
|
|
9
|
+
# (#for_append).
|
|
10
|
+
#
|
|
11
|
+
# Both decision tables live here, side by side, so that every difference
|
|
12
|
+
# between the two is visible in one place. The differences are intended
|
|
13
|
+
# and marked with "DIFF":
|
|
14
|
+
#
|
|
15
|
+
# * bind is used by most users, so it converts values for convenience
|
|
16
|
+
# (e.g. Time/Date as VARCHAR, BigDecimal as DECIMAL).
|
|
17
|
+
# * append is for bulk loading, so it passes values through with as
|
|
18
|
+
# little conversion as possible, favouring insert speed.
|
|
19
|
+
#
|
|
20
|
+
# #for_bind and #for_append return a type tag, or nil when the value is
|
|
21
|
+
# not supported. Each caller maps the tag to its own bind_* / append_*
|
|
22
|
+
# method.
|
|
23
|
+
module RubyTypeInference # :nodoc: all
|
|
24
|
+
# Ruby class => [tag for bind, tag for append]. Looked up by the exact
|
|
25
|
+
# class first, then with `===` in order (so DateTime matches Date).
|
|
26
|
+
# Integer and String depend on the value and are handled separately.
|
|
27
|
+
TABLE = {
|
|
28
|
+
NilClass => %i[null null],
|
|
29
|
+
Float => %i[double double],
|
|
30
|
+
TrueClass => %i[boolean boolean],
|
|
31
|
+
FalseClass => %i[boolean boolean],
|
|
32
|
+
# DIFF: bind sends Time/Date as VARCHAR, append as TIMESTAMP/DATE.
|
|
33
|
+
Time => %i[time_as_varchar timestamp],
|
|
34
|
+
Date => %i[date_as_varchar date],
|
|
35
|
+
# DIFF: BigDecimal is supported by bind only.
|
|
36
|
+
BigDecimal => [:decimal, nil],
|
|
37
|
+
# DIFF: DuckDB::Interval is supported by append only.
|
|
38
|
+
DuckDB::Interval => [nil, :interval]
|
|
39
|
+
}.freeze
|
|
40
|
+
|
|
41
|
+
# One hash per caller, so each lookup is a single fetch.
|
|
42
|
+
BIND_TABLE = TABLE.transform_values(&:first).freeze
|
|
43
|
+
APPEND_TABLE = TABLE.transform_values(&:last).freeze
|
|
44
|
+
|
|
45
|
+
module_function
|
|
46
|
+
|
|
47
|
+
# DIFF: bind uses BIGINT for every Integer in the int64 range and
|
|
48
|
+
# VARCHAR beyond it; append (below) picks the smallest of
|
|
49
|
+
# SMALLINT/INTEGER/BIGINT and HUGEINT beyond int64.
|
|
50
|
+
def for_bind(value)
|
|
51
|
+
case value
|
|
52
|
+
when Integer then Converter::RANGE_INT64.cover?(value) ? :bigint : :integer_as_varchar
|
|
53
|
+
when String then string_type(value)
|
|
54
|
+
else BIND_TABLE[value.class] || subclass_tag(BIND_TABLE, value)
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# Appender#append matches Integer and String itself before calling
|
|
59
|
+
# this (for speed); a change to those two branches must be made there too.
|
|
60
|
+
def for_append(value)
|
|
61
|
+
case value
|
|
62
|
+
when Integer # duplicated in Appender#append_integer_value; change both
|
|
63
|
+
if Converter::RANGE_INT16.cover?(value) then :smallint
|
|
64
|
+
elsif Converter::RANGE_INT32.cover?(value) then :integer
|
|
65
|
+
elsif Converter::RANGE_INT64.cover?(value) then :bigint
|
|
66
|
+
else :hugeint
|
|
67
|
+
end
|
|
68
|
+
when String then string_type(value) # duplicated in Appender#append_string_value; change both
|
|
69
|
+
else APPEND_TABLE[value.class] || subclass_tag(APPEND_TABLE, value)
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def string_type(value)
|
|
74
|
+
if value.instance_of?(DuckDB::Blob) || value.encoding == Encoding::BINARY
|
|
75
|
+
:blob
|
|
76
|
+
else
|
|
77
|
+
:varchar
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# For subclasses, and for classes whose tag is nil (not supported).
|
|
82
|
+
def subclass_tag(table, value)
|
|
83
|
+
# Same matching as a `case value when Klass` (the dispatch this replaced).
|
|
84
|
+
table.find { |klass, _| klass === value }&.last # rubocop:disable Style/CaseEquality
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
private_class_method :string_type, :subclass_tag
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
private_constant :RubyTypeInference
|
|
91
|
+
end
|
|
@@ -11,8 +11,7 @@ module DuckDB
|
|
|
11
11
|
def initialize(name)
|
|
12
12
|
raise TypeError, "#{name.class} is not a String or Symbol" unless name.is_a?(String) || name.is_a?(Symbol)
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
_initialize(@name)
|
|
14
|
+
_initialize(name.to_s)
|
|
16
15
|
end
|
|
17
16
|
|
|
18
17
|
# @param scalar_function [DuckDB::ScalarFunction] the overload to add
|
data/lib/duckdb/version.rb
CHANGED
data/lib/duckdb.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: duckdb
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 2.0.
|
|
4
|
+
version: 2.0.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Masaki Suketa
|
|
@@ -62,8 +62,8 @@ files:
|
|
|
62
62
|
- ext/duckdb/config.h
|
|
63
63
|
- ext/duckdb/connection.c
|
|
64
64
|
- ext/duckdb/connection.h
|
|
65
|
+
- ext/duckdb/converter.c
|
|
65
66
|
- ext/duckdb/converter.h
|
|
66
|
-
- ext/duckdb/conveter.c
|
|
67
67
|
- ext/duckdb/data_chunk.c
|
|
68
68
|
- ext/duckdb/data_chunk.h
|
|
69
69
|
- ext/duckdb/database.c
|
|
@@ -78,8 +78,6 @@ files:
|
|
|
78
78
|
- ext/duckdb/extracted_statements.h
|
|
79
79
|
- ext/duckdb/function_executor.c
|
|
80
80
|
- ext/duckdb/function_executor.h
|
|
81
|
-
- ext/duckdb/function_vector.c
|
|
82
|
-
- ext/duckdb/function_vector.h
|
|
83
81
|
- ext/duckdb/instance_cache.c
|
|
84
82
|
- ext/duckdb/instance_cache.h
|
|
85
83
|
- ext/duckdb/logical_type.c
|
|
@@ -115,6 +113,10 @@ files:
|
|
|
115
113
|
- ext/duckdb/value.h
|
|
116
114
|
- ext/duckdb/vector.c
|
|
117
115
|
- ext/duckdb/vector.h
|
|
116
|
+
- ext/duckdb/vector_reader.c
|
|
117
|
+
- ext/duckdb/vector_reader.h
|
|
118
|
+
- ext/duckdb/vector_writer.c
|
|
119
|
+
- ext/duckdb/vector_writer.h
|
|
118
120
|
- lib/duckdb.rb
|
|
119
121
|
- lib/duckdb/aggregate_function.rb
|
|
120
122
|
- lib/duckdb/aggregate_function_set.rb
|
|
@@ -142,6 +144,7 @@ files:
|
|
|
142
144
|
- lib/duckdb/pending_result.rb
|
|
143
145
|
- lib/duckdb/prepared_statement.rb
|
|
144
146
|
- lib/duckdb/result.rb
|
|
147
|
+
- lib/duckdb/ruby_type_inference.rb
|
|
145
148
|
- lib/duckdb/scalar_function.rb
|
|
146
149
|
- lib/duckdb/scalar_function/bind_info.rb
|
|
147
150
|
- lib/duckdb/scalar_function_set.rb
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
#ifndef RUBY_DUCKDB_FUNCTION_VECTOR_H
|
|
2
|
-
#define RUBY_DUCKDB_FUNCTION_VECTOR_H
|
|
3
|
-
|
|
4
|
-
/*
|
|
5
|
-
* Shared vector-write helper for UDF callbacks.
|
|
6
|
-
*
|
|
7
|
-
* Converts a Ruby VALUE to the appropriate DuckDB type and writes it
|
|
8
|
-
* into a result vector at the given index. Used by both ScalarFunction
|
|
9
|
-
* and AggregateFunction finalize paths.
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
/*
|
|
13
|
-
* Write `value` (a Ruby VALUE) into `vector` at position `index`.
|
|
14
|
-
*
|
|
15
|
-
* `element_type` is the logical type of the vector and determines
|
|
16
|
-
* which conversion is applied. If `value` is Qnil the row is marked
|
|
17
|
-
* invalid (NULL).
|
|
18
|
-
*
|
|
19
|
-
* Raises rb_eArgError for unsupported types - callers that cannot
|
|
20
|
-
* tolerate longjmp should wrap the call in rb_protect.
|
|
21
|
-
*/
|
|
22
|
-
void rbduckdb_vector_set_value_at(duckdb_vector vector,
|
|
23
|
-
duckdb_logical_type element_type,
|
|
24
|
-
idx_t index,
|
|
25
|
-
VALUE value);
|
|
26
|
-
|
|
27
|
-
#endif
|