Skip to content

Commit ef12bb0

Browse files
queeliusclaude
andcommitted
Fix BETWEEN optimization never triggering due to span comparison
The try_into_between function compared rq::Expr values including their span field. Since the same column referenced at two source positions has different spans, the equality check always failed, making the BETWEEN optimization dead code. Fix by comparing only the kind field (a_l.kind == b_l.kind) instead of the full Expr (a_l == b_l). Fixes #5737 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent f55952a commit ef12bb0

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

prqlc/prqlc/src/sql/gen_expr.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,9 @@ fn try_into_between(expr: rq::Expr, ctx: &mut Context) -> Result<Option<sql_ast:
432432

433433
// We need for the values on each arm to be the same; e.g. x
434434
// > 3 and x < 5
435-
if a_l == b_l {
435+
// Compare only `kind` (not `span`) since the same column
436+
// referenced at two source positions has different spans.
437+
if a_l.kind == b_l.kind {
436438
return Ok(Some(sql_ast::Expr::Between {
437439
expr: Box::new(
438440
translate_operand(a_l, true, 0, Associativity::Both, ctx)?

prqlc/prqlc/tests/integration/sql.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,22 @@ fn test_precedence_division() {
451451
");
452452
}
453453

454+
#[test]
455+
fn test_between_optimization() {
456+
// Regression test: >= and <= on same column should produce BETWEEN
457+
assert_snapshot!(compile(r#"
458+
from t
459+
filter (a >= 5 && a <= 10)
460+
"#).unwrap(), @r"
461+
SELECT
462+
*
463+
FROM
464+
t
465+
WHERE
466+
a BETWEEN 5 AND 10
467+
");
468+
}
469+
454470
#[test]
455471
fn test_precedence_01() {
456472
assert_snapshot!((compile(r###"

0 commit comments

Comments
 (0)