diff --git a/src/promql/src/functions/round.rs b/src/promql/src/functions/round.rs index c5ba4c218c0..ea7ac088810 100644 --- a/src/promql/src/functions/round.rs +++ b/src/promql/src/functions/round.rs @@ -66,7 +66,7 @@ impl Round { let null_array = Float64Array::new_null(value_array.len()); return Ok(ColumnarValue::Array(Arc::new(null_array))); }; - let op = |a: f64| round_to_nearest(a, nearest); + let op = |a: f64| Self::round_to_nearest(a, nearest); let result: PrimitiveArray = value_array.as_primitive::().unary(op); Ok(ColumnarValue::Array(Arc::new(result) as _)) @@ -83,23 +83,24 @@ impl Round { )), )?; - let result: PrimitiveArray = - datatypes::arrow::compute::binary(value_array, nearest_array, round_to_nearest) - .map_err(|err: ArrowError| { - DataFusionError::ArrowError(Box::new(err), None) - })?; + let result: PrimitiveArray = datatypes::arrow::compute::binary( + value_array, + nearest_array, + Self::round_to_nearest, + ) + .map_err(|err: ArrowError| DataFusionError::ArrowError(Box::new(err), None))?; Ok(ColumnarValue::Array(Arc::new(result) as _)) } } } -} -/// Rounds like Prometheus: ties round up (towards +Inf), and multiplying by the inverse -/// of `nearest` follows its float behavior, including NaN for `nearest == 0`. -fn round_to_nearest(value: f64, nearest: f64) -> f64 { - let inverse = 1.0 / nearest; - (value * inverse + 0.5).floor() / inverse + /// Matches the ordinary PromQL UDF, including NaN for `nearest == 0`. + /// With a positive `nearest`, ties round towards +Inf. + pub(crate) fn round_to_nearest(value: f64, nearest: f64) -> f64 { + let inverse = 1.0 / nearest; + (value * inverse + 0.5).floor() / inverse + } } #[cfg(test)] diff --git a/src/promql/src/lib.rs b/src/promql/src/lib.rs index f4a4fca99c4..737d90a4581 100644 --- a/src/promql/src/lib.rs +++ b/src/promql/src/lib.rs @@ -19,6 +19,7 @@ mod go_sort; pub mod grid_series; pub mod histogram_eval; pub mod instant_eval; +pub mod map; pub mod map_label; pub mod map_time; mod metrics; diff --git a/src/promql/src/map.rs b/src/promql/src/map.rs new file mode 100644 index 00000000000..00ea92a2017 --- /dev/null +++ b/src/promql/src/map.rs @@ -0,0 +1,109 @@ +// 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. + +//! Pointwise kernels for evaluated series, independent of planning and execution nodes. +//! +//! Expression column naming and omission of missing points belong to the planner/output adapter; +//! these kernels retain the shared field schema and represent missing grid positions by validity. + +#[cfg(test)] +mod tests; + +use datafusion::error::{DataFusionError, Result}; +use datatypes::arrow::array::Float64Array; +use datatypes::series::samples::{DenseSamplesRef, Samples, Series, SeriesFields, SeriesSchema}; +use datatypes::series::time::TimeAxis; + +use crate::functions::Round; + +/// Float math supported by the evaluated-series kernel. +#[derive(Clone, Copy, Debug)] +pub enum MathOp { + /// Absolute value. + Abs, + /// Rounds towards positive infinity. + Ceil, + /// Rounds towards negative infinity. + Floor, + /// IEEE square root, returning NaN for negative input. + Sqrt, + /// Rounds to a constant multiple; use `1.0` for PromQL's default argument. + Round { nearest: f64 }, +} + +/// Applies float math to every field of one evaluated series, preserving its labels. +/// +/// The explicit schema and actual grid range are preserved. Fields are processed in their own +/// chunk boundaries, without expanding to the full grid or densifying chunked samples. Missing +/// samples stay missing; ordinary NaNs remain values. An empty or all-missing input is valid. +/// +/// Eval must resolve raw staleness before calling this kernel. Evaluated samples use validity, +/// not NaN payloads, for missingness: arithmetic can produce the reserved stale bit pattern too. +/// Raw time axes and histogram/mixed segments are rejected. Unsupported query types must be +/// admitted to the ordinary path before execution; this function neither falls back nor returns +/// partial results. +pub fn map_math(schema: &SeriesSchema, series: &Series, op: MathOp) -> Result { + if !matches!(series.time, TimeAxis::Grid(_)) { + return Err(DataFusionError::Execution( + "Map math requires an evaluated grid time axis".into(), + )); + } + let fields = series + .fields + .iter() + .map(|samples| map_samples(samples, op)) + .collect::>>()?; + let fields = SeriesFields::try_from_vec(fields).map_err(series_error)?; + // Ordinary math calls preserve the metric name (unlike binary arithmetic): the HTTP + // adapter's collect_metric_names follows Call arguments. Keep that compatibility here. + Series::try_new(schema, series.metric.clone(), series.time.clone(), fields) + .map_err(series_error) +} + +impl MathOp { + fn apply(self, floats: &Float64Array) -> Float64Array { + match self { + Self::Abs => floats.unary(f64::abs), + Self::Ceil => floats.unary(f64::ceil), + Self::Floor => floats.unary(f64::floor), + Self::Sqrt => floats.unary(f64::sqrt), + Self::Round { nearest } => { + floats.unary(|sample| Round::round_to_nearest(sample, nearest)) + } + } + } +} + +fn map_samples(samples: &Samples, op: MathOp) -> Result { + let chunks = samples + .segments() + .iter() + .map(|segment| map_float_segment(segment, op)) + .collect::>>()?; + Samples::try_from_chunks(chunks).map_err(series_error) +} + +fn map_float_segment(segment: &Samples, op: MathOp) -> Result { + let Some(DenseSamplesRef::Float(floats)) = segment.dense() else { + return Err(DataFusionError::NotImplemented( + "Map math supports only float samples; histogram/mixed samples are not supported" + .into(), + )); + }; + Ok(Samples::from_float(op.apply(floats))) +} + +fn series_error(error: datatypes::error::Error) -> DataFusionError { + DataFusionError::External(Box::new(error)) +} diff --git a/src/promql/src/map/tests.rs b/src/promql/src/map/tests.rs new file mode 100644 index 00000000000..64793b73c51 --- /dev/null +++ b/src/promql/src/map/tests.rs @@ -0,0 +1,508 @@ +// 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. + +use std::sync::Arc; + +use common_query::prometheus::PROMETHEUS_STALE_NAN_BITS; +use datafusion::functions::math; +use datafusion_common::ScalarValue; +use datafusion_common::config::ConfigOptions; +use datafusion_expr::{ColumnarValue, ScalarFunctionArgs}; +use datatypes::arrow::array::{AsArray, UInt32Array}; +use datatypes::arrow::buffer::NullBuffer; +use datatypes::arrow::datatypes::{DataType, Field, Float64Type}; +use datatypes::series::histogram::{ + BucketSideView, CounterResetHint, HistogramBatch, HistogramBatchBuilder, HistogramView, +}; +use datatypes::series::samples::{FieldSpec, HistogramSamples, SampleRef}; +use datatypes::series::time::{EvalGrid, GridRange, GridSpec}; +use datatypes::series::{Label, MetricName}; + +use super::*; +use crate::functions::IeeeSqrt; + +fn schema(names: &[&str]) -> SeriesSchema { + SeriesSchema { + fields: names + .iter() + .map(|name| FieldSpec { + name: (*name).into(), + }) + .collect(), + eval_grid: Some(Arc::new( + EvalGrid::try_new(GridSpec::try_new(1000, 500, 64).unwrap()).unwrap(), + )), + } +} + +fn series(schema: &SeriesSchema, start: usize, fields: Vec) -> Series { + let time = TimeAxis::Grid( + GridRange::try_new(schema.eval_grid.clone().unwrap(), start, fields[0].len()).unwrap(), + ); + let metric = MetricName::try_new( + Some("requests".into()), + vec![ + Label { + name: "host".into(), + value: "a".into(), + }, + Label { + name: "empty".into(), + value: "".into(), + }, + ], + ) + .unwrap(); + Series::try_new( + schema, + metric, + time, + SeriesFields::try_from_vec(fields).unwrap(), + ) + .unwrap() +} + +fn floats(samples: &[Option]) -> Samples { + Samples::from_float(Float64Array::from(samples.to_vec())) +} + +fn assert_floats(actual: &Samples, expected: &[Option]) { + assert_eq!(actual.len(), expected.len()); + for (index, (actual, expected)) in actual.iter().zip(expected).enumerate() { + match (actual, expected) { + (SampleRef::Missing, None) => {} + (SampleRef::Float(actual), Some(expected)) if expected.is_nan() => { + assert!(actual.is_nan(), "sample {index}: {actual} is not NaN"); + } + (SampleRef::Float(actual), Some(expected)) => { + assert_eq!(actual.to_bits(), expected.to_bits(), "sample {index}"); + } + _ => panic!("unexpected sample kind at {index}"), + } + } +} + +#[test] +fn math_preserves_missing_and_uses_ieee_float_semantics() { + // Boundaries: negative values, signed zero, infinities, ordinary NaN and Missing. + let schema = schema(&["value"]); + let input = series( + &schema, + 3, + vec![floats(&[ + Some(-2.5), + Some(-0.5), + Some(-0.0), + Some(0.0), + Some(4.0), + Some(f64::INFINITY), + Some(f64::NEG_INFINITY), + Some(f64::NAN), + None, + ])], + ); + let cases = [ + ( + MathOp::Abs, + [ + 2.5, + 0.5, + 0.0, + 0.0, + 4.0, + f64::INFINITY, + f64::INFINITY, + f64::NAN, + ], + ), + ( + MathOp::Ceil, + [ + -2.0, + -0.0, + -0.0, + 0.0, + 4.0, + f64::INFINITY, + f64::NEG_INFINITY, + f64::NAN, + ], + ), + ( + MathOp::Floor, + [ + -3.0, + -1.0, + -0.0, + 0.0, + 4.0, + f64::INFINITY, + f64::NEG_INFINITY, + f64::NAN, + ], + ), + ( + MathOp::Sqrt, + [ + f64::NAN, + f64::NAN, + -0.0, + 0.0, + 2.0, + f64::INFINITY, + f64::NAN, + f64::NAN, + ], + ), + ( + MathOp::Round { nearest: 1.0 }, + [ + -2.0, + 0.0, + 0.0, + 0.0, + 4.0, + f64::INFINITY, + f64::NEG_INFINITY, + f64::NAN, + ], + ), + ]; + for (op, expected) in cases { + let output = map_math(&schema, &input, op).unwrap(); + let expected = expected + .into_iter() + .map(Some) + .chain([None]) + .collect::>(); + assert_floats(output.fields.get(0).unwrap(), &expected); + } +} + +#[test] +fn short_multifield_slices_keep_absolute_grid_and_labels() { + let schema = schema(&["left", "right"]); + let first = floats(&[Some(99.0), Some(-1.2), None, Some(-3.2), Some(99.0)]); + let second = floats(&[Some(99.0), Some(4.8), Some(-0.0), None, Some(99.0)]); + let fields = vec![ + Samples::try_from_chunks(vec![first.slice(1, 2).unwrap(), first.slice(3, 1).unwrap()]) + .unwrap(), + Samples::try_from_chunks(vec![ + second.slice(1, 1).unwrap(), + second.slice(2, 2).unwrap(), + ]) + .unwrap(), + ]; + let input = series(&schema, 7, fields); + let output = map_math(&schema, &input, MathOp::Floor).unwrap(); + let TimeAxis::Grid(range) = &output.time else { + panic!("lost grid axis") + }; + assert_eq!((range.start(), range.len()), (7, 3)); + assert!(Arc::ptr_eq( + range.grid(), + schema.eval_grid.as_ref().unwrap() + )); + assert_eq!(output.time.timestamp(0), Some(4500)); + assert_eq!(output.time.timestamp(2), Some(5500)); + // Ordinary math calls retain the input metric name: collect_metric_names follows Call + // arguments, and the Prometheus response adapter emits it. Binary arithmetic differs. + assert_eq!(output.metric.name(), Some("requests")); + assert_eq!(output.metric.tags(), input.metric.tags()); + assert_eq!(input.metric.name(), Some("requests")); + assert_floats( + input.fields.get(0).unwrap(), + &[Some(-1.2), None, Some(-3.2)], + ); + for (field, lengths) in output.fields.iter().zip([[2, 1], [1, 2]]) { + assert_eq!( + field + .segments() + .iter() + .map(Samples::len) + .collect::>(), + lengths + ); + } + drop(input); + drop(schema); + drop(first); + drop(second); + assert_floats( + output.fields.get(0).unwrap(), + &[Some(-2.0), None, Some(-4.0)], + ); + assert_floats( + output.fields.get(1).unwrap(), + &[Some(4.0), Some(-0.0), None], + ); +} + +#[test] +fn empty_and_all_missing_ranges_are_not_expanded() { + let schema = schema(&["value"]); + // Empty at grid end, one missing point, and a longer all-missing range. + for (start, len) in [(64, 0), (9, 1), (2, 4)] { + let input = series(&schema, start, vec![floats(&vec![None; len])]); + for op in [ + MathOp::Abs, + MathOp::Ceil, + MathOp::Floor, + MathOp::Sqrt, + MathOp::Round { nearest: 0.0 }, + ] { + let output = map_math(&schema, &input, op).unwrap(); + let TimeAxis::Grid(range) = &output.time else { + panic!("lost grid axis") + }; + assert_eq!((range.start(), range.len()), (start, len)); + assert_floats(output.fields.get(0).unwrap(), &vec![None; len]); + } + } +} + +#[test] +fn evaluated_missingness_depends_on_validity_not_nan_payloads() { + let schema = schema(&["value"]); + let stale = f64::from_bits(PROMETHEUS_STALE_NAN_BITS); + let ordinary_nan = f64::from_bits(0x7ff8_0000_0000_0042); + let input = series( + &schema, + 1, + vec![Samples::from_float(Float64Array::new( + vec![stale, ordinary_nan, -1.0].into(), + Some(NullBuffer::from(vec![false, true, true])), + ))], + ); + let output = map_math(&schema, &input, MathOp::Abs).unwrap(); + assert_floats( + output.fields.get(0).unwrap(), + &[None, Some(ordinary_nan), Some(1.0)], + ); + let SampleRef::Float(actual_nan) = output.fields.get(0).unwrap().sample(1) else { + panic!("lost NaN") + }; + assert_eq!(actual_nan.to_bits(), ordinary_nan.to_bits()); + + // Removing the sign of this ordinary NaN produces the reserved raw stale bits. A later Map + // must not reinterpret that computed value as a missing sample or reject the evaluated input. + let negative_nan = f64::from_bits(PROMETHEUS_STALE_NAN_BITS | (1_u64 << 63)); + let input = series(&schema, 1, vec![floats(&[Some(negative_nan)])]); + let absolute = map_math(&schema, &input, MathOp::Abs).unwrap(); + let SampleRef::Float(computed_nan) = absolute.fields.get(0).unwrap().sample(0) else { + panic!("lost computed NaN") + }; + assert_eq!(computed_nan.to_bits(), PROMETHEUS_STALE_NAN_BITS); + let rounded = map_math(&schema, &absolute, MathOp::Ceil).unwrap(); + assert_floats(rounded.fields.get(0).unwrap(), &[Some(f64::NAN)]); +} + +fn histogram_batch() -> Arc { + let empty_side = BucketSideView { + span_offsets: &[], + span_lengths: &[], + buckets: &[], + }; + let mut builder = HistogramBatchBuilder::new(); + builder + .push(HistogramView { + schema: 1, + zero_threshold: 0.01, + sum: 0.0, + count: 1.0, + zero_count: 1.0, + reset_hint: CounterResetHint::Unknown, + start_timestamp: None, + custom_values: &[], + positive: empty_side, + negative: empty_side, + }) + .unwrap(); + builder.finish().unwrap() +} + +#[test] +fn histogram_and_mixed_fields_are_rejected_without_partial_success() { + let batch = histogram_batch(); + let histogram = Samples::try_from_histogram(UInt32Array::from(vec![0]), batch.clone()).unwrap(); + let mixed = Samples::try_from_mixed( + Float64Array::from(vec![Some(1.0), None]), + HistogramSamples::try_new(UInt32Array::from(vec![None, Some(0)]), batch.clone()).unwrap(), + ) + .unwrap(); + let null_histogram = Samples::try_from_histogram(UInt32Array::from(vec![None]), batch).unwrap(); + let chunks = Samples::try_from_chunks(vec![floats(&[Some(-1.0)]), histogram.clone()]).unwrap(); + let schema = schema(&["float", "unsupported"]); + for unsupported in [histogram, mixed, null_histogram, chunks] { + let prefix = floats(&vec![Some(-2.0); unsupported.len()]); + let input = series(&schema, 2, vec![prefix, unsupported]); + assert!(matches!( + map_math(&schema, &input, MathOp::Abs), + Err(DataFusionError::NotImplemented(_)) + )); + assert_floats( + input.fields.get(0).unwrap(), + &vec![Some(-2.0); input.time.len()], + ); + } +} + +#[test] +fn raw_time_axes_are_not_treated_as_evaluated_grids() { + let mut schema = schema(&["value"]); + let input = series(&schema, 2, vec![floats(&[Some(-1.0), Some(2.0)])]); + schema.eval_grid = None; + for time in [ + TimeAxis::try_new(vec![2000, 2500].into()).unwrap(), + TimeAxis::try_from_chunks(vec![vec![2000].into(), vec![2500].into()]).unwrap(), + ] { + let raw = + Series::try_new(&schema, input.metric.clone(), time, input.fields.clone()).unwrap(); + assert!( + matches!(map_math(&schema, &raw, MathOp::Abs), Err(DataFusionError::Execution(reason)) if reason.contains("grid")) + ); + } +} + +#[test] +fn mismatched_schema_and_mutated_field_lengths_return_typed_errors() { + let schema = schema(&["value"]); + let mut input = series(&schema, 2, vec![floats(&[Some(-1.0), Some(2.0)])]); + let mut other_grid = schema.clone(); + other_grid.eval_grid = Some(Arc::new( + EvalGrid::try_new(GridSpec::try_new(0, 500, 64).unwrap()).unwrap(), + )); + assert!( + matches!(map_math(&other_grid, &input, MathOp::Abs), Err(DataFusionError::External(error)) if error.is::()) + ); + input.fields = SeriesFields::one(floats(&[Some(-1.0)])); + assert!( + matches!(map_math(&schema, &input, MathOp::Abs), Err(DataFusionError::External(error)) if error.is::()) + ); +} + +fn ordinary_math(op: MathOp, input: Float64Array) -> Float64Array { + // The ordinary planner's UDFs are the compatibility oracle, independent of the native + // dispatcher. Keep these aligned with PromPlanner::create_function_expr. + let number_rows = input.len(); + let mut args = vec![ColumnarValue::Array(Arc::new(input))]; + let udf = match op { + MathOp::Abs => math::abs(), + MathOp::Ceil => math::ceil(), + MathOp::Floor => math::floor(), + MathOp::Sqrt => Arc::new(IeeeSqrt::scalar_udf()), + MathOp::Round { nearest } => { + args.push(ColumnarValue::Scalar(ScalarValue::Float64(Some(nearest)))); + Arc::new(Round::scalar_udf()) + } + }; + let arg_fields = args + .iter() + .map(|arg| Arc::new(Field::new("value", arg.data_type(), true))) + .collect(); + let output = udf + .invoke_with_args(ScalarFunctionArgs { + args, + arg_fields, + number_rows, + return_field: Arc::new(Field::new("output", DataType::Float64, true)), + config_options: Arc::new(ConfigOptions::default()), + }) + .unwrap(); + let ColumnarValue::Array(output) = output else { + panic!("expected array") + }; + output.as_primitive::().clone() +} + +fn float_boundary_samples() -> Float64Array { + let mut samples = vec![ + Some(-2.5), + Some((-0.5_f64).next_down()), + Some(-0.5), + Some((-0.5_f64).next_up()), + Some(0.5_f64.next_down()), + Some(0.5), + Some(0.5_f64.next_up()), + Some(2.5), + Some(123.456), + ]; + // Both signs, every exponent, and mantissa boundaries cover zeros, subnormals, normal + // magnitudes, infinities, and signaling/quiet NaNs without a random seed or new dependency. + for exponent in 0..=0x7ff_u64 { + for mantissa in [0, 1, 1 << 51, (1 << 52) - 1] { + for sign in [0, 1_u64 << 63] { + samples.push(Some(f64::from_bits(sign | (exponent << 52) | mantissa))); + } + } + samples.push(None); + } + Float64Array::from(samples) +} + +#[test] +fn math_matches_existing_udfs_across_float_and_round_boundaries() { + let input = float_boundary_samples(); + let mut schema = schema(&["dense", "chunked"]); + let grid_len = u64::try_from(input.len() + 6).unwrap(); + schema.eval_grid = Some(Arc::new( + EvalGrid::try_new(GridSpec::try_new(1000, 500, grid_len).unwrap()).unwrap(), + )); + let chunks = (0..input.len()) + .step_by(61) + .map(|offset| Samples::from_float(input.slice(offset, 61.min(input.len() - offset)))) + .collect(); + let series = series( + &schema, + 5, + vec![ + Samples::from_float(input.clone()), + Samples::try_from_chunks(chunks).unwrap(), + ], + ); + let cases = [MathOp::Abs, MathOp::Ceil, MathOp::Floor, MathOp::Sqrt] + .into_iter() + .chain( + [ + 1.0, + 0.0, + -0.0, + 0.001, + 0.1, + 105.0, + -0.5, + f64::MIN_POSITIVE, + f64::from_bits(1), + f64::MAX, + f64::INFINITY, + f64::NEG_INFINITY, + f64::NAN, + ] + .into_iter() + .map(|nearest| MathOp::Round { nearest }), + ); + for op in cases { + let output = map_math(&schema, &series, op).unwrap(); + let expected = ordinary_math(op, input.clone()).iter().collect::>(); + for field in output.fields.iter() { + assert_floats(field, &expected); + } + assert_eq!(output.metric.name(), series.metric.name()); + assert_eq!(output.metric.tags(), series.metric.tags()); + let TimeAxis::Grid(range) = output.time else { + panic!("lost grid axis") + }; + assert_eq!((range.start(), range.len()), (5, input.len())); + } +}