Skip to content
Open
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 @@ -258,7 +258,7 @@ private LogicalPlan normalizeAgg(LogicalAggregate<Plan> aggregate, Optional<Logi
List<Expression> normalizedAggFuncs =
bottomSlotContext.normalizeToUseSlotRef(SessionVarGuardExpr.getExprWithGuard(aggFuncs));
if (normalizedAggFuncs.stream().anyMatch(agg -> !agg.children().isEmpty()
&& agg.child(0).containsType(AggregateFunction.class))) {
&& agg.children().stream().anyMatch(e -> e.containsType(AggregateFunction.class)))) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check still runs too late for children that were put into needPushDownSelfExprs. For example, group_concat(distinct k order by sum(k)) unwraps the order key in the distinct branch and maps sum(k) to a bottom-project alias slot before this block runs; similarly, count(distinct sum(k)) or an order key like sum(k) + row_number() over(...) can be replaced by a slot first. After normalizeToUseSlotRef, the aggregate child no longer contains an AggregateFunction, so this check misses the nested aggregate and may only fail later as an aggregate pushed into the wrong plan node. Please validate the original aggregate children/order-key children for non-window aggregate functions before any slot normalization, and add coverage for the distinct and pushdown-triggered variants.

throw new AnalysisException(
"aggregate function cannot contain aggregate parameters");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

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

import org.apache.doris.nereids.exceptions.AnalysisException;
import org.apache.doris.nereids.trees.expressions.Add;
import org.apache.doris.nereids.trees.expressions.Alias;
import org.apache.doris.nereids.trees.expressions.ExprId;
Expand Down Expand Up @@ -736,6 +737,15 @@ public void testAggregateOrderByExpressionNeedPushDown() {
);
}

@Test
public void testAggregateOrderByExpressionCannotContainAggregateFunction() {
String sql = "select group_concat(k order by sum(k)) as s "
+ "from (select 1 as k union all select 2) t";
AnalysisException exception = Assertions.assertThrows(AnalysisException.class,
() -> PlanChecker.from(connectContext).analyze(sql));
Assertions.assertEquals("aggregate function cannot contain aggregate parameters", exception.getMessage());
}

@Test
public void testDistinctAggregateOrderByExpressionNeedPushDown() {
String sql = "select group_concat(distinct name order by id + no) from t1";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ suite("agg_group_concat") {
from (select 1 as k union all select 2) t;
"""

test {
sql """
select group_concat(k order by sum(k)) as s
from (select 1 as k union all select 2) t;
"""
exception "aggregate function cannot contain aggregate parameters"
}

order_qt_group_concat_order_by_subquery """
select group_concat(kint order by (select 1), kint) as s
from agg_group_concat_table;
Expand Down
Loading