Repository navigation
fix(mongodb): evaluate deeply nested conditions instead of answering 500 - #571
LeeroyHannigan wants to merge 9 commits into
Conversation
pushdown.rs was 635 lines. The test module moves unchanged to pushdown_tests.rs, loaded with #[path] as backup_engine.rs loads its test files, so the next change can add to the analyzer without the file passing 600 lines.
…ushing it down On a table with no index and no stream, while the server's per-process cache holds a fresh entry (60 seconds) recording that the table has no index, a pushable UpdateItem or DeleteItem condition is compiled to a MongoDB filter, one `$and`, `$or` or `$nor` level per operator. MongoDB refuses a command nested past its BSON depth limit, so a chain of about 100 terms (`#v = :v OR #v = :v OR ...`) answered InternalServerError, and only while that cache entry was fresh. Amazon DynamoDB evaluates such conditions: a 150-term OR or AND chain, OR and AND alternating 140 levels deep, and a legacy `Expected` map of 120 entries joined by OR answer ConditionalCheckFailedException when they fail and apply the write when they hold (measured 2026-10-25 at 18:34 and 19:53 UTC, each failing case twice). The analyzer now marks a condition whose AND, OR and NOT nest more than 64 levels deep as not pushable, so it takes the session path that evaluates the condition in Rust. Measured on mongo:7.0.45 through the calls the pushdown makes, with the key filter merged in, a condition nested 98 levels runs and 99 fails; with `contains` leaves, 97 runs and 98 fails.
…leteItem Dual-target wire tests from measurements on 2026-10-25 at 18:34 and 19:53 UTC: a 150-term OR or AND chain, OR and AND alternating 100 and 140 levels deep, a 120-term OR inside an AND, and a legacy `Expected` map of 120 entries joined by OR answer ConditionalCheckFailedException when they fail on an absent key, and UpdateItem and DeleteItem apply when such a condition holds. Each test creates its own table and writes to it first, so on MongoDB the conditional write runs while the cache entry that enables the pushdown is fresh. The UpdateItem row in test_expression_operator_limits.py no longer needs its expected-failure marker on MongoDB.
98fb237 to
077d435
Compare
3a929c9 to
67ca6ea
Compare
…n-depth The base branch moved from 077d435 to b9148ec. The merge includes main at c2e9a32 and the commits #558 added after 077d435. Those commits check placeholders, operand types and the Scan ranges before the operator count. They leave the count of a PartiQL statement to the PartiQL parser and fix the syntax error texts. They also move the shared test helpers to tests/expression_fault_helpers.py. tests/test_expression_operator_limits.py had the one conflict. This branch removed the MongoDB expected-failure marker from the file. The base branch moved the helpers out of the file and changed the marker to read the backend from the test configuration. The resolution takes the base branch's side. Then it removes XFAIL_ON_MONGODB_DEEP_OR, its use on test_update_item_condition_at_the_cap_is_evaluated, and the import of _is_real_dynamodb and configured_backend, which only the marker used. docs/design/13-storage-mongodb.md merged without a conflict. The marker goes because of the fix on this branch. A condition whose AND, OR and NOT nest more than 64 levels deep now takes the session path, which evaluates it in Rust. On MongoDB the 150-term UpdateItem condition of that test answers ConditionalCheckFailedException, as DynamoDB does.
…n-depth The base branch moved from b9148ec to ab59fc2. The merge includes main at 8c64333 and the commits #558 added after b9148ec. Those commits report a condition syntax error as a typed fault and check a Segment or TotalSegments range without its partner. They also move the placeholder key length tests into their own file and add the one-condition-per-key rows. Two more add the v1.1.0 upgrade manual entry and drop four re-exports of extenddb_core::expression that this branch does not use. tests/test_expression_operator_limits.py had the one conflict. This branch removed XFAIL_ON_MONGODB_DEEP_OR from the file, with its use on test_update_item_condition_at_the_cap_is_evaluated and the conftest import that only the marker used. The base branch added a second marker, XFAIL_ON_EXTENDDB_NON_KEY_CONDITION, and a range text check that calls _is_real_dynamodb. The resolution keeps the new marker and drops the MongoDB one. It imports _is_real_dynamodb again, without configured_backend, which only the MongoDB marker used. The base commits change none of the other files of this branch: crates/storage-mongodb/src/pushdown.rs, pushdown_tests.rs, docs/design/13-storage-mongodb.md and tests/test_condition_nesting_depth.py merged as they were.
demohan
left a comment
There was a problem hiding this comment.
Deep conditions on MongoDB now answer as the service does. On mongo 7.0.48, 16 condition shapes at depths 64, 65, 98, 99, 100, 140 and 149, on UpdateItem and DeleteItem with existing and absent keys, each sent twice, gave 0 differences from the service across 1,040 records: error code and message, the returned item, ConsumedCapacity and the stored item. Legacy Expected maps, PutItem, TransactWriteItems and PartiQL UPDATE and DELETE with a deep WHERE gave 0 differences across 416 more. The base answers InternalServerError at depth 98 for contains chains and at 99 for 12 of 16 shapes.
The profiler shows depth 64 still pushed down (131 levels with = leaves, 134 with contains), and depth 65 and 149 sent with no condition filter. 1,031 cases answer as SQLite does on the same data. Losing the fast path costs about 1 ms at the median on DeleteItem and nothing measurable on UpdateItem, and the debug build no longer aborts at depth 149. Each new test fails with the depth check removed, and 18 of 18 pass against the service twice. CI is green.
I will approve once the two comments below match the measurements.
PR body
Optional. The last-run section describes 9e0f0f6 with 83 tests. At this head the two files collect 80: 79 pass and 1 is #558's expected failure.
| ); | ||
| // A 150-term chain, which nested 149 `$or` levels in one filter. | ||
| assert!(!is_pushable(&or_chain(150), &maps).is_yes()); | ||
| // Depth 97, the first depth at which a chain of `contains` leaves fails |
There was a problem hiding this comment.
With the depth check removed, 98 contains terms (depth 97) answer ConditionalCheckFailedException, and the first InternalServerError is at 99 terms (depth 98). The comment calls depth 97 the first that fails, and pushdown.rs:75 and the design doc say 97 runs and 98 fails.
There was a problem hiding this comment.
Agreed. With the check removed, 98 contains terms (depth 97) run and 99 terms (depth 98) fail. 0068e0560 sends 99 terms in that assertion, and the comment names depth 98 next to the term count. The same commit pins the 64/65 boundary with 65 and 66 literal terms in place of MAX_LOGICAL_DEPTH + 1 and + 2. A build with the limit at 80 passed the old test and fails this one at the 66-term chain.
| //! - `path <lt|le|gt|ge> :v` where `:v` is `B` (base64 string ordering | ||
| //! ≠ bytewise byte ordering across mismatched lengths) | ||
| //! - Any operand type the analyzer doesn't yet classify | ||
| //! - A condition whose `AND`, `OR` and `NOT` nest more than |
There was a problem hiding this comment.
A pushed filter nested past 64 levels runs on MongoDB, up to depth 98 with equality leaves and 97 with contains. The module doc says it would pass MongoDB's BSON depth limit. 64 is the margin the code keeps, and the doc can say that.
There was a problem hiding this comment.
Agreed. The filter runs up to depth 98 with equality leaves and 97 with contains. 40bcdf2cb rewrites the module doc. It calls 64 a margin below the depth at which MongoDB refuses the compiled filter: 99 levels with equality leaves, 98 with contains leaves.
|
|
||
| """Long and deeply nested condition expressions on UpdateItem and DeleteItem. | ||
|
|
||
| Amazon DynamoDB evaluates a condition whose AND, OR and NOT chains run 150 |
There was a problem hiding this comment.
Optional. The docstring names AND, OR and NOT chains, and no row uses NOT. On this head, 64, 65, 66, 98 and 99 terms of NOT attribute_exists answer correctly.
There was a problem hiding this comment.
No row in the file uses NOT. d171511d5 names AND and OR chains in the docstring. NOT counting stays with logical_nesting_past_the_limit_is_not_pushable in pushdown_tests.rs, which checks that a NOT adds one level.
logical_nesting_past_the_limit_is_not_pushable computed both sides of the 64/65 boundary from MAX_LOGICAL_DEPTH, so a build with the limit at 80 passed every test. The assertions now use literal chains. 65 terms (64 levels) are pushed down and 66 terms (65 levels) are not. The depth checks of the NOT cases use 64 and 65. With these values the limit-80 build fails at the 66-term chain. The comment on the deepest assertion named depth 97 as the first depth at which a chain of `contains` leaves fails on MongoDB. Measured with the depth check removed, on mongo:7.0.45, a chain of equality leaves runs at depth 98 and fails at 99. A chain of `contains` leaves runs at 97 and fails at 98. The assertion now sends 99 terms (98 levels) and its comment says which of the two it counts.
…red depths The module doc of pushdown.rs gave one reason for the 64-level limit: the compiled filter would pass MongoDB's BSON depth limit. Measured, it passes that limit at 99. A filter nested 65 to 98 levels runs on MongoDB with equality leaves, and 65 to 97 with `contains` leaves. The doc now calls 64 a margin below the depth at which MongoDB refuses the compiled filter. It gives the two measured depths, 99 and 98, as the comment on MAX_LOGICAL_DEPTH does.
…ne holding table
The failing rows of tests/test_condition_nesting_depth.py checked the error
code alone. They now assert the HTTP status 400 and the Error map
{ConditionalCheckFailedException, "The conditional request failed"} by
equality. That is the answer tests/test_conditional_writes.py asserts for a
failing condition.
test_update_with_holding_condition_applies and
test_delete_with_holding_condition_applies differed in the call and the final
check. They are one six-row table, test_holding_condition_applies, with the
write function as a parameter. The file still collects 17 tests.
The docstring named AND, OR and NOT chains, and no row in the file uses NOT.
It now names AND and OR. NOT counting stays with the unit test
logical_nesting_past_the_limit_is_not_pushable.
|
Thanks for the run against the service. The body now describes the head after this round. At |
demohan
left a comment
There was a problem hiding this comment.
The items from the earlier review are resolved at d171511. On a MongoDB 7.0.48 build with the depth check removed, contains chains run at depth 97 and fail at 98, and equality, attribute_exists, map member and right-nested chains run at 98 and fail at 99, so the comment at pushdown_tests.rs:245, the module doc and section 5.2 now state where MongoDB refuses the filter. The assertion sends 99 terms, and all 8 unit mutations of the limit fail the test. The docstring names only AND and OR chains, and the PR body describes this head: the two files collect 80 tests and give 79 passed and 1 expected failure (#558's marker) on MongoDB, twice.
The fix commits change two doc lines and two test files. On MongoDB this head matches the service in us-east-1 on all 752 records of 16 condition shapes at depths 64, 65, 97, 98, 99 and 149, on UpdateItem and DeleteItem against an existing and an absent key, twice each, and on 416 records of legacy Expected maps of 65 to 301 entries, PutItem, TransactWriteItems and PartiQL UPDATE and DELETE. Depth 64 still goes to MongoDB as a filter and 65 and 98 are evaluated in the server, the log has no error 15 lines, and memory stays flat over three runs of 150-term chains and Expected maps of up to 4000 entries. The new assertion passes against the service twice and in CI on all four backends, fmt, clippy and the 1.91.1 check are clean, and CI is green on 53 of 53 checks.
Changes since the last review
61dc9c8c6merges fix(expression): operator cap, syntax error wording, PutItem map keys #558's headee1d54168. Its two commits move the parser tests toparser_tests.rsand add the upgrade manual's rollback note formax_expression_operators.0068e0560pins the 64/65 boundary inlogical_nesting_past_the_limit_is_not_pushablewith literal chains of 65 and 66 terms. The test computed them asMAX_LOGICAL_DEPTH + 1and+ 2, so a build with the limit at 80 passed it. That build now fails at the 66-term chain. The deepest assertion sends 99 terms (98 levels). Its comment names depth 98 as the first at which a chain ofcontainsleaves fails on MongoDB.40bcdf2cbrewrites the module doc ofpushdown.rs. 64 is a margin below the depth at which MongoDB refuses the compiled filter: 99 levels with equality leaves, 98 withcontainsleaves.d171511d5makes the failing rows oftests/test_condition_nesting_depth.pyassert the HTTP status 400 and the Error map by equality. It folds the UpdateItem and DeleteItem holding tests into one six-row table. The docstring names AND and OR chains, since no row uses NOT.What
On MongoDB, an UpdateItem or DeleteItem condition with about 100 nested AND or OR terms answered InternalServerError. It is now evaluated, and the request answers as DynamoDB does.
On a table with no index and no stream, the MongoDB backend has a fast path for conditional writes. It compiles the condition into a MongoDB filter and sends it with the key filter, through
find_onefor UpdateItem andfind_one_and_deletefor DeleteItem. The compiler emits one$and,$oror$norlevel per operator, and a chain such as#v = :v OR #v = :v OR ...parses left-nested, so 150 terms become 149 nested levels. MongoDB refuses a command nested past its BSON depth limit with error 15 (BSONObj exceeds maximum nested object depth). The server answered InternalServerError.The fast path runs only while the server's per-process cache holds an entry recording that the table has no index. A write on the session path creates that entry, and it lasts 60 seconds. Outside that window the same request took the session path and answered correctly.
Measured on mongo:7.0.45 through those two calls, with the key filter merged in: a condition nested 98 levels runs and 99 fails. With
containsleaves, the deepest leaf the compiler emits, 97 runs and 98 fails. Right-nested and alternating chains have the same limits. List index paths and the map member paths of #582 (item_data.m.M.k) lengthen a field name and add no nesting.is_pushablenow refuses a condition whose AND, OR and NOT nest more than 64 levels deep (MAX_LOGICAL_DEPTH). Such a condition takes the session path, which evaluates it in Rust. Every condition the analyzer does not accept already takes that path. 64 is a margin below the two measured depths. A condition loses the fast path only with 65 or more AND, OR and NOT operators along one path. The answer is the same on either path.The first commit moves the analyzer's unit tests from
pushdown.rs(635 lines) topushdown_tests.rs, unchanged, loaded with#[path]asbackup_engine.rsloads its test files. The module doc and section 5.2 ofdocs/design/13-storage-mongodb.mdlist the new condition that is not pushed down, with the measured depths.Why
Closes no issue. #558's
test_update_item_condition_at_the_cap_is_evaluatedhit the fault on MongoDB and carries an expected-failure marker for that backend. This branch sits on #558 because it removes that marker.Measured in us-east-1 on 2026-10-25 between 18:34 and 19:55 UTC, each failing case sent twice with the same answer:
Expectedmap of 120 entries joined byConditionalOperatorOR answers the same, and so does anExpectedmap of 300 or 301 entries.Tests
logical_nesting_past_the_limit_is_not_pushablechecks with literal counts that the analyzer accepts 65 terms (64 levels) and refuses 66 terms (65 levels). It checks that the analyzer refuses a 150-term chain and a 99-term chain (98 levels) whatever the limit is set to. Depth 98 is the first MongoDB refuses withcontainsleaves. It checks that NOT and the deeper side of an operator count.tests/test_condition_nesting_depth.py, 17 dual-target tests from the measured rows. 7 failing and 6 holding conditions run on UpdateItem and DeleteItem, and the legacyExpectedrows are 2 failing and 2 holding. A failing row asserts the HTTP status 400 and the Error map{ConditionalCheckFailedException, "The conditional request failed"}by equality. That is the answertests/test_conditional_writes.pyasserts. The row also checks that the item did not change. Each test creates its own table and writes to it first. On MongoDB the conditional write then runs while the cache entry that enables the fast path is fresh.tests/test_expression_operator_limits.pyloses the expected-failure marker ontest_update_item_condition_at_the_cap_is_evaluated.Every CI job that runs the
tests/suite collects the new file (devtools/run-testshas no ignore entry for it):mongodb-pytest,cassandra-pytest,run-integrationandrun-integration-sqliteamong them.Conflicts with open PRs
git merge-tree --write-treeofd171511d5is clean againstmain, against #558's head, and against #487, #491, #523, #530, #537, #550, #598 and #599. It conflicts with #368 incrates/storage-mongodb/src/pushdown.rsand indocs/manuals/07-upgrade-manual.md. The manual conflict comes from #558's commits that this branch carries: #558's head alone conflicts with #368 in that file. Inpushdown.rs, #368 addsnul_attribute_name_is_not_pushableandcontrol_character_attribute_alias_is_not_pushableinside the test module this branch moves. Whichever merges second keeps#[path = "pushdown_tests.rs"] mod tests;and moves the two tests intopushdown_tests.rs, one indent level out. #368's change tounsafe_attribute_reasonand this branch's change tois_pushablemerge with no conflict.Related behaviour
Found while testing this branch and filed separately:
Nesting Levels have exceeded supported limits(UpdateItem: a result nested more than 32 levels is applied; the service refuses it #574, with fix(core): refuse an update whose result nests beyond 32 levels #590 open for it).mainhas the fixes for two MongoDB faults found the same way. fix(mongodb): store SET values as sent in the numeric ADD pipeline update #589 fixed an UpdateItem that SETs a string starting with$together with a numeric ADD (MongoDB: SET of a $-prefixed string with a numeric ADD answers 500 and corrupts the item #573). fix(mongodb): reach map members under their M field in a pushed-down condition #582 fixed a condition on a nested map member that the fast path accepted (MongoDB: a condition on a nested map member fails when it holds #572).Testing done
At
d171511d5, on a debug build withmongodb-test-hooks, MongoDB on mongo:7.0.45 (a single-node replica set on a loopback port):tests/test_condition_nesting_depth.pyandtests/test_expression_operator_limits.pyon MongoDB: 80 tests collected, 79 passed and 1 expected failure. That one isTestOperatorCount::test_one_condition_per_key_under_the_cap, which carries fix(expression): operator cap, syntax error wording, PutItem map keys #558's marker. The server log has no error 15.pushdown.rs, which has no depth check: all 17 tests oftests/test_condition_nesting_depth.pyfail with InternalServerError. The server log has 17 error-15 lines.cargo +1.97.0 test -p extenddb-storage-mongodb --lib: 191 passed and 83 ignored (they need a replica set). Thepushdownfilter: 22 passed. WithMAX_LOGICAL_DEPTHset to 80,logical_nesting_past_the_limit_is_not_pushablefails at the 66-term chain and the other 21 tests pass.cargo +1.97.0 fmt --all -- --checkis clean. CI's two clippy commands with-D warnings, restricted toextenddb-storage-mongodb(withextenddb-storage-mongodb/test-hooks, and withextenddb-engine/backup-test-hooks), give no warning.cargo +1.91.1 check -p extenddb-storage-mongodb --all-targetspasses.clippy::pedanticreports missing backticks around MongoDB and the operation names in the doc comments this change adds, as in the rest of the file. CI's clippy job does not run pedantic.python3 -m pytest --collect-only -qcollectstests/(6716 tests) andtests/python(672 tests) with no error.Checklist
cargo test --workspace)cargo fmt --check)cargo clippy -- -W clippy::pedantic)Storagetrait, auth model, on-diskformat, 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
Breaking changes
None.
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.