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


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PushCountIntoUnionAll.java:
##########
@@ -0,0 +1,188 @@
+// 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.
+// copied from 
https://github.com/trinodb/trino/blob/master/core/trino-main/src/main/java/io/trino/server/PluginManager.java

Review Comment:
   remove this line



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PushCountIntoUnionAll.java:
##########
@@ -0,0 +1,188 @@
+// 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.
+// copied from 
https://github.com/trinodb/trino/blob/master/core/trino-main/src/main/java/io/trino/server/PluginManager.java
+
+package org.apache.doris.nereids.rules.rewrite;
+
+import org.apache.doris.nereids.rules.Rule;
+import org.apache.doris.nereids.rules.RuleType;
+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.Slot;
+import org.apache.doris.nereids.trees.expressions.SlotReference;
+import 
org.apache.doris.nereids.trees.expressions.functions.agg.AggregateFunction;
+import org.apache.doris.nereids.trees.expressions.functions.agg.Count;
+import org.apache.doris.nereids.trees.expressions.functions.agg.Sum0;
+import org.apache.doris.nereids.trees.plans.Plan;
+import org.apache.doris.nereids.trees.plans.algebra.SetOperation.Qualifier;
+import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate;
+import org.apache.doris.nereids.trees.plans.logical.LogicalSetOperation;
+import org.apache.doris.nereids.trees.plans.logical.LogicalUnion;
+import org.apache.doris.nereids.util.ExpressionUtils;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableList.Builder;
+import com.google.common.collect.Lists;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * LogicalAggregate  (groupByExpr=[c1#13], outputExpr=[c1#13, count(c1#13) AS 
`count(c1)`#15])
+ *  +--LogicalUnion (outputs=[c1#13], regularChildrenOutputs=[[c1#9], [a#4], 
[a#7]])
+ *    |--child1 (output = [[c1#9]])
+ *    |--child2 (output = [[a#4]])
+ *    +--child3 (output = [[a#7]])
+ * transform to:
+ * LogicalAggregate (groupByExpr=[c1#13], outputExpr=[c1#13, 
sum0(count(c1)#19) AS `count(c1)`#15])
+ *  +--LogicalUnion (outputs=[c1#13, count(c1)#19], 
regularChildrenOutputs=[[c1#9, count(c1)#16],
+ *   [a#4, count(a)#17], [a#7, count(a)#18]])
+ *    |--LogicalAggregate (groupByExpr=[c1#9], outputExpr=[c1#9, count(c1#9) 
AS `count(c1)`#16])
+ *    |  +--child1
+ *    |--LogicalAggregate (groupByExpr=[a#4], outputExpr=[a#4, count(a#4) AS 
`count(a)`#17])
+ *    |  +--child2
+ *    +--LogicalAggregate (groupByExpr=[a#7], outputExpr=[a#7, count(a#7) AS 
`count(a)`#18]]
+ *      +--child3
+ */
+public class PushCountIntoUnionAll extends OneRewriteRuleFactory {
+    @Override
+    public Rule build() {
+        return logicalAggregate(logicalUnion().when(this::checkUnion))
+                .when(this::checkAgg)
+                .then(this::doPush)
+                .toRule(RuleType.PUSH_COUNT_INTO_UNION_ALL);
+    }
+
+    private Plan doPush(LogicalAggregate<LogicalUnion> agg) {
+        LogicalUnion logicalUnion = agg.child();
+        List<Slot> outputs = logicalUnion.getOutput();
+        Map<Slot, Integer> replaceMap = new HashMap<>();
+        for (int i = 0; i < outputs.size(); i++) {
+            replaceMap.put(outputs.get(i), i);
+        }
+        int childSize = logicalUnion.children().size();
+        List<Expression> upperGroupByExpressions = agg.getGroupByExpressions();
+        List<NamedExpression> upperOutputExpressions = 
agg.getOutputExpressions();
+        Builder<Plan> newChildren = 
ImmutableList.builderWithExpectedSize(childSize);
+        Builder<List<SlotReference>> childrenOutputs = 
ImmutableList.builderWithExpectedSize(childSize);
+        // create the pushed down LogicalAggregate
+        for (int i = 0; i < childSize; i++) {
+            Plan child = logicalUnion.children().get(i);
+            List<Slot> childOutputs = child.getOutput();
+            List<Expression> groupByExpressions = 
replaceExpressionByUnionAll(upperGroupByExpressions, replaceMap,
+                    childOutputs);
+            List<NamedExpression> outputExpressions = 
replaceExpressionByUnionAll(upperOutputExpressions, replaceMap,
+                    childOutputs);
+            LogicalAggregate<Plan> logicalAggregate = new 
LogicalAggregate<>(groupByExpressions, outputExpressions,
+                    child);
+            newChildren.add(logicalAggregate);
+            childrenOutputs.add((List<SlotReference>) (List) 
logicalAggregate.getOutput());
+        }
+
+        // create the new LogicalUnion
+        LogicalSetOperation newLogicalUnion = 
logicalUnion.withChildrenAndTheirOutputs(newChildren.build(),
+                childrenOutputs.build());
+        List<NamedExpression> newLogicalUnionOutputs = Lists.newArrayList();
+        for (NamedExpression ce : upperOutputExpressions) {
+            if (ce instanceof Alias) {
+                newLogicalUnionOutputs.add(new SlotReference(ce.getName(), 
ce.getDataType(), ce.nullable()));
+            } else if (ce instanceof SlotReference) {
+                newLogicalUnionOutputs.add(ce);
+            }

Review Comment:
   add else condition to return origin LogicalUnion, means you can not process 
this cases



-- 
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-unsubscr...@doris.apache.org

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


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

Reply via email to