Skip to content

Commit b39bd91

Browse files
authored
fix: distinguish Snowflake START WITH from CTEs (#738)
Co-authored-by: Kevin Longe <273747162+kvlonge@users.noreply.github.com>
1 parent c74b24a commit b39bd91

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

‎crates/sqlbuild-rules-native/src/sql_lint/_helpers/engine.rs‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -515,12 +515,16 @@ fn collect_rule_migration_facts(
515515
facts.cross_joins.push(tokens[index].span);
516516
}
517517
if text.eq_ignore_ascii_case("with") {
518-
if depths[index] > 0 {
518+
let starts_hierarchy = position.checked_sub(1).is_some_and(|previous| {
519+
tokens[significant[previous]].token_type == TokenType::Start
520+
});
521+
if depths[index] > 0 && !starts_hierarchy {
519522
facts.nested_ctes.push(tokens[index].span);
520523
}
521-
if significant
522-
.get(position + 1)
523-
.is_some_and(|&next| tokens[next].text.eq_ignore_ascii_case("recursive"))
524+
if !starts_hierarchy
525+
&& significant
526+
.get(position + 1)
527+
.is_some_and(|&next| tokens[next].text.eq_ignore_ascii_case("recursive"))
524528
{
525529
facts.recursive_ctes.push(tokens[index].span);
526530
}

‎crates/sqlbuild-rules-native/src/sql_lint/tests/test_sql_lint.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,12 @@ fn given_plain_sql_rules_when_linting_then_project_context_is_not_required() ->
443443
rule: "SQBRSQL036",
444444
expected_count: 1,
445445
},
446+
test_types::PlainSqlLintTestCase {
447+
description: "Snowflake hierarchy START WITH is not a nested CTE",
448+
sql: "WITH entities AS (SELECT id, parent_id FROM source_rows), hierarchy AS (SELECT id FROM entities START WITH parent_id IS NULL CONNECT BY parent_id = PRIOR id) SELECT id FROM hierarchy",
449+
rule: "SQBRSQL036",
450+
expected_count: 0,
451+
},
446452
test_types::PlainSqlLintTestCase {
447453
description: "recursive CTE",
448454
sql: "WITH RECURSIVE numbers AS (SELECT 1 UNION ALL SELECT 2) SELECT * FROM numbers",

0 commit comments

Comments
 (0)