Commit e7a2693
sql: parenthesize BETWEEN bounds by their left edge, not right edge
`Parser::parse_between` parses both bounds with
`parse_subexpr(Precedence::Like)`, starting fresh with nothing to the
bound's left. It therefore walks the bound's *left* spine and stops at the
first operator binding at or below `Like`, leaving that operator outside the
bound: `x BETWEEN 1 IS NULL AND y` parses `1` as the low bound and then
expects `AND` but finds `IS`.
`write_between_bound` decided parenthesization with `right_edge`, which
ranks the right-closing forms (`IS NULL`, `= ANY (…)`, `IN (…)`,
`a <op> ANY/ALL`) as `ATOM` because nothing binds into them *from the
right* — exactly the forms whose looseness lives on the left spine. So the
printer emitted them bare and the round trip broke (or silently
re-associated, e.g. a comparison bound rebinding the whole `BETWEEN` as its
left operand).
Switch the bound to `left_edge`, the mirror that walks the left spine, which
is the edge the parser actually consumes here. The `BETWEEN` subject keeps
`right_edge` (the `BETWEEN` keyword prints to its right). As with the rest of
the precedence model, parser-produced bounds are wrapped in `Expr::Nested`
(ranked `ATOM` on both edges), so the parser and pretty-printer datadriven
suites are unchanged — the new parens only appear for ASTs that lost their
`Nested`.
Tests: extends `between_bound_reparenthesized_after_nested_stripped` in
`sqlparser_common.rs` with the right-closing bound forms (`IS NULL`,
`= ANY`, `IN`, a boolean bound, and a left-spine operator buried under a
tighter top).
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>1 parent 05643d5 commit e7a2693
2 files changed
Lines changed: 45 additions & 27 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
607 | 607 | | |
608 | 608 | | |
609 | 609 | | |
610 | | - | |
611 | | - | |
612 | | - | |
613 | | - | |
614 | | - | |
615 | | - | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
616 | 619 | | |
617 | 620 | | |
618 | 621 | | |
619 | | - | |
| 622 | + | |
620 | 623 | | |
621 | 624 | | |
622 | 625 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
796 | 796 | | |
797 | 797 | | |
798 | 798 | | |
799 | | - | |
800 | | - | |
801 | | - | |
802 | | - | |
803 | | - | |
804 | | - | |
805 | | - | |
806 | | - | |
807 | | - | |
808 | | - | |
809 | | - | |
810 | | - | |
811 | | - | |
812 | | - | |
813 | | - | |
814 | | - | |
815 | | - | |
816 | | - | |
817 | | - | |
818 | | - | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
| 812 | + | |
| 813 | + | |
| 814 | + | |
| 815 | + | |
| 816 | + | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
| 820 | + | |
| 821 | + | |
| 822 | + | |
| 823 | + | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
| 827 | + | |
| 828 | + | |
| 829 | + | |
| 830 | + | |
| 831 | + | |
| 832 | + | |
| 833 | + | |
819 | 834 | | |
820 | 835 | | |
821 | 836 | | |
| |||
0 commit comments