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


##########
datafusion/sql/src/query.rs:
##########
@@ -96,7 +97,15 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
             }
         }?;
 
-        self.pipe_operators(plan, pipe_operators, planner_context)
+        let plan = self.pipe_operators(plan, pipe_operators, planner_context)?;
+        if !materialized_ctes.is_empty() && matches!(plan, 
LogicalPlan::Ddl(_)) {
+            return not_impl_err!("MATERIALIZED CTEs are not supported with 
SELECT INTO");
+        }
+        // A later CTE may read an earlier one, so the first CTE is the 
outermost.
+        Ok(materialized_ctes
+            .into_iter()
+            .rev()
+            .fold(plan, |plan, cte| cte.wrap(plan)))

Review Comment:
   Correction: I wrote that a correlated body also fails with `NOT 
MATERIALIZED`. That is not correct. With `NOT MATERIALIZED` or no keyword the 
query works, so this was a regression. Fixed in a1c9cc4b73: when the body of a 
`MATERIALIZED` CTE refers to an outer query, the CTE is inlined, as if 
`MATERIALIZED` was not given. A correlated body would have to run once per 
outer row, which is not supported. The slt file has tests for `EXISTS`, `NOT 
EXISTS` and a scalar subquery, and an `EXPLAIN` that shows no 
`MaterializedCteExec`.



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