Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "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" }
sqlquack-fingerprint = { git = "https://github.com/sqlstack-io/sqlquack.git", rev = "3543c055634ec2e3a0c58d732e55f570dafbe277", features = ["bigquery", "databricks", "duckdb", "postgres", "snowflake", "sqlite", "sqlserver"] }
sqlquack-format = { git = "https://github.com/sqlstack-io/sqlquack.git", rev = "3543c055634ec2e3a0c58d732e55f570dafbe277", features = ["bigquery", "databricks", "duckdb", "postgres", "snowflake", "sqlserver"] }
sqlquack-lint = { git = "https://github.com/sqlstack-io/sqlquack.git", rev = "3543c055634ec2e3a0c58d732e55f570dafbe277", features = ["bigquery", "databricks", "duckdb", "postgres", "snowflake", "sqlite"] }
sqlquack-parse = { git = "https://github.com/sqlstack-io/sqlquack.git", rev = "3543c055634ec2e3a0c58d732e55f570dafbe277" }
sqlquack-syntax = { git = "https://github.com/sqlstack-io/sqlquack.git", rev = "3543c055634ec2e3a0c58d732e55f570dafbe277" }
sqlquack-syntax-sqlserver = { git = "https://github.com/sqlstack-io/sqlquack.git", rev = "3543c055634ec2e3a0c58d732e55f570dafbe277" }

[workspace.lints.rust]
unsafe_code = "forbid"
Expand Down
15 changes: 14 additions & 1 deletion crates/sqlclean-cli/skill/references/docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Online: https://sqlclean.io/docs/configuration/

- Finding the file
- Keys
- Nesting limit
- Choosing files
- Dialects
- Validation
Expand Down Expand Up @@ -72,7 +73,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 |
| `max_function_call_depth` | `256` | The deepest function-call nesting SQLClean parses, from `1` to `512`; see [Nesting limit](#nesting-limit) |
| `[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 |
Expand All @@ -86,6 +87,18 @@ directory is the root.

Selection, exceptions and ignores are described in [Rules](rules/overview.md).

## Nesting limit

`max_function_call_depth` sets how deeply function calls may nest. A file nested deeper is a
`SQLCLEAN999` finding, and `format` leaves it as written. Below 512 the message names the key.

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.

A statement nested more than 512 levels deep is refused the same way in every dialect `check` reads,
whatever the setting and even without function calls, for example 600 `UNION ALL` operands.

## Choosing files

With no paths on the command line, a command reads every file under the configuration root whose
Expand Down
4 changes: 3 additions & 1 deletion crates/sqlclean-cli/skill/references/docs/embedding.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ println!("{}", format_json(request)?);
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`.
default. 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.

When the formatter cannot format the SQL, `formatted` is `false`, `sql` is the input unchanged,
and `reason` says why.
Expand Down
111 changes: 88 additions & 23 deletions crates/sqlclean/src/sql_facts/_helpers/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,15 @@ fn node_joins<'a>(node: &'a Expression, tokens: &[Token]) -> Vec<&'a Join> {
return Vec::new();
};
let mut joins: Vec<&Join> = Vec::new();
let mut pending: Vec<&Join> = select.joins.iter().rev().collect();
let mut pending: Vec<&Join> = select
.joins
.iter()
.chain(from_item_joins(
select.from.iter().flat_map(|from| &from.expressions),
tokens,
))
.collect();
pending.reverse();
while let Some(join) = pending.pop() {
joins.push(join);
if let Expression::JoinedTable(joined) = &join.this
Expand All @@ -251,12 +259,72 @@ fn node_joins<'a>(node: &'a Expression, tokens: &[Token]) -> Vec<&'a Join> {
joins
}

/// The joins each item of a FROM list of several items keeps with it, unless the item is written in parentheses.
fn from_item_joins<'a>(
items: impl Iterator<Item = &'a Expression>,
tokens: &[Token],
) -> impl Iterator<Item = &'a Join> {
items
.filter_map(|item| match item {
Expression::JoinedTable(joined) if !parenthesized_item(joined, tokens) => {
Some(joined.joins.iter())
}
_ => None,
})
.flatten()
}

/// Whether a FROM item is written inside one pair of parentheses, which closes after its last join.
fn parenthesized_item(joined: &JoinedTable, tokens: &[Token]) -> bool {
let (Some(first), Some(last)) = (
located_spans(&joined.left)
.chain(joined.joins.iter().flat_map(join_spans))
.map(|span| span.start)
.min(),
joined
.joins
.iter()
.flat_map(join_spans)
.map(|span| span.end)
.max(),
) else {
return false;
};
tokens
.iter()
.rposition(|token| token.span.end <= first)
.filter(|&open| tokens[open].token_type == TokenType::LParen)
.and_then(|open| closing_parenthesis(tokens, open))
.is_some_and(|close| last <= close.start)
}

/// The spans of the tables and columns a join names in its relation and its ON clause.
fn join_spans(join: &Join) -> impl Iterator<Item = Span> + '_ {
located_spans(&join.this).chain(join.on.iter().flat_map(located_spans))
}

/// The span of the `)` that closes the `(` at `open`.
fn closing_parenthesis(tokens: &[Token], open: usize) -> Option<Span> {
let mut depth: usize = 0;
for token in &tokens[open..] {
match token.token_type {
TokenType::LParen => depth += 1,
TokenType::RParen => {
depth -= 1;
if depth == 0 {
return Some(token.span);
}
}
_ => {}
}
}
None
}

/// Whether the `(` after a join group's JOIN keyword closes after everything the group names.
fn parenthesized(joined: &JoinedTable, tokens: &[Token]) -> bool {
let spans: Vec<Span> = located_spans(&joined.left)
.chain(joined.joins.iter().flat_map(|join| {
located_spans(&join.this).chain(join.on.iter().flat_map(located_spans))
}))
.chain(joined.joins.iter().flat_map(join_spans))
.collect();
let (Some(first), Some(last)) = (
spans.iter().map(|span| span.start).min(),
Expand All @@ -278,20 +346,7 @@ fn parenthesized(joined: &JoinedTable, tokens: &[Token]) -> bool {
else {
return false;
};
let mut depth: usize = 0;
for token in &tokens[open..] {
match token.token_type {
TokenType::LParen => depth += 1,
TokenType::RParen => {
depth -= 1;
if depth == 0 {
return last <= token.span.start;
}
}
_ => {}
}
}
false
closing_parenthesis(tokens, open).is_some_and(|close| last <= close.start)
}

/// The spans of the tables and columns `expression` names.
Expand All @@ -308,14 +363,24 @@ fn implicit_join_nodes(node: &Expression, tokens: &[Token], characters: &[char])
let Expression::Select(select) = node else {
return Vec::new();
};
let (Some(from), Some(first_join)) = (
select.from.as_ref(),
select.joins.first().and_then(|join| first_span(&join.this)),
) else {
let Some(from) = select.from.as_ref() else {
return Vec::new();
};
let Some(first_join) = select
.joins
.iter()
.chain(from_item_joins(from.expressions.iter(), tokens))
.filter_map(|join| first_span(&join.this))
.min_by_key(|span| span.start)
else {
return Vec::new();
};
let mut joins: Vec<JoinNode> = Vec::new();
for relation in &from.expressions {
for item in &from.expressions {
let relation: &Expression = match item {
Expression::JoinedTable(joined) if !parenthesized_item(joined, tokens) => &joined.left,
_ => item,
};
let Some(span) = first_span(relation).filter(|span| span.start > first_join.start) else {
continue;
};
Expand Down
Loading
Loading