refactor(promql): use the standard pow and atan2 in vector matching

Results only need to agree with Prometheus within the relative tolerance
promql-compat uses, so the ports of Go's math.Pow and math.Atan2 go; the
Go results stay as a fixture compared within that tolerance.

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>
This commit is contained in:
Dennis Zhuang
2026-10-17 22:54:32 +08:00
parent bc8c172fdf
commit 0db08505be
3 changed files with 71 additions and 295 deletions
+2 -3
View File
@@ -31,7 +31,6 @@ use datatypes::series::time::{EvalGrid, TimeAxis};
use crate::grid_series::GridSeriesBuilder;
mod go_math;
#[cfg(test)]
mod tests;
@@ -69,8 +68,8 @@ impl BinaryOp {
Self::Mul => (lhs * rhs, true),
Self::Div => (lhs / rhs, true),
Self::Mod => (lhs % rhs, true),
Self::Pow => (go_math::pow(lhs, rhs), true),
Self::Atan2 => (go_math::atan2(lhs, rhs), true),
Self::Pow => (lhs.powf(rhs), true),
Self::Atan2 => (lhs.atan2(rhs), true),
Self::Eq => (lhs, lhs == rhs),
Self::Ne => (lhs, lhs != rhs),
Self::Gt => (lhs, lhs > rhs),
-256
View File
@@ -1,256 +0,0 @@
// Copyright 2023 Greptime Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Go's `math.Pow` and `math.Atan2`, which Prometheus' `^` and `atan2` use. They are not
//! correctly rounded, so the platform `powf`/`atan2` differ from them in the last bits.
use std::f64::consts::PI;
const MASK: u64 = 0x7FF;
const SHIFT: u32 = 64 - 11 - 1;
const BIAS: i64 = 1023;
/// Go `math.Pow`. The integer part of the exponent is applied by repeated squaring, which is
/// exact on every platform; a fractional part goes through `exp(yf * ln(x))`, which Go itself
/// computes with platform-specific code.
pub(super) fn pow(x: f64, y: f64) -> f64 {
if y == 0.0 || x == 1.0 {
return 1.0;
}
if y == 1.0 {
return x;
}
if x.is_nan() || y.is_nan() {
return f64::NAN;
}
if x == 0.0 {
if y < 0.0 {
return if x.is_sign_negative() && is_odd_int(y) {
f64::NEG_INFINITY
} else {
f64::INFINITY
};
}
return if x.is_sign_negative() && is_odd_int(y) {
x
} else {
0.0
};
}
if y.is_infinite() {
return if x == -1.0 {
1.0
} else if (x.abs() < 1.0) == (y > 0.0) {
0.0
} else {
f64::INFINITY
};
}
if x.is_infinite() {
if x < 0.0 {
return pow(1.0 / x, -y);
}
return if y < 0.0 { 0.0 } else { f64::INFINITY };
}
if y == 0.5 {
return x.sqrt();
}
if y == -0.5 {
return 1.0 / x.sqrt();
}
let (mut yi, mut yf) = (y.abs().trunc(), y.abs().fract());
if yf != 0.0 && x < 0.0 {
return f64::NAN;
}
if yi >= (1u64 << 63) as f64 {
return if x == -1.0 {
1.0
} else if (x.abs() < 1.0) == (y > 0.0) {
0.0
} else {
f64::INFINITY
};
}
let mut a1 = 1.0;
let mut ae: i64 = 0;
if yf != 0.0 {
if yf > 0.5 {
yf -= 1.0;
yi += 1.0;
}
a1 = (yf * x.ln()).exp();
}
let (mut x1, mut xe) = frexp(x);
let mut i = yi as i64;
while i != 0 {
if !(-(1 << 12)..=(1 << 12)).contains(&xe) {
ae += xe;
break;
}
if i & 1 == 1 {
a1 *= x1;
ae += xe;
}
x1 *= x1;
xe <<= 1;
if x1 < 0.5 {
x1 += x1;
xe -= 1;
}
i >>= 1;
}
if y < 0.0 {
a1 = 1.0 / a1;
ae = -ae;
}
ldexp(a1, ae)
}
fn is_odd_int(x: f64) -> bool {
if x.abs() >= (1u64 << 53) as f64 {
return false;
}
x.fract() == 0.0 && (x.trunc() as i64) & 1 == 1
}
fn normalize(x: f64) -> (f64, i64) {
if x.abs() < f64::MIN_POSITIVE {
return (x * (1u64 << 52) as f64, -52);
}
(x, 0)
}
fn frexp(f: f64) -> (f64, i64) {
if f == 0.0 || f.is_infinite() || f.is_nan() {
return (f, 0);
}
let (f, mut exp) = normalize(f);
let mut x = f.to_bits();
exp += ((x >> SHIFT) & MASK) as i64 - BIAS + 1;
x &= !(MASK << SHIFT);
x |= ((-1 + BIAS) as u64) << SHIFT;
(f64::from_bits(x), exp)
}
fn ldexp(frac: f64, exp: i64) -> f64 {
if frac == 0.0 || frac.is_infinite() || frac.is_nan() {
return frac;
}
let (frac, e) = normalize(frac);
let mut exp = exp + e;
let mut x = frac.to_bits();
exp += ((x >> SHIFT) & MASK) as i64 - BIAS;
if exp < -1075 {
return 0.0_f64.copysign(frac);
}
if exp > 1023 {
return if frac < 0.0 {
f64::NEG_INFINITY
} else {
f64::INFINITY
};
}
let mut m = 1.0;
if exp < -1022 {
exp += 53;
m = 1.0 / (1u64 << 53) as f64;
}
x &= !(MASK << SHIFT);
x |= ((exp + BIAS) as u64) << SHIFT;
m * f64::from_bits(x)
}
/// Go `math.Atan2`.
pub(super) fn atan2(y: f64, x: f64) -> f64 {
if y.is_nan() || x.is_nan() {
return f64::NAN;
}
if y == 0.0 {
if x >= 0.0 && !x.is_sign_negative() {
return 0.0_f64.copysign(y);
}
return PI.copysign(y);
}
if x == 0.0 {
return (PI / 2.0).copysign(y);
}
if x.is_infinite() {
if x > 0.0 {
return if y.is_infinite() {
(PI / 4.0).copysign(y)
} else {
0.0_f64.copysign(y)
};
}
return if y.is_infinite() {
(3.0 * PI / 4.0).copysign(y)
} else {
PI.copysign(y)
};
}
if y.is_infinite() {
return (PI / 2.0).copysign(y);
}
let q = atan(y / x);
if x < 0.0 {
if q <= 0.0 {
return q + PI;
}
return q - PI;
}
q
}
fn atan(x: f64) -> f64 {
if x == 0.0 {
return x;
}
if x > 0.0 { satan(x) } else { -satan(-x) }
}
// Constants are copied verbatim from Go's `math/atan.go`.
#[allow(clippy::excessive_precision)]
fn satan(x: f64) -> f64 {
const MOREBITS: f64 = 6.123233995736765886130e-17;
const TAN3PIO8: f64 = 2.41421356237309504880;
if x <= 0.66 {
return xatan(x);
}
if x > TAN3PIO8 {
return PI / 2.0 - xatan(1.0 / x) + MOREBITS;
}
PI / 4.0 + xatan((x - 1.0) / (x + 1.0)) + 0.5 * MOREBITS
}
#[allow(clippy::excessive_precision)]
fn xatan(x: f64) -> f64 {
const P0: f64 = -8.750608600031904122785e-01;
const P1: f64 = -1.615753718733365076637e+01;
const P2: f64 = -7.500855792314704667340e+01;
const P3: f64 = -1.228866684490136173410e+02;
const P4: f64 = -6.485021904942025371773e+01;
const Q0: f64 = 2.485846490142306297962e+01;
const Q1: f64 = 1.650270098316988542046e+02;
const Q2: f64 = 4.328810604912902668951e+02;
const Q3: f64 = 4.853903996359136964868e+02;
const Q4: f64 = 1.945506571482613964425e+02;
let mut z = x * x;
z = z * ((((P0 * z + P1) * z + P2) * z + P3) * z + P4)
/ (((((z + Q0) * z + Q1) * z + Q2) * z + Q3) * z + Q4);
x * z + x
}
+69 -36
View File
@@ -703,7 +703,7 @@ fn vector_operations_match_prometheus() {
.iter()
.map(|series| (labels_of(&series.metric), points_of(series)))
.collect();
assert_eq!(actual, expected, "{case}");
assert_close(&actual, &expected, case);
matched += 1;
}
(Err(actual), Some(expected)) => {
@@ -728,41 +728,6 @@ fn vector_operations_match_prometheus() {
);
}
/// Prometheus' `^` and `atan2` are Go's `math.Pow` and `math.Atan2`. Integer exponents and
/// `atan2` are exact. A fractional exponent goes through Go's `math.Exp`/`math.Log`, which are
/// assembly on amd64; the port uses the Rust ones, within 2 ulps.
#[test]
fn pow_and_atan2_match_go() {
let mut checked = 0;
for line in include_str!("go_math_cases.txt").lines() {
if line.starts_with('#') {
continue;
}
let fields: Vec<&str> = line.split(' ').collect();
let bits = |field: &str| u64::from_str_radix(field, 16).unwrap();
let (a, b) = (
f64::from_bits(bits(fields[1])),
f64::from_bits(bits(fields[2])),
);
let expected = bits(fields[3]);
let (actual, tolerance) = match fields[0] {
"pow" => (
BinaryOp::Pow.apply(a, b).0,
if b.fract() == 0.0 { 0 } else { 2 },
),
"atan2" => (BinaryOp::Atan2.apply(a, b).0, 0),
function => panic!("unknown function {function}"),
};
assert!(
(actual.to_bits() as i64 - expected as i64).abs() <= tolerance,
"{line}: got {:x}",
actual.to_bits()
);
checked += 1;
}
assert_eq!(checked, 452);
}
/// Results that share a label set once `__type__` is dropped fail as a grouping violation within
/// one match group, and as duplicate label sets across match groups, which Prometheus detects
/// only after the operator.
@@ -812,3 +777,71 @@ fn colliding_results_fail_by_where_they_collide() {
assert!(error.contains(expected), "{error}");
}
}
/// Prometheus `almost.Equal` with relative tolerance 1e-10, treating results below 1e-10 as
/// equal and any NaN as equal to NaN, as promql-compat compares engines.
fn close(a: f64, b: f64) -> bool {
const EPSILON: f64 = 1e-10;
if a.is_nan() || b.is_nan() {
return a.is_nan() && b.is_nan();
}
if a == b {
return true;
}
let (diff, sum) = ((a - b).abs(), a.abs() + b.abs());
let equal = if a == 0.0 || b == 0.0 || sum < f64::MIN_POSITIVE {
diff < EPSILON * f64::MIN_POSITIVE
} else {
diff / sum.min(f64::MAX) < EPSILON
};
equal || (a.abs() < EPSILON && b.abs() < EPSILON)
}
fn assert_close(
actual: &BTreeMap<Labels, BTreeMap<usize, u64>>,
expected: &BTreeMap<Labels, BTreeMap<usize, u64>>,
case: &str,
) {
let points = |series: &BTreeMap<Labels, BTreeMap<usize, u64>>| {
series
.iter()
.map(|(labels, points)| (labels.clone(), points.keys().copied().collect::<Vec<_>>()))
.collect::<Vec<_>>()
};
assert_eq!(points(actual), points(expected), "{case}");
for (labels, points) in actual {
for (point, &bits) in points {
let (got, want) = (
f64::from_bits(bits),
f64::from_bits(expected[labels][point]),
);
assert!(
close(got, want),
"{case}: {labels:?} at {point}: {got} vs {want}"
);
}
}
}
/// Prometheus' `^` and `atan2` are Go's `math.Pow` and `math.Atan2`; the standard library
/// agrees within the comparison tolerance, special cases included.
#[test]
fn pow_and_atan2_match_go() {
let mut checked = 0;
for line in include_str!("go_math_cases.txt").lines() {
if line.starts_with('#') {
continue;
}
let fields: Vec<&str> = line.split(' ').collect();
let value = |field: &str| f64::from_bits(u64::from_str_radix(field, 16).unwrap());
let (a, b, expected) = (value(fields[1]), value(fields[2]), value(fields[3]));
let actual = match fields[0] {
"pow" => BinaryOp::Pow.apply(a, b).0,
"atan2" => BinaryOp::Atan2.apply(a, b).0,
function => panic!("unknown function {function}"),
};
assert!(close(actual, expected), "{line}: got {actual}");
checked += 1;
}
assert_eq!(checked, 452);
}