From b6c1a88d822ba4183aea2f51aa9bc52206dd169d Mon Sep 17 00:00:00 2001 From: Julien Bourdeau Date: Tue, 20 Aug 2024 12:05:15 +0200 Subject: [PATCH] feat: Support map of arrays --- .../connection_adapters/clickhouse/oid/map.rb | 4 ++++ .../connection_adapters/clickhouse/schema_creation.rb | 5 ++++- lib/clickhouse-activerecord/schema_dumper.rb | 7 +++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/active_record/connection_adapters/clickhouse/oid/map.rb b/lib/active_record/connection_adapters/clickhouse/oid/map.rb index 08067a33..9ae49449 100644 --- a/lib/active_record/connection_adapters/clickhouse/oid/map.rb +++ b/lib/active_record/connection_adapters/clickhouse/oid/map.rb @@ -26,6 +26,8 @@ def type def deserialize(value) if value.is_a?(::Hash) value.map { |k, item| [k.to_s, deserialize(item)] }.to_h + elsif value.is_a?(::Array) + value.map { |item| deserialize(item) } else return value if value.nil? case @subtype @@ -44,6 +46,8 @@ def deserialize(value) def serialize(value) if value.is_a?(::Hash) value.map { |k, item| [k.to_s, serialize(item)] }.to_h + elsif value.is_a?(::Array) + value.map { |item| serialize(item) } else return value if value.nil? case @subtype diff --git a/lib/active_record/connection_adapters/clickhouse/schema_creation.rb b/lib/active_record/connection_adapters/clickhouse/schema_creation.rb index 3d795a2b..d9da9b8c 100644 --- a/lib/active_record/connection_adapters/clickhouse/schema_creation.rb +++ b/lib/active_record/connection_adapters/clickhouse/schema_creation.rb @@ -33,7 +33,10 @@ def add_column_options!(sql, options) if options[:array] sql.gsub!(/\s+(.*)/, ' Array(\1)') end - if options[:map] + if options[:map] == :array + sql.gsub!(/\s+(.*)/, ' Map(String, Array(\1))') + end + if options[:map] == true sql.gsub!(/\s+(.*)/, ' Map(String, \1)') end sql.gsub!(/(\sString)\(\d+\)/, '\1') diff --git a/lib/clickhouse-activerecord/schema_dumper.rb b/lib/clickhouse-activerecord/schema_dumper.rb index 44a2bd8c..23fb0333 100644 --- a/lib/clickhouse-activerecord/schema_dumper.rb +++ b/lib/clickhouse-activerecord/schema_dumper.rb @@ -145,6 +145,10 @@ def schema_array(column) end def schema_map(column) + if column.sql_type =~ /Map\(([^,]+),\s*(Array)\)/ + return :array + end + (column.sql_type =~ /Map?\(/).nil? ? nil : true end @@ -157,6 +161,9 @@ def prepare_column_options(column) spec[:unsigned] = schema_unsigned(column) spec[:array] = schema_array(column) spec[:map] = schema_map(column) + if spec[:map] == :array + spec[:array] = nil + end spec[:low_cardinality] = schema_low_cardinality(column) spec.merge(super).compact end