This is an automated email from the ASF dual-hosted git repository.

morningman pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-doris.git


The following commit(s) were added to refs/heads/master by this push:
     new 7a83c56  [Bug] fix OrCompoundPredicate predicate fold bug #3596
7a83c56 is described below

commit 7a83c5662d2cf7c7ff6e9fbecab56b05b00b4238
Author: Mingyu Chen <[email protected]>
AuthorDate: Mon May 18 14:46:34 2020 +0800

    [Bug] fix OrCompoundPredicate predicate fold bug #3596
    
    Fix: #3596
    
    NOTICE(#3622):
    This is a "revert of revert pull request".
    This pr is mainly used to synthesize the PRs whose commits were
    scattered and submitted due to the wrong merge method into a complete 
single commit.
---
 .../java/org/apache/doris/analysis/SelectStmt.java | 33 +++++++++++++++++-----
 .../org/apache/doris/planner/QueryPlanTest.java    | 15 ++++++++++
 2 files changed, 41 insertions(+), 7 deletions(-)

diff --git a/fe/src/main/java/org/apache/doris/analysis/SelectStmt.java 
b/fe/src/main/java/org/apache/doris/analysis/SelectStmt.java
index f705651..122c5e5 100644
--- a/fe/src/main/java/org/apache/doris/analysis/SelectStmt.java
+++ b/fe/src/main/java/org/apache/doris/analysis/SelectStmt.java
@@ -618,17 +618,36 @@ public class SelectStmt extends QueryStmt {
             temp.add(makeCompound(cloneExprs, CompoundPredicate.Operator.AND));
         }
 
+        Expr result;
+        boolean isReturnCommonFactorExpr = false;
         for (List<Expr> exprList : clearExprs) {
             exprList.removeAll(cloneExprs);
+            if (exprList.size() == 0) {
+                // For example, the sql is "where (a = 1) or (a = 1 and B = 2)"
+                // if "(a = 1)" is extracted as a common factor expression, 
then the first expression "(a = 1)" has no expression
+                // other than a common factor expression, and the second 
expression "(a = 1 and B = 2)" has an expression of "(B = 2)"
+                //
+                // In this case, the common factor expression ("a = 1") can be 
directly used to replace the whole CompoundOrPredicate.
+                // In Fact, the common factor expression is actually the 
parent set of expression "(a = 1)" and expression "(a = 1 and B = 2)"
+                //
+                // exprList.size() == 0 means one child of CompoundOrPredicate 
has no expression other than a common factor expression.
+                isReturnCommonFactorExpr = true;
+                break;
+            }
             temp.add(makeCompound(exprList, CompoundPredicate.Operator.AND));
         }
-
-        // rebuild CompoundPredicate if found duplicate predicate will build 
(predcate) and (.. or ..)  predicate in
-        // step 1: will build (.. or ..)
-        Expr result = CollectionUtils.isNotEmpty(cloneExprs) ? new 
CompoundPredicate(CompoundPredicate.Operator.AND,
-                temp.get(0), makeCompound(temp.subList(1, temp.size()), 
CompoundPredicate.Operator.OR))
-                : makeCompound(temp, CompoundPredicate.Operator.OR);
-        LOG.debug("rewrite ors: " + result.toSql());
+        if (isReturnCommonFactorExpr) {
+            result = temp.get(0);
+        } else {
+            // rebuild CompoundPredicate if found duplicate predicate will 
build (predicate) and (.. or ..)  predicate in
+            // step 1: will build (.. or ..)
+            result = CollectionUtils.isNotEmpty(cloneExprs) ? new 
CompoundPredicate(CompoundPredicate.Operator.AND,
+                    temp.get(0), makeCompound(temp.subList(1, temp.size()), 
CompoundPredicate.Operator.OR))
+                    : makeCompound(temp, CompoundPredicate.Operator.OR);
+        }
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("rewrite ors: " + result.toSql());
+        }
         return result;
     }
 
diff --git a/fe/src/test/java/org/apache/doris/planner/QueryPlanTest.java 
b/fe/src/test/java/org/apache/doris/planner/QueryPlanTest.java
index 0398e8a..371c38d 100644
--- a/fe/src/test/java/org/apache/doris/planner/QueryPlanTest.java
+++ b/fe/src/test/java/org/apache/doris/planner/QueryPlanTest.java
@@ -835,4 +835,19 @@ public class QueryPlanTest {
         FeConstants.runningUnitTest = false;
         Assert.assertTrue(explainString.contains("partitions=1/1"));
     }
+
+    @Test
+    public void testOrCompoundPredicateFold() throws Exception {
+        String queryStr = "explain select * from  baseall where (k1 > 1) or 
(k1 > 1 and k2 < 1)";
+        String explainString = 
UtFrameUtils.getSQLPlanOrErrorMsg(connectContext, queryStr);
+        Assert.assertTrue(explainString.contains("PREDICATES: (`k1` > 1)\n"));
+
+        queryStr = "explain select * from  baseall where (k1 > 1 and k2 < 1) 
or  (k1 > 1)";
+        explainString = UtFrameUtils.getSQLPlanOrErrorMsg(connectContext, 
queryStr);
+        Assert.assertTrue(explainString.contains("PREDICATES: `k1` > 1\n"));
+
+        queryStr = "explain select * from  baseall where (k1 > 1) or (k1 > 1)";
+        explainString = UtFrameUtils.getSQLPlanOrErrorMsg(connectContext, 
queryStr);
+        Assert.assertTrue(explainString.contains("PREDICATES: (`k1` > 1)\n"));
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to