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/01 09:46:19 UTC

[GitHub] [doris] EmmyMiao87 commented on a diff in pull request #10478: [Enhancement](Nereids) prune column for filter/agg/join/sort

EmmyMiao87 commented on code in PR #10478:
URL: https://github.com/apache/doris/pull/10478#discussion_r911771172


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleType.java:
##########
@@ -39,6 +39,13 @@ public enum RuleType {
 
     // rewrite rules
     COLUMN_PRUNE_PROJECTION(RuleTypeClass.REWRITE),
+    COLUMN_PRUNE_AGGREGATION(RuleTypeClass.REWRITE),

Review Comment:
   Where is `COLUMN_PRUNE_AGGREGATION`  rule?



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleType.java:
##########
@@ -39,6 +39,13 @@ public enum RuleType {
 
     // rewrite rules
     COLUMN_PRUNE_PROJECTION(RuleTypeClass.REWRITE),
+    COLUMN_PRUNE_AGGREGATION(RuleTypeClass.REWRITE),
+    COLUMN_PRUNE_AGGREGATION_CHILD(RuleTypeClass.REWRITE),

Review Comment:
   Please unify the naming rules. There appear to be three `xxx`, `xxx_child`, `xxx_children`



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/AbstractPushDownProjectRule.java:
##########
@@ -0,0 +1,61 @@
+// 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.pattern.PatternDescriptor;
+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.SlotExtractor;
+import org.apache.doris.nereids.trees.plans.Plan;
+
+import com.google.common.collect.Lists;
+
+import java.util.List;
+import java.util.Set;
+
+/**
+ * push down project base class.
+ */
+public abstract class AbstractPushDownProjectRule<C extends Plan> extends OneRewriteRuleFactory {
+
+    PatternDescriptor<C, Plan> target;
+    RuleType ruleType;
+
+    @Override
+    public Rule<Plan> build() {
+        return logicalProject(target).then(project -> {
+            List<Expression> projects = Lists.newArrayList();
+            projects.addAll(project.getOutput());
+            Set<Slot> projectSlots = SlotExtractor.extractSlot(projects);

Review Comment:
   The return type of `LogicalPlan.getOutput` is directly List<Slot>.
   No extraction process is needed anymore.



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/PruneAggChildColumns.java:
##########
@@ -0,0 +1,68 @@
+// 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.operators.plans.logical.LogicalProject;
+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.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.expressions.SlotExtractor;
+import org.apache.doris.nereids.trees.plans.Plan;
+
+import com.google.common.collect.Lists;
+
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * prune its child output according to agg.
+ * pattern: agg()
+ * table a: k1,k2,k3,v1
+ * select k1,sum(v1) from a group by k1
+ * plan tree:
+ *    agg
+ *     |
+ *    scan(k1,k2,k3,v1)
+ * transformed:
+ *    agg
+ *     |
+ *   project(k1,v1)
+ *     |
+ *    scan(k1,k2,k3,v1)
+ */
+public class PruneAggChildColumns extends OneRewriteRuleFactory {
+
+    @Override
+    public Rule<Plan> build() {
+        return RuleType.COLUMN_PRUNE_AGGREGATION_CHILD.build(logicalAggregate().then(agg -> {
+            List<Expression> slots = Lists.newArrayList();
+            slots.addAll(agg.operator.getExpressions());
+            Set<Slot> outputs = SlotExtractor.extractSlot(slots);
+            List<NamedExpression> prunedOutputs = agg.child().getOutput().stream().filter(outputs::contains)

Review Comment:
   The output slot of agg is calculated from the output slot of the child. Simply passing the contains function does not determine which child's output slot the output slot is calculated from.
   For example select k1, sum(k2+k3) as sum from table
   Then the output slot of agg is k1, sum
   And the output slot of child is k1,k2,k3



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleType.java:
##########
@@ -39,6 +39,13 @@ public enum RuleType {
 
     // rewrite rules

Review Comment:
   Add comment "// column prune rules"



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/PruneSortChildColumns.java:
##########
@@ -0,0 +1,55 @@
+// 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.operators.plans.logical.LogicalProject;
+import org.apache.doris.nereids.operators.plans.logical.LogicalSort;
+import org.apache.doris.nereids.rules.RuleType;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.expressions.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.LogicalUnaryPlan;
+
+import com.google.common.collect.Lists;
+
+import java.util.Set;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+/**
+ * prune join children output.
+ * pattern: project(sort())
+ */
+public class PruneSortChildColumns extends AbstractPushDownProjectRule<LogicalUnaryPlan<LogicalSort, GroupPlan>> {
+
+    public PruneSortChildColumns() {
+        setRuleType(RuleType.COLUMN_PRUNE_SORT);
+        setTarget(logicalSort());
+    }
+
+    @Override
+    protected Plan pushDownProject(LogicalUnaryPlan<LogicalSort, GroupPlan> sortPlan, Set<Slot> references) {
+        Set<Slot> sortSlots = SlotExtractor.extractSlot(sortPlan.operator.getExpressions());
+        Set<Slot> required = Stream.concat(references.stream(), sortSlots.stream()).collect(Collectors.toSet());
+        if (required.containsAll(sortPlan.child().getOutput())) {

Review Comment:
   Same problem with agg rule



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/PruneAggChildColumns.java:
##########
@@ -0,0 +1,68 @@
+// 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.operators.plans.logical.LogicalProject;
+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.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.expressions.SlotExtractor;
+import org.apache.doris.nereids.trees.plans.Plan;
+
+import com.google.common.collect.Lists;
+
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * prune its child output according to agg.
+ * pattern: agg()
+ * table a: k1,k2,k3,v1
+ * select k1,sum(v1) from a group by k1
+ * plan tree:
+ *    agg

Review Comment:
   Why is the pattern of agg different from other operators?
   Agg may also be a project-> agg structure, such as
   ```select k1 from tbl1 group by k1,k2;```



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/PruneJoinChildrenColumns.java:
##########
@@ -0,0 +1,87 @@
+// 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.operators.plans.logical.LogicalJoin;
+import org.apache.doris.nereids.operators.plans.logical.LogicalProject;
+import org.apache.doris.nereids.rules.RuleType;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.expressions.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.LogicalBinaryPlan;
+
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * prune join children output.
+ * pattern: project(join())
+ * table a: k1,k2,k3,v1
+ * table b: k1,k2,v1,v2
+ * select a.k1,b.k2 from a join b on a.k1 = b.k1 where a.k3 > 1
+ * plan tree:
+ * project(a.k1,b.k2)
+ *        |
+ *      join(k1,k2,k3,v1,k1,k2,v1,v2)
+ *    /       \
+ * scan(a) scan(b)
+ * transformed:
+ *   project(a.k1,b.k2)
+ *      |
+ *    join(k1,k2,k3,v1,k1,k2,v1,v2)
+ *    /                     \
+ *  project(a.k1,a.k3)   project(b.k2,b.k1)
+ *    |                     |
+ *    scan                scan
+ */
+public class PruneJoinChildrenColumns
+        extends AbstractPushDownProjectRule<LogicalBinaryPlan<LogicalJoin, GroupPlan, GroupPlan>> {
+
+    public PruneJoinChildrenColumns() {
+        setRuleType(RuleType.COLUMN_PRUNE_JOIN_CHILDREN);
+        setTarget(logicalJoin());
+    }
+
+    @Override
+    protected Plan pushDownProject(LogicalBinaryPlan<LogicalJoin, GroupPlan, GroupPlan> joinPlan,
+            Set<Slot> references) {
+        if (joinPlan.operator.getCondition().isPresent()) {
+            references.addAll(SlotExtractor.extractSlot(joinPlan.operator.getCondition().get()));
+        }
+
+        List<NamedExpression> leftInputs = joinPlan.left().getOutput().stream().filter(references::contains)

Review Comment:
   Same problem with agg rule



-- 
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