Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}
60 changes: 60 additions & 0 deletions crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,21 @@ fn select_findings(
own.extend(found);
nested.extend(inner);
}
let windows: Vec<&NamedWindow> = std::iter::once(&select.windows)
.chain(after.iter().map(|node| &node.windows))
.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 };
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);
}
own.sort_by_key(|finding| finding.span.map(|span| span.start));
own.extend(nested);
own
Expand Down Expand Up @@ -402,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<String> = 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<bool> = 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<Item = &Expression> {
let orders = window.spec.order_by.iter().map(|ordered| &ordered.this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}
Loading