kosiew commented on code in PR #27725:
URL: https://github.com/apache/datafusion/pull/27725#discussion_r4766001986
##########
docs/source/library-user-guide/upgrading/57.0.0.md:
##########
@@ -237,23 +237,63 @@ that no scan reads.
A custom optimizer rule that changes the schema of the body of a
`MaterializedCte` must also change the `projection` of every scan of that CTE,
-because a scan reads the columns of the body by position. Use
-`MaterializedCteScan::with_body_columns` to remap a scan, and
-`MaterializedCteScan::with_body_schema` to give a scan the new types of the
-columns that it reads.
+because a scan reads the columns of the body by position. Use the methods of
Review Comment:
Non-blocking: Please distinguish ordinal remapping when read positions
change, schema refresh when read types change, and unchanged scans when unread
trailing columns are removed. Also document the coercion callback's obligation
to reanalyze the continuation.
##########
datafusion/expr/src/logical_plan/materialized_cte.rs:
##########
@@ -145,15 +224,197 @@ impl MaterializedCte {
self.continuation.schema()
}
+ /// Returns an error if a scan of this CTE in the continuation does not
+ /// have the names and types of the columns of the body that it reads.
+ pub(crate) fn check_scans(&self) -> Result<()> {
+ check_scans(self.id, &self.name, self.cte.schema(), &self.continuation)
+ }
+
/// The scans in the continuation that read this CTE, also in subquery
/// expressions. See [`MaterializedCteScan::find_in`].
pub fn scans(&self) -> Result<Vec<MaterializedCteScan>> {
MaterializedCteScan::find_in(&self.continuation, self.id)
}
- /// Return a node with the same id and name, and new inputs. The schemas
- /// of the scans are not checked again.
- pub fn with_new_inputs(
+ /// Call `f` with the body, then with the continuation, so that a rule can
+ /// change them in place. Return `true` if a call of `f` returns `true`.
+ ///
+ /// After both calls, if an input changed, check the scans of this CTE in
+ /// the continuation against the body, as [`LogicalPlan::check_invariants`]
+ /// does. Returns an error if a scan does not have the names and types of
+ /// the columns of the body that it reads. A rule can remove a column of
+ /// the body that no scan reads. The continuation can change its schema,
+ /// and the optimizer then recomputes the schema of this node.
+ pub fn map_inputs_mut(
+ &mut self,
+ mut f: impl FnMut(&mut LogicalPlan) -> Result<bool>,
+ ) -> Result<bool> {
+ let schema = Arc::clone(self.cte.schema());
+ let body = f(Arc::make_mut(&mut self.cte))?;
+ let continuation = f(Arc::make_mut(&mut self.continuation))?;
+ if body || continuation || !Arc::ptr_eq(&schema, self.cte.schema()) {
Review Comment:
This check can reject a valid bottom-up rewrite before the binder callback
repairs the temporary schema mismatch. Please keep explicit CTE edits checked,
validate generic rewrites at a shared post-rule boundary, and add optimizer
regression coverage with and without EXISTS.
--
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]