`max_parse_depth` boundary differs between the Python and Rust parsers
Summary
The max_parse_depth DoS guard trips at different nesting levels depending on which parser is active. The Python parser and the Rust (sqlfluffrs) parser measure parse depth differently, so the same configured max_parse_depth rejects at a different point under each engine.
The divergence is in the safe direction — Rust is always the more permissive of the two — so the default engine never rejects input that pure Python accepts. But the configured limit is not honoured identically, and the gap is not constant, so the boundary cannot be reconciled by re-tuning the default.
Reproduce
With the Rust extension installed (sqlfluffrs) and core.use_rust_parser = auto (the default), sweep max_parse_depth for nested brackets:
def accepts(sql, use_rust, limit):
config = FluffConfig(overrides={
"dialect": "ansi", "use_rust_parser": use_rust,
"max_parse_nodes": 0, "max_parse_depth": limit,
})
return not any(
v.rule_code() == "PRS" and "Maximum parse depth exceeded" in v.desc()
for v in Linter(config=config).parse_string(sql).violations
)At the default max_parse_depth = 600:
- Python rejects from ~35 nested brackets.
- Rust rejects from ~40 nested brackets.
Measurements
Smallest max_parse_depth at which each engine accepts (i.e. peak tracked depth), nesting depth 25:
| Shape | Python | Rust |
|---|---|---|
| nested brackets | 453 | 394 |
| nested functions | 665 | 557 |
| nested subqueries | 680 | 570 |
| bracketed arithmetic | 528 | 469 |
flat 1+1+… |
56 | 47 |
Fitted against nesting depth d (brackets): Python ≈ 53 + 16d, Rust ≈ 44 + 14d. The per-level rate varies by construct (functions/subqueries diverge ~2.4×/level versus Rust), so a fixed offset or scale factor cannot align the two engines.
Root cause
- Python (
src/sqlfluff/core/parser/context.py) incrementsmatch_depthon everydeeper_match()context. There are 15 call sites, and several are incidental to traversal rather than grammar structure — notably one per segment match (segments/base.py) on top of the grammar reference (grammar/base.py), so each segment reference is counted twice, plus algorithm wrappers (Bracket,GreedyUntil,Bracketed-StartBracket/-EndBracket,Trim-Greedy*,Delimited-Term). - Rust (
sqlfluffrs/sqlfluffrs_parser/src/parser/table_driven/iterative.rs) checks the table-driven frame-stack length. ARefframe descends into the referenced grammar; there is no frame for the segment itself.
Impact
Rust is never stricter, so the default engine does not produce false rejections relative to Python. The observable difference is the reverse: switching to pure Python (use_rust_parser = False) lowers the effective nesting ceiling for the same config. The existing tests use inputs far over the limit (120 brackets at limit 100), so the boundary itself is uncovered.
Possible fixes
- Remove Python's incidental extra depth entries so depth means "grammar-node nesting" in both engines, then recalibrate the default (touches
ParseContextand its 15 call sites; moves a user-facing default). - Mirror Python's accounting in Rust via a dedicated depth counter, recorded as a parity invariant in
table_driven/parity.rs(audit of ~36 descend sites).
Both are estimated at 2–3 focused days including validation. An interim safeguard has been opened as a PR: a differential test that pins only the safe direction (Rust not stricter than Python) across a shape matrix, so the direction cannot silently flip.
Related
- #7805 (closed) — the T-SQL nested-function case that motivated raising the default 255 → 600. This is the same guard, reported from the engine-parity angle.
Source: sqlfluff/sqlfluff