Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,8 @@ public List<Statistics> getChildrenStatistics() {
public StatementContext getStatementContext() {
return connectContext.getStatementContext();
}

public ConnectContext getConnectContext() {
return connectContext;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2515,36 +2515,37 @@ public PlanFragment visitPhysicalRepeat(PhysicalRepeat<? extends Plan> repeat, P
PlanFragment inputPlanFragment = repeat.child(0).accept(this, context);
List<List<Expr>> distributeExprLists = getDistributeExprs(repeat.child(0));

ImmutableSet<Expression> flattenGroupingSetExprs = ImmutableSet.copyOf(
ExpressionUtils.flatExpressions(repeat.getGroupingSets()));
List<Expression> flattenGroupingExpressions = repeat.getGroupByExpressions();
Set<Slot> preRepeatExpressions = Sets.newLinkedHashSet();
// keep group by expression coming first
for (Expression groupByExpr : flattenGroupingExpressions) {
// NormalizeRepeat had converted group by expression to slot
preRepeatExpressions.add((Slot) groupByExpr);
}

List<Slot> aggregateFunctionUsedSlots = repeat.getOutputExpressions()
.stream()
.filter(output -> !flattenGroupingSetExprs.contains(output))
.filter(output -> !output.containsType(GroupingScalarFunction.class))
.distinct()
.map(NamedExpression::toSlot)
// add aggregate function used expressions
for (NamedExpression outputExpr : repeat.getOutputExpressions()) {
if (!outputExpr.containsType(GroupingScalarFunction.class)) {
preRepeatExpressions.add(outputExpr.toSlot());
}
}

List<Expr> preRepeatExprs = preRepeatExpressions.stream()
.map(expr -> ExpressionTranslator.translate(expr, context))
.collect(ImmutableList.toImmutableList());

// keep flattenGroupingSetExprs comes first
List<Expr> preRepeatExprs = Stream.concat(flattenGroupingSetExprs.stream(), aggregateFunctionUsedSlots.stream())
.map(expr -> ExpressionTranslator.translate(expr, context)).collect(ImmutableList.toImmutableList());

// outputSlots's order need same with preRepeatExprs
List<Slot> outputSlots = Stream.concat(Stream
.concat(repeat.getOutputExpressions().stream()
.filter(output -> flattenGroupingSetExprs.contains(output)),
repeat.getOutputExpressions().stream()
.filter(output -> !flattenGroupingSetExprs.contains(output))
.filter(output -> !output.containsType(GroupingScalarFunction.class))
.distinct()
),
Stream.concat(Stream.of(repeat.getGroupingId().toSlot()),
repeat.getOutputExpressions().stream()
.filter(output -> output.containsType(GroupingScalarFunction.class)))
)
.map(NamedExpression::toSlot).collect(ImmutableList.toImmutableList());
// outputSlots's order must match preRepeatExprs, then grouping id, then grouping function slots
ImmutableList.Builder<Slot> outputSlotsBuilder
= ImmutableList.builderWithExpectedSize(repeat.getOutputExpressions().size() + 1);
outputSlotsBuilder.addAll(preRepeatExpressions);
outputSlotsBuilder.add(repeat.getGroupingId().toSlot());
for (NamedExpression outputExpr : repeat.getOutputExpressions()) {
if (outputExpr.containsType(GroupingScalarFunction.class)) {
outputSlotsBuilder.add(outputExpr.toSlot());
}
}

List<Slot> outputSlots = outputSlotsBuilder.build();
// NOTE: we should first translate preRepeatExprs, then generate output tuple,
// or else the preRepeatExprs can not find the bottom slotRef and throw
// exception: invalid slot id
Expand All @@ -2553,7 +2554,7 @@ public PlanFragment visitPhysicalRepeat(PhysicalRepeat<? extends Plan> repeat, P
// cube and rollup already convert to grouping sets in LogicalPlanBuilder.withAggregate()
GroupingInfo groupingInfo = new GroupingInfo(outputTuple, preRepeatExprs);

List<Set<Integer>> repeatSlotIdList = repeat.computeRepeatSlotIdList(getSlotIds(outputTuple));
List<Set<Integer>> repeatSlotIdList = repeat.computeRepeatSlotIdList(getSlotIds(outputTuple), outputSlots);
Set<Integer> allSlotId = repeatSlotIdList.stream()
.flatMap(Set::stream)
.collect(ImmutableSet.toImmutableSet());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import org.apache.doris.nereids.rules.rewrite.CountDistinctRewrite;
import org.apache.doris.nereids.rules.rewrite.CountLiteralRewrite;
import org.apache.doris.nereids.rules.rewrite.CreatePartitionTopNFromWindow;
import org.apache.doris.nereids.rules.rewrite.DecomposeRepeatWithPreAggregation;
import org.apache.doris.nereids.rules.rewrite.DecoupleEncodeDecode;
import org.apache.doris.nereids.rules.rewrite.DeferMaterializeTopNResult;
import org.apache.doris.nereids.rules.rewrite.DistinctAggStrategySelector;
Expand Down Expand Up @@ -880,7 +881,6 @@ private static List<RewriteJob> getWholeTreeRewriteJobs(
ImmutableSet.of(LogicalCTEAnchor.class),
() -> {
List<RewriteJob> rewriteJobs = Lists.newArrayListWithExpectedSize(300);

rewriteJobs.addAll(jobs(
topic("cte inline and pull up all cte anchor",
custom(RuleType.PULL_UP_CTE_ANCHOR, PullUpCteAnchor::new),
Expand Down Expand Up @@ -908,7 +908,8 @@ private static List<RewriteJob> getWholeTreeRewriteJobs(
rewriteJobs.addAll(jobs(topic("split multi distinct",
custom(RuleType.DISTINCT_AGG_STRATEGY_SELECTOR,
() -> DistinctAggStrategySelector.INSTANCE))));

rewriteJobs.addAll(jobs(topic("decompse repeat",
custom(RuleType.DECOMPOSE_REPEAT, () -> DecomposeRepeatWithPreAggregation.INSTANCE))));
// Rewrite search function before VariantSubPathPruning
// so that ElementAt expressions from search can be processed
rewriteJobs.addAll(jobs(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,7 @@
import org.apache.doris.nereids.trees.plans.algebra.Aggregate;
import org.apache.doris.nereids.trees.plans.algebra.InlineTable;
import org.apache.doris.nereids.trees.plans.algebra.OneRowRelation;
import org.apache.doris.nereids.trees.plans.algebra.Repeat.RepeatType;
import org.apache.doris.nereids.trees.plans.algebra.SetOperation.Qualifier;
import org.apache.doris.nereids.trees.plans.commands.AddConstraintCommand;
import org.apache.doris.nereids.trees.plans.commands.AdminCancelRebalanceDiskCommand;
Expand Down Expand Up @@ -4612,15 +4613,15 @@ private LogicalPlan withAggregate(LogicalPlan input, SelectColumnClauseContext s
for (GroupingSetContext groupingSetContext : groupingElementContext.groupingSet()) {
groupingSets.add(visit(groupingSetContext.expression(), Expression.class));
}
return new LogicalRepeat<>(groupingSets.build(), namedExpressions, input);
return new LogicalRepeat<>(groupingSets.build(), namedExpressions, RepeatType.GROUPING_SETS, input);
} else if (groupingElementContext.CUBE() != null) {
List<Expression> cubeExpressions = visit(groupingElementContext.expression(), Expression.class);
List<List<Expression>> groupingSets = ExpressionUtils.cubeToGroupingSets(cubeExpressions);
return new LogicalRepeat<>(groupingSets, namedExpressions, input);
return new LogicalRepeat<>(groupingSets, namedExpressions, RepeatType.CUBE, input);
} else if (groupingElementContext.ROLLUP() != null && groupingElementContext.WITH() == null) {
List<Expression> rollupExpressions = visit(groupingElementContext.expression(), Expression.class);
List<List<Expression>> groupingSets = ExpressionUtils.rollupToGroupingSets(rollupExpressions);
return new LogicalRepeat<>(groupingSets, namedExpressions, input);
return new LogicalRepeat<>(groupingSets, namedExpressions, RepeatType.ROLLUP, input);
} else {
List<GroupKeyWithOrder> groupKeyWithOrders = visit(groupingElementContext.expressionWithOrder(),
GroupKeyWithOrder.class);
Expand All @@ -4634,7 +4635,7 @@ private LogicalPlan withAggregate(LogicalPlan input, SelectColumnClauseContext s
}
if (groupingElementContext.ROLLUP() != null) {
List<List<Expression>> groupingSets = ExpressionUtils.rollupToGroupingSets(groupByExpressions);
return new LogicalRepeat<>(groupingSets, namedExpressions, input);
return new LogicalRepeat<>(groupingSets, namedExpressions, RepeatType.ROLLUP, input);
} else {
return new LogicalAggregate<>(groupByExpressions, namedExpressions, input);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,31 +111,7 @@ public List<List<PhysicalProperties>> visitPhysicalHashAggregate(
if (agg.getGroupByExpressions().isEmpty() && agg.getOutputExpressions().isEmpty()) {
return ImmutableList.of();
}
// If the origin attribute satisfies the group by key but does not meet the requirements, ban the plan.
// e.g. select count(distinct a) from t group by b;
// requiredChildProperty: a
// but the child is already distributed by b
// ban this plan
PhysicalProperties originChildProperty = originChildrenProperties.get(0);
PhysicalProperties requiredChildProperty = requiredProperties.get(0);
PhysicalProperties hashSpec = PhysicalProperties.createHash(agg.getGroupByExpressions(), ShuffleType.REQUIRE);
GroupExpression child = children.get(0);
if (child.getPlan() instanceof PhysicalDistribute) {
PhysicalProperties properties = new PhysicalProperties(
DistributionSpecAny.INSTANCE, originChildProperty.getOrderSpec());
Optional<Pair<Cost, GroupExpression>> pair = child.getOwnerGroup().getLowestCostPlan(properties);
// add null check
if (!pair.isPresent()) {
return ImmutableList.of();
}
GroupExpression distributeChild = pair.get().second;
PhysicalProperties distributeChildProperties = distributeChild.getOutputProperties(properties);
if (distributeChildProperties.satisfy(hashSpec)
&& !distributeChildProperties.satisfy(requiredChildProperty)) {
return ImmutableList.of();
}
}

if (!agg.getAggregateParam().canBeBanned) {
return visit(agg, context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ public Void visitPhysicalHashAggregate(PhysicalHashAggregate<? extends Plan> agg
Set<ExprId> intersectId = Sets.intersection(new HashSet<>(parentHashExprIds),
new HashSet<>(groupByExprIds));
if (!intersectId.isEmpty() && intersectId.size() < groupByExprIds.size()) {
if (shouldUseParent(parentHashExprIds, agg)) {
if (shouldUseParent(parentHashExprIds, agg, context)) {
addRequestPropertyToChildren(PhysicalProperties.createHash(
Utils.fastToImmutableList(intersectId), ShuffleType.REQUIRE));
}
Expand All @@ -469,7 +469,11 @@ public Void visitPhysicalHashAggregate(PhysicalHashAggregate<? extends Plan> agg
return null;
}

private boolean shouldUseParent(List<ExprId> parentHashExprIds, PhysicalHashAggregate<? extends Plan> agg) {
private boolean shouldUseParent(List<ExprId> parentHashExprIds, PhysicalHashAggregate<? extends Plan> agg,
PlanContext context) {
if (!context.getConnectContext().getSessionVariable().aggShuffleUseParentKey) {
return false;
}
Optional<GroupExpression> groupExpression = agg.getGroupExpression();
if (!groupExpression.isPresent()) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ public enum RuleType {
SHOR_CIRCUIT_POINT_QUERY(RuleTypeClass.REWRITE),
// skew rewrtie
SALT_JOIN(RuleTypeClass.REWRITE),

DECOMPOSE_REPEAT(RuleTypeClass.REWRITE),
DISTINCT_AGGREGATE_SPLIT(RuleTypeClass.REWRITE),
PROCESS_SCALAR_AGG_MUST_USE_MULTI_DISTINCT(RuleTypeClass.REWRITE),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,8 @@ protected LogicalAggregate<Plan> aggregateRewriteByView(
}
}
LogicalRepeat<Plan> repeat = new LogicalRepeat<>(rewrittenGroupSetsExpressions,
finalOutputExpressions, queryStructInfo.getGroupingId().get(), tempRewritedPlan);
finalOutputExpressions, queryStructInfo.getGroupingId().get(),
queryAggregate.getSourceRepeat().get().getRepeatType(), tempRewritedPlan);
return NormalizeRepeat.doNormalize(repeat);
}
return new LogicalAggregate<>(finalGroupExpressions, finalOutputExpressions, tempRewritedPlan);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ private List<Plan> implementOnePhase(LogicalAggregate<? extends Plan> logicalAgg
}
);
AggregateParam param = new AggregateParam(AggPhase.GLOBAL, AggMode.INPUT_TO_RESULT, !skipRegulator(logicalAgg));
return ImmutableList.of(new PhysicalHashAggregate<>(logicalAgg.getGroupByExpressions(), aggOutput, param,
return ImmutableList.of(new PhysicalHashAggregate<>(logicalAgg.getGroupByExpressions(), aggOutput,
logicalAgg.getPartitionExpressions(), param,
AggregateUtils.maybeUsingStreamAgg(logicalAgg.getGroupByExpressions(), param),
null, logicalAgg.child()));
}
Expand Down Expand Up @@ -159,7 +160,7 @@ public Void visitSessionVarGuardExpr(SessionVarGuardExpr expr, Map<String, Strin
return new AggregateExpression(aggFunc, bufferToResultParam, alias.toSlot());
});
return ImmutableList.of(new PhysicalHashAggregate<>(aggregate.getGroupByExpressions(),
globalAggOutput, bufferToResultParam,
globalAggOutput, aggregate.getPartitionExpressions(), bufferToResultParam,
AggregateUtils.maybeUsingStreamAgg(aggregate.getGroupByExpressions(), bufferToResultParam),
aggregate.getLogicalProperties(), localAgg));
}
Expand Down
Loading
Loading