adriangb opened a new pull request, #27725: URL: https://github.com/apache/datafusion/pull/27725
> [!NOTE] > This PR stacks on https://github.com/apache/datafusion/pull/27570. Review only the last three commits. The earlier commits belong to #27570. ## Which issue does this PR close? - Follow-up to https://github.com/apache/datafusion/pull/27570. ## Rationale for this change After https://github.com/apache/datafusion/pull/27570, `MaterializedCte` still exposes `try_new` and `into_parts`. Every caller that changes the body has to walk the scans of the CTE in the continuation, skip a nested CTE with the same id, and rebuild the node. Type coercion, `OptimizeProjections` and the physical planner each repeat this loop. A caller can also bind a scan to the wrong id, because `MaterializedCteScan::new`, `MaterializedCteId::next` and the scan `id` field are public. This PR puts the coupled body-and-scan operations on `MaterializedCte`, so that the traversal lives in one place, and then makes the constructors crate-private. ## What changes are included in this PR? | Commit | Change | | --- | --- | | 1. Declare a materialized CTE in `datafusion-expr` | Adds `MaterializedCte::declare(name, body) -> (PendingMaterializedCte, MaterializedCteScan)` and `PendingMaterializedCte::wrap(continuation)`. The SQL planner uses them in place of its private pending type. | | 2. Own the scan traversal | Adds `with_body` (type coercion), `project_body` (`OptimizeProjections`), `renumber` (physical planner) and `map_continuation` (decorrelation, filter push down). They share one private `map_scans` traversal and check the scans of the new node. Callers drop `into_parts` and `try_new`. | | 3. Narrow the public API | Makes `try_new`, `MaterializedCteScan::new`, `with_body_columns`, `with_body_schema`, `check_body_schema`, `find_in`, `MaterializedCteId::next` and the scan `id` / `name` fields crate-private. Removes `into_parts`. Moves the `OptimizeProjections` tests to `declare` / `wrap`. Updates the 57.0.0 upgrade guide. | `try_map_inputs_mut` and `with_new_inputs` stay public for generic tree code. `OptimizeProjections` keeps choosing the kept columns (including the "keep one column" rule) and uses `try_map_inputs_mut` to optimize the projected body, because that step does not change the body schema. Behavior notes for review: - `push_down_filter` builds the pushed filter with `Filter::try_new` and goes through `map_continuation`, which checks the scans. It used `Filter::new` and an unchecked in-place update. - The nested-same-id test in `OptimizeProjections` builds both occurrences from clones of one `PendingMaterializedCte` and its scan. ## What is the testing strategy for this PR? This is a refactor, so the existing tests cover it: the unit tests of `datafusion-expr`, `datafusion-optimizer` and `datafusion-sql`, and the `cte*.slt` sqllogictests. `declare` has a doctest. Each commit builds and passes these tests. `cargo clippy --all-targets --all-features -- -D warnings` passes. ## Are there any user-facing changes? Yes, an API change to `MaterializedCte`, which is new in 57.0.0 and not released yet. The upgrade guide describes the new API. 🤖 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]
