You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by mo...@apache.org on 2021/09/07 03:52:49 UTC
[incubator-doris] branch master updated: [Enhance] Add a rewrite
rule of compoundPredicate 'OR' 'AND' to hit prefix index (#6518)
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 2324e28 [Enhance] Add a rewrite rule of compoundPredicate 'OR' 'AND' to hit prefix index (#6518)
2324e28 is described below
commit 2324e282885f35595903b447cfb2b358a59f2d76
Author: zhoubintao <35...@users.noreply.github.com>
AuthorDate: Tue Sep 7 11:52:41 2021 +0800
[Enhance] Add a rewrite rule of compoundPredicate 'OR' 'AND' to hit prefix index (#6518)
add a rewrite rule of compoundPredicate 'OR' 'AND' to hit prefix index
unit test added.
`````
case true AND expr ==> expr
case expr AND true ==> expr
case false Or expr ==> expr
case expr Or false ==> expr
case false AND expr ==> false
case expr AND false ==> false
case true Or expr ==> true
case expr Or true ==> true
`````
---
.../java/org/apache/doris/analysis/Analyzer.java | 2 +
.../doris/rewrite/CompoundPredicateWriteRule.java | 89 ++++++++++++++++++++++
.../org/apache/doris/planner/QueryPlanTest.java | 62 +++++++++++++--
3 files changed, 147 insertions(+), 6 deletions(-)
diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/Analyzer.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/Analyzer.java
index 2a1fa33..23a8268 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/Analyzer.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/Analyzer.java
@@ -36,6 +36,7 @@ import org.apache.doris.planner.PlanNode;
import org.apache.doris.planner.RuntimeFilter;
import org.apache.doris.qe.ConnectContext;
import org.apache.doris.rewrite.BetweenToCompoundRule;
+import org.apache.doris.rewrite.CompoundPredicateWriteRule;
import org.apache.doris.rewrite.ExprRewriteRule;
import org.apache.doris.rewrite.ExprRewriter;
import org.apache.doris.rewrite.ExtractCommonFactorsRule;
@@ -268,6 +269,7 @@ public class Analyzer {
rules.add(NormalizeBinaryPredicatesRule.INSTANCE);
rules.add(FoldConstantsRule.INSTANCE);
rules.add(RewriteFromUnixTimeRule.INSTANCE);
+ rules.add(CompoundPredicateWriteRule.INSTANCE);
rules.add(SimplifyInvalidDateBinaryPredicatesDateRule.INSTANCE);
rules.add(RewriteEncryptKeyRule.INSTANCE);
rules.add(RewriteAliasFunctionRule.INSTANCE);
diff --git a/fe/fe-core/src/main/java/org/apache/doris/rewrite/CompoundPredicateWriteRule.java b/fe/fe-core/src/main/java/org/apache/doris/rewrite/CompoundPredicateWriteRule.java
new file mode 100644
index 0000000..4d604d4
--- /dev/null
+++ b/fe/fe-core/src/main/java/org/apache/doris/rewrite/CompoundPredicateWriteRule.java
@@ -0,0 +1,89 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.rewrite;
+
+import org.apache.doris.analysis.Analyzer;
+import org.apache.doris.analysis.BoolLiteral;
+import org.apache.doris.analysis.CompoundPredicate;
+import org.apache.doris.analysis.Expr;
+import org.apache.doris.common.AnalysisException;
+
+import java.util.List;
+
+/**
+ * Add rewrite CompoundPredicates 'OR' 'AND' rule
+ * 'OR' 'AND' rewrite rule
+ * case true and expr ==> expr
+ * case expr and true ==> expr
+ * case false or expr ==> expr
+ * case expr or false ==> expr
+ *
+ * case false and expr ==> false
+ * case expr and false ==> false
+ * case true or expr ==> true
+ * case expr or true ==> true
+ */
+
+public class CompoundPredicateWriteRule implements ExprRewriteRule {
+
+ public static ExprRewriteRule INSTANCE = new CompoundPredicateWriteRule();
+
+ @Override
+ public Expr apply(Expr expr, Analyzer analyzer) throws AnalysisException {
+
+ if (!(expr instanceof CompoundPredicate)) return expr;
+ CompoundPredicate cp = (CompoundPredicate) expr;
+
+ List<Expr> children = cp.getChildren();
+ if (children.size() != 2) {
+ return expr;
+ }
+ Expr leftChild = cp.getChild(0);
+ Expr rightChild = cp.getChild(1);
+
+ boolean and = (cp.getOp() == CompoundPredicate.Operator.AND);
+ boolean or = (cp.getOp() == CompoundPredicate.Operator.OR);
+
+ boolean leftChildTrue = (leftChild instanceof BoolLiteral) && (((BoolLiteral) leftChild).getValue());
+ boolean leftChildFalse = (leftChild instanceof BoolLiteral) && (!((BoolLiteral) leftChild).getValue());
+
+ boolean rightChildTrue = (rightChild instanceof BoolLiteral) && (((BoolLiteral) rightChild).getValue());
+ boolean rightChildFalse = (rightChild instanceof BoolLiteral) && (!((BoolLiteral) rightChild).getValue());
+
+ // case true and expr ==> expr
+ if (leftChildTrue && and) return rightChild;
+ // case expr and true ==> expr
+ if (and && rightChildTrue) return leftChild;
+ // case false or expr ==> expr
+ if (leftChildFalse && or) return rightChild;
+ // case expr or false ==> expr
+ if (or && rightChildFalse) return leftChild;
+
+ // case false and expr ==> false
+ if (leftChildFalse && and) return new BoolLiteral(false);
+ // case expr and false ==> false
+ if (and && rightChildFalse) return new BoolLiteral(false);
+ // case true or expr ==> true
+ if (leftChildTrue && or) return new BoolLiteral(true);
+ // case expr or true ==> true
+ if (or && rightChildTrue) return new BoolLiteral(true);
+
+ // other case ,return origin expr
+ return expr;
+ }
+}
diff --git a/fe/fe-core/src/test/java/org/apache/doris/planner/QueryPlanTest.java b/fe/fe-core/src/test/java/org/apache/doris/planner/QueryPlanTest.java
index b1a1a14..1f279b1 100644
--- a/fe/fe-core/src/test/java/org/apache/doris/planner/QueryPlanTest.java
+++ b/fe/fe-core/src/test/java/org/apache/doris/planner/QueryPlanTest.java
@@ -52,8 +52,6 @@ import java.io.File;
import java.util.List;
import java.util.UUID;
-import org.apache.log4j.LogManager;
-import org.apache.log4j.Logger;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
@@ -63,7 +61,6 @@ public class QueryPlanTest {
// use a unique dir so that it won't be conflict with other unit test which
// may also start a Mocked Frontend
private static String runningDir = "fe/mocked/QueryPlanTest/" + UUID.randomUUID().toString() + "/";
- private static final Logger LOG = LogManager.getLogger(QueryPlanTest.class);
private static ConnectContext connectContext;
@@ -1715,13 +1712,66 @@ public class QueryPlanTest {
connectContext.setDatabase("default_cluster:test");
String sql = "select * from tbl_null_column_view where add_column is not null;";
String explainString1 = UtFrameUtils.getSQLPlanOrErrorMsg(connectContext, "EXPLAIN " + sql);
- LOG.info(explainString1);
Assert.assertTrue(explainString1.contains("EMPTYSET"));
String sql2 = "select * from tbl_null_column_view where add_column is not null order by query_id;";
String explainString2 = UtFrameUtils.getSQLPlanOrErrorMsg(connectContext, "EXPLAIN " + sql2);
-
- LOG.info(explainString2);
Assert.assertTrue(explainString2.contains("EMPTYSET"));
}
+
+ @Test
+ public void testCompoundPredicateWriteRule() throws Exception {
+ connectContext.setDatabase("default_cluster:test");
+
+ // false or e ==> e
+ String sql1 = "select * from test.test1 where 2=-2 OR query_time=0;";
+ String explainString1 = UtFrameUtils.getSQLPlanOrErrorMsg(connectContext, "EXPLAIN " + sql1);
+ Assert.assertTrue(explainString1.contains("PREDICATES: `query_time` = 0"));
+
+ //true or e ==> true
+ String sql2 = "select * from test.test1 where -5=-5 OR query_time=0;";
+ String explainString2 = UtFrameUtils.getSQLPlanOrErrorMsg(connectContext, "EXPLAIN " + sql2);
+ Assert.assertTrue(!explainString2.contains("OR"));
+
+ //e or true ==> true
+ String sql3 = "select * from test.test1 where query_time=0 OR -5=-5;";
+ String explainString3 = UtFrameUtils.getSQLPlanOrErrorMsg(connectContext, "EXPLAIN " + sql3);
+ Assert.assertTrue(!explainString3.contains("OR"));
+
+ //e or false ==> e
+ String sql4 = "select * from test.test1 where -5!=-5 OR query_time=0;";
+ String explainString4 = UtFrameUtils.getSQLPlanOrErrorMsg(connectContext, "EXPLAIN " + sql4);
+ Assert.assertTrue(explainString4.contains("PREDICATES: `query_time` = 0"));
+
+
+ // true and e ==> e
+ String sql5 = "select * from test.test1 where -5=-5 AND query_time=0;";
+ String explainString5 = UtFrameUtils.getSQLPlanOrErrorMsg(connectContext, "EXPLAIN " + sql5);
+ Assert.assertTrue(explainString5.contains("PREDICATES: `query_time` = 0"));
+
+ // e and true ==> e
+ String sql6 = "select * from test.test1 where query_time=0 AND -5=-5;";
+ String explainString6 = UtFrameUtils.getSQLPlanOrErrorMsg(connectContext, "EXPLAIN " + sql6);
+ Assert.assertTrue(explainString6.contains("PREDICATES: `query_time` = 0"));
+
+ // false and e ==> false
+ String sql7 = "select * from test.test1 where -5!=-5 AND query_time=0;";
+ String explainString7 = UtFrameUtils.getSQLPlanOrErrorMsg(connectContext, "EXPLAIN " + sql7);
+ Assert.assertTrue(!explainString7.contains("FALSE"));
+
+ // e and false ==> false
+ String sql8 = "select * from test.test1 where query_time=0 AND -5!=-5;";
+ String explainString8 = UtFrameUtils.getSQLPlanOrErrorMsg(connectContext, "EXPLAIN " + sql8);
+ Assert.assertTrue(!explainString8.contains("FALSE"));
+
+ // (false or expr1) and (false or expr2) ==> expr1 and expr2
+ String sql9 = "select * from test.test1 where (-2=2 or query_time=2) and (-2=2 or stmt_id=2);";
+ String explainString9 = UtFrameUtils.getSQLPlanOrErrorMsg(connectContext, "EXPLAIN " + sql9);
+ Assert.assertTrue(explainString9.contains("PREDICATES: `query_time` = 2, `stmt_id` = 2"));
+
+ // false or (expr and true) ==> expr
+ String sql10 = "select * from test.test1 where (2=-2) OR (query_time=0 AND 1=1);";
+ String explainString10 = UtFrameUtils.getSQLPlanOrErrorMsg(connectContext, "EXPLAIN " + sql10);
+ Assert.assertTrue(explainString10.contains("PREDICATES: `query_time` = 0"));
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org