From ef12bb002cc1ab3dbefc8ae992c5a5ef15eb9606 Mon Sep 17 00:00:00 2001 From: Alexander Towell Date: Wed, 25 Mar 2026 08:33:54 -0500 Subject: [PATCH] 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) --- prqlc/prqlc/src/sql/gen_expr.rs | 4 +++- prqlc/prqlc/tests/integration/sql.rs | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/prqlc/prqlc/src/sql/gen_expr.rs b/prqlc/prqlc/src/sql/gen_expr.rs index 2fe0786b16f3..ea48584f249f 100644 --- a/prqlc/prqlc/src/sql/gen_expr.rs +++ b/prqlc/prqlc/src/sql/gen_expr.rs @@ -432,7 +432,9 @@ fn try_into_between(expr: rq::Expr, ctx: &mut Context) -> Result 3 and x < 5 - if a_l == b_l { + // Compare only `kind` (not `span`) since the same column + // referenced at two source positions has different spans. + 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..1917c3fb6e89 100644 --- a/prqlc/prqlc/tests/integration/sql.rs +++ b/prqlc/prqlc/tests/integration/sql.rs @@ -451,6 +451,22 @@ fn test_precedence_division() { "); } +#[test] +fn test_between_optimization() { + // Regression test: >= and <= on same column should produce BETWEEN + 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_precedence_01() { assert_snapshot!((compile(r###"