Skip to content

fix(partiql): raise the nesting caps to the depths the service reads - #1428

Open
LeeroyHannigan wants to merge 1 commit into
mainfrom
fix/885-nesting-caps
Open

LeeroyHannigan wants to merge 1 commit into
mainfrom
fix/885-nesting-caps

Conversation

@LeeroyHannigan

@LeeroyHannigan LeeroyHannigan commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

What

The PartiQL parser refuses fewer statements as nested too deep, up to the depths DynamoDB reads:

  • The group cap (MAX_GROUP_DEPTH) is 2000, from 1000. Parentheses round an operand nest as a group only, so they no longer count against the operand cap.
  • The operand cap (MAX_OPERAND_DEPTH) is 500, from 100.
  • The parser reads a run of NOT, CASE or PIVOT words before an operand in a loop. The answer is the last word's, as before, and the run costs no operand level.
  • The parser counts the calls and the + and - operators of the SET values, apart from those of the condition. Past 300 it refuses the statement with the existing operator count text. The count is separate because DynamoDB runs 300 nested calls in a SET value with three more operators in the WHERE condition.
  • LARGE_STACK_BYTES, the stack of the thread that parses a deep statement, is 128 MiB, from 64 MiB.

The code:

  • crates/core/src/partiql/parser/limits.rs: the caps, the stack size, count_value_operator, and the module comment with the stack figures.
  • crates/core/src/partiql/parser/operands.rs: operand_in_parentheses nests as a group, and keeps its own part of the condition's walk, so a = 1 + (b - c) still names the +.
  • crates/core/src/partiql/parser/terms.rs: the loop over a run of NOT, CASE and PIVOT words.
  • crates/core/src/partiql/parser/walk.rs and parser.rs: the counter, and its refusal at the head of the SET part.

No error text is new.

Why

DynamoDB was measured in us-east-1 on 24 November 2026 with ExecuteStatement, each statement twice. The SELECT statements named a table that does not exist. The UPDATE statements went to a table that does not exist, and to a table that exists. On the second table they named a key that no item has and a false condition, so no statement wrote an item. #885 gives the rows of 6 November.

RNFE stands for ResourceNotFoundException, CCF for ConditionalCheckFailedException. Both mean that the statement passed every check. On main the caps refuse each of these statements with Statement wasn't well formed, can't be processed: expression nesting depth exceeded.

Statement DynamoDB
SELECT * FROM <T> WHERE pk = 'q' AND a = 2000 parentheses round b RNFE both times
SELECT * FROM <T> WHERE pk = 'q' AND 2000 parentheses round a, then = 1 RNFE both times
1500 or 1800 parentheses round b RNFE once, a 500 once
UPDATE <T> SET x = 300 nested if_not_exists calls RNFE on the missing table, CCF on the table that exists
the same with 301 or 440 calls The expression contains too many operators; operator count: 301, on both tables

Testing done

  • cargo test -p extenddb-core --lib --no-fail-fast: 1147 passed, 1 ignored. cargo test -p extenddb-engine --lib --no-fail-fast: 871 passed. One backup restore test failed once under load. It passed in the next full run and in 5 runs alone, and this PR does not touch backup code. cargo +1.97.0 clippy -p extenddb-core -p extenddb-engine --all-targets -- -D warnings and cargo +1.97.0 fmt --all -- --check are clean.
  • A control ran the changed tests on the production code before each change, and the rows of that change failed.
  • A release build on SQLite answered 23 of the 29 rows of 24 November as DynamoDB does. It answered 9 of the 15 rows on the two tables as DynamoDB does. tests/test_partiql_grammar.py, tests/test_partiql_write.py and tests/test_partiql_limits.py against that build: 1414 passed, 3 skipped.
  • 200,001 random statements went through the parser on main and with this PR. 196 answers differ, and main refuses all 196 as nested too deep.
  • Stack, on a debug build: the deepest statement within the length limit takes 26.9 MB of the parse thread. The 128 MiB stack is about 5 times that. Release takes 8.0 MB. The engine runs the deepest SET value the parser accepts, 300 operators, in at most 987 KB of stack, on a 2 MiB request thread.

Production is +106 -43 lines, 29 and 6 of them code. The tests are +227 -36.

The rows that still differ from DynamoDB:

Conflicts with the open PRs that change the parser:

Closes #885

Checklist

  • I have read CONTRIBUTING.md
  • All tests pass (cargo test --workspace)
  • Code is formatted (cargo fmt --check)
  • Clippy is clean (cargo clippy -- -W clippy::pedantic)
  • I have added or updated tests for new functionality
  • I have updated documentation if behavior changed
  • Breaking changes are noted below (if any)
  • If this changes the wire protocol, Storage trait, auth model, on-disk
    format, or public CLI surface, an RFC has been accepted or is linked
    below. Otherwise, an ADR captures the decision (link below).

