From 21b78cb8202a02a6df864dcf2ed56cdbd456ecb4 Mon Sep 17 00:00:00 2001 From: AKIRA <33112463+Kikyou1997@users.noreply.github.com> Date: Thu, 19 Jan 2023 15:36:13 +0800 Subject: [PATCH] [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 --- .../rules/analysis/BindSlotReference.java | 34 +++++---- .../data/nereids_syntax_p0/analyze_agg.out | 3 + .../nereids_syntax_p0/analyze_agg.groovy | 71 +++++++++++++++++++ 3 files changed, 96 insertions(+), 12 deletions(-) create mode 100644 regression-test/data/nereids_syntax_p0/analyze_agg.out create mode 100644 regression-test/suites/nereids_syntax_p0/analyze_agg.groovy 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 groupBy = replacedGroupBy.stream() - .map(expression -> binder.bind(expression)) - .collect(Collectors.toList()); - - List unboundGroupBys = Lists.newArrayList(); - boolean hasUnbound = groupBy.stream().anyMatch( - expression -> { - if (expression.anyMatch(UnboundSlot.class::isInstance)) { - unboundGroupBys.add(expression); - return true; + .map(expression -> { + Expression e = binder.bind(expression); + if (e instanceof UnboundSlot) { + return childBinder.bind(e); } - return false; - }); - if (hasUnbound) { + return e; + }) + .collect(Collectors.toList()); + List unboundGroupBys = Lists.newArrayList(); + Predicate> 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 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