Class: ClickHouse::Client::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/click_house/client/formatter.rb

Constant Summary collapse

DEFAULT =
->(value) { value }
BASIC_TYPE_CASTERS =
{
  'Int32' => ->(value) { Integer(value) },
  'UInt32' => ->(value) { Integer(value) },
  'Int64' => ->(value) { Integer(value) },
  'UInt64' => ->(value) { Integer(value) },
  "DateTime64(6, 'UTC')" => ->(value) { ActiveSupport::TimeZone['UTC'].parse(value) },
  "IntervalSecond" => ->(value) { ActiveSupport::Duration.build(value.to_i) },
  "IntervalMillisecond" => ->(value) { ActiveSupport::Duration.build(value.to_i / 1000.0) }
}.freeze
TYPE_CASTERS =
BASIC_TYPE_CASTERS.merge(
  BASIC_TYPE_CASTERS.transform_keys { |type| "Nullable(#{type})" }
    .transform_values { |caster| ->(value) { value.nil? ? nil : caster.call(value) } }
)

Class Method Summary collapse

Class Method Details

.format(result) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/click_house/client/formatter.rb', line 23

def self.format(result)
  name_type_mapping = result['meta'].each_with_object({}) do |column, hash|
    hash[column['name']] = column['type']
  end

  result['data'].map do |row|
    row.each_with_object({}) do |(column, value), casted_row|
      caster = TYPE_CASTERS.fetch(name_type_mapping[column], DEFAULT)

      casted_row[column] = caster.call(value)
    end
  end
end