Welcome to mirror list, hosted at ThFree Co, Russian Federation.

formatter.rb « client « click_house « lib « click_house-client « gems - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: de7ae72bdf8e43bba71917cf674f6e4219c87dd0 (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
# 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) }
      }.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