Skip to content

sql: fix BETWEEN SYMMETRIC normalization to use matching typed lefts #151951

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: master
Choose a base branch
from

Conversation

Uzair5162
Copy link
Contributor

We normalize symmetric RangeCond expressions (<left> BETWEEN SYMMETRIC <from> AND <to>) by expanding them into 2 pairs of ComparisonExpr, covering both orderings of the BETWEEN bounds. When the type-checks for the left/from and left/to comparisons chose different coercions, the symmetric half could pair leftFrom with to (and leftTo with from) during normalization. This resulted in mixed-type comparisons on type-checked expressions and caused panics in some paths (e.g. AOST normalization).

This change ensures that the type-checked pairings are kept aligned when constructing ComparisonExpr in the symmetric case.

Fixes: #133395

Release note (bug fix): Previously, executing certain statements with BETWEEN SYMMETRIC expressions could panic if used with values of different types, such as ... b'bytes' BETWEEN SYMMETRIC 'a' AND 'c', which is now fixed.

We normalize symmetric `RangeCond` expressions (`<left> BETWEEN
SYMMETRIC <from> AND <to>`) by expanding them into 2 pairs of
`ComparisonExpr`, covering both orderings of the BETWEEN bounds. When
the type-checks for the left/from and left/to comparisons chose
different coercions, the symmetric half could pair `leftFrom` with
`to` (and `leftTo` with `from`) during normalization. This resulted in
mixed-type comparisons on type-checked expressions and caused panics in
some paths (e.g. AOST normalization).

This change ensures that the type-checked pairings are kept aligned when
constructing `ComparisonExpr` in the symmetric case.

Fixes: cockroachdb#133395

Release note (bug fix): Previously, executing certain statements with
`BETWEEN SYMMETRIC` expressions could panic if used with values of
different types, such as `... b'bytes' BETWEEN SYMMETRIC 'a' AND 'c'`,
which is now fixed.
@Uzair5162 Uzair5162 requested a review from a team August 15, 2025 21:38
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@Uzair5162 Uzair5162 requested review from mw5h and a team and removed request for a team August 15, 2025 21:39
Copy link
Contributor

@mw5h mw5h left a comment

Choose a reason for hiding this comment

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

:lgtm: Great job, thanks!

@mw5h reviewed 3 of 3 files at r1, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @Uzair5162)

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.

sql: CockroachDB panics when executing SELECT statement with JOIN and ill-formed AS FOR SYSTEM TIME
3 participants