Module: DuckDB::Converter

Included in:
Appender, PreparedStatement
Defined in:
lib/duckdb/converter.rb,
lib/duckdb/converter/int_to_sym.rb,
ext/duckdb/conveter.c

Overview

:nodoc: all

Defined Under Namespace

Modules: IntToSym

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

Class Method Details

._parse_date(value) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/duckdb/converter.rb', line 132

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.message}")
    end
  end
end

._parse_deciaml(value) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/duckdb/converter.rb', line 158

def _parse_deciaml(value)
  case value
  when BigDecimal
    value
  else
    begin
      BigDecimal(value.to_s)
    rescue StandardError => e
      raise(ArgumentError, "Cannot parse `#{value.inspect}` to BigDecimal object. #{e.message}")
    end
  end
end

._parse_time(value) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/duckdb/converter.rb', line 145

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.message}")
    end
  end
end

._to_date(year, month, day) ⇒ Object



27
28
29
# File 'lib/duckdb/converter.rb', line 27

def _to_date(year, month, day)
  Date.new(year, month, day)
end

._to_decimal_from_hugeint(width, scale, upper, lower = nil) ⇒ Object



108
109
110
111
# File 'lib/duckdb/converter.rb', line 108

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



113
114
115
116
117
118
# File 'lib/duckdb/converter.rb', line 113

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



104
105
106
# File 'lib/duckdb/converter.rb', line 104

def _to_hugeint_from_vector(lower, upper)
  (upper << HALF_HUGEINT_BIT) + lower
end

._to_infinity(value) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/duckdb/converter.rb', line 19

def _to_infinity(value)
  if value.positive?
    DuckDB::Infinity::POSITIVE
  else
    DuckDB::Infinity::NEGATIVE
  end
end

._to_interval_from_vector(months, days, micros) ⇒ Object



120
121
122
# File 'lib/duckdb/converter.rb', line 120

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



171
172
173
# File 'lib/duckdb/converter.rb', line 171

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



31
32
33
# File 'lib/duckdb/converter.rb', line 31

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



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/duckdb/converter.rb', line 35

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



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/duckdb/converter.rb', line 61

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



51
52
53
54
# File 'lib/duckdb/converter.rb', line 51

def _to_time_from_duckdb_timestamp_ms(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



56
57
58
59
# File 'lib/duckdb/converter.rb', line 56

def _to_time_from_duckdb_timestamp_ns(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



47
48
49
# File 'lib/duckdb/converter.rb', line 47

def _to_time_from_duckdb_timestamp_s(time)
  EPOCH + time
end

._to_time_from_duckdb_timestamp_tz(bits) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/duckdb/converter.rb', line 85

def _to_time_from_duckdb_timestamp_tz(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



124
125
126
127
128
129
130
# File 'lib/duckdb/converter.rb', line 124

def _to_uuid_from_vector(lower, 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