Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

misc: Add support of array in map #9

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion lib/active_record/connection_adapters/clickhouse/oid/map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ def deserialize(value)
def serialize(value)
return '{}' if value.nil?

"{#{value.map { |k, v| "'#{k}': '#{v}'" }.join(', ')}}"
res = value.map { |k, v| "#{quote(k, key_type)}: #{quote(v, value_type)}" }.join(', ')
"{#{res}}"
end

private
Expand All @@ -48,10 +49,29 @@ def cast_type(type)
: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\(.*\)/
byebug
sub_type = type.match(/Array\((.+)\)/).captures.first
"[#{value.map { |v| quote(v, sub_type) }.join(', ')}]"
else
value
end
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/active_record/connection_adapters/clickhouse_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ def cast_type(type)
:datetime
when /Date/
:date
when /Array/
type
else
:string
end
Expand Down Expand Up @@ -267,6 +269,8 @@ def quote(value)
case value
when Array
'[' + value.map { |v| quote(v) }.join(', ') + ']'
when Hash
'{' + value.map { |k, v| "#{quote(k)}: #{quote(v)}" }.join(', ') + '}'
else
super
end
Expand Down
1 change: 1 addition & 0 deletions lib/clickhouse-activerecord/schema_dumper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ def prepare_column_options(column)
if column.type == :map
spec[:key_type] = "\"#{column.key_type}\""
spec[:value_type] = "\"#{column.value_type}\""
spec[:array] = nil
end

spec.merge(super).compact
Expand Down
Loading