Skip to content

Commit

Permalink
feat: Support map of arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
julienbourdeau authored and vincent-pochet committed Oct 8, 2024
1 parent ddfa259 commit b6c1a88
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/active_record/connection_adapters/clickhouse/oid/map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
7 changes: 7 additions & 0 deletions lib/clickhouse-activerecord/schema_dumper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down

0 comments on commit b6c1a88

Please sign in to comment.