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
12 changes: 8 additions & 4 deletions crates/sqlbuild-rules-native/src/sql_lint/_helpers/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,12 +515,16 @@ fn collect_rule_migration_facts(
facts.cross_joins.push(tokens[index].span);
}
if text.eq_ignore_ascii_case("with") {
if depths[index] > 0 {
let starts_hierarchy = position.checked_sub(1).is_some_and(|previous| {
tokens[significant[previous]].token_type == TokenType::Start
});
if depths[index] > 0 && !starts_hierarchy {
facts.nested_ctes.push(tokens[index].span);
}
if significant
.get(position + 1)
.is_some_and(|&next| tokens[next].text.eq_ignore_ascii_case("recursive"))
if !starts_hierarchy
&& significant
.get(position + 1)
.is_some_and(|&next| tokens[next].text.eq_ignore_ascii_case("recursive"))
{
facts.recursive_ctes.push(tokens[index].span);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,12 @@ fn given_plain_sql_rules_when_linting_then_project_context_is_not_required() ->
rule: "SQBRSQL036",
expected_count: 1,
},
test_types::PlainSqlLintTestCase {
description: "Snowflake hierarchy START WITH is not a nested CTE",
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",
rule: "SQBRSQL036",
expected_count: 0,
},
test_types::PlainSqlLintTestCase {
description: "recursive CTE",
sql: "WITH RECURSIVE numbers AS (SELECT 1 UNION ALL SELECT 2) SELECT * FROM numbers",
Expand Down
Loading