From 110e0e1fcceab68716e0c75d87baffb14403b288 Mon Sep 17 00:00:00 2001 From: Maxime Lapointe Date: Tue, 25 Jul 2017 14:00:39 -0400 Subject: Avoid duplicate clauses when using #or Condenses the clauses that are common to both sides of the OR and put them outside, before the OR This fix the current behavior where the number of conditions is exponential based on the number of times #or is used. --- .../lib/active_record/relation/where_clause.rb | 25 ++++++++++++++-------- 1 file changed, 16 insertions(+), 9 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/relation/where_clause.rb b/activerecord/lib/active_record/relation/where_clause.rb index ef2bca9155..5b68c25bdd 100644 --- a/activerecord/lib/active_record/relation/where_clause.rb +++ b/activerecord/lib/active_record/relation/where_clause.rb @@ -15,6 +15,12 @@ module ActiveRecord ) end + def -(other) + WhereClause.new( + predicates - other.predicates, + ) + end + def merge(other) WhereClause.new( predicates_unreferenced_by(other) + other.predicates, @@ -26,15 +32,16 @@ module ActiveRecord end def or(other) - if empty? - self - elsif other.empty? - other - else - WhereClause.new( - [ast.or(other.ast)], - ) - end + left = self - other + common = self - left + right = other - common + + return common if left.empty? || right.empty? + + or_clause = WhereClause.new( + [left.ast.or(right.ast)] + ) + common + or_clause end def to_h(table_name = nil) -- cgit v1.2.3 From be81b5066074fee8126144d072c6132b93d1fe39 Mon Sep 17 00:00:00 2001 From: Maxime Lapointe Date: Fri, 28 Jul 2017 17:53:50 -0400 Subject: Edits following the reviews --- activerecord/lib/active_record/relation/where_clause.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/relation/where_clause.rb b/activerecord/lib/active_record/relation/where_clause.rb index 5b68c25bdd..752bb38481 100644 --- a/activerecord/lib/active_record/relation/where_clause.rb +++ b/activerecord/lib/active_record/relation/where_clause.rb @@ -36,12 +36,14 @@ module ActiveRecord common = self - left right = other - common - return common if left.empty? || right.empty? - - or_clause = WhereClause.new( - [left.ast.or(right.ast)] - ) - common + or_clause + if left.empty? || right.empty? + common + else + or_clause = WhereClause.new( + [left.ast.or(right.ast)], + ) + common + or_clause + end end def to_h(table_name = nil) -- cgit v1.2.3