From 3affafa1352e8d16773cbc19b95ed407c8d26ae6 Mon Sep 17 00:00:00 2001 From: Benjamin Curtis Date: Tue, 30 Jan 2024 23:33:07 -0800 Subject: [PATCH] Use ILIKE for model.arel_table[:column]#matches (#115) Just like Postgres does :) --- lib/arel/visitors/clickhouse.rb | 10 ++++++++++ spec/cases/model_spec.rb | 14 ++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/lib/arel/visitors/clickhouse.rb b/lib/arel/visitors/clickhouse.rb index e9a180aa..f39942d4 100644 --- a/lib/arel/visitors/clickhouse.rb +++ b/lib/arel/visitors/clickhouse.rb @@ -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' diff --git a/spec/cases/model_spec.rb b/spec/cases/model_spec.rb index fafd3993..a48a3f97 100644 --- a/spec/cases/model_spec.rb +++ b/spec/cases/model_spec.rb @@ -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')