This is an automated email from the ASF dual-hosted git repository.
huajianlan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/master by this push:
new 6da6ffb836 [feature] (Nereids) add rewrite rule to merge consecutive
filter nodes (#11248)
6da6ffb836 is described below
commit 6da6ffb836426743e5f6fad8d72e9334c40b785c
Author: minghong <[email protected]>
AuthorDate: Mon Aug 1 17:29:15 2022 +0800
[feature] (Nereids) add rewrite rule to merge consecutive filter nodes
(#11248)
this rule aims to merge consecutive filters.
For example:
logical plan tree:
```
project
|
filter(a>0)
|
filter(b>0)
|
scan
```
transformed to:
```
project
|
filter(a>0 and b>0)
|
scan
```
---
.../org/apache/doris/nereids/NereidsPlanner.java | 1 -
.../org/apache/doris/nereids/rules/RuleType.java | 3 +-
.../rewrite/logical/MergeConsecutiveFilters.java | 58 +++++++++++++++++++
.../logical/MergeConsecutiveFilterTest.java | 65 ++++++++++++++++++++++
4 files changed, 124 insertions(+), 3 deletions(-)
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/nereids/NereidsPlanner.java
b/fe/fe-core/src/main/java/org/apache/doris/nereids/NereidsPlanner.java
index 57f4e267ee..0da54c06cd 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/nereids/NereidsPlanner.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/NereidsPlanner.java
@@ -112,7 +112,6 @@ public class NereidsPlanner extends Planner {
deriveStats();
}
optimize();
-
// Get plan directly. Just for SSB.
return getRoot().extractPlan();
}
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleType.java
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleType.java
index fa6df6d5f7..e08270f08c 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleType.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleType.java
@@ -54,9 +54,8 @@ public enum RuleType {
REWRITE_AGG_EXPRESSION(RuleTypeClass.REWRITE),
REWRITE_FILTER_EXPRESSION(RuleTypeClass.REWRITE),
REWRITE_JOIN_EXPRESSION(RuleTypeClass.REWRITE),
-
REORDER_JOIN(RuleTypeClass.REWRITE),
-
+ MERGE_CONSECUTIVE_FILTERS(RuleTypeClass.REWRITE),
REWRITE_SENTINEL(RuleTypeClass.REWRITE),
// exploration rules
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/MergeConsecutiveFilters.java
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/MergeConsecutiveFilters.java
new file mode 100644
index 0000000000..4398f149cc
--- /dev/null
+++
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/MergeConsecutiveFilters.java
@@ -0,0 +1,58 @@
+// 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.nereids.rules.rewrite.logical;
+
+import org.apache.doris.nereids.rules.Rule;
+import org.apache.doris.nereids.rules.RuleType;
+import org.apache.doris.nereids.rules.rewrite.OneRewriteRuleFactory;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.plans.logical.LogicalFilter;
+import org.apache.doris.nereids.util.ExpressionUtils;
+
+/**
+ * this rule aims to merge consecutive filters.
+ * For example:
+ * logical plan tree:
+ * project
+ * |
+ * filter(a>0)
+ * |
+ * filter(b>0)
+ * |
+ * scan
+ * transformed to:
+ * project
+ * |
+ * filter(a>0 and b>0)
+ * |
+ * scan
+ */
+public class MergeConsecutiveFilters extends OneRewriteRuleFactory {
+ @Override
+ public Rule build() {
+ return logicalFilter(logicalFilter()).then(filter -> {
+ LogicalFilter<?> childFilter = filter.child();
+ Expression predicates = filter.getPredicates();
+ Expression childPredicates = childFilter.getPredicates();
+ Expression mergedPredicates = ExpressionUtils.and(predicates,
childPredicates);
+ LogicalFilter mergedFilter = new LogicalFilter(mergedPredicates,
childFilter.child());
+ return mergedFilter;
+ }).toRule(RuleType.MERGE_CONSECUTIVE_FILTERS);
+ }
+
+}
diff --git
a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/logical/MergeConsecutiveFilterTest.java
b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/logical/MergeConsecutiveFilterTest.java
new file mode 100644
index 0000000000..4767266c97
--- /dev/null
+++
b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/logical/MergeConsecutiveFilterTest.java
@@ -0,0 +1,65 @@
+// 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.nereids.rules.rewrite.logical;
+
+import org.apache.doris.nereids.PlannerContext;
+import org.apache.doris.nereids.analyzer.UnboundRelation;
+import org.apache.doris.nereids.memo.Memo;
+import org.apache.doris.nereids.rules.Rule;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.IntegerLiteral;
+import org.apache.doris.nereids.trees.plans.Plan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalFilter;
+import org.apache.doris.nereids.util.ExpressionUtils;
+import org.apache.doris.qe.ConnectContext;
+
+import com.google.common.collect.Lists;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+
+/**
+ * MergeConsecutiveFilter ut
+ */
+public class MergeConsecutiveFilterTest {
+ @Test
+ public void testMergeConsecutiveFilters() {
+ UnboundRelation relation = new
UnboundRelation(Lists.newArrayList("db", "table"));
+ Expression expression1 = new IntegerLiteral(1);
+ LogicalFilter filter1 = new LogicalFilter(expression1, relation);
+ Expression expression2 = new IntegerLiteral(2);
+ LogicalFilter filter2 = new LogicalFilter(expression2, filter1);
+ Expression expression3 = new IntegerLiteral(3);
+ LogicalFilter filter3 = new LogicalFilter(expression3, filter2);
+
+ PlannerContext plannerContext = new Memo(filter3)
+ .newPlannerContext(new ConnectContext())
+ .setDefaultJobContext();
+ List<Rule> rules = Lists.newArrayList(new
MergeConsecutiveFilters().build());
+ plannerContext.bottomUpRewrite(rules);
+ //check transformed plan
+ Plan resultPlan = plannerContext.getMemo().copyOut();
+ System.out.println(resultPlan.treeString());
+ Assertions.assertTrue(resultPlan instanceof LogicalFilter);
+ Expression allPredicates = ExpressionUtils.and(expression3,
+ ExpressionUtils.and(expression2, expression1));
+ Assertions.assertTrue(((LogicalFilter<?>)
resultPlan).getPredicates().equals(allPredicates));
+ Assertions.assertTrue(resultPlan.child(0) instanceof UnboundRelation);
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]