Skip to content

Commit

Permalink
[fix](nereids)FillUpMissingSlots rule didn't process standalone havin…
Browse files Browse the repository at this point in the history
…g clause correctly (apache#29143)
  • Loading branch information
starocean999 authored Dec 29, 2023
1 parent c3c34e1 commit 9925f7b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,25 @@ public List<Rule> buildRules() {
})
),
// Convert having to filter
RuleType.FILL_UP_HAVING_PROJECT.build(
logicalHaving(logicalProject()).then(having -> {
LogicalProject<Plan> project = having.child();
Set<Slot> projectOutputSet = project.getOutputSet();
Set<Slot> notExistedInProject = having.getExpressions().stream()
.map(Expression::getInputSlots)
.flatMap(Set::stream)
.filter(s -> !projectOutputSet.contains(s))
.collect(Collectors.toSet());
if (notExistedInProject.size() == 0) {
return null;
}
List<NamedExpression> projects = ImmutableList.<NamedExpression>builder()
.addAll(project.getProjects()).addAll(notExistedInProject).build();
return new LogicalProject<>(ImmutableList.copyOf(project.getOutput()),
having.withChildren(new LogicalProject<>(projects, project.child())));
})
),
// Convert having to filter
RuleType.FILL_UP_HAVING_PROJECT.build(
logicalHaving().then(having -> new LogicalFilter<>(having.getConjuncts(), having.child()))
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,10 +329,12 @@ private static boolean canCombine(Plan input, boolean changeChildren) {
Set<Expression> joinFilter, Set<Integer> usedPlansIndex, Map<Plan, JoinHintType> planToHintType) {
List<Expression> otherJoinConditions = Lists.newArrayList();
Set<ExprId> leftOutputExprIdSet = left.getOutputExprIdSet();
int candidateIndex = 0;
for (int i = 0; i < candidates.size(); i++) {
if (usedPlansIndex.contains(i)) {
continue;
}
candidateIndex = i;

Plan candidate = candidates.get(i);
Set<ExprId> rightOutputExprIdSet = candidate.getOutputExprIdSet();
Expand Down Expand Up @@ -362,21 +364,14 @@ private static boolean canCombine(Plan input, boolean changeChildren) {
}
// All { left -> one in [candidates] } is CrossJoin
// Generate a CrossJoin
for (int i = 0; i < candidates.size(); i++) {
if (usedPlansIndex.contains(i)) {
continue;
}
usedPlansIndex.add(i);
Plan right = candidates.get(i);
return new LogicalJoin<>(JoinType.CROSS_JOIN,
ExpressionUtils.EMPTY_CONDITION,
otherJoinConditions,
JoinHint.fromRightPlanHintType(planToHintType.getOrDefault(right, JoinHintType.NONE)),
Optional.empty(),
left, right);
}

throw new RuntimeException("findInnerJoin: can't reach here");
usedPlansIndex.add(candidateIndex);
Plan right = candidates.get(candidateIndex);
return new LogicalJoin<>(JoinType.CROSS_JOIN,
ExpressionUtils.EMPTY_CONDITION,
otherJoinConditions,
JoinHint.fromRightPlanHintType(planToHintType.getOrDefault(right, JoinHintType.NONE)),
Optional.empty(),
left, right);
}

private boolean nonJoinAndNonFilter(Plan plan) {
Expand Down
8 changes: 8 additions & 0 deletions regression-test/suites/nereids_syntax_p0/having.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,12 @@ suite("test_nereids_having") {
order_qt_select "SELECT SUM(a1 + a2) FROM test_nereids_having_tbl HAVING SUM(a1 + a2) > 0";
order_qt_select "SELECT SUM(a1 + a2) FROM test_nereids_having_tbl HAVING SUM(a1 + a2 + 3) > 0";
order_qt_select "SELECT COUNT(*) FROM test_nereids_having_tbl HAVING COUNT(*) > 0";
sql """SELECT alias2.`pk` AS field4
FROM
(SELECT pk
FROM test_nereids_having_tbl AS SQ1_alias1 ) AS alias2
HAVING alias2.`pk` <>
(SELECT *
FROM
(SELECT "xAbfcUSAOy") __DORIS_DUAL__ );"""
}

0 comments on commit 9925f7b

Please sign in to comment.