Skip to content

Commit ffbf6f8

Browse files
committed
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.
1 parent 4dd6098 commit ffbf6f8

3 files changed

Lines changed: 81 additions & 1 deletion

File tree

‎crates/sqlquack-semantics/src/databricks/profile/tests/test_lineage.rs‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ fn given_a_parenthesized_query_with_clauses_after_it_when_analysed_then_they_rea
550550
}
551551

552552
#[test]
553-
fn given_a_window_named_after_parentheses_when_checked_then_only_an_undefined_name_is_found() {
553+
fn given_a_named_window_when_checked_then_an_undefined_name_or_unknown_key_is_found() {
554554
let test_cases = [
555555
QueryFindingsTestCase {
556556
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
562562
sql: "( SELECT sum ( quantity ) OVER w2 AS x FROM orders LIMIT 1 ) WINDOW w AS ( ORDER BY quantity )",
563563
expected_findings: &[("E232", "Unknown named window 'w2'", 15)],
564564
},
565+
QueryFindingsTestCase {
566+
description: "an unknown partition key in the window named after the parentheses",
567+
sql: "( SELECT sum ( quantity ) OVER w AS x FROM orders LIMIT 1 ) WINDOW w AS ( PARTITION BY nope )",
568+
expected_findings: &[("E201", "Unknown column 'nope' in table 'orders'", 87)],
569+
},
570+
QueryFindingsTestCase {
571+
description: "an unknown order key in a WINDOW clause without parentheses",
572+
sql: "SELECT sum ( quantity ) OVER w AS x FROM orders WINDOW w AS ( ORDER BY nope )",
573+
expected_findings: &[("E201", "Unknown column 'nope' in table 'orders'", 71)],
574+
},
565575
];
566576
for test_case in test_cases {
567577
assert_eq!(

‎crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,16 @@ fn select_findings(
363363
own.extend(found);
364364
nested.extend(inner);
365365
}
366+
let windows = std::iter::once(&select.windows)
367+
.chain(after.iter().map(|node| &node.windows))
368+
.flatten()
369+
.flatten();
370+
let windowed = if anywhere { level } else { unnamed };
371+
for key in windows.flat_map(window_keys) {
372+
let (found, inner) = clause_findings(key, &windowed, context);
373+
own.extend(found);
374+
nested.extend(inner);
375+
}
366376
own.sort_by_key(|finding| finding.span.map(|span| span.start));
367377
own.extend(nested);
368378
own

‎crates/sqlquack-semantics/src/postgres/profile/tests/test_statement_findings.rs‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,63 @@ fn given_a_join_in_parentheses_when_resolving_names_then_its_conditions_and_item
265265
);
266266
}
267267
}
268+
269+
#[test]
270+
fn given_a_named_window_when_resolving_names_then_its_keys_read_the_input_columns() {
271+
let test_cases = [
272+
QueryFindingsTestCase {
273+
description: "known partition and order keys",
274+
sql: "SELECT sum(amount) OVER w AS x FROM orders WINDOW w AS (PARTITION BY customer_id ORDER BY id)",
275+
expected_findings: &[],
276+
},
277+
QueryFindingsTestCase {
278+
description: "an unknown order key",
279+
sql: "SELECT sum(amount) OVER w AS x FROM orders WINDOW w AS (ORDER BY nope)",
280+
expected_findings: &[("E201", "Unknown column 'nope' in table 'orders'", 65)],
281+
},
282+
QueryFindingsTestCase {
283+
description: "an unknown partition key",
284+
sql: "SELECT sum(amount) OVER w AS x FROM orders WINDOW w AS (PARTITION BY nope)",
285+
expected_findings: &[("E201", "Unknown column 'nope' in table 'orders'", 69)],
286+
},
287+
QueryFindingsTestCase {
288+
description: "an output alias, which a window definition does not read",
289+
sql: "SELECT amount AS q, sum(amount) OVER w AS x FROM orders WINDOW w AS (ORDER BY q)",
290+
expected_findings: &[("E201", "Unknown column 'q' in table 'orders'", 78)],
291+
},
292+
];
293+
let schema = duckdb_shop_schema();
294+
for test_case in test_cases {
295+
let statement = sqlquack_syntax_postgres::parse_strict(test_case.sql)
296+
.unwrap_or_else(|failure| panic!("{}: {failure:?}", test_case.description))
297+
.statements
298+
.remove(0);
299+
let context = LineageContext {
300+
profile: &POSTGRES,
301+
schema: &schema,
302+
sql: Some(test_case.sql),
303+
};
304+
let native: Vec<(&'static str, String, usize)> = statement_findings(&statement, context)
305+
.into_iter()
306+
.map(|finding| {
307+
(
308+
finding.code,
309+
finding.message,
310+
finding.span.map_or(0, |span| span.start),
311+
)
312+
})
313+
.collect();
314+
assert_eq!(
315+
found_findings(test_case.sql, (Dialect::PostgreSQL, &POSTGRES, &schema)),
316+
owned_findings(test_case.expected_findings),
317+
"{}",
318+
test_case.description
319+
);
320+
assert_eq!(
321+
native,
322+
owned_findings(test_case.expected_findings),
323+
"{}, PostgreSQL's parser",
324+
test_case.description
325+
);
326+
}
327+
}

0 commit comments

Comments
 (0)