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


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/SetPreAggStatus.java:
##########
@@ -0,0 +1,589 @@
+// 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.catalog.AggregateType;
+import org.apache.doris.catalog.KeysType;
+import org.apache.doris.catalog.MaterializedIndexMeta;
+import org.apache.doris.common.Pair;
+import org.apache.doris.nereids.jobs.JobContext;
+import org.apache.doris.nereids.trees.expressions.CaseWhen;
+import org.apache.doris.nereids.trees.expressions.Cast;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.expressions.SlotReference;
+import org.apache.doris.nereids.trees.expressions.VirtualSlotReference;
+import org.apache.doris.nereids.trees.expressions.WhenClause;
+import 
org.apache.doris.nereids.trees.expressions.functions.agg.AggregateFunction;
+import org.apache.doris.nereids.trees.expressions.functions.agg.BitmapUnion;
+import 
org.apache.doris.nereids.trees.expressions.functions.agg.BitmapUnionCount;
+import org.apache.doris.nereids.trees.expressions.functions.agg.Count;
+import org.apache.doris.nereids.trees.expressions.functions.agg.HllUnion;
+import org.apache.doris.nereids.trees.expressions.functions.agg.HllUnionAgg;
+import org.apache.doris.nereids.trees.expressions.functions.agg.Max;
+import org.apache.doris.nereids.trees.expressions.functions.agg.Min;
+import org.apache.doris.nereids.trees.expressions.functions.agg.Sum;
+import org.apache.doris.nereids.trees.expressions.functions.scalar.If;
+import org.apache.doris.nereids.trees.expressions.literal.NullLiteral;
+import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
+import org.apache.doris.nereids.trees.plans.Plan;
+import org.apache.doris.nereids.trees.plans.PreAggStatus;
+import org.apache.doris.nereids.trees.plans.RelationId;
+import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate;
+import org.apache.doris.nereids.trees.plans.logical.LogicalFilter;
+import org.apache.doris.nereids.trees.plans.logical.LogicalJoin;
+import org.apache.doris.nereids.trees.plans.logical.LogicalOlapScan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
+import org.apache.doris.nereids.trees.plans.logical.LogicalRepeat;
+import org.apache.doris.nereids.trees.plans.visitor.CustomRewriter;
+import org.apache.doris.nereids.trees.plans.visitor.DefaultPlanRewriter;
+import org.apache.doris.nereids.util.ExpressionUtils;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.Stack;
+
+/**
+ * SetPreAggStatus
+ * bottom-up tranverse the plan tree and collect required info into 
PreAggInfoContext
+ * when get to the bottom LogicalOlapScan node, we set the preagg status using 
info in PreAggInfoContext
+ */
+public class SetPreAggStatus extends 
DefaultPlanRewriter<Stack<SetPreAggStatus.PreAggInfoContext>>
+        implements CustomRewriter {
+    private Map<RelationId, PreAggInfoContext> olapScanPreAggContexts = new 
HashMap<>();
+
+    /**
+     * PreAggInfoContext
+     */
+    public static class PreAggInfoContext {
+        private List<Expression> filterConjuncts = new ArrayList<>();
+        private List<Expression> joinConjuncts = new ArrayList<>();
+        private List<Expression> groupByExpresssions = new ArrayList<>();
+        private Set<AggregateFunction> aggregateFunctions = new HashSet<>();
+        private Set<RelationId> olapScanIds = new HashSet<>();
+
+        private Map<Slot, Expression> replaceMap = new HashMap<>();
+
+        private void setReplaceMap(Map<Slot, Expression> replaceMap) {
+            this.replaceMap = replaceMap;
+        }
+
+        private void addRelationId(RelationId id) {
+            olapScanIds.add(id);
+        }
+
+        private void addJoinInfo(LogicalJoin logicalJoin) {
+            joinConjuncts.addAll(logicalJoin.getExpressions());
+            joinConjuncts = 
Lists.newArrayList(ExpressionUtils.replace(joinConjuncts, replaceMap));
+        }
+
+        private void addFilterConjuncts(List<Expression> conjuncts) {
+            filterConjuncts.addAll(conjuncts);
+            filterConjuncts = 
Lists.newArrayList(ExpressionUtils.replace(filterConjuncts, replaceMap));
+        }
+
+        private void addGroupByExpresssions(List<Expression> expressions) {
+            groupByExpresssions.addAll(expressions);
+            groupByExpresssions = 
Lists.newArrayList(ExpressionUtils.replace(groupByExpresssions, replaceMap));
+        }
+
+        private void addAggregateFunctions(Set<AggregateFunction> functions) {
+            aggregateFunctions.addAll(functions);
+            Set<AggregateFunction> newAggregateFunctions = Sets.newHashSet();
+            for (AggregateFunction aggregateFunction : aggregateFunctions) {
+                newAggregateFunctions
+                        .add((AggregateFunction) 
ExpressionUtils.replace(aggregateFunction, replaceMap));
+            }
+            aggregateFunctions = newAggregateFunctions;
+        }
+    }
+
+    @Override
+    public Plan rewriteRoot(Plan plan, JobContext jobContext) {
+        Plan newPlan = plan.accept(this, new Stack<>());
+        return newPlan.accept(new SetOlapScanPreAgg(), olapScanPreAggContexts);
+    }
+
+    @Override
+    public Plan visit(Plan plan, Stack<PreAggInfoContext> context) {
+        Plan newPlan = super.visit(plan, context);
+        context.clear();

Review Comment:
   can this plan use the preagg?
   ```
   LogicalUnion
   +-- LogicalAggregate
   |   +-- LogicalOlapScan
   +-- LogicalAggregate
       +-- LogicalOlapScan
   ```



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to