ADR / RFC: n/a


By submitting this pull request, I confirm that my contribution is made under
the terms of the Apache License 2.0 and I agree to the Developer Certificate of
Origin (DCO). See CONTRIBUTING.md for details.

Parentheses round an operand nest as a group only. The parser still
refuses a WHERE condition such as a = 1 + (b - c) for the +. It reads a
run of NOT, CASE or PIVOT words before an operand in a loop.

The group cap is 2000 and the operand cap 500. The service read 2000
levels of parentheses and 300 nested calls on 2026-11-24.

The parser counts the calls and the + and - operators of the SET values
apart from the condition. Past 300 it refuses the statement with the
operator count text, which the service gives for 301 nested calls.

The parse thread's stack is 128 MiB, about five times what the deepest
statement within the length limit uses on a debug build.

Closes #885

@demohan demohan left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The raised caps are safe on the stack at 622684c, but one new count is reported ahead of refusals the service reports first, so ten statements main answered as the service does now answer otherwise. The deepest statement under the length limit needs 27,136 KB of stack in a debug build and 6,272 KB in release, about 4.7 times below the 128 MiB parse thread in debug, 48 other forms at the limit need no more, and debug and release servers on their default 2 MiB request threads answered every deepest statement and stayed up. fmt, clippy, the core and engine tests and the 1.91.1 check are clean, CI is green on 64 of 64, ten of fourteen negative controls fail as they should, and the branch merges with main at 222d80a and builds and passes there. No later merged PR fixes #885.

I will approve once the SET values' operator count comes after their other refusals, the deep statements' cost is bounded, and wire tests hold the order and the caps.

crates/core/src/partiql/parser/limits/tests.rs:1

No wire test was added, and the three PartiQL wire files pass the same on main, so none of them holds the change. Four controls pass where they should fail: the count moved after the SET refusals, LARGE_STACK_BYTES back to 64 MiB, the !in_where guard dropped, and the first word's answer kept in a NOT, CASE or PIVOT run. The "983 KB ... engine's translation included" comment does not hold on a table that exists: the engine refuses 151 or more nested calls there.

};
refusal = refusal.or(values);
refusal = refusal
.or_else(|| (self.value_operators > MAX_OPERATORS).then(too_many_operators))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The SET values' operator count is reported before the SET values' other refusals. With 301 flat xN = a + 1 assignments beside any of 33 nested lists, y = -a, 1e200, 1e126, y = ? with no parameter, size(a), <<1,'a'>>, "" = 1 or 33 nested maps, the service in us-east-1 (twice each) and main give the other refusal, and this head answers with the operator count of 301, on SQLite and PostgreSQL. Counting after the SET value refusals and before the table lookup would match. The body's "main refuses all 196 differing statements as nested too deep" does not cover these.

/// Parenthesized groups a condition, an operand, a value or an IN member may
/// nest. The service read 2000 levels round a name and round a path; the
/// depths it was measured at are in `measured_depths` in the tests.
pub const MAX_GROUP_DEPTH: usize = 2000;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The service runs more than this. It read 2001 to 2700 groups round a name and round a condition, and 2500 round a path, a SET value, an INSERT value and an IN member, and fails with InternalFailure only from 2800 (3000 for the second set), so the rows at cap+1 in the tests pin a refusal the service does not give. A cap nearer the measured depths, or a differences row for the gap, would say it. Separately, the service read 700 nested size calls in parentheses six times of six, and 1000 levels and 1355 plain calls, so the comment at lines 79-80 and 93-95 and measured_depths do not hold; + and - both count (line 236), and for b = 1 + (c - d) the service names the +, as this head does (walk/tests.rs:140).

