Add compatibility tests for $percentile expression operator#655
Conversation
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>
There was a problem hiding this comment.
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
$percentilecompatibility test suite with focused coverage across core semantics, input forms, numeric types/special values, null/missing handling, non-numeric handling,pvalidation, andmethod/spec validation. - Introduces a shared
PercentileTestdataclass andpercentile_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. |
|
🤖 Auto-triaged by documentdb-triage-tool. Applied: Reasoningcomponent 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 |
What does this PR do?
Adds comprehensive compatibility tests for the
$percentileaggregation expression operator (used inside$project/$set/$addFields), which previously had only a single happy-path smoke test. The new tests live underexpressions/accumulator/percentile/and follow the$avgsibling pattern: aPercentileTest(BaseTestCase)dataclass plus apercentile_spec()builder inutils/, the sharedexecute_expressionhelpers, andassert_expression_result. Coverage is split by aspect across 7 files (49 cases): core selection andpordering, input forms, numeric types and special values, non-numeric handling, null/missing input,pvalidation, andmethod/spec validation. Two error-code constants used by the tests are added toframework/error_codes.py:PERCENTILE_INVALID_P_TYPE_ERROR(7750302) andPERCENTILE_SPEC_NOT_OBJECT_ERROR(7436200).Why was this PR needed?
$percentileis 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: undermethod: "approximate"it returns an order statistic at rank ceil(p·n) and always returnsdouble(even for Decimal128 input); non-numeric/null/missing inputs yield[null]; and malformedp/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 returnBadValue), and passingNaNas apvalue crashed themongodprocess — 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:
49 passed in 1.18s
Tests success screenshot:
Regression check on the reference sibling suite (
$avg):224 passed.Does this PR meet the acceptance criteria?
$avgregression 224 passed; isort/flake8/mypy clean$avgpattern perTEST_FORMAT.md/TEST_COVERAGE.md/FOLDER_STRUCTURE.md