xudong963 commented on code in PR #4461:
URL: https://github.com/apache/arrow-datafusion/pull/4461#discussion_r1038741251


##########
datafusion/sql/src/planner.rs:
##########
@@ -662,21 +662,33 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
         ctes: &mut HashMap<String, LogicalPlan>,
         outer_query_schema: Option<&DFSchema>,
     ) -> Result<LogicalPlan> {
+        // From clause may exist CTEs, we should separate them from global 
CTEs.
+        // CTEs in from clause are allowed to be duplicated.
+        // Such as `select * from (WITH source AS (select 1 as e) SELECT * 
FROM source) t1, (WITH source AS (select 1 as e) SELECT * FROM source) t2;` 
which is valid.
+        // So always use original global CTEs to plan CTEs in from clause.
+        // Btw, don't need to add CTEs in from to global CTEs.
+        let origin_ctes = ctes.clone();
         let left = self.create_relation(t.relation, ctes, outer_query_schema)?;
         match t.joins.len() {
-            0 => Ok(left),
+            0 => {
+                *ctes = origin_ctes;

Review Comment:
   Some cases don't have explicit `join`. Such as the test case in the ticket.



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

Reply via email to