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 @@ -17,8 +17,10 @@

package org.apache.doris.nereids.rules.rewrite;

import org.apache.doris.nereids.annotation.DependsRules;
import org.apache.doris.nereids.rules.Rule;
import org.apache.doris.nereids.rules.RuleType;
import org.apache.doris.nereids.rules.analysis.NormalizeAggregate;
import org.apache.doris.nereids.trees.expressions.Alias;
import org.apache.doris.nereids.trees.expressions.ExprId;
import org.apache.doris.nereids.trees.expressions.Expression;
Expand All @@ -44,6 +46,9 @@
import java.util.stream.Collectors;

/**MergeAggregate*/
@DependsRules({
NormalizeAggregate.class
})
public class MergeAggregate implements RewriteRuleFactory {
private static final ImmutableSet<String> ALLOW_MERGE_AGGREGATE_FUNCTIONS =
ImmutableSet.of("min", "max", "sum", "any_value");
Expand Down Expand Up @@ -108,10 +113,17 @@ private Plan mergeAggProjectAgg(LogicalAggregate<LogicalProject<LogicalAggregate
.withChildren(innerAgg.children());

// construct upper project
Map<SlotReference, Alias> childToAlias = project.getProjects().stream()
.filter(expr -> (expr instanceof Alias) && (expr.child(0) instanceof SlotReference))
.collect(Collectors.toMap(alias -> (SlotReference) alias.child(0), alias -> (Alias) alias));
List<Expression> projectGroupBy = ExpressionUtils.replace(replacedGroupBy, childToAlias);
Map<ExprId, NamedExpression> exprIdToNameExpressionMap = new HashMap<>();
for (NamedExpression pro : project.getProjects()) {
exprIdToNameExpressionMap.put(pro.getExprId(), pro);
}
List<Expression> originOuterAggGroupBy = outerAgg.getGroupByExpressions();
List<Expression> projectGroupBy = new ArrayList<>();
for (Expression expression : originOuterAggGroupBy) {
ExprId exprId = ((NamedExpression) expression).getExprId();
NamedExpression namedExpression = exprIdToNameExpressionMap.get(exprId);
projectGroupBy.add(namedExpression);
}
List<NamedExpression> upperProjects = ImmutableList.<NamedExpression>builder()
.addAll(projectGroupBy.stream().map(namedExpr -> (NamedExpression) namedExpr).iterator())
.addAll(replacedAggFunc.stream().map(expr -> ((NamedExpression) expr).toSlot()).iterator())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,3 +297,12 @@ PhysicalResultSink
------hashAgg[LOCAL]
--------PhysicalOlapScan[mal_test2]

-- !agg_project_agg_the_project_has_duplicate_slot_output --
1 7 7
2 4 4
6 \N \N
7 1 1
8 2 2
8 5 5
9 3 3

Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,10 @@ suite("merge_aggregate") {
explain shape plan
select sum(col1),min(col2),max(col3) from (select sum(a) col1, min(b) col2, max(pk) col3 from mal_test2 group by a) t;
"""

qt_agg_project_agg_the_project_has_duplicate_slot_output """
select max(col1), col10, col11 from
(select a,max(b) as col1, count(b) as col4, a as col10, a as col11
from mal_test1 group by a) t group by col10, col11 order by 1,2,3;
"""
}