[fix](Nereids): fill up miss slot of order having project (#27480)

fill up miss slot of order having project such as 
```
select a + 1 as c from t having by c > 2 order by a 
```
This commit is contained in:
谢健
2023-11-27 16:00:29 +08:00
committed by GitHub
parent 612347f650
commit cbdb886b6e
3 changed files with 28 additions and 0 deletions

View File

@ -65,6 +65,7 @@ public enum RuleType {
FILL_UP_HAVING_AGGREGATE(RuleTypeClass.REWRITE),
FILL_UP_HAVING_PROJECT(RuleTypeClass.REWRITE),
FILL_UP_SORT_AGGREGATE(RuleTypeClass.REWRITE),
FILL_UP_SORT_HAVING_PROJECT(RuleTypeClass.REWRITE),
FILL_UP_SORT_HAVING_AGGREGATE(RuleTypeClass.REWRITE),
FILL_UP_SORT_PROJECT(RuleTypeClass.REWRITE),

View File

@ -120,6 +120,26 @@ public class FillUpMissingSlots implements AnalysisRuleFactory {
});
})
),
RuleType.FILL_UP_SORT_HAVING_PROJECT.build(
logicalSort(logicalHaving(logicalProject())).then(sort -> {
Set<Slot> childOutput = sort.child().getOutputSet();
Set<Slot> notExistedInProject = sort.getOrderKeys().stream()
.map(OrderKey::getExpr)
.map(Expression::getInputSlots)
.flatMap(Set::stream)
.filter(s -> !childOutput.contains(s))
.collect(Collectors.toSet());
if (notExistedInProject.size() == 0) {
return null;
}
LogicalProject<?> project = sort.child().child();
List<NamedExpression> projects = ImmutableList.<NamedExpression>builder()
.addAll(project.getProjects())
.addAll(notExistedInProject).build();
Plan child = sort.withChildren(sort.child().withChildren(project.withProjects(projects)));
return new LogicalProject<>(ImmutableList.copyOf(project.getOutput()), child);
})
),
RuleType.FILL_UP_HAVING_AGGREGATE.build(
logicalHaving(aggregate()).then(having -> {
Aggregate<Plan> agg = having.child();