From 6b47b8798d240979ade7c65b65d4b0cce7f73613 Mon Sep 17 00:00:00 2001 From: feiniaofeiafei Date: Tue, 26 Mar 2024 16:30:51 +0800 Subject: [PATCH] [Fix](nereids) modify the binding aggregate function in order by --- .../rules/analysis/BindExpression.java | 21 +++++++++++++++++-- .../analysis/FillUpMissingSlotsTest.java | 2 +- .../order_by_bind_priority.out | 6 ++++++ .../order_by_bind_priority.groovy | 1 + 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindExpression.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindExpression.java index a77a140fbd43069..edb878fc92810ba 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindExpression.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindExpression.java @@ -765,8 +765,25 @@ private Plan bindSortWithoutSetOperation(MatchingContext> ctx) SimpleExprAnalyzer bindInInputChildScope = buildCustomSlotBinderAnalyzer( sort, cascadesContext, inputScope, true, false, - (self, unboundSlot) -> { - return self.bindExactSlotsByThisScope(unboundSlot, inputChildrenScope.get()); + (analyzer, unboundSlot) -> { + if (finalInput instanceof LogicalAggregate) { + LogicalAggregate aggregate = (LogicalAggregate) finalInput; + List outputExpressions = aggregate.getOutputExpressions(); + ImmutableList.Builder 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 boundInOutputWithoutAggFunc = analyzer.bindSlotByScope(unboundSlot, + outputWithoutAggFunc); + if (!boundInOutputWithoutAggFunc.isEmpty()) { + return ImmutableList.of(boundInOutputWithoutAggFunc.get(0)); + } + } + return analyzer.bindExactSlotsByThisScope(unboundSlot, inputChildrenScope.get()); }); Builder boundOrderKeys = ImmutableList.builderWithExpectedSize(sort.getOrderKeys().size()); diff --git a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/analysis/FillUpMissingSlotsTest.java b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/analysis/FillUpMissingSlotsTest.java index 49793fd18425da5..3502552e108ed20 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/analysis/FillUpMissingSlotsTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/analysis/FillUpMissingSlotsTest.java @@ -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" )); diff --git a/regression-test/data/nereids_syntax_p0/order_by_bind_priority.out b/regression-test/data/nereids_syntax_p0/order_by_bind_priority.out index 5c9a49fccc68155..3c7dcb1d31844c4 100644 --- a/regression-test/data/nereids_syntax_p0/order_by_bind_priority.out +++ b/regression-test/data/nereids_syntax_p0/order_by_bind_priority.out @@ -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 + diff --git a/regression-test/suites/nereids_syntax_p0/order_by_bind_priority.groovy b/regression-test/suites/nereids_syntax_p0/order_by_bind_priority.groovy index 5546202bb99b112..e434ab42092948f 100644 --- a/regression-test/suites/nereids_syntax_p0/order_by_bind_priority.groovy +++ b/regression-test/suites/nereids_syntax_p0/order_by_bind_priority.groovy @@ -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."