Repository navigation
Conversation
51b71ce to
527849a
Compare
|
run benchmark sql_planner env:
BENCH_FILTER: physical_cte_ |
|
🤖 Benchmark running (GKE) | trigger CPU Details (lscpu)Comparing mcte-narrow-api (527849a) to a72c60b (merge-base) diff Run configurationrun benchmark sql_planner
env:
BENCH_FILTER: "physical_cte_"Trigger commentResults will be posted here when complete File an issue against this benchmark runner |
kosiew
left a comment
There was a problem hiding this comment.
This PR has a neater and more organized approach to ensuring body-scan compatibility.
There are also more clone operations and I wonder how it affects performance.
| schema | ||
| .has_equivalent_names_and_types(self.cte.schema()) | ||
| .map_err(|e| { | ||
| e.context(format!( | ||
| "The body of the materialized CTE {} changed its columns", | ||
| self.name | ||
| )) | ||
| })?; |
There was a problem hiding this comment.
Dropping an unread trailing column fails here but succeeds through the subquery-aware traversal.
Please preserve executable-invariant checks or validate bound scans after both callbacks, update the documentation, and test valid pruning and incompatible schema changes through both paths.
|
🤖 Benchmark completed (GKE) | trigger Instance: Comparing mcte-narrow-api (527849a) to a72c60b (merge-base) diff Run configurationrun benchmark sql_planner
env:
BENCH_FILTER: "physical_cte_"Trigger commentCPU Details (lscpu)Details
Resource Usagesql_planner — base (merge-base)
sql_planner — branch
File an issue against this benchmark runner |
|
run benchmark sql_planner env:
BENCH_FILTER: physical_cte_ |
|
🤖 Benchmark running (GKE) | trigger CPU Details (lscpu)Comparing mcte-narrow-api (fd0a081) to a72c60b (merge-base) diff Run configurationrun benchmark sql_planner
env:
BENCH_FILTER: "physical_cte_"Trigger commentResults will be posted here when complete File an issue against this benchmark runner |
|
🤖 Benchmark completed (GKE) | trigger Instance: Comparing mcte-narrow-api (fd0a081) to a72c60b (merge-base) diff Run configurationrun benchmark sql_planner
env:
BENCH_FILTER: "physical_cte_"Trigger commentCPU Details (lscpu)Details
Resource Usagesql_planner — base (merge-base)
sql_planner — branch
File an issue against this benchmark runner |
A rule that builds a MaterializedCte with a struct literal or struct update skips the scan check in try_new. Private fields send every new node through try_new. The fields are pub(crate), so the tree-node and display code of datafusion-expr is unchanged. try_map_inputs_mut and into_parts replace the remaining direct field access in the optimizer and the physical planner. Later commits in this PR replace these interim hooks with checked methods. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The SQL planner allocated the id of a materialized CTE, built its scans and kept a pending node until the continuation was planned. Any other producer of the node has to repeat these steps and can bind a scan to the wrong id. Add MaterializedCteBuilder in datafusion-expr. It allocates the id, hands out a scan for each reference, and builds the node from the continuation. It is not Clone, so two nodes cannot share its id. The SQL planner uses it in place of its own pending type. Add From impls that wrap a MaterializedCte or a MaterializedCteScan in a LogicalPlan. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Type coercion, OptimizeProjections and the physical planner each change the body of a materialized CTE together with its scans. Each repeated the walk over the scans of the CTE, which must skip a nested occurrence of the same CTE, and rebuilt the node from its parts. OptimizeProjections also worked out which body columns to keep from the scan positions, which are a detail of the representation. Add these to datafusion-expr. They share one private scan traversal: - MaterializedCte::with_coerced_body gives the scans the new types of a coerced body. - MaterializedCte::prune_body_columns removes the body columns that no scan reads, and keeps one column if no scan reads a column. - renumber_materialized_ctes gives each CTE in a plan a new id. A new id does not change a schema, so it does not check the scans again. Add map_continuation and map_body for the rules that rewrite one input (decorrelation, filter push down, OptimizeProjections). They check the scans of the new node. The callers use these in place of into_parts and try_new. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The only caller of MaterializedCte::try_map_inputs_mut is map_children_mut in the optimizer, which does not care which input is the body and which is the continuation. The closure could also change the columns of the body without an error, and the scans would then read the wrong columns. Replace it with map_inputs_mut, which calls one closure for the body and then for the continuation. It returns an error if the body changes the names or the types of its columns. It skips the check when the schema of the body is the same Arc. It does not check the continuation, because the optimizer recomputes the schema of the node afterwards. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With the builder and the coupled methods in place, no caller outside datafusion-expr needs to build a node or a scan from its parts. A public constructor lets a caller bind a scan to the wrong id or give a scan a projection that does not match the body. Make these crate-private: MaterializedCte::try_new, scans, MaterializedCteScan::new, with_body_columns, with_body_schema, check_body_schema, find_in, MaterializedCteId::next, and the id and name fields of MaterializedCteScan. Remove into_parts. Rename with_new_inputs to with_new_inputs_unchecked, which returns Self and is crate-private. Its only caller is LogicalPlan::with_new_exprs. The physical planner needs to know whether a CTE is read, so add MaterializedCte::is_read. The module documentation says which APIs do not check the scans, and that check_invariants is the backstop. The OptimizeProjections tests build their plans with the builder. The 57.0.0 upgrade guide describes the new API. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Mechanical change: build a LogicalPlan from a MaterializedCte or a MaterializedCteScan with the From impls in place of the nested enum constructors. No behavior changes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The checks in try_new, check_invariants, is_read and prune_body_columns collected a clone of each scan. A visitor reads the scans in place. with_coerced_body and prune_body_columns built the new scans from the body, so they do not need a second traversal to check them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
map_inputs_mut rejected a body that removed an unread column, but check_invariants accepted the same plan from map_children. After both calls of the closure, map_inputs_mut checks the scans against the body with the check that check_invariants uses. The tests change the body through both paths. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
fd0a081 to
a82c02a
Compare
Note
This PR is an alternative to #27570. It includes the change of #27570 (private fields), then replaces its generic mutation hooks with checked methods.
MaterializedCteis new in 57.0.0 and is not released, so this PR does not break a released API.Which issue does this PR close?
Rationale for this change
Each
MaterializedCteScanreads the columns of its CTE body by position, so a change to the body needs a matching change to every scan. On main, the public API lets a rule change the body alone:#27570 makes the fields private, but it keeps generic hooks (
try_map_inputs_mut,with_new_inputs,into_parts+try_new) that do not check the scans. To do a body change right, each rule (type coercion,OptimizeProjections, the physical planner) repeats the same loop by hand: find every scan of this id, also in subqueries, skip a nested CTE with the same id, remap the scan, rebuild the node. The publicMaterializedCteScan::newandMaterializedCteId::nextalso let a caller bind a scan to the wrong CTE.What changes are included in this PR?
MaterializedCteowns each coupled body-and-scan operation. The fields and the raw constructors are crate-private.WITH ... AS MATERIALIZEDMaterializedCteBuilder::new(name, body),.scan(),.build(continuation)buildmap_body(f)map_continuation(f)with_coerced_body(body)prune_body_columns()map_inputs_mut(f)renumber_materialized_ctes(plan)with_new_exprs,map_childrencheck_invariantsis the backstopPlanning: the scan comes from the builder, so it has the right id.
A rule picks the method that names its change:
The optimizer runs
check_invariantsbefore and after the rules (and after each rule in debug builds), so a rule that bypasses these methods still fails with an error.The public API is the minimum that the callers in this repository need. For comparison, DuckDB and Polars use the same model (each consumer prunes its own columns, the body keeps their union), and DuckDB also pushes the OR of the consumer filters into the body.
map_bodycan express that rule without new API.What is the testing strategy for this PR?
Existing
datafusion-expr,datafusion-optimizer,datafusion-sqlunit tests and thecte*.sltsqllogictests. New: the builder doctest, and tests that drop an unread body column (accepted) and change a read column's type (rejected) through bothmap_inputs_mutandmap_children.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 it.🤖 Generated with Claude Code