forked from mongodb/mongo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedian_approx.js
46 lines (42 loc) · 1.49 KB
/
median_approx.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* Tests that the approximate median accumulator semantics matches the percentile semantics with the
* field 'p':[0.5].
* @tags: [
* requires_fcv_70,
* ]
*/
import {
testWithMultipleGroupsMedian,
testWithSingleGroupMedian
} from "jstests/aggregation/libs/percentiles_util.js";
const coll = db[jsTestName()];
/**
* Tests for correctness without grouping. Confirms that $median computes the expected value and
* also checks that $percentile for p=0.5 computes the same value, because $median is supposed to be
* completely equivalent to the latter (e.g. we should not optimize $median independently of
* $percentile).
*/
testWithSingleGroupMedian({
coll: coll,
docs: [{x: 0}, {x: "non-numeric"}, {x: 1}, {no_x: 0}, {x: 2}],
medianSpec: {$median: {input: "$x", method: "approximate"}},
expectedResult: 1,
msg: "Non-numeric data should be ignored"
});
testWithSingleGroupMedian({
coll: coll,
docs: [{x: "non-numeric"}, {non_x: 1}],
medianSpec: {$median: {input: "$x", method: "approximate"}},
expectedResult: null,
msg: "Median of completely non-numeric data."
});
/**
* Tests for correctness with grouping on $k and computing the percentile on $x.
*/
testWithMultipleGroupsMedian({
coll: coll,
docs: [{k: 0, x: 2}, {k: 0, x: 1}, {k: 1, x: 2}, {k: 2}, {k: 0, x: "str"}, {k: 1, x: 0}],
medianSpec: {$median: {input: "$x", method: "approximate"}},
expectedResult: [/* k:0 */ 1, /* k:1 */ 0, /* k:2 */ null],
msg: "Median of multiple groups"
});