Class: RubySnowflake::Row

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_snowflake/row.rb

Constant Summary collapse

EPOCH_JULIAN_DAY_NUMBER =
Date.new(1970,1,1).jd
TIME_FORMAT =
"%s.%N".freeze

Instance Method Summary collapse

Constructor Details

#initialize(row_types, column_to_index, data) ⇒ Row

Returns a new instance of Row.



11
12
13
14
15
# File 'lib/ruby_snowflake/row.rb', line 11

def initialize(row_types, column_to_index, data)
  @row_types = row_types
  @data = data
  @column_to_index = column_to_index
end

Instance Method Details

#[](column) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ruby_snowflake/row.rb', line 18

def [](column)
  index = column.is_a?(Numeric) ? Integer(column) : @column_to_index[column]
  return nil if index.nil?
  return nil if @data[index].nil?

  case @row_types[index][:type]
  when :boolean
    @data[index] == "true"
  when :date
    Date.jd(Integer(@data[index]) + EPOCH_JULIAN_DAY_NUMBER)
  when :fixed
    if @row_types[index][:scale] == 0
      Integer(@data[index])
    else
      BigDecimal(@data[index]).round(@row_types[index][:scale])
    end

  # snowflake treats these all as 64 bit IEEE 754 floating point numbers, and will we too
  when :float, :double, :"double precision", :real
    Float(@data[index])

  # Despite snowflake indicating that it sends the offset in minutes, the actual time in UTC
  # is always sent in the first half of the data. If an offset is sent it looks like:
  #   "1641008096.123000000 1980"
  # If there isn't one, it's just like this:
  #   "1641065696.123000000"
  # in all cases, the actual time, in UTC is the float value, and the offset is ignorable
  when :time, :datetime, :timestamp, :timestamp_ntz, :timestamp_ltz, :timestamp_tz
    Time.strptime(@data[index], TIME_FORMAT).utc
  else
    @data[index]
  end
end

#to_hObject



52
53
54
55
56
57
58
# File 'lib/ruby_snowflake/row.rb', line 52

def to_h
  output = {}
  @column_to_index.each_pair do |name, index|
    output[name.downcase] = self[index]
  end
  output
end

#to_sObject



60
61
62
# File 'lib/ruby_snowflake/row.rb', line 60

def to_s
  to_h.to_s
end