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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions crates/sqlquack-lint/src/lint/_helpers/additional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
});
Expand All @@ -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)
Expand Down Expand Up @@ -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())
}

Expand Down Expand Up @@ -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)
}
2 changes: 2 additions & 0 deletions crates/sqlquack-lint/src/lint/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
74 changes: 74 additions & 0 deletions crates/sqlquack-lint/src/lint/tests/test_keyword_aliases.rs
Original file line number Diff line number Diff line change
@@ -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<String> = 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::<Vec<_>>(),
"{}",
test_case.description
);
}
Ok(())
}
7 changes: 7 additions & 0 deletions crates/sqlquack-lint/src/lint/tests/test_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
}
Loading