From fd0e703b74e68d6bd4961b0a9dcf6ee1b86afa72 Mon Sep 17 00:00:00 2001 From: Kevin Longe <273747162+kvlonge@users.noreply.github.com> Date: Wed, 18 Nov 2026 16:19:33 +0000 Subject: [PATCH 1/2] fix(semantics): check the keys of a named window as column references A SELECT's clause check read its joins, filters, groups and orders but never a WINDOW clause, so an unknown column in a named window's PARTITION BY or ORDER BY went unreported in every dialect, both in SELECT ... WINDOW and in a WINDOW written after a parenthesized query. The keys of each named window are now checked at the level an inline OVER in a projection reads: output aliases in the dialects that read them anywhere, input columns only in the others. PostgreSQL, which follows SQL99 rules for a window definition, so refuses an output alias there. Databricks and PostgreSQL cases cover unknown partition and order keys, a window after the parentheses and an output alias. --- .../databricks/profile/tests/test_lineage.rs | 12 +++- .../src/lineage/_helpers/findings/levels.rs | 10 ++++ .../profile/tests/test_statement_findings.rs | 60 +++++++++++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) diff --git a/crates/sqlquack-semantics/src/databricks/profile/tests/test_lineage.rs b/crates/sqlquack-semantics/src/databricks/profile/tests/test_lineage.rs index ddc6060bb..cba7b298b 100644 --- a/crates/sqlquack-semantics/src/databricks/profile/tests/test_lineage.rs +++ b/crates/sqlquack-semantics/src/databricks/profile/tests/test_lineage.rs @@ -550,7 +550,7 @@ fn given_a_parenthesized_query_with_clauses_after_it_when_analysed_then_they_rea } #[test] -fn given_a_window_named_after_parentheses_when_checked_then_only_an_undefined_name_is_found() { +fn given_a_named_window_when_checked_then_an_undefined_name_or_unknown_key_is_found() { let test_cases = [ QueryFindingsTestCase { description: "the window function inside reads the window named after the parentheses", @@ -562,6 +562,16 @@ fn given_a_window_named_after_parentheses_when_checked_then_only_an_undefined_na sql: "( SELECT sum ( quantity ) OVER w2 AS x FROM orders LIMIT 1 ) WINDOW w AS ( ORDER BY quantity )", expected_findings: &[("E232", "Unknown named window 'w2'", 15)], }, + QueryFindingsTestCase { + description: "an unknown partition key in the window named after the parentheses", + sql: "( SELECT sum ( quantity ) OVER w AS x FROM orders LIMIT 1 ) WINDOW w AS ( PARTITION BY nope )", + expected_findings: &[("E201", "Unknown column 'nope' in table 'orders'", 87)], + }, + QueryFindingsTestCase { + description: "an unknown order key in a WINDOW clause without parentheses", + sql: "SELECT sum ( quantity ) OVER w AS x FROM orders WINDOW w AS ( ORDER BY nope )", + expected_findings: &[("E201", "Unknown column 'nope' in table 'orders'", 71)], + }, ]; for test_case in test_cases { assert_eq!( diff --git a/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs b/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs index 90acfc2c5..1f43e5f90 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs @@ -363,6 +363,16 @@ fn select_findings( own.extend(found); nested.extend(inner); } + let windows = std::iter::once(&select.windows) + .chain(after.iter().map(|node| &node.windows)) + .flatten() + .flatten(); + let windowed = if anywhere { level } else { unnamed }; + for key in windows.flat_map(window_keys) { + let (found, inner) = clause_findings(key, &windowed, context); + own.extend(found); + nested.extend(inner); + } own.sort_by_key(|finding| finding.span.map(|span| span.start)); own.extend(nested); own diff --git a/crates/sqlquack-semantics/src/postgres/profile/tests/test_statement_findings.rs b/crates/sqlquack-semantics/src/postgres/profile/tests/test_statement_findings.rs index 789beaaba..ffa2b82d3 100644 --- a/crates/sqlquack-semantics/src/postgres/profile/tests/test_statement_findings.rs +++ b/crates/sqlquack-semantics/src/postgres/profile/tests/test_statement_findings.rs @@ -265,3 +265,63 @@ fn given_a_join_in_parentheses_when_resolving_names_then_its_conditions_and_item ); } } + +#[test] +fn given_a_named_window_when_resolving_names_then_its_keys_read_the_input_columns() { + let test_cases = [ + QueryFindingsTestCase { + description: "known partition and order keys", + sql: "SELECT sum(amount) OVER w AS x FROM orders WINDOW w AS (PARTITION BY customer_id ORDER BY id)", + expected_findings: &[], + }, + QueryFindingsTestCase { + description: "an unknown order key", + sql: "SELECT sum(amount) OVER w AS x FROM orders WINDOW w AS (ORDER BY nope)", + expected_findings: &[("E201", "Unknown column 'nope' in table 'orders'", 65)], + }, + QueryFindingsTestCase { + description: "an unknown partition key", + sql: "SELECT sum(amount) OVER w AS x FROM orders WINDOW w AS (PARTITION BY nope)", + expected_findings: &[("E201", "Unknown column 'nope' in table 'orders'", 69)], + }, + QueryFindingsTestCase { + description: "an output alias, which a window definition does not read", + sql: "SELECT amount AS q, sum(amount) OVER w AS x FROM orders WINDOW w AS (ORDER BY q)", + expected_findings: &[("E201", "Unknown column 'q' in table 'orders'", 78)], + }, + ]; + let schema = duckdb_shop_schema(); + for test_case in test_cases { + let statement = sqlquack_syntax_postgres::parse_strict(test_case.sql) + .unwrap_or_else(|failure| panic!("{}: {failure:?}", test_case.description)) + .statements + .remove(0); + let context = LineageContext { + profile: &POSTGRES, + schema: &schema, + sql: Some(test_case.sql), + }; + let native: Vec<(&'static str, String, usize)> = statement_findings(&statement, context) + .into_iter() + .map(|finding| { + ( + finding.code, + finding.message, + finding.span.map_or(0, |span| span.start), + ) + }) + .collect(); + assert_eq!( + found_findings(test_case.sql, (Dialect::PostgreSQL, &POSTGRES, &schema)), + owned_findings(test_case.expected_findings), + "{}", + test_case.description + ); + assert_eq!( + native, + owned_findings(test_case.expected_findings), + "{}, PostgreSQL's parser", + test_case.description + ); + } +} From 838233fea4d4c34a1da86d0ae0efcea07e48aa6f Mon Sep 17 00:00:00 2001 From: Kevin Longe <273747162+kvlonge@users.noreply.github.com> Date: Wed, 18 Nov 2026 18:00:17 +0000 Subject: [PATCH 2/2] fix(semantics): check only the named windows an OVER reads The previous change checked the keys of every named window, so a window nothing used reported E201 or E222 for a key that is never resolved. DuckDB, SQLite and Databricks accept such a query, and Spark never resolves an unused window. A named window's keys are now checked only when an OVER in the query or in an ORDER BY after it names the window, directly or through a window that extends it. This holds in every dialect, so PostgreSQL's and ClickHouse's refusal of an unused bad window is not reported. DuckDB cases cover unused windows with unknown keys, qualifiers and a subquery, a used window, a window reached through another, and a key that reads an output alias. The previous commit's message said its Databricks cases covered an output alias; only the PostgreSQL ones do. --- .../profile/tests/test_statement_findings.rs | 49 ++++++++++++++++ .../src/lineage/_helpers/findings/levels.rs | 56 ++++++++++++++++++- 2 files changed, 102 insertions(+), 3 deletions(-) diff --git a/crates/sqlquack-semantics/src/duckdb/profile/tests/test_statement_findings.rs b/crates/sqlquack-semantics/src/duckdb/profile/tests/test_statement_findings.rs index 01755aae8..1867ddff0 100644 --- a/crates/sqlquack-semantics/src/duckdb/profile/tests/test_statement_findings.rs +++ b/crates/sqlquack-semantics/src/duckdb/profile/tests/test_statement_findings.rs @@ -648,3 +648,52 @@ fn given_duckdbs_parser_reads_window_calls_when_checking_arity_then_every_count_ ); } } + +#[test] +fn given_named_windows_when_resolving_names_then_only_the_windows_an_over_reads_are_checked() { + let test_cases = [ + QueryFindingsTestCase { + description: "a window key reads an output alias, as DuckDB's windows do", + sql: "SELECT amount AS q, sum(amount) OVER w AS x FROM orders WINDOW w AS (ORDER BY q)", + expected_findings: &[], + }, + QueryFindingsTestCase { + description: "an unused window with an unknown order key", + sql: "SELECT amount FROM orders WINDOW w AS (ORDER BY nope)", + expected_findings: &[], + }, + QueryFindingsTestCase { + description: "an unused window beside a used one", + sql: "SELECT sum(amount) OVER w AS x FROM orders WINDOW w AS (ORDER BY id), w2 AS (ORDER BY nope)", + expected_findings: &[], + }, + QueryFindingsTestCase { + description: "an unused window with an unknown qualifier", + sql: "SELECT amount FROM orders WINDOW w AS (PARTITION BY zz.amount)", + expected_findings: &[], + }, + QueryFindingsTestCase { + description: "an unused window whose key is a subquery reading an unknown column", + sql: "SELECT amount FROM orders WINDOW w AS (ORDER BY (SELECT max(nope_in) FROM customers))", + expected_findings: &[], + }, + QueryFindingsTestCase { + description: "a used window with an unknown order key", + sql: "SELECT sum(amount) OVER w AS x FROM orders WINDOW w AS (ORDER BY nope)", + expected_findings: &[("E201", "Unknown column 'nope' in table 'orders'", 65)], + }, + QueryFindingsTestCase { + description: "a window that the used window extends", + sql: "SELECT sum(amount) OVER w2 AS x FROM orders WINDOW w AS (PARTITION BY nope), w2 AS (w ORDER BY id)", + expected_findings: &[("E201", "Unknown column 'nope' in table 'orders'", 70)], + }, + ]; + for test_case in test_cases { + assert_eq!( + duckdb_parser_findings(test_case.sql), + owned_findings(test_case.expected_findings), + "{}", + test_case.description + ); + } +} diff --git a/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs b/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs index 1f43e5f90..0afe8fc55 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs @@ -363,12 +363,17 @@ fn select_findings( own.extend(found); nested.extend(inner); } - let windows = std::iter::once(&select.windows) + let windows: Vec<&NamedWindow> = std::iter::once(&select.windows) .chain(after.iter().map(|node| &node.windows)) .flatten() - .flatten(); + .flatten() + .collect(); + let reads: Vec<&Expression> = std::iter::once(node) + .chain(orders.iter().flat_map(|order| order_keys(order))) + .collect(); let windowed = if anywhere { level } else { unnamed }; - for key in windows.flat_map(window_keys) { + let used: Vec<&NamedWindow> = used_windows(windows, &reads, context); + for key in used.into_iter().flat_map(window_keys) { let (found, inner) = clause_findings(key, &windowed, context); own.extend(found); nested.extend(inner); @@ -412,6 +417,51 @@ fn ordered_name_place(select: &Select, after: &[&OrderBy], clause: &Expression) .any(|ordered| std::ptr::eq(&ordered.this, clause)) } +/// The windows of `windows` an OVER in `reads` names, directly or through a window extending it. +fn used_windows<'w>( + windows: Vec<&'w NamedWindow>, + reads: &[&Expression], + context: LineageContext<'_>, +) -> Vec<&'w NamedWindow> { + if windows.is_empty() { + return windows; + } + let mut wanted: Vec = reads + .iter() + .flat_map(|read| in_scope(read)) + .filter_map(|node| match node { + Expression::WindowFunction(window) => window.over.window_name.as_ref(), + _ => None, + }) + .map(|name| key(name, context)) + .collect(); + let mut used: Vec = vec![false; windows.len()]; + while let Some(name) = wanted.pop() { + for (index, window) in windows.iter().enumerate() { + if used[index] + || !context + .profile + .same_identifier(&key(&window.name, context), &name) + { + continue; + } + used[index] = true; + wanted.extend( + window + .spec + .window_name + .iter() + .map(|base| key(base, context)), + ); + } + } + windows + .into_iter() + .zip(used) + .filter_map(|(window, used)| used.then_some(window)) + .collect() +} + /// The partition and order keys of `window`. fn window_keys(window: &NamedWindow) -> impl Iterator { let orders = window.spec.order_by.iter().map(|ordered| &ordered.this);