From b231b3a2ca76fb929a1f10bd125cb00ae44f6bb8 Mon Sep 17 00:00:00 2001 From: Vladislav Syabruk Date: Mon, 19 Aug 2024 11:42:49 +0300 Subject: [PATCH] Fix map --- .../connection_adapters/clickhouse/oid/map.rb | 59 +++++++++++++++---- 1 file changed, 46 insertions(+), 13 deletions(-) diff --git a/lib/active_record/connection_adapters/clickhouse/oid/map.rb b/lib/active_record/connection_adapters/clickhouse/oid/map.rb index 08067a33..f2495fd8 100644 --- a/lib/active_record/connection_adapters/clickhouse/oid/map.rb +++ b/lib/active_record/connection_adapters/clickhouse/oid/map.rb @@ -5,22 +5,21 @@ module ConnectionAdapters module Clickhouse module OID # :nodoc: class Map < Type::Value # :nodoc: + attr_reader :key_type, :value_type def initialize(sql_type) - @subtype = case sql_type - when /U?Int\d+/ - :integer - when /DateTime/ - :datetime - when /Date/ - :date - else - :string - end + types = sql_type.match(/Map\((.+),\s?(.+)\)/).captures + + @key_type = cast_type(types.first) + @value_type = cast_type(types.last) end def type - @subtype + :map + end + + def cast(value) + value end def deserialize(value) @@ -28,7 +27,7 @@ def deserialize(value) value.map { |k, item| [k.to_s, deserialize(item)] }.to_h else return value if value.nil? - case @subtype + case @value_type when :integer value.to_i when :datetime @@ -46,7 +45,7 @@ def serialize(value) value.map { |k, item| [k.to_s, serialize(item)] }.to_h else return value if value.nil? - case @subtype + case @value_type when :integer value.to_i when :datetime @@ -61,6 +60,40 @@ def serialize(value) end end + private + + def cast_type(type) + return type if type.nil? + + case type + when /U?Int\d+/ + :integer + when /DateTime/ + :datetime + when /Date/ + :date + when /Array\(.*\)/ + type + else + :string + end + end + + def quote(value, type) + case cast_type(type) + when :string + "'#{value}'" + when :integer + value + when :datetime, :date + "'#{value.iso8601}'" + when /Array\(.*\)/ + sub_type = type.match(/Array\((.+)\)/).captures.first + "[#{value.map { |v| quote(v, sub_type) }.join(', ')}]" + else + value + end + end end end end