Skip to content

Commit 6e472d2

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

2 files changed

Lines changed: 102 additions & 3 deletions

File tree

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,3 +648,52 @@ fn given_duckdbs_parser_reads_window_calls_when_checking_arity_then_every_count_
648648
);
649649
}
650650
}
651+
652+
#[test]
653+
fn given_named_windows_when_resolving_names_then_only_the_windows_an_over_reads_are_checked() {
654+
let test_cases = [
655+
QueryFindingsTestCase {
656+
description: "a window key reads an output alias, as DuckDB's windows do",
657+
sql: "SELECT amount AS q, sum(amount) OVER w AS x FROM orders WINDOW w AS (ORDER BY q)",
658+
expected_findings: &[],
659+
},
660+
QueryFindingsTestCase {
661+
description: "an unused window with an unknown order key",
662+
sql: "SELECT amount FROM orders WINDOW w AS (ORDER BY nope)",
663+
expected_findings: &[],
664+
},
665+
QueryFindingsTestCase {
666+
description: "an unused window beside a used one",
667+
sql: "SELECT sum(amount) OVER w AS x FROM orders WINDOW w AS (ORDER BY id), w2 AS (ORDER BY nope)",
668+
expected_findings: &[],
669+
},
670+
QueryFindingsTestCase {
671+
description: "an unused window with an unknown qualifier",
672+
sql: "SELECT amount FROM orders WINDOW w AS (PARTITION BY zz.amount)",
673+
expected_findings: &[],
674+
},
675+
QueryFindingsTestCase {
676+
description: "an unused window whose key is a subquery reading an unknown column",
677+
sql: "SELECT amount FROM orders WINDOW w AS (ORDER BY (SELECT max(nope_in) FROM customers))",
678+
expected_findings: &[],
679+
},
680+
QueryFindingsTestCase {
681+
description: "a used window with an unknown order key",
682+
sql: "SELECT sum(amount) OVER w AS x FROM orders WINDOW w AS (ORDER BY nope)",
683+
expected_findings: &[("E201", "Unknown column 'nope' in table 'orders'", 65)],
684+
},
685+
QueryFindingsTestCase {
686+
description: "a window that the used window extends",
687+
sql: "SELECT sum(amount) OVER w2 AS x FROM orders WINDOW w AS (PARTITION BY nope), w2 AS (w ORDER BY id)",
688+
expected_findings: &[("E201", "Unknown column 'nope' in table 'orders'", 70)],
689+
},
690+
];
691+
for test_case in test_cases {
692+
assert_eq!(
693+
duckdb_parser_findings(test_case.sql),
694+
owned_findings(test_case.expected_findings),
695+
"{}",
696+
test_case.description
697+
);
698+
}
699+
}

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

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -363,12 +363,17 @@ fn select_findings(
363363
own.extend(found);
364364
nested.extend(inner);
365365
}
366-
let windows = std::iter::once(&select.windows)
366+
let windows: Vec<&NamedWindow> = std::iter::once(&select.windows)
367367
.chain(after.iter().map(|node| &node.windows))
368368
.flatten()
369-
.flatten();
369+
.flatten()
370+
.collect();
371+
let reads: Vec<&Expression> = std::iter::once(node)
372+
.chain(orders.iter().flat_map(|order| order_keys(order)))
373+
.collect();
370374
let windowed = if anywhere { level } else { unnamed };
371-
for key in windows.flat_map(window_keys) {
375+
let used: Vec<&NamedWindow> = used_windows(windows, &reads, context);
376+
for key in used.into_iter().flat_map(window_keys) {
372377
let (found, inner) = clause_findings(key, &windowed, context);
373378
own.extend(found);
374379
nested.extend(inner);
@@ -412,6 +417,51 @@ fn ordered_name_place(select: &Select, after: &[&OrderBy], clause: &Expression)
412417
.any(|ordered| std::ptr::eq(&ordered.this, clause))
413418
}
414419

420+
/// The windows of `windows` an OVER in `reads` names, directly or through a window extending it.
421+
fn used_windows<'w>(
422+
windows: Vec<&'w NamedWindow>,
423+
reads: &[&Expression],
424+
context: LineageContext<'_>,
425+
) -> Vec<&'w NamedWindow> {
426+
if windows.is_empty() {
427+
return windows;
428+
}
429+
let mut wanted: Vec<String> = reads
430+
.iter()
431+
.flat_map(|read| in_scope(read))
432+
.filter_map(|node| match node {
433+
Expression::WindowFunction(window) => window.over.window_name.as_ref(),
434+
_ => None,
435+
})
436+
.map(|name| key(name, context))
437+
.collect();
438+
let mut used: Vec<bool> = vec![false; windows.len()];
439+
while let Some(name) = wanted.pop() {
440+
for (index, window) in windows.iter().enumerate() {
441+
if used[index]
442+
|| !context
443+
.profile
444+
.same_identifier(&key(&window.name, context), &name)
445+
{
446+
continue;
447+
}
448+
used[index] = true;
449+
wanted.extend(
450+
window
451+
.spec
452+
.window_name
453+
.iter()
454+
.map(|base| key(base, context)),
455+
);
456+
}
457+
}
458+
windows
459+
.into_iter()
460+
.zip(used)
461+
.filter_map(|(window, used)| used.then_some(window))
462+
.collect()
463+
}
464+
415465
/// The partition and order keys of `window`.
416466
fn window_keys(window: &NamedWindow) -> impl Iterator<Item = &Expression> {
417467
let orders = window.spec.order_by.iter().map(|ordered| &ordered.this);

0 commit comments

Comments
 (0)