Skip to content

fix: BETWEEN optimization now triggers by comparing expr kind, not span#5740

Closed
prql-bot wants to merge 1 commit intomainfrom
fix/issue-5737
Closed

fix: BETWEEN optimization now triggers by comparing expr kind, not span#5740
prql-bot wants to merge 1 commit intomainfrom
fix/issue-5737

Conversation

@prql-bot
Copy link
Copy Markdown
Collaborator

Problem

The try_into_between function in SQL codegen was designed to optimize a >= low AND a <= high into a BETWEEN low AND high. However, the optimization never triggered because the equality check a_l == b_l compared full rq::Expr values including their span field. Since the two references to the same column originate from different source positions, they always have different spans, causing the check to fail.

Solution

Changed the comparison from a_l == b_l (full Expr including span) to a_l.kind == b_l.kind (just the expression kind). This is the correct semantic comparison — two column references are equivalent if they refer to the same column, regardless of where they appear in the source.

Testing

  • Added test_between_from_comparison verifying from t | filter (a >= 5 && a <= 10) produces a BETWEEN 5 AND 10
  • All 517 existing tests pass, including the existing test_ranges test that uses the in operator

Closes #5737 — automated triage

The `try_into_between` function compared full `rq::Expr` values (including
`span`) to detect `a >= low AND a <= high` patterns. Since the two references
to the same column originate from different source positions, their spans
differ, causing the equality check to always fail. Compare only `kind` instead.

Closes #5737

Co-Authored-By: Claude <noreply@anthropic.com>
Copy link
Copy Markdown
Collaborator Author

@prql-bot prql-bot left a comment

Choose a reason for hiding this comment

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

Duplicate overlap: PR #5738 by @queelius applies the same a_l.kind == b_l.kind fix to try_into_between. One of these should be closed as a duplicate. This PR is more focused (BETWEEN fix only), while #5738 bundles an unrelated SQLite div_i change.

The fix itself is correct — Expr includes span which varies by source position, so comparing .kind is the right semantic check for column equivalence.

@max-sixty
Copy link
Copy Markdown
Member

@prql-bot why did we open a duplicate PR?!

@prql-bot
Copy link
Copy Markdown
Collaborator Author

Sorry about that — the triage automation opened this PR without first checking whether an existing PR already addressed #5737. PR #5738 by @queelius has the same fix and was opened first.

Closing this as a duplicate in favor of #5738.

@prql-bot prql-bot closed this Mar 25, 2026
auto-merge was automatically disabled March 25, 2026 09:29

Pull request was closed

@vanillajonathan vanillajonathan deleted the fix/issue-5737 branch March 25, 2026 09:34
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.

BETWEEN optimization in SQL codegen never triggers due to span comparison

2 participants