morrySnow commented on code in PR #34738:
URL: https://github.com/apache/doris/pull/34738#discussion_r1604323524
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleType.java:
##########
@@ -241,6 +241,21 @@ public enum RuleType {
MATERIALIZED_INDEX_PROJECT_SCAN(RuleTypeClass.REWRITE),
MATERIALIZED_INDEX_PROJECT_FILTER_SCAN(RuleTypeClass.REWRITE),
MATERIALIZED_INDEX_FILTER_PROJECT_SCAN(RuleTypeClass.REWRITE),
+ PREAGG_STATUS_AGG_SCAN(RuleTypeClass.REWRITE),
+ PREAGG_STATUS_AGG_FILTER_SCAN(RuleTypeClass.REWRITE),
+ PREAGG_STATUS_AGG_PROJECT_SCAN(RuleTypeClass.REWRITE),
+ PREAGG_STATUS_AGG_PROJECT_FILTER_SCAN(RuleTypeClass.REWRITE),
+ PREAGG_STATUS_AGG_FILTER_PROJECT_SCAN(RuleTypeClass.REWRITE),
Review Comment:
why we have filter above project? for variant?
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/AdjustPreAggStatus.java:
##########
@@ -0,0 +1,704 @@
+// 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.annotation.Developing;
+import org.apache.doris.nereids.rules.Rule;
+import org.apache.doris.nereids.rules.RuleType;
+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.algebra.Project;
+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.LogicalOlapScan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
+import org.apache.doris.nereids.trees.plans.logical.LogicalRepeat;
+import org.apache.doris.nereids.util.ExpressionUtils;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Sets;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * AdjustPreAggStatus
+ */
+@Developing
+public class AdjustPreAggStatus implements RewriteRuleFactory {
+ ///////////////////////////////////////////////////////////////////////////
+ // All the patterns
+ ///////////////////////////////////////////////////////////////////////////
+ @Override
+ public List<Rule> buildRules() {
+ return ImmutableList.of(
+ // Aggregate(Scan)
+
logicalAggregate(logicalOlapScan().when(LogicalOlapScan::isPreAggStatusUnSet))
+ .thenApplyNoThrow(ctx -> {
+ LogicalAggregate<LogicalOlapScan> agg = ctx.root;
+ LogicalOlapScan scan = agg.child();
+ List<AggregateFunction> aggregateFunctions =
+ extractAggFunctionAndReplaceSlot(agg,
Optional.empty());
+ List<Expression> groupByExpressions =
agg.getGroupByExpressions();
+ Set<Expression> predicates = ImmutableSet.of();
+ PreAggStatus preAggStatus =
checkPreAggStatus(scan, predicates,
+ aggregateFunctions, groupByExpressions);
+ return
agg.withChildren(scan.withPreAggStatus(preAggStatus));
+ }).toRule(RuleType.PREAGG_STATUS_AGG_SCAN),
+
+ // Aggregate(Filter(Scan))
+ logicalAggregate(
+
logicalFilter(logicalOlapScan().when(LogicalOlapScan::isPreAggStatusUnSet)))
+ .thenApplyNoThrow(ctx -> {
+
LogicalAggregate<LogicalFilter<LogicalOlapScan>> agg = ctx.root;
+ LogicalFilter<LogicalOlapScan> filter =
agg.child();
+ LogicalOlapScan scan = filter.child();
+ List<AggregateFunction> aggregateFunctions
=
+
extractAggFunctionAndReplaceSlot(agg, Optional.empty());
+ List<Expression> groupByExpressions =
+ agg.getGroupByExpressions();
+ Set<Expression> predicates =
filter.getConjuncts();
+ PreAggStatus preAggStatus =
checkPreAggStatus(scan, predicates,
+ aggregateFunctions,
groupByExpressions);
+ return agg.withChildren(filter
+
.withChildren(scan.withPreAggStatus(preAggStatus)));
+
}).toRule(RuleType.PREAGG_STATUS_AGG_FILTER_SCAN),
+
+ // Aggregate(Project(Scan))
+ logicalAggregate(logicalProject(
+
logicalOlapScan().when(LogicalOlapScan::isPreAggStatusUnSet)))
+ .thenApplyNoThrow(ctx -> {
+
LogicalAggregate<LogicalProject<LogicalOlapScan>> agg =
+ ctx.root;
+ LogicalProject<LogicalOlapScan> project =
agg.child();
+ LogicalOlapScan scan = project.child();
+ List<AggregateFunction> aggregateFunctions
=
+
extractAggFunctionAndReplaceSlot(agg,
+ Optional.of(project));
+ List<Expression> groupByExpressions =
+
ExpressionUtils.replace(agg.getGroupByExpressions(),
+
project.getAliasToProducer());
+ Set<Expression> predicates =
ImmutableSet.of();
+ PreAggStatus preAggStatus =
checkPreAggStatus(scan, predicates,
+ aggregateFunctions,
groupByExpressions);
+ return agg.withChildren(project
+
.withChildren(scan.withPreAggStatus(preAggStatus)));
+
}).toRule(RuleType.PREAGG_STATUS_AGG_PROJECT_SCAN),
+
+ // Aggregate(Project(Filter(Scan)))
+ logicalAggregate(logicalProject(logicalFilter(
+
logicalOlapScan().when(LogicalOlapScan::isPreAggStatusUnSet))))
+ .thenApplyNoThrow(ctx -> {
+
LogicalAggregate<LogicalProject<LogicalFilter<LogicalOlapScan>>> agg = ctx.root;
+
LogicalProject<LogicalFilter<LogicalOlapScan>> project = agg.child();
+ LogicalFilter<LogicalOlapScan> filter =
project.child();
+ LogicalOlapScan scan = filter.child();
+ List<AggregateFunction> aggregateFunctions
=
+
extractAggFunctionAndReplaceSlot(agg, Optional.of(project));
+ List<Expression> groupByExpressions =
+
ExpressionUtils.replace(agg.getGroupByExpressions(),
+
project.getAliasToProducer());
+ Set<Expression> predicates =
filter.getConjuncts();
+ PreAggStatus preAggStatus =
checkPreAggStatus(scan, predicates,
+ aggregateFunctions,
groupByExpressions);
+ return
agg.withChildren(project.withChildren(filter
+
.withChildren(scan.withPreAggStatus(preAggStatus))));
+
}).toRule(RuleType.PREAGG_STATUS_AGG_PROJECT_FILTER_SCAN),
+
+ // Aggregate(Filter(Project(Scan)))
+ logicalAggregate(logicalFilter(logicalProject(
+
logicalOlapScan().when(LogicalOlapScan::isPreAggStatusUnSet))))
+ .thenApplyNoThrow(ctx -> {
+
LogicalAggregate<LogicalFilter<LogicalProject<LogicalOlapScan>>> agg = ctx.root;
+
LogicalFilter<LogicalProject<LogicalOlapScan>> filter =
+ agg.child();
+ LogicalProject<LogicalOlapScan> project =
filter.child();
+ LogicalOlapScan scan = project.child();
+ List<AggregateFunction> aggregateFunctions
=
+
extractAggFunctionAndReplaceSlot(agg, Optional.of(project));
+ List<Expression> groupByExpressions =
+
ExpressionUtils.replace(agg.getGroupByExpressions(),
+
project.getAliasToProducer());
+ Set<Expression> predicates =
ExpressionUtils.replace(
+ filter.getConjuncts(),
project.getAliasToProducer());
+ PreAggStatus preAggStatus =
checkPreAggStatus(scan, predicates,
+ aggregateFunctions,
groupByExpressions);
+ return
agg.withChildren(filter.withChildren(project
+
.withChildren(scan.withPreAggStatus(preAggStatus))));
+
}).toRule(RuleType.PREAGG_STATUS_AGG_FILTER_PROJECT_SCAN),
+
+ // Aggregate(Repeat(Scan))
+ logicalAggregate(
+
logicalRepeat(logicalOlapScan().when(LogicalOlapScan::isPreAggStatusUnSet)))
+ .thenApplyNoThrow(ctx -> {
+
LogicalAggregate<LogicalRepeat<LogicalOlapScan>> agg = ctx.root;
+ LogicalRepeat<LogicalOlapScan> repeat =
agg.child();
+ LogicalOlapScan scan = repeat.child();
+ List<AggregateFunction> aggregateFunctions
=
+
extractAggFunctionAndReplaceSlot(agg, Optional.empty());
+ List<Expression> groupByExpressions =
nonVirtualGroupByExprs(agg);
+ Set<Expression> predicates =
ImmutableSet.of();
+ PreAggStatus preAggStatus =
checkPreAggStatus(scan, predicates,
+ aggregateFunctions,
groupByExpressions);
+ return agg.withChildren(repeat
+
.withChildren(scan.withPreAggStatus(preAggStatus)));
+
}).toRule(RuleType.PREAGG_STATUS_AGG_REPEAT_SCAN),
+
+ // Aggregate(Repeat(Filter(Scan)))
+ logicalAggregate(logicalRepeat(logicalFilter(
+
logicalOlapScan().when(LogicalOlapScan::isPreAggStatusUnSet))))
+ .thenApplyNoThrow(ctx -> {
+
LogicalAggregate<LogicalRepeat<LogicalFilter<LogicalOlapScan>>> agg = ctx.root;
+
LogicalRepeat<LogicalFilter<LogicalOlapScan>> repeat = agg.child();
+ LogicalFilter<LogicalOlapScan> filter =
repeat.child();
+ LogicalOlapScan scan = filter.child();
+ List<AggregateFunction> aggregateFunctions
=
+
extractAggFunctionAndReplaceSlot(agg, Optional.empty());
+ List<Expression> groupByExpressions =
+ nonVirtualGroupByExprs(agg);
+ Set<Expression> predicates =
filter.getConjuncts();
+ PreAggStatus preAggStatus =
checkPreAggStatus(scan, predicates,
+ aggregateFunctions,
groupByExpressions);
+ return
agg.withChildren(repeat.withChildren(filter
+
.withChildren(scan.withPreAggStatus(preAggStatus))));
+
}).toRule(RuleType.PREAGG_STATUS_AGG_REPEAT_FILTER_SCAN),
+
+ // Aggregate(Repeat(Project(Scan)))
+ logicalAggregate(logicalRepeat(logicalProject(
+
logicalOlapScan().when(LogicalOlapScan::isPreAggStatusUnSet))))
+ .thenApplyNoThrow(ctx -> {
+
LogicalAggregate<LogicalRepeat<LogicalProject<LogicalOlapScan>>> agg = ctx.root;
+
LogicalRepeat<LogicalProject<LogicalOlapScan>> repeat = agg.child();
+ LogicalProject<LogicalOlapScan> project =
repeat.child();
+ LogicalOlapScan scan = project.child();
+ List<AggregateFunction> aggregateFunctions
=
+
extractAggFunctionAndReplaceSlot(agg, Optional.empty());
+ List<Expression> groupByExpressions =
+
ExpressionUtils.replace(nonVirtualGroupByExprs(agg),
+
project.getAliasToProducer());
+ Set<Expression> predicates =
ImmutableSet.of();
+ PreAggStatus preAggStatus =
checkPreAggStatus(scan, predicates,
+ aggregateFunctions,
groupByExpressions);
+ return
agg.withChildren(repeat.withChildren(project
+
.withChildren(scan.withPreAggStatus(preAggStatus))));
+
}).toRule(RuleType.PREAGG_STATUS_AGG_REPEAT_PROJECT_SCAN),
+
+ // Aggregate(Repeat(Project(Filter(Scan))))
+ logicalAggregate(logicalRepeat(logicalProject(logicalFilter(
+
logicalOlapScan().when(LogicalOlapScan::isPreAggStatusUnSet)))))
+ .thenApplyNoThrow(ctx -> {
+
LogicalAggregate<LogicalRepeat<LogicalProject<LogicalFilter<LogicalOlapScan>>>>
agg
+ = ctx.root;
+
LogicalRepeat<LogicalProject<LogicalFilter<LogicalOlapScan>>> repeat =
agg.child();
+
LogicalProject<LogicalFilter<LogicalOlapScan>> project = repeat.child();
+ LogicalFilter<LogicalOlapScan> filter =
project.child();
+ LogicalOlapScan scan = filter.child();
+ List<AggregateFunction> aggregateFunctions
=
+
extractAggFunctionAndReplaceSlot(agg, Optional.empty());
+ List<Expression> groupByExpressions =
+
ExpressionUtils.replace(nonVirtualGroupByExprs(agg),
+
project.getAliasToProducer());
+ Set<Expression> predicates =
filter.getConjuncts();
+ PreAggStatus preAggStatus =
checkPreAggStatus(scan, predicates,
+ aggregateFunctions,
groupByExpressions);
+ return agg.withChildren(repeat
+
.withChildren(project.withChildren(filter.withChildren(
+
scan.withPreAggStatus(preAggStatus)))));
+
}).toRule(RuleType.PREAGG_STATUS_AGG_REPEAT_PROJECT_FILTER_SCAN),
+
+ // Aggregate(Repeat(Filter(Project(Scan))))
+ logicalAggregate(logicalRepeat(logicalFilter(logicalProject(
+
logicalOlapScan().when(LogicalOlapScan::isPreAggStatusUnSet)))))
+ .thenApplyNoThrow(ctx -> {
+
LogicalAggregate<LogicalRepeat<LogicalFilter<LogicalProject<LogicalOlapScan>>>>
agg
+ = ctx.root;
+
LogicalRepeat<LogicalFilter<LogicalProject<LogicalOlapScan>>> repeat =
agg.child();
+
LogicalFilter<LogicalProject<LogicalOlapScan>> filter = repeat.child();
+ LogicalProject<LogicalOlapScan> project =
filter.child();
+ LogicalOlapScan scan = project.child();
+ List<AggregateFunction> aggregateFunctions
=
+
extractAggFunctionAndReplaceSlot(agg, Optional.of(project));
+ List<Expression> groupByExpressions =
+
ExpressionUtils.replace(nonVirtualGroupByExprs(agg),
+
project.getAliasToProducer());
+ Set<Expression> predicates =
ExpressionUtils.replace(
+ filter.getConjuncts(),
project.getAliasToProducer());
+ PreAggStatus preAggStatus =
checkPreAggStatus(scan, predicates,
+ aggregateFunctions,
groupByExpressions);
+ return agg.withChildren(repeat
+
.withChildren(filter.withChildren(project.withChildren(
+
scan.withPreAggStatus(preAggStatus)))));
+
}).toRule(RuleType.PREAGG_STATUS_AGG_REPEAT_FILTER_PROJECT_SCAN),
+
+ // Filter(Project(Scan))
+ logicalFilter(logicalProject(
+
logicalOlapScan().when(LogicalOlapScan::isPreAggStatusUnSet)))
+ .thenApplyNoThrow(ctx -> {
+
LogicalFilter<LogicalProject<LogicalOlapScan>> filter = ctx.root;
+ LogicalProject<LogicalOlapScan> project =
filter.child();
+ LogicalOlapScan scan = project.child();
+ List<AggregateFunction> aggregateFunctions
= ImmutableList.of();
+ List<Expression> groupByExpressions =
ImmutableList.of();
+ Set<Expression> predicates =
ExpressionUtils.replace(
+ filter.getConjuncts(),
project.getAliasToProducer());
+ PreAggStatus preAggStatus =
checkPreAggStatus(scan, predicates,
+ aggregateFunctions,
groupByExpressions);
+ return filter.withChildren(project
+
.withChildren(scan.withPreAggStatus(preAggStatus)));
+
}).toRule(RuleType.PREAGG_STATUS_FILTER_PROJECT_SCAN),
+
+ // Filter(Scan)
+
logicalFilter(logicalOlapScan().when(LogicalOlapScan::isPreAggStatusUnSet))
+ .thenApplyNoThrow(ctx -> {
+ LogicalFilter<LogicalOlapScan> filter = ctx.root;
+ LogicalOlapScan scan = filter.child();
+ List<AggregateFunction> aggregateFunctions =
ImmutableList.of();
+ List<Expression> groupByExpressions =
ImmutableList.of();
+ Set<Expression> predicates = filter.getConjuncts();
+ PreAggStatus preAggStatus =
checkPreAggStatus(scan, predicates,
+ aggregateFunctions, groupByExpressions);
+ return
filter.withChildren(scan.withPreAggStatus(preAggStatus));
+ }).toRule(RuleType.PREAGG_STATUS_FILTER_SCAN),
+
+ // only scan.
+ logicalOlapScan().when(LogicalOlapScan::isPreAggStatusUnSet)
+ .thenApplyNoThrow(ctx -> {
+ LogicalOlapScan scan = ctx.root;
+ List<AggregateFunction> aggregateFunctions =
ImmutableList.of();
+ List<Expression> groupByExpressions =
ImmutableList.of();
+ Set<Expression> predicates = ImmutableSet.of();
+ PreAggStatus preAggStatus =
checkPreAggStatus(scan, predicates,
+ aggregateFunctions, groupByExpressions);
+ return scan.withPreAggStatus(preAggStatus);
+ }).toRule(RuleType.PREAGG_STATUS_SCAN));
+ }
+
+ ///////////////////////////////////////////////////////////////////////////
+ // Set pre-aggregation status.
+ ///////////////////////////////////////////////////////////////////////////
+
+ /**
+ * Do aggregate function extraction and replace aggregate function's input
slots by underlying project.
+ * <p>
+ * 1. extract aggregate functions in aggregate plan.
+ * <p>
+ * 2. replace aggregate function's input slot by underlying project
expression if project is present.
+ * <p>
+ * For example:
+ * <pre>
+ * input arguments:
+ * agg: Aggregate(sum(v) as sum_value)
+ * underlying project: Project(a + b as v)
+ *
+ * output:
+ * sum(a + b)
+ * </pre>
+ */
+ private List<AggregateFunction>
extractAggFunctionAndReplaceSlot(LogicalAggregate<?> agg,
+ Optional<LogicalProject<?>> project) {
+ Optional<Map<Slot, Expression>> slotToProducerOpt =
+ project.map(Project::getAliasToProducer);
+ return agg.getOutputExpressions().stream()
+ // extract aggregate functions.
+ .flatMap(e ->
e.<Set<AggregateFunction>>collect(AggregateFunction.class::isInstance)
+ .stream())
+ // replace aggregate function's input slot by its producing
expression.
+ .map(expr -> slotToProducerOpt
+ .map(slotToExpressions -> (AggregateFunction)
ExpressionUtils.replace(expr,
+ slotToExpressions))
+ .orElse(expr))
+ .collect(Collectors.toList());
+ }
+
+ private PreAggStatus checkPreAggStatus(LogicalOlapScan olapScan,
Set<Expression> predicates,
+ List<AggregateFunction> aggregateFuncs, List<Expression>
groupingExprs) {
+ long selectIndexId = olapScan.getSelectedIndexId();
+ MaterializedIndexMeta meta =
olapScan.getTable().getIndexMetaByIndexId(selectIndexId);
+ if (meta.getKeysType() == KeysType.DUP_KEYS || (meta.getKeysType() ==
KeysType.UNIQUE_KEYS
+ && olapScan.getTable().getEnableUniqueKeyMergeOnWrite())) {
+ return PreAggStatus.on();
+ }
Review Comment:
let it be a short path method before we do extract and normalize agg
function? for most case, this will work well
--
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]