Skip to content

Commit

Permalink
[fix](nereids)only push down subquery in non-window agg functions (#2…
Browse files Browse the repository at this point in the history
  • Loading branch information
starocean999 authored and xiaokang committed Nov 4, 2023
1 parent 071538c commit b387ac2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,20 +103,20 @@ public Rule build() {

List<NamedExpression> aggregateOutput = aggregate.getOutputExpressions();
Set<Alias> existsAlias = ExpressionUtils.mutableCollect(aggregateOutput, Alias.class::isInstance);
// we need push down subquery exprs in side non-distinct agg functions
Set<SubqueryExpr> subqueryExprs = ExpressionUtils.mutableCollect(
Lists.newArrayList(ExpressionUtils.mutableCollect(aggregateOutput,
expr -> expr instanceof AggregateFunction
&& !((AggregateFunction) expr).isDistinct())),

List<AggregateFunction> aggFuncs = Lists.newArrayList();
aggregateOutput.forEach(o -> o.accept(CollectNonWindowedAggFuncs.INSTANCE, aggFuncs));

// we need push down subquery exprs inside non-window and non-distinct agg functions
Set<SubqueryExpr> subqueryExprs = ExpressionUtils.mutableCollect(aggFuncs.stream()
.filter(aggFunc -> !aggFunc.isDistinct()).collect(Collectors.toList()),
SubqueryExpr.class::isInstance);
Set<Expression> groupingByExprs = ImmutableSet.copyOf(aggregate.getGroupByExpressions());
NormalizeToSlotContext bottomSlotContext =
NormalizeToSlotContext.buildContext(existsAlias, Sets.union(groupingByExprs, subqueryExprs));
Set<NamedExpression> bottomOutputs =
bottomSlotContext.pushDownToNamedExpression(Sets.union(groupingByExprs, subqueryExprs));

List<AggregateFunction> aggFuncs = Lists.newArrayList();
aggregateOutput.forEach(o -> o.accept(CollectNonWindowedAggFuncs.INSTANCE, aggFuncs));
// use group by context to normalize agg functions to process
// sql like: select sum(a + 1) from t group by a + 1
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,15 @@ true
-- !sql16 --
12

-- !sql17 --
12
12

-- !sql18 --
12
12

-- !sql20 --
5
7

Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,23 @@ suite("test_subquery_in_project") {
"""

qt_sql15 """
select sum(age + (select sum(age) from test_sql)) from test_sql;
select sum(age + (select sum(age) from test_sql)) from test_sql order by 1;
"""

qt_sql16 """
select sum(distinct age + (select sum(age) from test_sql)) from test_sql;
select sum(distinct age + (select sum(age) from test_sql)) from test_sql order by 1;
"""

qt_sql17 """
select sum(age + (select sum(age) from test_sql)) over() from test_sql order by 1;
"""

qt_sql18 """
select sum(age + (select sum(age) from test_sql)) over() from test_sql group by dt, age order by 1;
"""

qt_sql20 """
select sum(age + (select sum(age) from test_sql)) from test_sql group by dt, age order by 1;
"""

sql """drop table if exists test_sql;"""
Expand Down

0 comments on commit b387ac2

Please sign in to comment.