Skip to content

fix(ddx-ad): write a plan as SQL as ddx reads it to differentiate it - #290

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

alxmrs merged 1 commit into
mainfrom
fix/to-sql-read-form

Conversation

@alxmrs

@alxmrs alxmrs commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

∂ Claude (ddx):

Fixes #288.

Why

On DuckDB, ddxdb.to_sql of an engine's own plan gave SQL with another result, without an error. DuckDB's Substrait producer writes no column mask on a read whose query uses none of the table's columns, and counts that read as one column wide. #279 taught ddx that rule in in_read_form, the function that converts a producer's plan into the form ddx reads, but only on the way into differentiation. to_sql read the plan as Substrait reads it, as every column, so each field after such a read landed on the wrong column. #282 made to_sql public, so a caller who writes an engine's plan as SQL hits this. ddx's own program steps don't, since ddx writes those plans itself.

How

to_sql reads the plan through in_read_form before writing it. It then also writes, like differentiation reads them, a window relation as a projection of window functions, and a right semi or anti join as the left one with its sides swapped (#287). Both mean the same as before. With #280, DuckDB's one-argument subtract will be written as negation too.

#279's rule applied to every read with no mask, but DuckDB means no columns only for a named table. Its read of a virtual table, such as the empty one it plans LIMIT 0 as, has no mask and reads every column. ddxdb writes such a plan with to_sql to make the placeholder table of a CTE a view reads twice, so once to_sql used the rule, three tests of such views failed. The rule now applies to named tables only.

in_read_form borrows a plan it doesn't change, so ddx's own steps, whose producer is ddx, aren't copied.

Tests

  • A ddx-ad unit test writes a hand-built DuckDB-tagged cross join of a maskless read and a masked one: the SQL selects s.v. Without the fix, it selected x.w. The same test checks that a DuckDB read of a virtual table with no mask is left whole.
  • test_duckdb.py: DuckDB: to_sql still reads a maskless read as every column (#279's fix, not in to_sql), giving wrong SQL silently #288's query, written by to_sql and run on DuckDB, gives the query's value (1.875).
  • Locally: the wheel built from this branch passes ddxdb's Python tests (215, 4 skipped, 1 xfail), the three tests of views whose CTE is read twice included; ddx-ad's unit tests (151), and ddx-datafusion's to_sql, to_sql_fuzz (300 seeds), ad_sql_text and sql_programs pass.

🤖 Generated with Claude Code

https://claude.ai/code/session_01CvszJhH8pn8H9G69wEPMU2

Summary by CodeRabbit

  • Bug Fixes
    • Improved DuckDB SQL generation for queries that include reads used to repeat rows, cross joins, or select only some columns. These queries now better preserve the intended results and avoid referencing columns that aren’t needed.
    • Queries involving virtual tables are handled more consistently when converting plans to SQL.

@coderabbitai

coderabbitai Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

reads_no_columns now matches unmasked positive-width named-table reads, not virtual-table reads. to_sql applies in_read_form before generating SQL. Regression tests check generated SQL references and compare DuckDB execution results.

Changes: DuckDB SQL read handling

Layer / File(s) Summary
Define DuckDB maskless read behavior
crates/ddx-ad/src/forward.rs
reads_no_columns now requires a named table. A test checks that an unmasked virtual-table read remains borrowed by in_read_form.
Apply read form in SQL generation
crates/ddx-ad/src/to_sql/mod.rs, crates/ddx-ad/src/to_sql/reader.rs, python/ddxdb/tests/test_duckdb.py
to_sql applies in_read_form before reading and printing the plan. Regression tests check generated SQL column references and verify DuckDB execution returns [(1.875,)].

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related PRs

  • xqlsystems/ddx#279: Changes the DuckDB maskless-read interpretation that this PR applies in to_sql.
  • xqlsystems/ddx#280: Uses the in_read_form interface that this PR applies before SQL generation.
Merge Risk: Minimal · up to 3fc50

to_sql now reads DuckDB plans in the same form as differentiation, which fixes the silently wrong SQL for maskless named-table reads. No concrete merge-blocking risk was found, and regression tests cover the reported case.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the ddx-ad fix: writing a plan as SQL in the form ddx reads it, which matches the main change. The to_sql function now converts input through in_read_form, and the reads_no_columns rul…
Linked Issues check Passed Directly linked issue #288 requires that to_sql read a DuckDB maskless named-table read as one column wide, as differentiation does (#279). The reviewed head changes crates/ddx-ad/src/to_sql/mod.rs so…
Out of Scope Changes check Passed The to_sql change routes the plan through in_read_form, the same function differentiation uses. The consequences the PR describes are: (a) window relations are written as a projection of window functi…
Full details: Title check

Explanation

The title names the ddx-ad fix: writing a plan as SQL in the form ddx reads it, which matches the main change. The to_sql function now converts input through in_read_form, and the reads_no_columns rule is narrowed to named tables. The title is specific and concise, and it does not describe anything absent from the changeset.

Full details: Linked Issues check

Explanation

Directly linked issue #288 requires that to_sql read a DuckDB maskless named-table read as one column wide, as differentiation does (#279). The reviewed head changes crates/ddx-ad/src/to_sql/mod.rs so that to_sql calls crate::forward::in_read_form on the plan before reader::query. The diff confirms this change. The in_read_form rule for maskless reads is the one #279 added, so the same rule now applies to to_sql. The PR adds a ddx-ad unit test (to_sql/reader.rs, cross join selecting s.v, not x.w) and a test_duckdb.py regression test that expects [(1.875,)], matching the acceptance example in #288. The PR narrows the maskless rule (reads_no_columns in forward.rs) to named tables, which leaves the #288 case (named tables x and s) unchanged. Limit: the forward.rs diff could not be read (the changed blob was unavailable and the file read was truncated). The narrowing and the unit test rely on the PR summary and description, not on code I read directly. The core to_sql change is verified.

Full details: Out of Scope Changes check

Explanation

The to_sql change routes the plan through in_read_form, the same function differentiation uses. The consequences the PR describes are: (a) window relations are written as a projection of window functions, and (b) right semi and anti joins are written as left joins with swapped sides (#287). Neither form is requested by #288, but both come from the same read path and the PR states they keep the same meaning. The narrowing of the maskless-read rule to named tables is required by #288's own fix: without it, the virtual-table reads of CTE placeholders in ddxdb tests change. The added test for a borrowed virtual-table plan supports that narrowing. The ignored_files list is empty. No change was found that is unrelated to #288 or to the read path it fixes. Limit: forward.rs could not be read in full, so the narrowing itself was not checked against code.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • 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

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 3029b7d and 3fc5013.

Files selected for processing: 4

  • crates/ddx-ad/src/forward.rs
  • crates/ddx-ad/src/to_sql/mod.rs
  • crates/ddx-ad/src/to_sql/reader.rs
  • python/ddxdb/tests/test_duckdb.py

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

to_sql read a DuckDB plan's read with no column mask as every column of
its table, where DuckDB means none, so the fields after it landed on the
wrong column and the SQL gave another result, without an error (#288).
#279 fixed that for programs only, in in_read_form. to_sql now reads a
plan through in_read_form too.

DuckDB means no columns only for a named table: its read of a virtual
table, such as the empty one it plans `LIMIT 0` as, has no mask and reads
every column. in_read_form now leaves such a read whole.

Fixes #288.

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-read-form branch from a9f1c9a to 3fc5013 Compare November 21, 2026 15:27
@alxmrs
alxmrs merged commit c2543dd into main Nov 21, 2026
16 checks passed
@alxmrs
alxmrs deleted the fix/to-sql-read-form branch November 21, 2026 15:54
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.

DuckDB: to_sql still reads a maskless read as every column (#279's fix, not in to_sql), giving wrong SQL silently

1 participant