/// 2026-11-05: `SET x = ('a', 'b')` stores a list). The parentheses nest
/// as a group and not as an operand: the service reads 1001 levels of
/// them round a name, a string and a SET value (measured 2026-11-24).
fn operand_in_parentheses(&mut self) -> PResult<Operand> {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each level of an operand in parentheses rescans forward through list_in_parentheses_ahead (predicates.rs:252), so the parse is quadratic in the depth, and this makes 2000 levels reachable: one statement at 2000 levels takes 13 ms, and a 100-statement ExecuteTransaction takes 1.18 to 1.20 s against 123 to 146 ms for the deepest one main accepts, with the parse holding a runtime worker meanwhile. Deciding the list once per group would keep it linear.

@LJMart11 LJMart11 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed at 622684c. 2 findings: 1 Important, 1 Docs.

The change raises the PartiQL parser's group cap to 2000 and its operand cap to 500, nests an operand in parentheses as a group only, reads runs of NOT, CASE and PIVOT in a loop, and counts the calls and +/- of the SET values against the 300-operator limit. Confirmed: the SET-value count matches the service when it is the only refusal (300 xN = a + 1 assignments give ConditionalCheckFailedException and 301 give operator count: 301, on the service twice each and on a SQLite server from this head; 150 + 151 across two values gives 301 on both); the NOT/CASE/PIVOT loop gives main's answer on 26 mixed runs in a WHERE condition and a SET value; a = 1 + (b - c) still names the +. Mutations of the count, the call and arithmetic counting, the parentheses hunk and the word loop each fail a test in limits/tests.rs or walk/tests.rs. The head merged with main builds under 1.94.0 and passes the core (1242) and engine (978) lib tests. The core (1147) and engine (871) lib tests pass at the head under 1.91.1.

  • Important crates/core/src/partiql/parser/walk.rs:351 — The SET-value operator count is reported ahead of the number, function, nesting and parameter refusals the service reports first, so UPDATEs main answered as the service does now get operator count: 301.
  • Docs docs/manuals/07-upgrade-manual.md:385 — After an upgrade, an UPDATE whose SET values hold more than 300 calls and +/- in total, such as 301 xN = a + 1 assignments, is refused with the operator count text where v1.1.0 ran it, and neither Behavior Changes by Release nor the PR body says so.

Docs — outside the diff

docs/manuals/07-upgrade-manual.md:385
Docs
After an upgrade, an UPDATE whose SET values hold more than 300 calls and +/- in total is refused (limits.rs:238-242, walk.rs:351), and Behavior Changes by Release has no entry for it. 301 assignments xN = a + 1, or 150 and 151 + in two values, get The expression contains too many operators; operator count: 301; the previous parser accepted both. The service refuses them too, but a workload that ran before now fails. The body says the parser refuses fewer statements and that main refuses all 196 differing random statements as nested too deep; neither covers this.

Fix: add a Behavior Changes entry for the SET-value operator count and list it under the body's breaking changes.

Evidence: the parser on UPDATE "tbl" SET x0 = a + 1, ..., x300 = a + 1 WHERE pk = 'q' answers OK at main and operator count: 301 here. A SQLite server from this head answers 400 ValidationException with that text, and 300 assignments get ConditionalCheckFailedException, as the service does (2 of 2 trials each).

};
refusal = refusal.or(values);
refusal = refusal
.or_else(|| (self.value_operators > MAX_OPERATORS).then(too_many_operators))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important
The SET-value operator count at walk.rs:351 comes before every other SET-value refusal, but the service reports several of those refusals ahead of the count. With 301 xN = a + 1 assignments plus one more assignment, the service answers Number overflow ... for y = 1e200, The function is not allowed in an update expression; function: size for y = size(a), Nesting Levels have exceeded supported limits under root[0]... for 33 nested lists, and Number of parameters in request and statement don't match. for y = ?. It gives those answers whether the extra assignment is written first or last. A SQLite server built from this head answers 400 ValidationException: The expression contains too many operators; operator count: 301 for all of them, on a table that exists and on one that does not. At main the parser accepts these statements, or gives the nesting text for the lists. A client that branches on the message gets a different answer, and nothing in the logs shows the reordering.

The service orders the unary - arity refusal by where it is written. y = -a written after the 301 assignments gets operator count: 301, which this head matches and main does not. Written first, it gets Incorrect number of operands ...; operator or function: -, number of operands: 1. Moving the count after .or(values) matches the service for the lists and for -a written first. It breaks -a written last, and it still answers the count for 1e200, size(a), <<1,'a'>> written last and ?.

Fix: report the count after the number-range, update-function, literal-nesting, bag and parameter-count checks. Order it against the sign refusal by token position, keeping the position where the count passed 300. Pin the 12 statements below in limits/tests.rs. The rows at limits/tests.rs:513-518 (nesting_forms) and the 301/440 rows of measured_depths (:684-690) are the ones the move touches.

Evidence: walk.rs:350-352 reads refusal = refusal.or_else(|| (self.value_operators > MAX_OPERATORS).then(too_many_operators)).or(values);. Live DynamoDB, UPDATE "noarev-1428-missing" SET x0 = a + 1, ..., x300 = a + 1, y = 1e200 WHERE pk = 'q' AND zz = 'never': Number overflow. Attempting to store a number with magnitude larger than supported range under root. This head on SQLite: 400 ValidationException: The expression contains too many operators; operator count: 301. With the count moved after .or(values), cargo test -p extenddb-core --lib partiql still passes (263 tests).

This branch has not been deployed

No deployments
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.

PartiQL (#411): the parser's nesting caps refuse statements that DynamoDB runs

3 participants