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/04 06:56:23 UTC

[GitHub] [doris] EmmyMiao87 commented on a diff in pull request #10412: [Feature] [nereids] Agg rewrite rule of nereids optmizer

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


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/logical/LogicalAggregate.java:
##########
@@ -56,6 +57,16 @@ public LogicalAggregate(List<Expression> groupByExprList, List<NamedExpression>
         super(OperatorType.LOGICAL_AGGREGATION);
         this.groupByExprList = groupByExprList;
         this.outputExpressionList = outputExpressionList;
+        this.disassembled = false;

Review Comment:
   No need this line. The default value of disassembled is false



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/logical/LogicalAggregate.java:
##########
@@ -43,6 +43,7 @@
  */
 public class LogicalAggregate extends LogicalUnaryOperator {
 
+    private final boolean disassembled;

Review Comment:
   Maybe it would be more appropriate to change the name to two-stage



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleType.java:
##########
@@ -38,6 +38,7 @@ public enum RuleType {
     PROJECT_TO_GLOBAL_AGGREGATE(RuleTypeClass.REWRITE),
 
     // rewrite rules
+    AGGREGATE_DISASSEMBLE(RuleTypeClass.REWRITE),

Review Comment:
   It seems that it is now split into two-stage aggregation by default? Should a todo be added, indicating that whether to apply the rule or not will be determined according to the cost situation in the future.



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/logical/LogicalAggregate.java:
##########
@@ -56,6 +57,16 @@ public LogicalAggregate(List<Expression> groupByExprList, List<NamedExpression>
         super(OperatorType.LOGICAL_AGGREGATION);
         this.groupByExprList = groupByExprList;
         this.outputExpressionList = outputExpressionList;
+        this.disassembled = false;
+    }
+
+    public LogicalAggregate(List<Expression> groupByExprList,
+            List<NamedExpression> outputExpressionList,
+            boolean disassembled) {
+        super(OperatorType.LOGICAL_AGGREGATION);
+        this.groupByExprList = groupByExprList;
+        this.outputExpressionList = outputExpressionList;
+        this.disassembled = false;

Review Comment:
   The parameter is passed in, the result is still set to FALSE directly?



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleSet.java:
##########
@@ -39,6 +40,10 @@ public class RuleSet {
             .add(new JoinLeftAssociative())
             .build();
 
+    public static final List<Rule<Plan>> REWRITE_RULES = planRuleFactories()

Review Comment:
   Is it appropriate to put aggregation and splitting at the current stage? 
   In theory almost all rewrite rules don't care if agg is one or two. 
   If the agg split is performed too early, it will affect the writing of some agg related rules.



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/AggregateDisassemble.java:
##########
@@ -0,0 +1,151 @@
+// 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;
+
+import org.apache.doris.analysis.FunctionName;
+import org.apache.doris.catalog.Catalog;
+import org.apache.doris.catalog.Function;
+import org.apache.doris.catalog.Function.CompareMode;
+import org.apache.doris.catalog.Type;
+import org.apache.doris.nereids.operators.Operator;
+import org.apache.doris.nereids.operators.plans.logical.LogicalAggregate;
+import org.apache.doris.nereids.rules.Rule;
+import org.apache.doris.nereids.rules.RuleType;
+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.functions.AggregateFunction;
+import org.apache.doris.nereids.trees.plans.Plan;
+import org.apache.doris.nereids.types.DataType;
+
+import com.clearspring.analytics.util.Lists;
+import com.google.common.base.Preconditions;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * Used to generate the merge agg node for distributed execution.
+ * Do this in following steps:
+ *  1. clone output expr list, find all agg function
+ *  2. set found agg function intermediaType
+ *  3. create new child plan rooted at new local agg
+ *  4. update the slot referenced by expr of merge agg
+ *  5. create plan rooted at merge agg, return it.
+ */
+public class AggregateDisassemble extends OneRewriteRuleFactory {
+
+    @Override
+    public Rule<Plan> build() {
+        return logicalAggregate().when(p -> {
+            LogicalAggregate logicalAggregation = p.getOperator();
+            return !logicalAggregation.isDisassembled();
+        }).thenApply(ctx -> {
+            Plan plan = ctx.root;
+            Operator operator = plan.getOperator();
+            LogicalAggregate agg = (LogicalAggregate) operator;
+            List<NamedExpression> outputExpressionList = agg.getOutputExpressionList();
+            List<NamedExpression> intermediateAggExpressionList = Lists.newArrayList();
+            for (NamedExpression namedExpression : outputExpressionList) {

Review Comment:
   Don't try to extract the agg function from the output, but read the agg function directly from the operator



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/AggregateDisassemble.java:
##########
@@ -0,0 +1,151 @@
+// 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;
+
+import org.apache.doris.analysis.FunctionName;
+import org.apache.doris.catalog.Catalog;
+import org.apache.doris.catalog.Function;
+import org.apache.doris.catalog.Function.CompareMode;
+import org.apache.doris.catalog.Type;
+import org.apache.doris.nereids.operators.Operator;
+import org.apache.doris.nereids.operators.plans.logical.LogicalAggregate;
+import org.apache.doris.nereids.rules.Rule;
+import org.apache.doris.nereids.rules.RuleType;
+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.functions.AggregateFunction;
+import org.apache.doris.nereids.trees.plans.Plan;
+import org.apache.doris.nereids.types.DataType;
+
+import com.clearspring.analytics.util.Lists;
+import com.google.common.base.Preconditions;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * Used to generate the merge agg node for distributed execution.
+ * Do this in following steps:
+ *  1. clone output expr list, find all agg function
+ *  2. set found agg function intermediaType
+ *  3. create new child plan rooted at new local agg
+ *  4. update the slot referenced by expr of merge agg
+ *  5. create plan rooted at merge agg, return it.
+ */
+public class AggregateDisassemble extends OneRewriteRuleFactory {
+
+    @Override
+    public Rule<Plan> build() {
+        return logicalAggregate().when(p -> {
+            LogicalAggregate logicalAggregation = p.getOperator();
+            return !logicalAggregation.isDisassembled();

Review Comment:
   It seems that we should use the framework mechanism of rule apply to ensure that an operator should not apply repeated rules again, rather than judging here.



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/AggregateDisassemble.java:
##########
@@ -0,0 +1,151 @@
+// 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;
+
+import org.apache.doris.analysis.FunctionName;
+import org.apache.doris.catalog.Catalog;
+import org.apache.doris.catalog.Function;
+import org.apache.doris.catalog.Function.CompareMode;
+import org.apache.doris.catalog.Type;
+import org.apache.doris.nereids.operators.Operator;
+import org.apache.doris.nereids.operators.plans.logical.LogicalAggregate;
+import org.apache.doris.nereids.rules.Rule;
+import org.apache.doris.nereids.rules.RuleType;
+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.functions.AggregateFunction;
+import org.apache.doris.nereids.trees.plans.Plan;
+import org.apache.doris.nereids.types.DataType;
+
+import com.clearspring.analytics.util.Lists;
+import com.google.common.base.Preconditions;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * Used to generate the merge agg node for distributed execution.
+ * Do this in following steps:
+ *  1. clone output expr list, find all agg function
+ *  2. set found agg function intermediaType
+ *  3. create new child plan rooted at new local agg
+ *  4. update the slot referenced by expr of merge agg
+ *  5. create plan rooted at merge agg, return it.
+ */
+public class AggregateDisassemble extends OneRewriteRuleFactory {
+
+    @Override
+    public Rule<Plan> build() {
+        return logicalAggregate().when(p -> {
+            LogicalAggregate logicalAggregation = p.getOperator();
+            return !logicalAggregation.isDisassembled();
+        }).thenApply(ctx -> {
+            Plan plan = ctx.root;
+            Operator operator = plan.getOperator();
+            LogicalAggregate agg = (LogicalAggregate) operator;
+            List<NamedExpression> outputExpressionList = agg.getOutputExpressionList();
+            List<NamedExpression> intermediateAggExpressionList = Lists.newArrayList();
+            for (NamedExpression namedExpression : outputExpressionList) {
+                namedExpression = (NamedExpression) namedExpression.clone();
+                List<AggregateFunction> functionCallList =
+                        namedExpression.collect(org.apache.doris.catalog.AggregateFunction.class::isInstance);
+                for (AggregateFunction functionCall : functionCallList) {
+                    FunctionName functionName = new FunctionName(functionCall.getName());
+                    List<Expression> expressionList = functionCall.getArguments();
+                    List<Type> staleTypeList = expressionList.stream().map(Expression::getDataType)
+                            .map(DataType::toCatalogDataType).collect(Collectors.toList());
+                    Function staleFuncDesc = new Function(functionName, staleTypeList,

Review Comment:
   Wrap these 73~83 lines of getting functions from the old framework in a function. At the same time, with TODO, the acquisition and registration of subsequent lines will be modified uniformly.



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/AggregateDisassemble.java:
##########
@@ -0,0 +1,151 @@
+// 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;
+
+import org.apache.doris.analysis.FunctionName;
+import org.apache.doris.catalog.Catalog;
+import org.apache.doris.catalog.Function;
+import org.apache.doris.catalog.Function.CompareMode;
+import org.apache.doris.catalog.Type;
+import org.apache.doris.nereids.operators.Operator;
+import org.apache.doris.nereids.operators.plans.logical.LogicalAggregate;
+import org.apache.doris.nereids.rules.Rule;
+import org.apache.doris.nereids.rules.RuleType;
+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.functions.AggregateFunction;
+import org.apache.doris.nereids.trees.plans.Plan;
+import org.apache.doris.nereids.types.DataType;
+
+import com.clearspring.analytics.util.Lists;
+import com.google.common.base.Preconditions;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * Used to generate the merge agg node for distributed execution.
+ * Do this in following steps:
+ *  1. clone output expr list, find all agg function
+ *  2. set found agg function intermediaType
+ *  3. create new child plan rooted at new local agg
+ *  4. update the slot referenced by expr of merge agg
+ *  5. create plan rooted at merge agg, return it.
+ */
+public class AggregateDisassemble extends OneRewriteRuleFactory {
+
+    @Override
+    public Rule<Plan> build() {
+        return logicalAggregate().when(p -> {
+            LogicalAggregate logicalAggregation = p.getOperator();
+            return !logicalAggregation.isDisassembled();
+        }).thenApply(ctx -> {
+            Plan plan = ctx.root;
+            Operator operator = plan.getOperator();
+            LogicalAggregate agg = (LogicalAggregate) operator;
+            List<NamedExpression> outputExpressionList = agg.getOutputExpressionList();
+            List<NamedExpression> intermediateAggExpressionList = Lists.newArrayList();
+            for (NamedExpression namedExpression : outputExpressionList) {
+                namedExpression = (NamedExpression) namedExpression.clone();
+                List<AggregateFunction> functionCallList =
+                        namedExpression.collect(org.apache.doris.catalog.AggregateFunction.class::isInstance);
+                for (AggregateFunction functionCall : functionCallList) {
+                    FunctionName functionName = new FunctionName(functionCall.getName());
+                    List<Expression> expressionList = functionCall.getArguments();
+                    List<Type> staleTypeList = expressionList.stream().map(Expression::getDataType)
+                            .map(DataType::toCatalogDataType).collect(Collectors.toList());
+                    Function staleFuncDesc = new Function(functionName, staleTypeList,
+                            functionCall.getDataType().toCatalogDataType(),
+                            // I think an aggregate function will never have a variable length parameters
+                            false);
+                    Function staleFunc = Catalog.getCurrentCatalog()
+                            .getFunction(staleFuncDesc, CompareMode.IS_IDENTICAL);
+                    Preconditions.checkArgument(staleFunc instanceof org.apache.doris.catalog.AggregateFunction);
+                    org.apache.doris.catalog.AggregateFunction
+                            staleAggFunc = (org.apache.doris.catalog.AggregateFunction) staleFunc;
+                    Type staleIntermediateType = staleAggFunc.getIntermediateType();
+                    Type staleRetType = staleAggFunc.getReturnType();
+                    if (staleIntermediateType != null && !staleIntermediateType.equals(staleRetType)) {
+                        functionCall.setIntermediate(DataType.convertFromCatalogDataType(staleIntermediateType));
+                    }
+                }
+                intermediateAggExpressionList.add(namedExpression);
+            }
+            LogicalAggregate localAgg = new LogicalAggregate(
+                    agg.getGroupByExprList().stream().map(Expression::clone).collect(Collectors.toList()),
+                    intermediateAggExpressionList,
+                    true
+            );
+
+            Plan childPlan = plan(localAgg, plan.child(0));
+            List<Slot> stalePlanOutputSlotList = plan.getOutput();
+            List<Slot> childOutputSlotList = childPlan.getOutput();
+            int childOutputSize = stalePlanOutputSlotList.size();
+            Preconditions.checkState(childOutputSize == childOutputSlotList.size());
+            Map<Slot, Slot> staleToNew = new HashMap<>();
+            for (int i = 0; i < stalePlanOutputSlotList.size(); i++) {
+                staleToNew.put(stalePlanOutputSlotList.get(i), childOutputSlotList.get(i));
+            }
+            List<Expression> groupByExpressionList = agg.getGroupByExprList();
+            for (int i = 0; i < groupByExpressionList.size(); i++) {
+                replaceSlot(staleToNew, groupByExpressionList, groupByExpressionList.get(i), i);
+            }
+            List<NamedExpression> mergeOutputExpressionList = agg.getOutputExpressionList();
+            for (int i = 0; i < mergeOutputExpressionList.size(); i++) {
+                replaceSlot(staleToNew, mergeOutputExpressionList, mergeOutputExpressionList.get(i), i);
+            }
+            LogicalAggregate mergeAgg = new LogicalAggregate(

Review Comment:
   You need a representation to determine whether the current agg is a lower agg or an upper one.



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