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

Enable JSON type in clickhouse #139

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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 .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>
<allow_experimental_object_type>1</allow_experimental_object_type>
</default>
</profiles>

Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ false`. The default integer is `UInt32`
| UInt64 | 0 to 18446744073709551615 | 5,6,7,8 |
| UInt256 | 0 to ... | 8+ |
| Array | ... | ... |
| Json* | ... | ... |

* **Note**: using the JSON type requires that `allow_experimental_object_type = 1` is [set in ClickHouse settings](https://clickhouse.com/docs/en/sql-reference/data-types/json).

Example:

Expand Down
3 changes: 3 additions & 0 deletions lib/active_record/connection_adapters/clickhouse_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ class ClickhouseAdapter < AbstractAdapter
boolean: { name: 'Bool' },
uuid: { name: 'UUID' },

json: { name: 'Object(\'json\')' },

enum8: { name: 'Enum8' },
enum16: { name: 'Enum16' },

Expand Down Expand Up @@ -207,6 +209,7 @@ def initialize_type_map(m) # :nodoc:

m.register_type %r(bool)i, ActiveModel::Type::Boolean.new
m.register_type %r{uuid}i, Clickhouse::OID::Uuid.new
m.register_type %r{Object\('json'\)}i, Type::Json.new
# register_class_with_limit m, %r(Array), Clickhouse::OID::Array
m.register_type(%r(Array)) do |sql_type|
Clickhouse::OID::Array.new(sql_type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def up
t.string :byte_array, null: true
t.uuid :relation_uuid
t.decimal :decimal_value, precision: 38, scale: 16, null: true
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 @@ -154,6 +154,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 'UUID column type' do
let(:random_uuid) { SecureRandom.uuid }
let!(:record1) do
Expand Down
Loading