adriangb opened a new pull request, #27739:
URL: https://github.com/apache/datafusion/pull/27739

   > Stacked on https://github.com/apache/datafusion/pull/27725 (which stacks 
on https://github.com/apache/datafusion/pull/27570). Only the last commit, 
`106714a91`, belongs to this PR.
   
   ## Which issue does this PR close?
   
   - None. This is defense in depth found while reviewing 
https://github.com/apache/datafusion/pull/27570.
   
   ## Rationale for this change
   
   A `MaterializedCteScan` reads the body of the `MaterializedCte` with its id 
by column position. The invariant check only validated the scans inside the 
continuation of their CTE. A scan in the body of its own CTE, beside the CTE, 
or in a subquery above it was never checked. `prune_body_columns` does not 
remap such a scan either. The physical binder binds scans by id over the whole 
plan, so such a scan could read the wrong columns.
   
   No SQL query that we tried reaches this state today. The check makes a 
future optimizer rule that moves a scan out of scope fail loudly instead of 
returning wrong results.
   
   ## What changes are included in this PR?
   
   `assert_materialized_cte_scans_are_valid` now walks the plan with a scope 
stack:
   
   | Position of a scan of CTE `N` | Result |
   | --- | --- |
   | In the continuation of `N`, also in a subquery expression | Checked 
against the body of the innermost `N` |
   | In the body of `N`, beside `N`, or above `N` | Error: "is not in the 
continuation of the CTE" |
   | `N` is not in the plan | Allowed, so that a check on a part of a plan 
keeps working |
   
   The physical binder needs no change. `MaterializedCteBinder::bind` already 
calls `check_body_schema` before it binds a scan.
   
   ## What is the testing strategy for this PR?
   
   New unit tests in `materialized_cte.rs` build invalid plans by hand and 
assert the exact error:
   - `check_invariants_rejects_a_scan_outside_the_continuation`: a scan in its 
own body, beside its CTE, and in a subquery above its CTE. A scan in a subquery 
inside the continuation stays valid.
   - `check_invariants_binds_a_scan_to_the_innermost_cte_with_its_id`: two 
nested copies of a CTE with the same id and different body types.
   
   The existing CTE sqllogictests pass.
   
   ## Are there any user-facing changes?
   
   No. Valid plans are unchanged. A plan with an out-of-scope scan now fails 
`check_invariants` at `InvariantLevel::Executable`.
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to