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
|
@@ -1,5 +1,38 @@
|
|
|
1
1
|
#include "ruby-duckdb.h"
|
|
2
2
|
|
|
3
|
+
static void set_row_valid(duckdb_vector vector, idx_t index) {
|
|
4
|
+
uint64_t *validity = duckdb_vector_get_validity(vector);
|
|
5
|
+
if (validity != NULL) {
|
|
6
|
+
duckdb_validity_set_row_valid(validity, index);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/*
|
|
11
|
+
* *out = value * factor + sub (0 <= sub < factor), or 0 when the result does
|
|
12
|
+
* not fit in int64_t. Near INT64_MIN the product alone can overflow while the
|
|
13
|
+
* sum still fits, so that case is computed from value + 1.
|
|
14
|
+
*/
|
|
15
|
+
static int scale_add(int64_t value, int64_t factor, int64_t sub, int64_t *out) {
|
|
16
|
+
int64_t high;
|
|
17
|
+
|
|
18
|
+
if (value > (INT64_MAX - sub) / factor) {
|
|
19
|
+
return 0;
|
|
20
|
+
}
|
|
21
|
+
if (value >= INT64_MIN / factor) {
|
|
22
|
+
*out = value * factor + sub;
|
|
23
|
+
return 1;
|
|
24
|
+
}
|
|
25
|
+
if (value < INT64_MIN / factor - 1) {
|
|
26
|
+
return 0;
|
|
27
|
+
}
|
|
28
|
+
high = (value + 1) * factor;
|
|
29
|
+
if (high - INT64_MIN < factor - sub) {
|
|
30
|
+
return 0;
|
|
31
|
+
}
|
|
32
|
+
*out = high - (factor - sub);
|
|
33
|
+
return 1;
|
|
34
|
+
}
|
|
35
|
+
|
|
3
36
|
void rbduckdb_vector_set_value_at(duckdb_vector vector, duckdb_logical_type element_type, idx_t index, VALUE value) {
|
|
4
37
|
duckdb_type type_id;
|
|
5
38
|
void* vector_data;
|
|
@@ -68,15 +101,22 @@ void rbduckdb_vector_set_value_at(duckdb_vector vector, duckdb_logical_type elem
|
|
|
68
101
|
VALUE str = rb_obj_as_string(value);
|
|
69
102
|
const char *str_ptr = StringValuePtr(str);
|
|
70
103
|
idx_t str_len = RSTRING_LEN(str);
|
|
104
|
+
/*
|
|
105
|
+
* Mark the row valid first: DuckDB sets it to NULL itself when
|
|
106
|
+
* the string is not valid UTF-8, and that must not be undone.
|
|
107
|
+
*/
|
|
108
|
+
set_row_valid(vector, index);
|
|
71
109
|
duckdb_vector_assign_string_element_len(vector, index, str_ptr, str_len);
|
|
72
|
-
|
|
110
|
+
return;
|
|
73
111
|
}
|
|
74
112
|
case DUCKDB_TYPE_BLOB: {
|
|
75
113
|
VALUE str = rb_obj_as_string(value);
|
|
76
114
|
const char *str_ptr = StringValuePtr(str);
|
|
77
115
|
idx_t str_len = RSTRING_LEN(str);
|
|
116
|
+
/* Mark the row valid (it may have been NULL before). */
|
|
117
|
+
set_row_valid(vector, index);
|
|
78
118
|
duckdb_vector_assign_string_element_len(vector, index, str_ptr, str_len);
|
|
79
|
-
|
|
119
|
+
return;
|
|
80
120
|
}
|
|
81
121
|
case DUCKDB_TYPE_TIMESTAMP: {
|
|
82
122
|
duckdb_timestamp ts = rbduckdb_to_duckdb_timestamp_from_time_value(value);
|
|
@@ -100,18 +140,33 @@ void rbduckdb_vector_set_value_at(duckdb_vector vector, duckdb_logical_type elem
|
|
|
100
140
|
case DUCKDB_TYPE_TIMESTAMP_NS: {
|
|
101
141
|
duckdb_timestamp ts = rbduckdb_to_duckdb_timestamp_from_time_value(value);
|
|
102
142
|
duckdb_timestamp_ns ts_ns;
|
|
103
|
-
|
|
143
|
+
int64_t sub_micro = NUM2LL(rb_funcall(value, rb_intern("nsec"), 0)) % 1000;
|
|
144
|
+
if (!scale_add(ts.micros, 1000, sub_micro, &ts_ns.nanos)) {
|
|
145
|
+
rb_raise(rb_eRangeError, "timestamp out of range for TIMESTAMP_NS");
|
|
146
|
+
}
|
|
104
147
|
((duckdb_timestamp_ns *)vector_data)[index] = ts_ns;
|
|
105
148
|
break;
|
|
106
149
|
}
|
|
107
150
|
case DUCKDB_TYPE_TIMESTAMP_TZ: {
|
|
108
|
-
duckdb_timestamp ts
|
|
151
|
+
duckdb_timestamp ts;
|
|
152
|
+
int64_t secs;
|
|
153
|
+
if (!rb_obj_is_kind_of(value, rb_cTime)) {
|
|
154
|
+
rb_raise(rb_eTypeError, "Expected Time object for TIMESTAMP_TZ");
|
|
155
|
+
}
|
|
156
|
+
/* An instant: microseconds since the Unix epoch, honoring utc_offset. */
|
|
157
|
+
secs = NUM2LL(rb_funcall(value, rb_intern("to_i"), 0));
|
|
158
|
+
/* INT64_MAX and -INT64_MAX are DuckDB's infinity and -infinity sentinels. */
|
|
159
|
+
if (!scale_add(secs, 1000000, NUM2LL(rb_funcall(value, rb_intern("usec"), 0)), &ts.micros) ||
|
|
160
|
+
ts.micros == INT64_MAX || ts.micros == -INT64_MAX) {
|
|
161
|
+
rb_raise(rb_eRangeError, "timestamp out of range for TIMESTAMP_TZ");
|
|
162
|
+
}
|
|
109
163
|
((duckdb_timestamp *)vector_data)[index] = ts;
|
|
110
164
|
break;
|
|
111
165
|
}
|
|
112
166
|
case DUCKDB_TYPE_DATE: {
|
|
113
|
-
|
|
114
|
-
|
|
167
|
+
if (!rb_respond_to(value, rb_intern("year")) ||
|
|
168
|
+
!rb_respond_to(value, rb_intern("month")) ||
|
|
169
|
+
!rb_respond_to(value, rb_intern("day"))) {
|
|
115
170
|
rb_raise(rb_eTypeError, "Expected Date object for DATE");
|
|
116
171
|
}
|
|
117
172
|
|
|
@@ -215,9 +270,15 @@ void rbduckdb_vector_set_value_at(duckdb_vector vector, duckdb_logical_type elem
|
|
|
215
270
|
break;
|
|
216
271
|
}
|
|
217
272
|
default:
|
|
218
|
-
rb_raise(rb_eArgError, "Unsupported
|
|
273
|
+
rb_raise(rb_eArgError, "Unsupported type for writing a value: DuckDB type id %d", (int)type_id);
|
|
219
274
|
break;
|
|
220
275
|
}
|
|
276
|
+
|
|
277
|
+
/*
|
|
278
|
+
* A cell may have been set to NULL earlier (e.g. a reused DataChunk), so
|
|
279
|
+
* mark it valid again once the value has been written successfully.
|
|
280
|
+
*/
|
|
281
|
+
set_row_valid(vector, index);
|
|
221
282
|
}
|
|
222
283
|
|
|
223
284
|
void rbduckdb_input_columns_load(duckdb_data_chunk input, idx_t n, duckdb_vector **vectors, duckdb_logical_type **types) {
|
|
@@ -1,12 +1,20 @@
|
|
|
1
|
-
#ifndef
|
|
2
|
-
#define
|
|
1
|
+
#ifndef RUBY_DUCKDB_VECTOR_WRITER_H
|
|
2
|
+
#define RUBY_DUCKDB_VECTOR_WRITER_H
|
|
3
3
|
|
|
4
4
|
/*
|
|
5
|
-
*
|
|
5
|
+
* Ruby value -> DuckDB vector cell writer.
|
|
6
6
|
*
|
|
7
7
|
* Converts a Ruby VALUE to the appropriate DuckDB type and writes it
|
|
8
|
-
* into a
|
|
9
|
-
*
|
|
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.
|
|
10
18
|
*/
|
|
11
19
|
|
|
12
20
|
/*
|
|
@@ -16,7 +24,7 @@
|
|
|
16
24
|
* which conversion is applied. If `value` is Qnil the row is marked
|
|
17
25
|
* invalid (NULL).
|
|
18
26
|
*
|
|
19
|
-
* Raises
|
|
27
|
+
* Raises ArgumentError for unsupported types - callers that cannot
|
|
20
28
|
* tolerate longjmp should wrap the call in rb_protect.
|
|
21
29
|
*/
|
|
22
30
|
void rbduckdb_vector_set_value_at(duckdb_vector vector,
|
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
|
|
@@ -384,55 +384,30 @@ module DuckDB
|
|
|
384
384
|
|
|
385
385
|
private
|
|
386
386
|
|
|
387
|
-
#
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
when
|
|
393
|
-
|
|
394
|
-
when
|
|
395
|
-
|
|
396
|
-
when
|
|
397
|
-
|
|
398
|
-
when
|
|
399
|
-
|
|
400
|
-
when
|
|
401
|
-
bind_varchar(index, value.strftime('%Y-%m-%d %H:%M:%S.%N'))
|
|
402
|
-
when Date
|
|
403
|
-
bind_varchar(index, value.strftime('%Y-%m-%d'))
|
|
404
|
-
when BigDecimal
|
|
405
|
-
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)
|
|
406
401
|
else
|
|
407
402
|
raise(DuckDB::Error, "not supported type `#{value}` (#{value.class})")
|
|
408
403
|
end
|
|
409
404
|
end
|
|
410
405
|
|
|
411
|
-
def bind_integer_value(index, value)
|
|
412
|
-
if RANGE_INT64.cover?(value)
|
|
413
|
-
bind_int64(index, value)
|
|
414
|
-
else
|
|
415
|
-
bind_varchar(index, value.to_s)
|
|
416
|
-
end
|
|
417
|
-
end
|
|
418
|
-
|
|
419
|
-
def bind_string_value(index, value)
|
|
420
|
-
if blob?(value)
|
|
421
|
-
bind_blob(index, value)
|
|
422
|
-
else
|
|
423
|
-
bind_varchar(index, value)
|
|
424
|
-
end
|
|
425
|
-
end
|
|
426
|
-
|
|
427
406
|
def bind_with_name(name, value)
|
|
428
407
|
raise DuckDB::Error, 'not supported binding with name' unless respond_to?(:bind_parameter_index)
|
|
429
408
|
|
|
430
409
|
i = bind_parameter_index(name)
|
|
431
410
|
bind_with_index(i, value)
|
|
432
411
|
end
|
|
433
|
-
|
|
434
|
-
def blob?(value)
|
|
435
|
-
value.instance_of?(DuckDB::Blob) || value.encoding == Encoding::BINARY
|
|
436
|
-
end
|
|
437
412
|
end
|
|
438
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
|
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.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
|