From a867c3960c5023bfcbb7a11b5ecc8076dfe1588d Mon Sep 17 00:00:00 2001 From: Kevin Longe <273747162+kvlonge@users.noreply.github.com> Date: Thu, 12 Nov 2026 23:09:35 +0000 Subject: [PATCH] fix(lint): read a FIRST or LAST join alias as the relation's alias Snowflake reads a bare FIRST or LAST after a joined table as its alias, which the parser accepts since 0.11.0. The lint's token path only took identifier tokens as aliases and qualifiers, so for JOIN t first ON ..., LEFT JOIN t last USING (...) and CROSS JOIN t first it took the table name as the alias and reported SQLCLEAN032 although first.x is selected, and it reported SQLCLEAN022 for first.x as an unnamed calculation. --- .../src/lint/_helpers/additional.rs | 11 ++- crates/sqlquack-lint/src/lint/tests.rs | 2 + .../src/lint/tests/test_keyword_aliases.rs | 74 +++++++++++++++++++ .../src/lint/tests/test_types.rs | 7 ++ 4 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 crates/sqlquack-lint/src/lint/tests/test_keyword_aliases.rs diff --git a/crates/sqlquack-lint/src/lint/_helpers/additional.rs b/crates/sqlquack-lint/src/lint/_helpers/additional.rs index 563fb7141..2fc0e079e 100644 --- a/crates/sqlquack-lint/src/lint/_helpers/additional.rs +++ b/crates/sqlquack-lint/src/lint/_helpers/additional.rs @@ -1260,7 +1260,7 @@ fn simple_projection(tokens: &[Token], item: &[usize]) -> bool { let wildcard = trimmed.first().is_some_and(|&index| { tokens[index].token_type == TokenType::Star || (trimmed.len() >= QUALIFIED_REFERENCE_LENGTH - && is_identifier(&tokens[index]) + && is_relation_name(&tokens[index]) && tokens[trimmed[1]].token_type == TokenType::Dot && tokens[trimmed[2]].token_type == TokenType::Star) }); @@ -1269,7 +1269,7 @@ fn simple_projection(tokens: &[Token], item: &[usize]) -> bool { && (is_bare_column_token(&tokens[trimmed[0]]) || tokens[trimmed[0]].token_type == TokenType::Star) || trimmed.len() == QUALIFIED_REFERENCE_LENGTH - && is_identifier(&tokens[trimmed[0]]) + && is_relation_name(&tokens[trimmed[0]]) && tokens[trimmed[1]].token_type == TokenType::Dot && (is_bare_column_token(&tokens[trimmed[2]]) || tokens[trimmed[2]].token_type == TokenType::Star) @@ -1497,7 +1497,7 @@ fn relation_alias_after_join( relation[..relation_end] .iter() .rev() - .find(|&&index| is_identifier(&tokens[index])) + .find(|&&index| is_relation_name(&tokens[index])) .map(|&index| tokens[index].text.to_ascii_lowercase()) } @@ -1884,3 +1884,8 @@ fn is_identifier(token: &Token) -> bool { TokenType::Identifier | TokenType::QuotedIdentifier | TokenType::Var ) } + +/// Whether the token can name a relation as its alias or qualifier: an identifier, or FIRST or LAST, which Snowflake reads as a join alias. +fn is_relation_name(token: &Token) -> bool { + is_identifier(token) || matches!(token.token_type, TokenType::First | TokenType::Last) +} diff --git a/crates/sqlquack-lint/src/lint/tests.rs b/crates/sqlquack-lint/src/lint/tests.rs index 8befcc58b..58e463baf 100644 --- a/crates/sqlquack-lint/src/lint/tests.rs +++ b/crates/sqlquack-lint/src/lint/tests.rs @@ -8,6 +8,8 @@ mod empty_fixture_lint; mod helpers; #[path = "tests/test_join_predicates.rs"] mod join_predicates; +#[path = "tests/test_keyword_aliases.rs"] +mod keyword_aliases; #[path = "tests/test_lint_exemptions.rs"] mod lint_exemptions; #[path = "tests/test_long_chains.rs"] diff --git a/crates/sqlquack-lint/src/lint/tests/test_keyword_aliases.rs b/crates/sqlquack-lint/src/lint/tests/test_keyword_aliases.rs new file mode 100644 index 000000000..2e4adbc86 --- /dev/null +++ b/crates/sqlquack-lint/src/lint/tests/test_keyword_aliases.rs @@ -0,0 +1,74 @@ +use crate::lint::tests::helpers::diagnostics_for_rules; +use crate::lint::tests::test_types::KeywordAliasTestCase; + +#[test] +fn given_a_first_or_last_join_alias_when_linting_then_it_qualifies_like_an_ordinary_alias() +-> Result<(), String> { + let test_cases = [ + KeywordAliasTestCase { + description: "joined relation aliased FIRST contributes a selected value", + sql: "SELECT o.brand_code, first.brand_name AS b FROM stg_brands AS o INNER JOIN stg_brands first ON first.brand_code = o.brand_code", + rule: "SQLCLEAN032", + expected_anchor: None, + }, + KeywordAliasTestCase { + description: "joined relation aliased LAST contributes a selected value", + sql: "SELECT o.brand_code, last.country_code AS c FROM stg_brands AS o LEFT JOIN stg_brands last USING (brand_code)", + rule: "SQLCLEAN032", + expected_anchor: None, + }, + KeywordAliasTestCase { + description: "cross-joined relation aliased FIRST contributes a selected value", + sql: "SELECT o.order_id, first.customer_id FROM orders AS o CROSS JOIN customers first", + rule: "SQLCLEAN032", + expected_anchor: None, + }, + KeywordAliasTestCase { + description: "column qualified by a FIRST alias has a stable output name", + sql: "SELECT o.order_id, first.customer_id FROM orders AS o CROSS JOIN customers first", + rule: "SQLCLEAN022", + expected_anchor: None, + }, + KeywordAliasTestCase { + description: "star qualified by a FIRST alias with EXCLUDE has a stable output shape", + sql: "SELECT first.* EXCLUDE (customer_id) FROM orders AS o CROSS JOIN customers first", + rule: "SQLCLEAN022", + expected_anchor: None, + }, + KeywordAliasTestCase { + description: "ordinary joined alias contributes a selected value", + sql: "SELECT o.order_id, f.customer_id FROM orders AS o CROSS JOIN customers f", + rule: "SQLCLEAN032", + expected_anchor: None, + }, + KeywordAliasTestCase { + description: "joined relation aliased LAST that contributes nothing", + sql: "SELECT o.order_id FROM orders AS o LEFT JOIN customers last ON last.customer_id = o.customer_id", + rule: "SQLCLEAN032", + expected_anchor: Some("JOIN"), + }, + ]; + for test_case in &test_cases { + let diagnostics = diagnostics_for_rules(test_case.sql, &[test_case.rule])?; + let anchors: Vec = diagnostics + .iter() + .map(|diagnostic| { + let start = diagnostic["start"].as_u64().unwrap_or_default() as usize; + let end = diagnostic["end"].as_u64().unwrap_or_default() as usize; + test_case + .sql + .chars() + .skip(start) + .take(end - start) + .collect() + }) + .collect(); + assert_eq!( + anchors, + test_case.expected_anchor.into_iter().collect::>(), + "{}", + test_case.description + ); + } + Ok(()) +} diff --git a/crates/sqlquack-lint/src/lint/tests/test_types.rs b/crates/sqlquack-lint/src/lint/tests/test_types.rs index fb1adc9c0..8950da451 100644 --- a/crates/sqlquack-lint/src/lint/tests/test_types.rs +++ b/crates/sqlquack-lint/src/lint/tests/test_types.rs @@ -171,3 +171,10 @@ pub(crate) struct UnusedCteTestCase { pub sql: &'static str, pub expected_unused: &'static [&'static str], } + +pub(crate) struct KeywordAliasTestCase { + pub description: &'static str, + pub sql: &'static str, + pub rule: &'static str, + pub expected_anchor: Option<&'static str>, +}