Commit 6b00c3b
committed
Replace parallel condition/result vectors with single CaseWhen vector 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.1 parent 97f0be6 commit 6b00c3b
File tree
5 files changed
+160
-50
lines changed- src
- ast
- parser
- tests
5 files changed
+160
-50
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
599 | 599 | | |
600 | 600 | | |
601 | 601 | | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
602 | 618 | | |
603 | 619 | | |
604 | 620 | | |
| |||
917 | 933 | | |
918 | 934 | | |
919 | 935 | | |
920 | | - | |
921 | | - | |
| 936 | + | |
922 | 937 | | |
923 | 938 | | |
924 | 939 | | |
| |||
1612 | 1627 | | |
1613 | 1628 | | |
1614 | 1629 | | |
1615 | | - | |
1616 | 1630 | | |
1617 | 1631 | | |
1618 | 1632 | | |
1619 | 1633 | | |
1620 | 1634 | | |
1621 | 1635 | | |
1622 | | - | |
1623 | | - | |
| 1636 | + | |
| 1637 | + | |
1624 | 1638 | | |
1625 | | - | |
1626 | 1639 | | |
1627 | 1640 | | |
1628 | 1641 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1444 | 1444 | | |
1445 | 1445 | | |
1446 | 1446 | | |
1447 | | - | |
1448 | 1447 | | |
1449 | 1448 | | |
1450 | 1449 | | |
1451 | 1450 | | |
1452 | 1451 | | |
1453 | 1452 | | |
1454 | | - | |
1455 | | - | |
| 1453 | + | |
| 1454 | + | |
| 1455 | + | |
1456 | 1456 | | |
1457 | 1457 | | |
1458 | 1458 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2020 | 2020 | | |
2021 | 2021 | | |
2022 | 2022 | | |
2023 | | - | |
2024 | 2023 | | |
2025 | | - | |
| 2024 | + | |
2026 | 2025 | | |
2027 | | - | |
| 2026 | + | |
| 2027 | + | |
2028 | 2028 | | |
2029 | 2029 | | |
2030 | 2030 | | |
| |||
2038 | 2038 | | |
2039 | 2039 | | |
2040 | 2040 | | |
2041 | | - | |
2042 | 2041 | | |
2043 | 2042 | | |
2044 | 2043 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6539 | 6539 | | |
6540 | 6540 | | |
6541 | 6541 | | |
6542 | | - | |
6543 | | - | |
6544 | | - | |
6545 | | - | |
6546 | | - | |
| 6542 | + | |
| 6543 | + | |
| 6544 | + | |
6547 | 6545 | | |
6548 | | - | |
6549 | | - | |
6550 | | - | |
6551 | | - | |
| 6546 | + | |
| 6547 | + | |
| 6548 | + | |
| 6549 | + | |
| 6550 | + | |
| 6551 | + | |
| 6552 | + | |
| 6553 | + | |
| 6554 | + | |
| 6555 | + | |
| 6556 | + | |
| 6557 | + | |
| 6558 | + | |
| 6559 | + | |
| 6560 | + | |
6552 | 6561 | | |
6553 | | - | |
6554 | | - | |
6555 | | - | |
6556 | | - | |
6557 | | - | |
6558 | 6562 | | |
6559 | 6563 | | |
6560 | 6564 | | |
| |||
6573 | 6577 | | |
6574 | 6578 | | |
6575 | 6579 | | |
6576 | | - | |
6577 | | - | |
| 6580 | + | |
| 6581 | + | |
| 6582 | + | |
| 6583 | + | |
6578 | 6584 | | |
6579 | 6585 | | |
6580 | 6586 | | |
| |||
13734 | 13740 | | |
13735 | 13741 | | |
13736 | 13742 | | |
| 13743 | + | |
| 13744 | + | |
| 13745 | + | |
| 13746 | + | |
| 13747 | + | |
| 13748 | + | |
| 13749 | + | |
| 13750 | + | |
| 13751 | + | |
| 13752 | + | |
| 13753 | + | |
| 13754 | + | |
| 13755 | + | |
| 13756 | + | |
| 13757 | + | |
| 13758 | + | |
| 13759 | + | |
| 13760 | + | |
| 13761 | + | |
| 13762 | + | |
| 13763 | + | |
| 13764 | + | |
| 13765 | + | |
| 13766 | + | |
| 13767 | + | |
13737 | 13768 | | |
13738 | 13769 | | |
13739 | 13770 | | |
| |||
13761 | 13792 | | |
13762 | 13793 | | |
13763 | 13794 | | |
13764 | | - | |
13765 | | - | |
13766 | | - | |
13767 | | - | |
| 13795 | + | |
| 13796 | + | |
| 13797 | + | |
| 13798 | + | |
| 13799 | + | |
| 13800 | + | |
| 13801 | + | |
13768 | 13802 | | |
13769 | | - | |
13770 | | - | |
13771 | | - | |
13772 | | - | |
13773 | | - | |
13774 | | - | |
13775 | | - | |
13776 | | - | |
13777 | | - | |
13778 | | - | |
13779 | | - | |
13780 | | - | |
13781 | | - | |
13782 | | - | |
13783 | | - | |
13784 | | - | |
13785 | | - | |
| 13803 | + | |
| 13804 | + | |
| 13805 | + | |
| 13806 | + | |
| 13807 | + | |
| 13808 | + | |
| 13809 | + | |
| 13810 | + | |
| 13811 | + | |
| 13812 | + | |
| 13813 | + | |
| 13814 | + | |
| 13815 | + | |
| 13816 | + | |
| 13817 | + | |
| 13818 | + | |
13786 | 13819 | | |
13787 | 13820 | | |
13788 | 13821 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
83 | 83 | | |
84 | 84 | | |
85 | 85 | | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
86 | 151 | | |
87 | 152 | | |
88 | 153 | | |
| |||
0 commit comments