Skip to content

Commit

Permalink
Support JSON type
Browse files Browse the repository at this point in the history
  • Loading branch information
Sylvain UTARD committed Jan 28, 2025
1 parent 5f2fbf4 commit 1d0bb9a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .docker/clickhouse/users.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<profiles>
<default>
<load_balancing>random</load_balancing>
<enable_json_type>1</enable_json_type>
</default>
</profiles>

Expand Down Expand Up @@ -31,4 +32,4 @@
</interval>
</default>
</quotas>
</clickhouse>
</clickhouse>
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ false`. The default integer is `UInt32`
| UInt256 | 0 to ... | 8+ |
| Array | ... | ... |
| Map | ... | ... |
| JSON | ... | ... |

Example:

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 @@ -110,6 +110,8 @@ class ClickhouseAdapter < AbstractAdapter
uint64: { name: 'UInt64' },
# uint128: { name: 'UInt128' }, not yet implemented in clickhouse
uint256: { name: 'UInt256' },

json: { name: 'JSON' },
}.freeze

include Clickhouse::SchemaStatements
Expand Down Expand Up @@ -242,6 +244,8 @@ def initialize_type_map(m) # :nodoc:
m.register_type(%r(Map)) do |sql_type|
Clickhouse::OID::Map.new(sql_type)
end

m.register_type %r(JSON)i, Type::Json.new
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def up
t.string :byte_array
t.uuid :relation_uuid
t.decimal :decimal_value, precision: 38, scale: 16
t.json :json_value, null: false, default: {}
end
end
end
22 changes: 22 additions & 0 deletions spec/single/model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,28 @@ class ModelPk < ActiveRecord::Base
end
end

describe 'JSON column type' do
let(:json) { { 'key' => 'value' } }
let!(:record1) { Model.create!(event_name: 'some event', json_value: json) }

it 'is mapped to :json' do
type = Model.columns_hash['json_value'].type
expect(type).to eq(:json)
end

it 'keeps JSON value' do
expect(Model.first.json_value).to eq(json)
end

context 'when the JSON column is complex' do
let(:json) { { 'key' => { 'nested_key' => 'value', 'another_key' => ['something'] } } }

it 'keeps JSON value' do
expect(Model.first.json_value).to eq(json)
end
end
end

describe 'boolean column type' do
let!(:record1) { Model.create!(event_name: 'some event', event_value: 1, date: date) }

Expand Down

0 comments on commit 1d0bb9a

Please sign in to comment.