From 05a35015eea964826364ca925d7e7d4d296aa53a Mon Sep 17 00:00:00 2001 From: Kevin Longe <273747162+kvlonge@users.noreply.github.com> Date: Wed, 18 Nov 2026 19:45:20 +0000 Subject: [PATCH 01/24] fix(databricks): keep raw strings in path keys and resources A raw string in a semi-structured path key and in a resource gave the same tree as the plain string: SELECT c1 : [ r'k' ] FROM t1 and SELECT c1 : [ 'k' ] FROM t1, and ADD FILE r'v1' and ADD FILE 'v1' (the same for ADD JAR and LIST FILE). A path key now keeps r'k' as a raw string literal. A resource unquotes only a plain string; a raw one is kept as written, as Spark's resource commands unquote only text that begins with a quote. --- .../src/parse/_helpers/tree/forms.rs | 8 ++++++-- .../src/parse/_helpers/values/primary.rs | 2 +- .../src/parse/tests/test_written_forms.rs | 20 +++++++++++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/crates/sqlquack-syntax-databricks/src/parse/_helpers/tree/forms.rs b/crates/sqlquack-syntax-databricks/src/parse/_helpers/tree/forms.rs index 1c6a9a31d..8cfdc8ec7 100644 --- a/crates/sqlquack-syntax-databricks/src/parse/_helpers/tree/forms.rs +++ b/crates/sqlquack-syntax-databricks/src/parse/_helpers/tree/forms.rs @@ -409,12 +409,16 @@ impl Parser<'_> { }))) } - /// `resource`: tokens with no space between them as written, as Spark splits resources, or a string. + /// `resource`: unspaced tokens as written, as Spark splits resources, or a string; `r'x'` stays written. fn resource(&mut self) -> Parsing { if self.ends_at(0) { return Err(self.error()); } - if self.string_at(0) && !self.adjacent(0) { + let quoted: bool = matches!( + self.peek().map(|token| token.kind), + Some(TokenKind::String { raw: false }) + ); + if quoted && !self.adjacent(0) { return Ok(string(&self.advance()?.value)); } let first: usize = self.at; diff --git a/crates/sqlquack-syntax-databricks/src/parse/_helpers/values/primary.rs b/crates/sqlquack-syntax-databricks/src/parse/_helpers/values/primary.rs index 09476ac8b..00c15058f 100644 --- a/crates/sqlquack-syntax-databricks/src/parse/_helpers/values/primary.rs +++ b/crates/sqlquack-syntax-databricks/src/parse/_helpers/values/primary.rs @@ -895,7 +895,7 @@ impl Parser<'_> { fn path_bracket(&mut self, names: bool) -> Parsing { self.expect_char('[')?; let index: Expression = match self.peek().map(|token| token.kind) { - Some(TokenKind::String { .. }) => string(&self.advance()?.value), + Some(TokenKind::String { .. }) => literal(self.one_string()?), Some(TokenKind::Number(NumberKind::Integer)) => { literal(Literal::Number(self.advance()?.value)) } diff --git a/crates/sqlquack-syntax-databricks/src/parse/tests/test_written_forms.rs b/crates/sqlquack-syntax-databricks/src/parse/tests/test_written_forms.rs index 9e667f9c9..5ffdd1b95 100644 --- a/crates/sqlquack-syntax-databricks/src/parse/tests/test_written_forms.rs +++ b/crates/sqlquack-syntax-databricks/src/parse/tests/test_written_forms.rs @@ -28,6 +28,26 @@ fn given_one_form_written_in_different_ways_when_parsing_then_each_tree_keeps_wh written: &["ADD FILE v1", "ADD FILE `v1`", "ADD FILE 'v1'"], expected_distinct_trees: 3, }, + DistinctTreesTestCase { + description: "a resource written as a raw string and as a string", + written: &[ + "ADD FILE r'v1'", + "ADD FILE 'v1'", + "ADD JAR r'v1'", + "ADD JAR 'v1'", + "LIST FILE r'v1'", + "LIST FILE 'v1'", + ], + expected_distinct_trees: 6, + }, + DistinctTreesTestCase { + description: "a path key written as a raw string and as a string", + written: &[ + "SELECT c1 : [ r'k' ] FROM t1", + "SELECT c1 : [ 'k' ] FROM t1", + ], + expected_distinct_trees: 2, + }, DistinctTreesTestCase { description: "a listed resource written backquoted and as a string holding the backquotes", written: &["LIST FILE `v1 Q`", "LIST FILE '`v1 Q`'"], From ae39a854a0cd715ab48d0a492aab7228f5ba013e Mon Sep 17 00:00:00 2001 From: Kevin Longe <273747162+kvlonge@users.noreply.github.com> Date: Wed, 18 Nov 2026 19:51:28 +0000 Subject: [PATCH 02/24] fix(databricks): keep a returned table's columns apart from their spacing A function's RETURNS TABLE ( ... ) kept its column list as the source text, whitespace and comments included, so reformatting the statement changed its tree and the formatter round trip failed: RETURNS TABLE ( c1 INT , c2 STRING ) and RETURNS TABLE(c1 INT, c2 STRING) gave two trees. returns_table_body now holds the list's tokens separated by single spaces, so spacing and comments no longer reach the tree while every name and type still does. --- crates/sqlquack-syntax-databricks/README.md | 6 +++--- .../src/parse/_helpers/commands/routines.rs | 8 +++++--- .../src/parse/tests/test_written_forms.rs | 10 ++++++++++ 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/crates/sqlquack-syntax-databricks/README.md b/crates/sqlquack-syntax-databricks/README.md index 33c0cc7f4..ba93de75a 100644 --- a/crates/sqlquack-syntax-databricks/README.md +++ b/crates/sqlquack-syntax-databricks/README.md @@ -164,9 +164,9 @@ These parse without a gap, but the shared tree keeps less than the source said: - `TIMESTAMP_LTZ` and `TIMESTAMP_NTZ` literals become a `Cast` to a custom type; collations on string types are dropped. - `COPY *` on a view becomes `copy_grants`. -- A function's `RETURNS TABLE (columns)` keeps the column list as `returns_table_body` text; its - `COMMENT`, `SPECIFIC` and `DEFAULT COLLATION` become `options` properties, and parameter - comments and `NOT NULL` are dropped. +- A function's `RETURNS TABLE (columns)` keeps the column list as `returns_table_body`, its + tokens separated by single spaces; its `COMMENT`, `SPECIFIC` and `DEFAULT COLLATION` become + `options` properties, and parameter comments and `NOT NULL` are dropped. - `SHOW TBLPROPERTIES t (key)` holds the key as the `scope` of `scope_kind` `PROPERTY`, a key written as a string literal as a string and any other as its dotted identifier: the shared `Show` has no field for a property key. diff --git a/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/routines.rs b/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/routines.rs index aae17a33d..ccd3abe7f 100644 --- a/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/routines.rs +++ b/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/routines.rs @@ -182,9 +182,11 @@ impl Parser<'_> { let first: usize = self.at; if self.is_char('(') { self.parenthesized(|parser| parser.function_parameter())?; - let from: usize = self.tokens[first].start; - node.returns_table_body = - Some(self.sql[from..self.tokens[self.at - 1].end].to_owned()); + let written: Vec<&str> = self.tokens[first..self.at] + .iter() + .map(|token| &self.sql[token.start..token.end]) + .collect(); + node.returns_table_body = Some(written.join(" ")); } } else { node.return_type = Some(self.data_type()?); diff --git a/crates/sqlquack-syntax-databricks/src/parse/tests/test_written_forms.rs b/crates/sqlquack-syntax-databricks/src/parse/tests/test_written_forms.rs index 5ffdd1b95..fa2bed5fa 100644 --- a/crates/sqlquack-syntax-databricks/src/parse/tests/test_written_forms.rs +++ b/crates/sqlquack-syntax-databricks/src/parse/tests/test_written_forms.rs @@ -28,6 +28,16 @@ fn given_one_form_written_in_different_ways_when_parsing_then_each_tree_keeps_wh written: &["ADD FILE v1", "ADD FILE `v1`", "ADD FILE 'v1'"], expected_distinct_trees: 3, }, + DistinctTreesTestCase { + description: "a returned table's columns with different spacing and comments", + written: &[ + "CREATE FUNCTION f1 ( ) RETURNS TABLE ( c1 INT , c2 STRING ) RETURN SELECT 1 , 'a'", + "CREATE FUNCTION f1 ( ) RETURNS TABLE(c1 INT, c2 STRING) RETURN SELECT 1 , 'a'", + "CREATE FUNCTION f1 ( ) RETURNS TABLE (\n c1 INT, -- x\n c2 STRING\n) RETURN SELECT 1 , 'a'", + "CREATE FUNCTION f1 ( ) RETURNS TABLE ( c1 BIGINT , c2 STRING ) RETURN SELECT 1 , 'a'", + ], + expected_distinct_trees: 2, + }, DistinctTreesTestCase { description: "a resource written as a raw string and as a string", written: &[ From eb464301fedaaea2a6c1d84f20e3c3c671319523 Mon Sep 17 00:00:00 2001 From: Kevin Longe <273747162+kvlonge@users.noreply.github.com> Date: Wed, 18 Nov 2026 20:32:30 +0000 Subject: [PATCH 03/24] fix(semantics): find a named window in any case where names fold An OVER window name was looked up among the WINDOW clause's names by its exact key, so SELECT sum(b) OVER W AS x FROM t WINDOW w AS (...) gave a false E232 "Unknown named window 'W'" in DuckDB and SQLite, which resolve W to w. The check for which windows an OVER reaches already compared names the profile's way. The lookup now compares names with the profile's identifier rule too. A DuckDB case covers a window named in another case. --- .../src/duckdb/profile/tests/test_statement_findings.rs | 5 +++++ .../src/lineage/_helpers/checks/scope_rules.rs | 9 +++++++-- 2 files changed, 12 insertions(+), 2 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 2d8651296..6a9287221 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 @@ -683,6 +683,11 @@ fn given_named_windows_when_resolving_names_then_only_the_windows_an_over_reads_ 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 named in another case, which DuckDB resolves to it", + 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: "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)", diff --git a/crates/sqlquack-semantics/src/lineage/_helpers/checks/scope_rules.rs b/crates/sqlquack-semantics/src/lineage/_helpers/checks/scope_rules.rs index 267cd2ead..52e8f63a7 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/checks/scope_rules.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/checks/scope_rules.rs @@ -334,7 +334,7 @@ fn structure_rules( )); } } - let windows: HashSet<&str> = level + let windows: Vec<&str> = level .scope .windows .iter() @@ -345,7 +345,12 @@ fn structure_rules( if let Expression::WindowFunction(window) = expression { has_window = true; if let Some(name) = &window.over.window_name - && !windows.contains(level.key(name).as_str()) + && !windows.iter().any(|known| { + level + .context + .profile + .same_identifier(known, &level.key(name)) + }) { findings.push(rule( "E232", From 6beb3290bcf36d1d904c4cde666178e1a8db0648 Mon Sep 17 00:00:00 2001 From: Kevin Longe <273747162+kvlonge@users.noreply.github.com> Date: Wed, 18 Nov 2026 21:39:23 +0000 Subject: [PATCH 04/24] fix(semantics): report an unknown base of a window an OVER reads A named window can extend another, as WINDOW w2 AS (zz ORDER BY b). When an OVER read w2 and zz named no window, no finding was reported, while PostgreSQL, DuckDB and SQLite refuse the query. The window an OVER reads is now followed along its bases, and a base no WINDOW clause names gives E232 "Unknown named window" at the base's name. As for a window's keys, a window no OVER reaches is not checked. DuckDB cases cover a direct and a two-step unknown base and an unused window. --- .../profile/tests/test_statement_findings.rs | 15 +++++ .../lineage/_helpers/checks/scope_rules.rs | 59 +++++++++++++++---- 2 files changed, 64 insertions(+), 10 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 6a9287221..b4004662c 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 @@ -688,6 +688,21 @@ fn given_named_windows_when_resolving_names_then_only_the_windows_an_over_reads_ 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: "a used window extending an unknown window", + sql: "SELECT sum(amount) OVER w2 AS x FROM orders WINDOW w AS (ORDER BY id), w2 AS (zz ORDER BY id)", + expected_findings: &[("E232", "Unknown named window 'zz'", 78)], + }, + QueryFindingsTestCase { + description: "an unknown window two steps along the used window's chain", + sql: "SELECT sum(amount) OVER w3 AS x FROM orders WINDOW w2 AS (zz ORDER BY id), w3 AS (w2)", + expected_findings: &[("E232", "Unknown named window 'zz'", 58)], + }, + QueryFindingsTestCase { + description: "an unused window extending an unknown window", + sql: "SELECT amount FROM orders WINDOW w2 AS (zz ORDER BY id)", + expected_findings: &[], + }, 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)", diff --git a/crates/sqlquack-semantics/src/lineage/_helpers/checks/scope_rules.rs b/crates/sqlquack-semantics/src/lineage/_helpers/checks/scope_rules.rs index 52e8f63a7..7fe3cfb0d 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/checks/scope_rules.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/checks/scope_rules.rs @@ -279,6 +279,40 @@ fn output_aliases<'s>( aliases } +/// An unknown base of a window an OVER in `read` reaches, as `w2 AS (zz ORDER BY b)`. +fn unknown_bases<'s>( + (node, select): (&Expression, &'s Select), + mut read: Vec<&'s Identifier>, + level: RuleLevel<'_, '_>, + known: &dyn Fn(&Identifier) -> bool, +) -> Vec { + let mut findings: Vec = Vec::new(); + let mut seen: HashSet = HashSet::new(); + while let Some(name) = read.pop() { + if !seen.insert(level.key(name)) { + continue; + } + let definitions = select.windows.iter().flatten().filter(|window| { + level + .context + .profile + .same_identifier(&level.key(&window.name), &level.key(name)) + }); + for base in definitions.filter_map(|window| window.spec.window_name.as_ref()) { + match known(base) { + true => read.push(base), + false => findings.push(issue( + ("E232", SCOPE_PHASE), + format!("Unknown named window '{}'", base.name), + base.span, + node, + )), + } + } + } + findings +} + /// Duplicate names, unknown named windows, QUALIFY without a window, and bad LIMIT or OFFSET. fn structure_rules( (node, select): (&Expression, &Select), @@ -340,27 +374,32 @@ fn structure_rules( .iter() .map(|(name, _)| name.as_str()) .collect(); + let known = |name: &Identifier| { + windows.iter().any(|known| { + level + .context + .profile + .same_identifier(known, &level.key(name)) + }) + }; let mut has_window: bool = false; + let mut read: Vec<&Identifier> = Vec::new(); for expression in in_scope(node) { if let Expression::WindowFunction(window) = expression { has_window = true; - if let Some(name) = &window.over.window_name - && !windows.iter().any(|known| { - level - .context - .profile - .same_identifier(known, &level.key(name)) - }) - { - findings.push(rule( + match &window.over.window_name { + Some(name) if !known(name) => findings.push(rule( "E232", format!("Unknown named window '{}'", name.name), expression, node, - )); + )), + Some(name) => read.push(name), + None => {} } } } + findings.extend(unknown_bases((node, select), read, level, &known)); match engine { Engine::Postgres | Engine::Sqlite | Engine::Snowflake | Engine::DuckDB => { let rules = dialect_rules(level.context.profile); From 14e978548a6f2666184f3316901ed5269fb91859 Mon Sep 17 00:00:00 2001 From: Kevin Longe <273747162+kvlonge@users.noreply.github.com> Date: Wed, 18 Nov 2026 21:47:22 +0000 Subject: [PATCH 05/24] fix(syntax): keep a window name's quotes in OVER and WINDOW The shared parser built window names without their quoting: the name after OVER, a base named inside OVER ( ... ) or a WINDOW definition, and the WINDOW clause's own names all came out unquoted, and a quoted name after WINDOW did not parse, because WINDOW followed by a quoted name read as an alias. Semantics parses every dialect with this parser, so in PostgreSQL SELECT sum(x) OVER "W" ... WINDOW w AS (...) resolved "W" to w instead of reporting E232, and WINDOW "w" AS (...) was a parse error. Each of these names now keeps whether it was quoted, and WINDOW followed by a quoted name starts the clause. Formatting reads tokens and is unchanged. A parser case covers each name position, and PostgreSQL cases cover a quoted name that does not match, one that does, and an unquoted name that folds to a quoted lower-case window. --- .../profile/tests/test_statement_findings.rs | 15 ++++++ .../src/parser/_helpers/functions/generic.rs | 6 ++- .../src/parser/_helpers/queries/clauses.rs | 4 +- .../src/parser/_helpers/queries/select.rs | 7 ++- .../src/parser/_helpers/statements/analyze.rs | 6 ++- .../src/parser/_helpers/statements/entry.rs | 6 ++- .../src/parser/tests/test_parser.rs | 50 +++++++++++++++++++ 7 files changed, 85 insertions(+), 9 deletions(-) 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 c819ac4bd..5a4cc2875 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 @@ -289,6 +289,21 @@ fn given_a_named_window_when_resolving_names_then_its_keys_read_the_input_column 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)], }, + QueryFindingsTestCase { + description: "a quoted window name keeps its case, so W is not w (18.6: 42704)", + sql: "SELECT sum(amount) OVER \"W\" AS x FROM orders WINDOW w AS (ORDER BY id)", + expected_findings: &[("E232", "Unknown named window 'W'", 11)], + }, + QueryFindingsTestCase { + description: "a quoted window name read by the same quoted name", + sql: "SELECT sum(amount) OVER \"W\" AS x FROM orders WINDOW \"W\" AS (ORDER BY id)", + expected_findings: &[], + }, + QueryFindingsTestCase { + description: "an unquoted name folds to the quoted lower-case window", + sql: "SELECT sum(amount) OVER (W ORDER BY id) AS x FROM orders WINDOW \"w\" AS (PARTITION BY nope)", + expected_findings: &[("E201", "Unknown column 'nope' in table 'orders'", 85)], + }, ]; let schema = duckdb_shop_schema(); for test_case in test_cases { diff --git a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/functions/generic.rs b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/functions/generic.rs index 44f2d425f..2c4f0300b 100644 --- a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/functions/generic.rs +++ b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/functions/generic.rs @@ -1255,7 +1255,7 @@ impl Parser { /// Parse OVER clause; inside a semantic view, which has no named windows, a window never starts with a name. pub(crate) fn parse_over_clause(&mut self) -> Result { if !self.check(TokenType::LParen) { - let window_name = self.parse_unquoted_identifier(Self::expect_identifier_or_keyword)?; + let window_name = self.parse_quoting_identifier(Self::expect_identifier_or_keyword)?; return Ok(Over { window_name: Some(window_name), partition_by: Vec::new(), @@ -1270,6 +1270,7 @@ impl Parser { let window_name = if !self.config.in_semantic_view && (self.check(TokenType::Identifier) || self.check(TokenType::Var) + || self.check(TokenType::QuotedIdentifier) || self.check_keyword()) && !self.check(TokenType::Partition) && !self.check(TokenType::Order) @@ -1280,7 +1281,8 @@ impl Parser { && !self.check(TokenType::Sort) { let pos = self.current; - let name = identifier_from_token(self.advance()?, false); + let quoted = self.check(TokenType::QuotedIdentifier); + let name = identifier_from_token(self.advance()?, quoted); if self.check(TokenType::Order) || self.check(TokenType::Partition) || self.check(TokenType::Rows) diff --git a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/queries/clauses.rs b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/queries/clauses.rs index 8aca194eb..a352e2567 100644 --- a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/queries/clauses.rs +++ b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/queries/clauses.rs @@ -1814,7 +1814,7 @@ impl Parser { let mut windows: Vec = Vec::new(); loop { - let name = self.parse_unquoted_identifier(Self::expect_identifier)?; + let name = self.parse_quoting_identifier(Self::expect_identifier)?; self.expect_token(TokenType::As)?; self.expect_token(TokenType::LParen)?; @@ -1835,7 +1835,7 @@ impl Parser { | TokenType::Comma ) }) { - Some(self.parse_unquoted_identifier(Self::expect_identifier)?) + Some(self.parse_quoting_identifier(Self::expect_identifier)?) } else { None }; diff --git a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/queries/select.rs b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/queries/select.rs index a31534f63..53a63163a 100644 --- a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/queries/select.rs +++ b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/queries/select.rs @@ -100,7 +100,12 @@ impl Parser { && !(self.is_snowflake_reserved_keyword() && !self.check(TokenType::Window)) && !(self.check(TokenType::Window) && self.peek_nth(1).is_some_and(|token| { - matches!(token.token_type, TokenType::Var | TokenType::Identifier) + matches!( + token.token_type, + TokenType::Var + | TokenType::Identifier + | TokenType::QuotedIdentifier + ) })) { let alias = self.parse_projection_alias_identifier(false)?; diff --git a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/statements/analyze.rs b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/statements/analyze.rs index 2f6abfb0a..64dfc0d78 100644 --- a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/statements/analyze.rs +++ b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/statements/analyze.rs @@ -280,8 +280,10 @@ impl Parser { || self.check(TokenType::Window) && { let next_pos = self.current + 1; next_pos >= self.tokens.len() - || self.tokens[next_pos].token_type != TokenType::Var - && self.tokens[next_pos].token_type != TokenType::Identifier + || !matches!( + self.tokens[next_pos].token_type, + TokenType::Var | TokenType::Identifier | TokenType::QuotedIdentifier + ) } } diff --git a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/statements/entry.rs b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/statements/entry.rs index e8e41c366..3cab85392 100644 --- a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/statements/entry.rs +++ b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/statements/entry.rs @@ -1677,8 +1677,10 @@ impl Parser { if self.check(TokenType::Window) && { let next_pos = self.current + 1; next_pos < self.tokens.len() - && (self.tokens[next_pos].token_type == TokenType::Var - || self.tokens[next_pos].token_type == TokenType::Identifier) + && matches!( + self.tokens[next_pos].token_type, + TokenType::Var | TokenType::Identifier | TokenType::QuotedIdentifier + ) } { self.skip(); Some(self.parse_named_windows()?) diff --git a/crates/sqlquack-syntax-snowflake/src/parser/tests/test_parser.rs b/crates/sqlquack-syntax-snowflake/src/parser/tests/test_parser.rs index 6fca0d86d..d51d534bf 100644 --- a/crates/sqlquack-syntax-snowflake/src/parser/tests/test_parser.rs +++ b/crates/sqlquack-syntax-snowflake/src/parser/tests/test_parser.rs @@ -1131,3 +1131,53 @@ fn given_a_snowflake_cte_whose_body_is_a_bare_values_list_when_parsing_then_pars ); } } + +#[test] +fn given_quoted_window_names_when_parsing_then_each_keeps_its_quotes() { + let test_cases = [ + ParseValueTestCase { + description: "an OVER name keeps its quotes", + dialect: Some(DialectType::PostgreSQL), + sql: "SELECT sum(x) OVER \"W\" AS a, sum(x) OVER (\"V\" ORDER BY y) AS b FROM t WINDOW \"W\" AS (ORDER BY y), \"V\" AS (\"W\")", + pointer: "/0/select/expressions/0/alias/this/window_function/over/window_name/quoted", + expected_value: Ok(json!(true)), + }, + ParseValueTestCase { + description: "a base named inside OVER keeps its quotes", + dialect: Some(DialectType::PostgreSQL), + sql: "SELECT sum(x) OVER \"W\" AS a, sum(x) OVER (\"V\" ORDER BY y) AS b FROM t WINDOW \"W\" AS (ORDER BY y), \"V\" AS (\"W\")", + pointer: "/0/select/expressions/1/alias/this/window_function/over/window_name/quoted", + expected_value: Ok(json!(true)), + }, + ParseValueTestCase { + description: "a WINDOW clause name keeps its quotes", + dialect: Some(DialectType::PostgreSQL), + sql: "SELECT sum(x) OVER \"W\" AS a, sum(x) OVER (\"V\" ORDER BY y) AS b FROM t WINDOW \"W\" AS (ORDER BY y), \"V\" AS (\"W\")", + pointer: "/0/select/windows/0/name/quoted", + expected_value: Ok(json!(true)), + }, + ParseValueTestCase { + description: "a base named in a WINDOW definition keeps its quotes", + dialect: Some(DialectType::PostgreSQL), + sql: "SELECT sum(x) OVER \"W\" AS a, sum(x) OVER (\"V\" ORDER BY y) AS b FROM t WINDOW \"W\" AS (ORDER BY y), \"V\" AS (\"W\")", + pointer: "/0/select/windows/1/spec/window_name/quoted", + expected_value: Ok(json!(true)), + }, + ParseValueTestCase { + description: "an unquoted WINDOW clause name is not quoted", + dialect: Some(DialectType::PostgreSQL), + sql: "SELECT sum(x) OVER w AS a FROM t WINDOW w AS (ORDER BY y)", + pointer: "/0/select/windows/0/name/quoted", + expected_value: Ok(json!(false)), + }, + ]; + + for test_case in test_cases { + assert_eq!( + value_at(test_case.dialect, test_case.sql, test_case.pointer), + test_case.expected_value, + "{}", + test_case.description + ); + } +} From 0e004822ebdcc50425bc3df350ac3bd0834603a8 Mon Sep 17 00:00:00 2001 From: Kevin Longe <273747162+kvlonge@users.noreply.github.com> Date: Wed, 18 Nov 2026 22:38:32 +0000 Subject: [PATCH 06/24] fix(semantics): check a named window's frame offsets as column references A named window's PARTITION BY and ORDER BY keys were resolved, but not the offsets of its frame, so WINDOW w AS (ORDER BY id ROWS BETWEEN nope PRECEDING AND CURRENT ROW) reported nothing while the same frame written inline in OVER gave E201. The offsets of a used window's frame bounds now resolve with its keys, in the same scope. DuckDB cases cover an unknown column in a start offset, the inline form, and an unknown qualifier in an end offset. --- .../profile/tests/test_statement_findings.rs | 19 +++++++++++++++++++ .../src/lineage/_helpers/findings/levels.rs | 14 +++++++++++--- 2 files changed, 30 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 b4004662c..889d1b7e9 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 @@ -703,6 +703,25 @@ fn given_named_windows_when_resolving_names_then_only_the_windows_an_over_reads_ sql: "SELECT amount FROM orders WINDOW w2 AS (zz ORDER BY id)", expected_findings: &[], }, + QueryFindingsTestCase { + description: "an unknown column in a used window's frame offset", + sql: "SELECT sum(amount) OVER w AS x FROM orders WINDOW w AS (ORDER BY id ROWS BETWEEN nope PRECEDING AND CURRENT ROW)", + expected_findings: &[("E201", "Unknown column 'nope' in table 'orders'", 81)], + }, + QueryFindingsTestCase { + description: "the same offset written inline", + sql: "SELECT sum(amount) OVER (ORDER BY id ROWS BETWEEN nope PRECEDING AND CURRENT ROW) AS x FROM orders", + expected_findings: &[("E201", "Unknown column 'nope' in table 'orders'", 50)], + }, + QueryFindingsTestCase { + description: "an unknown qualifier in a used window's end offset", + sql: "SELECT sum(amount) OVER w AS x FROM orders WINDOW w AS (ORDER BY id ROWS BETWEEN 1 PRECEDING AND zz.id FOLLOWING)", + expected_findings: &[( + "E222", + "Unknown table or alias 'zz' referenced by column 'id'", + 97, + )], + }, 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)", diff --git a/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs b/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs index 0afe8fc55..42888e94b 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs @@ -5,7 +5,7 @@ use std::collections::HashMap; use sqlquack_syntax::Expression; use sqlquack_syntax::ExpressionWalk; use sqlquack_syntax::expressions::queries::models::{ - JoinKind, NamedWindow, OrderBy, Select, Subquery, With, + JoinKind, NamedWindow, OrderBy, Select, Subquery, WindowFrameBound, With, }; use crate::dialects::main::dialect_rules::dialect_rules; @@ -462,10 +462,18 @@ fn used_windows<'w>( .collect() } -/// The partition and order keys of `window`. +/// The partition and order keys of `window`, and its frame's offsets. fn window_keys(window: &NamedWindow) -> impl Iterator { let orders = window.spec.order_by.iter().map(|ordered| &ordered.this); - window.spec.partition_by.iter().chain(orders) + let frame = window.spec.frame.iter(); + let bounds = frame.flat_map(|frame| std::iter::once(&frame.start).chain(&frame.end)); + let offsets = bounds.filter_map(|bound| match bound { + WindowFrameBound::Preceding(offset) + | WindowFrameBound::Following(offset) + | WindowFrameBound::Value(offset) => Some(offset.as_ref()), + _ => None, + }); + window.spec.partition_by.iter().chain(orders).chain(offsets) } /// The keys of `order`. From 88640e754fb9bdf11de9b196c11a78862ffbd7fe Mon Sep 17 00:00:00 2001 From: Kevin Longe <273747162+kvlonge@users.noreply.github.com> Date: Wed, 18 Nov 2026 22:53:12 +0000 Subject: [PATCH 07/24] fix(semantics): hide a projection's own alias from the named window it reads Where a SELECT's outputs can be read anywhere, an inline OVER key that reads its own projection's alias gave E201, but the same key in a named window did not: SELECT sum(amount) OVER w AS x FROM orders WINDOW w AS (ORDER BY x) reported nothing, while DuckDB refuses both forms. A used window's keys are now read once for each projection whose OVER reaches it, with that projection's own alias hidden, as for an inline window; a window only other clauses reach is read as before. A finding a window shared by several projections gives is reported once. DuckDB cases cover the own alias, a shared window ordered by one reader's alias, and an unknown column in a shared window. --- .../profile/tests/test_statement_findings.rs | 15 ++++ .../src/lineage/_helpers/findings/levels.rs | 76 +++++++++++++++++-- 2 files changed, 84 insertions(+), 7 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 889d1b7e9..9c5b4241e 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 @@ -722,6 +722,21 @@ fn given_named_windows_when_resolving_names_then_only_the_windows_an_over_reads_ 97, )], }, + QueryFindingsTestCase { + description: "a used window ordered by its own projection's alias, as inline (2.0: Binder Error)", + sql: "SELECT sum(amount) OVER w AS x FROM orders WINDOW w AS (ORDER BY x)", + expected_findings: &[("E201", "Unknown column 'x' in table 'orders'", 65)], + }, + QueryFindingsTestCase { + description: "a window shared by two projections, ordered by one's alias", + sql: "SELECT sum(amount) OVER w AS x, avg(amount) OVER w AS y FROM orders WINDOW w AS (ORDER BY y)", + expected_findings: &[("E201", "Unknown column 'y' in table 'orders'", 90)], + }, + QueryFindingsTestCase { + description: "an unknown column in a window shared by two projections is reported once", + sql: "SELECT sum(amount) OVER w AS x, avg(amount) OVER w AS y FROM orders WINDOW w AS (ORDER BY nope)", + expected_findings: &[("E201", "Unknown column 'nope' in table 'orders'", 90)], + }, 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)", diff --git a/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs b/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs index 42888e94b..56b16a4ec 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs @@ -371,13 +371,9 @@ fn select_findings( 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); - } + let (found, inner) = window_findings(select, (windows, &reads), (level, anywhere), context); + own.extend(found); + nested.extend(inner); own.sort_by_key(|finding| finding.span.map(|span| span.start)); own.extend(nested); own @@ -417,6 +413,72 @@ fn ordered_name_place(select: &Select, after: &[&OrderBy], clause: &Expression) .any(|ordered| std::ptr::eq(&ordered.this, clause)) } +/// The findings in the windows OVERs in `reads` reach, each read as the projection reaching it reads. +fn window_findings( + select: &Select, + (windows, reads): (Vec<&NamedWindow>, &[&Expression]), + (level, anywhere): (Level<'_, '_>, bool), + context: LineageContext<'_>, +) -> (Vec, Vec) { + let used: Vec<&NamedWindow> = used_windows(windows.clone(), reads, context); + let names: Vec = select + .expressions + .iter() + .map(|projection| output_key(unannotated(projection), context)) + .collect(); + let mut readers: Vec<(Level<'_, '_>, Vec<&NamedWindow>)> = match anywhere { + true => select + .expressions + .iter() + .zip(&names) + .map(|(projection, name)| { + let reached = used_windows(windows.clone(), &[projection], context); + let hidden = Level { + hidden: Some(name.as_str()), + ..level + }; + (hidden, reached) + }) + .collect(), + false => Vec::new(), + }; + let projected: Vec<&NamedWindow> = readers + .iter() + .flat_map(|(_, reached)| reached.iter().copied()) + .collect(); + let mut rest: Vec<&NamedWindow> = Vec::new(); + for window in used { + if !projected.iter().any(|read| std::ptr::eq(*read, window)) { + rest.push(window); + } + } + let unnamed = Level { + enclosing: &[], + own: &[], + ..level + }; + readers.push((if anywhere { level } else { unnamed }, rest)); + let (mut own, mut nested): (Vec, Vec) = (Vec::new(), Vec::new()); + for (reader, reached) in readers { + for key in reached.into_iter().flat_map(window_keys) { + let (found, inner) = clause_findings(key, &reader, context); + own.extend( + found + .into_iter() + .filter(|finding| !own.contains(finding)) + .collect::>(), + ); + nested.extend( + inner + .into_iter() + .filter(|finding| !nested.contains(finding)) + .collect::>(), + ); + } + } + (own, nested) +} + /// The windows of `windows` an OVER in `reads` names, directly or through a window extending it. fn used_windows<'w>( windows: Vec<&'w NamedWindow>, From 099b4dafd7596598a0f689f88ca9db0d340931d2 Mon Sep 17 00:00:00 2001 From: Kevin Longe <273747162+kvlonge@users.noreply.github.com> Date: Wed, 18 Nov 2026 22:58:17 +0000 Subject: [PATCH 08/24] fix(semantics): keep SQLite and BigQuery window keys from reading output aliases SQLite and BigQuery let a SELECT's clauses read its output aliases, and the window keys of the select list were read the same way, so SELECT quantity AS q, sum(quantity) OVER (PARTITION BY q) AS x FROM orders reported nothing. SQLite 3.40 refuses it with "no such column: q", inline or through a named window, and BigQuery's OVER cannot read an output alias either. A dialect now says whether the keys of a window the select list reads may read output aliases. For SQLite and BigQuery they read only the input columns; a window in ORDER BY still reads the aliases, as SQLite accepts. Other dialects are unchanged. SQLite cases cover an inline and a named window that refuse the alias and two ORDER BY windows that accept it; a BigQuery case covers an inline window. --- .../src/bigquery/profile/models.rs | 4 ++ .../src/bigquery/profile/tests/test_checks.rs | 5 ++ .../src/clickhouse/profile/models.rs | 4 ++ .../src/databricks/profile/models.rs | 4 ++ .../src/duckdb/profile/models.rs | 4 ++ .../src/lineage/_helpers/findings/checks.rs | 2 + .../src/lineage/_helpers/findings/levels.rs | 58 ++++++++++++++----- .../sqlquack-semantics/src/lineage/models.rs | 4 ++ .../sqlquack-semantics/src/lineage/types.rs | 3 + .../src/postgres/profile/models.rs | 4 ++ .../src/snowflake/profile/models.rs | 4 ++ .../src/sqlite/profile/models.rs | 4 ++ .../src/sqlite/profile/tests/test_lineage.rs | 20 +++++++ .../src/sqlserver/profile/models.rs | 4 ++ 14 files changed, 111 insertions(+), 13 deletions(-) diff --git a/crates/sqlquack-semantics/src/bigquery/profile/models.rs b/crates/sqlquack-semantics/src/bigquery/profile/models.rs index e012b65ab..146a40d85 100644 --- a/crates/sqlquack-semantics/src/bigquery/profile/models.rs +++ b/crates/sqlquack-semantics/src/bigquery/profile/models.rs @@ -58,6 +58,10 @@ impl DialectRules for BigQueryRules { false } + fn window_keys_read_outputs(&self) -> bool { + false + } + fn checks_call_signatures(&self) -> bool { true } diff --git a/crates/sqlquack-semantics/src/bigquery/profile/tests/test_checks.rs b/crates/sqlquack-semantics/src/bigquery/profile/tests/test_checks.rs index 0a58c00ba..532bdd5d7 100644 --- a/crates/sqlquack-semantics/src/bigquery/profile/tests/test_checks.rs +++ b/crates/sqlquack-semantics/src/bigquery/profile/tests/test_checks.rs @@ -6,6 +6,11 @@ use crate::lineage::tests::test_types::QueryFindingsTestCase; #[test] fn given_a_bigquery_query_when_checking_it_then_findings_follow_googlesql() { let test_cases = [ + QueryFindingsTestCase { + description: "an inline window ordered by another output's alias", + sql: "SELECT quantity AS q, sum(quantity) OVER (ORDER BY q) AS x FROM orders", + expected_findings: &[("E201", "Unknown column 'q' in table 'orders'", 51)], + }, QueryFindingsTestCase { description: "a TIMESTAMP_TRUNC date part is not a column (analyzer: accepted)", sql: "SELECT TIMESTAMP_TRUNC(ordered_at, DAY) AS v FROM orders", diff --git a/crates/sqlquack-semantics/src/clickhouse/profile/models.rs b/crates/sqlquack-semantics/src/clickhouse/profile/models.rs index c5c5bed61..18006988e 100644 --- a/crates/sqlquack-semantics/src/clickhouse/profile/models.rs +++ b/crates/sqlquack-semantics/src/clickhouse/profile/models.rs @@ -73,6 +73,10 @@ impl DialectRules for ClickHouseRules { false } + fn window_keys_read_outputs(&self) -> bool { + true + } + fn checks_call_signatures(&self) -> bool { true } diff --git a/crates/sqlquack-semantics/src/databricks/profile/models.rs b/crates/sqlquack-semantics/src/databricks/profile/models.rs index cdc35fc72..4096edfbe 100644 --- a/crates/sqlquack-semantics/src/databricks/profile/models.rs +++ b/crates/sqlquack-semantics/src/databricks/profile/models.rs @@ -63,6 +63,10 @@ impl DialectRules for DatabricksRules { false } + fn window_keys_read_outputs(&self) -> bool { + true + } + fn checks_call_signatures(&self) -> bool { true } diff --git a/crates/sqlquack-semantics/src/duckdb/profile/models.rs b/crates/sqlquack-semantics/src/duckdb/profile/models.rs index f1cc261d8..7dcc12ce2 100644 --- a/crates/sqlquack-semantics/src/duckdb/profile/models.rs +++ b/crates/sqlquack-semantics/src/duckdb/profile/models.rs @@ -64,6 +64,10 @@ impl DialectRules for DuckDbRules { false } + fn window_keys_read_outputs(&self) -> bool { + true + } + fn checks_call_signatures(&self) -> bool { true } diff --git a/crates/sqlquack-semantics/src/lineage/_helpers/findings/checks.rs b/crates/sqlquack-semantics/src/lineage/_helpers/findings/checks.rs index 29ce9a360..7fafa46d9 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/findings/checks.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/findings/checks.rs @@ -32,6 +32,8 @@ pub(crate) struct Level<'s, 'a> { pub(crate) own: &'s [String], /// The name of the projection being read, which hides own aliases spelled the same. pub(crate) hidden: Option<&'s str>, + /// Whether the keys of a window the clause reads may read this level's output aliases. + pub(crate) windows_read_outputs: bool, pub(crate) connect: bool, /// Whether a SEMI or ANTI join filters the level, its right side hidden from the outputs. pub(crate) filtering_join: bool, diff --git a/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs b/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs index 56b16a4ec..9c4eb1104 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs @@ -5,7 +5,7 @@ use std::collections::HashMap; use sqlquack_syntax::Expression; use sqlquack_syntax::ExpressionWalk; use sqlquack_syntax::expressions::queries::models::{ - JoinKind, NamedWindow, OrderBy, Select, Subquery, WindowFrameBound, With, + JoinKind, NamedWindow, OrderBy, Over, Select, Subquery, WindowFrameBound, With, }; use crate::dialects::main::dialect_rules::dialect_rules; @@ -314,6 +314,7 @@ fn select_findings( enclosing: aliases, own: &own_outputs, hidden: None, + windows_read_outputs: true, connect: select.connect.is_some(), filtering_join: every_join(select).into_iter().any(|join| { matches!( @@ -338,6 +339,7 @@ fn select_findings( let projected = match &own_name { Some(own_name) => Level { hidden: Some(own_name), + windows_read_outputs: rules.window_keys_read_outputs(), ..level }, None => unnamed, @@ -426,6 +428,12 @@ fn window_findings( .iter() .map(|projection| output_key(unannotated(projection), context)) .collect(); + let unnamed = Level { + enclosing: &[], + own: &[], + ..level + }; + let projected_reads: bool = dialect_rules(context.profile).window_keys_read_outputs(); let mut readers: Vec<(Level<'_, '_>, Vec<&NamedWindow>)> = match anywhere { true => select .expressions @@ -433,11 +441,14 @@ fn window_findings( .zip(&names) .map(|(projection, name)| { let reached = used_windows(windows.clone(), &[projection], context); - let hidden = Level { - hidden: Some(name.as_str()), - ..level + let reader = match projected_reads { + true => Level { + hidden: Some(name.as_str()), + ..level + }, + false => unnamed, }; - (hidden, reached) + (reader, reached) }) .collect(), false => Vec::new(), @@ -452,11 +463,6 @@ fn window_findings( rest.push(window); } } - let unnamed = Level { - enclosing: &[], - own: &[], - ..level - }; readers.push((if anywhere { level } else { unnamed }, rest)); let (mut own, mut nested): (Vec, Vec) = (Vec::new(), Vec::new()); for (reader, reached) in readers { @@ -526,8 +532,13 @@ fn used_windows<'w>( /// The partition and order keys of `window`, and its frame's offsets. fn window_keys(window: &NamedWindow) -> impl Iterator { - let orders = window.spec.order_by.iter().map(|ordered| &ordered.this); - let frame = window.spec.frame.iter(); + over_keys(&window.spec) +} + +/// The partition and order keys of `over`, and its frame's offsets. +fn over_keys(over: &Over) -> impl Iterator { + let orders = over.order_by.iter().map(|ordered| &ordered.this); + let frame = over.frame.iter(); let bounds = frame.flat_map(|frame| std::iter::once(&frame.start).chain(&frame.end)); let offsets = bounds.filter_map(|bound| match bound { WindowFrameBound::Preceding(offset) @@ -535,7 +546,7 @@ fn window_keys(window: &NamedWindow) -> impl Iterator { | WindowFrameBound::Value(offset) => Some(offset.as_ref()), _ => None, }); - window.spec.partition_by.iter().chain(orders).chain(offsets) + over.partition_by.iter().chain(orders).chain(offsets) } /// The keys of `order`. @@ -649,6 +660,27 @@ fn clause_findings( bound.extend(lambda.parameters.iter().map(|name| key(name, context))); pending.push(&lambda.body); } + Expression::WindowFunction(window) if !level.windows_read_outputs => { + pending.push(&window.this); + pending.extend( + window + .keep + .iter() + .flat_map(|keep| &keep.order_by) + .map(|ordered| &ordered.this), + ); + let keys = Level { + enclosing: &[], + own: &[], + windows_read_outputs: true, + ..*level + }; + for key in over_keys(&window.over) { + let (found, inner) = clause_findings(key, &keys, context); + own.extend(found); + nested.extend(inner); + } + } _ => { for child in node.children() { if !date_part_argument(node, child, context) { diff --git a/crates/sqlquack-semantics/src/lineage/models.rs b/crates/sqlquack-semantics/src/lineage/models.rs index 702825e77..605973126 100644 --- a/crates/sqlquack-semantics/src/lineage/models.rs +++ b/crates/sqlquack-semantics/src/lineage/models.rs @@ -282,6 +282,10 @@ impl DialectRules for SharedRules { false } + fn window_keys_read_outputs(&self) -> bool { + true + } + fn checks_call_signatures(&self) -> bool { true } diff --git a/crates/sqlquack-semantics/src/lineage/types.rs b/crates/sqlquack-semantics/src/lineage/types.rs index 02e6b064e..461bc04c7 100644 --- a/crates/sqlquack-semantics/src/lineage/types.rs +++ b/crates/sqlquack-semantics/src/lineage/types.rs @@ -72,6 +72,9 @@ pub(crate) trait DialectRules: Sync { /// Where outputs are not read anywhere, whether GROUP BY keys may read them as ORDER BY keys do. fn group_keys_read_outputs(&self) -> bool; + /// Where outputs are read anywhere, whether the keys of a window the select list reads may read them. + fn window_keys_read_outputs(&self) -> bool; + /// Whether calls are checked against the signatures of the functions they name. fn checks_call_signatures(&self) -> bool; diff --git a/crates/sqlquack-semantics/src/postgres/profile/models.rs b/crates/sqlquack-semantics/src/postgres/profile/models.rs index 76d74ac21..3c780159b 100644 --- a/crates/sqlquack-semantics/src/postgres/profile/models.rs +++ b/crates/sqlquack-semantics/src/postgres/profile/models.rs @@ -65,6 +65,10 @@ impl DialectRules for PostgresRules { true } + fn window_keys_read_outputs(&self) -> bool { + false + } + fn checks_call_signatures(&self) -> bool { true } diff --git a/crates/sqlquack-semantics/src/snowflake/profile/models.rs b/crates/sqlquack-semantics/src/snowflake/profile/models.rs index ed8a9a79e..882a22a13 100644 --- a/crates/sqlquack-semantics/src/snowflake/profile/models.rs +++ b/crates/sqlquack-semantics/src/snowflake/profile/models.rs @@ -103,6 +103,10 @@ impl DialectRules for SnowflakeRules { false } + fn window_keys_read_outputs(&self) -> bool { + true + } + fn checks_call_signatures(&self) -> bool { true } diff --git a/crates/sqlquack-semantics/src/sqlite/profile/models.rs b/crates/sqlquack-semantics/src/sqlite/profile/models.rs index 047ce4706..4709a010d 100644 --- a/crates/sqlquack-semantics/src/sqlite/profile/models.rs +++ b/crates/sqlquack-semantics/src/sqlite/profile/models.rs @@ -58,6 +58,10 @@ impl DialectRules for SqliteRules { false } + fn window_keys_read_outputs(&self) -> bool { + false + } + fn checks_call_signatures(&self) -> bool { false } diff --git a/crates/sqlquack-semantics/src/sqlite/profile/tests/test_lineage.rs b/crates/sqlquack-semantics/src/sqlite/profile/tests/test_lineage.rs index 9f14a7315..ae41638c5 100644 --- a/crates/sqlquack-semantics/src/sqlite/profile/tests/test_lineage.rs +++ b/crates/sqlquack-semantics/src/sqlite/profile/tests/test_lineage.rs @@ -41,6 +41,16 @@ fn given_a_sqlite_query_sqlite_rejects_when_checking_then_the_broken_rule_is_fou 0, )], }, + QueryFindingsTestCase { + description: "an inline window partitioned by another output's alias (3.40: no such column)", + sql: "SELECT quantity AS q, sum(quantity) OVER (PARTITION BY q) AS x FROM orders", + expected_findings: &[("E201", "Unknown column 'q' in table 'orders'", 55)], + }, + QueryFindingsTestCase { + description: "a named window ordered by another output's alias (3.40: no such column)", + sql: "SELECT quantity AS q, sum(quantity) OVER w AS x FROM orders WINDOW w AS (ORDER BY q)", + expected_findings: &[("E201", "Unknown column 'q' in table 'orders'", 82)], + }, QueryFindingsTestCase { description: "a ranking function without OVER", sql: "SELECT ROW_NUMBER() AS r FROM orders", @@ -84,6 +94,16 @@ fn given_a_sqlite_query_sqlite_rejects_when_checking_then_the_broken_rule_is_fou #[test] fn given_a_sqlite_query_sqlite_accepts_when_checking_then_nothing_is_found() { let test_cases = [ + QueryFindingsTestCase { + description: "a window in ORDER BY reading an output alias (3.40: accepted)", + sql: "SELECT quantity AS q FROM orders ORDER BY sum(quantity) OVER (ORDER BY q)", + expected_findings: &[], + }, + QueryFindingsTestCase { + description: "a named window only ORDER BY reads, ordered by an output alias (3.40: accepted)", + sql: "SELECT quantity AS q FROM orders WINDOW w AS (ORDER BY q) ORDER BY sum(quantity) OVER w", + expected_findings: &[], + }, QueryFindingsTestCase { description: "a set operation's ORDER BY naming an output in upper case", sql: "SELECT id FROM orders UNION ALL SELECT id FROM customers ORDER BY ID", diff --git a/crates/sqlquack-semantics/src/sqlserver/profile/models.rs b/crates/sqlquack-semantics/src/sqlserver/profile/models.rs index 0e3b0ddbe..3775c28a3 100644 --- a/crates/sqlquack-semantics/src/sqlserver/profile/models.rs +++ b/crates/sqlquack-semantics/src/sqlserver/profile/models.rs @@ -65,6 +65,10 @@ impl DialectRules for SqlServerRules { false } + fn window_keys_read_outputs(&self) -> bool { + false + } + fn checks_call_signatures(&self) -> bool { false } From 32d890e39cb81c3449487995b780d9fc9669114e Mon Sep 17 00:00:00 2001 From: Kevin Longe <273747162+kvlonge@users.noreply.github.com> Date: Wed, 18 Nov 2026 23:36:28 +0000 Subject: [PATCH 09/24] fix(sqlite): a projection cannot read another projection's alias SQLite resolves output aliases in WHERE, GROUP BY, HAVING and ORDER BY, and in subqueries read there, but not in the select list itself: both `SELECT quantity AS q, q + 1 FROM orders` and a scalar subquery in the select list reading `q` fail with "no such column". A new dialect rule, select_list_reads_outputs, is false for SQLite only, so its select list reads input columns and reports E201 for an output alias. --- .../src/bigquery/profile/models.rs | 4 ++ .../src/clickhouse/profile/models.rs | 4 ++ .../src/databricks/profile/models.rs | 4 ++ .../src/duckdb/profile/models.rs | 4 ++ .../src/lineage/_helpers/findings/levels.rs | 14 ++++++- .../sqlquack-semantics/src/lineage/models.rs | 4 ++ .../sqlquack-semantics/src/lineage/types.rs | 3 ++ .../src/postgres/profile/models.rs | 4 ++ .../src/snowflake/profile/models.rs | 4 ++ .../src/sqlite/profile/models.rs | 4 ++ .../src/sqlite/profile/tests/test_lineage.rs | 39 +++++++++++++++++++ .../src/sqlserver/profile/models.rs | 4 ++ 12 files changed, 90 insertions(+), 2 deletions(-) diff --git a/crates/sqlquack-semantics/src/bigquery/profile/models.rs b/crates/sqlquack-semantics/src/bigquery/profile/models.rs index 146a40d85..567661533 100644 --- a/crates/sqlquack-semantics/src/bigquery/profile/models.rs +++ b/crates/sqlquack-semantics/src/bigquery/profile/models.rs @@ -62,6 +62,10 @@ impl DialectRules for BigQueryRules { false } + fn select_list_reads_outputs(&self) -> bool { + true + } + fn checks_call_signatures(&self) -> bool { true } diff --git a/crates/sqlquack-semantics/src/clickhouse/profile/models.rs b/crates/sqlquack-semantics/src/clickhouse/profile/models.rs index 18006988e..0fa5fde77 100644 --- a/crates/sqlquack-semantics/src/clickhouse/profile/models.rs +++ b/crates/sqlquack-semantics/src/clickhouse/profile/models.rs @@ -77,6 +77,10 @@ impl DialectRules for ClickHouseRules { true } + fn select_list_reads_outputs(&self) -> bool { + true + } + fn checks_call_signatures(&self) -> bool { true } diff --git a/crates/sqlquack-semantics/src/databricks/profile/models.rs b/crates/sqlquack-semantics/src/databricks/profile/models.rs index 4096edfbe..493b77ea1 100644 --- a/crates/sqlquack-semantics/src/databricks/profile/models.rs +++ b/crates/sqlquack-semantics/src/databricks/profile/models.rs @@ -67,6 +67,10 @@ impl DialectRules for DatabricksRules { true } + fn select_list_reads_outputs(&self) -> bool { + true + } + fn checks_call_signatures(&self) -> bool { true } diff --git a/crates/sqlquack-semantics/src/duckdb/profile/models.rs b/crates/sqlquack-semantics/src/duckdb/profile/models.rs index 7dcc12ce2..a73da0ca5 100644 --- a/crates/sqlquack-semantics/src/duckdb/profile/models.rs +++ b/crates/sqlquack-semantics/src/duckdb/profile/models.rs @@ -68,6 +68,10 @@ impl DialectRules for DuckDbRules { true } + fn select_list_reads_outputs(&self) -> bool { + true + } + fn checks_call_signatures(&self) -> bool { true } diff --git a/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs b/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs index 9c4eb1104..88ce6acff 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs @@ -337,6 +337,11 @@ fn select_findings( let own_name: Option = anywhere.then(|| output_key(unannotated(projection), context)); let projected = match &own_name { + Some(_) if !rules.select_list_reads_outputs() => Level { + own: &[], + windows_read_outputs: true, + ..level + }, Some(own_name) => Level { hidden: Some(own_name), windows_read_outputs: rules.window_keys_read_outputs(), @@ -433,7 +438,12 @@ fn window_findings( own: &[], ..level }; - let projected_reads: bool = dialect_rules(context.profile).window_keys_read_outputs(); + let rules: &dyn DialectRules = dialect_rules(context.profile); + let projected_reads: bool = rules.window_keys_read_outputs(); + let unprojected = match rules.select_list_reads_outputs() { + true => unnamed, + false => Level { own: &[], ..level }, + }; let mut readers: Vec<(Level<'_, '_>, Vec<&NamedWindow>)> = match anywhere { true => select .expressions @@ -446,7 +456,7 @@ fn window_findings( hidden: Some(name.as_str()), ..level }, - false => unnamed, + false => unprojected, }; (reader, reached) }) diff --git a/crates/sqlquack-semantics/src/lineage/models.rs b/crates/sqlquack-semantics/src/lineage/models.rs index 605973126..137dc88ff 100644 --- a/crates/sqlquack-semantics/src/lineage/models.rs +++ b/crates/sqlquack-semantics/src/lineage/models.rs @@ -286,6 +286,10 @@ impl DialectRules for SharedRules { true } + fn select_list_reads_outputs(&self) -> bool { + true + } + fn checks_call_signatures(&self) -> bool { true } diff --git a/crates/sqlquack-semantics/src/lineage/types.rs b/crates/sqlquack-semantics/src/lineage/types.rs index 461bc04c7..1e5a09b3b 100644 --- a/crates/sqlquack-semantics/src/lineage/types.rs +++ b/crates/sqlquack-semantics/src/lineage/types.rs @@ -75,6 +75,9 @@ pub(crate) trait DialectRules: Sync { /// Where outputs are read anywhere, whether the keys of a window the select list reads may read them. fn window_keys_read_outputs(&self) -> bool; + /// Where outputs are read anywhere, whether the select list may read them too. + fn select_list_reads_outputs(&self) -> bool; + /// Whether calls are checked against the signatures of the functions they name. fn checks_call_signatures(&self) -> bool; diff --git a/crates/sqlquack-semantics/src/postgres/profile/models.rs b/crates/sqlquack-semantics/src/postgres/profile/models.rs index 3c780159b..ac26aa33b 100644 --- a/crates/sqlquack-semantics/src/postgres/profile/models.rs +++ b/crates/sqlquack-semantics/src/postgres/profile/models.rs @@ -69,6 +69,10 @@ impl DialectRules for PostgresRules { false } + fn select_list_reads_outputs(&self) -> bool { + true + } + fn checks_call_signatures(&self) -> bool { true } diff --git a/crates/sqlquack-semantics/src/snowflake/profile/models.rs b/crates/sqlquack-semantics/src/snowflake/profile/models.rs index 882a22a13..9041e1f06 100644 --- a/crates/sqlquack-semantics/src/snowflake/profile/models.rs +++ b/crates/sqlquack-semantics/src/snowflake/profile/models.rs @@ -107,6 +107,10 @@ impl DialectRules for SnowflakeRules { true } + fn select_list_reads_outputs(&self) -> bool { + true + } + fn checks_call_signatures(&self) -> bool { true } diff --git a/crates/sqlquack-semantics/src/sqlite/profile/models.rs b/crates/sqlquack-semantics/src/sqlite/profile/models.rs index 4709a010d..90606a37d 100644 --- a/crates/sqlquack-semantics/src/sqlite/profile/models.rs +++ b/crates/sqlquack-semantics/src/sqlite/profile/models.rs @@ -62,6 +62,10 @@ impl DialectRules for SqliteRules { false } + fn select_list_reads_outputs(&self) -> bool { + false + } + fn checks_call_signatures(&self) -> bool { false } diff --git a/crates/sqlquack-semantics/src/sqlite/profile/tests/test_lineage.rs b/crates/sqlquack-semantics/src/sqlite/profile/tests/test_lineage.rs index ae41638c5..8712837e8 100644 --- a/crates/sqlquack-semantics/src/sqlite/profile/tests/test_lineage.rs +++ b/crates/sqlquack-semantics/src/sqlite/profile/tests/test_lineage.rs @@ -51,6 +51,25 @@ fn given_a_sqlite_query_sqlite_rejects_when_checking_then_the_broken_rule_is_fou sql: "SELECT quantity AS q, sum(quantity) OVER w AS x FROM orders WINDOW w AS (ORDER BY q)", expected_findings: &[("E201", "Unknown column 'q' in table 'orders'", 82)], }, + QueryFindingsTestCase { + description: "a projection reading an earlier output's alias (3.40: no such column)", + sql: "SELECT quantity AS q, q + 1 AS r FROM orders", + expected_findings: &[("E201", "Unknown column 'q' in table 'orders'", 22)], + }, + QueryFindingsTestCase { + description: "a projection reading a later output's alias (3.40: no such column)", + sql: "SELECT q + 1 AS r, quantity AS q FROM orders", + expected_findings: &[("E201", "Unknown column 'q' in table 'orders'", 7)], + }, + QueryFindingsTestCase { + description: "a subquery in the select list reading an output alias (3.40: no such column)", + sql: "SELECT quantity AS q, (SELECT q) AS r FROM orders", + expected_findings: &[( + "E201", + "Unknown column 'q' (not found in any referenced table)", + 30, + )], + }, QueryFindingsTestCase { description: "a ranking function without OVER", sql: "SELECT ROW_NUMBER() AS r FROM orders", @@ -94,6 +113,26 @@ fn given_a_sqlite_query_sqlite_rejects_when_checking_then_the_broken_rule_is_fou #[test] fn given_a_sqlite_query_sqlite_accepts_when_checking_then_nothing_is_found() { let test_cases = [ + QueryFindingsTestCase { + description: "a nested select list's named window reading the enclosing query's alias (3.40: accepted)", + sql: "SELECT quantity AS q FROM orders WHERE EXISTS (SELECT sum(1) OVER w FROM customers WINDOW w AS (ORDER BY q))", + expected_findings: &[], + }, + QueryFindingsTestCase { + description: "an output alias read in WHERE, GROUP BY and HAVING (3.40: accepted)", + sql: "SELECT quantity AS q, count(*) AS n FROM orders WHERE q > 0 GROUP BY q HAVING q > 0", + expected_findings: &[], + }, + QueryFindingsTestCase { + description: "a subquery in WHERE reading an output alias (3.40: accepted)", + sql: "SELECT quantity AS q FROM orders WHERE (SELECT q) > 0", + expected_findings: &[], + }, + QueryFindingsTestCase { + description: "a subquery's select list reading the enclosing query's alias (3.40: accepted)", + sql: "SELECT quantity AS q FROM orders WHERE EXISTS (SELECT q)", + expected_findings: &[], + }, QueryFindingsTestCase { description: "a window in ORDER BY reading an output alias (3.40: accepted)", sql: "SELECT quantity AS q FROM orders ORDER BY sum(quantity) OVER (ORDER BY q)", diff --git a/crates/sqlquack-semantics/src/sqlserver/profile/models.rs b/crates/sqlquack-semantics/src/sqlserver/profile/models.rs index 3775c28a3..2e2f2141b 100644 --- a/crates/sqlquack-semantics/src/sqlserver/profile/models.rs +++ b/crates/sqlquack-semantics/src/sqlserver/profile/models.rs @@ -69,6 +69,10 @@ impl DialectRules for SqlServerRules { false } + fn select_list_reads_outputs(&self) -> bool { + true + } + fn checks_call_signatures(&self) -> bool { false } From b7349a2cffda3b592c1a0853ae0980543ad95bea Mon Sep 17 00:00:00 2001 From: Kevin Longe <273747162+kvlonge@users.noreply.github.com> Date: Wed, 18 Nov 2026 23:43:41 +0000 Subject: [PATCH 10/24] fix(duckdb): a NATURAL join of sides sharing no column is an error DuckDB refuses a NATURAL join whose sides have no column name in common ("No columns found to join on in NATURAL JOIN"), where PostgreSQL and SQLite treat it as a cross join. On DuckDB, a NATURAL join of closed sides that share no name now reports E201 at the joined item. Only the join's own left side counts, not a comma neighbour, matching DuckDB 2.0. --- .../profile/tests/test_statement_findings.rs | 49 +++++++++++++++++++ .../src/lineage/_helpers/findings/checks.rs | 27 ++++++++-- .../src/lineage/_helpers/tracing/sources.rs | 3 +- 3 files changed, 75 insertions(+), 4 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 9c5b4241e..86dcfd91a 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 @@ -786,6 +786,55 @@ fn given_a_comma_after_a_joined_item_when_resolving_names_then_the_join_reads_on } } +#[test] +fn given_a_natural_join_when_resolving_names_then_sides_sharing_no_column_are_refused() { + let test_cases = [ + QueryFindingsTestCase { + description: "a NATURAL join of tables sharing no column (2.0: no columns found to join on)", + sql: "SELECT * FROM orders NATURAL JOIN shop.products", + expected_findings: &[("E201", "NATURAL JOIN has no column both sides offer", 39)], + }, + QueryFindingsTestCase { + description: "a NATURAL LEFT join sharing no column", + sql: "SELECT * FROM customers NATURAL LEFT JOIN exports", + expected_findings: &[("E201", "NATURAL JOIN has no column both sides offer", 42)], + }, + QueryFindingsTestCase { + description: "a NATURAL join whose comma neighbour, not its left item, shares a column", + sql: "SELECT * FROM customers c, shop.products p NATURAL JOIN orders o", + expected_findings: &[("E201", "NATURAL JOIN has no column both sides offer", 56)], + }, + QueryFindingsTestCase { + description: "a NATURAL join of a derived table sharing no column", + sql: "SELECT * FROM orders NATURAL JOIN (SELECT 1 AS z) s", + expected_findings: &[("E201", "NATURAL JOIN has no column both sides offer", 34)], + }, + QueryFindingsTestCase { + description: "a second NATURAL join whose right shares no column with the joined left", + sql: "SELECT * FROM orders o NATURAL JOIN customers c NATURAL JOIN exports e", + expected_findings: &[("E201", "NATURAL JOIN has no column both sides offer", 61)], + }, + QueryFindingsTestCase { + description: "a NATURAL join of tables sharing a column", + sql: "SELECT * FROM orders NATURAL JOIN customers", + expected_findings: &[], + }, + QueryFindingsTestCase { + description: "a NATURAL join sharing a column spelled in another case", + sql: "SELECT * FROM orders NATURAL JOIN (SELECT 1 AS ID) s", + expected_findings: &[], + }, + ]; + for test_case in test_cases { + assert_eq!( + duckdb_parser_findings(test_case.sql), + owned_findings(test_case.expected_findings), + "{}", + test_case.description + ); + } +} + #[test] fn given_joined_and_comma_items_when_expanding_a_star_then_the_columns_follow_the_written_order() { let test_cases = [ diff --git a/crates/sqlquack-semantics/src/lineage/_helpers/findings/checks.rs b/crates/sqlquack-semantics/src/lineage/_helpers/findings/checks.rs index 7fafa46d9..f2bbe1f34 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/findings/checks.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/findings/checks.rs @@ -11,7 +11,7 @@ use crate::lineage::_helpers::checks::scope_rules::output_width; use crate::lineage::_helpers::tracing::queries::empty_scope; use crate::lineage::_helpers::tracing::resolution::key; use crate::lineage::_helpers::tracing::reuse::holds_unpivot; -use crate::lineage::_helpers::tracing::sources::{from_parts, item_sources, sided_parts}; +use crate::lineage::_helpers::tracing::sources::{from_parts, item_sources, natural, sided_parts}; use crate::lineage::constants::{ ALIAS_COUNT_CODE, AMBIGUOUS_COLUMN_CODE, COLUMNS_NO_MATCH, CONNECT_BY_LEVEL, NAME_PHASE, UNKNOWN_COLUMN_CODE, UNKNOWN_QUALIFIER_CODE, @@ -19,7 +19,7 @@ use crate::lineage::constants::{ use crate::lineage::constants::{ METADATA_PREFIX, POSITIONAL_PREFIX, SEQUENCE_COLUMNS, SINGLE_JOIN_SOURCES, SQL_STAR, }; -use crate::lineage::models::NameFinding; +use crate::lineage::models::{Engine, NameFinding}; use crate::lineage::models::{LineageContext, LineageScope, NamedQuery, ScopeSource}; /// The level a clause is read in: its scope, the aliases it may read, CONNECT BY and filter joins. @@ -394,7 +394,7 @@ fn unknown_message(written: &str, scope: &LineageScope<'_>, context: LineageCont } } -/// A USING key a closed side of its join lacks. +/// A USING key a closed side of its join lacks, or on DuckDB a NATURAL join of closed sides sharing no name. pub(crate) fn using_findings( select: &Select, scope: &LineageScope<'_>, @@ -415,6 +415,18 @@ pub(crate) fn using_findings( continue; }; let (left, right) = (sources(part.left), sources(part.right)); + if natural(join.kind) + && Engine::of(context) == Engine::DuckDB + && !shares_name(left, right, context) + { + findings.push(NameFinding { + code: UNKNOWN_COLUMN_CODE, + message: "NATURAL JOIN has no column both sides offer".to_owned(), + span: join.this.source_span().or(at), + phase: NAME_PHASE, + ..NameFinding::default() + }); + } for name in &join.using { let wanted: String = key(name, context); if lacks(left, &wanted, context) || lacks(right, &wanted, context) { @@ -432,6 +444,15 @@ pub(crate) fn using_findings( findings } +/// Whether a side of a join is open or offers a column the other side offers. +fn shares_name(left: &[ScopeSource], right: &[ScopeSource], context: LineageContext<'_>) -> bool { + left.iter().chain(right).any(unjudged) + || right + .iter() + .flat_map(|source| source.query.columns.iter()) + .any(|column| !lacks(left, &column.key, context)) +} + /// Whether a closed side of a join offers no column named `wanted`. fn lacks(side: &[ScopeSource], wanted: &str, context: LineageContext<'_>) -> bool { !side.iter().any(unjudged) && !side.iter().any(|source| offers(source, wanted, context)) diff --git a/crates/sqlquack-semantics/src/lineage/_helpers/tracing/sources.rs b/crates/sqlquack-semantics/src/lineage/_helpers/tracing/sources.rs index 9e9eee1e3..9be73e393 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/tracing/sources.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/tracing/sources.rs @@ -303,7 +303,8 @@ fn joined( (merged_keys, rows) } -fn natural(kind: JoinKind) -> bool { +/// Whether a join is NATURAL, joining on the column names both sides offer. +pub(crate) fn natural(kind: JoinKind) -> bool { matches!( kind, JoinKind::Natural | JoinKind::NaturalLeft | JoinKind::NaturalRight | JoinKind::NaturalFull From 67f9861f9522045c82511c847668799a91bc1f89 Mon Sep 17 00:00:00 2001 From: Kevin Longe <273747162+kvlonge@users.noreply.github.com> Date: Wed, 18 Nov 2026 23:58:15 +0000 Subject: [PATCH 11/24] fix(semantics): a merged join key is ambiguous beside another item's column on PostgreSQL and SQLite PostgreSQL and SQLite read an unqualified USING or NATURAL key as the merged column only when no FROM item outside that join offers the name: `a JOIN b USING (id), c` and `a JOIN b USING (id) JOIN c ON ...` with a `c.id` are "ambiguous" there, while DuckDB binds the merged key. A new dialect rule, merged_keys_hide_other_items, is false for PostgreSQL and SQLite, which now report E221 when a merged key and an uncovered item both offer the name. A later USING or NATURAL join merging the key again still reads it once. --- .../src/bigquery/profile/models.rs | 4 ++ .../src/clickhouse/profile/models.rs | 4 ++ .../src/databricks/profile/models.rs | 4 ++ .../src/duckdb/profile/models.rs | 4 ++ .../src/lineage/_helpers/findings/checks.rs | 37 +++++++++++-- .../sqlquack-semantics/src/lineage/models.rs | 4 ++ .../sqlquack-semantics/src/lineage/types.rs | 3 + .../src/postgres/profile/models.rs | 4 ++ .../profile/tests/test_statement_findings.rs | 55 +++++++++++++++++++ .../src/snowflake/profile/models.rs | 4 ++ .../src/sqlite/profile/models.rs | 4 ++ .../src/sqlite/profile/tests/test_lineage.rs | 46 ++++++++++++++++ .../src/sqlserver/profile/models.rs | 4 ++ 13 files changed, 171 insertions(+), 6 deletions(-) diff --git a/crates/sqlquack-semantics/src/bigquery/profile/models.rs b/crates/sqlquack-semantics/src/bigquery/profile/models.rs index 567661533..215460b58 100644 --- a/crates/sqlquack-semantics/src/bigquery/profile/models.rs +++ b/crates/sqlquack-semantics/src/bigquery/profile/models.rs @@ -66,6 +66,10 @@ impl DialectRules for BigQueryRules { true } + fn merged_keys_hide_other_items(&self) -> bool { + true + } + fn checks_call_signatures(&self) -> bool { true } diff --git a/crates/sqlquack-semantics/src/clickhouse/profile/models.rs b/crates/sqlquack-semantics/src/clickhouse/profile/models.rs index 0fa5fde77..64680ca29 100644 --- a/crates/sqlquack-semantics/src/clickhouse/profile/models.rs +++ b/crates/sqlquack-semantics/src/clickhouse/profile/models.rs @@ -81,6 +81,10 @@ impl DialectRules for ClickHouseRules { true } + fn merged_keys_hide_other_items(&self) -> bool { + true + } + fn checks_call_signatures(&self) -> bool { true } diff --git a/crates/sqlquack-semantics/src/databricks/profile/models.rs b/crates/sqlquack-semantics/src/databricks/profile/models.rs index 493b77ea1..eb0365c67 100644 --- a/crates/sqlquack-semantics/src/databricks/profile/models.rs +++ b/crates/sqlquack-semantics/src/databricks/profile/models.rs @@ -71,6 +71,10 @@ impl DialectRules for DatabricksRules { true } + fn merged_keys_hide_other_items(&self) -> bool { + true + } + fn checks_call_signatures(&self) -> bool { true } diff --git a/crates/sqlquack-semantics/src/duckdb/profile/models.rs b/crates/sqlquack-semantics/src/duckdb/profile/models.rs index a73da0ca5..1c4e9fdab 100644 --- a/crates/sqlquack-semantics/src/duckdb/profile/models.rs +++ b/crates/sqlquack-semantics/src/duckdb/profile/models.rs @@ -72,6 +72,10 @@ impl DialectRules for DuckDbRules { true } + fn merged_keys_hide_other_items(&self) -> bool { + true + } + fn checks_call_signatures(&self) -> bool { true } diff --git a/crates/sqlquack-semantics/src/lineage/_helpers/findings/checks.rs b/crates/sqlquack-semantics/src/lineage/_helpers/findings/checks.rs index f2bbe1f34..f8a83d830 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/findings/checks.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/findings/checks.rs @@ -20,7 +20,7 @@ use crate::lineage::constants::{ METADATA_PREFIX, POSITIONAL_PREFIX, SEQUENCE_COLUMNS, SINGLE_JOIN_SOURCES, SQL_STAR, }; use crate::lineage::models::{Engine, NameFinding}; -use crate::lineage::models::{LineageContext, LineageScope, NamedQuery, ScopeSource}; +use crate::lineage::models::{LineageContext, LineageScope, MergedKey, NamedQuery, ScopeSource}; /// The level a clause is read in: its scope, the aliases it may read, CONNECT BY and filter joins. #[derive(Clone, Copy)] @@ -104,11 +104,7 @@ pub(crate) fn column_finding( } let mut scope: Option<&LineageScope<'_>> = Some(level.scope); while let Some(current) = scope { - if current - .merged - .iter() - .any(|merged| context.profile.same_identifier(&merged.column.key, &wanted)) - { + if merged_answers(current, &wanted, context) { return None; } let matches: usize = offering_count(current, &wanted, context); @@ -510,6 +506,35 @@ fn offers(source: &ScopeSource, wanted: &str, context: LineageContext<'_>) -> bo .contains(&wanted)) } +/// Whether a USING or NATURAL key named `wanted` answers it: where other items may share the name, alone. +fn merged_answers(scope: &LineageScope<'_>, wanted: &str, context: LineageContext<'_>) -> bool { + let named = |merged: &&MergedKey| context.profile.same_identifier(&merged.column.key, wanted); + if !scope.merged.iter().any(|merged| named(&merged)) { + return false; + } + if dialect_rules(context.profile).merged_keys_hide_other_items() { + return true; + } + let keys: Vec<&std::ops::Range> = scope + .merged + .iter() + .filter(named) + .map(|merged| &merged.sources) + .collect(); + let uncovered: usize = scope + .sources + .iter() + .enumerate() + .filter(|(index, source)| !covered(&keys, *index) && offers(source, wanted, context)) + .count(); + keys.len() + uncovered <= 1 +} + +/// Whether one of the merged keys' joins holds the `index`th source. +fn covered(keys: &[&std::ops::Range], index: usize) -> bool { + keys.iter().any(|range| range.contains(&index)) +} + /// How many FROM items offer `wanted`, two under one name counted once. fn offering_count(scope: &LineageScope<'_>, wanted: &str, context: LineageContext<'_>) -> usize { let mut names: Vec<&str> = Vec::new(); diff --git a/crates/sqlquack-semantics/src/lineage/models.rs b/crates/sqlquack-semantics/src/lineage/models.rs index 137dc88ff..aa94a4970 100644 --- a/crates/sqlquack-semantics/src/lineage/models.rs +++ b/crates/sqlquack-semantics/src/lineage/models.rs @@ -290,6 +290,10 @@ impl DialectRules for SharedRules { true } + fn merged_keys_hide_other_items(&self) -> bool { + true + } + fn checks_call_signatures(&self) -> bool { true } diff --git a/crates/sqlquack-semantics/src/lineage/types.rs b/crates/sqlquack-semantics/src/lineage/types.rs index 1e5a09b3b..3dd780d92 100644 --- a/crates/sqlquack-semantics/src/lineage/types.rs +++ b/crates/sqlquack-semantics/src/lineage/types.rs @@ -78,6 +78,9 @@ pub(crate) trait DialectRules: Sync { /// Where outputs are read anywhere, whether the select list may read them too. fn select_list_reads_outputs(&self) -> bool; + /// Whether an unqualified USING or NATURAL key reads its merged column though an item outside its join offers the name. + fn merged_keys_hide_other_items(&self) -> bool; + /// Whether calls are checked against the signatures of the functions they name. fn checks_call_signatures(&self) -> bool; diff --git a/crates/sqlquack-semantics/src/postgres/profile/models.rs b/crates/sqlquack-semantics/src/postgres/profile/models.rs index ac26aa33b..9d964c453 100644 --- a/crates/sqlquack-semantics/src/postgres/profile/models.rs +++ b/crates/sqlquack-semantics/src/postgres/profile/models.rs @@ -73,6 +73,10 @@ impl DialectRules for PostgresRules { true } + fn merged_keys_hide_other_items(&self) -> bool { + false + } + fn checks_call_signatures(&self) -> bool { true } 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 5a4cc2875..1defa9802 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 @@ -358,6 +358,61 @@ fn given_a_comma_after_a_joined_item_when_resolving_names_then_the_join_reads_on sql: "SELECT * FROM orders a JOIN orders b USING (status), customers c", expected_findings: &[], }, + QueryFindingsTestCase { + description: "a USING key read unqualified beside a comma item offering it (18: ambiguous)", + sql: "SELECT id FROM orders a JOIN customers b USING (id), customers c", + expected_findings: &[( + "E221", + "Ambiguous unqualified column 'id' found in 3 referenced tables", + 7, + )], + }, + QueryFindingsTestCase { + description: "a USING key read unqualified beside a later ON join offering it (18: ambiguous)", + sql: "SELECT id FROM orders a JOIN customers b USING (id) JOIN customers c ON c.id = a.id", + expected_findings: &[( + "E221", + "Ambiguous unqualified column 'id' found in 3 referenced tables", + 7, + )], + }, + QueryFindingsTestCase { + description: "a USING key read unqualified beside an earlier comma item offering it (18: ambiguous)", + sql: "SELECT id FROM orders a, customers b JOIN customers c USING (id)", + expected_findings: &[( + "E221", + "Ambiguous unqualified column 'id' found in 3 referenced tables", + 7, + )], + }, + QueryFindingsTestCase { + description: "a NATURAL key read unqualified beside a comma item offering it (18: ambiguous)", + sql: "SELECT id FROM orders a NATURAL JOIN customers b, customers c", + expected_findings: &[( + "E221", + "Ambiguous unqualified column 'id' found in 3 referenced tables", + 7, + )], + }, + QueryFindingsTestCase { + description: "a USING key of a parenthesised join beside a later item offering it (18: ambiguous)", + sql: "SELECT id FROM (orders a JOIN customers b USING (id)) JOIN customers c ON true", + expected_findings: &[( + "E221", + "Ambiguous unqualified column 'id' found in 3 referenced tables", + 7, + )], + }, + QueryFindingsTestCase { + description: "a USING key a later USING join merges again (18: accepted)", + sql: "SELECT id FROM orders a JOIN customers b USING (id) JOIN customers c USING (id)", + expected_findings: &[], + }, + QueryFindingsTestCase { + description: "a USING key outer joins merge again (18: accepted)", + sql: "SELECT id FROM orders a LEFT JOIN customers b USING (id) RIGHT JOIN customers c USING (id)", + expected_findings: &[], + }, ]; let schema = duckdb_shop_schema(); for test_case in test_cases { diff --git a/crates/sqlquack-semantics/src/snowflake/profile/models.rs b/crates/sqlquack-semantics/src/snowflake/profile/models.rs index 9041e1f06..ed1690cb2 100644 --- a/crates/sqlquack-semantics/src/snowflake/profile/models.rs +++ b/crates/sqlquack-semantics/src/snowflake/profile/models.rs @@ -111,6 +111,10 @@ impl DialectRules for SnowflakeRules { true } + fn merged_keys_hide_other_items(&self) -> bool { + true + } + fn checks_call_signatures(&self) -> bool { true } diff --git a/crates/sqlquack-semantics/src/sqlite/profile/models.rs b/crates/sqlquack-semantics/src/sqlite/profile/models.rs index 90606a37d..1b3a10917 100644 --- a/crates/sqlquack-semantics/src/sqlite/profile/models.rs +++ b/crates/sqlquack-semantics/src/sqlite/profile/models.rs @@ -66,6 +66,10 @@ impl DialectRules for SqliteRules { false } + fn merged_keys_hide_other_items(&self) -> bool { + false + } + fn checks_call_signatures(&self) -> bool { false } diff --git a/crates/sqlquack-semantics/src/sqlite/profile/tests/test_lineage.rs b/crates/sqlquack-semantics/src/sqlite/profile/tests/test_lineage.rs index 8712837e8..6502c26a1 100644 --- a/crates/sqlquack-semantics/src/sqlite/profile/tests/test_lineage.rs +++ b/crates/sqlquack-semantics/src/sqlite/profile/tests/test_lineage.rs @@ -70,6 +70,42 @@ fn given_a_sqlite_query_sqlite_rejects_when_checking_then_the_broken_rule_is_fou 30, )], }, + QueryFindingsTestCase { + description: "a USING key read unqualified beside a comma item offering it (3.40: ambiguous)", + sql: "SELECT id FROM orders a JOIN customers b USING (id), customers c", + expected_findings: &[( + "E221", + "Ambiguous unqualified column 'id' found in 3 referenced tables", + 7, + )], + }, + QueryFindingsTestCase { + description: "a USING key read unqualified beside a later ON join offering it (3.40: ambiguous)", + sql: "SELECT id FROM orders a JOIN customers b USING (id) JOIN customers c ON c.id = a.id", + expected_findings: &[( + "E221", + "Ambiguous unqualified column 'id' found in 3 referenced tables", + 7, + )], + }, + QueryFindingsTestCase { + description: "a NATURAL key read unqualified beside a comma item offering it (3.40: ambiguous)", + sql: "SELECT id FROM orders a NATURAL JOIN customers b, customers c", + expected_findings: &[( + "E221", + "Ambiguous unqualified column 'id' found in 3 referenced tables", + 7, + )], + }, + QueryFindingsTestCase { + description: "a USING key of a parenthesised join beside a later item offering it (3.40: ambiguous)", + sql: "SELECT id FROM (orders a JOIN customers b USING (id)) JOIN customers c ON true", + expected_findings: &[( + "E221", + "Ambiguous unqualified column 'id' found in 3 referenced tables", + 7, + )], + }, QueryFindingsTestCase { description: "a ranking function without OVER", sql: "SELECT ROW_NUMBER() AS r FROM orders", @@ -118,6 +154,16 @@ fn given_a_sqlite_query_sqlite_accepts_when_checking_then_nothing_is_found() { sql: "SELECT quantity AS q FROM orders WHERE EXISTS (SELECT sum(1) OVER w FROM customers WINDOW w AS (ORDER BY q))", expected_findings: &[], }, + QueryFindingsTestCase { + description: "a USING key a later USING join merges again (3.40: accepted)", + sql: "SELECT id FROM orders a JOIN customers b USING (id) JOIN customers c USING (id)", + expected_findings: &[], + }, + QueryFindingsTestCase { + description: "a USING key outer joins merge again (3.40: accepted)", + sql: "SELECT id FROM orders a LEFT JOIN customers b USING (id) RIGHT JOIN customers c USING (id)", + expected_findings: &[], + }, QueryFindingsTestCase { description: "an output alias read in WHERE, GROUP BY and HAVING (3.40: accepted)", sql: "SELECT quantity AS q, count(*) AS n FROM orders WHERE q > 0 GROUP BY q HAVING q > 0", diff --git a/crates/sqlquack-semantics/src/sqlserver/profile/models.rs b/crates/sqlquack-semantics/src/sqlserver/profile/models.rs index 2e2f2141b..8172f9c9b 100644 --- a/crates/sqlquack-semantics/src/sqlserver/profile/models.rs +++ b/crates/sqlquack-semantics/src/sqlserver/profile/models.rs @@ -73,6 +73,10 @@ impl DialectRules for SqlServerRules { true } + fn merged_keys_hide_other_items(&self) -> bool { + true + } + fn checks_call_signatures(&self) -> bool { false } From a80c80c2eb0cf0ed40f5b0432b6204e71e4e5cc1 Mon Sep 17 00:00:00 2001 From: Kevin Longe <273747162+kvlonge@users.noreply.github.com> Date: Thu, 19 Nov 2026 00:14:05 +0000 Subject: [PATCH 12/24] fix(semantics): a join condition reads only the items its engine lets it see An ON condition was checked against every FROM item of its level. In PostgreSQL it sees only its own comma group, up to the join: `a, b JOIN c ON a.id = c.id` is an "invalid reference to FROM-clause entry", also from a subquery or a parenthesised join, and an ON condition reading a later comma group or a later item of its own join chain is a "missing FROM-clause entry". DuckDB lets it read every earlier item and rejects only later ones. A new dialect rule, join_conditions_read, gives each join condition the items it reaches: its own group on PostgreSQL, every earlier item on DuckDB, and every item elsewhere, as before. The existing name checks then report E222 or E201. --- .../src/bigquery/profile/models.rs | 6 +- .../src/clickhouse/profile/models.rs | 6 +- .../src/databricks/profile/models.rs | 6 +- .../src/duckdb/profile/models.rs | 6 +- .../profile/tests/test_statement_findings.rs | 52 +++++++++++ .../src/lineage/_helpers/findings/levels.rs | 78 +++++++++++++++- .../src/lineage/_helpers/tracing/sources.rs | 9 +- .../sqlquack-semantics/src/lineage/models.rs | 17 ++++ .../sqlquack-semantics/src/lineage/types.rs | 5 +- .../src/postgres/profile/models.rs | 6 +- .../profile/tests/test_statement_findings.rs | 92 +++++++++++++++++++ .../src/snowflake/profile/models.rs | 6 +- .../src/sqlite/profile/models.rs | 6 +- .../src/sqlite/profile/tests/test_lineage.rs | 10 ++ .../src/sqlserver/profile/models.rs | 6 +- 15 files changed, 298 insertions(+), 13 deletions(-) diff --git a/crates/sqlquack-semantics/src/bigquery/profile/models.rs b/crates/sqlquack-semantics/src/bigquery/profile/models.rs index 215460b58..fd03fd0a0 100644 --- a/crates/sqlquack-semantics/src/bigquery/profile/models.rs +++ b/crates/sqlquack-semantics/src/bigquery/profile/models.rs @@ -10,7 +10,7 @@ use crate::bigquery::profile::_helpers::typing::{ use crate::bigquery::profile::constants::{BIGQUERY_DATE_PART_FUNCTIONS, BIGQUERY_WEEKDAY_PARTS}; use crate::dialects::types::DialectProfile; use crate::lineage::constants::SNOWFLAKE_VALUES_COLUMN; -use crate::lineage::models::Engine; +use crate::lineage::models::{Engine, JoinConditionReach}; use crate::lineage::models::{Lineage, LineageContext, TransformKind}; use crate::lineage::types::DialectRules; use crate::lineage::types::ValueType; @@ -70,6 +70,10 @@ impl DialectRules for BigQueryRules { true } + fn join_conditions_read(&self) -> JoinConditionReach { + JoinConditionReach::Every + } + fn checks_call_signatures(&self) -> bool { true } diff --git a/crates/sqlquack-semantics/src/clickhouse/profile/models.rs b/crates/sqlquack-semantics/src/clickhouse/profile/models.rs index 64680ca29..4a813dd22 100644 --- a/crates/sqlquack-semantics/src/clickhouse/profile/models.rs +++ b/crates/sqlquack-semantics/src/clickhouse/profile/models.rs @@ -14,7 +14,7 @@ use crate::lineage::constants::{ DATE_PART_FUNCTIONS, SNOWFLAKE_VALUES_COLUMN, WINDOW_ONLY_FUNCTIONS, }; use crate::lineage::main::rendered_type::rendered_type; -use crate::lineage::models::Engine; +use crate::lineage::models::{Engine, JoinConditionReach}; use crate::lineage::models::{Lineage, LineageContext}; use crate::lineage::types::DialectRules; use crate::lineage::types::ValueType; @@ -85,6 +85,10 @@ impl DialectRules for ClickHouseRules { true } + fn join_conditions_read(&self) -> JoinConditionReach { + JoinConditionReach::Every + } + fn checks_call_signatures(&self) -> bool { true } diff --git a/crates/sqlquack-semantics/src/databricks/profile/models.rs b/crates/sqlquack-semantics/src/databricks/profile/models.rs index eb0365c67..dc1b394b6 100644 --- a/crates/sqlquack-semantics/src/databricks/profile/models.rs +++ b/crates/sqlquack-semantics/src/databricks/profile/models.rs @@ -11,7 +11,7 @@ use crate::databricks::profile::_helpers::typing::{ use crate::databricks::profile::constants::DATABRICKS_VALUES_COLUMN; use crate::dialects::types::DialectProfile; use crate::lineage::constants::DATE_PART_FUNCTIONS; -use crate::lineage::models::Engine; +use crate::lineage::models::{Engine, JoinConditionReach}; use crate::lineage::models::{Lineage, LineageContext}; use crate::lineage::types::DialectRules; use crate::lineage::types::ValueType; @@ -75,6 +75,10 @@ impl DialectRules for DatabricksRules { true } + fn join_conditions_read(&self) -> JoinConditionReach { + JoinConditionReach::Every + } + fn checks_call_signatures(&self) -> bool { true } diff --git a/crates/sqlquack-semantics/src/duckdb/profile/models.rs b/crates/sqlquack-semantics/src/duckdb/profile/models.rs index 1c4e9fdab..fef814aa6 100644 --- a/crates/sqlquack-semantics/src/duckdb/profile/models.rs +++ b/crates/sqlquack-semantics/src/duckdb/profile/models.rs @@ -16,7 +16,7 @@ use crate::duckdb::profile::constants::{ DUCKDB_VALUES_COLUMN, }; use crate::lineage::constants::{DATE_PART_FUNCTIONS, WINDOW_ONLY_FUNCTIONS}; -use crate::lineage::models::Engine; +use crate::lineage::models::{Engine, JoinConditionReach}; use crate::lineage::models::{Lineage, LineageContext}; use crate::lineage::types::DialectRules; use crate::lineage::types::ValueType; @@ -76,6 +76,10 @@ impl DialectRules for DuckDbRules { true } + fn join_conditions_read(&self) -> JoinConditionReach { + JoinConditionReach::Earlier + } + fn checks_call_signatures(&self) -> bool { true } 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 86dcfd91a..fa825ca53 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 @@ -770,6 +770,58 @@ fn given_a_comma_after_a_joined_item_when_resolving_names_then_the_join_reads_on sql: "SELECT * FROM orders a JOIN orders b USING (status), customers c", expected_findings: &[], }, + QueryFindingsTestCase { + description: "an ON condition reading a USING key only a later join merges (2.0: column not found)", + sql: "SELECT 1 FROM exports a JOIN exports b ON id IS NOT NULL JOIN orders c ON true JOIN customers d USING (id)", + expected_findings: &[( + "E201", + "Unknown column 'id' (not found in any referenced table)", + 42, + )], + }, + QueryFindingsTestCase { + description: "an ON condition reading a key a later join widens, beside an item offering the name (2.0: accepted)", + sql: "SELECT 1 FROM orders x JOIN (orders a JOIN customers b USING (id)) ON true JOIN exports c ON id IS NOT NULL JOIN customers d USING (id)", + expected_findings: &[], + }, + QueryFindingsTestCase { + description: "an ON condition reading a USING key a later join merges again (2.0: accepted)", + sql: "SELECT 1 FROM orders a JOIN customers b USING (id) JOIN exports c ON id IS NOT NULL JOIN customers d USING (id)", + expected_findings: &[], + }, + QueryFindingsTestCase { + description: "an ON condition reading a later comma group's item (2.0: table not found)", + sql: "SELECT 1 FROM orders a JOIN customers c ON b.id = c.id, customers b", + expected_findings: &[( + "E222", + "Unknown table or alias 'b' referenced by column 'id'", + 43, + )], + }, + QueryFindingsTestCase { + description: "an ON condition reading a later item of its own join chain (2.0: table not found)", + sql: "SELECT 1 FROM orders a JOIN customers b ON c.id = a.id JOIN customers c ON true", + expected_findings: &[( + "E222", + "Unknown table or alias 'c' referenced by column 'id'", + 43, + )], + }, + QueryFindingsTestCase { + description: "an ON condition reading an earlier comma group's item (2.0: accepted)", + sql: "SELECT 1 FROM orders a, customers b JOIN customers c ON a.id = c.id", + expected_findings: &[], + }, + QueryFindingsTestCase { + description: "an ON condition reading an earlier comma group's column (2.0: accepted)", + sql: "SELECT 1 FROM orders a, customers b JOIN customers c ON customer_id = c.id", + expected_findings: &[], + }, + QueryFindingsTestCase { + description: "a parenthesised join's ON condition reading an earlier comma item (2.0: accepted)", + sql: "SELECT 1 FROM orders a, (customers b JOIN customers c ON a.id = c.id)", + expected_findings: &[], + }, QueryFindingsTestCase { description: "an unqualified USING key beside a later item's column of its name, which DuckDB binds", sql: "SELECT id FROM orders a JOIN customers b USING (id), exports c CROSS JOIN customers d", diff --git a/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs b/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs index 88ce6acff..74ec3748b 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs @@ -26,11 +26,11 @@ use crate::lineage::_helpers::tracing::queries::{ }; use crate::lineage::_helpers::tracing::resolution::key; use crate::lineage::_helpers::tracing::reuse::{Reach, Reuse, reusing}; -use crate::lineage::_helpers::tracing::sources::{every_join, from_parts}; +use crate::lineage::_helpers::tracing::sources::{every_join, from_parts, sided_parts}; use crate::lineage::constants::{COLUMNS_FUNCTION, OUTER_RIGHT_JOINS}; -use crate::lineage::models::Engine; use crate::lineage::models::NameFinding; -use crate::lineage::models::{Lineage, LineageContext, LineageScope, NamedQuery}; +use crate::lineage::models::{Engine, JoinConditionReach}; +use crate::lineage::models::{Lineage, LineageContext, LineageScope, MergedKey, NamedQuery}; use crate::lineage::types::DialectRules; /// The findings of `query` and its inner queries; `aliases` are enclosing levels' output aliases. @@ -205,6 +205,67 @@ fn left_scope<'a>( }) } +/// Each join condition of `select`, with the FROM items `reach` lets it read. +fn condition_scopes<'s, 'a>( + select: &'s Select, + scope: &LineageScope<'a>, + reach: JoinConditionReach, +) -> Vec<(&'s Expression, LineageScope<'a>)> { + let at = |part: usize| { + scope.starts.get(part).map_or(scope.sources.len(), |start| { + (*start).min(scope.sources.len()) + }) + }; + let mut scopes: Vec<(&'s Expression, LineageScope<'a>)> = Vec::new(); + for part in sided_parts(select) { + let Some(join) = part.join else { + continue; + }; + let first: usize = match reach { + JoinConditionReach::OwnGroup => at(part.left.start), + _ => 0, + }; + let reached: std::ops::Range = first..at(part.right.end); + for condition in join.on.iter().chain(join.match_condition.iter()) { + scopes.push((condition, reached_scope(scope, reached.clone()))); + } + } + scopes +} + +/// `scope` narrowed to the sources in `reached`, with each merged key as it stood within them. +fn reached_scope<'a>( + scope: &LineageScope<'a>, + reached: std::ops::Range, +) -> LineageScope<'a> { + LineageScope { + sources: scope.sources[reached.clone()].to_vec(), + merged: scope + .merged + .iter() + .filter_map(|merged| { + let range = reached_range(merged, &reached)?; + Some(MergedKey { + sources: range.start - reached.start..range.end - reached.start, + earlier: Vec::new(), + ..merged.clone() + }) + }) + .collect(), + ..empty_scope(scope.ctes, scope.outer) + } +} + +/// The latest sources a merged key stood for wholly inside `reached`, if it was merged by then. +fn reached_range<'k>( + merged: &'k MergedKey, + reached: &std::ops::Range, +) -> Option<&'k std::ops::Range> { + std::iter::once(&merged.sources) + .chain(merged.earlier.iter().rev()) + .find(|range| reached.start <= range.start && range.end <= reached.end) +} + /// The key of a derived table in FROM, by its alias; none when `item` is not a derived table. fn derived_key(item: &Expression, context: LineageContext<'_>) -> Option> { match item { @@ -355,6 +416,10 @@ fn select_findings( } let mut read: Vec<&Expression> = clauses(select); read.extend(orders.iter().flat_map(|order| order_keys(order))); + let conditions: Vec<(&Expression, LineageScope<'_>)> = match rules.join_conditions_read() { + JoinConditionReach::Every => Vec::new(), + reach => condition_scopes(select, &scope, reach), + }; for clause in read { let named_place: bool = ordered_name_place(select, &orders, clause) || (rules.group_keys_read_outputs() && group_key_place(select, clause)); @@ -366,6 +431,13 @@ fn select_findings( }, (false, false) => unnamed, }; + let read = match conditions.iter().find(|(on, _)| std::ptr::eq(*on, clause)) { + Some((_, reached)) => Level { + scope: reached, + ..read + }, + None => read, + }; let (found, inner) = clause_findings(clause, &read, context); own.extend(found); nested.extend(inner); diff --git a/crates/sqlquack-semantics/src/lineage/_helpers/tracing/sources.rs b/crates/sqlquack-semantics/src/lineage/_helpers/tracing/sources.rs index 9be73e393..a0deb2885 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/tracing/sources.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/tracing/sources.rs @@ -285,6 +285,7 @@ fn joined( expression: None, }, sources: reach.clone(), + earlier: Vec::new(), }; match merged_keys.iter_mut().find(|merged| { overlaps(&merged.sources, &reach) @@ -295,7 +296,13 @@ fn joined( Some(merged) => { let sources: Range = merged.sources.start.min(reach.start)..merged.sources.end.max(reach.end); - *merged = MergedKey { sources, ..column }; + let mut earlier: Vec> = std::mem::take(&mut merged.earlier); + earlier.push(merged.sources.clone()); + *merged = MergedKey { + sources, + earlier, + ..column + }; } None => merged_keys.push(column), } diff --git a/crates/sqlquack-semantics/src/lineage/models.rs b/crates/sqlquack-semantics/src/lineage/models.rs index aa94a4970..c6316e569 100644 --- a/crates/sqlquack-semantics/src/lineage/models.rs +++ b/crates/sqlquack-semantics/src/lineage/models.rs @@ -161,6 +161,8 @@ pub(crate) struct MergedKey { pub(crate) column: ScopedColumn, /// The sources of the join, whose columns of the key's name it stands in for. pub(crate) sources: Range, + /// The sources it stood for before later joins merged it again, oldest first. + pub(crate) earlier: Vec>, } /// A FROM item: its key, its known columns, and whether it offers others of unknown name. @@ -294,6 +296,10 @@ impl DialectRules for SharedRules { true } + fn join_conditions_read(&self) -> JoinConditionReach { + JoinConditionReach::Every + } + fn checks_call_signatures(&self) -> bool { true } @@ -427,6 +433,17 @@ impl DialectRules for SharedRules { } } +/// Which FROM items a join's ON condition may read. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) enum JoinConditionReach { + /// Every FROM item of its query level. + Every, + /// Every item before the join, and the join's own item. + Earlier, + /// The items of its own comma group before the join, and the join's own item. + OwnGroup, +} + /// The engine whose binder the checks follow. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub(crate) enum Engine { diff --git a/crates/sqlquack-semantics/src/lineage/types.rs b/crates/sqlquack-semantics/src/lineage/types.rs index 3dd780d92..65fc5fa89 100644 --- a/crates/sqlquack-semantics/src/lineage/types.rs +++ b/crates/sqlquack-semantics/src/lineage/types.rs @@ -8,7 +8,7 @@ use sqlquack_syntax::{DataType, Expression}; use crate::dialects::types::DialectProfile; use crate::lineage::_helpers::checks::window_only::named_window_only; use crate::lineage::main::rendered_type::rendered_type; -use crate::lineage::models::{Engine, Nullability}; +use crate::lineage::models::{Engine, JoinConditionReach, Nullability}; use crate::lineage::models::{ Lineage, LineageContext, NullabilityRule, SchemaColumn, SourceColumn, }; @@ -81,6 +81,9 @@ pub(crate) trait DialectRules: Sync { /// Whether an unqualified USING or NATURAL key reads its merged column though an item outside its join offers the name. fn merged_keys_hide_other_items(&self) -> bool; + /// Which FROM items a join's ON condition may read. + fn join_conditions_read(&self) -> JoinConditionReach; + /// Whether calls are checked against the signatures of the functions they name. fn checks_call_signatures(&self) -> bool; diff --git a/crates/sqlquack-semantics/src/postgres/profile/models.rs b/crates/sqlquack-semantics/src/postgres/profile/models.rs index 9d964c453..5006d5bd3 100644 --- a/crates/sqlquack-semantics/src/postgres/profile/models.rs +++ b/crates/sqlquack-semantics/src/postgres/profile/models.rs @@ -8,7 +8,7 @@ use crate::dialects::types::DialectProfile; use crate::lineage::constants::{ DATE_PART_FUNCTIONS, SNOWFLAKE_VALUES_COLUMN, WINDOW_ONLY_FUNCTIONS, }; -use crate::lineage::models::Engine; +use crate::lineage::models::{Engine, JoinConditionReach}; use crate::lineage::models::{Lineage, LineageContext}; use crate::lineage::types::DialectRules; use crate::lineage::types::ValueType; @@ -77,6 +77,10 @@ impl DialectRules for PostgresRules { false } + fn join_conditions_read(&self) -> JoinConditionReach { + JoinConditionReach::OwnGroup + } + fn checks_call_signatures(&self) -> bool { true } 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 1defa9802..105f4ed65 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 @@ -358,6 +358,98 @@ fn given_a_comma_after_a_joined_item_when_resolving_names_then_the_join_reads_on sql: "SELECT * FROM orders a JOIN orders b USING (status), customers c", expected_findings: &[], }, + QueryFindingsTestCase { + description: "an ON condition reading a USING key only a later join merges (18: column does not exist)", + sql: "SELECT 1 FROM exports a JOIN exports b ON id IS NOT NULL JOIN orders c ON true JOIN customers d USING (id)", + expected_findings: &[( + "E201", + "Unknown column 'id' (not found in any referenced table)", + 42, + )], + }, + QueryFindingsTestCase { + description: "an ON condition reading a key a later join widens past an item offering the name (18: ambiguous)", + sql: "SELECT 1 FROM orders x JOIN (orders a JOIN customers b USING (id)) ON true JOIN exports c ON id IS NOT NULL JOIN customers d USING (id)", + expected_findings: &[( + "E221", + "Ambiguous unqualified column 'id' found in 3 referenced tables", + 93, + )], + }, + QueryFindingsTestCase { + description: "an ON condition reading a USING key a later join merges again (18: accepted)", + sql: "SELECT 1 FROM orders a JOIN customers b USING (id) JOIN exports c ON id IS NOT NULL JOIN customers d USING (id)", + expected_findings: &[], + }, + QueryFindingsTestCase { + description: "an ON condition reading a NATURAL key a later join merges again (18: accepted)", + sql: "SELECT 1 FROM orders a NATURAL JOIN customers b JOIN exports c ON id IS NOT NULL NATURAL JOIN customers d", + expected_findings: &[], + }, + QueryFindingsTestCase { + description: "an ON condition reading an earlier comma group's item (18: invalid reference)", + sql: "SELECT 1 FROM orders a, customers b JOIN customers c ON a.id = c.id", + expected_findings: &[( + "E222", + "Unknown table or alias 'a' referenced by column 'id'", + 56, + )], + }, + QueryFindingsTestCase { + description: "an ON condition reading an earlier comma group's column (18: cannot be referenced)", + sql: "SELECT 1 FROM orders a, customers b JOIN customers c ON customer_id = c.id", + expected_findings: &[( + "E201", + "Unknown column 'customer_id' (not found in any referenced table)", + 56, + )], + }, + QueryFindingsTestCase { + description: "an ON condition reading a later comma group's item (18: missing FROM entry)", + sql: "SELECT 1 FROM orders a JOIN customers c ON b.id = c.id, customers b", + expected_findings: &[( + "E222", + "Unknown table or alias 'b' referenced by column 'id'", + 43, + )], + }, + QueryFindingsTestCase { + description: "an ON condition reading a later item of its own join chain (18: missing FROM entry)", + sql: "SELECT 1 FROM orders a JOIN customers b ON c.id = a.id JOIN customers c ON true", + expected_findings: &[( + "E222", + "Unknown table or alias 'c' referenced by column 'id'", + 43, + )], + }, + QueryFindingsTestCase { + description: "a parenthesised join's ON condition reading an earlier comma item (18: invalid reference)", + sql: "SELECT 1 FROM orders a, (customers b JOIN customers c ON a.id = c.id)", + expected_findings: &[( + "E222", + "Unknown table or alias 'a' referenced by column 'id'", + 57, + )], + }, + QueryFindingsTestCase { + description: "a subquery in an ON condition reading an earlier comma group's item (18: invalid reference)", + sql: "SELECT 1 FROM orders a, customers b JOIN customers c ON (SELECT a.id) = c.id", + expected_findings: &[( + "E222", + "Unknown table or alias 'a' referenced by column 'id'", + 64, + )], + }, + QueryFindingsTestCase { + description: "an ON condition reading its own comma group (18: accepted)", + sql: "SELECT 1 FROM orders a, customers b JOIN customers c ON b.id = c.id", + expected_findings: &[], + }, + QueryFindingsTestCase { + description: "an ON condition reading its group's merged key, an earlier group offering the name (18: accepted)", + sql: "SELECT 1 FROM customers a, customers b JOIN customers c USING (name) JOIN orders d ON name = d.status", + expected_findings: &[], + }, QueryFindingsTestCase { description: "a USING key read unqualified beside a comma item offering it (18: ambiguous)", sql: "SELECT id FROM orders a JOIN customers b USING (id), customers c", diff --git a/crates/sqlquack-semantics/src/snowflake/profile/models.rs b/crates/sqlquack-semantics/src/snowflake/profile/models.rs index ed1690cb2..d80152c28 100644 --- a/crates/sqlquack-semantics/src/snowflake/profile/models.rs +++ b/crates/sqlquack-semantics/src/snowflake/profile/models.rs @@ -8,7 +8,7 @@ use crate::dialects::types::DialectProfile; use crate::lineage::constants::{ DATE_PART_FUNCTIONS, SNOWFLAKE_VALUES_COLUMN, WINDOW_ONLY_FUNCTIONS, }; -use crate::lineage::models::Engine; +use crate::lineage::models::{Engine, JoinConditionReach}; use crate::lineage::models::{Lineage, LineageContext}; use crate::lineage::types::DialectRules; use crate::lineage::types::ValueType; @@ -115,6 +115,10 @@ impl DialectRules for SnowflakeRules { true } + fn join_conditions_read(&self) -> JoinConditionReach { + JoinConditionReach::Every + } + fn checks_call_signatures(&self) -> bool { true } diff --git a/crates/sqlquack-semantics/src/sqlite/profile/models.rs b/crates/sqlquack-semantics/src/sqlite/profile/models.rs index 1b3a10917..37ac788f5 100644 --- a/crates/sqlquack-semantics/src/sqlite/profile/models.rs +++ b/crates/sqlquack-semantics/src/sqlite/profile/models.rs @@ -6,7 +6,7 @@ use sqlquack_syntax::{DataType, Dialect, Expression}; use crate::dialects::types::DialectProfile; use crate::lineage::constants::{DATE_PART_FUNCTIONS, WINDOW_ONLY_FUNCTIONS}; -use crate::lineage::models::Engine; +use crate::lineage::models::{Engine, JoinConditionReach}; use crate::lineage::models::{Lineage, LineageContext, Nullability}; use crate::lineage::types::DialectRules; use crate::lineage::types::ValueType; @@ -70,6 +70,10 @@ impl DialectRules for SqliteRules { false } + fn join_conditions_read(&self) -> JoinConditionReach { + JoinConditionReach::Every + } + fn checks_call_signatures(&self) -> bool { false } diff --git a/crates/sqlquack-semantics/src/sqlite/profile/tests/test_lineage.rs b/crates/sqlquack-semantics/src/sqlite/profile/tests/test_lineage.rs index 6502c26a1..601e34fad 100644 --- a/crates/sqlquack-semantics/src/sqlite/profile/tests/test_lineage.rs +++ b/crates/sqlquack-semantics/src/sqlite/profile/tests/test_lineage.rs @@ -154,6 +154,16 @@ fn given_a_sqlite_query_sqlite_accepts_when_checking_then_nothing_is_found() { sql: "SELECT quantity AS q FROM orders WHERE EXISTS (SELECT sum(1) OVER w FROM customers WINDOW w AS (ORDER BY q))", expected_findings: &[], }, + QueryFindingsTestCase { + description: "an ON condition reading a later comma group's item (3.40: accepted)", + sql: "SELECT 1 FROM orders a JOIN customers c ON b.id = c.id, customers b", + expected_findings: &[], + }, + QueryFindingsTestCase { + description: "an ON condition reading a later item of its own join chain (3.40: accepted)", + sql: "SELECT 1 FROM orders a JOIN customers b ON c.id = a.id JOIN customers c ON true", + expected_findings: &[], + }, QueryFindingsTestCase { description: "a USING key a later USING join merges again (3.40: accepted)", sql: "SELECT id FROM orders a JOIN customers b USING (id) JOIN customers c USING (id)", diff --git a/crates/sqlquack-semantics/src/sqlserver/profile/models.rs b/crates/sqlquack-semantics/src/sqlserver/profile/models.rs index 8172f9c9b..32c634b52 100644 --- a/crates/sqlquack-semantics/src/sqlserver/profile/models.rs +++ b/crates/sqlquack-semantics/src/sqlserver/profile/models.rs @@ -9,7 +9,7 @@ use crate::lineage::constants::{ DATE_PART_FUNCTIONS, SNOWFLAKE_VALUES_COLUMN, WINDOW_ONLY_FUNCTIONS, }; use crate::lineage::main::rendered_type::rendered_type; -use crate::lineage::models::Engine; +use crate::lineage::models::{Engine, JoinConditionReach}; use crate::lineage::models::{Lineage, LineageContext}; use crate::lineage::types::DialectRules; use crate::lineage::types::ValueType; @@ -77,6 +77,10 @@ impl DialectRules for SqlServerRules { true } + fn join_conditions_read(&self) -> JoinConditionReach { + JoinConditionReach::Every + } + fn checks_call_signatures(&self) -> bool { false } From 1556ff6cadbc3722bc40946454a4fffbf6b5a549 Mon Sep 17 00:00:00 2001 From: Kevin Longe <273747162+kvlonge@users.noreply.github.com> Date: Thu, 19 Nov 2026 03:13:28 +0000 Subject: [PATCH 13/24] perf(semantics): normalise a single-spaced ASCII type spelling in one step type_family normalised every spelling by splitting on whitespace, collecting, joining and lowercasing, four allocations per call, though spellings such as NUMBER(38,0) are almost always printable ASCII with single spaces already. Such a spelling is now lowered in place; any other takes the full path, so the result is unchanged. The analysis workload runs 4.3% fewer instructions. --- .../src/lineage/_helpers/checks/families.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/crates/sqlquack-semantics/src/lineage/_helpers/checks/families.rs b/crates/sqlquack-semantics/src/lineage/_helpers/checks/families.rs index eca1fd599..845c50e48 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/checks/families.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/checks/families.rs @@ -270,6 +270,11 @@ pub(crate) fn normalized_spelling(spelling: &str) -> String { let trimmed: &str = spelling .trim() .trim_matches(|character| matches!(character, '"' | '\'' | '`')); + if is_single_spaced_ascii(trimmed) { + let mut lowered: String = trimmed.to_owned(); + lowered.make_ascii_lowercase(); + return lowered; + } trimmed .split_whitespace() .collect::>() @@ -277,6 +282,17 @@ pub(crate) fn normalized_spelling(spelling: &str) -> String { .to_lowercase() } +/// Whether `text` is printable ASCII with single spaces between words and none around them. +fn is_single_spaced_ascii(text: &str) -> bool { + let bytes: &[u8] = text.as_bytes(); + bytes.first() != Some(&b' ') + && bytes.last() != Some(&b' ') + && !text.contains(" ") + && bytes + .iter() + .all(|byte| *byte == b' ' || byte.is_ascii_graphic()) +} + /// The family of an array, map or struct spelling, or of a decimal that holds only integers. pub(crate) fn composite_family(normalized: &str) -> Option { if let Some((base, inner)) = type_arguments(normalized) { From b17c324dec9185f164e00d49af143d2588ba942f Mon Sep 17 00:00:00 2001 From: Kevin Longe <273747162+kvlonge@users.noreply.github.com> Date: Thu, 19 Nov 2026 03:16:10 +0000 Subject: [PATCH 14/24] perf(semantics): upper-case a Snowflake identifier in place Snowflake's resolved_identifier built each unquoted name's upper-case copy byte by byte through an iterator. It now copies the name once and upper-cases it in place, with the same result. The analysis workload runs 0.65% fewer instructions. --- .../sqlquack-semantics/src/snowflake/profile/models.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/crates/sqlquack-semantics/src/snowflake/profile/models.rs b/crates/sqlquack-semantics/src/snowflake/profile/models.rs index d80152c28..be6da1992 100644 --- a/crates/sqlquack-semantics/src/snowflake/profile/models.rs +++ b/crates/sqlquack-semantics/src/snowflake/profile/models.rs @@ -35,7 +35,11 @@ impl DialectProfile for Snowflake { fn resolved_identifier(&self, name: &str, quoted: bool) -> String { match quoted { true => name.to_owned(), - false => name.to_ascii_uppercase(), + false => { + let mut upper: String = name.to_owned(); + upper.make_ascii_uppercase(); + upper + } } } @@ -66,7 +70,9 @@ impl DialectProfile for SnowflakeQuotedIgnoringCase { } fn resolved_identifier(&self, name: &str, _quoted: bool) -> String { - name.to_ascii_uppercase() + let mut upper: String = name.to_owned(); + upper.make_ascii_uppercase(); + upper } fn same_identifier(&self, left: &str, right: &str) -> bool { From 0276edfdbe6d390be6cdf3698dc1ff302196a475 Mon Sep 17 00:00:00 2001 From: Kevin Longe <273747162+kvlonge@users.noreply.github.com> Date: Thu, 19 Nov 2026 03:19:24 +0000 Subject: [PATCH 15/24] perf(semantics): order findings without a position map when none needs one ordered_findings mapped every node of the statement to its depth-first position on each call, with SipHash, to sort findings by where they occur. When every finding is a name finding, all sort at position zero in the order they were emitted, so the map went unread; a statement with no finding built it too. Such a list is now returned as it is, and the map that remains uses FxHash, as the crate's other position maps do. The analysis workload runs 4.5% fewer instructions. --- .../src/lineage/_helpers/findings/statement.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/crates/sqlquack-semantics/src/lineage/_helpers/findings/statement.rs b/crates/sqlquack-semantics/src/lineage/_helpers/findings/statement.rs index 2ce051e31..9fdcf1392 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/findings/statement.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/findings/statement.rs @@ -1,6 +1,6 @@ //! The findings of one parsed statement, each finding once, in polyglot's order. -use std::collections::HashMap; +use rustc_hash::FxHashMap; use sqlquack_syntax::Expression; use sqlquack_syntax::ExpressionWalk; @@ -42,7 +42,10 @@ pub(crate) fn ordered_findings( distinct.push(finding); } } - let order: HashMap = statement + if distinct.iter().all(|finding| finding.phase == NAME_PHASE) { + return distinct; + } + let order: FxHashMap = statement .dfs() .enumerate() .map(|(position, node)| (std::ptr::from_ref(node) as usize, position)) From 4a81f84621ecc06c77f9b591f25e920f9155c969 Mon Sep 17 00:00:00 2001 From: Kevin Longe <273747162+kvlonge@users.noreply.github.com> Date: Thu, 19 Nov 2026 03:31:30 +0000 Subject: [PATCH 16/24] perf(semantics): read the family of a bare type word without allocating Most types that lineage carries are a single word such as BIGINT or VARCHAR. For a spelling made only of ASCII letters, digits and underscores, normalising, looking for arguments or a composite form, and stripping signedness all reduce to the base family of the lower-cased word, so type_family now lowers such a word on the stack and looks it up directly. Every other spelling takes the existing path unchanged. --- .../src/lineage/_helpers/checks/families.rs | 23 +++++++++++++++++++ .../src/lineage/main/type_family.rs | 5 +++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/crates/sqlquack-semantics/src/lineage/_helpers/checks/families.rs b/crates/sqlquack-semantics/src/lineage/_helpers/checks/families.rs index 845c50e48..c7707d1c5 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/checks/families.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/checks/families.rs @@ -14,6 +14,9 @@ use crate::lineage::models::{LineageContext, LineageScope}; /// The scale of a fixed-point type that holds only integers. const INTEGER_SCALE: &str = "0"; +/// The longest bare type word `word_family` lowers on the stack. +const WORD_CAPACITY: usize = 32; + pub(crate) fn type_arguments(normalized: &str) -> Option<(&str, &str)> { let open: usize = normalized.find('(')?; if !normalized.ends_with(')') || open + 1 >= normalized.len() { @@ -25,6 +28,26 @@ pub(crate) fn type_arguments(normalized: &str) -> Option<(&str, &str)> { )) } +/// The family of a bare type word such as `BIGINT`, lowered without allocating, else `None`. +pub(crate) fn word_family(spelling: &str) -> Option { + let bytes: &[u8] = spelling.as_bytes(); + if bytes.is_empty() + || bytes.len() > WORD_CAPACITY + || !bytes + .iter() + .all(|byte| byte.is_ascii_alphanumeric() || *byte == b'_') + { + return None; + } + let mut lowered: [u8; WORD_CAPACITY] = [0; WORD_CAPACITY]; + for (slot, byte) in lowered.iter_mut().zip(bytes) { + *slot = byte.to_ascii_lowercase(); + } + Some(base_family(&String::from_utf8_lossy( + &lowered[..bytes.len()], + ))) +} + fn base_family(base: &str) -> Family { match base { "bool" | "boolean" => Family::Boolean, diff --git a/crates/sqlquack-semantics/src/lineage/main/type_family.rs b/crates/sqlquack-semantics/src/lineage/main/type_family.rs index 11914a411..492c7a497 100644 --- a/crates/sqlquack-semantics/src/lineage/main/type_family.rs +++ b/crates/sqlquack-semantics/src/lineage/main/type_family.rs @@ -1,12 +1,15 @@ //! `type_family`: the family of a type as spelled. use crate::lineage::_helpers::checks::families::{ - composite_family, normalized_spelling, scalar_family, type_arguments, + composite_family, normalized_spelling, scalar_family, type_arguments, word_family, }; use crate::lineage::models::Family; /// The family of a type as spelled, `NUMBER(38,0)` an integer and `VARCHAR(10)` a string. pub(crate) fn type_family(spelling: &str) -> Family { + if let Some(family) = word_family(spelling) { + return family; + } let normalized: String = normalized_spelling(spelling); if normalized.is_empty() { return Family::Unknown; From 62538700c35d88af52035bcdeba1d5f36b3af0de Mon Sep 17 00:00:00 2001 From: Kevin Longe <273747162+kvlonge@users.noreply.github.com> Date: Thu, 19 Nov 2026 03:38:47 +0000 Subject: [PATCH 17/24] perf(semantics): walk a node's children without collecting them first The scope walks behind type locations, aggregate placement, clause findings and held-node checks asked each node for a fresh vector of its children, which allocated once per visited node. They now visit the children in place through the syntax visitor that children() itself uses, pushing them onto the walk's own stack and reversing that slice where the walk reversed the vector, so every walk sees the same nodes in the same order. The held-node check still stops judging children once one holds. --- .../src/lineage/_helpers/checks/issues.rs | 13 +++++++---- .../lineage/_helpers/checks/scope_rules.rs | 22 ++++++++++--------- .../src/lineage/_helpers/findings/levels.rs | 6 ++--- .../src/lineage/_helpers/tracing/reuse.rs | 9 ++++---- 4 files changed, 28 insertions(+), 22 deletions(-) diff --git a/crates/sqlquack-semantics/src/lineage/_helpers/checks/issues.rs b/crates/sqlquack-semantics/src/lineage/_helpers/checks/issues.rs index aa1fb4455..a53c7d78f 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/checks/issues.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/checks/issues.rs @@ -1,8 +1,8 @@ //! Findings of the checks: where polyglot locates them, and their codes. use sqlquack_syntax::Expression; -use sqlquack_syntax::ExpressionWalk; use sqlquack_syntax::tokens::models::Span; +use sqlquack_syntax::traversal::types::AstNode; use crate::lineage::_helpers::tracing::queries::takes_clauses; use crate::lineage::models::NameFinding; @@ -58,13 +58,18 @@ pub(crate) fn in_scope(node: &Expression) -> Vec<&Expression> { continue; } visited.push(current); - let mut children: Vec<&Expression> = current.children(); - children.reverse(); - pending.extend(children); + let start: usize = pending.len(); + each_child(current, |child| pending.push(child)); + pending[start..].reverse(); } visited } +/// Calls `visit` on each direct child of `node`, in the order `children` lists them, collecting none. +pub(crate) fn each_child<'a>(node: &'a Expression, mut visit: impl FnMut(&'a Expression)) { + node.visit_syntax_untracked(&mut visit, &mut |_| {}); +} + /// Whether `node` starts a query of its own: a SELECT, set operation, CTE or derived table. pub(crate) fn query_boundary(node: &Expression) -> bool { match node { diff --git a/crates/sqlquack-semantics/src/lineage/_helpers/checks/scope_rules.rs b/crates/sqlquack-semantics/src/lineage/_helpers/checks/scope_rules.rs index 7fe3cfb0d..7e80ef54f 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/checks/scope_rules.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/checks/scope_rules.rs @@ -9,7 +9,7 @@ use sqlquack_syntax::expressions::names::models::{Column, Identifier, Literal}; use sqlquack_syntax::expressions::queries::models::{OrderBy, Select}; use crate::dialects::main::dialect_rules::dialect_rules; -use crate::lineage::_helpers::checks::issues::{in_scope, issue, rule_location}; +use crate::lineage::_helpers::checks::issues::{each_child, in_scope, issue, rule_location}; use crate::lineage::_helpers::findings::checks::keyword_function; use crate::lineage::_helpers::tracing::expressions::is_aggregate; use crate::lineage::_helpers::tracing::resolution::key; @@ -709,14 +709,16 @@ fn placement( findings.push(rule("E231", MISPLACED_AGGREGATE.to_owned(), node, select)); continue; } - let mut children: Vec<&Expression> = node.children(); - children.reverse(); - pending.extend(children.into_iter().map(|child| Placed { - node: child, - inside_aggregate: placed.inside_aggregate || aggregate, - inside_window: placed.inside_window, - window_function: false, - })); + let start: usize = pending.len(); + each_child(node, |child| { + pending.push(Placed { + node: child, + inside_aggregate: placed.inside_aggregate || aggregate, + inside_window: placed.inside_window, + window_function: false, + }); + }); + pending[start..].reverse(); } findings } @@ -740,7 +742,7 @@ fn ordinary_aggregate(expression: &Expression) -> bool { if is_aggregate(node) { return true; } - pending.extend(node.children()); + each_child(node, |child| pending.push(child)); } false } diff --git a/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs b/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs index 74ec3748b..a6bad5bcb 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs @@ -9,7 +9,7 @@ use sqlquack_syntax::expressions::queries::models::{ }; use crate::dialects::main::dialect_rules::dialect_rules; -use crate::lineage::_helpers::checks::issues::in_scope; +use crate::lineage::_helpers::checks::issues::{each_child, in_scope}; use crate::lineage::_helpers::checks::node_checks::{NodeLevel, node_findings}; use crate::lineage::_helpers::checks::scope_rules::{ RuleLevel, select_rules, set_order, set_order_by, @@ -764,11 +764,11 @@ fn clause_findings( } } _ => { - for child in node.children() { + each_child(node, |child| { if !date_part_argument(node, child, context) { pending.push(child); } - } + }); } } } diff --git a/crates/sqlquack-semantics/src/lineage/_helpers/tracing/reuse.rs b/crates/sqlquack-semantics/src/lineage/_helpers/tracing/reuse.rs index 8dac95caa..db25848f0 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/tracing/reuse.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/tracing/reuse.rs @@ -10,6 +10,7 @@ use sqlquack_syntax::Expression; use sqlquack_syntax::ExpressionWalk; use sqlquack_syntax::expressions::queries::models::With; +use crate::lineage::_helpers::checks::issues::each_child; use crate::lineage::_helpers::tracing::expressions::is_aggregate; use crate::lineage::models::{FromItems, Lineage, NamedQuery, ResolvedQuery}; @@ -135,11 +136,9 @@ fn holds( Some(Some(holds_it)) => holds_it, Some(None) => { let holds_it: bool = crate::lineage::_helpers::tracing::stack::deeper(|| { - wanted(node) - || node - .children() - .into_iter() - .any(|child| holds(child, judged, wanted)) + let mut found: bool = wanted(node); + each_child(node, |child| found = found || holds(child, judged, wanted)); + found }); with_kept(|kept| judged(kept).insert(address, holds_it)); holds_it From da98aa543e113083054d5865b5dd2c833586e771 Mon Sep 17 00:00:00 2001 From: Kevin Longe <273747162+kvlonge@users.noreply.github.com> Date: Thu, 19 Nov 2026 03:44:55 +0000 Subject: [PATCH 18/24] perf(semantics): hash scope-rule names with FxHash The scope rules and the level findings kept their alias, name and table-index sets in std maps, which hash every key with SipHash. They now use the FxHash maps the rest of the crate already uses. Each set is only probed by key or searched with any(), never iterated in an order that reaches a finding, so the hasher cannot change what is reported. --- .../lineage/_helpers/checks/scope_rules.rs | 47 +++++++++---------- .../src/lineage/_helpers/findings/levels.rs | 5 +- 2 files changed, 25 insertions(+), 27 deletions(-) diff --git a/crates/sqlquack-semantics/src/lineage/_helpers/checks/scope_rules.rs b/crates/sqlquack-semantics/src/lineage/_helpers/checks/scope_rules.rs index 7e80ef54f..fa0c41f44 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/checks/scope_rules.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/checks/scope_rules.rs @@ -1,7 +1,6 @@ //! A SELECT's scope rules: structure, aggregate and window placement, grouping and ordering. -use std::collections::{HashMap, HashSet}; - +use rustc_hash::{FxHashMap, FxHashSet}; use serde_json::{Map, Value}; use sqlquack_syntax::Expression; use sqlquack_syntax::ExpressionWalk; @@ -86,7 +85,7 @@ fn rule(code: &'static str, message: String, at: &Expression, select: &Expressio type Clause<'s, 'm> = ( &'s Expression, (bool, bool), - &'m HashMap, + &'m FxHashMap, ); /// What the rules read of a SELECT's level: its scope and the engine. @@ -109,8 +108,8 @@ impl RuleLevel<'_, '_> { .any(|source| source.query.open || source.query.columns.is_empty()) } - fn input_names(&self) -> HashSet<&str> { - let mut names: HashSet<&str> = HashSet::new(); + fn input_names(&self) -> FxHashSet<&str> { + let mut names: FxHashSet<&str> = FxHashSet::default(); for source in &self.scope.sources { names.extend( source @@ -132,13 +131,13 @@ pub(crate) fn select_rules( let mut findings: Vec = structure_rules((node, select), level); findings.extend(order_positions((node, select), level)); findings.extend(star_modifiers((node, select), level)); - let aliases: HashMap = output_aliases(select, level); - let empty: HashMap = HashMap::new(); - let usable: &HashMap = match level.open() { + let aliases: FxHashMap = output_aliases(select, level); + let empty: FxHashMap = FxHashMap::default(); + let usable: &FxHashMap = match level.open() { true => &empty, false => &aliases, }; - let mut seen: HashMap = HashMap::new(); + let mut seen: FxHashMap = FxHashMap::default(); for projection in &select.expressions { findings.extend(placement(projection, (true, true), (&seen, level), node)); if let Expression::Alias(alias) = projection { @@ -250,8 +249,8 @@ fn order_by_all(order: &OrderBy, _level: RuleLevel<'_, '_>) -> bool { fn output_aliases<'s>( select: &'s Select, level: RuleLevel<'_, '_>, -) -> HashMap { - let mut aliases: HashMap = HashMap::new(); +) -> FxHashMap { + let mut aliases: FxHashMap = FxHashMap::default(); if !select .expressions .iter() @@ -259,8 +258,8 @@ fn output_aliases<'s>( { return aliases; } - let inputs: HashSet<&str> = level.input_names(); - let mut duplicate: HashSet = HashSet::new(); + let inputs: FxHashSet<&str> = level.input_names(); + let mut duplicate: FxHashSet = FxHashSet::default(); for projection in &select.expressions { let Expression::Alias(alias) = projection else { continue; @@ -287,7 +286,7 @@ fn unknown_bases<'s>( known: &dyn Fn(&Identifier) -> bool, ) -> Vec { let mut findings: Vec = Vec::new(); - let mut seen: HashSet = HashSet::new(); + let mut seen: FxHashSet = FxHashSet::default(); while let Some(name) = read.pop() { if !seen.insert(level.key(name)) { continue; @@ -322,7 +321,7 @@ fn structure_rules( let rules: &dyn DialectRules = dialect_rules(level.context.profile); let mut findings: Vec = Vec::new(); if let Some(with) = &select.with { - let mut names: HashSet = HashSet::new(); + let mut names: FxHashSet = FxHashSet::default(); for cte in &with.ctes { let mismatched = |width: usize| rules.refuses_cte_alias_count(cte.columns.len(), width); if !cte.columns.is_empty() && output_width(&cte.this).is_some_and(mismatched) { @@ -352,7 +351,7 @@ fn structure_rules( } } } - let mut names: HashSet = HashSet::new(); + let mut names: FxHashSet = FxHashSet::default(); for (item, _) in from_parts(select) .into_iter() .filter(|_| !matches!(engine, Engine::Sqlite | Engine::Databricks)) @@ -577,7 +576,7 @@ fn star_modifiers( if level.open() || !select.expressions.iter().any(modified) { return Vec::new(); } - let inputs: HashSet<&str> = level.input_names(); + let inputs: FxHashSet<&str> = level.input_names(); let mut findings: Vec = Vec::new(); for projection in &select.expressions { let Expression::Star(star) = projection else { @@ -631,7 +630,7 @@ fn query_start(node: &Expression) -> bool { fn placement( expression: &Expression, (aggregates, windows): (bool, bool), - (aliases, level): (&HashMap, RuleLevel<'_, '_>), + (aliases, level): (&FxHashMap, RuleLevel<'_, '_>), select: &Expression, ) -> Vec { let mut findings: Vec = Vec::new(); @@ -641,7 +640,7 @@ fn placement( inside_window: false, window_function: false, }]; - let mut expanded: HashSet<(String, bool, bool)> = HashSet::new(); + let mut expanded: FxHashSet<(String, bool, bool)> = FxHashSet::default(); while let Some(placed) = pending.pop() { let node: &Expression = placed.node; if query_start(node) { @@ -750,7 +749,7 @@ fn ordinary_aggregate(expression: &Expression) -> bool { /// The group keys of `select`: GROUP BY expressions, ordinals and aliases resolved, sets opened. fn group_keys<'s>( select: &'s Select, - aliases: &HashMap, + aliases: &FxHashMap, level: RuleLevel<'_, '_>, ) -> Vec<&'s Expression> { let mut groups: Vec<&Expression> = Vec::new(); @@ -1048,7 +1047,7 @@ fn offers(source: &ScopeSource, name: &str, level: RuleLevel<'_, '_>) -> bool { /// Ungrouped, unaggregated columns an aggregate query reads; none in SQLite, which reads any row. fn grouping( (node, select): (&Expression, &Select), - aliases: &HashMap, + aliases: &FxHashMap, level: RuleLevel<'_, '_>, ) -> Vec { if Engine::of(level.context) == Engine::Sqlite { @@ -1145,7 +1144,7 @@ fn grouping( for (expression, named) in read { let mut lambdas: Vec<(usize, Vec)> = vec![(0, Vec::new())]; let mut pending: Vec<(&Expression, usize)> = vec![(unalias(expression), 0)]; - let mut expanded: HashSet = HashSet::new(); + let mut expanded: FxHashSet = FxHashSet::default(); while let Some((current, frame)) = pending.pop() { if query_start(current) || is_aggregate(current) @@ -1264,7 +1263,7 @@ fn distinct_order( { return Vec::new(); } - let mut selected: HashSet = HashSet::new(); + let mut selected: FxHashSet = FxHashSet::default(); for projection in &select.expressions { if let Expression::Alias(alias) = projection { selected.insert(level.key(&alias.alias)); @@ -1341,7 +1340,7 @@ pub(crate) fn set_order_by( } /// The output names of a set operation's first branch, when every one is named. -fn output_names(query: &Expression, context: LineageContext<'_>) -> Option> { +fn output_names(query: &Expression, context: LineageContext<'_>) -> Option> { match query { Expression::Select(select) => select .expressions diff --git a/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs b/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs index a6bad5bcb..85e582c59 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs @@ -1,7 +1,6 @@ //! Walking a query level by level: each level's clauses read in its scope, inner queries after. -use std::collections::HashMap; - +use rustc_hash::FxHashMap; use sqlquack_syntax::Expression; use sqlquack_syntax::ExpressionWalk; use sqlquack_syntax::expressions::queries::models::{ @@ -143,7 +142,7 @@ fn unread_ctes(owner: &Expression, with: &With, context: LineageContext<'_>) -> .iter() .map(|cte| key(&cte.alias, context)) .collect(); - let mut indices: HashMap> = HashMap::new(); + let mut indices: FxHashMap> = FxHashMap::default(); for (index, name) in names.iter().enumerate() { indices .entry(name.to_ascii_lowercase()) From f5f1503f13bd9aeb68460721d43422814c35c312 Mon Sep 17 00:00:00 2001 From: Kevin Longe <273747162+kvlonge@users.noreply.github.com> Date: Thu, 19 Nov 2026 03:50:58 +0000 Subject: [PATCH 19/24] perf(semantics): collect a SELECT's scope once for its structure rules The structure rules walked a SELECT's own scope for named windows and then walked it again to find window functions written without OVER. They now collect the scope once and read the same nodes, in the same order, for both. --- .../src/lineage/_helpers/checks/scope_rules.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/crates/sqlquack-semantics/src/lineage/_helpers/checks/scope_rules.rs b/crates/sqlquack-semantics/src/lineage/_helpers/checks/scope_rules.rs index fa0c41f44..cf1ce5ba2 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/checks/scope_rules.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/checks/scope_rules.rs @@ -34,12 +34,11 @@ const MISPLACED_WINDOW: &str = /// A finding for each window function without OVER that the all-engine placement rule misses (42809). fn unwindowed_calls( - node: &Expression, + (node, scoped): (&Expression, &[&Expression]), window_only: &dyn Fn(&Expression) -> bool, ) -> Vec { - let scoped: Vec<&Expression> = in_scope(node); let mut wrapped: Vec<&Expression> = Vec::new(); - for expression in &scoped { + for expression in scoped { match expression { Expression::WindowFunction(window) => wrapped.push(&window.this), Expression::WithinGroup(group) => wrapped.push(&group.this), @@ -47,7 +46,7 @@ fn unwindowed_calls( } } let mut findings: Vec = Vec::new(); - for expression in scoped { + for &expression in scoped { let named: bool = matches!( expression, Expression::Function(_) | Expression::AggregateFunction(_) @@ -383,7 +382,8 @@ fn structure_rules( }; let mut has_window: bool = false; let mut read: Vec<&Identifier> = Vec::new(); - for expression in in_scope(node) { + let scoped: Vec<&Expression> = in_scope(node); + for &expression in &scoped { if let Expression::WindowFunction(window) = expression { has_window = true; match &window.over.window_name { @@ -402,9 +402,13 @@ fn structure_rules( match engine { Engine::Postgres | Engine::Sqlite | Engine::Snowflake | Engine::DuckDB => { let rules = dialect_rules(level.context.profile); - findings.extend(unwindowed_calls(node, &|call| rules.window_only(call))); + findings.extend(unwindowed_calls((node, &scoped), &|call| { + rules.window_only(call) + })); + } + Engine::BigQuery => { + findings.extend(unwindowed_calls((node, &scoped), &bigquery_window_only)); } - Engine::BigQuery => findings.extend(unwindowed_calls(node, &bigquery_window_only)), Engine::Databricks => {} } if select.qualify.is_some() && !has_window { From b4d5106260943ac02896d0e80a66b866024026c0 Mon Sep 17 00:00:00 2001 From: Kevin Longe <273747162+kvlonge@users.noreply.github.com> Date: Thu, 19 Nov 2026 03:55:08 +0000 Subject: [PATCH 20/24] perf(semantics): look an aggregate's name up in a set Whether a named call is an aggregate was answered by comparing its name with each of the 63 aggregate names in turn, ignoring ASCII case. Every listed name is upper-case ASCII, so the name is now upper-cased on the stack and looked up in a set of them, which gives the same answer for every spelling. A name longer than the longest listed one is refused without a lookup. --- .../lineage/_helpers/tracing/expressions.rs | 38 +++++++++++++++++-- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/crates/sqlquack-semantics/src/lineage/_helpers/tracing/expressions.rs b/crates/sqlquack-semantics/src/lineage/_helpers/tracing/expressions.rs index 44d2824e9..ba4ff6970 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/tracing/expressions.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/tracing/expressions.rs @@ -2,8 +2,9 @@ use std::cell::RefCell; use std::rc::Rc; +use std::sync::LazyLock; -use rustc_hash::FxHashMap; +use rustc_hash::{FxHashMap, FxHashSet}; use sqlquack_syntax::ExpressionWalk; use sqlquack_syntax::expressions::names::models::{Identifier, Star}; @@ -32,6 +33,13 @@ use crate::lineage::models::{ use crate::lineage::types::DialectRules; use crate::lineage::types::ValueType; +/// The longest name in `AGGREGATE_FUNCTIONS`. +const AGGREGATE_NAME_CAPACITY: usize = longest_name(AGGREGATE_FUNCTIONS); + +/// `AGGREGATE_FUNCTIONS` as a set, every name in upper case. +static AGGREGATE_NAMES: LazyLock> = + LazyLock::new(|| AGGREGATE_FUNCTIONS.iter().copied().collect()); + /// The lineage of `expression` read in `scope`. pub(crate) fn expression_lineage( expression: &Expression, @@ -422,13 +430,35 @@ pub(crate) fn is_aggregate(expression: &Expression) -> bool { | Expression::BitwiseXorAgg(_) | Expression::ArgMax(_) | Expression::ArgMin(_) => true, - Expression::Function(function) => AGGREGATE_FUNCTIONS - .iter() - .any(|name| function.name.eq_ignore_ascii_case(name)), + Expression::Function(function) => aggregate_name(&function.name), _ => false, } } +/// The length of the longest of `names`. +const fn longest_name(names: &[&str]) -> usize { + let mut longest: usize = 0; + let mut index: usize = 0; + while index < names.len() { + if names[index].len() > longest { + longest = names[index].len(); + } + index += 1; + } + longest +} + +/// Whether `name` is an aggregate function's in any ASCII case, upper-cased on the stack. +fn aggregate_name(name: &str) -> bool { + let mut upper: [u8; AGGREGATE_NAME_CAPACITY] = [0; AGGREGATE_NAME_CAPACITY]; + let Some(written) = upper.get_mut(..name.len()) else { + return false; + }; + written.copy_from_slice(name.as_bytes()); + written.make_ascii_uppercase(); + AGGREGATE_NAMES.contains(String::from_utf8_lossy(written).as_ref()) +} + /// A star read as a value, as in `OBJECT_CONSTRUCT(*)`: everything its columns read. fn star_lineage(star: &Star, scope: &LineageScope<'_>, context: LineageContext<'_>) -> Lineage { if star.table.is_none() && scope.sources.is_empty() { From 0ba833cde7bffc9aff59d7668137a74bda35c6bd Mon Sep 17 00:00:00 2001 From: Kevin Longe <273747162+kvlonge@users.noreply.github.com> Date: Thu, 19 Nov 2026 04:06:28 +0000 Subject: [PATCH 21/24] perf(semantics): append a node's findings instead of extending with them Each node's checks return a vector that is usually empty, and node_findings added each one to the node's findings with extend, which builds an iterator for every vector, empty or not. It now appends them, which moves the same findings in the same order without the iterator. --- .../src/lineage/_helpers/checks/node_checks.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/crates/sqlquack-semantics/src/lineage/_helpers/checks/node_checks.rs b/crates/sqlquack-semantics/src/lineage/_helpers/checks/node_checks.rs index 496e4afb5..ab3062f07 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/checks/node_checks.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/checks/node_checks.rs @@ -97,7 +97,7 @@ pub(crate) fn node_findings(node: &Expression, level: NodeLevel<'_, '_>) -> Vec< }; let signatures_abstain: bool = !dialect_rules(level.context.profile).checks_call_signatures(); if signatures_abstain { - findings.extend(located(own, node)); + findings.append(&mut located(own, node)); return findings; } let checked = check_call(node, level.engine(), &family); @@ -113,18 +113,18 @@ pub(crate) fn node_findings(node: &Expression, level: NodeLevel<'_, '_>) -> Vec< } call.extend(refused); } - own.extend(call); - own.extend(expression_checks(node, level)); + own.append(&mut call); + own.append(&mut expression_checks(node, level)); if !(signed && !matches!(node, Expression::Function(_))) { - own.extend(operator_checks(node, level, signed)); + own.append(&mut operator_checks(node, level, signed)); } - findings.extend(located(own, node)); + findings.append(&mut located(own, node)); if let Expression::Function(function) = node { - let keyword_only: Vec = keyword_only_call(function, level.engine(), node); + let mut keyword_only: Vec = keyword_only_call(function, level.engine(), node); if keyword_only.is_empty() { - findings.extend(unknown_duckdb_call(function, level, node)); + findings.append(&mut unknown_duckdb_call(function, level, node)); } - findings.extend(keyword_only); + findings.append(&mut keyword_only); } findings } From 747ec278fab1fe2694b886ede1c7eb4e57a0c224 Mon Sep 17 00:00:00 2001 From: Kevin Longe <273747162+kvlonge@users.noreply.github.com> Date: Thu, 19 Nov 2026 04:11:26 +0000 Subject: [PATCH 22/24] perf(semantics): append a level's findings instead of extending with them select_findings gathered each node's, clause's and rule's findings with extend, building an iterator for every vector it received, most of them empty. It now appends them, which moves the same findings in the same order. --- .../src/lineage/_helpers/findings/levels.rs | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs b/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs index 85e582c59..ed2a3f74e 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs @@ -318,7 +318,7 @@ fn select_findings( context, }; if typed_rules(context) { - nested.extend(select_rules( + nested.append(&mut select_rules( (node, select), RuleLevel { scope: &scope, @@ -335,11 +335,11 @@ fn select_findings( .into_iter() .chain(after_keys.flat_map(in_scope)) { - nested.extend(node_findings(checked, level)); + nested.append(&mut node_findings(checked, level)); } } let mut own: Vec = alias_count_findings(select, &scope, context); - own.extend(using_findings(select, &scope, context)); + own.append(&mut using_findings(select, &scope, context)); let postgres: bool = Engine::of(context) == Engine::Postgres; let rules: &dyn DialectRules = dialect_rules(context.profile); let anywhere: bool = rules.outputs_read_anywhere(); @@ -409,9 +409,9 @@ fn select_findings( }, None => unnamed, }; - let (found, inner) = clause_findings(projection, &projected, context); - own.extend(found); - nested.extend(inner); + let (mut found, mut inner) = clause_findings(projection, &projected, context); + own.append(&mut found); + nested.append(&mut inner); } let mut read: Vec<&Expression> = clauses(select); read.extend(orders.iter().flat_map(|order| order_keys(order))); @@ -437,9 +437,9 @@ fn select_findings( }, None => read, }; - let (found, inner) = clause_findings(clause, &read, context); - own.extend(found); - nested.extend(inner); + let (mut found, mut inner) = clause_findings(clause, &read, context); + own.append(&mut found); + nested.append(&mut inner); } let windows: Vec<&NamedWindow> = std::iter::once(&select.windows) .chain(after.iter().map(|node| &node.windows)) @@ -449,11 +449,12 @@ fn select_findings( let reads: Vec<&Expression> = std::iter::once(node) .chain(orders.iter().flat_map(|order| order_keys(order))) .collect(); - let (found, inner) = window_findings(select, (windows, &reads), (level, anywhere), context); - own.extend(found); - nested.extend(inner); + let (mut found, mut inner) = + window_findings(select, (windows, &reads), (level, anywhere), context); + own.append(&mut found); + nested.append(&mut inner); own.sort_by_key(|finding| finding.span.map(|span| span.start)); - own.extend(nested); + own.append(&mut nested); own } From 150a37d445f3595331d38c01d31b9c98e4ec0dd7 Mon Sep 17 00:00:00 2001 From: Kevin Longe <273747162+kvlonge@users.noreply.github.com> Date: Thu, 19 Nov 2026 05:37:28 +0000 Subject: [PATCH 23/24] docs: complete the 0.22.0 lint breaking-change entry The entry stopped mid-sentence after "600". It now names the 600 `::` casts that no longer lint in those dialects, matching the release notes. --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6801b24d0..1ec6e9e4f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ ### ⚠ BREAKING CHANGES -* **lint:** lint now refuses a Postgres, BigQuery or Databricks statement whose tree nests past 512 levels, as Snowflake, DuckDB and SQLite already did, so 600 UNION ALL operands or 600 +* **lint:** lint now refuses a Postgres, BigQuery or Databricks statement whose tree nests past 512 levels, as Snowflake, DuckDB and SQLite already did, so 600 UNION ALL operands or 600 `::` casts no longer lint in those dialects. * keep each comma item's joins with it in Postgres, Databricks, DuckDB and SQL Server * **bigquery:** sqlquack-syntax adds Literal::RawByteString and Literal::JoinedBytes, and GrantPrincipal gains a written field, so an exhaustive match on Literal or a GrantPrincipal struct literal no longer compiles. From 56952db1bf7e40f68b97426586c541c0e1b70284 Mon Sep 17 00:00:00 2001 From: Kevin Longe <273747162+kvlonge@users.noreply.github.com> Date: Thu, 19 Nov 2026 05:42:08 +0000 Subject: [PATCH 24/24] perf(semantics): record the lower analysis instruction count The analysis workload now runs 182,168,067 instructions against the recorded 239,698,431 (24.0% fewer); parse is unchanged within noise. --- coverage/instruction-counts.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/coverage/instruction-counts.toml b/coverage/instruction-counts.toml index f16559f40..bead8e03d 100644 --- a/coverage/instruction-counts.toml +++ b/coverage/instruction-counts.toml @@ -15,5 +15,5 @@ rustc = "rustc 1.99.1 (58cf8f9c3 2026-10-13)" tolerance_percent = 2 [counts] -analysis = 239698431 -parse = 253081114 +analysis = 182168067 +parse = 252986430