This is an automated email from the ASF dual-hosted git repository. morningman pushed a commit to branch revert-3609-revert-3597-compond_expr in repository https://gitbox.apache.org/repos/asf/incubator-doris.git
commit 6bcc323b569c4c73a3f716f17b04e1f8faee7b29 Author: Mingyu Chen <[email protected]> AuthorDate: Mon May 18 13:24:09 2020 +0800 Revert "Revert "[Bug] fix OrCompoundPredicate predicate fold bug #3596" (#3609)" This reverts commit 20f20239f2f561867a1e41d6845673ea0b84c229. --- .../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]
