From 5d089e0c42052f7aa9622bfd6165e2c358c823b9 Mon Sep 17 00:00:00 2001 From: prql-bot <107324867+prql-bot@users.noreply.github.com> Date: Wed, 25 Mar 2026 09:10:36 +0000 Subject: [PATCH] fix: BETWEEN optimization now triggers by comparing expr kind, not span 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 --- prqlc/prqlc/src/sql/gen_expr.rs | 2 +- prqlc/prqlc/tests/integration/sql.rs | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/prqlc/prqlc/src/sql/gen_expr.rs b/prqlc/prqlc/src/sql/gen_expr.rs index 2fe0786b16f3..973647600a0c 100644 --- a/prqlc/prqlc/src/sql/gen_expr.rs +++ b/prqlc/prqlc/src/sql/gen_expr.rs @@ -432,7 +432,7 @@ fn try_into_between(expr: rq::Expr, ctx: &mut Context) -> Result 3 and x < 5 - if a_l == b_l { + if a_l.kind == b_l.kind { return Ok(Some(sql_ast::Expr::Between { expr: Box::new( translate_operand(a_l, true, 0, Associativity::Both, ctx)? diff --git a/prqlc/prqlc/tests/integration/sql.rs b/prqlc/prqlc/tests/integration/sql.rs index f8dd1d03b042..2c117d3ad585 100644 --- a/prqlc/prqlc/tests/integration/sql.rs +++ b/prqlc/prqlc/tests/integration/sql.rs @@ -1575,6 +1575,21 @@ fn test_ranges() { "); } +#[test] +fn test_between_from_comparison() { + assert_snapshot!((compile(r###" + from t + filter (a >= 5 && a <= 10) + "###).unwrap()), @r" + SELECT + * + FROM + t + WHERE + a BETWEEN 5 AND 10 + "); +} + #[test] fn test_in_values_01() { assert_snapshot!((compile(r#"