Skip to content

Commit 85a34e9

Browse files
jonahgaomartin-g
andauthored
Fix CTE reference resolution slt tests (#21049)
## Which issue does this PR close? N/A ## Rationale for this change PR #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 #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 <martin-g@users.noreply.github.com>
1 parent 59e8a79 commit 85a34e9

File tree

2 files changed

+30
-36
lines changed

2 files changed

+30
-36
lines changed

datafusion/sqllogictest/src/test_context.rs

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ impl TestContext {
9898

9999
let file_name = relative_path.file_name().unwrap().to_str().unwrap();
100100
match file_name {
101-
"cte_quoted_reference.slt" => {
102-
info!("Registering strict catalog provider for CTE tests");
103-
register_strict_orders_catalog(test_ctx.session_ctx());
101+
"cte.slt" => {
102+
info!("Registering strict schema provider for CTE tests");
103+
register_strict_schema_provider(test_ctx.session_ctx());
104104
}
105105
"information_schema_table_types.slt" => {
106106
info!("Registering local temporary table");
@@ -178,10 +178,10 @@ impl TestContext {
178178
}
179179

180180
// ==============================================================================
181-
// Strict Catalog / Schema Provider (sqllogictest-only)
181+
// Strict Schema Provider (sqllogictest-only)
182182
// ==============================================================================
183183
//
184-
// The goal of `cte_quoted_reference.slt` is to exercise end-to-end query planning
184+
// The goal of `StrictOrdersSchema` is to exercise end-to-end query planning
185185
// while detecting *unexpected* catalog lookups.
186186
//
187187
// Specifically, if DataFusion incorrectly treats a CTE reference (e.g. `"barbaz"`)
@@ -192,26 +192,6 @@ impl TestContext {
192192
// This makes the "extra provider lookup" bug observable in an end-to-end test,
193193
// rather than being silently ignored by default providers that return `Ok(None)`
194194
// for unknown tables.
195-
196-
#[derive(Debug)]
197-
struct StrictOrdersCatalog {
198-
schema: Arc<dyn SchemaProvider>,
199-
}
200-
201-
impl CatalogProvider for StrictOrdersCatalog {
202-
fn as_any(&self) -> &dyn Any {
203-
self
204-
}
205-
206-
fn schema_names(&self) -> Vec<String> {
207-
vec!["public".to_string()]
208-
}
209-
210-
fn schema(&self, name: &str) -> Option<Arc<dyn SchemaProvider>> {
211-
(name == "public").then(|| Arc::clone(&self.schema))
212-
}
213-
}
214-
215195
#[derive(Debug)]
216196
struct StrictOrdersSchema {
217197
orders: Arc<dyn TableProvider>,
@@ -245,7 +225,7 @@ impl SchemaProvider for StrictOrdersSchema {
245225
}
246226
}
247227

248-
fn register_strict_orders_catalog(ctx: &SessionContext) {
228+
fn register_strict_schema_provider(ctx: &SessionContext) {
249229
let schema = Arc::new(Schema::new(vec![Field::new(
250230
"order_id",
251231
DataType::Int32,
@@ -265,13 +245,14 @@ fn register_strict_orders_catalog(ctx: &SessionContext) {
265245
orders: Arc::new(orders),
266246
});
267247

268-
// Override the default "datafusion" catalog for this test file so that any
269-
// unexpected lookup is caught immediately.
270-
ctx.register_catalog(
271-
"datafusion",
272-
Arc::new(StrictOrdersCatalog {
273-
schema: schema_provider,
274-
}),
248+
let previous = ctx
249+
.catalog("datafusion")
250+
.expect("default catalog should exist")
251+
.register_schema("strict_schema", schema_provider)
252+
.expect("strict schema registration should succeed");
253+
assert!(
254+
previous.is_none(),
255+
"strict_schema unexpectedly already existed in datafusion catalog"
275256
);
276257
}
277258

datafusion/sqllogictest/test_files/cte.slt

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ physical_plan
4242
statement error DataFusion error: Error during planning: WITH query name "a" specified more than once
4343
WITH a AS (SELECT 1), a AS (SELECT 2) SELECT * FROM a;
4444

45-
statement ok
46-
CREATE TABLE orders AS VALUES (1), (2);
4745

4846
##########
4947
## CTE Reference Resolution
@@ -59,10 +57,14 @@ CREATE TABLE orders AS VALUES (1), (2);
5957
#
6058
# Refs: https://github.com/apache/datafusion/issues/18932
6159
#
62-
# NOTE: This test relies on a strict catalog/schema provider registered in
60+
# NOTE: This test relies on a strict schema provider registered in
6361
# `datafusion/sqllogictest/src/test_context.rs` that provides only the `orders`
6462
# table and panics on unexpected lookups.
6563

64+
# Use the strict schema provider
65+
statement ok
66+
set datafusion.catalog.default_schema = "strict_schema";
67+
6668
statement ok
6769
set datafusion.sql_parser.enable_ident_normalization = true;
6870

@@ -99,6 +101,17 @@ with barbaz as (select * from orders) select * from barbaz;
99101
1
100102
2
101103

104+
# Reset to default configs
105+
statement ok
106+
set datafusion.sql_parser.enable_ident_normalization = true;
107+
108+
statement ok
109+
set datafusion.catalog.default_schema = "public";
110+
111+
## CTE reference resolution tests end ##
112+
113+
114+
102115
# Test disabling recursive CTE
103116
statement ok
104117
set datafusion.execution.enable_recursive_ctes = false;

0 commit comments

Comments
 (0)