kosiew commented on code in PR #27725:
URL: https://github.com/apache/datafusion/pull/27725#discussion_r4719249614
##########
datafusion/expr/src/logical_plan/materialized_cte.rs:
##########
@@ -145,15 +231,184 @@ impl MaterializedCte {
self.continuation.schema()
}
+ /// Whether a scan in the continuation reads this CTE, also in a subquery
+ /// expression. A CTE that nothing reads does not need to be computed.
+ pub fn is_read(&self) -> Result<bool> {
+ self.scans().map(|scans| !scans.is_empty())
+ }
+
/// The scans in the continuation that read this CTE, also in subquery
/// expressions. See [`MaterializedCteScan::find_in`].
- pub fn scans(&self) -> Result<Vec<MaterializedCteScan>> {
+ pub(crate) 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`.
+ ///
+ /// Returns an error if `f` changes the names or the types of the columns
+ /// of the body, because the scans read them by position. The continuation
+ /// is not checked: its schema can change, 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))?;
+ if !Arc::ptr_eq(&schema, self.cte.schema()) {
+ 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
+ ))
+ })?;
Review 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.
--
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]