rails/activerecord/lib/arel/nodes/nary.rb
Jean Boussier bfcc13ab7c Arel: make Or nodes "Nary" like And
Fix: https://github.com/rails/rails/issues/51386

This significantly reduce the depth of the tree for large `OR`
conditions. I was initially a bit on the fence about that fix,
but given that `And` is already implemented this way, I see no
reasons not to do the same.

Amusingly, the reported repro script now makes SQLite fail:

```ruby
SQLite3::SQLException: Expression tree is too large (maximum depth 1000)
```
2024-04-04 14:59:56 +02:00

40 lines
720 B
Ruby

# frozen_string_literal: true
module Arel # :nodoc: all
module Nodes
class Nary < Arel::Nodes::NodeExpression
attr_reader :children
def initialize(children)
super()
@children = children
end
def left
children.first
end
def right
children[1]
end
def fetch_attribute(&block)
children.any? && children.all? { |child| child.fetch_attribute(&block) }
end
def hash
[self.class, children].hash
end
def eql?(other)
self.class == other.class &&
self.children == other.children
end
alias :== :eql?
end
And = Class.new(Nary)
Or = Class.new(Nary)
end
end