diff --git a/coverage/syntax-bigquery/README.md b/coverage/syntax-bigquery/README.md index 6b77701c3..b422a5903 100644 --- a/coverage/syntax-bigquery/README.md +++ b/coverage/syntax-bigquery/README.md @@ -490,18 +490,18 @@ parser reports every case as `no_parser`, which must fail rather than compare eq Where the local parser and BigQuery differ by design rather than by a missing form: -- **Nesting.** The parser counts nested levels against a budget of 200 units and refuses deeper +- **Nesting.** The parser counts nested levels against a budget of 600 units and refuses deeper statements with "the statement nests too deeply", a parser check rather than a syntax error. - A level can cost more than one unit: the budget allows 98 nested scalar, `EXISTS` and `IN` + A level can cost more than one unit: the budget allows 298 nested scalar, `EXISTS` and `IN` subqueries, parenthesised joins and set operations, lambdas and parenthesised table-valued - function arguments, 197 parentheses, calls and `CASE` expressions, and fewer levels of mixed - constructs (39 of `(SELECT f(-( ... )))`). It also refuses a statement whose tree nests more + function arguments, 597 parentheses, calls and `CASE` expressions, and fewer levels of mixed + constructs (119 of `(SELECT f(-( ... )))`). It also refuses a statement whose tree nests more than 8,000 levels, which bounds flat chains and chains nested in another chain's operand. GoogleSQL's parser accepts all of these at 20,000 levels. - **Nested keyword-led calls.** A keyword reading of a call such as `TRIM` or `POSITION` is tried at most once at each position, so nested calls take about quadratic time in their depth; the - nesting budget caps it at about 0.4 s in a debug build and 31 ms in a release build at 195 - levels. + nesting budget caps it at about 2.6 s in a debug build and 0.37 s in a release build at 590 + levels of `TRIM(`. - **A string date part.** `EXTRACT('HOUR' FROM ts)` is refused by the local parser as a syntax error. GoogleSQL's parser takes it and its analysis refuses it ("A valid date part name is required"), so the verdict matches BigQuery's and only the syntax flag differs. diff --git a/crates/sqlquack-lint/src/bigquery/profile/main/bigquery_profile.rs b/crates/sqlquack-lint/src/bigquery/profile/main/bigquery_profile.rs index 10dfe858a..77dea8ea7 100644 --- a/crates/sqlquack-lint/src/bigquery/profile/main/bigquery_profile.rs +++ b/crates/sqlquack-lint/src/bigquery/profile/main/bigquery_profile.rs @@ -17,7 +17,7 @@ pub(crate) fn bigquery_profile() -> LintProfile { .tokenize(sql) .map_err(|error| error.to_string()) }, - statements: |sql, _| native_statements(sql, strict_parse), + statements: |sql, options| native_statements(sql, strict_parse, options), predicates: |text, _| native_predicates(text, strict_parse), own_predicate_parser: true, foreign_aggregates: &FOREIGN_AGGREGATE_CALLS, diff --git a/crates/sqlquack-lint/src/databricks/profile/main/databricks_profile.rs b/crates/sqlquack-lint/src/databricks/profile/main/databricks_profile.rs index bfee5dc29..6b91796af 100644 --- a/crates/sqlquack-lint/src/databricks/profile/main/databricks_profile.rs +++ b/crates/sqlquack-lint/src/databricks/profile/main/databricks_profile.rs @@ -17,7 +17,7 @@ pub(crate) fn databricks_profile() -> LintProfile { .tokenize(sql) .map_err(|error| error.to_string()) }, - statements: |sql, _| native_statements(sql, strict_parse), + statements: |sql, options| native_statements(sql, strict_parse, options), predicates: |text, _| native_predicates(text, strict_parse), own_predicate_parser: true, foreign_aggregates: &FOREIGN_AGGREGATE_CALLS, diff --git a/crates/sqlquack-lint/src/dialects/main/native_statements.rs b/crates/sqlquack-lint/src/dialects/main/native_statements.rs index 0ca2470ce..ccfb640d0 100644 --- a/crates/sqlquack-lint/src/dialects/main/native_statements.rs +++ b/crates/sqlquack-lint/src/dialects/main/native_statements.rs @@ -1,14 +1,29 @@ //! `native_statements`: the statements of SQL from a dialect's own strict parser. -use sqlquack_syntax::Expression; +use sqlquack_syntax::guard::main::enforce_ast::enforce_ast; +use sqlquack_syntax::{ComplexityGuardOptions, Expression, ParseOptions}; use crate::dialects::_helpers::parse_errors::{parse_error, uncovered}; use crate::dialects::types::StrictParse; -/// The statements `parse` reads from `sql`, refusing what that engine refuses with the line and column where it stopped. -pub(crate) fn native_statements(sql: &str, parse: StrictParse) -> Result, String> { - parse(sql).map_err(|(position, message)| match message { +/// The statements `parse` reads from `sql`, refusing what that engine refuses and trees past `options`' AST depth. +pub(crate) fn native_statements( + sql: &str, + parse: StrictParse, + options: &ParseOptions, +) -> Result, String> { + let statements: Vec = parse(sql).map_err(|(position, message)| match message { Some(message) => parse_error(sql, position, &message), None => uncovered(sql, position), - }) + })?; + let depth_only = ComplexityGuardOptions { + max_ast_nodes: None, + max_ast_depth: options + .complexity_guard + .as_ref() + .and_then(|guard| guard.max_ast_depth), + ..Default::default() + }; + enforce_ast(&statements, &depth_only).map_err(|error| error.to_string())?; + Ok(statements) } diff --git a/crates/sqlquack-lint/src/lint/_helpers/parsing.rs b/crates/sqlquack-lint/src/lint/_helpers/parsing.rs index 0859173ec..d8c97188d 100644 --- a/crates/sqlquack-lint/src/lint/_helpers/parsing.rs +++ b/crates/sqlquack-lint/src/lint/_helpers/parsing.rs @@ -2,9 +2,11 @@ use std::collections::HashSet; -use sqlquack_parse::parsing::constants::MAX_FUNCTION_CALL_DEPTH; +use sqlquack_parse::parsing::constants::{MAX_CALL_DEPTH_CEILING, MAX_FUNCTION_CALL_DEPTH}; use sqlquack_parse::parsing::main::call_depth_hint::call_depth_hint; use sqlquack_parse::parsing::main::checked_call_depth::checked_call_depth; +use sqlquack_syntax::guard::main::enforce_lexed_tokens::enforce_lexed_tokens; +use sqlquack_syntax::guard::main::has_function_call::has_function_call; use sqlquack_syntax::tokens::models::{Token, TokenType}; use sqlquack_syntax::{ComplexityGuardOptions, Expression, ExpressionWalk, ParseOptions}; @@ -23,23 +25,50 @@ pub(crate) fn parse( .map_err(|reason| format!("max_function_call_depth: {reason}"))?; let profile: LintProfile = profile(dialect); let tokens: Vec = (profile.tokens)(sql)?; + let parse_text = empty_relation + .map(|name| without_empty_relation_parentheses(sql, &tokens, name)) + .unwrap_or_default(); + let (parse_text, parse_tokens): (&str, Vec) = if parse_text.is_empty() { + (sql, Vec::new()) + } else { + (&parse_text, (profile.tokens)(&parse_text)?) + }; + let guarded_tokens: &[Token] = if parse_tokens.is_empty() { + &tokens + } else { + &parse_tokens + }; + let depth_guard = ComplexityGuardOptions { + max_parser_depth: None, + max_input_bytes: None, + max_tokens: None, + max_ast_nodes: None, + max_ast_depth: None, + max_parenthesis_depth: Some(MAX_CALL_DEPTH_CEILING), + max_function_call_depth: Some(max_call_depth), + }; + enforce_lexed_tokens(guarded_tokens, &depth_guard).map_err(|error| { + call_depth_hint( + error.to_string(), + max_call_depth, + Some("max_function_call_depth"), + || has_function_call(guarded_tokens), + ) + })?; let options = ParseOptions { complexity_guard: Some(ComplexityGuardOptions { max_function_call_depth: Some(max_call_depth), ..Default::default() }), }; - let parse_text = empty_relation - .map(|name| without_empty_relation_parentheses(sql, &tokens, name)) - .unwrap_or_default(); - let parse_text = if parse_text.is_empty() { - sql - } else { - &parse_text - }; let statements: Vec = (profile.statements)(parse_text, &options).map_err(|message| { - call_depth_hint(message, max_call_depth, Some("max_function_call_depth")) + call_depth_hint( + message, + max_call_depth, + Some("max_function_call_depth"), + || has_function_call(guarded_tokens), + ) })?; for statement in &statements { if statement diff --git a/crates/sqlquack-lint/src/lint/tests/helpers.rs b/crates/sqlquack-lint/src/lint/tests/helpers.rs index 2eefe7d28..633e4d6e2 100644 --- a/crates/sqlquack-lint/src/lint/tests/helpers.rs +++ b/crates/sqlquack-lint/src/lint/tests/helpers.rs @@ -10,6 +10,47 @@ pub(crate) fn nested_function_sql(depth: usize) -> String { format!("SELECT {}1{}", "F(".repeat(depth), ")".repeat(depth)) } +/// `depth` nested calls under an output alias. +pub(crate) fn aliased_calls_sql(depth: usize) -> String { + format!( + "SELECT {}1{} AS n FROM t", + "F(".repeat(depth), + ")".repeat(depth) + ) +} + +/// `depth` nested calls compared in a WHERE clause. +pub(crate) fn compared_calls_sql(depth: usize) -> String { + format!( + "SELECT * FROM t WHERE {}1{} = 1", + "F(".repeat(depth), + ")".repeat(depth) + ) +} + +/// `depth` nested calls under an alias inside a CTE. +pub(crate) fn cte_calls_sql(depth: usize) -> String { + format!( + "WITH c AS (SELECT {}1{} AS n FROM t) SELECT n FROM c", + "F(".repeat(depth), + ")".repeat(depth) + ) +} + +/// `depth` nested calls around a subquery reading the fixture harness's empty relation. +pub(crate) fn calls_around_empty_relation_sql(depth: usize) -> String { + format!( + "SELECT {}(SELECT 1 FROM __empty_fixture()){}", + "F(".repeat(depth), + ")".repeat(depth) + ) +} + +/// `depth` UNION ALL operands after a first SELECT, with no call. +pub(crate) fn union_chain_sql(depth: usize) -> String { + format!("SELECT 1{}", " UNION ALL SELECT 1".repeat(depth)) +} + pub(crate) fn nested_from_sql(depth: usize) -> String { format!( "SELECT * FROM {}orders{}", @@ -26,17 +67,43 @@ pub(crate) fn diagnostics(sql: &str) -> Result, String> { pub(crate) fn diagnostics_within( sql: &str, max_function_call_depth: Option, +) -> Result, String> { + dialect_diagnostics_within(sql, "snowflake", max_function_call_depth) +} + +/// The `dialect` diagnostics of `sql`, with the request's `max_function_call_depth`, null when absent. +pub(crate) fn dialect_diagnostics_within( + sql: &str, + dialect: &str, + max_function_call_depth: Option, ) -> Result, String> { let request = json!({ "version": 1, "sql": sql, - "dialect": "snowflake", + "dialect": dialect, "max_function_call_depth": max_function_call_depth }); let response = lint_json(&request.to_string())?; diagnostic_values(&response) } +/// The `dialect` diagnostics of `sql` in the fixture harness, with the request's `max_function_call_depth`. +pub(crate) fn harness_diagnostics_within( + sql: &str, + dialect: &str, + max_function_call_depth: Option, +) -> Result, String> { + let request = json!({ + "version": 1, + "sql": sql, + "dialect": dialect, + "max_function_call_depth": max_function_call_depth, + "fixture_harness": fixture_harness() + }); + let response = lint_json(&request.to_string())?; + diagnostic_values(&response) +} + pub(crate) fn diagnostics_for_rules(sql: &str, rules: &[&str]) -> Result, String> { diagnostics_for_dialect(sql, "snowflake", rules) } 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 f569a1e57..9f057c5e7 100644 --- a/crates/sqlquack-lint/src/lint/tests/test_function_depth.rs +++ b/crates/sqlquack-lint/src/lint/tests/test_function_depth.rs @@ -116,3 +116,204 @@ fn given_a_call_depth_limit_when_parsing_sql_then_the_limit_applies() { ); } } + +#[test] +fn given_a_call_depth_limit_in_each_linted_dialect_when_linting_then_it_is_the_exact_limit() { + let test_cases = [ + test_types::DialectDepthTestCase { + sql: helpers::nested_function_sql, + description: "a lowered limit of 3 lints 3 nested calls", + depth: 3, + max_function_call_depth: Some(3), + expected_outcome_end: "linted", + }, + test_types::DialectDepthTestCase { + sql: helpers::nested_function_sql, + description: "a lowered limit of 3 refuses 4 nested calls", + depth: 4, + max_function_call_depth: Some(3), + expected_outcome_end: "value 4 exceeds configured limit 3; raise \ + max_function_call_depth, up to 512", + }, + test_types::DialectDepthTestCase { + sql: helpers::nested_function_sql, + description: "a limit of 64 refuses 100 nested calls at the 65th", + depth: 100, + max_function_call_depth: Some(64), + expected_outcome_end: "value 65 exceeds configured limit 64; raise \ + max_function_call_depth, up to 512", + }, + test_types::DialectDepthTestCase { + sql: helpers::nested_function_sql, + description: "the default limit lints 256 nested calls", + depth: 256, + max_function_call_depth: None, + expected_outcome_end: "linted", + }, + test_types::DialectDepthTestCase { + sql: helpers::nested_function_sql, + description: "the default limit refuses 257 nested calls", + depth: 257, + max_function_call_depth: None, + expected_outcome_end: "value 257 exceeds configured limit 256; raise \ + max_function_call_depth, up to 512", + }, + test_types::DialectDepthTestCase { + sql: helpers::nested_function_sql, + description: "the ceiling of 512 lints 511 nested calls in a bare SELECT", + depth: 511, + max_function_call_depth: Some(512), + expected_outcome_end: "linted", + }, + test_types::DialectDepthTestCase { + sql: helpers::nested_function_sql, + description: "the ceiling of 512 refuses 512 nested calls, the statement being a level", + depth: 512, + max_function_call_depth: Some(512), + expected_outcome_end: "value 513 exceeds configured limit 512. The maximum is 512. The \ + statement and every expression around the calls also count toward that limit, so at \ + 512 a bare SELECT can nest at most 511 calls, and fewer fit inside an alias, \ + comparison or CTE.", + }, + test_types::DialectDepthTestCase { + sql: helpers::union_chain_sql, + description: "the ceiling of 512 refuses 600 UNION ALL operands without the calls note", + depth: 600, + max_function_call_depth: Some(512), + expected_outcome_end: "E_GUARD_AST_DEPTH_EXCEEDED: value 513 exceeds configured limit 512", + }, + test_types::DialectDepthTestCase { + sql: helpers::union_chain_sql, + description: "the ceiling of 512 lints 300 UNION ALL operands", + depth: 300, + max_function_call_depth: Some(512), + expected_outcome_end: "linted", + }, + test_types::DialectDepthTestCase { + sql: helpers::aliased_calls_sql, + description: "the ceiling of 512 lints 510 nested calls under an alias", + depth: 510, + max_function_call_depth: Some(512), + expected_outcome_end: "linted", + }, + test_types::DialectDepthTestCase { + sql: helpers::aliased_calls_sql, + description: "the ceiling of 512 refuses 511 nested calls under an alias", + depth: 511, + max_function_call_depth: Some(512), + expected_outcome_end: "value 513 exceeds configured limit 512. The maximum is 512. The \ + statement and every expression around the calls also count toward that limit, so at \ + 512 a bare SELECT can nest at most 511 calls, and fewer fit inside an alias, \ + comparison or CTE.", + }, + test_types::DialectDepthTestCase { + sql: helpers::compared_calls_sql, + description: "the ceiling of 512 lints 510 nested calls in a comparison", + depth: 510, + max_function_call_depth: Some(512), + expected_outcome_end: "linted", + }, + test_types::DialectDepthTestCase { + sql: helpers::compared_calls_sql, + description: "the ceiling of 512 refuses 511 nested calls in a comparison", + depth: 511, + max_function_call_depth: Some(512), + expected_outcome_end: "value 513 exceeds configured limit 512. The maximum is 512. The \ + statement and every expression around the calls also count toward that limit, so at \ + 512 a bare SELECT can nest at most 511 calls, and fewer fit inside an alias, \ + comparison or CTE.", + }, + test_types::DialectDepthTestCase { + sql: helpers::cte_calls_sql, + description: "the ceiling of 512 lints 509 nested calls in a CTE", + depth: 509, + max_function_call_depth: Some(512), + expected_outcome_end: "linted", + }, + test_types::DialectDepthTestCase { + sql: helpers::cte_calls_sql, + description: "the ceiling of 512 refuses 510 nested calls in a CTE", + depth: 510, + max_function_call_depth: Some(512), + expected_outcome_end: "value 513 exceeds configured limit 512. The maximum is 512. The \ + statement and every expression around the calls also count toward that limit, so at \ + 512 a bare SELECT can nest at most 511 calls, and fewer fit inside an alias, \ + comparison or CTE.", + }, + test_types::DialectDepthTestCase { + sql: helpers::nested_function_sql, + description: "the ceiling of 512 refuses 513 nested calls, naming it as fixed", + depth: 513, + max_function_call_depth: Some(512), + expected_outcome_end: "value 513 exceeds configured limit 512; 512 is SQLQuack's \ + fixed nesting ceiling, which no setting raises", + }, + ]; + for dialect in [ + "snowflake", + "duckdb", + "sqlite", + "postgres", + "bigquery", + "databricks", + ] { + for test_case in &test_cases { + let sql = (test_case.sql)(test_case.depth); + let outcome: String = helpers::dialect_diagnostics_within( + &sql, + dialect, + test_case.max_function_call_depth, + ) + .map_or_else(|error| error, |_| "linted".to_owned()); + assert!( + outcome.ends_with(test_case.expected_outcome_end), + "{dialect}: {}: {outcome}", + test_case.description + ); + } + } +} + +#[test] +fn given_a_fixture_harness_when_linting_at_a_call_depth_limit_then_its_empty_relation_is_no_call() { + let test_cases = [ + test_types::DialectDepthTestCase { + sql: helpers::calls_around_empty_relation_sql, + description: "a limit of 1 lints one call around the harness's empty relation", + depth: 1, + max_function_call_depth: Some(1), + expected_outcome_end: "linted", + }, + test_types::DialectDepthTestCase { + sql: helpers::calls_around_empty_relation_sql, + description: "a limit of 1 refuses two calls around the harness's empty relation", + depth: 2, + max_function_call_depth: Some(1), + expected_outcome_end: "value 2 exceeds configured limit 1; raise \ + max_function_call_depth, up to 512", + }, + ]; + for dialect in [ + "snowflake", + "duckdb", + "sqlite", + "postgres", + "bigquery", + "databricks", + ] { + for test_case in &test_cases { + let sql = (test_case.sql)(test_case.depth); + let outcome: String = helpers::harness_diagnostics_within( + &sql, + dialect, + test_case.max_function_call_depth, + ) + .map_or_else(|error| error, |_| "linted".to_owned()); + assert!( + outcome.ends_with(test_case.expected_outcome_end), + "{dialect}: {}: {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 067ed71bc..7f07e4625 100644 --- a/crates/sqlquack-lint/src/lint/tests/test_types.rs +++ b/crates/sqlquack-lint/src/lint/tests/test_types.rs @@ -112,6 +112,17 @@ pub(crate) struct FunctionDepthFailureTestCase { pub expected_error_end: &'static str, } +pub(crate) struct DialectDepthTestCase { + pub description: &'static str, + /// The statement holding `depth` nested calls. + pub sql: fn(usize) -> String, + pub depth: usize, + /// The request's `max_function_call_depth`; absent for the default. + pub max_function_call_depth: Option, + /// How the error ends, or `linted` when the SQL lints. + pub expected_outcome_end: &'static str, +} + pub(crate) struct ParseSqlDepthTestCase { pub description: &'static str, pub depth: usize, diff --git a/crates/sqlquack-lint/src/postgres/profile/main/postgres_profile.rs b/crates/sqlquack-lint/src/postgres/profile/main/postgres_profile.rs index 8ca2102f1..75c502abf 100644 --- a/crates/sqlquack-lint/src/postgres/profile/main/postgres_profile.rs +++ b/crates/sqlquack-lint/src/postgres/profile/main/postgres_profile.rs @@ -15,7 +15,7 @@ use crate::postgres::profile::constants::{ pub(crate) fn postgres_profile() -> LintProfile { LintProfile { tokens: |sql| shared_tokens(sql, Dialect::PostgreSQL), - statements: |sql, _| native_statements(sql, strict_parse), + statements: |sql, options| native_statements(sql, strict_parse, options), predicates: |_, tokens| shared_predicates(tokens, Dialect::PostgreSQL), own_predicate_parser: false, foreign_aggregates: &FOREIGN_AGGREGATE_CALLS, diff --git a/crates/sqlquack-parse/src/parsing/constants.rs b/crates/sqlquack-parse/src/parsing/constants.rs index eb47f60ec..cd9bf13ff 100644 --- a/crates/sqlquack-parse/src/parsing/constants.rs +++ b/crates/sqlquack-parse/src/parsing/constants.rs @@ -3,11 +3,17 @@ /// Nested function calls allowed per statement, above the parser's default, for generated SQL. pub const MAX_FUNCTION_CALL_DEPTH: usize = 256; -/// The highest function-call limit a caller may set, the fixed nesting limit every parse keeps. +/// The maximum is 512. The statement and every expression around the calls also count toward that limit, so at 512 a bare SELECT can nest at most 511 calls, and fewer fit inside an alias, comparison or CTE. pub const MAX_CALL_DEPTH_CEILING: usize = 512; +/// What a refusal at the ceiling adds: the statement's tree, not only its calls, meets the limit. +pub const CALL_DEPTH_CEILING_NOTE: &str = "The maximum is 512. The statement and every expression around the calls also count toward that limit, so at 512 a bare SELECT can nest at most 511 calls, and fewer fit inside an alias, comparison or CTE."; + /// The guard code of a parse refused for function calls nested past the limit. pub const FUNCTION_NESTING_CODE: &str = "E_GUARD_FUNCTION_NESTING_DEPTH_EXCEEDED"; /// The guard code of a parse refused for brackets nested past the fixed ceiling. pub const NESTING_CODE: &str = "E_GUARD_NESTING_DEPTH_EXCEEDED"; + +/// The guard code of a parse refused for a statement tree nested past the fixed ceiling. +pub const AST_DEPTH_CODE: &str = "E_GUARD_AST_DEPTH_EXCEEDED"; diff --git a/crates/sqlquack-parse/src/parsing/main/call_depth_hint.rs b/crates/sqlquack-parse/src/parsing/main/call_depth_hint.rs index 94e932e54..e3e63ddb4 100644 --- a/crates/sqlquack-parse/src/parsing/main/call_depth_hint.rs +++ b/crates/sqlquack-parse/src/parsing/main/call_depth_hint.rs @@ -1,10 +1,21 @@ //! `call_depth_hint`: what a refusal for nesting past a limit tells the caller. -use crate::parsing::constants::{FUNCTION_NESTING_CODE, MAX_CALL_DEPTH_CEILING, NESTING_CODE}; +use crate::parsing::constants::{ + AST_DEPTH_CODE, CALL_DEPTH_CEILING_NOTE, FUNCTION_NESTING_CODE, MAX_CALL_DEPTH_CEILING, + NESTING_CODE, +}; -/// `message` naming the fixed ceiling it met, or how to raise `limit` through `setting`. -pub fn call_depth_hint(message: String, limit: usize, setting: Option<&str>) -> String { +/// `message` naming the fixed ceiling it met, or how to raise `limit` through `setting`; `has_calls` says whether the SQL calls anything. +pub fn call_depth_hint( + message: String, + limit: usize, + setting: Option<&str>, + has_calls: impl FnOnce() -> bool, +) -> String { let function_nesting: bool = message.contains(FUNCTION_NESTING_CODE); + if message.contains(AST_DEPTH_CODE) && limit >= MAX_CALL_DEPTH_CEILING && has_calls() { + return format!("{message}. {CALL_DEPTH_CEILING_NOTE}"); + } if message.contains(NESTING_CODE) || (function_nesting && limit >= MAX_CALL_DEPTH_CEILING) { return format!( "{message}; {MAX_CALL_DEPTH_CEILING} is SQLQuack's fixed nesting ceiling, which no \ diff --git a/crates/sqlquack-parse/src/parsing/main/parse_source_within.rs b/crates/sqlquack-parse/src/parsing/main/parse_source_within.rs index 1bbb1aad9..38cc8784d 100644 --- a/crates/sqlquack-parse/src/parsing/main/parse_source_within.rs +++ b/crates/sqlquack-parse/src/parsing/main/parse_source_within.rs @@ -1,5 +1,6 @@ //! `parse_source_within`: source SQL to statements, with the caller's function-call limit. +use sqlquack_syntax::guard::main::has_function_call::has_function_call; use sqlquack_syntax::tokens::errors::ParseFailure; use sqlquack_syntax::{Dialect, Expression}; @@ -21,7 +22,10 @@ pub fn parse_source_within( }, )?; parse_guarded(sql, dialect, limit).map_err(|failure| ParseFailure { - message: call_depth_hint(failure.message, limit, None), + message: call_depth_hint(failure.message, limit, None, || { + sqlquack_syntax_snowflake::dialect_tokens(sql, dialect) + .is_ok_and(|tokens| has_function_call(&tokens)) + }), ..failure }) } diff --git a/crates/sqlquack-parse/src/parsing/tests/test_parse_source.rs b/crates/sqlquack-parse/src/parsing/tests/test_parse_source.rs index 40a5b2217..36bbaf195 100644 --- a/crates/sqlquack-parse/src/parsing/tests/test_parse_source.rs +++ b/crates/sqlquack-parse/src/parsing/tests/test_parse_source.rs @@ -44,6 +44,17 @@ fn given_nested_function_calls_when_parsed_on_a_small_stack_then_the_limit_decid levels: 511, expected_outcome: Ok(1), }, + CallDepthTestCase { + description: "the ceiling of 512 refuses 512 nested calls, saying why fewer fit", + limit: Some(512), + levels: 512, + expected_outcome: Err( + "value 513 exceeds configured limit 512. The maximum is 512. The statement and \ + every expression around the calls also count toward that limit, so at 512 a bare \ + SELECT can nest at most 511 calls, and fewer fit inside an alias, comparison or \ + CTE.", + ), + }, CallDepthTestCase { description: "a limit of 1000 is refused, naming the ceiling of 512", limit: Some(1000), @@ -99,6 +110,34 @@ fn given_nested_function_calls_when_parsed_on_a_small_stack_then_the_limit_decid } } +#[test] +fn given_a_deep_tree_without_calls_when_parsed_at_the_ceiling_then_the_refusal_has_no_calls_note() { + let test_cases = [CallDepthTestCase { + description: "600 UNION ALL operands at the ceiling of 512 get the bare depth refusal", + limit: Some(512), + levels: 600, + expected_outcome: Err("E_GUARD_AST_DEPTH_EXCEEDED: value 513 exceeds configured limit 512"), + }]; + + for test_case in test_cases { + let sql = format!("SELECT 1{}", " UNION ALL SELECT 1".repeat(test_case.levels)); + let outcome: Result = + parse_source_within(&sql, Dialect::Snowflake, test_case.limit) + .map(|statements| statements.len()) + .map_err(|failure| failure.message); + let ends_as_expected: Result = + outcome.as_ref().map(|count| *count).map_err(|message| { + message.ends_with(test_case.expected_outcome.err().unwrap_or_default()) + }); + assert_eq!( + ends_as_expected, + test_case.expected_outcome.map_err(|_| true), + "{}: {outcome:?}", + test_case.description + ); + } +} + #[test] fn given_a_requested_call_depth_when_checked_then_only_1_to_512_is_accepted() { let test_cases = [ diff --git a/crates/sqlquack-syntax-bigquery/README.md b/crates/sqlquack-syntax-bigquery/README.md index 22bb9ba80..1f131d06a 100644 --- a/crates/sqlquack-syntax-bigquery/README.md +++ b/crates/sqlquack-syntax-bigquery/README.md @@ -125,10 +125,11 @@ No input exhausts the caller's stack, and none takes more than polynomial time i prefixes, subscript indexes, the operands of keyword-led calls such as `EXTRACT` and `TRIM`, table-valued function arguments, parenthesised FROM items, joins and row patterns) runs on a stack that grows on demand, so a 2 MiB thread parses as an 8 MiB one does. A statement - nesting past 200 levels is refused with "the statement nests too deeply", a parser check + nesting past 600 levels is refused with "the statement nests too deeply", a parser check rather than a syntax error; once one reading passes the limit, every other reading fails at - once. A level can cost more than one unit, so the limit allows 98 nested subqueries, 197 - parentheses or calls, and fewer levels of mixed constructs. GoogleSQL's parser has no such + once. A level can cost more than one unit, so the limit allows 298 nested subqueries, 597 + parentheses or calls, and fewer levels of mixed constructs. That is past SQLQuack's fixed + ceiling of 512 nested calls, so the configured call-depth limit decides first. GoogleSQL's parser has no such limit, so deeper inputs it accepts are refused here. - Flat chains of operators, set operations, joins and pipe operators parse in linear time, but a statement whose tree nests more than 8,000 levels is refused with the same error, so a diff --git a/crates/sqlquack-syntax-bigquery/src/parse/_helpers/tree/cursor.rs b/crates/sqlquack-syntax-bigquery/src/parse/_helpers/tree/cursor.rs index 76283999b..d32c6786e 100644 --- a/crates/sqlquack-syntax-bigquery/src/parse/_helpers/tree/cursor.rs +++ b/crates/sqlquack-syntax-bigquery/src/parse/_helpers/tree/cursor.rs @@ -14,8 +14,8 @@ use crate::scan::models::{NumberKind, Token, TokenKind}; pub(crate) type Parsing = Result; -/// Nesting deeper than this fails as a parser check, not as a syntax error. -const MAX_DEPTH: usize = 200; +/// Nesting deeper than this fails as a parser check, past SQLQuack's 512-call ceiling. +const MAX_DEPTH: usize = 600; /// Stack left below which a deeper level moves to a new segment, so the caller's stack size never decides. const RED_ZONE: usize = if cfg!(debug_assertions) { diff --git a/crates/sqlquack-syntax-bigquery/src/parse/tests/test_parse.rs b/crates/sqlquack-syntax-bigquery/src/parse/tests/test_parse.rs index ba52a943e..77133e00b 100644 --- a/crates/sqlquack-syntax-bigquery/src/parse/tests/test_parse.rs +++ b/crates/sqlquack-syntax-bigquery/src/parse/tests/test_parse.rs @@ -1561,10 +1561,10 @@ fn given_deep_nesting_when_parsing_on_a_small_stack_then_it_parses_or_is_refused expected_refusal: None, }, NestingTestCase { - description: "100 rounds of a call, CASE and CAST nest past the limit", + description: "300 rounds of a call, CASE and CAST nest past the limit", sql: || { - let open: String = "abs(CASE WHEN true THEN CAST(".repeat(100); - format!("SELECT {open}1{}", " AS INT64) END)".repeat(100)) + let open: String = "abs(CASE WHEN true THEN CAST(".repeat(300); + format!("SELECT {open}1{}", " AS INT64) END)".repeat(300)) }, expected_refusal: too_deep, }, @@ -1574,8 +1574,8 @@ fn given_deep_nesting_when_parsing_on_a_small_stack_then_it_parses_or_is_refused expected_refusal: None, }, NestingTestCase { - description: "201 nested bare subscripts nest past the limit", - sql: || format!("SELECT {}0{} FROM t", "a[".repeat(201), "]".repeat(201)), + description: "601 nested bare subscripts nest past the limit", + sql: || format!("SELECT {}0{} FROM t", "a[".repeat(601), "]".repeat(601)), expected_refusal: too_deep, }, NestingTestCase { @@ -1628,10 +1628,10 @@ fn given_deep_nesting_when_parsing_on_a_small_stack_then_it_parses_or_is_refused expected_refusal: too_deep, }, NestingTestCase { - description: "201 nested OVERLAY inputs", + description: "601 nested OVERLAY inputs", sql: || { - let open: String = "OVERLAY(".repeat(201); - format!("SELECT {open}'a'{}", " PLACING 'b' FROM 1)".repeat(201)) + let open: String = "OVERLAY(".repeat(601); + format!("SELECT {open}'a'{}", " PLACING 'b' FROM 1)".repeat(601)) }, expected_refusal: too_deep, }, @@ -1724,8 +1724,8 @@ fn given_deep_nesting_when_parsing_on_a_small_stack_then_it_parses_or_is_refused NestingTestCase { description: "past the limit, every other reading fails at once instead of retrying", sql: || { - let open: String = "(SELECT 1 IN ".repeat(150); - format!("SELECT 1 IN {open}(1){}", ")".repeat(150)) + let open: String = "(SELECT 1 IN ".repeat(400); + format!("SELECT 1 IN {open}(1){}", ")".repeat(400)) }, expected_refusal: too_deep, }, diff --git a/crates/sqlquack-syntax-databricks/src/parse/_helpers/tree/cursor.rs b/crates/sqlquack-syntax-databricks/src/parse/_helpers/tree/cursor.rs index ad3fa4546..c4233b2e5 100644 --- a/crates/sqlquack-syntax-databricks/src/parse/_helpers/tree/cursor.rs +++ b/crates/sqlquack-syntax-databricks/src/parse/_helpers/tree/cursor.rs @@ -12,8 +12,8 @@ use crate::scan::models::{Token, TokenKind}; pub(crate) type Parsing = Result; -/// Nesting deeper than this fails as a parser check, not as a syntax error. -const MAX_DEPTH: usize = 200; +/// Nesting deeper than this fails as a parser check, past SQLQuack's 512-call ceiling. +const MAX_DEPTH: usize = 600; /// Stack left below which a deeper level moves to a new segment, whatever the caller's stack. const RED_ZONE: usize = if cfg!(debug_assertions) { diff --git a/crates/sqlquack-syntax-databricks/src/parse/tests/test_small_stack.rs b/crates/sqlquack-syntax-databricks/src/parse/tests/test_small_stack.rs index 00d14d881..736d3fd83 100644 --- a/crates/sqlquack-syntax-databricks/src/parse/tests/test_small_stack.rs +++ b/crates/sqlquack-syntax-databricks/src/parse/tests/test_small_stack.rs @@ -115,8 +115,8 @@ fn given_deep_nesting_when_parsing_on_a_small_stack_then_it_is_refused_not_abort sql: || { format!( "SELECT 1 IN {}(1){}", - "(SELECT 1 IN ".repeat(150), - ")".repeat(150) + "(SELECT 1 IN ".repeat(400), + ")".repeat(400) ) }, expected_refusal: too_deep, @@ -139,8 +139,8 @@ fn given_deep_nesting_when_parsing_on_a_small_stack_then_it_is_refused_not_abort sql: || { format!( "CACHE SELECT {}1{} FROM t1", - "(".repeat(300), - ")".repeat(300) + "(".repeat(700), + ")".repeat(700) ) }, expected_refusal: None, diff --git a/crates/sqlquack-syntax-postgres/README.md b/crates/sqlquack-syntax-postgres/README.md index 2b976c17e..cfe3ed4d2 100644 --- a/crates/sqlquack-syntax-postgres/README.md +++ b/crates/sqlquack-syntax-postgres/README.md @@ -15,8 +15,9 @@ Nesting is bounded so that no input exhausts the caller's stack or takes exponen - Each nested level (parentheses, subqueries, FROM items, calls, CASE, prefixes such as `NOT`, statements inside `BEGIN ATOMIC` and `EXPLAIN`, and type names) runs on a stack that grows on - demand (the default `stacker` feature), and a statement nesting past 300 levels is refused with - 54001's "stack depth limit exceeded". A type's `[]` bounds count against the same limit. Once + demand (the default `stacker` feature), and a statement nesting past 600 levels, beyond + SQLQuack's fixed ceiling of 512 nested calls, is refused with 54001's "stack depth limit + exceeded". A type's `[]` bounds count against the same limit. Once one reading passes the limit, every other reading fails at once and the statement is refused. PostgreSQL's own limit depends on its stack size, so it accepts some deeper texts. - A flat chain of operators (other than `AND` and `OR`), casts or indirections is refused with diff --git a/crates/sqlquack-syntax-postgres/src/parse/_helpers/tree/cursor.rs b/crates/sqlquack-syntax-postgres/src/parse/_helpers/tree/cursor.rs index dd7f97d0c..cbdc4e021 100644 --- a/crates/sqlquack-syntax-postgres/src/parse/_helpers/tree/cursor.rs +++ b/crates/sqlquack-syntax-postgres/src/parse/_helpers/tree/cursor.rs @@ -13,8 +13,8 @@ use sqlquack_syntax::Gap; pub(crate) type Parsing = Result; -/// Recursion deeper than this fails as PostgreSQL's stack depth limit does (54001), not as syntax. -const MAX_DEPTH: usize = 300; +/// Recursion deeper than this fails as 54001, past SQLQuack's 512-call ceiling; PostgreSQL nests deeper. +const MAX_DEPTH: usize = 600; /// The longest flat chain of operators other than AND and OR, or of indirections. const MAX_CHAIN: usize = 1000; diff --git a/crates/sqlquack-syntax/src/guard/main/enforce_lexed_tokens.rs b/crates/sqlquack-syntax/src/guard/main/enforce_lexed_tokens.rs new file mode 100644 index 000000000..4fc916b53 --- /dev/null +++ b/crates/sqlquack-syntax/src/guard/main/enforce_lexed_tokens.rs @@ -0,0 +1,11 @@ +//! `enforce_lexed_tokens`. + +use crate::guard::_helpers::tokens::enforce_token_slice; +use crate::guard::models::ComplexityGuardOptions; +use crate::tokens::models::Token; +use crate::tokens::types::Result; + +/// Refuses `tokens` past `options`' token, parenthesis and function-call limits, before any parse. +pub fn enforce_lexed_tokens(tokens: &[Token], options: &ComplexityGuardOptions) -> Result<()> { + enforce_token_slice(tokens, options) +} diff --git a/crates/sqlquack-syntax/src/guard/main/has_function_call.rs b/crates/sqlquack-syntax/src/guard/main/has_function_call.rs new file mode 100644 index 000000000..261eb0198 --- /dev/null +++ b/crates/sqlquack-syntax/src/guard/main/has_function_call.rs @@ -0,0 +1,16 @@ +//! `has_function_call`. + +use crate::guard::_helpers::tokens::{is_function_call_name_token, is_trivia_token}; +use crate::tokens::models::{Token, TokenType}; + +/// Whether `tokens` hold a call: a name directly before an opening parenthesis, as the call-depth guard counts one. +pub fn has_function_call(tokens: &[Token]) -> bool { + let significant: Vec = tokens + .iter() + .map(|token| token.token_type) + .filter(|token_type| !is_trivia_token(*token_type)) + .collect(); + significant + .windows(2) + .any(|pair| is_function_call_name_token(pair[0]) && pair[1] == TokenType::LParen) +} diff --git a/crates/sqlquack-syntax/src/guard/main/mod.rs b/crates/sqlquack-syntax/src/guard/main/mod.rs index d0715a115..76159c5ad 100644 --- a/crates/sqlquack-syntax/src/guard/main/mod.rs +++ b/crates/sqlquack-syntax/src/guard/main/mod.rs @@ -2,6 +2,8 @@ pub mod enforce_ast; pub mod enforce_input; +pub mod enforce_lexed_tokens; pub mod enforce_parser_token_stats; pub mod enforce_parser_tokens; +pub mod has_function_call; pub mod is_guard_error;