This is an automated email from the ASF dual-hosted git repository.

github-bot pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion.git


The following commit(s) were added to refs/heads/main by this push:
     new 85a34e93d9 Fix CTE reference resolution slt tests (#21049)
85a34e93d9 is described below

commit 85a34e93d969d8994ba285e290a4a53023c1eb8c
Author: Jonah Gao <[email protected]>
AuthorDate: Sun Mar 22 23:19:10 2026 +0800

    Fix CTE reference resolution slt tests (#21049)
    
    ## Which issue does this PR close?
    
    N/A
    
    ## Rationale for this change
    
    PR https://github.com/apache/datafusion/pull/19519 introduces new tests
    to verify that DataFusion won't unexpectedly lookup CTE names from the
    catalog. It registers a strict SchemaProvider that panics with
    unexpected table lookups.
    
    
https://github.com/apache/datafusion/blob/d138c36cb08c2dc028b29bbd20853b24cf0f3b8b/datafusion/sqllogictest/src/test_context.rs#L230-L241
    
    The problem is that PR #19862 skips registering the strict
    SchemaProvider and bypasses the unexpected lookup check. Therefore,
    there tests become meaningless.
    
    
    <!--
    Why are you proposing this change? If this is already explained clearly
    in the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand
    your changes and offer better suggestions for fixes.
    -->
    
    ## What changes are included in this PR?
    
    Re-register the strict SchemaProvider into cte.slt.
    
    <!--
    There is no need to duplicate the description in the issue here but it
    is sometimes worth providing a summary of the individual changes in this
    PR.
    -->
    
    ## Are these changes tested?
    Yes.
    
    I also backported these new tests to tag 52.0.0, which does not include
    the fix in https://github.com/apache/datafusion/pull/19519, and they
    failed as expected.
    ```sh
    $ cargo test --profile release-nonlto --test sqllogictests -- cte
       Compiling datafusion-sqllogictest v52.0.0 
(/Users/jonah/Work/datafusion/datafusion/sqllogictest)
        Finished `release-nonlto` profile [optimized] target(s) in 19.50s
         Running bin/sqllogictests.rs 
(/Users/jonah/Work/datafusion/target/release-nonlto/deps/sqllogictests-f9c15c7896eb5af9)
    [00:00:00] ####------------------------------------       6/75      
"cte.slt"
    thread 'tokio-runtime-worker' (486408) panicked at 
datafusion/sqllogictest/src/test_context.rs:215:22:
    unexpected table lookup: barbaz. This maybe indicates a CTE reference was 
incorrectly treated as a catalog table reference.
    note: run with `RUST_BACKTRACE=1` environment variable to display a 
backtrace
    Completed 3 test files in 0 seconds                                         
                                                                                
                                 failure in cte.slt for sql with barbaz as 
(select * from orders) select * from "barbaz";
    caused by
    External error: task 13 panicked with message "unexpected table lookup: 
barbaz. This maybe indicates a CTE reference was incorrectly treated as a 
catalog table reference."
    Error: Execution("1 failures")
    error: test failed, to rerun pass `-p datafusion-sqllogictest --test 
sqllogictests`
    ```
    
    <!--
    We typically require tests for all PRs in order to:
    1. Prevent the code from being accidentally broken by subsequent changes
    2. Serve as another way to document the expected behavior of the code
    
    If tests are not included in your PR, please explain why (for example,
    are they covered by existing tests)?
    -->
    
    ## Are there any user-facing changes?
    No.
    <!--
    If there are user-facing changes then we may require documentation to be
    updated before approving the PR.
    -->
    
    <!--
    If there are any breaking changes to public APIs, please add the `api
    change` label.
    -->
    
    ---------
    
    Co-authored-by: Martin Grigorov <[email protected]>
---
 datafusion/sqllogictest/src/test_context.rs | 47 +++++++++--------------------
 datafusion/sqllogictest/test_files/cte.slt  | 19 ++++++++++--
 2 files changed, 30 insertions(+), 36 deletions(-)

diff --git a/datafusion/sqllogictest/src/test_context.rs 
b/datafusion/sqllogictest/src/test_context.rs
index 8bd0cabcb0..b286c43321 100644
--- a/datafusion/sqllogictest/src/test_context.rs
+++ b/datafusion/sqllogictest/src/test_context.rs
@@ -98,9 +98,9 @@ impl TestContext {
 
         let file_name = relative_path.file_name().unwrap().to_str().unwrap();
         match file_name {
-            "cte_quoted_reference.slt" => {
-                info!("Registering strict catalog provider for CTE tests");
-                register_strict_orders_catalog(test_ctx.session_ctx());
+            "cte.slt" => {
+                info!("Registering strict schema provider for CTE tests");
+                register_strict_schema_provider(test_ctx.session_ctx());
             }
             "information_schema_table_types.slt" => {
                 info!("Registering local temporary table");
@@ -178,10 +178,10 @@ impl TestContext {
 }
 
 // 
==============================================================================
-// Strict Catalog / Schema Provider (sqllogictest-only)
+// Strict Schema Provider (sqllogictest-only)
 // 
==============================================================================
 //
-// The goal of `cte_quoted_reference.slt` is to exercise end-to-end query 
planning
+// The goal of `StrictOrdersSchema` is to exercise end-to-end query planning
 // while detecting *unexpected* catalog lookups.
 //
 // Specifically, if DataFusion incorrectly treats a CTE reference (e.g. 
`"barbaz"`)
@@ -192,26 +192,6 @@ impl TestContext {
 // This makes the "extra provider lookup" bug observable in an end-to-end test,
 // rather than being silently ignored by default providers that return 
`Ok(None)`
 // for unknown tables.
-
-#[derive(Debug)]
-struct StrictOrdersCatalog {
-    schema: Arc<dyn SchemaProvider>,
-}
-
-impl CatalogProvider for StrictOrdersCatalog {
-    fn as_any(&self) -> &dyn Any {
-        self
-    }
-
-    fn schema_names(&self) -> Vec<String> {
-        vec!["public".to_string()]
-    }
-
-    fn schema(&self, name: &str) -> Option<Arc<dyn SchemaProvider>> {
-        (name == "public").then(|| Arc::clone(&self.schema))
-    }
-}
-
 #[derive(Debug)]
 struct StrictOrdersSchema {
     orders: Arc<dyn TableProvider>,
@@ -245,7 +225,7 @@ impl SchemaProvider for StrictOrdersSchema {
     }
 }
 
-fn register_strict_orders_catalog(ctx: &SessionContext) {
+fn register_strict_schema_provider(ctx: &SessionContext) {
     let schema = Arc::new(Schema::new(vec![Field::new(
         "order_id",
         DataType::Int32,
@@ -265,13 +245,14 @@ fn register_strict_orders_catalog(ctx: &SessionContext) {
         orders: Arc::new(orders),
     });
 
-    // Override the default "datafusion" catalog for this test file so that any
-    // unexpected lookup is caught immediately.
-    ctx.register_catalog(
-        "datafusion",
-        Arc::new(StrictOrdersCatalog {
-            schema: schema_provider,
-        }),
+    let previous = ctx
+        .catalog("datafusion")
+        .expect("default catalog should exist")
+        .register_schema("strict_schema", schema_provider)
+        .expect("strict schema registration should succeed");
+    assert!(
+        previous.is_none(),
+        "strict_schema unexpectedly already existed in datafusion catalog"
     );
 }
 
diff --git a/datafusion/sqllogictest/test_files/cte.slt 
b/datafusion/sqllogictest/test_files/cte.slt
index 65f694eb37..a0a541a826 100644
--- a/datafusion/sqllogictest/test_files/cte.slt
+++ b/datafusion/sqllogictest/test_files/cte.slt
@@ -42,8 +42,6 @@ physical_plan
 statement error DataFusion error: Error during planning: WITH query name "a" 
specified more than once
 WITH a AS (SELECT 1), a AS (SELECT 2) SELECT * FROM a;
 
-statement ok
-CREATE TABLE orders AS VALUES (1), (2);
 
 ##########
 ## CTE Reference Resolution
@@ -59,10 +57,14 @@ CREATE TABLE orders AS VALUES (1), (2);
 #
 # Refs: https://github.com/apache/datafusion/issues/18932
 #
-# NOTE: This test relies on a strict catalog/schema provider registered in
+# NOTE: This test relies on a strict schema provider registered in
 # `datafusion/sqllogictest/src/test_context.rs` that provides only the `orders`
 # table and panics on unexpected lookups.
 
+# Use the strict schema provider
+statement ok
+set datafusion.catalog.default_schema = "strict_schema";
+
 statement ok
 set datafusion.sql_parser.enable_ident_normalization = true;
 
@@ -99,6 +101,17 @@ with barbaz as (select * from orders) select * from barbaz;
 1
 2
 
+# Reset to default configs
+statement ok
+set datafusion.sql_parser.enable_ident_normalization = true;
+
+statement ok
+set datafusion.catalog.default_schema = "public";
+
+## CTE reference resolution tests end ##
+
+
+
 # Test disabling recursive CTE
 statement ok
 set datafusion.execution.enable_recursive_ctes = false;


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to