-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathinternal.rs
322 lines (285 loc) · 9.95 KB
/
internal.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
//! Internal implementaton of InfluxDB "Selector" Functions
//! Tests are in selector module
//!
//! This module is implemented with macros rather than generic types;
//! I tried valiantly (at least in my mind) to use Generics , but I
//! couldn't get the traits to work out correctly (as Bool, I64/F64
//! and Utf8 arrow types don't share enough in common).
use std::{fmt::Debug, sync::Arc};
use arrow::{
array::{Array, ArrayRef, TimestampNanosecondArray},
compute::kernels::aggregate::{max as array_max, min as array_min},
datatypes::DataType,
};
use datafusion::{
error::{DataFusionError, Result as DataFusionResult},
functions_aggregate::min_max::{MaxAccumulator, MinAccumulator},
physical_plan::Accumulator,
scalar::ScalarValue,
};
use super::type_handling::make_struct_scalar;
/// How to compare values/time.
#[derive(Debug, Clone, Copy)]
pub enum Comparison {
Min,
Max,
}
impl Comparison {
fn is_update<T>(&self, old: &T, new: &T) -> bool
where
T: PartialOrd,
{
match self {
Self::Min => new < old,
Self::Max => old < new,
}
}
}
/// What to compare?
#[derive(Debug, Clone, Copy)]
pub enum Target {
Time,
Value,
}
/// Did we find a new min/max
#[derive(Debug)]
enum ActionNeeded {
UpdateValueAndTime,
UpdateTime,
Nothing,
}
impl ActionNeeded {
fn update_value(&self) -> bool {
match self {
Self::UpdateValueAndTime => true,
Self::UpdateTime => false,
Self::Nothing => false,
}
}
fn update_time(&self) -> bool {
match self {
Self::UpdateValueAndTime => true,
Self::UpdateTime => true,
Self::Nothing => false,
}
}
}
/// Common state implementation for different selectors.
#[derive(Debug)]
pub struct Selector {
comp: Comparison,
target: Target,
timezone: Option<Arc<str>>,
value: ScalarValue,
time: Option<i64>,
other: Box<[ScalarValue]>,
}
impl Selector {
pub fn new<'a>(
comp: Comparison,
target: Target,
timezone: Option<Arc<str>>,
data_type: &'a DataType,
other_types: impl IntoIterator<Item = &'a DataType>,
) -> DataFusionResult<Self> {
Ok(Self {
comp,
target,
timezone,
value: ScalarValue::try_from(data_type)?,
time: None,
other: other_types
.into_iter()
.map(ScalarValue::try_from)
.collect::<DataFusionResult<_>>()?,
})
}
fn update_time_based(
&mut self,
value_arr: &ArrayRef,
time_arr: &ArrayRef,
other_arrs: &[ArrayRef],
) -> DataFusionResult<()> {
let time_arr = arrow::compute::nullif(time_arr, &arrow::compute::is_null(&value_arr)?)?;
let time_arr = time_arr
.as_any()
.downcast_ref::<TimestampNanosecondArray>()
// the input type arguments should be ensured by datafusion
.expect("Second argument was time");
let cur_time = match self.comp {
Comparison::Min => array_min(time_arr),
Comparison::Max => array_max(time_arr),
};
let need_update = match (&self.time, &cur_time) {
(Some(time), Some(cur_time)) => self.comp.is_update(time, cur_time),
// No existing min/max, so update needed
(None, Some(_)) => true,
// No actual min/max time found, so no update needed
(_, None) => false,
};
if need_update {
let index = time_arr
.iter()
// arrow doesn't tell us what index had the
// min/max, so need to find it ourselves
.enumerate()
.filter(|(_, time)| cur_time == *time)
.map(|(idx, _)| idx)
// break tie: favor first value
.next()
.unwrap(); // value always exists
// update all or nothing in case of an error
let value_new = ScalarValue::try_from_array(&value_arr, index)?;
let other_new = other_arrs
.iter()
.map(|arr| ScalarValue::try_from_array(arr, index))
.collect::<DataFusionResult<_>>()?;
self.time = cur_time;
self.value = value_new;
self.other = other_new;
}
Ok(())
}
fn update_value_based(
&mut self,
value_arr: &ArrayRef,
time_arr: &ArrayRef,
other_arrs: &[ArrayRef],
) -> DataFusionResult<()> {
use ActionNeeded::*;
let cur_value = match self.comp {
Comparison::Min => {
let mut min_accu = MinAccumulator::try_new(value_arr.data_type())?;
min_accu.update_batch(&[Arc::clone(value_arr)])?;
min_accu.evaluate()?
}
Comparison::Max => {
let mut max_accu = MaxAccumulator::try_new(value_arr.data_type())?;
max_accu.update_batch(&[Arc::clone(value_arr)])?;
max_accu.evaluate()?
}
};
let action_needed = match (&self.value.is_null(), &cur_value.is_null()) {
(false, false) => {
if self.comp.is_update(&self.value, &cur_value) {
// new min/max found
UpdateValueAndTime
} else if cur_value == self.value {
// same maximum found, time might need update
UpdateTime
} else {
Nothing
}
}
// No existing min/max value, so update needed
(true, false) => UpdateValueAndTime,
// No actual min/max value found, so no update needed
(_, true) => Nothing,
};
if action_needed.update_value() {
self.value = cur_value;
self.time = None; // ignore time associated with old value
}
// Note even though we are computing the MAX value,
// the timestamp returned is the one with the *lowest*
// numerical value
if action_needed.update_time() {
// only keep values where we've found our current value.
// Note: We MUST also mask-out NULLs in `value_arr`, otherwise we may easily select that!
let time_arr = arrow::compute::nullif(
time_arr,
&arrow::compute::kernels::cmp::neq(
&self.value.to_array_of_size(time_arr.len())?,
&value_arr,
)?,
)?;
let time_arr =
arrow::compute::nullif(&time_arr, &arrow::compute::is_null(&value_arr)?)?;
let time_arr = time_arr
.as_any()
.downcast_ref::<TimestampNanosecondArray>()
// the input type arguments should be ensured by datafusion
.expect("Second argument was time");
// Note: we still use the MINIMUM timestamp here even if this is the max VALUE aggregator.
let found_new_time = match (array_min(time_arr), self.time) {
(Some(x), Some(y)) => {
if x < y {
self.time = Some(x);
true
} else {
false
}
}
(Some(x), None) => {
self.time = Some(x);
true
}
(None, _) => false,
};
// update other if required
if found_new_time && !self.other.is_empty() {
let index = time_arr
.iter()
// arrow doesn't tell us what index had the
// minimum, so need to find it ourselves
.enumerate()
.filter(|(_, time)| self.time == *time)
.map(|(idx, _)| idx)
// break tie: favor first value
.next()
.unwrap(); // value always exists
self.other = other_arrs
.iter()
.map(|arr| ScalarValue::try_from_array(arr, index))
.collect::<DataFusionResult<_>>()?;
}
}
Ok(())
}
}
impl Accumulator for Selector {
fn state(&mut self) -> DataFusionResult<Vec<ScalarValue>> {
Ok([
self.value.clone(),
ScalarValue::TimestampNanosecond(self.time, self.timezone.clone()),
]
.into_iter()
.chain(self.other.iter().cloned())
.collect())
}
fn update_batch(&mut self, values: &[ArrayRef]) -> DataFusionResult<()> {
if values.is_empty() {
return Ok(());
}
if values.len() < 2 {
return Err(DataFusionError::Internal(format!(
"Internal error: Expected at least 2 arguments passed to selector function but got {}",
values.len()
)));
}
let value_arr = &values[0];
let time_arr = &values[1];
let other_arrs = &values[2..];
match self.target {
Target::Time => self.update_time_based(value_arr, time_arr, other_arrs)?,
Target::Value => self.update_value_based(value_arr, time_arr, other_arrs)?,
}
Ok(())
}
fn merge_batch(&mut self, states: &[ArrayRef]) -> DataFusionResult<()> {
// merge is the same operation as update for these selectors
self.update_batch(states)
}
fn evaluate(&mut self) -> DataFusionResult<ScalarValue> {
make_struct_scalar(
&self.value,
&ScalarValue::TimestampNanosecond(self.time, self.timezone.clone()),
self.other.iter(),
)
}
fn size(&self) -> usize {
std::mem::size_of_val(self) - std::mem::size_of_val(&self.value)
+ self.value.size()
+ self.other.iter().map(|s| s.size()).sum::<usize>()
}
}