From 6583e49b1b4ef61806c0d5755ea917b48357dcff Mon Sep 17 00:00:00 2001 From: jianghaochen Date: Thu, 29 Dec 2022 18:35:55 +0800 Subject: [PATCH] [Fix](Nereids) Group by binding should be consistent with the behavior of the old optimizer --- .../nereids/rules/analysis/BindSlotReference.java | 13 +++++-------- .../doris/nereids/trees/plans/GroupingSetsTest.java | 6 ++++++ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindSlotReference.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindSlotReference.java index 003967757bd0d5..efef4df8e8d044 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindSlotReference.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindSlotReference.java @@ -192,16 +192,13 @@ public List buildRules() { List output = bind(agg.getOutputExpressions(), agg.children(), agg, ctx.cascadesContext); - // The columns referenced in group by are first obtained from the child's output, - // and then from the node's output - Map childOutputsToExpr = agg.child().getOutput().stream() - .collect(Collectors.toMap(Slot::getName, Slot::toSlot, (oldExpr, newExpr) -> oldExpr)); + // This is consistent with the behavior of the old optimizer. + // TODO:It will be changed to the SQL standard in the future, + // first obtained from the child, and then obtained from the output of the operator. Map aliasNameToExpr = output.stream() .filter(ne -> ne instanceof Alias) .map(Alias.class::cast) .collect(Collectors.toMap(Alias::getName, UnaryNode::child, (oldExpr, newExpr) -> oldExpr)); - aliasNameToExpr.entrySet().stream() - .forEach(e -> childOutputsToExpr.putIfAbsent(e.getKey(), e.getValue())); List replacedGroupBy = agg.getGroupByExpressions().stream() .map(groupBy -> { @@ -209,8 +206,8 @@ public List buildRules() { UnboundSlot unboundSlot = (UnboundSlot) groupBy; if (unboundSlot.getNameParts().size() == 1) { String name = unboundSlot.getNameParts().get(0); - if (childOutputsToExpr.containsKey(name)) { - return childOutputsToExpr.get(name); + if (aliasNameToExpr.containsKey(name)) { + return aliasNameToExpr.get(name); } } } diff --git a/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/plans/GroupingSetsTest.java b/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/plans/GroupingSetsTest.java index a30364b1162fd6..ef6837e8ae7576 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/plans/GroupingSetsTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/plans/GroupingSetsTest.java @@ -232,4 +232,10 @@ public void test2_3() { .checkPlannerResult("select if(col1 = null, 'all', 2) as col2, count(*) as cnt from" + " (select null as col1 union all select 'a' as col1 ) t group by grouping sets ((col2),());"); } + + @Test + public void testGroup() { + PlanChecker.from(connectContext) + .checkPlannerResult("select b.k2 as k2 from t1 a left join (select k2 from t1) b on a.k2 = b.k2 group by k2"); + } }