Skip to content

fix(analysis): one 256 function-call depth for Polyglot and SQLQuack's shared parsers - #1596

Merged
kvlonge merged 5 commits into
mainfrom
fix/call-depth
Nov 19, 2026
Merged

kvlonge merged 5 commits into
mainfrom
fix/call-depth

Conversation

@kvlonge

@kvlonge kvlonge commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Why

A Snowflake SQL macro whose body nests 65 REPLACE calls failed to compile with P015: the native Snowflake parser used SQLQuack's default 64-call guard. Other parse paths set 128, 512 or no limit, so the same SQL passed one command and failed another.

Changes

  • sqlbuild_analysis::parse_guard holds one MAX_FUNCTION_CALL_DEPTH of 256 (SQLQuack's own guarded depth). Every Polyglot parse and SQLQuack's shared and Snowflake parsers take their options from it; Python's Polyglot proxy reads the same constant from the native module.
  • SQLQuack's per-dialect parser crates (BigQuery, ClickHouse, Databricks, DuckDB, PostgreSQL, SQLite, T-SQL) take no options and keep their own built-in limits. They are a documented exclusion until SQLQuack accepts options for them.
  • Semantic validation always applies the shared guard; query enrichment sets it as a typed option; two pass-throughs that could not fail are gone.
  • make check-ci gains check-parse-guard, which fails on library-default options, unguarded option literals (including ParseOptions { .. }), bare Polyglot parses, parse_data_type without options, or a local depth.
  • Built-in model rules now parse SQL as deeply nested as the compiler accepts; SQLClean rules keep their own budget.
  • The four root checks (parse guard, native stub, native layering, release gate) share one runner.

Verification

  • Tests parse 256 nested calls with each guarded parser on a 2 MiB thread and check one more names the limit; a Snowflake macro nested past it fails with P015 naming the limit 256.
  • Scanner unit tests cover each flagged pattern and the per-dialect exclusion.
  • cargo tests and clippy for touched crates, make check-ci; one review plus follow-up.

A Snowflake SQL macro whose body nests 65 REPLACE calls failed to compile
with P015 "E_GUARD_FUNCTION_NESTING_DEPTH_EXCEEDED: value 65 exceeds
configured limit 64". The native Snowflake module, which parses macro
bodies and models since v0.208.0, passed SQLQuack's default options, so
the library's 64-call guard applied. Other parse paths set their own
limit (128 in query facts and Polyglot query analysis, 512 elsewhere) or
none, so the same SQL passed one command and failed another.

Every Polyglot and SQLQuack parse now takes its options from
sqlbuild_analysis::parse_guard, whose one MAX_FUNCTION_CALL_DEPTH is 256,
as SQLQuack's own guarded parse uses. Python's Polyglot proxy reads the
same constant from the native module. Both parsers grow their stack on
demand and keep their parser-depth (1,024) and AST-depth (512) limits;
tests parse 256 nested calls with each guarded parser on a 2 MiB thread
and check one more names the limit. A macro nested past it still fails
with P015 naming the model, both lines and limit 256.

`make check-ci` gains check-parse-guard, which fails when a crate source
parses with a library default: ParseOptions::default(), an option
literal without complexity_guard, a bare Polyglot parse, or a local
depth.

SQLClean rules parse inside sqlquack-lint with its own limit of 128,
which SQLBuild cannot set.
The parse guard, native stub, native layering and release gate checks
each parsed --root, listed their errors under a heading and printed a
success line in the same way. They now share run_root_check and keep
only what they check and the messages they print.
…e its gaps

The shared 256 function-call depth covers every Polyglot parse and
SQLQuack's shared and Snowflake parsers. SQLQuack's per-dialect parser
crates (BigQuery, ClickHouse, Databricks, DuckDB, PostgreSQL, SQLite and
T-SQL) take no options and keep their own built-in limits, so they are a
documented exclusion until SQLQuack accepts options for them. The module
docs and the check's messages now say so instead of every SQL parse.

- Semantic validation always applies the shared guard; a request's own
  guard no longer overrides it.
- Query enrichment sets the typed guard on its analysis options instead
  of round-tripping it through JSON.
- The guarded_parse_options and proxy_parse_options pass-throughs, which
  could not fail, are gone; callers use polyglot_parse_options directly.
- The parse-guard check also flags a ParseOptions literal without a
  guard and a parse_data_type call without options.
@kvlonge
kvlonge enabled auto-merge November 19, 2026 12:23
@kvlonge
kvlonge added this pull request to the merge queue Nov 19, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 19, 2026
…e compiler

The built-in model rules' query facts now share the compiler's 256
function-call depth, so SQL nested 130 calls deep, once beyond their
128 budget, is evaluated. The unevaluated-resource case for SQBRMODEL101
becomes a case that the rule evaluates it; SQLClean rules keep their own
budget and their unevaluated cases stay.
@kvlonge
kvlonge enabled auto-merge November 19, 2026 13:53
@kvlonge
kvlonge added this pull request to the merge queue Nov 19, 2026
Merged via the queue into main with commit e4276d1 Nov 19, 2026
34 checks passed
@kvlonge
kvlonge deleted the fix/call-depth branch November 19, 2026 15:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant