Skip to content

fix(mongodb): evaluate deeply nested conditions instead of answering 500 - #571

Open
LeeroyHannigan wants to merge 9 commits into
fix/expression-operator-limitsfrom
fix/mongodb-condition-depth
Open

LeeroyHannigan wants to merge 9 commits into
fix/expression-operator-limitsfrom
fix/mongodb-condition-depth

Conversation

@LeeroyHannigan

@LeeroyHannigan LeeroyHannigan commented Oct 25, 2026 •

Copy link
Copy Markdown
Collaborator

Changes since the last review

  • 61dc9c8c6 merges fix(expression): operator cap, syntax error wording, PutItem map keys #558's head ee1d54168. Its two commits move the parser tests to parser_tests.rs and add the upgrade manual's rollback note for max_expression_operators.
  • 0068e0560 pins the 64/65 boundary in logical_nesting_past_the_limit_is_not_pushable with literal chains of 65 and 66 terms. The test computed them as MAX_LOGICAL_DEPTH + 1 and + 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 of contains leaves fails on MongoDB.
  • 40bcdf2cb rewrites the module doc of pushdown.rs. 64 is a margin below the depth at which MongoDB refuses the compiled filter: 99 levels with equality leaves, 98 with contains leaves.
  • d171511d5 makes the failing rows of tests/test_condition_nesting_depth.py assert 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_one for UpdateItem and find_one_and_delete for DeleteItem. The compiler emits one $and, $or or $nor level 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 contains leaves, 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_pushable now 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) to pushdown_tests.rs, unchanged, loaded with #[path] as backup_engine.rs loads its test files. The module doc and section 5.2 of docs/design/13-storage-mongodb.md list 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_evaluated hit 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:

  • On an absent key, a 150-term OR chain, a 150-term AND chain, and OR and AND alternating 100 and 140 levels deep answer ConditionalCheckFailedException. A 120-term OR inside an AND answers the same. A legacy Expected map of 120 entries joined by ConditionalOperator OR answers the same, and so does an Expected map of 300 or 301 entries.
  • When such a condition holds, UpdateItem and DeleteItem apply the write, including ReturnValues ALL_OLD, ALL_NEW and UPDATED_NEW, and a TransactWriteItems ConditionCheck.

Tests

  • Unit: logical_nesting_past_the_limit_is_not_pushable checks 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 with contains leaves. It checks that NOT and the deeper side of an operator count.
  • Wire: 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 legacy Expected rows 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 answer tests/test_conditional_writes.py asserts. 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.py loses the expected-failure marker on test_update_item_condition_at_the_cap_is_evaluated.

Every CI job that runs the tests/ suite collects the new file (devtools/run-tests has no ignore entry for it): mongodb-pytest, cassandra-pytest, run-integration and run-integration-sqlite among them.

Conflicts with open PRs

git merge-tree --write-tree of d171511d5 is clean against main, against #558's head, and against #487, #491, #523, #530, #537, #550, #598 and #599. It conflicts with #368 in crates/storage-mongodb/src/pushdown.rs and in docs/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. In pushdown.rs, #368 adds nul_attribute_name_is_not_pushable and control_character_attribute_alias_is_not_pushable inside the test module this branch moves. Whichever merges second keeps #[path = "pushdown_tests.rs"] mod tests; and moves the two tests into pushdown_tests.rs, one indent level out. #368's change to unsafe_attribute_reason and this branch's change to is_pushable merge with no conflict.

Related behaviour

Found while testing this branch and filed separately:

Testing done

At d171511d5, on a debug build with mongodb-test-hooks, MongoDB on mongo:7.0.45 (a single-node replica set on a loopback port):

  • tests/test_condition_nesting_depth.py and tests/test_expression_operator_limits.py on MongoDB: 80 tests collected, 79 passed and 1 expected failure. That one is TestOperatorCount::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.
  • The same server built with the base's pushdown.rs, which has no depth check: all 17 tests of tests/test_condition_nesting_depth.py fail 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). The pushdown filter: 22 passed. With MAX_LOGICAL_DEPTH set to 80, logical_nesting_past_the_limit_is_not_pushable fails at the 66-term chain and the other 21 tests pass.
  • cargo +1.97.0 fmt --all -- --check is clean. CI's two clippy commands with -D warnings, restricted to extenddb-storage-mongodb (with extenddb-storage-mongodb/test-hooks, and with extenddb-engine/backup-test-hooks), give no warning. cargo +1.91.1 check -p extenddb-storage-mongodb --all-targets passes. clippy::pedantic reports 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 -q collects tests/ (6716 tests) and tests/python (672 tests) with no error.

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

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.

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.
…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 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.

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

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.

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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

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.

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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.

Comment thread tests/test_condition_nesting_depth.py Outdated

"""Long and deeply nested condition expressions on UpdateItem and DeleteItem.

Amazon DynamoDB evaluates a condition whose AND, OR and NOT chains run 150

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.

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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.
@LeeroyHannigan

Copy link
Copy Markdown
Collaborator Author

Thanks for the run against the service. The body now describes the head after this round. At d171511d5 the two files collect 80 tests on MongoDB: 79 pass, and test_one_condition_per_key_under_the_cap is #558's expected failure. The dated run history is gone. The body opens with the four commits since your review. They are the merge of #558's head (61dc9c8c6), the literal boundary in the unit test (0068e0560), the module doc (40bcdf2cb) and the wire test assertions (d171511d5). Each of your three inline comments has a reply that names its commit.

@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 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.

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.

2 participants