adriangb commented on code in PR #27725:
URL: https://github.com/apache/datafusion/pull/27725#discussion_r4775274970


##########
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:
   Fixed in c3a1b53a2. `map_inputs_mut` no longer checks the scans; 
`check_invariants` checks them after the rule, the same way on both paths:
   
   ```text
   bottom-up rule
     body:   x Int32 -> Int64     // scans stale here, no longer rejected
     CTE:    coerce_inputs(..)    // scans repaired
   check_invariants               // per rule (debug) / after all rules 
(release)
   ```
   
   Tests in `optimizer.rs`, each run with and without `EXISTS`:
   - `bottom_up_rule_can_repair_cte_scans`: passes, scan ends up `Int64`
   - `rule_that_breaks_cte_scans_fails`: errors after `widen_cte_body`



##########
docs/source/library-user-guide/upgrading/index.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:
   Done, the guide has a table now:
   
   | Body change                    | Scans                                  |
   | ------------------------------ | -------------------------------------- |
   | drop unread trailing columns   | unchanged                              |
   | drop or reorder other columns  | `prune_body_columns` remaps positions  |
   | change a read column's type    | `coerce_inputs` refreshes the schemas  |
   
   and says the `coerce_inputs` closure must recompute the continuation's 
schemas.



-- 
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