Skip to content

Commit

Permalink
Use ILIKE for model.arel_table[:column]#matches (PNixx#115)
Browse files Browse the repository at this point in the history
Just like Postgres does :)
  • Loading branch information
stympy authored Jan 31, 2024
1 parent c91066a commit 3affafa
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/arel/visitors/clickhouse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ def visit_Arel_Nodes_Using o, collector
collector
end

def visit_Arel_Nodes_Matches(o, collector)
op = o.case_sensitive ? " LIKE " : " ILIKE "
infix_value o, collector, op
end

def visit_Arel_Nodes_DoesNotMatch(o, collector)
op = o.case_sensitive ? " NOT LIKE " : " NOT ILIKE "
infix_value o, collector, op
end

def sanitize_as_setting_value(value)
if value == :default
'DEFAULT'
Expand Down
14 changes: 14 additions & 0 deletions spec/cases/model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,20 @@ class Model < ActiveRecord::Base
end
end

describe 'arel predicates' do
describe '#matches' do
it 'uses ilike for case insensitive matches' do
sql = model.where(model.arel_table[:event_name].matches('some event')).to_sql
expect(sql).to eq("SELECT sample.* FROM sample WHERE sample.event_name ILIKE 'some event'")
end

it 'uses like for case sensitive matches' do
sql = model.where(model.arel_table[:event_name].matches('some event', nil, true)).to_sql
expect(sql).to eq("SELECT sample.* FROM sample WHERE sample.event_name LIKE 'some event'")
end
end
end

describe 'DateTime64 create' do
it 'create a new record' do
time = DateTime.parse('2023-07-21 08:00:00.123')
Expand Down

0 comments on commit 3affafa

Please sign in to comment.