924060929 commented on code in PR #32758: URL: https://github.com/apache/doris/pull/32758#discussion_r1539158079
########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindExpression.java: ########## @@ -763,9 +763,38 @@ private Plan bindSortWithoutSetOperation(MatchingContext<LogicalSort<Plan>> ctx) return self.bindExactSlotsByThisScope(unboundSlot, inputChildrenScope.get()); }); + SimpleExprAnalyzer bindInInputChildScope = buildCustomSlotBinderAnalyzer( + sort, cascadesContext, inputScope, true, false, + (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()); + FunctionRegistry functionRegistry = cascadesContext.getConnectContext().getEnv().getFunctionRegistry(); for (OrderKey orderKey : sort.getOrderKeys()) { - Expression boundKey = analyzer.analyze(orderKey.getExpr()); + Expression boundKey; + if (hasAggregateFunction(orderKey.getExpr(), functionRegistry)) { + boundKey = bindInInputChildScope.analyze(orderKey.getExpr()); + } else { + boundKey = bindInInputScopeThenInputChildScope.analyze(orderKey.getExpr()); + } Review Comment: we should bind bindInInputChildScope when **in** aggregate function, not equals to **has** aggregate function, see `bindHavingAggregate`, there has a visitor to do this function -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org