micromegas_datafusion_extensions/histogram/
accessors.rs1use super::histogram_udaf::HistogramArray;
2use super::otel_codec::{decode_column, is_otel_blob_type, native_or_otel_signature};
3use datafusion::{
4 arrow::{
5 array::{Float64Builder, UInt64Builder},
6 datatypes::DataType,
7 },
8 error::DataFusionError,
9 logical_expr::{ColumnarValue, ScalarUDF, SimpleScalarUDF},
10};
11use std::sync::Arc;
12
13fn sum_from_histogram(values: &[ColumnarValue]) -> Result<ColumnarValue, DataFusionError> {
14 if values.len() != 1 {
15 return Err(DataFusionError::Execution(
16 "wrong number of arguments to sum_from_histogram".into(),
17 ));
18 }
19
20 if is_otel_blob_type(&values[0].data_type()) {
21 let arrays = ColumnarValue::values_to_arrays(values)?;
22 let histograms = decode_column("sum_from_histogram", &arrays[0])?;
23 let mut result_builder = Float64Builder::with_capacity(histograms.len());
24 result_builder.extend(histograms.iter().map(|h| h.as_ref().and_then(|h| h.sum)));
25 return Ok(ColumnarValue::Array(Arc::new(result_builder.finish())));
26 }
27
28 let histo_array: HistogramArray = (&values[0]).try_into()?;
29 let mut result_builder = Float64Builder::with_capacity(histo_array.len());
30 for index_histo in 0..histo_array.len() {
31 if histo_array.is_null_at(index_histo) {
32 result_builder.append_null();
33 continue;
34 }
35 result_builder.append_value(histo_array.get_sum(index_histo)?);
36 }
37
38 Ok(ColumnarValue::Array(Arc::new(result_builder.finish())))
39}
40
41pub fn make_sum_from_histogram_udf() -> ScalarUDF {
45 ScalarUDF::from(SimpleScalarUDF::new_with_signature(
46 "sum_from_histogram",
47 native_or_otel_signature(&[]),
48 DataType::Float64,
49 Arc::new(&sum_from_histogram),
50 ))
51}
52
53fn count_from_histogram(values: &[ColumnarValue]) -> Result<ColumnarValue, DataFusionError> {
54 if values.len() != 1 {
55 return Err(DataFusionError::Execution(
56 "wrong number of arguments to count_from_histogram".into(),
57 ));
58 }
59
60 if is_otel_blob_type(&values[0].data_type()) {
61 let arrays = ColumnarValue::values_to_arrays(values)?;
62 let histograms = decode_column("count_from_histogram", &arrays[0])?;
63 let mut result_builder = UInt64Builder::with_capacity(histograms.len());
64 result_builder.extend(histograms.iter().map(|h| h.as_ref().map(|h| h.count)));
65 return Ok(ColumnarValue::Array(Arc::new(result_builder.finish())));
66 }
67
68 let histo_array: HistogramArray = (&values[0]).try_into()?;
69 let mut result_builder = UInt64Builder::with_capacity(histo_array.len());
70 for index_histo in 0..histo_array.len() {
71 if histo_array.is_null_at(index_histo) {
72 result_builder.append_null();
73 continue;
74 }
75 result_builder.append_value(histo_array.get_count(index_histo)?);
76 }
77
78 Ok(ColumnarValue::Array(Arc::new(result_builder.finish())))
79}
80
81pub fn make_count_from_histogram_udf() -> ScalarUDF {
85 ScalarUDF::from(SimpleScalarUDF::new_with_signature(
86 "count_from_histogram",
87 native_or_otel_signature(&[]),
88 DataType::UInt64,
89 Arc::new(&count_from_histogram),
90 ))
91}