geoffreyclaude commented on code in PR #24755:
URL: https://github.com/apache/datafusion/pull/24755#discussion_r3932872064


##########
datafusion/core/tests/user_defined/relation_planner.rs:
##########
@@ -357,6 +429,123 @@ mod tests {
         ");
     }
 
+    // A name-based relation planner can defer to a visible CTE, preserving the
+    // same shadowing behavior as a catalog table with that name.
+    #[tokio::test]
+    async fn cte_takes_precedence_over_virtual_table() {
+        let ctx = ctx_with_numbers();
+
+        let result = execute_sql_to_string(
+            &ctx,
+            "WITH numbers AS (SELECT 42 AS number) SELECT * FROM numbers",
+        )
+        .await;
+
+        assert_snapshot!(result, @r"
+        +--------+
+        | number |
+        +--------+
+        | 42     |
+        +--------+
+        ");
+    }
+
+    // Deferring a visible CTE continues through the planner chain. A later
+    // planner with a deliberately reserved name can still intercept it.
+    #[tokio::test]
+    async fn cte_defer_still_allows_reserved_planner() {
+        let ctx = SessionContext::new()
+            .with_planner(ReservedNumbersPlanner)
+            .with_planner(NumbersPlanner);
+
+        let result = execute_sql_to_string(
+            &ctx,
+            "WITH numbers AS (SELECT 42 AS number) SELECT * FROM numbers",
+        )
+        .await;
+
+        assert_snapshot!(result, @r"
+        +--------+
+        | number |
+        +--------+
+        | 999    |
+        +--------+
+        ");
+    }
+
+    // The extension lookup follows DataFusion's existing name matching. Both
+    // the extension and the default planner therefore resolve the same CTE for
+    // a quoted dotted name and a qualified reference with the same rendering.
+    #[tokio::test]
+    async fn cte_lookup_matches_default_qualified_name_resolution() {
+        let ctx = SessionContext::new().with_planner(QualifiedNumbersPlanner);
+
+        let result = execute_sql_to_string(
+            &ctx,
+            "WITH \"foo.bar\" AS (SELECT 42 AS number) SELECT * FROM foo.bar",
+        )
+        .await;
+
+        assert_snapshot!(result, @r"
+        +--------+
+        | number |
+        +--------+
+        | 42     |
+        +--------+
+        ");
+    }
+
+    // A CTE shadows an ordinary table reference, not a same-named table
+    // function call with arguments.
+    #[tokio::test]
+    async fn cte_does_not_shadow_same_named_table_function() {
+        let ctx = ctx_with_numbers();
+
+        let result = execute_sql_to_string(
+            &ctx,
+            "WITH numbers AS (SELECT 42 AS number) SELECT * FROM numbers(1)",
+        )
+        .await;
+
+        assert_snapshot!(result, @r"
+        +--------+
+        | number |
+        +--------+
+        | 1      |
+        | 2      |
+        | 3      |
+        +--------+
+        ");
+    }
+
+    // CTE visibility follows the query's lexical scope: the nested CTE wins
+    // inside the derived table but does not hide the outer virtual relation.
+    #[tokio::test]
+    async fn cte_visibility_is_scoped_to_nested_query() {

Review Comment:
   Good suggestion! I added a new test in 6de6b0 verifying that an outer CTE 
remains visible inside a derived query and still takes precedence over the 
virtual relation.



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