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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//! The percentile aggregates' tests: the argument's two rejection classes,
//! the rank arithmetic at its edges, and REL8-2030's discrete pick — split
//! from `aggregate_tests` so the percentile behaviours have one file
//! (stellar REL8-2030 F-1-2).
use super::aggregate_tests::{run, run_result};
use super::test_support::{distinct_percentile, group_with_vals, group_with_xs, percentile_fn};
use relate_core::{Error, Value};
use relate_ir::Expr;
/// The percentile argument's two failures are distinct: a non-numeric value
/// is the wrong-type class, a number outside [0, 1] the `NumberOutOfRange`
/// kind on the run-time `ArgumentValue` variant.
#[test]
fn percentile_argument_splits_wrong_type_from_out_of_range() {
let group = group_with_vals(&[Value::Int(1), Value::Int(2)]);
let non_numeric = Expr::Function {
name: "percentileCont".into(),
distinct: false,
args: vec![Expr::Variable("x".into()), Expr::String("0.5".into())],
};
let error = run_result(&group, &non_numeric).unwrap_err();
assert!(
matches!(&error, Error::InvalidArgumentType(detail) if detail == "percentile must be a number"),
"{error}"
);
let error = run_result(&group, &percentile_fn("percentileCont", 2.0)).unwrap_err();
assert!(matches!(&error, Error::ArgumentValue { .. }), "{error}");
assert_eq!(
error.to_string(),
"semantic error: NumberOutOfRange: percentile"
);
}
/// LABS-709: an empty group has no percentile to report — and no row for the
/// second argument to be evaluated against. The `AggScope` seed policy
/// (`group.first().or(import)`) never fabricates a row, so the percentile
/// evaluation does not either.
#[test]
fn percentile_over_an_empty_group_is_null() {
for name in ["percentileCont", "percentileDisc"] {
assert_eq!(run(&[], &percentile_fn(name, 0.5)), Value::Null, "{name}");
}
}
/// LABS-709: the percentile sample sorts with the engine's total order
/// (`f64::total_cmp`), which places NaN after every number — a defined
/// rule, never `partial_cmp(...).unwrap_or(Equal)`'s fabricated position.
/// With NaN sorted last, the p=0.5 position reads the middle value.
#[test]
fn percentile_samples_sort_nan_by_total_order() {
let group = group_with_vals(&[Value::Float(f64::NAN), Value::Float(1.0), Value::Float(3.0)]);
for name in ["percentileCont", "percentileDisc"] {
assert_eq!(
run(&group, &percentile_fn(name, 0.5)),
Value::Float(3.0),
"{name}"
);
}
}
/// LABS-709: percentile position 0 is the first sample value for both
/// kinds. The discrete rank arithmetic is total (`saturating_sub`), so
/// `p == 0.0` cannot wrap `(len * p).ceil() as usize - 1` to the last
/// element in a release build. The continuous kind interpolates to an f64;
/// the discrete kind answers the INTEGER operand itself.
#[test]
fn percentile_zero_is_the_first_sample_value() {
assert_eq!(
run(
&group_with_xs(&[3, 1, 2]),
&percentile_fn("percentileCont", 0.0)
),
Value::Float(1.0)
);
assert_eq!(
run(
&group_with_xs(&[3, 1, 2]),
&percentile_fn("percentileDisc", 0.0)
),
Value::Int(1)
);
}
/// REL8-2030 (corpus 27): `percentileDisc` returns the picked operand
/// itself — an INTEGER keeps its type and its exact identity above 2^53 —
/// while the continuous kind interpolates through an f64.
#[test]
fn percentile_disc_answers_the_input_value() {
let high = 9_007_199_254_740_993_i64; // 2^53 + 1, not representable in f64
let group = group_with_vals(&[Value::Int(high), Value::Int(1)]);
assert_eq!(
run(&group, &percentile_fn("percentileDisc", 1.0)),
Value::Int(high)
);
assert_eq!(
run(&group, &percentile_fn("percentileDisc", 0.0)),
Value::Int(1)
);
// The maximum i64, and a mixed sample keeping the picked element's type.
let group = group_with_vals(&[Value::Int(i64::MAX)]);
assert_eq!(
run(&group, &percentile_fn("percentileDisc", 0.5)),
Value::Int(i64::MAX)
);
let group = group_with_vals(&[Value::Int(1), Value::Float(2.5), Value::Int(3)]);
assert_eq!(
run(&group, &percentile_fn("percentileDisc", 0.9)),
Value::Int(3)
);
}
/// LABS-709: the single-sample percentile returns the sample itself, which
/// is load-bearing for ±Infinity — the general interpolation multiplies the
/// one value by 0.0, and `inf * 0.0` is NaN, so the degenerate-weight branch
/// is what keeps `percentileCont([inf], 0.5)` infinite rather than NaN.
#[test]
fn percentile_single_infinite_sample_stays_infinite() {
for sample in [f64::INFINITY, f64::NEG_INFINITY] {
assert_eq!(
run(
&group_with_vals(&[Value::Float(sample)]),
&percentile_fn("percentileCont", 0.5)
),
Value::Float(sample),
"single sample {sample}"
);
}
}
/// rust-review (Medium): a zero interpolation weight must never turn an
/// infinite endpoint into NaN. A percentile position landing exactly on a
/// sample (`f == 0.0`) has one weight of 1.0 and the other of 0.0, so the
/// endpoint must be selected, not multiplied in — both endpoints are
/// pinned, and a mid-interval weight keeps an infinite endpoint infinite.
#[test]
fn percentile_interpolation_keeps_infinite_endpoints_infinite() {
let group = group_with_vals(&[
Value::Float(1.0),
Value::Float(2.0),
Value::Float(f64::NEG_INFINITY),
Value::Float(f64::INFINITY),
]);
// p == 0.0 selects the first sample: weight 1.0, the second weight 0.0.
assert_eq!(
run(&group, &percentile_fn("percentileCont", 0.0)),
Value::Float(f64::NEG_INFINITY),
"p == 0.0"
);
// p == 1.0 selects the last sample: weight 0.0, the last weight 1.0.
assert_eq!(
run(&group, &percentile_fn("percentileCont", 1.0)),
Value::Float(f64::INFINITY),
"p == 1.0"
);
// A non-degenerate weight (0 < f < 1) over an infinite endpoint already
// stays infinite; pinned so the short-circuit cannot narrow that.
assert_eq!(
run(
&group_with_vals(&[Value::Float(1.0), Value::Float(f64::INFINITY)]),
&percentile_fn("percentileCont", 0.5)
),
Value::Float(f64::INFINITY),
"mid-interval weight"
);
}
/// rust-review round 2: the pooled (`f64::total_cmp`) and retained
/// (`Value::order_by_cmp`) samples must agree on NaN's position. A negative
/// NaN — the bit pattern `sqrt(-1)` produces — sorts below `-inf` under
/// `total_cmp` alone, while the engine's total order ranks every NaN last;
/// the pooled sort now applies that same rule, so the plain, DISTINCT and
/// discrete percentiles read one order.
#[test]
fn negative_nan_sorts_last_on_both_percentile_paths() {
let group = group_with_vals(&[
Value::Float(-f64::NAN),
Value::Float(f64::NEG_INFINITY),
Value::Float(1.0),
]);
for (label, call) in [
("pooled sample", percentile_fn("percentileCont", 0.0)),
(
"retained sample",
distinct_percentile("percentileCont", 0.0),
),
("discrete pick", percentile_fn("percentileDisc", 0.0)),
] {
assert_eq!(
run(&group, &call),
Value::Float(f64::NEG_INFINITY),
"{label}"
);
}
}