You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by mo...@apache.org on 2023/01/19 07:36:20 UTC

[doris] branch master updated: [fix](nereids) Fix bind failed of the slots in the group by clause (#16077)

This is an automated email from the ASF dual-hosted git repository.

morrysnow pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new 21b78cb820 [fix](nereids) Fix bind failed of the slots in the group by clause (#16077)
21b78cb820 is described below

commit 21b78cb8202a02a6df864dcf2ed56cdbd456ecb4
Author: AKIRA <33...@users.noreply.github.com>
AuthorDate: Thu Jan 19 15:36:13 2023 +0800

    [fix](nereids) Fix bind failed of the slots in the group by clause (#16077)
    
    Child's slot with same name to the slots in the outputexpression would be discarded which would cause the bind failed, since the slots in the group by expressions cannot find the corresponding bound slots from the child's output
---
 .../nereids/rules/analysis/BindSlotReference.java  | 32 ++++++----
 .../data/nereids_syntax_p0/analyze_agg.out         |  3 +
 .../suites/nereids_syntax_p0/analyze_agg.groovy    | 71 ++++++++++++++++++++++
 3 files changed, 95 insertions(+), 11 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 ad02e542c9..97458c28d2 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
@@ -85,6 +85,7 @@ import java.util.Map;
 import java.util.Objects;
 import java.util.Optional;
 import java.util.Set;
+import java.util.function.Predicate;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
 
@@ -294,21 +295,30 @@ public class BindSlotReference implements AnalysisRuleFactory {
 
                     boundSlots.addAll(outputSlots);
                     SlotBinder binder = new SlotBinder(toScope(Lists.newArrayList(boundSlots)), ctx.cascadesContext);
+                    SlotBinder childBinder
+                            = new SlotBinder(toScope(new ArrayList<>(agg.child().getOutputSet())), ctx.cascadesContext);
 
                     List<Expression> groupBy = replacedGroupBy.stream()
-                            .map(expression -> binder.bind(expression))
+                            .map(expression -> {
+                                Expression e = binder.bind(expression);
+                                if (e instanceof UnboundSlot) {
+                                    return childBinder.bind(e);
+                                }
+                                return e;
+                            })
                             .collect(Collectors.toList());
-
                     List<Expression> unboundGroupBys = Lists.newArrayList();
-                    boolean hasUnbound = groupBy.stream().anyMatch(
-                            expression -> {
-                                if (expression.anyMatch(UnboundSlot.class::isInstance)) {
-                                    unboundGroupBys.add(expression);
-                                    return true;
-                                }
-                                return false;
-                            });
-                    if (hasUnbound) {
+                    Predicate<List<Expression>> hasUnBound = (exprs) -> {
+                        return exprs.stream().anyMatch(
+                                expression -> {
+                                    if (expression.anyMatch(UnboundSlot.class::isInstance)) {
+                                        unboundGroupBys.add(expression);
+                                        return true;
+                                    }
+                                    return false;
+                                });
+                    };
+                    if (hasUnBound.test(groupBy)) {
                         throw new AnalysisException("cannot bind GROUP BY KEY: " + unboundGroupBys.get(0).toSql());
                     }
                     List<NamedExpression> newOutput = adjustNullableForAgg(agg, output);
diff --git a/regression-test/data/nereids_syntax_p0/analyze_agg.out b/regression-test/data/nereids_syntax_p0/analyze_agg.out
new file mode 100644
index 0000000000..9c9c4c6c8a
--- /dev/null
+++ b/regression-test/data/nereids_syntax_p0/analyze_agg.out
@@ -0,0 +1,3 @@
+-- This file is automatically generated. You should know what you did if you want to edit this
+-- !sql --
+
diff --git a/regression-test/suites/nereids_syntax_p0/analyze_agg.groovy b/regression-test/suites/nereids_syntax_p0/analyze_agg.groovy
new file mode 100644
index 0000000000..e5184234f3
--- /dev/null
+++ b/regression-test/suites/nereids_syntax_p0/analyze_agg.groovy
@@ -0,0 +1,71 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+suite("analyze_agg") {
+    sql """DROP TABLE IF EXISTS t1"""
+    sql """DROP TABLE IF EXISTS t2"""
+
+    sql """SET enable_fallback_to_original_planner=false"""
+    sql """SET enable_nereids_planner=true"""
+
+    sql """    
+        create table t1
+        (
+           id INT,
+           a VARCHAR(32)
+        )ENGINE = OLAP
+        UNIQUE KEY(id)
+        DISTRIBUTED BY HASH(id) BUCKETS 30
+        PROPERTIES (
+            "replication_allocation" = "tag.location.default: 1"
+        );
+    """
+    sql """
+        create table t2
+        (
+            id INT,
+            b VARCHAR(30),
+            c INT default '0',
+            d VARCHAR(30),
+            e VARCHAR(32),
+            a VARCHAR(32),
+            f VARCHAR(32)
+        )ENGINE = OLAP
+        UNIQUE KEY(id)
+        DISTRIBUTED BY HASH(id) BUCKETS 30
+        PROPERTIES (
+            "replication_allocation" = "tag.location.default: 1"
+        );
+    """
+    qt_sql """
+        
+        SELECT
+               tt.d,
+               tt2.c
+        FROM  t1 t
+                 LEFT JOIN t2 tt
+                           ON tt.f = t.a
+                               and tt.b = 'EA'
+                 left join t2 tt2
+                           on tt2.f = t.a
+                               and tt2.b = 'CS'
+        group by
+                 tt.d,
+                 tt2.d,
+                 tt2.c;
+    """
+}
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org