Replace parallel condition/result vectors with single CaseWhen vector in Expr::Case #1733
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 separateconditions
andresults
vectors, would visit all conditions first and then all results, which didn't match the source order. The newCaseWhen
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
andresults
) made it possible to create invalid CASE expressions where the number of conditions didn't match the number of results. When this happened, theDisplay
implementation would silently drop elements from the longer list, potentially masking bugs. The newCaseWhen
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.