diff --git a/Cargo.lock b/Cargo.lock index f429d33..4f1ddcb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -612,8 +612,8 @@ dependencies = [ [[package]] name = "sqlquack-derive" -version = "0.18.1" -source = "git+https://github.com/sqlstack-io/sqlquack.git?rev=ae2bef7a2299fa202212c721a7c173a9c32fb9bb#ae2bef7a2299fa202212c721a7c173a9c32fb9bb" +version = "0.21.0" +source = "git+https://github.com/sqlstack-io/sqlquack.git?rev=b938ec518b6ab5491090c55832f7d5a4059059bf#b938ec518b6ab5491090c55832f7d5a4059059bf" dependencies = [ "proc-macro2", "quote", @@ -622,8 +622,8 @@ dependencies = [ [[package]] name = "sqlquack-fingerprint" -version = "0.18.1" -source = "git+https://github.com/sqlstack-io/sqlquack.git?rev=ae2bef7a2299fa202212c721a7c173a9c32fb9bb#ae2bef7a2299fa202212c721a7c173a9c32fb9bb" +version = "0.21.0" +source = "git+https://github.com/sqlstack-io/sqlquack.git?rev=b938ec518b6ab5491090c55832f7d5a4059059bf#b938ec518b6ab5491090c55832f7d5a4059059bf" dependencies = [ "sha2", "sqlquack-parse", @@ -632,8 +632,8 @@ dependencies = [ [[package]] name = "sqlquack-format" -version = "0.18.1" -source = "git+https://github.com/sqlstack-io/sqlquack.git?rev=ae2bef7a2299fa202212c721a7c173a9c32fb9bb#ae2bef7a2299fa202212c721a7c173a9c32fb9bb" +version = "0.21.0" +source = "git+https://github.com/sqlstack-io/sqlquack.git?rev=b938ec518b6ab5491090c55832f7d5a4059059bf#b938ec518b6ab5491090c55832f7d5a4059059bf" dependencies = [ "serde_json", "sqlquack-parse", @@ -642,8 +642,8 @@ dependencies = [ [[package]] name = "sqlquack-lint" -version = "0.18.1" -source = "git+https://github.com/sqlstack-io/sqlquack.git?rev=ae2bef7a2299fa202212c721a7c173a9c32fb9bb#ae2bef7a2299fa202212c721a7c173a9c32fb9bb" +version = "0.21.0" +source = "git+https://github.com/sqlstack-io/sqlquack.git?rev=b938ec518b6ab5491090c55832f7d5a4059059bf#b938ec518b6ab5491090c55832f7d5a4059059bf" dependencies = [ "rayon", "rustc-hash", @@ -655,8 +655,8 @@ dependencies = [ [[package]] name = "sqlquack-parse" -version = "0.18.1" -source = "git+https://github.com/sqlstack-io/sqlquack.git?rev=ae2bef7a2299fa202212c721a7c173a9c32fb9bb#ae2bef7a2299fa202212c721a7c173a9c32fb9bb" +version = "0.21.0" +source = "git+https://github.com/sqlstack-io/sqlquack.git?rev=b938ec518b6ab5491090c55832f7d5a4059059bf#b938ec518b6ab5491090c55832f7d5a4059059bf" dependencies = [ "sqlquack-syntax", "sqlquack-syntax-bigquery", @@ -668,8 +668,8 @@ dependencies = [ [[package]] name = "sqlquack-syntax" -version = "0.18.1" -source = "git+https://github.com/sqlstack-io/sqlquack.git?rev=ae2bef7a2299fa202212c721a7c173a9c32fb9bb#ae2bef7a2299fa202212c721a7c173a9c32fb9bb" +version = "0.21.0" +source = "git+https://github.com/sqlstack-io/sqlquack.git?rev=b938ec518b6ab5491090c55832f7d5a4059059bf#b938ec518b6ab5491090c55832f7d5a4059059bf" dependencies = [ "serde", "serde_json", @@ -681,8 +681,8 @@ dependencies = [ [[package]] name = "sqlquack-syntax-bigquery" -version = "0.18.1" -source = "git+https://github.com/sqlstack-io/sqlquack.git?rev=ae2bef7a2299fa202212c721a7c173a9c32fb9bb#ae2bef7a2299fa202212c721a7c173a9c32fb9bb" +version = "0.21.0" +source = "git+https://github.com/sqlstack-io/sqlquack.git?rev=b938ec518b6ab5491090c55832f7d5a4059059bf#b938ec518b6ab5491090c55832f7d5a4059059bf" dependencies = [ "sqlquack-syntax", "stacker", @@ -690,8 +690,8 @@ dependencies = [ [[package]] name = "sqlquack-syntax-databricks" -version = "0.18.1" -source = "git+https://github.com/sqlstack-io/sqlquack.git?rev=ae2bef7a2299fa202212c721a7c173a9c32fb9bb#ae2bef7a2299fa202212c721a7c173a9c32fb9bb" +version = "0.21.0" +source = "git+https://github.com/sqlstack-io/sqlquack.git?rev=b938ec518b6ab5491090c55832f7d5a4059059bf#b938ec518b6ab5491090c55832f7d5a4059059bf" dependencies = [ "sqlquack-syntax", "stacker", @@ -699,8 +699,8 @@ dependencies = [ [[package]] name = "sqlquack-syntax-postgres" -version = "0.18.1" -source = "git+https://github.com/sqlstack-io/sqlquack.git?rev=ae2bef7a2299fa202212c721a7c173a9c32fb9bb#ae2bef7a2299fa202212c721a7c173a9c32fb9bb" +version = "0.21.0" +source = "git+https://github.com/sqlstack-io/sqlquack.git?rev=b938ec518b6ab5491090c55832f7d5a4059059bf#b938ec518b6ab5491090c55832f7d5a4059059bf" dependencies = [ "sqlquack-syntax", "stacker", @@ -708,8 +708,8 @@ dependencies = [ [[package]] name = "sqlquack-syntax-snowflake" -version = "0.18.1" -source = "git+https://github.com/sqlstack-io/sqlquack.git?rev=ae2bef7a2299fa202212c721a7c173a9c32fb9bb#ae2bef7a2299fa202212c721a7c173a9c32fb9bb" +version = "0.21.0" +source = "git+https://github.com/sqlstack-io/sqlquack.git?rev=b938ec518b6ab5491090c55832f7d5a4059059bf#b938ec518b6ab5491090c55832f7d5a4059059bf" dependencies = [ "serde", "serde_json", @@ -721,8 +721,8 @@ dependencies = [ [[package]] name = "sqlquack-syntax-sqlserver" -version = "0.18.1" -source = "git+https://github.com/sqlstack-io/sqlquack.git?rev=ae2bef7a2299fa202212c721a7c173a9c32fb9bb#ae2bef7a2299fa202212c721a7c173a9c32fb9bb" +version = "0.21.0" +source = "git+https://github.com/sqlstack-io/sqlquack.git?rev=b938ec518b6ab5491090c55832f7d5a4059059bf#b938ec518b6ab5491090c55832f7d5a4059059bf" dependencies = [ "sqlquack-syntax", "stacker", diff --git a/Cargo.toml b/Cargo.toml index 8ec4831..e02c436 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,12 +30,12 @@ sha2 = "0.10" similar = "2" sqlclean = { path = "crates/sqlclean" } # SQLQuack's formatter and lint engine, and the tokens and trees they read, which replace Polyglot. -sqlquack-fingerprint = { git = "https://github.com/sqlstack-io/sqlquack.git", rev = "ae2bef7a2299fa202212c721a7c173a9c32fb9bb", features = ["bigquery", "databricks", "duckdb", "postgres", "snowflake", "sqlite", "sqlserver"] } -sqlquack-format = { git = "https://github.com/sqlstack-io/sqlquack.git", rev = "ae2bef7a2299fa202212c721a7c173a9c32fb9bb", features = ["bigquery", "databricks", "duckdb", "postgres", "snowflake", "sqlserver"] } -sqlquack-lint = { git = "https://github.com/sqlstack-io/sqlquack.git", rev = "ae2bef7a2299fa202212c721a7c173a9c32fb9bb", features = ["bigquery", "databricks", "duckdb", "postgres", "snowflake", "sqlite"] } -sqlquack-parse = { git = "https://github.com/sqlstack-io/sqlquack.git", rev = "ae2bef7a2299fa202212c721a7c173a9c32fb9bb" } -sqlquack-syntax = { git = "https://github.com/sqlstack-io/sqlquack.git", rev = "ae2bef7a2299fa202212c721a7c173a9c32fb9bb" } -sqlquack-syntax-sqlserver = { git = "https://github.com/sqlstack-io/sqlquack.git", rev = "ae2bef7a2299fa202212c721a7c173a9c32fb9bb" } +sqlquack-fingerprint = { git = "https://github.com/sqlstack-io/sqlquack.git", rev = "b938ec518b6ab5491090c55832f7d5a4059059bf", features = ["bigquery", "databricks", "duckdb", "postgres", "snowflake", "sqlite", "sqlserver"] } +sqlquack-format = { git = "https://github.com/sqlstack-io/sqlquack.git", rev = "b938ec518b6ab5491090c55832f7d5a4059059bf", features = ["bigquery", "databricks", "duckdb", "postgres", "snowflake", "sqlserver"] } +sqlquack-lint = { git = "https://github.com/sqlstack-io/sqlquack.git", rev = "b938ec518b6ab5491090c55832f7d5a4059059bf", features = ["bigquery", "databricks", "duckdb", "postgres", "snowflake", "sqlite"] } +sqlquack-parse = { git = "https://github.com/sqlstack-io/sqlquack.git", rev = "b938ec518b6ab5491090c55832f7d5a4059059bf" } +sqlquack-syntax = { git = "https://github.com/sqlstack-io/sqlquack.git", rev = "b938ec518b6ab5491090c55832f7d5a4059059bf" } +sqlquack-syntax-sqlserver = { git = "https://github.com/sqlstack-io/sqlquack.git", rev = "b938ec518b6ab5491090c55832f7d5a4059059bf" } [workspace.lints.rust] unsafe_code = "forbid" diff --git a/crates/sqlclean-cli/skill/references/docs/cli/facts.md b/crates/sqlclean-cli/skill/references/docs/cli/facts.md index 1016d5f..e18263b 100644 --- a/crates/sqlclean-cli/skill/references/docs/cli/facts.md +++ b/crates/sqlclean-cli/skill/references/docs/cli/facts.md @@ -13,7 +13,7 @@ so use it to see what a Rule will read before writing one. ## Usage ```bash -sqlclean facts [--dialect ] < query.sql +sqlclean facts [--dialect ] [--max-function-call-depth ] < query.sql ``` ```bash @@ -26,6 +26,7 @@ echo "SELECT o.order_id, COUNT(*) AS items FROM orders AS o JOIN order_items AS | Flag | Description | |------|-------------| | `--dialect ` | The dialect to parse with (default: `generic`); see [Dialects](../configuration.md#dialects) | +| `--max-function-call-depth ` | The deepest function-call nesting to parse, from `1` to `512` (default: `256`); pass your `max_function_call_depth` setting so `facts` reads what `check` reads | `facts` reads no configuration file and no paths. @@ -75,4 +76,4 @@ facts with `line`, `column` and `text` added to every node. See | Code | Meaning | |------|---------| | `0` | The facts were printed | -| `2` | The SQL does not parse under the dialect, or the dialect is unknown | +| `2` | The SQL does not parse under the dialect, nests function calls deeper than the limit, or the dialect is unknown | diff --git a/crates/sqlclean-cli/skill/references/docs/configuration.md b/crates/sqlclean-cli/skill/references/docs/configuration.md index 2686101..7c78dec 100644 --- a/crates/sqlclean-cli/skill/references/docs/configuration.md +++ b/crates/sqlclean-cli/skill/references/docs/configuration.md @@ -21,6 +21,7 @@ applies. dialect = "duckdb" # default "generic" include = ["**/*.sql"] exclude = ["vendor/**"] +max_function_call_depth = 256 # nested function calls a file may hold [[dialects]] # a different dialect for some paths paths = ["warehouse/**"] @@ -71,6 +72,7 @@ directory is the root. | `include` | `["**/*.sql"]` | Glob patterns of the files to read | | `exclude` | `[]` | Glob patterns of files never to read | | `[[dialects]]` | none | `paths` and `dialect`: a different dialect for matching files | +| `max_function_call_depth` | `256` | The deepest function-call nesting SQLClean parses, from `1` to `512`; a file nested deeper is a `SQLCLEAN999` finding and is not formatted | | `[format] line_width` | `100` | The line width the formatter fills; see [Line width](formatting.md#line-width) | | `[format] indent` | `4` | Spaces per indent step, from `1` to `8`; see [Indent](formatting.md#indent) | | `[rules] select` | Rules enabled by default | Rule codes or prefixes to run | diff --git a/crates/sqlclean-cli/skill/references/docs/embedding.md b/crates/sqlclean-cli/skill/references/docs/embedding.md index 4a600a2..df67cd0 100644 --- a/crates/sqlclean-cli/skill/references/docs/embedding.md +++ b/crates/sqlclean-cli/skill/references/docs/embedding.md @@ -90,6 +90,7 @@ the dialect. Offsets in requests, findings, edits and facts count Unicode charac | `ignored_rules` | `[]` | Rule codes or prefixes to skip | | `max_literal_length` | `100` | The `SQLCLEAN044` limit | | `max_ranking_order_by` | `5` | The `SQLCLEAN043` limit | +| `max_function_call_depth` | `256` | Function calls nested deeper than this are a parse error that names this setting; at most `512` | | `config_file` | `sqlclean.toml` | The configuration file name that remediation text refers to | | `long_literal_guidance` | SQLClean's advice | The advice `SQLCLEAN044` gives before its `max_literal_length` setting, so a host can point at its own way to share a value | | `externally_referenced_ctes` | `[]` | CTE names that something outside the SQL reads, so `SQLCLEAN005` does not report them | @@ -150,6 +151,9 @@ println!("{}", format_json(request)?); `indent` sets the spaces per indent step. Without it the engine indents two spaces, its layout before the `sqlclean` command chose four; pass `"indent": 4` to match `sqlclean format`. +`max_function_call_depth` bounds nested function calls as it does in a lint request: `256` by +default, at most `512`. + When the formatter cannot format the SQL, `formatted` is `false`, `sql` is the input unchanged, and `reason` says why. diff --git a/crates/sqlclean-cli/src/checking/_helpers/builtin.rs b/crates/sqlclean-cli/src/checking/_helpers/builtin.rs index 58c9b30..5cefe3b 100644 --- a/crates/sqlclean-cli/src/checking/_helpers/builtin.rs +++ b/crates/sqlclean-cli/src/checking/_helpers/builtin.rs @@ -21,7 +21,7 @@ use crate::positions::models::Edit; pub(crate) fn builtin_findings( file: &SourceFile, codes: &BTreeSet, - thresholds: Thresholds, + (thresholds, max_function_call_depth): (Thresholds, usize), ) -> Vec { let request = LintRequest { version: LINT_API_VERSION, @@ -30,6 +30,7 @@ pub(crate) fn builtin_findings( enabled_rules: Some(codes.iter().cloned().collect()), max_literal_length: Some(thresholds.max_literal_length), max_ranking_order_by: Some(thresholds.max_ranking_order_by), + max_function_call_depth: Some(max_function_call_depth), config_file: Some(CONFIG_FILE_NAME.to_owned()), ..LintRequest::default() }; diff --git a/crates/sqlclean-cli/src/checking/_helpers/custom.rs b/crates/sqlclean-cli/src/checking/_helpers/custom.rs index 693a66f..f9c0f12 100644 --- a/crates/sqlclean-cli/src/checking/_helpers/custom.rs +++ b/crates/sqlclean-cli/src/checking/_helpers/custom.rs @@ -80,12 +80,13 @@ pub(crate) fn custom_findings( if misses.is_empty() { return Ok(findings); } + let depth: usize = evaluation.config.max_function_call_depth; let mut host_files: Vec = pending .into_par_iter() - .map(|(file, missing)| host_file(file, missing)) + .map(|(file, missing)| host_file(file, missing, depth)) .collect::>()?; if !missing_repository.is_empty() { - host_files = with_every_file(files, host_files)?; + host_files = with_every_file(files, host_files, depth)?; } let results: Vec = evaluate_on_hosts(evaluation, options, host_files, missing_repository)?; @@ -188,8 +189,9 @@ fn file_key( ]) } -fn host_file(file: &SourceFile, rules: Vec) -> Result { - let facts = file_facts(&file.text, &file.dialect)?; +/// The file as a Rule host reads it, its facts parsed within the configured call-depth limit. +fn host_file(file: &SourceFile, rules: Vec, depth: usize) -> Result { + let facts = file_facts(&file.text, &file.dialect, Some(depth))?; if let Some(reason) = facts.unavailable { return Err(reason); } @@ -206,10 +208,11 @@ fn host_file(file: &SourceFile, rules: Vec) -> Result fn with_every_file( files: &[&SourceFile], mut host_files: Vec, + depth: usize, ) -> Result, String> { for file in files { if !host_files.iter().any(|host| host.path == file.path) { - host_files.push(host_file(file, Vec::new())?); + host_files.push(host_file(file, Vec::new(), depth)?); } } host_files.sort_by(|left, right| left.path.cmp(&right.path)); diff --git a/crates/sqlclean-cli/src/checking/_helpers/orchestration.rs b/crates/sqlclean-cli/src/checking/_helpers/orchestration.rs index 0a2ee67..3d2f55f 100644 --- a/crates/sqlclean-cli/src/checking/_helpers/orchestration.rs +++ b/crates/sqlclean-cli/src/checking/_helpers/orchestration.rs @@ -137,8 +137,9 @@ fn run_builtins(run: &BuiltinRun<'_>, mut cache: ResultCache) -> (Vec, .collect(); let selection: String = codes.iter().cloned().collect::>().join(","); let thresholds = run.config.rules.thresholds; + let depth: usize = run.config.max_function_call_depth; let limits: String = format!( - "{}:{}", + "{}:{}:{depth}", thresholds.max_literal_length, thresholds.max_ranking_order_by ); let keys: Vec = run @@ -160,7 +161,9 @@ fn run_builtins(run: &BuiltinRun<'_>, mut cache: ResultCache) -> (Vec, .files .par_iter() .zip(cached) - .map(|(file, cached)| cached.unwrap_or_else(|| builtin_findings(file, &codes, thresholds))) + .map(|(file, cached)| { + cached.unwrap_or_else(|| builtin_findings(file, &codes, (thresholds, depth))) + }) .collect(); for ((file, key), found) in run.files.iter().zip(&keys).zip(&computed) { cache.put(key, Some(&file.path), found); diff --git a/crates/sqlclean-cli/src/command/_helpers/facts_command.rs b/crates/sqlclean-cli/src/command/_helpers/facts_command.rs index 325eb80..3104996 100644 --- a/crates/sqlclean-cli/src/command/_helpers/facts_command.rs +++ b/crates/sqlclean-cli/src/command/_helpers/facts_command.rs @@ -3,16 +3,26 @@ use std::io::{Read, Write}; use sqlclean::sql_facts::main::file_facts::file_facts; +use sqlclean::sql_lint::constants::MAX_CALL_DEPTH_CEILING; use crate::command::constants::EXIT_SUCCESS; use crate::command::models::FactsArgs; pub(crate) fn run(arguments: &FactsArgs) -> Result { + if !(1..=MAX_CALL_DEPTH_CEILING).contains(&arguments.max_function_call_depth) { + return Err(format!( + "--max-function-call-depth must be between 1 and {MAX_CALL_DEPTH_CEILING}" + )); + } let mut sql = String::new(); std::io::stdin() .read_to_string(&mut sql) .map_err(|error| error.to_string())?; - let facts = file_facts(&sql, &arguments.dialect)?; + let facts = file_facts( + &sql, + &arguments.dialect, + Some(arguments.max_function_call_depth), + )?; let json: String = serde_json::to_string(&facts).map_err(|error| error.to_string())?; let _ = writeln!(std::io::stdout(), "{json}"); Ok(EXIT_SUCCESS) diff --git a/crates/sqlclean-cli/src/command/_helpers/format_command.rs b/crates/sqlclean-cli/src/command/_helpers/format_command.rs index 034f1b0..a78f823 100644 --- a/crates/sqlclean-cli/src/command/_helpers/format_command.rs +++ b/crates/sqlclean-cli/src/command/_helpers/format_command.rs @@ -18,7 +18,17 @@ pub(crate) fn run(config: &Config, arguments: &FormatArgs) -> Result let (mut changed, mut refused, mut unavailable): (usize, usize, usize) = (0, 0, 0); let results: Vec> = files .par_iter() - .map(|file| format_source(&file.text, &file.dialect, config.line_width, config.indent)) + .map(|file| { + format_source( + &file.text, + &file.dialect, + ( + config.line_width, + config.indent, + config.max_function_call_depth, + ), + ) + }) .collect(); for (file, result) in files.iter().zip(results) { let formatted: String = match result { diff --git a/crates/sqlclean-cli/src/command/models.rs b/crates/sqlclean-cli/src/command/models.rs index 72ff5f0..02fd236 100644 --- a/crates/sqlclean-cli/src/command/models.rs +++ b/crates/sqlclean-cli/src/command/models.rs @@ -3,6 +3,7 @@ use std::path::PathBuf; use clap::{Args, Parser, Subcommand}; +use sqlclean::sql_lint::constants::MAX_FUNCTION_CALL_DEPTH; use crate::reporting::models::OutputFormat; @@ -94,6 +95,9 @@ pub struct RulesArgs { pub struct FactsArgs { #[arg(long, default_value = "generic")] pub dialect: String, + /// Most nested function calls the parser follows, from 1 to 512. + #[arg(long, default_value_t = MAX_FUNCTION_CALL_DEPTH)] + pub max_function_call_depth: usize, } #[derive(Debug, Args)] diff --git a/crates/sqlclean-cli/src/config/_helpers/file_schema.rs b/crates/sqlclean-cli/src/config/_helpers/file_schema.rs index e4607d2..cfae759 100644 --- a/crates/sqlclean-cli/src/config/_helpers/file_schema.rs +++ b/crates/sqlclean-cli/src/config/_helpers/file_schema.rs @@ -11,6 +11,7 @@ use crate::config::models::{DialectOverride, RuleException, RuleIgnore, Threshol pub(crate) struct ConfigFile { pub(crate) dialect: Option, pub(crate) include: Option>, + pub(crate) max_function_call_depth: Option, #[serde(default)] pub(crate) exclude: Vec, #[serde(default)] diff --git a/crates/sqlclean-cli/src/config/_helpers/loading.rs b/crates/sqlclean-cli/src/config/_helpers/loading.rs index 53d4491..5fa23ae 100644 --- a/crates/sqlclean-cli/src/config/_helpers/loading.rs +++ b/crates/sqlclean-cli/src/config/_helpers/loading.rs @@ -3,6 +3,8 @@ use std::fs; use std::path::{Path, PathBuf}; +use sqlclean::sql_lint::constants::MAX_FUNCTION_CALL_DEPTH; + use crate::config::_helpers::file_schema::ConfigFile; use crate::config::_helpers::validation::validate; use crate::config::constants::{ @@ -52,6 +54,9 @@ fn effective_config(root: PathBuf, file: ConfigFile) -> Config { dialects: file.dialects, line_width: file.format.line_width.unwrap_or(DEFAULT_LINE_WIDTH), indent: file.format.indent.unwrap_or(DEFAULT_INDENT), + max_function_call_depth: file + .max_function_call_depth + .unwrap_or(MAX_FUNCTION_CALL_DEPTH), rules: RulesConfig { select: file.rules.select, ignore: file.rules.ignore, diff --git a/crates/sqlclean-cli/src/config/_helpers/validation.rs b/crates/sqlclean-cli/src/config/_helpers/validation.rs index 190a703..83ea7f6 100644 --- a/crates/sqlclean-cli/src/config/_helpers/validation.rs +++ b/crates/sqlclean-cli/src/config/_helpers/validation.rs @@ -1,6 +1,7 @@ //! Checks that need more than the schema: positive limits, valid globs, dialects and reasons. use globset::Glob; +use sqlclean::sql_lint::constants::MAX_CALL_DEPTH_CEILING; use sqlclean::sql_lint::main::validate_dialect::validate_dialect; use crate::config::constants::MAX_INDENT; @@ -22,6 +23,11 @@ pub(crate) fn validate(config: &Config) -> Result<(), String> { "[format] indent must be between 1 and {MAX_INDENT}" )); } + if !(1..=MAX_CALL_DEPTH_CEILING).contains(&config.max_function_call_depth) { + return Err(format!( + "max_function_call_depth must be between 1 and {MAX_CALL_DEPTH_CEILING}" + )); + } let thresholds = config.rules.thresholds; if thresholds.max_literal_length == 0 || thresholds.max_ranking_order_by == 0 { return Err( diff --git a/crates/sqlclean-cli/src/config/models.rs b/crates/sqlclean-cli/src/config/models.rs index 0db827f..ef353e7 100644 --- a/crates/sqlclean-cli/src/config/models.rs +++ b/crates/sqlclean-cli/src/config/models.rs @@ -17,6 +17,8 @@ pub struct Config { pub line_width: usize, /// Spaces per indent step in formatted SQL. pub indent: usize, + /// Function calls nested deeper than this make a file unparseable. + pub max_function_call_depth: usize, pub rules: RulesConfig, } diff --git a/crates/sqlclean-cli/src/config/tests/test_load_config.rs b/crates/sqlclean-cli/src/config/tests/test_load_config.rs index a676b48..2890258 100644 --- a/crates/sqlclean-cli/src/config/tests/test_load_config.rs +++ b/crates/sqlclean-cli/src/config/tests/test_load_config.rs @@ -9,13 +9,14 @@ fn given_a_config_file_when_loading_from_below_then_the_nearest_one_applies() -> let test_cases = [ LoadedConfigTestCase { description: "values set in a parent directory apply and unset thresholds keep defaults", - config_text: "dialect = \"duckdb\"\n[format]\nline_width = 80\n[rules]\nselect = [\"SQLCLEAN004\"]\n[rules.thresholds]\nmax_literal_length = 40\n", + config_text: "dialect = \"duckdb\"\nmax_function_call_depth = 300\n[format]\nline_width = 80\n[rules]\nselect = [\"SQLCLEAN004\"]\n[rules.thresholds]\nmax_literal_length = 40\n", start: "models/orders", expected_dialect: "duckdb", expected_line_width: 80, expected_select: Some(&["SQLCLEAN004"]), expected_max_literal_length: 40, expected_max_ranking_order_by: 5, + expected_max_function_call_depth: 300, }, LoadedConfigTestCase { description: "an empty file gives the defaults", @@ -26,6 +27,7 @@ fn given_a_config_file_when_loading_from_below_then_the_nearest_one_applies() -> expected_select: None, expected_max_literal_length: 100, expected_max_ranking_order_by: 5, + expected_max_function_call_depth: 256, }, ]; for test_case in &test_cases { @@ -70,6 +72,11 @@ fn given_a_config_file_when_loading_from_below_then_the_nearest_one_applies() -> "{}", test_case.description ); + assert_eq!( + config.max_function_call_depth, test_case.expected_max_function_call_depth, + "{}", + test_case.description + ); } Ok(()) } @@ -102,6 +109,16 @@ fn given_invalid_values_when_loading_then_each_is_rejected_with_its_reason() -> config_text: "[format]\nindent = 0\n", expected_error: "indent must be between 1 and 8", }, + RejectedConfigTestCase { + description: "a function-call depth above SQLQuack's ceiling", + config_text: "max_function_call_depth = 513\n", + expected_error: "max_function_call_depth must be between 1 and 512", + }, + RejectedConfigTestCase { + description: "a function-call depth of zero", + config_text: "max_function_call_depth = 0\n", + expected_error: "max_function_call_depth must be between 1 and 512", + }, ]; for test_case in &test_cases { let directory = tempfile::tempdir().map_err(|error| error.to_string())?; diff --git a/crates/sqlclean-cli/src/config/tests/test_types.rs b/crates/sqlclean-cli/src/config/tests/test_types.rs index 1115689..4f0f266 100644 --- a/crates/sqlclean-cli/src/config/tests/test_types.rs +++ b/crates/sqlclean-cli/src/config/tests/test_types.rs @@ -7,6 +7,7 @@ pub(crate) struct LoadedConfigTestCase { pub(crate) expected_select: Option<&'static [&'static str]>, pub(crate) expected_max_literal_length: usize, pub(crate) expected_max_ranking_order_by: usize, + pub(crate) expected_max_function_call_depth: usize, } pub(crate) struct RejectedConfigTestCase { diff --git a/crates/sqlclean-cli/src/fixing/_helpers/passes.rs b/crates/sqlclean-cli/src/fixing/_helpers/passes.rs index ae818de..b7f2614 100644 --- a/crates/sqlclean-cli/src/fixing/_helpers/passes.rs +++ b/crates/sqlclean-cli/src/fixing/_helpers/passes.rs @@ -27,7 +27,7 @@ pub(crate) fn fix_sources( while passes < MAX_FIX_PASSES { let outcome: CheckOutcome = check_sources(config, &files, options)?; let edits: BTreeMap> = select_edits(&outcome.findings, unsafe_fixes); - let applied: usize = apply_pass(&mut files, &edits); + let applied: usize = apply_pass(&mut files, &edits, config.max_function_call_depth); if applied == 0 { break; } @@ -36,7 +36,15 @@ pub(crate) fn fix_sources( } let mut format_refusals: Vec<(String, String)> = Vec::new(); for file in &mut files { - match format_source(&file.text, &file.dialect, config.line_width, config.indent) { + match format_source( + &file.text, + &file.dialect, + ( + config.line_width, + config.indent, + config.max_function_call_depth, + ), + ) { Ok(Formatted::Sql(formatted)) => file.text = formatted, Ok(Formatted::Unavailable(_)) => {} Err(reason) => format_refusals.push((file.path.clone(), reason)), @@ -52,8 +60,12 @@ pub(crate) fn fix_sources( }) } -/// Apply each file's edits unless they turn parsing SQL into SQL that no longer parses. -fn apply_pass(files: &mut [SourceFile], edits: &BTreeMap>) -> usize { +/// Apply each file's edits unless they turn parsing SQL into SQL that no longer parses within the call-depth limit. +fn apply_pass( + files: &mut [SourceFile], + edits: &BTreeMap>, + depth: usize, +) -> usize { let mut applied: usize = 0; for file in files.iter_mut() { let Some(file_edits) = edits.get(&file.path) else { @@ -64,8 +76,8 @@ fn apply_pass(files: &mut [SourceFile], edits: &BTreeMap>) -> continue; } if let (Ok(_), Err(_)) = ( - file_facts(&file.text, &file.dialect), - file_facts(&fixed, &file.dialect), + file_facts(&file.text, &file.dialect, Some(depth)), + file_facts(&fixed, &file.dialect, Some(depth)), ) { continue; } diff --git a/crates/sqlclean-cli/src/formatting/main/format_source.rs b/crates/sqlclean-cli/src/formatting/main/format_source.rs index b59439d..b6be5c9 100644 --- a/crates/sqlclean-cli/src/formatting/main/format_source.rs +++ b/crates/sqlclean-cli/src/formatting/main/format_source.rs @@ -12,11 +12,10 @@ use crate::formatting::models::Formatted; pub fn format_source( text: &str, dialect: &str, - line_width: usize, - indent: usize, + (line_width, indent, max_function_call_depth): (usize, usize, usize), ) -> Result { let characters: Vec = text.chars().collect(); - let Some(mut boundaries) = statement_ends(text, dialect)? else { + let Some(mut boundaries) = statement_ends(text, dialect, Some(max_function_call_depth))? else { return Ok(Formatted::Unavailable(format!( "SQL formatting is unavailable for the {dialect} dialect: SQLQuack does not parse it yet" ))); @@ -29,7 +28,11 @@ pub fn format_source( for end in boundaries { let chunk: String = characters[start..end].iter().collect(); start = end; - let formatted: String = match format_statement(chunk.trim(), dialect, line_width, indent)? { + let formatted: String = match format_statement( + chunk.trim(), + dialect, + (line_width, indent, max_function_call_depth), + )? { Formatted::Sql(sql) => sql, unavailable @ Formatted::Unavailable(_) => return Ok(unavailable), }; @@ -51,8 +54,7 @@ pub fn format_source( fn format_statement( sql: &str, dialect: &str, - line_width: usize, - indent: usize, + (line_width, indent, max_function_call_depth): (usize, usize, usize), ) -> Result { let request = FormatRequest { version: LINT_API_VERSION, @@ -60,6 +62,7 @@ fn format_statement( dialect: dialect.to_owned(), line_width: Some(line_width), indent: Some(indent), + max_function_call_depth: Some(max_function_call_depth), ..FormatRequest::default() }; let response: FormatResponse = format_requests(vec![request]) diff --git a/crates/sqlclean/src/sql_facts/_helpers/collection.rs b/crates/sqlclean/src/sql_facts/_helpers/collection.rs index f0eb0b1..c9cfd01 100644 --- a/crates/sqlclean/src/sql_facts/_helpers/collection.rs +++ b/crates/sqlclean/src/sql_facts/_helpers/collection.rs @@ -21,7 +21,11 @@ use crate::sql_lint::main::lint::lint; use crate::sql_lint::main::parse_sql::parse_sql; use crate::sql_lint::types::{LintRequest, LintResponse, ParsedSql}; -pub(crate) fn file_facts(sql: &str, dialect: &str) -> Result { +pub(crate) fn file_facts( + sql: &str, + dialect: &str, + max_function_call_depth: Option, +) -> Result { if let Some(reason) = unavailable(dialect)? { return Ok(FileFacts { statements: Vec::new(), @@ -33,7 +37,7 @@ pub(crate) fn file_facts(sql: &str, dialect: &str) -> Result tokens, statements, .. }, ranges, - ) = parsed_statements(sql, dialect)?; + ) = parsed_statements(sql, dialect, max_function_call_depth)?; let characters: Vec = sql.chars().collect(); let snowflake: bool = dialect.eq_ignore_ascii_case("snowflake"); let facts: Vec = statements @@ -59,14 +63,18 @@ pub(crate) fn file_facts(sql: &str, dialect: &str) -> Result }) } -pub(crate) fn statement_ends(sql: &str, dialect: &str) -> Result>, String> { +pub(crate) fn statement_ends( + sql: &str, + dialect: &str, + max_function_call_depth: Option, +) -> Result>, String> { if TSQL_DIALECTS.contains(&dialect.to_ascii_lowercase().as_str()) { return Ok(Some(sqlserver_statement_ends(sql))); } if unavailable(dialect)?.is_some() { return Ok(None); } - let (_, ranges) = parsed_statements(sql, dialect)?; + let (_, ranges) = parsed_statements(sql, dialect, max_function_call_depth)?; Ok(Some( ranges.iter().map(|range| range.terminated_end).collect(), )) @@ -104,12 +112,13 @@ fn unavailable(dialect: &str) -> Result, String> { })) } -/// Parse `sql` and split its tokens into one range per parsed statement. +/// Parse `sql` within the call-depth limit and split its tokens into one range per parsed statement. fn parsed_statements( sql: &str, dialect: &str, + max_function_call_depth: Option, ) -> Result<(ParsedSql, Vec), String> { - let parsed: ParsedSql = parse_sql(sql, dialect)?; + let parsed: ParsedSql = parse_sql(sql, dialect, max_function_call_depth)?; let ranges: Vec = statement_token_ranges(&parsed.tokens); if ranges.len() != parsed.statements.len() { return Err(format!( diff --git a/crates/sqlclean/src/sql_facts/main/file_facts.rs b/crates/sqlclean/src/sql_facts/main/file_facts.rs index 0b3824b..3e9e8a6 100644 --- a/crates/sqlclean/src/sql_facts/main/file_facts.rs +++ b/crates/sqlclean/src/sql_facts/main/file_facts.rs @@ -2,7 +2,11 @@ use crate::sql_facts::models::FileFacts; -/// Each statement's structural facts, or why they are unavailable for a dialect SQLQuack does not parse yet. -pub fn file_facts(sql: &str, dialect: &str) -> Result { - crate::sql_facts::_helpers::collection::file_facts(sql, dialect) +/// Each statement's structural facts within a call-depth limit (None: the default), or why they are unavailable for a dialect SQLQuack does not parse yet. +pub fn file_facts( + sql: &str, + dialect: &str, + max_function_call_depth: Option, +) -> Result { + crate::sql_facts::_helpers::collection::file_facts(sql, dialect, max_function_call_depth) } diff --git a/crates/sqlclean/src/sql_facts/main/statement_ends.rs b/crates/sqlclean/src/sql_facts/main/statement_ends.rs index adeddc4..99ea37f 100644 --- a/crates/sqlclean/src/sql_facts/main/statement_ends.rs +++ b/crates/sqlclean/src/sql_facts/main/statement_ends.rs @@ -1,6 +1,10 @@ //! Where each statement of one SQL file ends, without collecting its facts. -/// The end of each statement of `sql`, after its terminator; `None` when SQLQuack does not parse `dialect` yet. -pub fn statement_ends(sql: &str, dialect: &str) -> Result>, String> { - crate::sql_facts::_helpers::collection::statement_ends(sql, dialect) +/// The end of each statement of `sql`, after its terminator, parsed within a call-depth limit (None: the default); `None` when SQLQuack does not parse `dialect` yet. +pub fn statement_ends( + sql: &str, + dialect: &str, + max_function_call_depth: Option, +) -> Result>, String> { + crate::sql_facts::_helpers::collection::statement_ends(sql, dialect, max_function_call_depth) } diff --git a/crates/sqlclean/src/sql_facts/tests/test_file_facts.rs b/crates/sqlclean/src/sql_facts/tests/test_file_facts.rs index bf0138e..633a03a 100644 --- a/crates/sqlclean/src/sql_facts/tests/test_file_facts.rs +++ b/crates/sqlclean/src/sql_facts/tests/test_file_facts.rs @@ -97,7 +97,7 @@ FROM recent AS r LEFT JOIN customers AS c ON r.customer_id = c.customer_id", }, ]; for test_case in &test_cases { - let facts: FileFacts = file_facts(test_case.sql, "duckdb")?; + let facts: FileFacts = file_facts(test_case.sql, "duckdb", None)?; let statement: &StatementFacts = &facts.statements[test_case.statement]; let located = |start: usize, end: usize| text(test_case.sql, start, end); assert_eq!( @@ -245,7 +245,7 @@ fn given_comma_and_nested_joins_when_collecting_postgres_facts_then_joins_read_a }, ]; for test_case in &test_cases { - let facts: FileFacts = file_facts(test_case.sql, "postgres")?; + let facts: FileFacts = file_facts(test_case.sql, "postgres", None)?; let joins: Vec<(&str, &str, bool, String)> = facts.statements[0] .joins .iter() @@ -286,7 +286,7 @@ fn given_sql_text_when_collecting_facts_then_only_unparseable_sql_is_an_error() }, ]; for test_case in &test_cases { - let statements: Option = file_facts(test_case.sql, "duckdb") + let statements: Option = file_facts(test_case.sql, "duckdb", None) .map(|facts| facts.statements.len()) .ok(); assert_eq!( @@ -323,7 +323,7 @@ fn given_sql_text_when_finding_statement_ends_then_each_ends_after_its_terminato ]; for test_case in &test_cases { let characters: Vec = test_case.sql.chars().collect(); - let statements: Option> = statement_ends(test_case.sql, "duckdb") + let statements: Option> = statement_ends(test_case.sql, "duckdb", None) .ok() .flatten() .map(|ends| { @@ -368,8 +368,9 @@ fn given_a_dialect_sqlquack_does_not_parse_when_collecting_facts_then_they_are_u }, ]; for test_case in &test_cases { - let facts: FileFacts = file_facts("SELECT 1; SELECT 2", test_case.dialect)?; - let ends: Option> = statement_ends("SELECT 1; SELECT 2", test_case.dialect)?; + let facts: FileFacts = file_facts("SELECT 1; SELECT 2", test_case.dialect, None)?; + let ends: Option> = + statement_ends("SELECT 1; SELECT 2", test_case.dialect, None)?; assert_eq!( facts.unavailable.as_deref(), test_case.expected_unavailable, diff --git a/crates/sqlclean/src/sql_lint/_helpers/formatter.rs b/crates/sqlclean/src/sql_lint/_helpers/formatter.rs index e071c3a..35de174 100644 --- a/crates/sqlclean/src/sql_lint/_helpers/formatter.rs +++ b/crates/sqlclean/src/sql_lint/_helpers/formatter.rs @@ -1,5 +1,8 @@ use rayon::iter::{IntoParallelIterator, ParallelIterator}; use sqlquack_format::{FormatDialect, FormatOptions}; +use sqlquack_parse::parsing::constants::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::Dialect as SqlQuackDialect; use crate::constants::INTERPOLATION_PLACEHOLDER_PREFIX; @@ -79,19 +82,31 @@ fn format_with_sqlquack( dialect: FormatDialect, macros: Option, ) -> Result { + let max_function_call_depth: usize = checked_call_depth( + request + .max_function_call_depth + .unwrap_or(MAX_FUNCTION_CALL_DEPTH), + ) + .map_err(|reason| format!("max_function_call_depth: {reason}"))?; let options = FormatOptions { line_width: request.line_width, indent: request.indent.unwrap_or(DEFAULT_INDENT), token_widths: request.token_widths, parse: sqlquack_syntax::ParseOptions { complexity_guard: Some(sqlquack_syntax::ComplexityGuardOptions { - max_function_call_depth: request.max_function_call_depth, + max_function_call_depth: Some(max_function_call_depth), ..Default::default() }), }, ..FormatOptions::new(dialect) }; - let sql = format_around_cte_macros(&request.sql, macros, &options)?; + let sql = format_around_cte_macros(&request.sql, macros, &options).map_err(|reason| { + call_depth_hint( + reason, + max_function_call_depth, + Some("max_function_call_depth"), + ) + })?; if request.sql.contains(INTERPOLATION_PLACEHOLDER_PREFIX) && sql != request.sql && format_around_cte_macros(&sql, macros, &options)? != sql diff --git a/crates/sqlclean/src/sql_lint/constants.rs b/crates/sqlclean/src/sql_lint/constants.rs index d66908d..cc0f25e 100644 --- a/crates/sqlclean/src/sql_lint/constants.rs +++ b/crates/sqlclean/src/sql_lint/constants.rs @@ -1,4 +1,10 @@ pub const LINT_API_VERSION: u32 = 1; +/// Nested function calls a request accepts by default. +pub const MAX_FUNCTION_CALL_DEPTH: usize = + sqlquack_parse::parsing::constants::MAX_FUNCTION_CALL_DEPTH; +/// The most nested function calls a request may allow. +pub const MAX_CALL_DEPTH_CEILING: usize = + sqlquack_parse::parsing::constants::MAX_CALL_DEPTH_CEILING; /// The dialect name SQLQuack lays out as Snowflake. pub(crate) const SNOWFLAKE_DIALECT: &str = "snowflake"; /// The dialect name SQLQuack lays out as DuckDB. diff --git a/crates/sqlclean/src/sql_lint/main/parse_sql.rs b/crates/sqlclean/src/sql_lint/main/parse_sql.rs index 70bf461..1dbe474 100644 --- a/crates/sqlclean/src/sql_lint/main/parse_sql.rs +++ b/crates/sqlclean/src/sql_lint/main/parse_sql.rs @@ -2,7 +2,11 @@ use crate::sql_lint::types::ParsedSql; -/// Parse `sql` under `dialect` with SQLQuack, rejecting statements the parser kept as opaque text. -pub fn parse_sql(sql: &str, dialect: &str) -> Result { - sqlquack_lint::parse_sql(sql, dialect) +/// Parse `sql` under `dialect` within a call-depth limit (None: the default), rejecting statements the parser kept as opaque text. +pub fn parse_sql( + sql: &str, + dialect: &str, + max_function_call_depth: Option, +) -> Result { + sqlquack_lint::parse_sql(sql, dialect, max_function_call_depth) } diff --git a/crates/sqlclean/src/sql_lint/tests.rs b/crates/sqlclean/src/sql_lint/tests.rs index cc7e710..83592bb 100644 --- a/crates/sqlclean/src/sql_lint/tests.rs +++ b/crates/sqlclean/src/sql_lint/tests.rs @@ -10,6 +10,8 @@ mod empty_fixture_lint; mod format_corpus; #[path = "tests/test_function_call_spacing.rs"] mod function_call_spacing; +#[path = "tests/test_function_depth.rs"] +mod function_depth; #[path = "tests/helpers.rs"] mod helpers; #[path = "tests/test_layout_rules.rs"] @@ -18,6 +20,8 @@ mod layout_rules; mod line_wrap; #[path = "tests/test_lint_exemptions.rs"] mod lint_exemptions; +#[path = "tests/test_named_windows.rs"] +mod named_windows; #[path = "tests/test_set_operation_formatter.rs"] mod set_operation_formatter; #[path = "tests/test_sql_lint.rs"] diff --git a/crates/sqlclean/src/sql_lint/tests/test_function_depth.rs b/crates/sqlclean/src/sql_lint/tests/test_function_depth.rs new file mode 100644 index 0000000..da448b9 --- /dev/null +++ b/crates/sqlclean/src/sql_lint/tests/test_function_depth.rs @@ -0,0 +1,114 @@ +use serde_json::{Value, json}; + +use crate::sql_lint::main::engine::lint_json; +use crate::sql_lint::main::formatter::format_json; +use crate::sql_lint::tests::{helpers, test_types}; + +#[test] +fn given_nested_function_calls_when_linting_then_the_requested_or_default_depth_bounds_them() { + let test_cases = [ + test_types::FunctionDepthTestCase { + description: "the default depth of 256 parses", + depth: 256, + max_function_call_depth: None, + expected_error: None, + }, + test_types::FunctionDepthTestCase { + description: "one call past the default fails and names the setting", + depth: 257, + max_function_call_depth: None, + expected_error: Some("configured limit 256; raise max_function_call_depth, up to 512"), + }, + test_types::FunctionDepthTestCase { + description: "a requested depth bounds the parse", + depth: 65, + max_function_call_depth: Some(64), + expected_error: Some("configured limit 64; raise max_function_call_depth"), + }, + test_types::FunctionDepthTestCase { + description: "a requested depth above the default lets deeper calls parse", + depth: 300, + max_function_call_depth: Some(300), + expected_error: None, + }, + ]; + for test_case in test_cases { + let request = json!({ + "version": 1, + "sql": helpers::nested_function_sql(test_case.depth), + "dialect": "snowflake", + "max_function_call_depth": test_case.max_function_call_depth, + }); + let outcome: Result = lint_json(&request.to_string()); + let error: String = outcome.clone().err().unwrap_or_default(); + let diagnostics: Value = outcome + .ok() + .and_then(|response| serde_json::from_str::(&response).ok()) + .map_or_else(|| json!([]), |response| response["diagnostics"].clone()); + assert_eq!( + !error.is_empty(), + test_case.expected_error.is_some(), + "{}: {error}", + test_case.description + ); + assert!( + error.contains(test_case.expected_error.unwrap_or_default()), + "{}: {error}", + test_case.description + ); + assert_eq!(diagnostics, json!([]), "{}", test_case.description); + } +} + +#[test] +fn given_nested_function_calls_when_formatting_then_the_requested_or_default_depth_bounds_them() { + let test_cases = [ + test_types::FunctionDepthTestCase { + description: "the default depth of 256 formats", + depth: 256, + max_function_call_depth: None, + expected_error: None, + }, + test_types::FunctionDepthTestCase { + description: "one call past the default is declined, naming the setting that raises it", + depth: 257, + max_function_call_depth: None, + expected_error: Some("configured limit 256; raise max_function_call_depth, up to 512"), + }, + test_types::FunctionDepthTestCase { + description: "a requested depth bounds the formatter", + depth: 65, + max_function_call_depth: Some(64), + expected_error: Some("configured limit 64"), + }, + test_types::FunctionDepthTestCase { + description: "a requested depth above the most allowed is declined", + depth: 2, + max_function_call_depth: Some(513), + expected_error: Some("max_function_call_depth"), + }, + ]; + for test_case in test_cases { + let request = json!({ + "version": 1, + "sql": helpers::nested_function_sql(test_case.depth), + "dialect": "snowflake", + "max_function_call_depth": test_case.max_function_call_depth, + }); + let response: Value = + serde_json::from_str(&format_json(&request.to_string()).expect("a format response")) + .expect("JSON"); + let reason: &str = response["reason"].as_str().unwrap_or_default(); + assert_eq!( + response["formatted"] == json!(false), + test_case.expected_error.is_some(), + "{}: {reason}", + test_case.description + ); + assert!( + reason.contains(test_case.expected_error.unwrap_or_default()), + "{}: {reason}", + test_case.description + ); + } +} diff --git a/crates/sqlclean/src/sql_lint/tests/test_named_windows.rs b/crates/sqlclean/src/sql_lint/tests/test_named_windows.rs new file mode 100644 index 0000000..d5484cb --- /dev/null +++ b/crates/sqlclean/src/sql_lint/tests/test_named_windows.rs @@ -0,0 +1,57 @@ +use crate::sql_lint::tests::{helpers, test_types}; + +/// Databricks keeps a WINDOW clause written after a parenthesized query on the subquery, so a ranking call naming that window is still checked. +#[test] +fn given_databricks_window_after_parentheses_when_linting_then_ranking_rules_read_it() +-> Result<(), String> { + let test_cases = [ + test_types::NamedWindowRankingTestCase { + 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, o.d, o.e, o.f)", + rule: "SQLCLEAN043", + expected_anchors: &["ROW_NUMBER"], + }, + test_types::NamedWindowRankingTestCase { + description: "the same with a WITH before the parentheses", + 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, o.d, o.e, o.f)", + rule: "SQLCLEAN043", + expected_anchors: &["ROW_NUMBER"], + }, + test_types::NamedWindowRankingTestCase { + description: "an ordering without a declared 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", + expected_anchors: &["ROW_NUMBER"], + }, + test_types::NamedWindowRankingTestCase { + description: "a ranking sort within the cap in a WINDOW after the parentheses is clean", + sql: "(SELECT o.order_id, ROW_NUMBER() OVER w AS rn FROM orders AS o) WINDOW w AS (ORDER BY o.a)", + rule: "SQLCLEAN043", + expected_anchors: &[], + }, + ]; + + for test_case in &test_cases { + let diagnostics = + helpers::diagnostics_for_dialect(test_case.sql, "databricks", &[test_case.rule])?; + let anchors: Vec = diagnostics + .iter() + .map(|diagnostic| { + let start = diagnostic["start"].as_u64().unwrap_or_default() as usize; + let end = diagnostic["end"].as_u64().unwrap_or_default() as usize; + test_case + .sql + .chars() + .skip(start) + .take(end - start) + .collect() + }) + .collect(); + assert_eq!( + anchors, test_case.expected_anchors, + "{}", + test_case.description + ); + } + Ok(()) +} diff --git a/crates/sqlclean/src/sql_lint/tests/test_sql_lint.rs b/crates/sqlclean/src/sql_lint/tests/test_sql_lint.rs index 8c727aa..bdacb58 100644 --- a/crates/sqlclean/src/sql_lint/tests/test_sql_lint.rs +++ b/crates/sqlclean/src/sql_lint/tests/test_sql_lint.rs @@ -5,52 +5,6 @@ use crate::sql_lint::tests::helpers::{fixture_harness, format_batch_json}; use crate::sql_lint::tests::{helpers, test_types}; use crate::sql_tokens::main::query_fingerprint::query_fingerprint; -#[test] -fn given_production_function_depth_when_linting_then_bounded_parser_accepts_it() { - let test_cases = [test_types::FunctionDepthTestCase { - description: "production depth remains supported", - depth: 65, - expected_diagnostic_count: 0, - }]; - for test_case in test_cases { - let sql = helpers::nested_function_sql(test_case.depth); - let diagnostics = helpers::diagnostics(&sql).unwrap_or_else(|error| { - panic!("{}: {error}", test_case.description); - }); - assert_eq!( - diagnostics.len(), - test_case.expected_diagnostic_count, - "{}", - test_case.description - ); - } -} - -#[test] -fn given_excessive_function_depth_when_linting_then_complexity_guard_rejects_it() { - let test_cases = [test_types::FunctionDepthFailureTestCase { - description: "excessive depth remains bounded", - depth: 129, - expected_code: "E_GUARD_FUNCTION_NESTING_DEPTH_EXCEEDED", - expected_limit: "configured limit 128", - }]; - for test_case in test_cases { - let sql = helpers::nested_function_sql(test_case.depth); - let error = - helpers::diagnostics(&sql).expect_err("excessive nested calls should remain bounded"); - assert!( - error.contains(test_case.expected_code), - "{}", - test_case.description - ); - assert!( - error.contains(test_case.expected_limit), - "{}", - test_case.description - ); - } -} - #[test] fn given_sql_cases_when_linting_then_diagnostics_match() -> Result<(), String> { let test_cases = [ diff --git a/crates/sqlclean/src/sql_lint/tests/test_types.rs b/crates/sqlclean/src/sql_lint/tests/test_types.rs index 6b20224..7a5c785 100644 --- a/crates/sqlclean/src/sql_lint/tests/test_types.rs +++ b/crates/sqlclean/src/sql_lint/tests/test_types.rs @@ -88,6 +88,13 @@ pub(crate) struct DialectLintRuleTestCase { pub expected_reason: &'static str, } +pub(crate) struct NamedWindowRankingTestCase { + pub description: &'static str, + pub sql: &'static str, + pub rule: &'static str, + pub expected_anchors: &'static [&'static str], +} + pub(crate) struct LineWrapTestCase { pub description: &'static str, pub sql: &'static str, @@ -135,14 +142,9 @@ pub(crate) struct InvalidRuleTestCase { pub(crate) struct FunctionDepthTestCase { pub description: &'static str, pub depth: usize, - pub expected_diagnostic_count: usize, -} - -pub(crate) struct FunctionDepthFailureTestCase { - pub description: &'static str, - pub depth: usize, - pub expected_code: &'static str, - pub expected_limit: &'static str, + pub max_function_call_depth: Option, + /// The limit the parse error names; `None` when the SQL parses. + pub expected_error: Option<&'static str>, } pub(crate) struct CrossJoinTestCase { diff --git a/tests/e2e/src/sqlclean/hosting/_test_types.py b/tests/e2e/src/sqlclean/hosting/_test_types.py index 484009e..299295c 100644 --- a/tests/e2e/src/sqlclean/hosting/_test_types.py +++ b/tests/e2e/src/sqlclean/hosting/_test_types.py @@ -20,6 +20,29 @@ class CheckTestCase: command: str = "check" +@dataclass(frozen=True) +class CallDepthTestCase: + """A configuration and the findings checking 300 nested function calls in relation must give.""" + + description: str + config: str + expected_codes: tuple[str, ...] + expected_message: str + relation: str = "a" + files: dict[str, str] = field(default_factory=dict) + + +@dataclass(frozen=True) +class CallDepthFormatTestCase: + """A configuration and what formatting 300 nested function calls twice must give.""" + + description: str + config: str + expected_exits: tuple[int, int] + expected_start: str + expected_message: str + + @dataclass(frozen=True) class RewriteTestCase: """A project, a fix or format invocation and the file contents it must leave.""" diff --git a/tests/e2e/src/sqlclean/hosting/test_sqlclean_command.py b/tests/e2e/src/sqlclean/hosting/test_sqlclean_command.py index 8edac9d..f962963 100644 --- a/tests/e2e/src/sqlclean/hosting/test_sqlclean_command.py +++ b/tests/e2e/src/sqlclean/hosting/test_sqlclean_command.py @@ -10,6 +10,8 @@ from tests.e2e.src.sqlclean.hosting._test_types import ( CacheTestCase, + CallDepthFormatTestCase, + CallDepthTestCase, CheckTestCase, IdempotencyTestCase, ManyFilesTestCase, @@ -24,6 +26,7 @@ DUCKDB: str = 'dialect = "duckdb"\n' MYSQL_FILES: str = DUCKDB + '[[dialects]]\npaths = ["mysql/**"]\ndialect = "mysql"\n' CROSS_JOIN_ONLY: str = DUCKDB + '[rules]\nselect = ["SQLCLEAN038"]\n' +SNOWFLAKE_CROSS_JOIN_ONLY: str = 'dialect = "snowflake"\n[rules]\nselect = ["SQLCLEAN038"]\n' CUSTOM: str = ( DUCKDB + '[rules]\nselect = ["XSQLCLEANNAME", "XSQLCLEANSTYLE"]\ncustom = ["sql_rules"]\n' ) @@ -179,6 +182,96 @@ def test_given_project_when_checking_then_reports_findings_and_exit_status( assert tuple(file["path"] for file in report["unavailable"]) == test_case.expected_unavailable +@pytest.mark.parametrize( + "test_case", + [ + CallDepthTestCase( + description="the default limit refuses 300 nested calls and names the key that raises it", + config=SNOWFLAKE_CROSS_JOIN_ONLY, + expected_codes=("SQLCLEAN999",), + expected_message="raise max_function_call_depth, up to 512", + ), + CallDepthTestCase( + description="max_function_call_depth in sqlclean.toml lets 300 nested calls parse", + config="max_function_call_depth = 300\n" + SNOWFLAKE_CROSS_JOIN_ONLY, + expected_codes=(), + expected_message="", + ), + CallDepthTestCase( + description="custom Rules read 300 nested calls under max_function_call_depth = 300", + config="max_function_call_depth = 300\n" + CUSTOM, + expected_codes=("XSQLCLEANNAME001",), + expected_message="forbidden prefix", + relation="tmp_a", + files={"sql_rules/naming.py": SAMPLE_RULES}, + ), + ], + ids=lambda case: case.description, +) +def test_given_deeply_nested_calls_when_checking_then_the_configured_depth_bounds_parsing( + tmp_path: Path, test_case: CallDepthTestCase +) -> None: + nested: str = "lower(" * 300 + "name" + ")" * 300 + write_project( + tmp_path, + config=test_case.config, + files={ + "models/q.sql": f"SELECT {nested} AS n FROM {test_case.relation}\n", + **test_case.files, + }, + ) + + completed: subprocess.CompletedProcess[str] = run( + tmp_path, "check", "--output-format", "json", environment={} + ) + assert completed.returncode in (0, 1), completed.stderr + findings: list[dict[str, object]] = json.loads(completed.stdout)["findings"] + + assert tuple(finding["code"] for finding in findings) == test_case.expected_codes, findings + assert all(test_case.expected_message in str(finding["message"]) for finding in findings) + + +@pytest.mark.parametrize( + "test_case", + [ + CallDepthFormatTestCase( + description="the default limit leaves 300 nested calls as written and names the key", + config=DUCKDB, + expected_exits=(1, 1), + expected_start="select lower(lower(", + expected_message="raise max_function_call_depth, up to 512", + ), + CallDepthFormatTestCase( + description="max_function_call_depth = 300 formats 300 nested calls, and they stay formatted", + config="max_function_call_depth = 300\n" + DUCKDB, + expected_exits=(0, 0), + expected_start="SELECT\n LOWER(\n LOWER(\n", + expected_message="", + ), + ], + ids=lambda case: case.description, +) +def test_given_deeply_nested_calls_when_formatting_then_the_configured_depth_bounds_parsing( + tmp_path: Path, test_case: CallDepthFormatTestCase +) -> None: + nested: str = "lower(" * 300 + "name" + ")" * 300 + write_project( + tmp_path, + config=test_case.config, + files={"models/q.sql": f"select {nested} as n from a\n"}, + ) + + formatted: subprocess.CompletedProcess[str] = run(tmp_path, "format", environment={}) + rechecked: subprocess.CompletedProcess[str] = run(tmp_path, "format", "--check", environment={}) + text: str = (tmp_path / "models/q.sql").read_text(encoding="utf-8") + + assert (formatted.returncode, rechecked.returncode) == test_case.expected_exits, ( + formatted.stderr + ) + assert text.startswith(test_case.expected_start), text[:200] + assert test_case.expected_message in formatted.stdout + formatted.stderr + + @pytest.mark.parametrize( "test_case", [ diff --git a/website/src/content/docs/docs/cli/facts.mdx b/website/src/content/docs/docs/cli/facts.mdx index 8e29b53..3427f10 100644 --- a/website/src/content/docs/docs/cli/facts.mdx +++ b/website/src/content/docs/docs/cli/facts.mdx @@ -12,7 +12,7 @@ so use it to see what a Rule will read before writing one. ## Usage ```bash -sqlclean facts [--dialect ] < query.sql +sqlclean facts [--dialect ] [--max-function-call-depth ] < query.sql ``` ```bash @@ -25,6 +25,7 @@ echo "SELECT o.order_id, COUNT(*) AS items FROM orders AS o JOIN order_items AS | Flag | Description | |------|-------------| | `--dialect ` | The dialect to parse with (default: `generic`); see [Dialects](/docs/configuration/#dialects) | +| `--max-function-call-depth ` | The deepest function-call nesting to parse, from `1` to `512` (default: `256`); pass your `max_function_call_depth` setting so `facts` reads what `check` reads | `facts` reads no configuration file and no paths. @@ -74,4 +75,4 @@ facts with `line`, `column` and `text` added to every node. See | Code | Meaning | |------|---------| | `0` | The facts were printed | -| `2` | The SQL does not parse under the dialect, or the dialect is unknown | +| `2` | The SQL does not parse under the dialect, nests function calls deeper than the limit, or the dialect is unknown | diff --git a/website/src/content/docs/docs/configuration.mdx b/website/src/content/docs/docs/configuration.mdx index 4e8b08e..fb73454 100644 --- a/website/src/content/docs/docs/configuration.mdx +++ b/website/src/content/docs/docs/configuration.mdx @@ -10,6 +10,7 @@ applies. dialect = "duckdb" # default "generic" include = ["**/*.sql"] exclude = ["vendor/**"] +max_function_call_depth = 256 # nested function calls a file may hold [[dialects]] # a different dialect for some paths paths = ["warehouse/**"] @@ -60,6 +61,7 @@ directory is the root. | `include` | `["**/*.sql"]` | Glob patterns of the files to read | | `exclude` | `[]` | Glob patterns of files never to read | | `[[dialects]]` | none | `paths` and `dialect`: a different dialect for matching files | +| `max_function_call_depth` | `256` | The deepest function-call nesting SQLClean parses, from `1` to `512`; a file nested deeper is a `SQLCLEAN999` finding and is not formatted | | `[format] line_width` | `100` | The line width the formatter fills; see [Line width](/docs/formatting/#line-width) | | `[format] indent` | `4` | Spaces per indent step, from `1` to `8`; see [Indent](/docs/formatting/#indent) | | `[rules] select` | Rules enabled by default | Rule codes or prefixes to run | diff --git a/website/src/content/docs/docs/embedding.mdx b/website/src/content/docs/docs/embedding.mdx index bbe6d23..99462ec 100644 --- a/website/src/content/docs/docs/embedding.mdx +++ b/website/src/content/docs/docs/embedding.mdx @@ -76,6 +76,7 @@ the dialect. Offsets in requests, findings, edits and facts count Unicode charac | `ignored_rules` | `[]` | Rule codes or prefixes to skip | | `max_literal_length` | `100` | The `SQLCLEAN044` limit | | `max_ranking_order_by` | `5` | The `SQLCLEAN043` limit | +| `max_function_call_depth` | `256` | Function calls nested deeper than this are a parse error that names this setting; at most `512` | | `config_file` | `sqlclean.toml` | The configuration file name that remediation text refers to | | `long_literal_guidance` | SQLClean's advice | The advice `SQLCLEAN044` gives before its `max_literal_length` setting, so a host can point at its own way to share a value | | `externally_referenced_ctes` | `[]` | CTE names that something outside the SQL reads, so `SQLCLEAN005` does not report them | @@ -136,6 +137,9 @@ println!("{}", format_json(request)?); `indent` sets the spaces per indent step. Without it the engine indents two spaces, its layout before the `sqlclean` command chose four; pass `"indent": 4` to match `sqlclean format`. +`max_function_call_depth` bounds nested function calls as it does in a lint request: `256` by +default, at most `512`. + When the formatter cannot format the SQL, `formatted` is `false`, `sql` is the input unchanged, and `reason` says why.