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

Class Method Summary collapse

Class Method Details

._parse_date(value) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/duckdb/converter.rb', line 64

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_time(value) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/duckdb/converter.rb', line 77

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



16
17
18
# File 'lib/duckdb/converter.rb', line 16

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

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



40
41
42
43
# File 'lib/duckdb/converter.rb', line 40

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



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

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



36
37
38
# File 'lib/duckdb/converter.rb', line 36

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

._to_interval_from_vector(months, days, micros) ⇒ Object



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

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



90
91
92
# File 'lib/duckdb/converter.rb', line 90

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



20
21
22
# File 'lib/duckdb/converter.rb', line 20

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



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/duckdb/converter.rb', line 24

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_uuid_from_vector(lower, upper) ⇒ Object



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

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