Skip to content

Fix: common_sub_expression_eliminate optimizer rule failed #16066

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Col-Waltz
Copy link

Which issue does this PR close?

Rationale for this change

Common_sub_expression_eliminate rule failed with error:
SchemaError(FieldNotFound {field: <name>}, valid_fields: []})
due to the schema being changed by the second application of
find_common_exprs

As I understood the source of the problem was in sequential call of
find_common_exprs. First call returned original names as aggr_expr
and changed names as new_aggr_expr. Second call takes into account
only new_aggr_expr and if names was already changed by first call
will return changed names as aggr_expr(original ones)
and put them into Projection logic.

What changes are included in this PR?

I used existing NamePreserver mechanism to restore original schema names and
generate Projection with original name at the end of aggregate optimization.

Are these changes tested?

Yes this changes are tested and I added test to this commit.
Error emerges not only in PREPARE requests, I created SELECT request which
causes the same error.

Are there any user-facing changes?

No the change only fixes optimization rule.

Common_sub_expression_eliminate rule failed with error:
`SchemaError(FieldNotFound {field: <name>}, valid_fields: []})`
due to the schema being changed by the second application of
`find_common_exprs`

As I understood the source of the problem was in sequential call of
`find_common_exprs`. First call returned original names as `aggr_expr`
and changed names as `new_aggr_expr`. Second call takes into account
only `new_aggr_expr` and if names was already changed by first call
will return changed names as `aggr_expr`(original ones)
and put them into Projection logic.

I used NamePreserver mechanism to restore original schema names and
generate Projection with original name at the end of aggregate
optimization.
@github-actions github-actions bot added optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt) labels May 16, 2025
.map(|expr| Some(name_preserver.save(expr)))
.collect::<Vec<_>>()
} else {
new_aggr_expr
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the same as a vec of all None?

vec![None; new_agg_expr.len()]`

Copy link
Author

@Col-Waltz Col-Waltz May 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it is, thanks for the comment. But this will not work, because vec! requires value to implement Clone trait but the Option<SavedName> doesn't. It seems to me easier to do this by map instead of adding trait to SavedName.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Failed optimizations with Int64 type
2 participants