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/10/30 09:05:10 UTC

[GitHub] [doris] 924060929 commented on a diff in pull request #12583: [feature](Nereids) implement grouping sets

924060929 commented on code in PR #12583:
URL: https://github.com/apache/doris/pull/12583#discussion_r1008814774


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/ResolveHaving.java:
##########
@@ -47,30 +49,50 @@
 /**
  * Resolve having clause to the aggregation.
  */
-public class ResolveHaving extends OneAnalysisRuleFactory {
+public class ResolveHaving implements AnalysisRuleFactory {

Review Comment:
   In nereids, Resolve should rename to Bind, and Resolved should rename to Bound.
   Please keep the naming consistent



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/NormalizeGroupBy.java:
##########
@@ -0,0 +1,265 @@
+// 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.Alias;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.SlotReference;
+import org.apache.doris.nereids.trees.expressions.VirtualSlotReference;
+import org.apache.doris.nereids.trees.expressions.functions.agg.AggregateFunction;
+import org.apache.doris.nereids.trees.expressions.functions.grouping.GroupingSetsFunction;
+import org.apache.doris.nereids.trees.expressions.literal.Literal;
+import org.apache.doris.nereids.trees.plans.GroupPlan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate;
+import org.apache.doris.nereids.trees.plans.logical.LogicalGroupBy;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
+import org.apache.doris.nereids.util.ExpressionUtils;
+import org.apache.doris.nereids.util.Utils;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.stream.Collectors;
+
+/**
+ * normalize output, aggFunc and groupingFunc in grouping sets.
+ * This rule is executed after NormalizeAggregate.
+ *
+ * eg: select sum(k2 + 1), grouping(k1) from t1 group by grouping sets ((k1));
+ * Original Plan:
+ * Aggregate(
+ *   keys:[k1#1, grouping_id()#0, grouping_prefix(k1#1)#7],
+ *   outputs:[sum((k2 + 1)#8) AS `sum((k2 + 1))`#9, GROUPING_PREFIX_(k1#1)#7)
+ * +-- Project(projections: k1#1, (k2#2 + 1) as (k2 + 1)#8), grouping_prefix(k1#1)#7
+ *     +-- GroupingSets(
+ *         keys:[k1#1, grouping_id()#0, grouping_prefix(k1#1)#7]
+ *         outputs:sum(k2#2 + 1) as `sum(k2 + 1)`#3, group(grouping_prefix(k1#1)#7) as `grouping(k1 + 1)`#4
+ *
+ * After rule:
+ * Aggregate(
+ *   keys:[k1#1, SR#9]
+ *   outputs:[sum((k2 + 1)#8) AS `sum((k2 + 1))`#9, GROUPING_PREFIX_(k1#1)#7))
+ *     +-- Project(k1#1, (K2 + 1)#10 as `(k2 + 1)`#8, grouping_id()#0, grouping_prefix(k1#1)#7)
+ *         +-- GropingSets(
+ *             keys:[k1#1, grouping_id()#0, grouping_prefix(k1#1)#7]
+ *             outputs:k1#1, (k2 + 1)#10, grouping_prefix(k1#1)#7
+ */
+public class NormalizeGroupBy extends OneRewriteRuleFactory {
+    @Override
+    public Rule build() {
+        return logicalAggregate(logicalProject(logicalGroupBy().whenNot(LogicalGroupBy::isNormalized)))
+                .then(agg -> {

Review Comment:
   this rule is too complex, please extract main computation flow.
   



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/GroupByResolve.java:
##########
@@ -0,0 +1,121 @@
+// 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.analysis.OneAnalysisRuleFactory;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.VirtualSlotReference;
+import org.apache.doris.nereids.trees.expressions.functions.grouping.GroupingSetsFunction;
+import org.apache.doris.nereids.trees.expressions.visitor.DefaultExpressionRewriter;
+import org.apache.doris.nereids.trees.plans.GroupPlan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate;
+import org.apache.doris.nereids.trees.plans.logical.LogicalGroupBy;
+import org.apache.doris.nereids.types.BigIntType;
+import org.apache.doris.planner.PlannerContext;
+
+import com.google.common.collect.Lists;
+
+import java.util.ArrayList;
+import java.util.BitSet;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * Parse the grouping sets operator,
+ * for the groupingFunc function, create a virtualSlotRefrance for a period of time.
+ *
+ * At the same time, synchronize the groupBy in groupingSets to aggregate.
+ */
+public class GroupByResolve extends OneAnalysisRuleFactory {

Review Comment:
   Why not move this rule into the BindSlotReference? I think BindSlotReference should binding all slot in every plan.



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalGroupBy.java:
##########
@@ -0,0 +1,287 @@
+// 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.trees.plans.logical;
+
+import org.apache.doris.nereids.exceptions.AnalysisException;
+import org.apache.doris.nereids.memo.GroupExpression;
+import org.apache.doris.nereids.properties.LogicalProperties;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.VirtualSlotReference;
+import org.apache.doris.nereids.trees.plans.Plan;
+import org.apache.doris.nereids.trees.plans.PlanType;
+import org.apache.doris.nereids.trees.plans.algebra.GroupBy;
+import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
+import org.apache.doris.nereids.types.BigIntType;
+import org.apache.doris.nereids.util.Utils;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Lists;
+
+import java.util.ArrayList;
+import java.util.BitSet;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.Set;
+
+/**
+ * Logical Grouping sets.
+ */
+public abstract class LogicalGroupBy<CHILD_TYPE extends Plan> extends LogicalUnary<CHILD_TYPE> implements GroupBy {

Review Comment:
   GroupBy look like AST, but nereids LogicalXxx is a Plan, using a mixture of naming conventions will cause complications later on.
   
   And GroupBy is misleading, because it not contains the normal group by expressions.
   I suggest rename it to GroupingElement, LogicalGroupingElement, PhysicalGroupingElement
   
   



##########
fe/fe-core/src/main/java/org/apache/doris/catalog/BuiltinFunctions.java:
##########
@@ -51,6 +53,11 @@ public class BuiltinFunctions implements FunctionHelper {
             agg(Sum.class)
     );
 
+    public final ImmutableList<GroupingSetsFunc> groupingSetsFunctions = ImmutableList.of(
+            groupingSets(Grouping.class),
+            groupingSets(GroupingId.class, "grouping_id")
+    );

Review Comment:
   `grouping()` and `grouping_id()` are scalar function definitely, don't use special function list here, this will make others imitate wrongly and add more special function list.
   
   The list separate by the function type of top abstract level:
   - scalar function
   - aggregate function
   - table function
   
   They describe how many rows would be consumed and how many rows would be produced, any function should be classified into one of them.
   
   So I suggest rename GroupingSetsFunc to GroupingScalarFunc and  extends ScalarFunc, then register in the scalar function list, without other special list.
   ```java
   public final List<ScalarFunc> scalarFunctions = ImmutableList.builder()
           .add(...) // other general functions
           .add(groupingScalar(Grouping.class))
           .add(groupingScalar(GroupingId.class, "groupinig_id"))
           .build();
   ```



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