promql/functions/
changes.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Implementation of [`changes`](https://prometheus.io/docs/prometheus/latest/querying/functions/#changes) in PromQL. Refer to the [original
16//! implementation](https://github.com/prometheus/prometheus/blob/main/promql/functions.go#L1023-L1040).
17
18use std::sync::Arc;
19
20use common_macro::range_fn;
21use datafusion::arrow::array::{Float64Array, TimestampMillisecondArray};
22use datafusion::common::DataFusionError;
23use datafusion::logical_expr::{ScalarUDF, Volatility};
24use datafusion::physical_plan::ColumnarValue;
25use datatypes::arrow::array::Array;
26use datatypes::arrow::datatypes::DataType;
27
28use crate::functions::extract_array;
29use crate::range_array::RangeArray;
30
31/// used to count the number of value changes that occur within a specific time range
32#[range_fn(name = Changes, ret = Float64Array, display_name = prom_changes)]
33pub fn changes(_: &TimestampMillisecondArray, values: &Float64Array) -> Option<f64> {
34    if values.is_empty() {
35        None
36    } else {
37        let (first, rest) = values.values().split_first().unwrap();
38        let mut num_changes = 0;
39        let mut prev_element = first;
40        for cur_element in rest {
41            if cur_element != prev_element && !(cur_element.is_nan() && prev_element.is_nan()) {
42                num_changes += 1;
43            }
44            prev_element = cur_element;
45        }
46        Some(num_changes as f64)
47    }
48}
49
50#[cfg(test)]
51mod test {
52    use super::*;
53    use crate::functions::test_util::simple_range_udf_runner;
54
55    // build timestamp range and value range arrays for test
56    fn build_test_range_arrays(
57        timestamps: Vec<i64>,
58        values: Vec<f64>,
59        ranges: Vec<(u32, u32)>,
60    ) -> (RangeArray, RangeArray) {
61        let ts_array = Arc::new(TimestampMillisecondArray::from_iter(
62            timestamps.into_iter().map(Some),
63        ));
64        let values_array = Arc::new(Float64Array::from_iter(values));
65
66        let ts_range_array = RangeArray::from_ranges(ts_array, ranges.clone()).unwrap();
67        let value_range_array = RangeArray::from_ranges(values_array, ranges).unwrap();
68
69        (ts_range_array, value_range_array)
70    }
71
72    #[test]
73    fn calculate_changes() {
74        let timestamps = vec![
75            1000i64, 3000, 5000, 7000, 9000, 11000, 13000, 15000, 17000, 200000, 500000,
76        ];
77        let ranges = vec![
78            (0, 1),
79            (0, 4),
80            (0, 6),
81            (0, 10),
82            (0, 0), // empty range
83        ];
84
85        // assertion 1
86        let values_1 = vec![1.0, 2.0, 3.0, 0.0, 1.0, 0.0, 0.0, 1.0, 2.0, 0.0];
87        let (ts_array_1, value_array_1) =
88            build_test_range_arrays(timestamps.clone(), values_1, ranges.clone());
89        simple_range_udf_runner(
90            Changes::scalar_udf(),
91            ts_array_1,
92            value_array_1,
93            vec![],
94            vec![Some(0.0), Some(3.0), Some(5.0), Some(8.0), None],
95        );
96
97        // assertion 2
98        let values_2 = vec![1.0, 2.0, 3.0, 4.0, 5.0, 1.0, 2.0, 3.0, 4.0, 5.0];
99        let (ts_array_2, value_array_2) =
100            build_test_range_arrays(timestamps.clone(), values_2, ranges.clone());
101        simple_range_udf_runner(
102            Changes::scalar_udf(),
103            ts_array_2,
104            value_array_2,
105            vec![],
106            vec![Some(0.0), Some(3.0), Some(5.0), Some(9.0), None],
107        );
108
109        // assertion 3
110        let values_3 = vec![0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0];
111        let (ts_array_3, value_array_3) = build_test_range_arrays(timestamps, values_3, ranges);
112        simple_range_udf_runner(
113            Changes::scalar_udf(),
114            ts_array_3,
115            value_array_3,
116            vec![],
117            vec![Some(0.0), Some(0.0), Some(1.0), Some(1.0), None],
118        );
119    }
120}