[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
This commit is contained in:
AKIRA
2023-01-19 15:36:13 +08:00
committed by GitHub
parent 0144c51ddb
commit 21b78cb820
3 changed files with 96 additions and 12 deletions

View File

@ -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))
.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;
.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<Expression> unboundGroupBys = Lists.newArrayList();
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);

View File

@ -0,0 +1,3 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !sql --

View File

@ -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;
"""
}