Skip to content

Commit 4dd6098

Browse files
committed
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.
1 parent 20f2974 commit 4dd6098

3 files changed

Lines changed: 52 additions & 3 deletions

File tree

‎crates/sqlquack-lint/src/lint/main/parse_sql.rs‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22
33
use crate::lint::models::ParsedSql;
44

5-
/// Parse `sql` under `dialect`, rejecting statements the parser kept as opaque text.
6-
pub fn parse_sql(sql: &str, dialect: &str) -> Result<ParsedSql, String> {
5+
/// Parse `sql` under `dialect` within a call-depth limit (None: the default), refusing opaque text.
6+
pub fn parse_sql(
7+
sql: &str,
8+
dialect: &str,
9+
max_function_call_depth: Option<usize>,
10+
) -> Result<ParsedSql, String> {
711
let Some(parsed) = crate::dialects::main::lint_dialect::lint_dialect(dialect)? else {
812
return Err(format!("SQLQuack does not parse the {dialect} dialect yet"));
913
};
10-
crate::lint::_helpers::parsing::parse(sql, parsed, (None, None))
14+
crate::lint::_helpers::parsing::parse(sql, parsed, (None, max_function_call_depth))
1115
}

‎crates/sqlquack-lint/src/lint/tests/test_function_depth.rs‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,39 @@ fn given_excessive_function_depth_when_linting_then_complexity_guard_rejects_it(
8080
);
8181
}
8282
}
83+
84+
#[test]
85+
fn given_a_call_depth_limit_when_parsing_sql_then_the_limit_applies() {
86+
let test_cases = [
87+
test_types::ParseSqlDepthTestCase {
88+
description: "a raised limit of 400 parses 300 nested calls",
89+
depth: 300,
90+
max_function_call_depth: Some(400),
91+
expected_outcome_end: "parsed",
92+
},
93+
test_types::ParseSqlDepthTestCase {
94+
description: "the default limit refuses 300 nested calls",
95+
depth: 300,
96+
max_function_call_depth: None,
97+
expected_outcome_end: "value 257 exceeds configured limit 256; raise \
98+
max_function_call_depth, up to 512",
99+
},
100+
test_types::ParseSqlDepthTestCase {
101+
description: "a limit past the ceiling is refused",
102+
depth: 300,
103+
max_function_call_depth: Some(1000),
104+
expected_outcome_end: "the function-call depth limit must be between 1 and 512, \
105+
SQLQuack's fixed nesting ceiling, not 1000",
106+
},
107+
];
108+
for test_case in test_cases {
109+
let sql = helpers::nested_function_sql(test_case.depth);
110+
let outcome: String = crate::parse_sql(&sql, "duckdb", test_case.max_function_call_depth)
111+
.map_or_else(|error| error, |_| "parsed".to_owned());
112+
assert!(
113+
outcome.ends_with(test_case.expected_outcome_end),
114+
"{}: {outcome}",
115+
test_case.description
116+
);
117+
}
118+
}

‎crates/sqlquack-lint/src/lint/tests/test_types.rs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,15 @@ pub(crate) struct FunctionDepthFailureTestCase {
112112
pub expected_error_end: &'static str,
113113
}
114114

115+
pub(crate) struct ParseSqlDepthTestCase {
116+
pub description: &'static str,
117+
pub depth: usize,
118+
/// The `max_function_call_depth` argument; None for the default.
119+
pub max_function_call_depth: Option<usize>,
120+
/// How the error ends, or `parsed` when the SQL parses.
121+
pub expected_outcome_end: &'static str,
122+
}
123+
115124
pub(crate) struct NestingRefusalTestCase {
116125
pub description: &'static str,
117126
pub dialect: &'static str,

0 commit comments

Comments
 (0)