[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);