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###"