You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by GitBox <gi...@apache.org> on 2022/07/27 04:10:19 UTC

[GitHub] [doris] morrySnow commented on a diff in pull request #11162: [feature] (Nereids) add rule to push down predicate through aggregate

morrySnow commented on code in PR #11162:
URL: https://github.com/apache/doris/pull/11162#discussion_r930601833


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/PushPredicateThroughAggregation.java:
##########
@@ -0,0 +1,110 @@
+// 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.expressions.Slot;
+import org.apache.doris.nereids.trees.expressions.visitor.SlotExtractor;
+import org.apache.doris.nereids.trees.plans.GroupPlan;
+import org.apache.doris.nereids.trees.plans.Plan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate;
+import org.apache.doris.nereids.trees.plans.logical.LogicalFilter;
+import org.apache.doris.nereids.util.ExpressionUtils;
+
+import com.google.common.collect.Lists;
+
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+
+/**
+ * Push the predicate in the LogicalFilter to the aggregate child.
+ * For example:
+ * Logical plan tree:
+ *                 any_node
+ *                   |
+ *                filter (a>0 and b>0)
+ *                   |
+ *                group by(a, c)
+ *                   |
+ *                 scan
+ * transformed to:
+ *                 project
+ *                   |
+ *              upper filter (b>0)
+ *                   |
+ *                group by(a, c)
+ *                   |
+ *              bottom filter (a>0)
+ *                   |
+ *                 scan
+ * Note:
+ *    'a>0' could be push down, because 'a' is in group by keys;
+ *    but 'b>0' could not push down, because 'b' is not in group by keys.
+ *
+ */
+
+public class PushPredicateThroughAggregation extends OneRewriteRuleFactory {
+
+    @Override
+    public Rule build() {
+        return logicalFilter(logicalAggregate()).then(filter -> {
+            LogicalAggregate<GroupPlan> aggregate = filter.child();
+            Set<Slot> groupBySlots = new HashSet<>();
+            for (Expression groupByExpression : aggregate.getGroupByExpressionList()) {
+                if (groupByExpression instanceof Slot) {

Review Comment:
   i do not understand this example. why could do filter(a > 2) after group by f(a), since a is neither key nor aggregate function output.
   If u mean
   ```
   Filter(f(a) > 2)
   |--Aggregate([sum(b)], [f(a)])
   ```
   then,  i think the filter should could be pushed down aggregate node.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org