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
47 changes: 23 additions & 24 deletions lib/code_to_query/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -552,36 +552,14 @@ def build_arel_condition(table, filter, bind_spec)
operator = filter['op']

case operator
when '='
key = filter_bind_key(filter)
append_bind_spec(bind_spec, key: key, column: filter['column'])
column.eq(Arel::Nodes::BindParam.new(key))
when '!=', '<>'
key = filter_bind_key(filter)
append_bind_spec(bind_spec, key: key, column: filter['column'])
column.not_eq(Arel::Nodes::BindParam.new(key))
when '=', '!=', '<>', '>', '>=', '<', '<='
build_arel_scalar_comparison(column, filter, bind_spec)
when 'exists'
# Force fallback to string builder for complex correlated subqueries
raise StandardError, 'exists Arel compilation is not implemented; falling back to string builder'
when 'not_exists'
# Force fallback to string builder for complex correlated subqueries
raise StandardError, 'not_exists Arel compilation is not implemented; falling back to string builder'
when '>'
key = filter_bind_key(filter)
append_bind_spec(bind_spec, key: key, column: filter['column'])
column.gt(Arel::Nodes::BindParam.new(key))
when '>='
key = filter_bind_key(filter)
append_bind_spec(bind_spec, key: key, column: filter['column'])
column.gteq(Arel::Nodes::BindParam.new(key))
when '<'
key = filter_bind_key(filter)
append_bind_spec(bind_spec, key: key, column: filter['column'])
column.lt(Arel::Nodes::BindParam.new(key))
when '<='
key = filter_bind_key(filter)
append_bind_spec(bind_spec, key: key, column: filter['column'])
column.lteq(Arel::Nodes::BindParam.new(key))
when 'between'
start_key, end_key = between_bind_keys(filter)
append_bind_spec(bind_spec, key: start_key, column: filter['column'])
Expand Down Expand Up @@ -609,6 +587,27 @@ def build_arel_condition(table, filter, bind_spec)
end
end

def build_arel_scalar_comparison(column, filter, bind_spec)
key = filter_bind_key(filter)
bind_param = Arel::Nodes::BindParam.new(key)
append_bind_spec(bind_spec, key: key, column: filter['column'])

case filter['op']
when '='
column.eq(bind_param)
when '!=', '<>'
column.not_eq(bind_param)
when '>'
column.gt(bind_param)
when '>='
column.gteq(bind_param)
when '<'
column.lt(bind_param)
when '<='
column.lteq(bind_param)
end
end

def filter_bind_key(filter)
filter['param'] || filter['column']
end
Expand Down
45 changes: 45 additions & 0 deletions spec/code_to_query/compiler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,51 @@
end
end

context 'with Arel scalar comparison filters' do
let(:arel_table) { Arel::Table.new(:users) }

it 'builds scalar predicates for every comparison operator' do
expectations = {
'=' => Arel::Nodes::Equality,
'!=' => Arel::Nodes::NotEqual,
'<>' => Arel::Nodes::NotEqual,
'>' => Arel::Nodes::GreaterThan,
'>=' => Arel::Nodes::GreaterThanOrEqual,
'<' => Arel::Nodes::LessThan,
'<=' => Arel::Nodes::LessThanOrEqual
}

expectations.each do |operator, node_class|
bind_spec = []
filter = { 'column' => 'age', 'op' => operator, 'param' => "age_#{operator}" }

condition = compiler.__send__(:build_arel_condition, arel_table, filter, bind_spec)

expect(condition).to be_a(node_class)
expect(bind_spec).to eq([{ key: "age_#{operator}", column: 'age', cast: nil }])
end
end

it 'preserves bind order across scalar predicates' do
bind_spec = []
filters = [
{ 'column' => 'active', 'op' => '=', 'param' => 'is_active' },
{ 'column' => 'age', 'op' => '>=', 'param' => 'min_age' },
{ 'column' => 'score', 'op' => '<', 'param' => 'max_score' }
]

filters.each do |filter|
compiler.__send__(:build_arel_condition, arel_table, filter, bind_spec)
end

expect(bind_spec).to eq([
{ key: 'is_active', column: 'active', cast: nil },
{ key: 'min_age', column: 'age', cast: nil },
{ key: 'max_score', column: 'score', cast: nil }
])
end
end

context 'with NOT EXISTS anti-join filter' do
let(:intent) do
{
Expand Down
Loading