Skip to content

Add compatibility tests for $percentile expression operator#655

Open
Kwadwot wants to merge 1 commit into
documentdb:mainfrom
Kwadwot:fix-issue-percentile-operator
Open

Add compatibility tests for $percentile expression operator#655
Kwadwot wants to merge 1 commit into
documentdb:mainfrom
Kwadwot:fix-issue-percentile-operator

Conversation

@Kwadwot

@Kwadwot Kwadwot commented Jun 29, 2026

Copy link
Copy Markdown

What does this PR do?

Adds comprehensive compatibility tests for the $percentile aggregation expression operator (used inside $project/$set/$addFields), which previously had only a single happy-path smoke test. The new tests live under expressions/accumulator/percentile/ and follow the $avg sibling pattern: a PercentileTest(BaseTestCase) dataclass plus a percentile_spec() builder in utils/, the shared execute_expression helpers, and assert_expression_result. Coverage is split by aspect across 7 files (49 cases): core selection and p ordering, input forms, numeric types and special values, non-numeric handling, null/missing input, p validation, and method/spec validation. Two error-code constants used by the tests are added to framework/error_codes.py: PERCENTILE_INVALID_P_TYPE_ERROR (7750302) and PERCENTILE_SPEC_NOT_OBJECT_ERROR (7436200).

Why was this PR needed?

$percentile is a tracked feature in the taxonomy (docs/feature-tree.csv) but had only smoke-level coverage. Characterizing its behavior against a live server surfaced specifics worth pinning down: under method: "approximate" it returns an order statistic at rank ceil(p·n) and always returns double (even for Decimal128 input); non-numeric/null/missing inputs yield [null]; and malformed p/method/spec values map to distinct error codes. Two findings emerged during investigation (captured on MongoDB 8.3.4): method: "discrete" and "continuous" are not supported (only "approximate", others return BadValue), and passing NaN as a p value crashed the mongod process — that case was deliberately excluded from the suite as a likely upstream robustness bug.

What are the relevant issue numbers?

Closes #195

How to test it (on a MongoDB instance) + Screenshot:

Test run against MongoDB 8.3.4:

pytest documentdb_tests/compatibility/tests/core/operator/expressions/accumulator/percentile/ --connection-string mongodb://localhost:27017 --engine-name mongodb -q

49 passed in 1.18s

Tests success screenshot:

image

Regression check on the reference sibling suite ($avg): 224 passed.

Does this PR meet the acceptance criteria?

  • Tests added for new/changed behavior — 49 cases across 7 files
  • All tests passing — 49 passed; $avg regression 224 passed; isort/flake8/mypy clean
  • Follows project style guide — mirrors the $avg pattern per TEST_FORMAT.md / TEST_COVERAGE.md / FOLDER_STRUCTURE.md
  • No breaking changes introduced — additive only (new tests + two sorted error-code constants)
  • Documentation updated (if applicable) — n/a (test-only contribution; no user-facing docs affected)

Add comprehensive compatibility coverage for the $percentile aggregation
expression operator under expressions/accumulator/percentile/, following the
$avg sibling pattern: a PercentileTest dataclass and percentile_spec() builder
in utils/, the shared execute_expression helpers, and assert_expression_result.

Coverage is split by aspect across 7 files (49 cases): core selection and p
ordering, input forms (literal/raw/scalar/expression/field/dotted-path/empty),
numeric types and special values, non-numeric handling, null/missing input, p
validation, and method/spec validation. Expected values and error codes were
captured against MongoDB 8.3.4.

Add two server error-code constants used by the new tests:
PERCENTILE_INVALID_P_TYPE_ERROR (7750302) and PERCENTILE_SPEC_NOT_OBJECT_ERROR
(7436200).

Notable behaviors asserted: only method "approximate" is supported (discrete
and continuous are rejected), and the approximate method always returns double
values regardless of input numeric type.

Fixes documentdb#195

Signed-off-by: Kwadwot <kwadwoptwumasi@gmail.com>
Copilot AI review requested due to automatic review settings June 29, 2026 05:32
@Kwadwot Kwadwot requested a review from a team as a code owner June 29, 2026 05:32

Copilot AI 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.

Pull request overview

Adds comprehensive compatibility coverage for the $percentile aggregation expression operator, aligning the test structure with existing accumulator-expression suites (e.g., $avg) and codifying observed MongoDB 8.3.4 behaviors and error-code mappings.

Changes:

  • Adds a new $percentile compatibility test suite with focused coverage across core semantics, input forms, numeric types/special values, null/missing handling, non-numeric handling, p validation, and method/spec validation.
  • Introduces a shared PercentileTest dataclass and percentile_spec() helper to keep test case construction consistent and concise.
  • Adds two new error-code constants used by the new validation tests.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated no comments.

Show a summary per file
File Description
documentdb_tests/framework/error_codes.py Adds two $percentile-related error code constants used by the new tests.
documentdb_tests/compatibility/tests/core/operator/expressions/accumulator/percentile/utils/percentile_common.py Introduces shared PercentileTest and a helper to build $percentile specs.
documentdb_tests/compatibility/tests/core/operator/expressions/accumulator/percentile/test_percentile_core.py Covers core selection semantics and output ordering relative to p.
documentdb_tests/compatibility/tests/core/operator/expressions/accumulator/percentile/test_percentile_input_forms.py Verifies supported input expression forms and empty-input behavior.
documentdb_tests/compatibility/tests/core/operator/expressions/accumulator/percentile/test_percentile_data_types.py Validates numeric-type handling and special-value ordering/behavior.
documentdb_tests/compatibility/tests/core/operator/expressions/accumulator/percentile/test_percentile_non_numeric.py Ensures non-numeric filtering and all-non-numeric null-result behavior.
documentdb_tests/compatibility/tests/core/operator/expressions/accumulator/percentile/test_percentile_null.py Covers null and missing-input behavior (including field-reference cases).
documentdb_tests/compatibility/tests/core/operator/expressions/accumulator/percentile/test_percentile_p_validation.py Adds p shape/range/type validation cases and expected error-code assertions.
documentdb_tests/compatibility/tests/core/operator/expressions/accumulator/percentile/test_percentile_method.py Validates method acceptance/rejection and spec-document validation/error codes.

@documentdb-triage-tool documentdb-triage-tool Bot added compatibility test Compatibility test related enhancement New feature or request performance labels Jun 29, 2026
@documentdb-triage-tool

Copy link
Copy Markdown

🤖 Auto-triaged by documentdb-triage-tool.

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

Reasoning

component from path globs (test-coverage, test-framework); effort from diff stats (623+0 LOC, 9 files); LLM failed: Invalid response body while trying to fetch https://api.anthropic.com/v1/messages: Premature close

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.

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 performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add compatibility test for $percentile (second pass)

2 participants