blob: a19c55e3e3dddb63ba2d2bb441477ec258355a95 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# frozen_string_literal: true
module ClickHouse
module Client
class Formatter
DEFAULT = ->(value) { value }
TYPE_CASTERS = {
'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
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
end
end
end
|