Class: DuckDB::Interval

Inherits:
Object
  • Object
show all
Defined in:
lib/duckdb/interval.rb

Overview

Interval class represents DuckDB’s interval type value.

The usage is as follows:

require 'duckdb'

interval = DuckDB::Interval.new(interval_months: 14, interval_days: 3, interval_micros: 14706123456)
# or
# interval = DuckDB::Interval.mk_interval(year: 1, month: 2, day: 3, hour: 4, min: 5, sec: 6, usec: 123456)
# or
# interval = DuckDB::Interval.iso8601_parse('P1Y2M3DT4H5M6.123456S')

db = DuckDB::Database.open # database in memory
con = db.connect

con.execute('CREATE TABLE users (value INTERVAL)')

require 'duckdb'
db = DuckDB::Database.open
con = db.connect
con.query('CREATE TABLE intervals (interval_value INTERVAL)')
appender = con.appender('intervals')
appender
  .append_interval(interval)
  .end_row
  .flush

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(interval_months: 0, interval_days: 0, interval_micros: 0) ⇒ Interval

creates the Interval object. The arguments are the number of months, days, and microseconds. The default value is 0.

DuckDB::Interval.new(interval_months: 1, interval_days: 2, interval_micros: 3)
=> #<DuckDB::Interval:0x00007f9b9c0b3b60 @interval_months=1, @interval_days=2, @interval_micros=3>


156
157
158
159
160
# File 'lib/duckdb/interval.rb', line 156

def initialize(interval_months: 0, interval_days: 0, interval_micros: 0)
  @interval_months = interval_months
  @interval_days = interval_days
  @interval_micros = interval_micros
end

Instance Attribute Details

#interval_daysObject (readonly)

Returns the value of attribute interval_days.



148
149
150
# File 'lib/duckdb/interval.rb', line 148

def interval_days
  @interval_days
end

#interval_microsObject (readonly)

Returns the value of attribute interval_micros.



148
149
150
# File 'lib/duckdb/interval.rb', line 148

def interval_micros
  @interval_micros
end

#interval_monthsObject (readonly)

Returns the value of attribute interval_months.



148
149
150
# File 'lib/duckdb/interval.rb', line 148

def interval_months
  @interval_months
end

Class Method Details

.iso8601_parse(value) ⇒ Object

parses the ISO8601 format string and return the Interval object.

DuckDB::Interval.iso8601_parse('P1Y2M3DT4H5M6.123456S')
=> #<DuckDB::Interval:0x00007f9b9c0b3b60 @interval_months=14, @interval_days=3, @interval_micros=14706123456>

Raises:

  • (ArgumentError)


54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/duckdb/interval.rb', line 54

def iso8601_parse(value)
  if value.length > ISO8601_STRING_LENGTH
    raise ArgumentError, "Argument of iso8601_parse is too long. It must be less than #{ISO8601_STRING_LENGTH}."
  end

  m = ISO8601_REGEXP.match(value)
  raise ArgumentError, "The argument `#{value}` can't be parse." if m.nil?

  year, month, day, hour, min, sec, usec = matched_to_i(m)

  mk_interval(year: year, month: month, day: day, hour: hour, min: min, sec: sec, usec: usec)
end

.mk_interval(year: 0, month: 0, day: 0, hour: 0, min: 0, sec: 0, usec: 0) ⇒ Object

creates the Interval object.

DuckDB::Interval.mk_interval(year: 1, month: 2, day: 3, hour: 4, min: 5, sec: 6, usec: 123456)
=> #<DuckDB::Interval:0x00007f9b9c0b3b60 @interval_months=14, @interval_days=3, @interval_micros=14706123456>


71
72
73
74
75
76
77
# File 'lib/duckdb/interval.rb', line 71

def mk_interval(year: 0, month: 0, day: 0, hour: 0, min: 0, sec: 0, usec: 0) # rubocop:disable Metrics/ParameterLists
  Interval.new(
    interval_months: (year * 12) + month,
    interval_days: day,
    interval_micros: (((hour * 3600) + (min * 60) + sec) * 1_000_000) + usec
  )
end

.to_interval(value) ⇒ Object

Convert the value to the Interval object. The value can be String or Interval object. If the value is String, it is parsed as ISO8601 format. If the value is Interval object, it is returned as is. Otherwise, ArgumentError is raised.

DuckDB::Interval.to_interval('P1Y2M3DT4H5M6.123456S')
=> #<DuckDB::Interval:0x00007f9b9c0b3b60 @interval_months=14, @interval_days=3, @interval_micros=14706123456>

interval = DuckDB::Interval.to_interval('P1Y2M3DT4H5M6.123456S')
DuckDB::Interval.to_interval(interval)
=> #<DuckDB::Interval:0x00007f9b9c0b3b60 @interval_months=14, @interval_days=3, @interval_micros=14706123456>


91
92
93
94
95
96
97
98
99
100
# File 'lib/duckdb/interval.rb', line 91

def to_interval(value)
  case value
  when String
    iso8601_parse(value)
  when Interval
    value
  else
    raise ArgumentError, "The argument `#{value}` can't be parse."
  end
end

Instance Method Details

#==(other) ⇒ Object



162
163
164
165
166
167
# File 'lib/duckdb/interval.rb', line 162

def ==(other)
  other.is_a?(Interval) &&
    @interval_months == other.interval_months &&
    @interval_days == other.interval_days &&
    @interval_micros == other.interval_micros
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


169
170
171
# File 'lib/duckdb/interval.rb', line 169

def eql?(other)
  self == other
end