Module: DuckDB::Converter
- Included in:
- Appender, PreparedStatement
- Defined in:
- lib/duckdb/converter.rb,
ext/duckdb/conveter.c
Constant Summary collapse
- HALF_HUGEINT_BIT =
64
- HALF_HUGEINT =
1 << HALF_HUGEINT_BIT
- FLIP_HUGEINT =
1 << 63
- EPOCH =
Time.local(1970, 1, 1)
- EPOCH_UTC =
Time.new(1970, 1, 1, 0, 0, 0, 0)
Class Method Summary collapse
- ._parse_date(value) ⇒ Object
- ._parse_time(value) ⇒ Object
- ._to_date(year, month, day) ⇒ Object
- ._to_decimal_from_hugeint(width, scale, upper, lower = nil) ⇒ Object
- ._to_decimal_from_value(_width, scale, value) ⇒ Object
- ._to_hugeint_from_vector(lower, upper) ⇒ Object
- ._to_interval_from_vector(months, days, micros) ⇒ Object
- ._to_query_progress(percentage, rows_processed, total_rows_to_process) ⇒ Object
- ._to_time(year, month, day, hour, minute, second, microsecond) ⇒ Object
- ._to_time_from_duckdb_time(hour, minute, second, microsecond) ⇒ Object
- ._to_time_from_duckdb_time_tz(hour, min, sec, micro, timezone) ⇒ Object
- ._to_time_from_duckdb_timestamp_ms(time) ⇒ Object
- ._to_time_from_duckdb_timestamp_ns(time) ⇒ Object
- ._to_time_from_duckdb_timestamp_s(time) ⇒ Object
- ._to_time_from_duckdb_timestamp_tz(bits) ⇒ Object
- ._to_uuid_from_vector(lower, upper) ⇒ Object
Class Method Details
._parse_date(value) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/duckdb/converter.rb', line 123 def _parse_date(value) case value when Date, Time value else begin Date.parse(value) rescue StandardError => e raise(ArgumentError, "Cannot parse `#{value.inspect}` to Date object. #{e.}") end end end |
._parse_time(value) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/duckdb/converter.rb', line 136 def _parse_time(value) case value when Time value else begin Time.parse(value) rescue StandardError => e raise(ArgumentError, "Cannot parse `#{value.inspect}` to Time object. #{e.}") end end end |
._to_date(year, month, day) ⇒ Object
18 19 20 |
# File 'lib/duckdb/converter.rb', line 18 def _to_date(year, month, day) Date.new(year, month, day) end |
._to_decimal_from_hugeint(width, scale, upper, lower = nil) ⇒ Object
99 100 101 102 |
# File 'lib/duckdb/converter.rb', line 99 def _to_decimal_from_hugeint(width, scale, upper, lower = nil) v = lower.nil? ? upper : _to_hugeint_from_vector(lower, upper) _to_decimal_from_value(width, scale, v) end |
._to_decimal_from_value(_width, scale, value) ⇒ Object
104 105 106 107 108 109 |
# File 'lib/duckdb/converter.rb', line 104 def _to_decimal_from_value(_width, scale, value) v = value.to_s v = v.rjust(scale + 1, '0') if v.length < scale v[-scale, 0] = '.' if scale.positive? BigDecimal(v) end |
._to_hugeint_from_vector(lower, upper) ⇒ Object
95 96 97 |
# File 'lib/duckdb/converter.rb', line 95 def _to_hugeint_from_vector(lower, upper) (upper << HALF_HUGEINT_BIT) + lower end |
._to_interval_from_vector(months, days, micros) ⇒ Object
111 112 113 |
# File 'lib/duckdb/converter.rb', line 111 def _to_interval_from_vector(months, days, micros) Interval.new(interval_months: months, interval_days: days, interval_micros: micros) end |
._to_query_progress(percentage, rows_processed, total_rows_to_process) ⇒ Object
149 150 151 |
# File 'lib/duckdb/converter.rb', line 149 def _to_query_progress(percentage, rows_processed, total_rows_to_process) DuckDB::QueryProgress.new(percentage, rows_processed, total_rows_to_process).freeze end |
._to_time(year, month, day, hour, minute, second, microsecond) ⇒ Object
22 23 24 |
# File 'lib/duckdb/converter.rb', line 22 def _to_time(year, month, day, hour, minute, second, microsecond) Time.local(year, month, day, hour, minute, second, microsecond) end |
._to_time_from_duckdb_time(hour, minute, second, microsecond) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/duckdb/converter.rb', line 26 def _to_time_from_duckdb_time(hour, minute, second, microsecond) Time.parse( format( '%<hour>02d:%<minute>02d:%<second>02d.%<microsecond>06d', hour: hour, minute: minute, second: second, microsecond: microsecond ) ) end |
._to_time_from_duckdb_time_tz(hour, min, sec, micro, timezone) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/duckdb/converter.rb', line 52 def _to_time_from_duckdb_time_tz(hour, min, sec, micro, timezone) sign = '+' if timezone.negative? timezone = -timezone sign = '-' end tzhour = timezone / 3600 tzmin = (timezone % 3600) / 60 Time.parse( format( '%<hour>02d:%<min>02d:%<sec>02d.%<micro>06d%<sign>s%<tzhour>02d:%<tzmin>02d', hour: hour, min: min, sec: sec, micro: micro, sign: sign, tzhour: tzhour, tzmin: tzmin ) ) end |
._to_time_from_duckdb_timestamp_ms(time) ⇒ Object
42 43 44 45 |
# File 'lib/duckdb/converter.rb', line 42 def (time) tm = EPOCH + (time / 1000) Time.local(tm.year, tm.month, tm.day, tm.hour, tm.min, tm.sec, time % 1000 * 1000) end |
._to_time_from_duckdb_timestamp_ns(time) ⇒ Object
47 48 49 50 |
# File 'lib/duckdb/converter.rb', line 47 def (time) tm = EPOCH + (time / 1_000_000_000) Time.local(tm.year, tm.month, tm.day, tm.hour, tm.min, tm.sec, time % 1_000_000_000 / 1000) end |
._to_time_from_duckdb_timestamp_s(time) ⇒ Object
38 39 40 |
# File 'lib/duckdb/converter.rb', line 38 def (time) EPOCH + time end |
._to_time_from_duckdb_timestamp_tz(bits) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/duckdb/converter.rb', line 76 def (bits) micro = bits % 1_000_000 sec = (bits / 1_000_000) time = EPOCH_UTC + sec Time.parse( format( '%<year>04d-%<mon>02d-%<day>02d %<hour>02d:%<min>02d:%<sec>02d.%<micro>06d +0000', year: time.year, mon: time.month, day: time.day, hour: time.hour, min: time.min, sec: time.sec, micro: micro ) ) end |
._to_uuid_from_vector(lower, upper) ⇒ Object
115 116 117 118 119 120 121 |
# File 'lib/duckdb/converter.rb', line 115 def _to_uuid_from_vector(lower, upper) upper = upper ^ FLIP_HUGEINT upper += HALF_HUGEINT if upper.negative? str = _to_hugeint_from_vector(lower, upper).to_s(16).rjust(32, '0') "#{str[0, 8]}-#{str[8, 4]}-#{str[12, 4]}-#{str[16, 4]}-#{str[20, 12]}" end |