Skip to content
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
15 changes: 15 additions & 0 deletions lib/active_record/connection_adapters/clickhouse/column.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ def virtual?
default_kind.materialized? || default_kind.alias?
end

# Base Column equality ignores our extra attributes, so ActiveRecord's
# deduplication registry would intern a plain column and a MATERIALIZED
# column of the same name/type as one object — leaking virtual? onto
# the plain column and silently dropping it from INSERTs.
def ==(other)
super &&
other.respond_to?(:default_kind) && default_kind.to_s == other.default_kind.to_s &&
other.respond_to?(:codec) && codec == other.codec
end
alias eql? ==

def hash
[super, default_kind.to_s, codec].hash
end

private

def deduplicated
Expand Down
38 changes: 38 additions & 0 deletions spec/single/column_dedup_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

RSpec.describe ActiveRecord::ConnectionAdapters::Clickhouse::Column do
def build_column(default_kind: nil, codec: nil)
metadata = ActiveRecord::ConnectionAdapters::SqlTypeMetadata.new(
sql_type: 'String', type: :string
)
args = ['city_name']
args << ActiveModel::Type::String.new if ActiveRecord.version >= Gem::Version.new('8.1')
args += [nil, metadata, false, nil]
described_class.new(*args, codec: codec, default_kind: default_kind)
end

it 'does not treat a materialized column as equal to a plain column' do
plain = build_column
materialized = build_column(default_kind: 'MATERIALIZED')

expect(plain).not_to eq(materialized)
expect(plain.hash).not_to eq(materialized.hash)
end

it 'does not deduplicate a plain column into a materialized column' do
materialized = build_column(default_kind: 'MATERIALIZED').deduplicate
plain = build_column.deduplicate

expect(plain).not_to be_virtual
expect(plain).not_to equal(materialized)
end

it 'treats identical columns as equal' do
expect(build_column).to eq(build_column)
expect(build_column.hash).to eq(build_column.hash)
end

it 'does not treat columns with different codecs as equal' do
expect(build_column(codec: 'ZSTD')).not_to eq(build_column)
end
end
Loading