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
use crate::aggregate::approx_percentile_cont::ApproxPercentileAccumulator;
use crate::aggregate::tdigest::{Centroid, TDigest, DEFAULT_MAX_SIZE};
use crate::expressions::ApproxPercentileCont;
use crate::{AggregateExpr, PhysicalExpr};
use arrow::{
array::ArrayRef,
datatypes::{DataType, Field},
};
use datafusion_common::Result;
use datafusion_common::ScalarValue;
use datafusion_expr::{Accumulator, AggregateState};
use std::{any::Any, sync::Arc};
#[derive(Debug)]
pub struct ApproxPercentileContWithWeight {
approx_percentile_cont: ApproxPercentileCont,
column_expr: Arc<dyn PhysicalExpr>,
weight_expr: Arc<dyn PhysicalExpr>,
percentile_expr: Arc<dyn PhysicalExpr>,
}
impl ApproxPercentileContWithWeight {
pub fn new(
expr: Vec<Arc<dyn PhysicalExpr>>,
name: impl Into<String>,
return_type: DataType,
) -> Result<Self> {
debug_assert_eq!(expr.len(), 3);
let sub_expr = vec![expr[0].clone(), expr[2].clone()];
let approx_percentile_cont =
ApproxPercentileCont::new(sub_expr, name, return_type)?;
Ok(Self {
approx_percentile_cont,
column_expr: expr[0].clone(),
weight_expr: expr[1].clone(),
percentile_expr: expr[2].clone(),
})
}
}
impl AggregateExpr for ApproxPercentileContWithWeight {
fn as_any(&self) -> &dyn Any {
self
}
fn field(&self) -> Result<Field> {
self.approx_percentile_cont.field()
}
#[allow(rustdoc::private_intra_doc_links)]
fn state_fields(&self) -> Result<Vec<Field>> {
self.approx_percentile_cont.state_fields()
}
fn expressions(&self) -> Vec<Arc<dyn PhysicalExpr>> {
vec![
self.column_expr.clone(),
self.weight_expr.clone(),
self.percentile_expr.clone(),
]
}
fn create_accumulator(&self) -> Result<Box<dyn Accumulator>> {
let approx_percentile_cont_accumulator =
self.approx_percentile_cont.create_plain_accumulator()?;
let accumulator = ApproxPercentileWithWeightAccumulator::new(
approx_percentile_cont_accumulator,
);
Ok(Box::new(accumulator))
}
fn name(&self) -> &str {
self.approx_percentile_cont.name()
}
}
#[derive(Debug)]
pub struct ApproxPercentileWithWeightAccumulator {
approx_percentile_cont_accumulator: ApproxPercentileAccumulator,
}
impl ApproxPercentileWithWeightAccumulator {
pub fn new(approx_percentile_cont_accumulator: ApproxPercentileAccumulator) -> Self {
Self {
approx_percentile_cont_accumulator,
}
}
}
impl Accumulator for ApproxPercentileWithWeightAccumulator {
fn state(&self) -> Result<Vec<AggregateState>> {
self.approx_percentile_cont_accumulator.state()
}
fn update_batch(&mut self, values: &[ArrayRef]) -> Result<()> {
let means = &values[0];
let weights = &values[1];
debug_assert_eq!(
means.len(),
weights.len(),
"invalid number of values in means and weights"
);
let means_f64 = ApproxPercentileAccumulator::convert_to_float(means)?;
let weights_f64 = ApproxPercentileAccumulator::convert_to_float(weights)?;
let mut digests: Vec<TDigest> = vec![];
for (mean, weight) in means_f64.iter().zip(weights_f64.iter()) {
digests.push(TDigest::new_with_centroid(
DEFAULT_MAX_SIZE,
Centroid::new(*mean, *weight),
))
}
self.approx_percentile_cont_accumulator
.merge_digests(&digests);
Ok(())
}
fn evaluate(&self) -> Result<ScalarValue> {
self.approx_percentile_cont_accumulator.evaluate()
}
fn merge_batch(&mut self, states: &[ArrayRef]) -> Result<()> {
self.approx_percentile_cont_accumulator
.merge_batch(states)?;
Ok(())
}
fn size(&self) -> usize {
std::mem::size_of_val(self)
- std::mem::size_of_val(&self.approx_percentile_cont_accumulator)
+ self.approx_percentile_cont_accumulator.size()
}
}