Skip to content

Remove $documents dependency from expression test helpers (PR#715 Rebased) - #730

Merged
eerxuan merged 10 commits into
documentdb:mainfrom
samafarc:pr-715-rebased
Nov 6, 2026
Merged

eerxuan merged 10 commits into
documentdb:mainfrom
samafarc:pr-715-rebased

Conversation

@samafarc

@samafarc samafarc commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Supersedes #715 (rebased onto current main).

Summary

The shared expression helpers and the tests that used $documents only to
produce an input row no longer depend on $documents. Engines that do not
support $documents (or the collectionless {aggregate: 1} form it needs)
can now run these tests and fail only on the behavior under test.

Tests that exercise $documents itself are unchanged: the $documents
smoke tests, collection-agnostic aggregate tests (allowDiskUse,
maxTimeMS, explain, field, pipeline stages, cursor acceptance), $lookup
and $facet sub-pipeline tests, stage-position tests, view/collMod
validation, $rankFusion content errors, and the $$CLUSTER_TIME
composition test that checks a $documents-sourced pipeline.

Changes

Helpers (operator/expressions/utils/utils.py)

  • execute_expression and execute_project insert one empty document and
    aggregate over the test collection instead of using $documents: [{}].
    The docstrings note the one difference (the row carries an _id).
  • New execute_expression_single_row(collection, expression): evaluates the
    expression once ($limit: 1 + $project) over a collection that already
    holds documents, without inserting anything. It replaces the hand-written
    copies of that pipeline in the $$CLUSTER_TIME and $$NOW tests.
  • framework/test_format_validator.py: registers the new helper as one that
    runs a command.

Tests converted off $documents

  • variable/system_variables/cluster_time/test_cluster_time_clock_progression.py:
    final comparisons use execute_expression_single_row. _read_cluster_time
    and _read_cluster_time_with_metadata read from a one-document side
    collection (<collection>_clock) seeded on the first read, so they add
    nothing to the test's own collection and later reads are write-free. The
    majority read concern case aggregates over the test collection.
  • cluster_time/test_cluster_time_write_paths.py: the four "value advanced"
    comparisons use execute_expression_single_row.
  • now/test_now_core_semantics.py: millisecond sampling, the advance check,
    and the getMore check use execute_expression_single_row.
  • now/test_now_date_operations.py, test_system_variables_now_expression_engine.py:
    insert a document and aggregate over the collection.
  • root/test_root_core_behavior.py, current/test_current_document_and_pipeline.py:
    the empty-document cases insert a document and strip it to a field-less row
    with {$project: {_id: 0}} before reading $$ROOT / $$CURRENT.
  • operator/expressions/test_expressions_combination_variables.py,
    operator/test_operator_combination_setField_stages.py,
    operator/test_operator_combination_unsetField_stages.py: insert a
    document and aggregate over the collection.
  • data-types/bson_types/types/test_types_string.py: the max-length string
    is passed as a $literal (a document holding it would exceed the maximum
    document size, so it cannot be inserted).
  • stages/lookup/test_lookup_verbose_let_wiring.py: the $unionWith case
    unions the foreign collection instead of a $documents source;
    lookup/utils/lookup_common.py now substitutes the FOREIGN sentinel in
    $unionWith.coll.

CI and dev environment (follow from the helper change)

Moving the helpers onto real collections means the suite now creates and
drops far more collections per run. The first CI run of this change hit the
6h job limit (run 31131591225):
the replica set ran out of open files, WiredTiger panicked, and every
remaining test then waited on connection timeouts against a dead server.

  • dev/compose.yaml: raises nofile to 1048576 on both MongoDB services
    (the replica set keeps drop-pending files open and peaks near 60k
    descriptors; 64000 was not enough). The header explains why.
  • .github/workflows/pr-tests.yml: a watchdog aborts the test run when a
    target container exits mid-run, so a crash fails the job in about a minute
    instead of 6h, and --timeout=120 caps any single hung test.

Testing

  • MongoDB 8.2.4 replica set (fresh container, nofile 1048576), all touched
    test files plus the whole stages/lookup folder: 550 passed on this branch,
    550 passed on main, same test IDs (the $$ROOT empty-document case is no
    longer parametrized, so its ID drops the -empty_document suffix).
  • pytest -m unit: 152 passed (same as main).
  • pre-commit: black, isort, flake8, mypy pass.

@samafarc
samafarc requested a review from a team as a code owner October 19, 2026 20:42
@samafarc samafarc changed the title Remove $documents dependency from expression test helpers (PR 715 Rebased) Remove $documents dependency from expression test helpers (PR#715 Rebased) Oct 19, 2026

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

Summary

Replaces the {aggregate: 1, $documents: [{}]} scaffold in execute_expression / execute_project with insert_one({}) plus an aggregate over the collection, and adds CI hardening (nofile limit, target watchdog, --timeout) in the same PR. I checked how the helpers are used. Across the 471 test files that call them (10,224 parametrized invocations), none pre-populates the collection, and the 10 expressions that mention $$ROOT, $$CURRENT, $$DESCEND or $_id give the same result with or without an _id. CI is green on both mongo targets. The dependency is not fully removed yet (item 1) and the description does not match the diff (item 2).

Critical Issues

  1. [Required] $documents is still used as a scaffold in 17 places in 9 files, so the description's goal ("run against engines without $documents") is not met.

    • cluster_time/test_cluster_time_clock_progression.py: 407, 418 (_read_cluster_time, _read_cluster_time_with_metadata), 632
    • cluster_time/test_cluster_time_write_paths.py: 654, 693, 805, 847
    • now/test_now_core_semantics.py: 90, 228
    • operator/test_operator_combination_setField_stages.py: 19, 43
    • operator/test_operator_combination_unsetField_stages.py: 81, 104
    • current/test_current_document_and_pipeline.py: 174
    • now/test_now_date_operations.py: 212
    • test_system_variables_now_expression_engine.py: 159
    • data-types/bson_types/types/test_types_string.py: 175

    (lookup/test_lookup_verbose_let_wiring.py:254 uses it as a $unionWith data source; borderline.) 5 of the 11 cluster_time tests this PR rewrites still call _read_cluster_time*, so they still need $documents. The remaining uses in aggregate, $lookup, $facet, stage-position, view and rankFusion tests test $documents itself and are fine. Please convert the scaffold sites too, or narrow the description.

  2. [Required] The description lists 2 files; the diff has 7, including pr-tests.yml and dev/compose.yaml. The CI changes follow from the helper change (the first CI run of it hit the 6h limit: run 31131591225), so please say that in the description, or move the CI part to its own PR. #715 contains the same five commits and is still open; please close one.

Suggestions

  1. [Suggestion] 13 hand-written copies of "aggregate + $limit: 1 + $project" (11 in cluster_time, one in NOW, one in ROOT). REVIEW.md asks for no duplication and helpers in utils/. Add one helper next to execute_expression, for example execute_expression_single_row(collection, expression). The remaining _read_cluster_time* sites could use it too.

  2. [Suggestion] test_root_empty_document now uses $replaceWith + $literal, which replaces one stage dependency with two. {$project: {_id: 0}} followed by {$project: {_id: 0, result: "$$ROOT"}} should give the same field-less row using only $project. I have not run it, so please confirm in CI.

Nitpicks

  • dev/compose.yaml explains the WiredTiger file-handle behavior twice in the header.

Positive Feedback

  • The docstrings state the divergence (_id, $$ROOT, $$CURRENT).
  • The watchdog's two-reading rule and baseline check avoid false aborts.
  • The 11 cluster_time rewrites match the commit message, and each has a populated collection before its final aggregate.

eerxuan and others added 9 commits November 5, 2026 23:03
The expression compatibility tests use a collectionless
`{aggregate: 1}` + `$documents: [{}]` pipeline purely as a scaffold
to feed a single empty document into $project. They do not test
$documents itself, yet they inherit a hard dependency on $documents
support from the shared helpers.

Swap the scaffold for `collection.insert_one({})` plus an aggregate
over the named collection. This is logically identical: both feed
exactly one empty document to $project, so literal expressions and
field references (which resolve to missing against an empty doc)
behave the same. The `collection` fixture is function-scoped, so each
test still runs against a fresh single-document collection.

Updates the two shared helpers (execute_expression, execute_project)
that ~205 files inherit, plus two inline call sites in
test_expressions_combination_variables.py. The _with_insert sibling
helpers are unchanged.

Signed-off-by: Yunxuan Shi <yunxuan@amazon.com>
Signed-off-by: Samar Farooqi <samafarc@amazon.com>
The $documents-removal commit made execute_expression/execute_project
insert a document and aggregate over the whole collection. Two newly
synced system-variable tests relied on the old $documents:[{}] contract
and broke:

- test_root_empty_document: needs a truly field-less input so $$ROOT is
  {}, but an inserted doc always carries an auto _id. It now shapes its
  own pipeline ($replaceWith:{$literal:{}}) instead of the shared helper.
- test_now_identical_across_getmore_batches: pre-loads 300 docs, so the
  whole-collection helper emitted 300 rows. It now uses an inline
  pipeline with $limit:1 to collapse to the single expected row.

The shared helpers stay on plain insert_one({}) so the ~359 literal
expression call sites gain no $replaceWith/$limit dependency.

Signed-off-by: Yunxuan Shi <yunxuan@amazon.com>
Signed-off-by: Samar Farooqi <samafarc@amazon.com>
The replica-set test job was hitting GitHub's 6h max execution limit.
Root cause: the suite creates and drops tens of thousands of collections
per run; WiredTiger keeps an open fd per table it has touched (closing idle
ones only very lazily) and, on the replica set, dropped collections become
drop-pending idents whose files linger until majority-committed and swept.
So the descriptor count tracks total collections created over the run, not
the number live at once, and mid-run mongod runs out of descriptors. That
surfaces as a WiredTiger WT_PANIC (EMFILE -> fassert) and crashes the
server; every remaining test then blocks on connection timeouts and the
job crawls for hours. Standalone is unaffected because it reclaims dropped
tables immediately and never approaches the ceiling.

Two changes:

1. Raise ulimits.nofile to 1048576 (the limit MongoDB's own packaging ships)
   on both mongod targets. The replica set peaks near 60k open files partway
   through the suite; 64000 was not enough headroom -- it still crashed at
   ~31k collections -- so this is set well above the peak.

2. Add a target-death watchdog and a per-test --timeout to the test job so
   that if a target ever does die mid-run, the job fails in about a minute
   instead of timing out every remaining test against a dead target for
   hours. The watchdog sanity-checks its probe first and steps aside if it
   cannot read a healthy baseline, so it cannot false-abort a healthy run.

Signed-off-by: Yunxuan Shi <yunxuan@amazon.com>
Signed-off-by: Samar Farooqi <samafarc@amazon.com>
These tests write documents to advance the logical clock (the behavior
under test), then assert a variable/literal expression via execute_expression.
The old helper ran collectionless ($documents: [{}]) so those writes never
affected it. The new helper aggregates over collection.name, so every document
the test wrote becomes an output row -- the assertion expected [{result: X}]
but got one identical row per document.

Each affected assertion evaluates only $$CLUSTER_TIME and Python-computed
literals (never a document field), so the value is identical on any row. Add
{$limit: 1} to collapse the aggregation to the single-row evaluation the
$documents scaffold used to provide -- preserving exactly what each test
verifies without reintroducing the $documents dependency this PR removes.
This is the same shaping the PR already applied to
test_now_identical_across_getmore_batches. The 5 tests whose collection stays
empty are unaffected and still use the helper directly.

Signed-off-by: Yunxuan Shi <yunxuan@amazon.com>
Signed-off-by: Samar Farooqi <samafarc@amazon.com>
Watchdog (pr-tests.yml): require two consecutive low container-count
readings before aborting. A single reading of the resource-heavy suite's
`docker compose ps` can transiently error and read as 0, which would
false-abort a healthy run; a real crash stays low across polls, so this
rules out the false abort while adding at most one poll interval to a
genuine fast-fail.

Helper docstrings (utils.py): the insert+aggregate form leaves the input
row with an auto-generated `_id` rather than the field-less row
`$documents: [{}]` produced, so note that an expression reading `$_id`,
`$$ROOT`, or `$$CURRENT` sees that id and diverges (references to any other
missing field are still identical). Documentation only; no behavior change.

Signed-off-by: Yunxuan Shi <yunxuan@amazon.com>
Signed-off-by: Samar Farooqi <samafarc@amazon.com>
Several tests evaluate a final expression over a collection they have
already populated, where execute_expression would emit one row per
document. They each hand-wrote an aggregate with $limit: 1 and a
$project. Add a shared helper for that shape that evaluates the
expression once and does not insert, and register it with the test
format validator.

Signed-off-by: Samar Farooqi <samafarc@amazon.com>
Convert the remaining system variable tests that used $documents only
to produce an input row so they run over the test collection instead:

- Use execute_expression_single_row for the hand-written single-row
  aggregates in the $$CLUSTER_TIME and $$NOW tests and for the
  comparisons that used a $documents row.
- Read $$CLUSTER_TIME from a one-document side collection in the same
  database, seeded once, so reads add nothing to the test's own
  collection and stay write-free after the first one.
- Build the field-less row for $$ROOT and $$CURRENT by inserting a
  document and excluding _id with $project.
- Insert a document and aggregate over the collection for the
  remaining $$NOW and majority read concern cases.

Signed-off-by: Samar Farooqi <samafarc@amazon.com>
- $setField/$unsetField: insert a document and aggregate over the
  collection; the stages build their output from literals.
- Max-length string: pass the string as a $literal, since a document
  holding it would exceed the maximum document size.
- $lookup let wiring: $unionWith the foreign collection instead of a
  $documents source, and substitute the FOREIGN sentinel in
  $unionWith.coll.

Signed-off-by: Samar Farooqi <samafarc@amazon.com>
The header described WiredTiger's lazy file-handle closing twice.
Merge it into one sentence.

Signed-off-by: Samar Farooqi <samafarc@amazon.com>
@samafarc

samafarc commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @eerxuan, all addressed:

  1. Remaining $documents scaffold sites: all 17 sites in the 9 files are converted, plus the lookup $unionWith one.
    • _read_cluster_time / _read_cluster_time_with_metadata read from a one-document side collection, seeded on the first read. Inserting into the test's own collection broke tests that inspect the whole collection, and this keeps every later read write-free, so the idle tests stay idle.
    • The max-length string can't be inserted (the document would be over the max size), so it goes in as a $literal in $project.
    • The lookup case now unions the foreign collection, and lookup_common substitutes FOREIGN in $unionWith.coll.
    • What's left uses $documents as the thing under test (smoke, collection-agnostic aggregate options, $lookup/$facet, stage-position, views/collMod, $rankFusion, and the $$CLUSTER_TIME composition test).
  2. Description vs diff: the description now lists every file and explains that the CI and compose changes follow from the helper change (run 31131591225 hit the 6h limit after the replica set ran out of open files). I've asked on Remove $documents dependency from expression test helpers #715 for it to be closed as superseded.
  3. Shared helper: added execute_expression_single_row(collection, expression) next to execute_expression and used it for the cluster_time and $$NOW copies, _read_cluster_time, and the new conversions. The $$ROOT one needs a field-less row, so it uses item 4 instead.
  4. test_root_empty_document: now {$project: {_id: 0}} then {$project: {_id: 0, result: "$$ROOT"}}. Verified on MongoDB 8.2.4: same field-less row, test passes. $$CURRENT uses the same pattern.
  5. compose.yaml: merged the two WiredTiger explanations into one.

Rebased onto current main (10eec47). The touched files plus all of stages/lookup pass 550/550 on MongoDB 8.2.4, same as main.

@documentdb-triage-tool documentdb-triage-tool Bot added compatibility test Compatibility test related enhancement New feature or request labels Nov 6, 2026
@documentdb-triage-tool

Copy link
Copy Markdown

🤖 Auto-triaged by documentdb-triage-tool.

Applied: compatibility test, enhancement
Project fields suggested: Component test-coverage · Priority P2 · Effort L · Status Needs Review
Confidence: 0.82 (mixed)

Reasoning

component from path globs (test-coverage, ci, test-framework); effort from diff stats (284+157 LOC, 17 files); LLM: Refactors test helpers and multiple test files to remove $documents dependency, enabling broader engine compatibility — multi-file change touching shared utilities and numerous test cases across one component.

If a label is wrong, remove it manually and ping @patty-chow so the rules can be tuned. The bot will not re-label items that already have component labels.

@eerxuan
eerxuan merged commit 09411e7 into documentdb:main Nov 6, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

compatibility test Compatibility test related enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants