Browse Source

Merge pull request #9444 from ghalliday/issue16798

HPCC-16798 Simplify guard condition of (x || (x && y)) -> x

Reviewed By: Shamser Ahmed <shamser.ahmed@lexisnexis.co.uk>
Reviewed-By: Richard Chapman <rchapman@hpccsystems.com>
Richard Chapman 8 years ago
parent
commit
8d3530350e
1 changed files with 19 additions and 6 deletions
  1. 19 6
      ecl/hqlcpp/hqlhoist.cpp

+ 19 - 6
ecl/hqlcpp/hqlhoist.cpp

@@ -102,6 +102,16 @@ void CHqlExprMultiGuard::combine(CHqlExprMultiGuard & other)
     }
 }
 
+
+static IHqlExpression * querySubset(IHqlExpression * newGuard, IHqlExpression * oldGuard)
+{
+    if (newGuard == oldGuard)
+        return newGuard;
+    if (newGuard->getOperator() == no_and)
+        return querySubset(newGuard->queryChild(0), oldGuard);
+    return nullptr;
+}
+
 void CHqlExprMultiGuard::combine(CHqlExprGuard & other)
 {
     ForEachItemIn(i, guarded)
@@ -109,12 +119,15 @@ void CHqlExprMultiGuard::combine(CHqlExprGuard & other)
         CHqlExprGuard & cur = guarded.item(i);
         if (cur.original == other.original)
         {
-            //condition is now (a || b)
-            OwnedHqlExpr newCond;
-            if (matchesBoolean(cur.guard, true) || matchesBoolean(other.guard, true))
-                newCond.set(queryBoolExpr(true));
-            else
-                newCond.setown(createBoolExpr(no_or, LINK(cur.guard), LINK(other.guard)));
+            LinkedHqlExpr newCond = querySubset(other.guard, cur.guard);
+            if (!newCond)
+            {
+                //condition is now (a || b)
+                if (matchesBoolean(cur.guard, true) || matchesBoolean(other.guard, true))
+                    newCond.set(queryBoolExpr(true));
+                else
+                    newCond.setown(createBoolExpr(no_or, LINK(cur.guard), LINK(other.guard)));
+            }
 
             //MORE: Could sometimes reuse the existing guard if this was created by this node
             Owned<CHqlExprGuard> newGuard = new CHqlExprGuard(newCond, cur.original, cur.guardContainsCandidate||other.guardContainsCandidate);