Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix](nereids)FillUpMissingSlots rule didn't process standalone having clause correctly #29317

Merged
merged 1 commit into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,24 @@ public List<Rule> buildRules() {
});
})
),
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 @@ -328,10 +328,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 @@ -361,21 +363,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__ );"""
}
Loading