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
10 changes: 7 additions & 3 deletions crates/sqlquack-lint/src/lint/main/parse_sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ParsedSql, String> {
/// 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<usize>,
) -> Result<ParsedSql, String> {
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))
}
36 changes: 36 additions & 0 deletions crates/sqlquack-lint/src/lint/tests/test_function_depth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}
9 changes: 9 additions & 0 deletions crates/sqlquack-lint/src/lint/tests/test_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize>,
/// 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,
Expand Down
46 changes: 40 additions & 6 deletions crates/sqlquack-lint/src/quality/_helpers/ranking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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(
Expand All @@ -55,7 +57,7 @@ pub(crate) fn diagnostics(
}
let mut found: Vec<LintDiagnostic> = 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()
});
Expand Down Expand Up @@ -140,10 +142,15 @@ fn proven(
fn ranking_windows(statements: &[Expression]) -> Vec<RankingWindow<'_>> {
let mut found: Vec<RankingWindow<'_>> = 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;
Expand All @@ -167,14 +174,43 @@ fn ranking_windows(statements: &[Expression]) -> Vec<RankingWindow<'_>> {
function,
window,
select,
windows: windows.clone(),
});
}
}
}
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<dyn Iterator<Item = &Expression>>) = 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<String> = HashSet::new();
loop {
Expand All @@ -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;
}
Expand Down
40 changes: 40 additions & 0 deletions crates/sqlquack-lint/src/quality/tests/test_quality_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Value> =
Expand Down
Loading