common_function/scalars/
uddsketch_calc.rs

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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// 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.

//! Implementation of the scalar function `uddsketch_calc`.

use std::fmt;
use std::fmt::Display;
use std::sync::Arc;

use common_query::error::{DowncastVectorSnafu, InvalidFuncArgsSnafu, Result};
use common_query::prelude::{Signature, Volatility};
use datatypes::data_type::ConcreteDataType;
use datatypes::prelude::Vector;
use datatypes::scalars::{ScalarVector, ScalarVectorBuilder};
use datatypes::vectors::{BinaryVector, Float64VectorBuilder, MutableVector, VectorRef};
use snafu::OptionExt;
use uddsketch::UDDSketch;

use crate::function::{Function, FunctionContext};
use crate::function_registry::FunctionRegistry;

const NAME: &str = "uddsketch_calc";

/// UddSketchCalcFunction implements the scalar function `uddsketch_calc`.
///
/// It accepts two arguments:
/// 1. A percentile (as f64) for which to compute the estimated quantile (e.g. 0.95 for p95).
/// 2. The serialized UDDSketch state, as produced by the aggregator (binary).
///
/// For each row, it deserializes the sketch and returns the computed quantile value.
#[derive(Debug, Default)]
pub struct UddSketchCalcFunction;

impl UddSketchCalcFunction {
    pub fn register(registry: &FunctionRegistry) {
        registry.register(Arc::new(UddSketchCalcFunction));
    }
}

impl Display for UddSketchCalcFunction {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", NAME.to_ascii_uppercase())
    }
}

impl Function for UddSketchCalcFunction {
    fn name(&self) -> &str {
        NAME
    }

    fn return_type(&self, _input_types: &[ConcreteDataType]) -> Result<ConcreteDataType> {
        Ok(ConcreteDataType::float64_datatype())
    }

    fn signature(&self) -> Signature {
        // First argument: percentile (float64)
        // Second argument: UDDSketch state (binary)
        Signature::exact(
            vec![
                ConcreteDataType::float64_datatype(),
                ConcreteDataType::binary_datatype(),
            ],
            Volatility::Immutable,
        )
    }

    fn eval(&self, _func_ctx: FunctionContext, columns: &[VectorRef]) -> Result<VectorRef> {
        if columns.len() != 2 {
            return InvalidFuncArgsSnafu {
                err_msg: format!("uddsketch_calc expects 2 arguments, got {}", columns.len()),
            }
            .fail();
        }

        let perc_vec = &columns[0];
        let sketch_vec = columns[1]
            .as_any()
            .downcast_ref::<BinaryVector>()
            .with_context(|| DowncastVectorSnafu {
                err_msg: format!("expect BinaryVector, got {}", columns[1].vector_type_name()),
            })?;
        let len = sketch_vec.len();
        let mut builder = Float64VectorBuilder::with_capacity(len);

        for i in 0..len {
            let perc_opt = perc_vec.get(i).as_f64_lossy();
            let sketch_opt = sketch_vec.get_data(i);

            if sketch_opt.is_none() || perc_opt.is_none() {
                builder.push_null();
                continue;
            }

            let sketch_bytes = sketch_opt.unwrap();
            let perc = perc_opt.unwrap();

            // Deserialize the UDDSketch from its bincode representation
            let sketch: UDDSketch = match bincode::deserialize(sketch_bytes) {
                Ok(s) => s,
                Err(e) => {
                    common_telemetry::trace!("Failed to deserialize UDDSketch: {}", e);
                    builder.push_null();
                    continue;
                }
            };

            // Compute the estimated quantile from the sketch
            let result = sketch.estimate_quantile(perc);
            builder.push(Some(result));
        }

        Ok(builder.to_vector())
    }
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use datatypes::vectors::{BinaryVector, Float64Vector};

    use super::*;

    #[test]
    fn test_uddsketch_calc_function() {
        let function = UddSketchCalcFunction;
        assert_eq!("uddsketch_calc", function.name());
        assert_eq!(
            ConcreteDataType::float64_datatype(),
            function
                .return_type(&[ConcreteDataType::float64_datatype()])
                .unwrap()
        );

        // Create a test sketch
        let mut sketch = UDDSketch::new(128, 0.01);
        sketch.add_value(10.0);
        sketch.add_value(20.0);
        sketch.add_value(30.0);
        sketch.add_value(40.0);
        sketch.add_value(50.0);
        sketch.add_value(60.0);
        sketch.add_value(70.0);
        sketch.add_value(80.0);
        sketch.add_value(90.0);
        sketch.add_value(100.0);

        // Get expected values directly from the sketch
        let expected_p50 = sketch.estimate_quantile(0.5);
        let expected_p90 = sketch.estimate_quantile(0.9);
        let expected_p95 = sketch.estimate_quantile(0.95);

        let serialized = bincode::serialize(&sketch).unwrap();
        let percentiles = vec![0.5, 0.9, 0.95];

        let args: Vec<VectorRef> = vec![
            Arc::new(Float64Vector::from_vec(percentiles.clone())),
            Arc::new(BinaryVector::from(vec![Some(serialized.clone()); 3])),
        ];

        let result = function.eval(FunctionContext::default(), &args).unwrap();
        assert_eq!(result.len(), 3);

        // Test median (p50)
        assert!(
            matches!(result.get(0), datatypes::value::Value::Float64(v) if (v - expected_p50).abs() < 1e-10)
        );
        // Test p90
        assert!(
            matches!(result.get(1), datatypes::value::Value::Float64(v) if (v - expected_p90).abs() < 1e-10)
        );
        // Test p95
        assert!(
            matches!(result.get(2), datatypes::value::Value::Float64(v) if (v - expected_p95).abs() < 1e-10)
        );
    }

    #[test]
    fn test_uddsketch_calc_function_errors() {
        let function = UddSketchCalcFunction;

        // Test with invalid number of arguments
        let args: Vec<VectorRef> = vec![Arc::new(Float64Vector::from_vec(vec![0.95]))];
        let result = function.eval(FunctionContext::default(), &args);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("uddsketch_calc expects 2 arguments"));

        // Test with invalid binary data
        let args: Vec<VectorRef> = vec![
            Arc::new(Float64Vector::from_vec(vec![0.95])),
            Arc::new(BinaryVector::from(vec![Some(vec![1, 2, 3])])), // Invalid binary data
        ];
        let result = function.eval(FunctionContext::default(), &args).unwrap();
        assert_eq!(result.len(), 1);
        assert!(matches!(result.get(0), datatypes::value::Value::Null));
    }
}