From 5911e701b8e1544c6c3787089c7b5e56c4e6bee2 Mon Sep 17 00:00:00 2001 From: Kevin Longe <273747162+kvlonge@users.noreply.github.com> Date: Wed, 18 Nov 2026 20:39:32 +0000 Subject: [PATCH 1/2] fix(lint): resolve a named window written after a parenthesized query Databricks keeps a WINDOW clause written after a parenthesized query on the subquery node, but the ranking rules looked a window name up only in the SELECT's own WINDOW clause. SQLCLEAN018 and SQLCLEAN043 were therefore silent for (SELECT ..., ROW_NUMBER() OVER w AS rn FROM orders) WINDOW w AS (ORDER BY ...), with or without a WITH before the parentheses. An OVER name now resolves against the SELECT's own windows, then the enclosing WINDOW clauses, nearest first: those after parentheses and those of outer SELECTs whose FROM holds the query, so a nearer definition of the same name wins, as in Spark. Databricks cases cover both rules, the WITH form, a proven ordering and a name defined at two levels. The key inference for ROW_NUMBER() = 1 dedups still reads only an inline OVER, as before. --- .../src/quality/_helpers/ranking.rs | 46 ++++++++++++++++--- .../src/quality/tests/test_quality_rules.rs | 40 ++++++++++++++++ 2 files changed, 80 insertions(+), 6 deletions(-) diff --git a/crates/sqlquack-lint/src/quality/_helpers/ranking.rs b/crates/sqlquack-lint/src/quality/_helpers/ranking.rs index 964eb1d72..bfe2bf7d6 100644 --- a/crates/sqlquack-lint/src/quality/_helpers/ranking.rs +++ b/crates/sqlquack-lint/src/quality/_helpers/ranking.rs @@ -6,8 +6,8 @@ use std::collections::{HashMap, HashSet}; use crate::tree::main::call_name::call_name; use crate::tree::main::walk_in_scope::walk_in_scope; use sqlquack_syntax::expressions::functions::models::WindowFunction; -use sqlquack_syntax::expressions::queries::models::Over; use sqlquack_syntax::expressions::queries::models::Select; +use sqlquack_syntax::expressions::queries::models::{NamedWindow, Over}; use sqlquack_syntax::tokens::models::Token; use sqlquack_syntax::{Expression, ExpressionWalk}; @@ -31,6 +31,8 @@ struct RankingWindow<'a> { function: &'static str, window: &'a WindowFunction, select: &'a Select, + /// The WINDOW clauses its OVER can name: the SELECT's own, then those after enclosing parentheses. + windows: Vec<&'a NamedWindow>, } pub(crate) fn diagnostics( @@ -55,7 +57,7 @@ pub(crate) fn diagnostics( } let mut found: Vec = Vec::new(); for ranking in ranking_windows(request.statements) { - let over: Option<&Over> = effective_over(&ranking.window.over, ranking.select); + let over: Option<&Over> = effective_over(&ranking.window.over, &ranking.windows); let order: Vec<&Expression> = over.map_or_else(Vec::new, |over| { over.order_by.iter().map(|ordered| &ordered.this).collect() }); @@ -140,10 +142,15 @@ fn proven( fn ranking_windows(statements: &[Expression]) -> Vec> { let mut found: Vec> = Vec::new(); for statement in statements { + let outer: HashMap<*const Select, Vec<&NamedWindow>> = enclosing_windows(statement); for node in statement.dfs() { let Expression::Select(select) = node else { continue; }; + let mut windows: Vec<&NamedWindow> = select.windows.iter().flatten().collect(); + if let Some(enclosing) = outer.get(&std::ptr::from_ref(select.as_ref())) { + windows.extend(enclosing.iter().rev()); + } for local in walk_in_scope(node) { let Expression::WindowFunction(window) = local else { continue; @@ -167,6 +174,7 @@ fn ranking_windows(statements: &[Expression]) -> Vec> { function, window, select, + windows: windows.clone(), }); } } @@ -174,7 +182,35 @@ fn ranking_windows(statements: &[Expression]) -> Vec> { found } -fn effective_over<'a>(over: &'a Over, select: &'a Select) -> Option<&'a Over> { +/// Each SELECT's enclosing WINDOW clauses, from parentheses and outer SELECTs, outermost first. +fn enclosing_windows(statement: &Expression) -> HashMap<*const Select, Vec<&NamedWindow>> { + let mut found: HashMap<*const Select, Vec<&NamedWindow>> = HashMap::new(); + for node in statement.dfs() { + let (windows, inside): (&[NamedWindow], Box>) = match node + { + Expression::Subquery(subquery) => match &subquery.windows { + Some(windows) => (windows, Box::new(subquery.this.dfs())), + None => continue, + }, + Expression::Select(select) => match &select.windows { + Some(windows) => (windows, Box::new(node.dfs().skip(1))), + None => continue, + }, + _ => continue, + }; + for inner in inside { + if let Expression::Select(select) = inner { + found + .entry(std::ptr::from_ref(select.as_ref())) + .or_default() + .extend(windows); + } + } + } + found +} + +fn effective_over<'a>(over: &'a Over, windows: &[&'a NamedWindow]) -> Option<&'a Over> { let mut current: &Over = over; let mut seen: HashSet = HashSet::new(); loop { @@ -185,10 +221,8 @@ fn effective_over<'a>(over: &'a Over, select: &'a Select) -> Option<&'a Over> { if !seen.insert(name.clone()) { return None; } - current = &select - .windows + current = &windows .iter() - .flatten() .find(|window| lower(&window.name.name) == name)? .spec; } diff --git a/crates/sqlquack-lint/src/quality/tests/test_quality_rules.rs b/crates/sqlquack-lint/src/quality/tests/test_quality_rules.rs index 57eb6d04a..7cd782ed9 100644 --- a/crates/sqlquack-lint/src/quality/tests/test_quality_rules.rs +++ b/crates/sqlquack-lint/src/quality/tests/test_quality_rules.rs @@ -567,6 +567,46 @@ fn given_a_query_or_cte_body_in_parentheses_when_linting_then_its_ctes_are_check expected_anchors: &[], expected_fixed_sql: None, }, + QualityRuleTestCase { + description: "a ranking sort above the cap in a WINDOW after the parentheses", + sql: "(SELECT o.order_id, ROW_NUMBER() OVER w AS rn FROM orders AS o) WINDOW w AS (ORDER BY o.a, o.b, o.c)", + rule: "SQLCLEAN043", + relation_keys: r#"{}"#, + expected_anchors: &["ROW_NUMBER"], + expected_fixed_sql: None, + }, + QualityRuleTestCase { + description: "a ranking sort above the cap in a WINDOW after parentheses with a WITH before them", + sql: "WITH c AS (SELECT 1 AS x) (SELECT o.order_id, ROW_NUMBER() OVER w AS rn FROM orders AS o) WINDOW w AS (ORDER BY o.a, o.b, o.c)", + rule: "SQLCLEAN043", + relation_keys: r#"{}"#, + expected_anchors: &["ROW_NUMBER"], + expected_fixed_sql: None, + }, + QualityRuleTestCase { + description: "an ordering without a key in a WINDOW after the parentheses is unproven", + sql: "(SELECT o.order_id, ROW_NUMBER() OVER w AS rn FROM orders AS o) WINDOW w AS (ORDER BY o.ordered_at)", + rule: "SQLCLEAN018", + relation_keys: r#"{"orders": [["order_id"]]}"#, + expected_anchors: &["ROW_NUMBER"], + expected_fixed_sql: None, + }, + QualityRuleTestCase { + description: "a nested SELECT's window resolves to the nearer WINDOW, not the one after the parentheses", + sql: "(SELECT s.rn FROM (SELECT o.order_id, ROW_NUMBER() OVER w AS rn FROM orders AS o) AS s WINDOW w AS (ORDER BY o.a, o.b, o.c)) WINDOW w AS (ORDER BY o.order_id)", + rule: "SQLCLEAN043", + relation_keys: r#"{}"#, + expected_anchors: &["ROW_NUMBER"], + expected_fixed_sql: None, + }, + QualityRuleTestCase { + description: "an ordering by the key in a WINDOW after the parentheses is proven", + sql: "(SELECT o.order_id, ROW_NUMBER() OVER w AS rn FROM orders AS o) WINDOW w AS (ORDER BY o.order_id)", + rule: "SQLCLEAN018", + relation_keys: r#"{"orders": [["order_id"]]}"#, + expected_anchors: &[], + expected_fixed_sql: None, + }, ]; for test_case in &test_cases { let diagnostics: Vec = From 815fd91228cf51c43421b29173801959032066fa Mon Sep 17 00:00:00 2001 From: Kevin Longe <273747162+kvlonge@users.noreply.github.com> Date: Wed, 18 Nov 2026 20:39:32 +0000 Subject: [PATCH 2/2] feat(lint)!: take the function-call depth limit in parse_sql parse_sql always parsed within the default limit of 256 nested calls, so SQL that a lint request with a raised max_function_call_depth accepted was refused by callers that parse through parse_sql, with a hint to raise a setting already raised. parse_sql(sql, dialect, max_function_call_depth) now takes the limit: None keeps the default, and a value above 512 is refused as a lint request refuses it. This is breaking: every caller passes the new argument. --- .../sqlquack-lint/src/lint/main/parse_sql.rs | 10 ++++-- .../src/lint/tests/test_function_depth.rs | 36 +++++++++++++++++++ .../src/lint/tests/test_types.rs | 9 +++++ 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/crates/sqlquack-lint/src/lint/main/parse_sql.rs b/crates/sqlquack-lint/src/lint/main/parse_sql.rs index 2eed4aec2..3d60d09f1 100644 --- a/crates/sqlquack-lint/src/lint/main/parse_sql.rs +++ b/crates/sqlquack-lint/src/lint/main/parse_sql.rs @@ -2,10 +2,14 @@ use crate::lint::models::ParsedSql; -/// Parse `sql` under `dialect`, rejecting statements the parser kept as opaque text. -pub fn parse_sql(sql: &str, dialect: &str) -> Result { +/// Parse `sql` under `dialect` within a call-depth limit (None: the default), refusing opaque text. +pub fn parse_sql( + sql: &str, + dialect: &str, + max_function_call_depth: Option, +) -> Result { let Some(parsed) = crate::dialects::main::lint_dialect::lint_dialect(dialect)? else { return Err(format!("SQLQuack does not parse the {dialect} dialect yet")); }; - crate::lint::_helpers::parsing::parse(sql, parsed, (None, None)) + crate::lint::_helpers::parsing::parse(sql, parsed, (None, max_function_call_depth)) } diff --git a/crates/sqlquack-lint/src/lint/tests/test_function_depth.rs b/crates/sqlquack-lint/src/lint/tests/test_function_depth.rs index 66741b4ad..f569a1e57 100644 --- a/crates/sqlquack-lint/src/lint/tests/test_function_depth.rs +++ b/crates/sqlquack-lint/src/lint/tests/test_function_depth.rs @@ -80,3 +80,39 @@ fn given_excessive_function_depth_when_linting_then_complexity_guard_rejects_it( ); } } + +#[test] +fn given_a_call_depth_limit_when_parsing_sql_then_the_limit_applies() { + let test_cases = [ + test_types::ParseSqlDepthTestCase { + description: "a raised limit of 400 parses 300 nested calls", + depth: 300, + max_function_call_depth: Some(400), + expected_outcome_end: "parsed", + }, + test_types::ParseSqlDepthTestCase { + description: "the default limit refuses 300 nested calls", + depth: 300, + max_function_call_depth: None, + expected_outcome_end: "value 257 exceeds configured limit 256; raise \ + max_function_call_depth, up to 512", + }, + test_types::ParseSqlDepthTestCase { + description: "a limit past the ceiling is refused", + depth: 300, + max_function_call_depth: Some(1000), + expected_outcome_end: "the function-call depth limit must be between 1 and 512, \ + SQLQuack's fixed nesting ceiling, not 1000", + }, + ]; + for test_case in test_cases { + let sql = helpers::nested_function_sql(test_case.depth); + let outcome: String = crate::parse_sql(&sql, "duckdb", test_case.max_function_call_depth) + .map_or_else(|error| error, |_| "parsed".to_owned()); + assert!( + outcome.ends_with(test_case.expected_outcome_end), + "{}: {outcome}", + test_case.description + ); + } +} diff --git a/crates/sqlquack-lint/src/lint/tests/test_types.rs b/crates/sqlquack-lint/src/lint/tests/test_types.rs index fd32f084c..067ed71bc 100644 --- a/crates/sqlquack-lint/src/lint/tests/test_types.rs +++ b/crates/sqlquack-lint/src/lint/tests/test_types.rs @@ -112,6 +112,15 @@ pub(crate) struct FunctionDepthFailureTestCase { pub expected_error_end: &'static str, } +pub(crate) struct ParseSqlDepthTestCase { + pub description: &'static str, + pub depth: usize, + /// The `max_function_call_depth` argument; None for the default. + pub max_function_call_depth: Option, + /// How the error ends, or `parsed` when the SQL parses. + pub expected_outcome_end: &'static str, +} + pub(crate) struct NestingRefusalTestCase { pub description: &'static str, pub dialect: &'static str,