morrySnow commented on code in PR #15240:
URL: https://github.com/apache/doris/pull/15240#discussion_r1058913444


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindSlotReference.java:
##########
@@ -373,13 +374,45 @@ private Plan bindSortWithAggregateFunction(
             LogicalSort<? extends Plan> sort, Aggregate<? extends Plan> 
aggregate, CascadesContext ctx) {
         // We should deduplicate the slots, otherwise the binding process will 
fail due to the
         // ambiguous slots exist.
-        Set<Slot> boundSlots = Stream.concat(Stream.of(aggregate), 
aggregate.children().stream())
-                .flatMap(plan -> plan.getOutput().stream())
-                .collect(Collectors.toSet());
+        //Set<Slot> boundSlots = Stream.concat(Stream.of(aggregate), 
aggregate.children().stream())
+        //        .flatMap(plan -> plan.getOutput().stream())
+        //        .collect(Collectors.toSet());
+        //List<OrderKey> sortItemList = sort.getOrderKeys()
+        //        .stream()
+        //        .map(orderKey -> {
+        //            Expression item = new SlotBinder(toScope(new 
ArrayList<>(boundSlots)), sort, ctx)
+        //                    .bind(orderKey.getExpr());
+        //            return new OrderKey(item, orderKey.isAsc(), 
orderKey.isNullFirst());
+        //        }).collect(Collectors.toList());
+        //return new LogicalSort<>(sortItemList, sort.child());

Review Comment:
   remove useless comments



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindSlotReference.java:
##########
@@ -373,13 +374,45 @@ private Plan bindSortWithAggregateFunction(
             LogicalSort<? extends Plan> sort, Aggregate<? extends Plan> 
aggregate, CascadesContext ctx) {
         // We should deduplicate the slots, otherwise the binding process will 
fail due to the
         // ambiguous slots exist.
-        Set<Slot> boundSlots = Stream.concat(Stream.of(aggregate), 
aggregate.children().stream())
-                .flatMap(plan -> plan.getOutput().stream())
-                .collect(Collectors.toSet());
+        //Set<Slot> boundSlots = Stream.concat(Stream.of(aggregate), 
aggregate.children().stream())
+        //        .flatMap(plan -> plan.getOutput().stream())
+        //        .collect(Collectors.toSet());
+        //List<OrderKey> sortItemList = sort.getOrderKeys()
+        //        .stream()
+        //        .map(orderKey -> {
+        //            Expression item = new SlotBinder(toScope(new 
ArrayList<>(boundSlots)), sort, ctx)
+        //                    .bind(orderKey.getExpr());
+        //            return new OrderKey(item, orderKey.isAsc(), 
orderKey.isNullFirst());
+        //        }).collect(Collectors.toList());
+        //return new LogicalSort<>(sortItemList, sort.child());
+        //try bound order key with agg output, if failed, try to bound with 
output of agg.child
+        //example:
+        //        select
+        //        col1 * -1 as col1    # inner_col1 * -1 as alias_col1
+        //        from
+        //                (
+        //                        select 1 as col1
+        //                        union
+        //                        select -2 as col1
+        //                ) t
+        //        group by col1
+        //        order by col1;     # order by order_col1
+        // bind order_col1 with alias_col1, then, bind it with inner_col1
+        List<Slot> boundSlots = Lists.newArrayList();
+        Set<String> slotNames = new HashSet<>();
+
+        Stream.concat(Stream.of(aggregate), aggregate.children().stream())
+                .flatMap(plan -> plan.getOutput().stream()).forEach(slot -> {
+                    if (!slotNames.contains(slot.getName())) {
+                        slotNames.add(slot.getName());
+                        boundSlots.add(slot);
+                    }
+                });

Review Comment:
   we should not rely on the item order in stream api. instead, the better way 
is bind two round using difference scopes. the first round use aggregate output 
and the second round use aggregate's child if still has unbound slot in 
orderkeys



-- 
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

Reply via email to