prql icon indicating copy to clipboard operation
prql copied to clipboard

BETWEEN optimization in SQL codegen never triggers due to span comparison

Open queelius opened this issue 8 months ago • 1 comments

Bug Description

The try_into_between function in prqlc/prqlc/src/sql/gen_expr.rs is designed to optimize a >= low AND a <= high into a BETWEEN low AND high. However, this optimization never triggers because the equality check a_l == b_l compares rq::Expr values including their span field.

Since the two references to column a in a >= 5 && a <= 10 originate from different source positions, they have different span values, causing the equality check to always fail.

Reproduction

from t | filter (a >= 5 && a <= 10)

Expected output:

SELECT * FROM t WHERE a BETWEEN 5 AND 10

Actual output:

SELECT * FROM t WHERE a >= 5 AND a <= 10

Root Cause

rq::Expr (in prqlc/prqlc/src/ir/rq/expr.rs) derives PartialEq, which compares both kind and span:

#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, JsonSchema)]
pub struct Expr {
    pub kind: ExprKind,
    pub span: Option<Span>,
}

In try_into_between, the check if a_l == b_l compares the full Expr including spans. The two column references to a have the same CId (same column) but different Span values (different source positions), so the comparison fails.

Suggested Fix

Compare only the kind field instead of the full Expr:

// Before:
if a_l == b_l {

// After:
if a_l.kind == b_l.kind {

Impact

This is not a correctness bug -- the generated SQL is semantically equivalent. It's a dead-code / missed-optimization bug. The BETWEEN optimization code has likely never worked since it was written.

queelius avatar Mar 25 '26 08:03 queelius

Thanks for reporting this! I was able to reproduce the issue and identified the root cause: the try_into_between function compared full rq::Expr values (including span), so two references to the same column from different source positions always compared as unequal.

I've opened #5740 with a fix — it changes the comparison to a_l.kind == b_l.kind so only the semantic content is compared. A maintainer will review it shortly.

prql-bot avatar Mar 25 '26 09:03 prql-bot