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

Add support for Map datatype #144

Merged
merged 5 commits into from
Aug 16, 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,4 @@ crashlytics.properties
crashlytics-build.properties
fabric.properties
.rspec_status
.tool-versions
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ false`. The default integer is `UInt32`
| UInt64 | 0 to 18446744073709551615 | 5,6,7,8 |
| UInt256 | 0 to ... | 8+ |
| Array | ... | ... |
| Map | ... | ... |

Example:

Expand Down
68 changes: 68 additions & 0 deletions lib/active_record/connection_adapters/clickhouse/oid/map.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# frozen_string_literal: true

module ActiveRecord
module ConnectionAdapters
module Clickhouse
module OID # :nodoc:
class Map < Type::Value # :nodoc:

def initialize(sql_type)
@subtype = case sql_type
when /U?Int\d+/
:integer
when /DateTime/
:datetime
when /Date/
:date
else
:string
end
end

def type
@subtype
end

def deserialize(value)
if value.is_a?(::Hash)
value.map { |k, item| [k.to_s, deserialize(item)] }.to_h
else
return value if value.nil?
case @subtype
when :integer
value.to_i
when :datetime
::DateTime.parse(value)
when :date
::Date.parse(value)
else
super
end
end
end

def serialize(value)
if value.is_a?(::Hash)
value.map { |k, item| [k.to_s, serialize(item)] }.to_h
else
return value if value.nil?
case @subtype
when :integer
value.to_i
when :datetime
DateTime.new.serialize(value)
when :date
Date.new.serialize(value)
when :string
value.to_s
else
super
end
end
end

end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ def add_column_options!(sql, options)
if options[:array]
sql.gsub!(/\s+(.*)/, ' Array(\1)')
end
if options[:map]
sql.gsub!(/\s+(.*)/, ' Map(String, \1)')
end
sql.gsub!(/(\sString)\(\d+\)/, '\1')
sql << " DEFAULT #{quote_default_expression(options[:default], options[:column])}" if options_include_default?(options)
sql
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def enum(*args, **options)
private

def valid_column_definition_options
super + [:array, :low_cardinality, :fixed_string, :value, :type]
super + [:array, :low_cardinality, :fixed_string, :value, :type, :map]
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def assume_migrated_upto_version(version, migrations_paths = nil)
# Fix insert_all method
# https://github.com/PNixx/clickhouse-activerecord/issues/71#issuecomment-1923244983
def with_yaml_fallback(value) # :nodoc:
if value.is_a?(Array)
if value.is_a?(Array) || value.is_a?(Hash)
value
else
super
Expand Down
7 changes: 7 additions & 0 deletions lib/active_record/connection_adapters/clickhouse_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require 'active_record/connection_adapters/clickhouse/oid/date'
require 'active_record/connection_adapters/clickhouse/oid/date_time'
require 'active_record/connection_adapters/clickhouse/oid/big_integer'
require 'active_record/connection_adapters/clickhouse/oid/map'
require 'active_record/connection_adapters/clickhouse/oid/uuid'
require 'active_record/connection_adapters/clickhouse/schema_definitions'
require 'active_record/connection_adapters/clickhouse/schema_creation'
Expand Down Expand Up @@ -211,6 +212,10 @@ def initialize_type_map(m) # :nodoc:
m.register_type(%r(Array)) do |sql_type|
Clickhouse::OID::Array.new(sql_type)
end

m.register_type(%r(Map)) do |sql_type|
Clickhouse::OID::Map.new(sql_type)
end
end
end

Expand All @@ -223,6 +228,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(', ') + '}'
danielwestendorf marked this conversation as resolved.
Show resolved Hide resolved
else
super
end
Expand Down
5 changes: 5 additions & 0 deletions lib/clickhouse-activerecord/schema_dumper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ def schema_array(column)
(column.sql_type =~ /Array?\(/).nil? ? nil : true
end

def schema_map(column)
(column.sql_type =~ /Map?\(/).nil? ? nil : true
end

def schema_low_cardinality(column)
(column.sql_type =~ /LowCardinality?\(/).nil? ? nil : true
end
Expand All @@ -176,6 +180,7 @@ def prepare_column_options(column)
spec = {}
spec[:unsigned] = schema_unsigned(column)
spec[:array] = schema_array(column)
spec[:map] = schema_map(column)
spec[:low_cardinality] = schema_low_cardinality(column)
spec.merge(super).compact
end
Expand Down
11 changes: 11 additions & 0 deletions spec/fixtures/migrations/add_map_datetime/1_create_verbs_table.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateVerbsTable < ActiveRecord::Migration[7.1]
def up
create_table :verbs, options: 'MergeTree ORDER BY date', force: true do |t|
t.datetime :map_datetime, null: false, map: true
t.string :map_string, null: false, map: true
t.integer :map_int, null: false, map: true
t.date :date, null: false
end
end
end

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ def up
create_table :some, id: false do |t|
t.string :fixed_string1, fixed_string: 1, null: false
t.string :fixed_string16_array, fixed_string: 16, array: true, null: true
t.string :fixed_string16_map, fixed_string: 16, map: true, null: true
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def up
t.string :col1, low_cardinality: true, null: false
t.string :col2, low_cardinality: true, null: true
t.string :col3, low_cardinality: true, array: true, null: true
t.string :col4, low_cardinality: true, map: true, null: true
end
end
end
8 changes: 6 additions & 2 deletions spec/single/migration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,15 @@

current_schema = schema(model)

expect(current_schema.keys.count).to eq(3)
expect(current_schema.keys.count).to eq(4)
expect(current_schema).to have_key('col1')
expect(current_schema).to have_key('col2')
expect(current_schema).to have_key('col3')
expect(current_schema).to have_key('col4')
expect(current_schema['col1'].sql_type).to eq('LowCardinality(String)')
expect(current_schema['col2'].sql_type).to eq('LowCardinality(Nullable(String))')
expect(current_schema['col3'].sql_type).to eq('Array(LowCardinality(Nullable(String)))')
expect(current_schema['col4'].sql_type).to eq('Map(String, LowCardinality(Nullable(String)))')
end
end

Expand All @@ -167,11 +169,13 @@

current_schema = schema(model)

expect(current_schema.keys.count).to eq(2)
expect(current_schema.keys.count).to eq(3)
expect(current_schema).to have_key('fixed_string1')
expect(current_schema).to have_key('fixed_string16_array')
expect(current_schema).to have_key('fixed_string16_map')
expect(current_schema['fixed_string1'].sql_type).to eq('FixedString(1)')
expect(current_schema['fixed_string16_array'].sql_type).to eq('Array(Nullable(FixedString(16)))')
expect(current_schema['fixed_string16_map'].sql_type).to eq('Map(String, Nullable(FixedString(16)))')
end
end

Expand Down
57 changes: 56 additions & 1 deletion spec/single/model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,6 @@ class ModelPk < ActiveRecord::Base
end

context 'array' do

let!(:model) do
Class.new(ActiveRecord::Base) do
self.table_name = 'actions'
Expand Down Expand Up @@ -316,4 +315,60 @@ class ModelPk < ActiveRecord::Base
end
end
end

context 'map' do
let!(:model) do
Class.new(ActiveRecord::Base) do
self.table_name = 'verbs'
end
end

before do
migrations_dir = File.join(FIXTURES_PATH, 'migrations', 'add_map_datetime')
quietly { ActiveRecord::MigrationContext.new(migrations_dir, model.connection.schema_migration).up }
end

describe '#create' do
it 'creates a new record' do
expect {
model.create!(
map_datetime: {a: 1.day.ago, b: Time.now, c: '2022-12-06 15:22:49'},
map_string: {a: 'asdf', b: 'jkl' },
map_int: {a: 1, b: 2},
date: date
)
}.to change { model.count }
record = model.first
expect(record.map_datetime.is_a?(Hash)).to be_truthy
expect(record.map_datetime['a'].is_a?(DateTime)).to be_truthy
expect(record.map_string['a'].is_a?(String)).to be_truthy
expect(record.map_string).to eq({'a' => 'asdf', 'b' => 'jkl'})
expect(record.map_int.is_a?(Hash)).to be_truthy
expect(record.map_int).to eq({'a' => 1, 'b' => 2})
end

it 'create with insert all' do
expect {
model.insert_all([{
map_datetime: {a: 1.day.ago, b: Time.now, c: '2022-12-06 15:22:49'},
map_string: {a: 'asdf', b: 'jkl' },
map_int: {a: 1, b: 2},
date: date
}])
}.to change { model.count }
end

it 'get record' do
model.connection.insert("INSERT INTO #{model.table_name} (id, map_datetime, date) VALUES (1, {'a': '2022-12-05 15:22:49', 'b': '2022-12-06 15:22:49'}, '2022-12-06')")
expect(model.count).to eq(1)
record = model.first
expect(record.date.is_a?(Date)).to be_truthy
expect(record.date).to eq(Date.parse('2022-12-06'))
expect(record.map_datetime.is_a?(Hash)).to be_truthy
expect(record.map_datetime['a'].is_a?(DateTime)).to be_truthy
expect(record.map_datetime['a']).to eq(DateTime.parse('2022-12-05 15:22:49'))
expect(record.map_datetime['b']).to eq(DateTime.parse('2022-12-06 15:22:49'))
end
end
end
end
Loading