Skip to content

Commit

Permalink
[Fix](nereids) modify the binding aggregate function in order by
Browse files Browse the repository at this point in the history
  • Loading branch information
feiniaofeiafei committed Mar 26, 2024
1 parent b099b75 commit 6b47b87
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -765,8 +765,25 @@ private Plan bindSortWithoutSetOperation(MatchingContext<LogicalSort<Plan>> ctx)

SimpleExprAnalyzer bindInInputChildScope = buildCustomSlotBinderAnalyzer(
sort, cascadesContext, inputScope, true, false,
(self, unboundSlot) -> {
return self.bindExactSlotsByThisScope(unboundSlot, inputChildrenScope.get());
(analyzer, unboundSlot) -> {
if (finalInput instanceof LogicalAggregate) {
LogicalAggregate<Plan> aggregate = (LogicalAggregate<Plan>) finalInput;
List<NamedExpression> outputExpressions = aggregate.getOutputExpressions();
ImmutableList.Builder<Slot> outputSlots = ImmutableList.builderWithExpectedSize(
outputExpressions.size());
for (NamedExpression outputExpr : outputExpressions) {
if (!outputExpr.anyMatch(expr -> expr instanceof AggregateFunction)) {
outputSlots.add(outputExpr.toSlot());
}
}
Scope outputWithoutAggFunc = toScope(cascadesContext, outputSlots.build());
List<Slot> boundInOutputWithoutAggFunc = analyzer.bindSlotByScope(unboundSlot,
outputWithoutAggFunc);
if (!boundInOutputWithoutAggFunc.isEmpty()) {
return ImmutableList.of(boundInOutputWithoutAggFunc.get(0));
}
}
return analyzer.bindExactSlotsByThisScope(unboundSlot, inputChildrenScope.get());
});

Builder<OrderKey> boundOrderKeys = ImmutableList.builderWithExpectedSize(sort.getOrderKeys().size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ void testJoinWithHaving() {
void testInvalidHaving() {
ExceptionChecker.expectThrowsWithMsg(
AnalysisException.class,
"a2 in having clause should be grouped by.",
"a2 should be grouped by.",
() -> PlanChecker.from(connectContext).analyze(
"SELECT a1 FROM t1 GROUP BY a1 HAVING a2 > 0"
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@
2 1 4
3 3 3

-- !test_bind_order_by_in_no_agg_func_output --
1 4
2 -2
3 3
5 5

Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ suite("order_by_bind_priority") {
qt_test_bind_order_by_with_aggfun1 "select 2*abs(sum(c1)) as c1, c1,sum(c1)+c1 from t_order_by_bind_priority group by c1 order by sum(c1)+c1 asc;"
qt_test_bind_order_by_with_aggfun2 "select 2*abs(sum(c1)) as c2, c1,sum(c1)+c2 from t_order_by_bind_priority group by c1,c2 order by sum(c1)+c2 asc;"
qt_test_bind_order_by_with_aggfun3 "select abs(sum(c1)) as c1, c1,sum(c2) as c2 from t_order_by_bind_priority group by c1 order by sum(c1) asc;"
qt_test_bind_order_by_in_no_agg_func_output "select abs(c1) xx, sum(c2) from t_order_by_bind_priority group by xx order by min(xx)"
test {
sql "select abs(sum(c1)) as c1, c1,sum(c2) as c2 from t_order_by_bind_priority group by c1 order by sum(c1)+c2 asc;"
exception "c2 should be grouped by."
Expand Down

0 comments on commit 6b47b87

Please sign in to comment.