-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
[Enhancement] eliminate constant cte #53286
base: main
Are you sure you want to change the base?
[Enhancement] eliminate constant cte #53286
Conversation
Signed-off-by: stephen <[email protected]>
return true; | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The most risky bug in this code is:
The transformation logic assumes all children of the CTE are leaves, but if a non-leaf is encountered, it prematurely stops by returning only direct inputs instead of transforming deeper structures. This can skip necessary transformations on nested structures.
You can modify the code like this:
@Override
public List<OptExpression> transform(OptExpression input, OptimizerContext context) {
return flattenCTE(input);
}
private List<OptExpression> flattenCTE(OptExpression root) {
List<OptExpression> flattened = new ArrayList<>();
for (OptExpression optExpression : root.getInputs()) {
if (areAllLeafNodesConstants(optExpression)) {
flattened.addAll(optExpression.getInputs());
} else {
flattened.add(optExpression);
}
}
return flattened;
}
This modifies the transform
method to directly flatten CTEs that consist entirely of constant expressions by further expanding into their inputs while preserving the structure when not all nodes are constants.
[FE Incremental Coverage Report]✅ pass : 1 / 1 (100.00%) file detail
|
Quality Gate passedIssues Measures |
[Java-Extensions Incremental Coverage Report]✅ pass : 0 / 0 (0%) |
[BE Incremental Coverage Report]✅ pass : 0 / 0 (0%) |
Why I'm doing:
If all leaf nodes of cte are constant, we can directly inline this cte.After inline, we can perform more optimizations, such as
with cte as (select 111) select * from cte join (select * from (select * from t1 join cte) s1) s2;
we can eliminate join node by EliminateJoinWithConstantRule to prevent error join order.
What I'm doing:
Fixes #issue
What type of PR is this:
Does this PR entail a change in behavior?
If yes, please specify the type of change:
Checklist:
Bugfix cherry-pick branch check: