Skip to content

Assert explain hash fields through assertProperties - #732

Merged
eerxuan merged 3 commits into
documentdb:mainfrom
samafarc:fix-explain-hash-assert
Nov 6, 2026
Merged

eerxuan merged 3 commits into
documentdb:mainfrom
samafarc:fix-explain-hash-assert

Conversation

@samafarc

@samafarc samafarc commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The explain hash tests (test_explain_queryhash_shape.py, test_explain_queryhash_pipeline.py) read planCacheShapeHash / queryShapeHash with file-local helpers that raise RuntimeError when explain fails or the field is missing. That error fires before any assertion runs, so on an engine that does not report these hashes, all 168 cases fail with a helper error instead of an assertion failure about the property under test.

This change keeps the explain result raw and does every check through one assertProperties call:

  • New system/diagnostic/commands/explain/utils/explain_hash.py (next to the only tests that use it) with assert_hash_relation(result_a, result_b, paths, same, msg). It asserts that both results carry a non-empty string hash at the given path (or the first of several candidate paths), then that the two hashes are equal (same=True) or differ (same=False).
  • Each hash is checked as explain reported it, not stringified, so an int, document or Binary hash fails NonEmptyStr.
  • The dotted path is walked with the framework's _walk_path, so a missing field and a present-but-null field are told apart: the first is reported as "field is missing", the second as "got None".
  • A failed explain result is passed straight to assertProperties, so it is reported as an assertion failure with the server's error. The helper then returns, so an assertResult override provider (framework/override_hook.py) that handles that failure is consulted exactly once per test.
  • The four get_* / _get_* helpers are replaced by explain_find / _explain_aggregate, which only run explain and return the raw result.
  • The $out / $merge layout (stages.0.$cursor.queryPlanner.planCacheShapeHash) is kept as a second candidate path.

The expectations are unchanged. On MongoDB every case checks the same equal/different property as before. One behavior is stricter: previously a missing hash on the second query could not be told apart from a missing hash on the first. Now each missing hash is reported by name (hash_a / hash_b), and a Ne comparison never passes because a hash is absent.

Testing

  • MongoDB 8.2.4 replica set: 168/168 pass in both files, same as on main.
  • Amazon DocumentDB 8.0 (dev build), run on the first revision: before the change all 168 cases fail with RuntimeError from the helpers. After it, every case fails inside assertProperties (156 missing-hash PROPERTY_MISMATCH, 12 explain errors carrying the server code), so the results can be judged per assertion.
  • Helper negative cases (no engine): int, document and Binary hashes fail NonEmptyStr; a null hash reports got None; a missing hash reports field is missing; equal vs differ mismatches fail; the $out / $merge layout resolves; an explain error fails through assertProperties.
  • pytest -m unit: 152/152 pass.
  • black, isort, flake8 and mypy are clean.

Example failure on an engine without the field:

[PROPERTY_MISMATCH] m (hash at queryShapeHash)
  expected 'hash_a' to be a non-empty string, but field is missing
  expected 'hash_b' to be a non-empty string, but field is missing

@samafarc
samafarc requested a review from a team as a code owner October 21, 2026 21:26

@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

Moving the hash reads out of helpers that raised RuntimeError and into one assertProperties call is the right fix. Every test still makes exactly one assertion, a failed explain is reported through the helper, and a missing hash on either side now fails the check instead of erroring before it. Two small issues in the new helper.

Critical Issues

  1. [Required] explain_hash.py: hashes[key] = str(value) turns whatever explain returns into a string before NonEmptyStr() runs. So the "hash is a non-empty string" check can't fail on type: an int, a document or a Binary hash would pass. Store value as is. Eq/Ne still compare correctly when both sides are strings.

Suggestions

  1. [Suggestion] _lookup repeats the dotted-path walk that assertProperties already does (_walk_path in framework/assertions.py: dict keys plus numeric list indices). The one difference is that it returns None instead of the absent marker. So a hash that is present but null is reported as "field is missing" rather than "got None". Returning a sentinel for absent, and keeping None as a value, would make that failure message accurate.

Questions

  1. [Nitpick] explain_hash.py is used only by commands/explain/ tests, but it lives in system/diagnostic/utils/. Is it meant for other diagnostic commands? If not, commands/explain/utils/ is the closest common level.
  2. [Nitpick] The branch is 3 commits behind main. Please rebase before merge.

Positive Feedback

  • The early return on a failed explain reports the server error once, not as two missing hashes.
  • Keeping the $out/$merge layout as a second candidate path keeps those pipeline cases exact without adding a branch in each test.

@documentdb-triage-tool documentdb-triage-tool Bot added compatibility test Compatibility test related enhancement New feature or request labels Nov 5, 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 M · Status Needs Review
Confidence: 0.82 (mixed)

Reasoning

component from path globs (test-coverage); effort from diff stats (121+113 LOC, 3 files); LLM: Refactors explain hash test helpers so failures surface as proper assertion failures rather than RuntimeErrors, improving diagnostics across 168 test cases without changing expectations.

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.

The explain queryShapeHash / planCacheShapeHash tests pulled the hash
out of the explain result in local helpers and raised RuntimeError when
the field was missing or explain failed. That failure happened before
any assertion helper ran, so an engine that does not report these
fields failed every case with an exception instead of an assertion
result.

Replace the four helpers with explain_find / _explain_aggregate, which
only return the raw explain result, and a shared assert_hash_relation
helper in diagnostic/utils/explain_hash.py. It checks in a single
assertProperties call that both hashes are non-empty strings and that
they are equal (same shape) or different (different shape). An explain
error is passed to assertProperties and fails there with the server
error.

Behavior change: a different-shape test now fails when the second
explain has no hash, where before Ne passed on a missing field. Test
expectations are otherwise unchanged; 168/168 still pass on MongoDB
8.2.4.

Signed-off-by: Samar Farooqi <samafarc@amazon.com>
explain_hash.py is used only by the explain queryShapeHash /
planCacheShapeHash tests, so move it from system/diagnostic/utils/ to
system/diagnostic/commands/explain/utils/ and import it relatively, as
the other command-local utils packages are. No behavior change.

Signed-off-by: Samar Farooqi <samafarc@amazon.com>
assert_hash_relation stringified each hash before NonEmptyStr ran, so
an int, document or Binary hash passed the check. Store the value as
explain reported it; Eq / Ne still compare two string hashes the same
way.

Also drop the local _lookup helper and walk the dotted path with the
framework's _walk_path, which returns the _FIELD_ABSENT sentinel for a
missing field. _lookup returned None for a missing field, so a hash
that was present but null was reported as "field is missing"; it is now
reported as "got None".

Signed-off-by: Samar Farooqi <samafarc@amazon.com>
@samafarc
samafarc force-pushed the fix-explain-hash-assert branch from 598e494 to 995fb6e Compare November 6, 2026 00:40
@samafarc

samafarc commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @eerxuan, all addressed:

  1. str(value): the hash is now stored as explain returned it, so NonEmptyStr sees the real type. An int, document or Binary hash used to pass and now fails with expected 'hash_a' to be a non-empty string, got 123. Two string hashes compare the same as before.
  2. _lookup: removed. The helper now uses the framework's _walk_path and its absent sentinel, so a missing hash still reports "field is missing" and a present-but-null hash reports "got None". A null at the first candidate path is now kept as the value rather than falling through to the next path.
  3. Location: moved to system/diagnostic/commands/explain/utils/explain_hash.py. The tests import it as from .utils.explain_hash import ..., like the other command-local utils (the absolute import went over the line limit).
  4. Rebase: rebased onto current main (10eec47).

168/168 still pass on MongoDB 8.2.4, same as main.

@eerxuan
eerxuan merged commit 5b566ac 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