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

Replace parallel condition/result vectors with single CaseWhen vector in Expr::Case #1733

Merged
merged 1 commit into from
Feb 22, 2025

Conversation

lovasoa
Copy link
Contributor

@lovasoa lovasoa commented Feb 20, 2025

The primary motivation for this change is to fix the visitor traversal order for CASE expressions. In SQL, CASE expressions follow a specific syntactic order (e.g., CASE a WHEN 1 THEN 2 WHEN 3 THEN 4 ELSE 5), AST visitors now process nodes in the same order as they appear in the source code. The previous implementation, using separate conditions and results vectors, would visit all conditions first and then all results, which didn't match the source order. The new CaseWhen structure ensures visitors process expressions in the correct order: a,1,2,3,4,5.

A secondary benefit is making invalid states unrepresentable in the type system. The previous implementation using parallel vectors (conditions and results) made it possible to create invalid CASE expressions where the number of conditions didn't match the number of results. When this happened, the Display implementation would silently drop elements from the longer list, potentially masking bugs. The new CaseWhen struct couples each condition with its result, making it impossible to create such mismatched states.

While this is a breaking change to the AST structure, sqlparser has a history of making such changes when they improve correctness. I don't expect significant downstream breakages, and the benefits of correct visitor ordering and type safety are significant, so I think the trade-off is worthwhile.

@lovasoa lovasoa force-pushed the case_representation branch 2 times, most recently from 0503160 to afd8e6e Compare February 20, 2025 16:38
@lovasoa
Copy link
Contributor Author

lovasoa commented Feb 20, 2025

fixes sqlpage/SQLPage#818

@lovasoa lovasoa force-pushed the case_representation branch from afd8e6e to 6b00c3b Compare February 20, 2025 16:39
… in Expr::Case

The primary motivation for this change is to fix the visitor traversal order for CASE expressions. In SQL, CASE expressions follow a specific syntactic order (e.g., `CASE a WHEN 1 THEN 2 WHEN 3 THEN 4 ELSE 5`), AST visitors now process nodes in the same order as they appear in the source code. The previous implementation, using separate `conditions` and `results` vectors, would visit all conditions first and then all results, which didn't match the source order. The new `CaseWhen` structure ensures visitors process expressions in the correct order: `a,1,2,3,4,5`.

A secondary benefit is making invalid states unrepresentable in the type system. The previous implementation using parallel vectors (`conditions` and `results`) made it possible to create invalid CASE expressions where the number of conditions didn't match the number of results. When this happened, the `Display` implementation would silently drop elements from the longer list, potentially masking bugs. The new `CaseWhen` struct couples each condition with its result, making it impossible to create such mismatched states.

While this is a breaking change to the AST structure, sqlparser has a history of making such changes when they improve correctness. I don't expect significant downstream breakages, and the benefits of correct visitor ordering and type safety are significant, so I think the trade-off is worthwhile.
@lovasoa lovasoa force-pushed the case_representation branch from 6b00c3b to 04732cf Compare February 21, 2025 16:45
Copy link
Contributor

@iffyio iffyio left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks @lovasoa!
cc @alamb

@iffyio iffyio merged commit 72312ba into apache:main Feb 22, 2025
9 checks passed
@lovasoa lovasoa deleted the case_representation branch February 22, 2025 13:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants