Skip to content

fix(ddx-ad): write between as SQL's BETWEEN - #281

Merged
alxmrs merged 1 commit into
mainfrom
fix/to-sql-between
Nov 21, 2026
Merged

alxmrs merged 1 commit into
mainfrom
fix/to-sql-between

Conversation

@alxmrs

@alxmrs alxmrs commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

∂ Claude (ddx):

Fixes #263.

Why

On DuckDB, ddx planned a gradient through a range filter but couldn't run it: "writing the function between as SQL: ddx does not know its name in SQL". Through a join's equality, DuckDB's optimizer derives a range filter for one side from the other's: from b.i1 >= 0 AND b.i1 < 2 and a.i1 = b.i1, it adds between(a.i1, 0, 2) to a's filter, beside a's own comparisons. Its Substrait producer writes that call. ddx copies a filter it doesn't differentiate into its steps as it is, and to_sql had no SQL for between. torch-sql's split writes each part as such a filter.

How

to_sql reads between(x, lo, hi) as a new Expr::Between and writes (x BETWEEN lo AND hi), which both dialects read. DuckDB's producer drops whether each bound is held (between(a.i1, 0, 2) above excludes 2), so SQL's BETWEEN, which holds both, is the widest the call could mean. The comparisons DuckDB keeps beside it make the rows exact.

Edge cases

  • On DuckDB 1.5.6, a between appears only as such a derived filter. BETWEEN in SQL, and every range on one table, are planned as gte/lte/gt/lt comparisons. If DuckDB wrote an exclusive between alone, ddx would keep a row at its bound.
  • between with other than three arguments is refused as an invalid plan.
  • A NULL bound gives what SQL's BETWEEN gives, which is what DuckDB folded it from.

Tests

🤖 Generated with Claude Code

https://claude.ai/code/session_01CvszJhH8pn8H9G69wEPMU2

@coderabbitai

coderabbitai Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 56 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 6edc824 and 9f753fd.

Files selected for processing: 4

  • crates/ddx-ad/src/to_sql/ir.rs
  • crates/ddx-ad/src/to_sql/print.rs
  • crates/ddx-ad/src/to_sql/reader.rs
  • python/ddxdb/tests/test_duckdb.py
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@alxmrs alxmrs left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

DuckDB's optimizer folds a range filter on an integer, `i1 >= 0 AND
i1 < 2`, into `between(i1, 0, 1)`, and its Substrait producer writes that
call. ddx copies a filter it doesn't differentiate into its steps, and
to_sql had no name for `between`, so the program couldn't run: "writing
the function `between` as SQL: ddx does not know its name in SQL".

to_sql now writes it as `x BETWEEN lo AND hi`, which holds both bounds,
as Substrait's and DuckDB's `between` do, and which both dialects read.

Fixes #263.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CvszJhH8pn8H9G69wEPMU2
@alxmrs
alxmrs force-pushed the fix/to-sql-between branch from c58c550 to 9f753fd Compare November 21, 2026 14:03
@alxmrs
alxmrs merged commit 02b8365 into main Nov 21, 2026
16 checks passed
@alxmrs
alxmrs deleted the fix/to-sql-between branch November 21, 2026 15:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

On DuckDB, a range filter can become a between call that ddx's SQL writer can't write

1 participant