Skip to content

refactor: make the fields of MaterializedCte private - #27570

Open
adriangb wants to merge 1 commit into
apache:mainfrom
adriangb:mcte-private-fields
Open

adriangb wants to merge 1 commit into
apache:mainfrom
adriangb:mcte-private-fields

Conversation

@adriangb

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Follow-up to #26274. This PR is stacked on it: only the last commit belongs to this PR. Review #26274 first.

Rationale for this change

#26274 makes the schema and projection fields of MaterializedCteScan private, so that they always match. The MaterializedCte node still has public fields. A rule that builds the node with a struct literal or a struct update skips the scan check in MaterializedCte::try_new.

What changes are included in this PR?

  • The fields of MaterializedCte are pub(crate). Downstream code reads them through id(), name(), body() and continuation().
  • New MaterializedCte::inputs_mut and MaterializedCte::into_inputs replace the direct field access in the optimizer driver, TypeCoercion, DecorrelatePredicateSubquery, PushDownFilter, OptimizeProjections and the physical planner.
  • Rules that rebuild the node from owned inputs call try_new, so the scans are checked.

Are these changes tested?

Yes, by the existing tests. The change does not change behavior.

Are there any user-facing changes?

Yes. This is a breaking API change, which the 57.0.0 upgrade guide describes.

@github-actions github-actions Bot added documentation Improvements or additions to documentation logical-expr Logical plan and expressions optimizer Optimizer rules core Core DataFusion crate sqllogictest SQL Logic Tests (.slt) physical-plan Changes to the physical-plan crate labels Nov 18, 2026
@adriangb
adriangb force-pushed the mcte-private-fields branch from 5b625f0 to bdf3a4d Compare November 19, 2026 17:17
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, and every in-place change through inputs_mut or with_new_inputs.

The fields are pub(crate), so the tree-node and display code of
datafusion-expr is unchanged. inputs_mut and into_inputs replace the
remaining direct field access in the optimizer and the physical planner.
@adriangb
adriangb force-pushed the mcte-private-fields branch from bdf3a4d to ffb77b4 Compare November 20, 2026 06:15
@github-actions github-actions Bot removed sqllogictest SQL Logic Tests (.slt) physical-plan Changes to the physical-plan crate labels Nov 20, 2026
@adriangb
adriangb marked this pull request as ready for review November 20, 2026 06:24
@adriangb

Copy link
Copy Markdown
Contributor Author

@kosiew mind taking a look at this small followup? This is a small change to prevent us from having to make another pub -> priv change like in #26274 (in that case so we could encode the invariant of schema and projection being in sync, hard if anything can edit them in place).

@kosiew kosiew left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@adriangb, thanks for the fix. Restricting field access closes the unchecked construction path, and the migrated rebuilds correctly use try_new.

Could we adjust the input mutation APIs to also allow future validation?


/// The body and the continuation, in this order, for a rule that changes
/// them in place. The schemas of the scans are not checked again.
pub fn inputs_mut(&mut self) -> (&mut Arc<LogicalPlan>, &mut Arc<LogicalPlan>) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could we replace inputs_mut with a fallible closure-based try_map_inputs_mut and make with_new_inputs return Result<Self>? Both can remain unchecked for now, but these signatures would permit future validation.

The fields of `MaterializedCte` are private. Read them with
`MaterializedCte::id`, `name`, `body` and `continuation`. Build a node with
`MaterializedCte::try_new`, which checks the scans of the CTE. A rule that
changes the inputs in place can use `MaterializedCte::inputs_mut` or

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit: with_new_inputs returns a new node rather than modifying inputs in place. Please distinguish it from the in-place mutation API and document which paths validate scans.


/// The body and the continuation, in this order. Build a node again with
/// [`Self::try_new`].
pub fn into_inputs(self) -> (Arc<LogicalPlan>, Arc<LogicalPlan>) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Optional: could into_parts return the ID, name, body, and continuation? Current callers copy the name before consuming the node. Moving all four values would simplify reconstruction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Core DataFusion crate documentation Improvements or additions to documentation logical-expr Logical plan and expressions optimizer Optimizer rules

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants