fix: BETWEEN optimization now triggers by comparing expr kind, not span#5740
Closed
fix: BETWEEN optimization now triggers by comparing expr kind, not span#5740
Conversation
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>
prql-bot
commented
Mar 25, 2026
Collaborator
Author
prql-bot
left a comment
There was a problem hiding this comment.
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.
Member
|
@prql-bot why did we open a duplicate PR?! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
The
try_into_betweenfunction in SQL codegen was designed to optimizea >= low AND a <= highintoa BETWEEN low AND high. However, the optimization never triggered because the equality checka_l == b_lcompared fullrq::Exprvalues including theirspanfield. 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(fullExprincludingspan) toa_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
test_between_from_comparisonverifyingfrom t | filter (a >= 5 && a <= 10)producesa BETWEEN 5 AND 10test_rangestest that uses theinoperatorCloses #5737 — automated triage