-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy pathmod.rs
More file actions
285 lines (243 loc) · 9.77 KB
/
mod.rs
File metadata and controls
285 lines (243 loc) · 9.77 KB
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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
use vortex_error::VortexResult;
use crate::ArrayRef;
use crate::Columnar;
use crate::ExecutionCtx;
use crate::aggregate_fn::Accumulator;
use crate::aggregate_fn::AggregateFnId;
use crate::aggregate_fn::AggregateFnVTable;
use crate::aggregate_fn::DynAccumulator;
use crate::aggregate_fn::EmptyOptions;
use crate::dtype::DType;
use crate::scalar::Scalar;
/// Return the last non-null value of an array.
///
/// See [`Last`] for details.
pub fn last(array: &ArrayRef, ctx: &mut ExecutionCtx) -> VortexResult<Scalar> {
let mut acc = Accumulator::try_new(Last, EmptyOptions, array.dtype().clone())?;
acc.accumulate(array, ctx)?;
acc.finish()
}
/// Return the last non-null value seen across all batches.
#[derive(Clone, Debug)]
pub struct Last;
/// Partial accumulator state for the [`Last`] aggregate.
pub struct LastPartial {
/// The nullable version of the input dtype, used for the result and for empty/all-null inputs.
return_dtype: DType,
/// The last non-null value seen so far, or `None` if no non-null value has been observed.
value: Option<Scalar>,
}
impl AggregateFnVTable for Last {
type Options = EmptyOptions;
type Partial = LastPartial;
fn id(&self) -> AggregateFnId {
AggregateFnId::new_ref("vortex.last")
}
fn serialize(&self, _options: &Self::Options) -> VortexResult<Option<Vec<u8>>> {
unimplemented!("Last is not yet serializable");
}
fn return_dtype(&self, _options: &Self::Options, input_dtype: &DType) -> Option<DType> {
Some(input_dtype.as_nullable())
}
fn partial_dtype(&self, options: &Self::Options, input_dtype: &DType) -> Option<DType> {
self.return_dtype(options, input_dtype)
}
fn empty_partial(
&self,
_options: &Self::Options,
input_dtype: &DType,
) -> VortexResult<Self::Partial> {
Ok(LastPartial {
return_dtype: input_dtype.as_nullable(),
value: None,
})
}
fn combine_partials(&self, partial: &mut Self::Partial, other: Scalar) -> VortexResult<()> {
// Each new non-null partial replaces the previous one; nulls are ignored.
if !other.is_null() {
partial.value = Some(other);
}
Ok(())
}
fn to_scalar(&self, partial: &Self::Partial) -> VortexResult<Scalar> {
Ok(match &partial.value {
Some(v) => v.clone(),
None => Scalar::null(partial.return_dtype.clone()),
})
}
fn reset(&self, partial: &mut Self::Partial) {
partial.value = None;
}
#[inline]
fn is_saturated(&self, _partial: &Self::Partial) -> bool {
// Last can never short-circuit: a later batch can always supersede the current value.
false
}
fn try_accumulate(
&self,
partial: &mut Self::Partial,
batch: &ArrayRef,
_ctx: &mut ExecutionCtx,
) -> VortexResult<bool> {
if let Some(idx) = batch.validity_mask()?.last() {
let scalar = batch.scalar_at(idx)?;
partial.value = Some(scalar.into_nullable());
}
Ok(true)
}
fn accumulate(
&self,
_partial: &mut Self::Partial,
_batch: &Columnar,
_ctx: &mut ExecutionCtx,
) -> VortexResult<()> {
unreachable!("Last::try_accumulate handles all arrays")
}
fn finalize(&self, partials: ArrayRef) -> VortexResult<ArrayRef> {
Ok(partials)
}
fn finalize_scalar(&self, partial: &Self::Partial) -> VortexResult<Scalar> {
self.to_scalar(partial)
}
}
#[cfg(test)]
mod tests {
use vortex_buffer::buffer;
use vortex_error::VortexResult;
use crate::IntoArray;
use crate::LEGACY_SESSION;
use crate::VortexSessionExecute;
use crate::aggregate_fn::Accumulator;
use crate::aggregate_fn::AggregateFnVTable;
use crate::aggregate_fn::DynAccumulator;
use crate::aggregate_fn::EmptyOptions;
use crate::aggregate_fn::fns::last::Last;
use crate::aggregate_fn::fns::last::last;
use crate::arrays::ChunkedArray;
use crate::arrays::ConstantArray;
use crate::arrays::PrimitiveArray;
use crate::arrays::VarBinArray;
use crate::dtype::DType;
use crate::dtype::Nullability;
use crate::dtype::Nullability::Nullable;
use crate::dtype::PType;
use crate::scalar::Scalar;
use crate::validity::Validity;
#[test]
fn last_non_null() -> VortexResult<()> {
let array = PrimitiveArray::new(buffer![10i32, 20, 30], Validity::NonNullable).into_array();
let mut ctx = LEGACY_SESSION.create_execution_ctx();
assert_eq!(last(&array, &mut ctx)?, Scalar::primitive(30i32, Nullable));
Ok(())
}
#[test]
fn last_skips_trailing_nulls() -> VortexResult<()> {
let array =
PrimitiveArray::from_option_iter([Some(7i32), Some(8), None, None]).into_array();
let mut ctx = LEGACY_SESSION.create_execution_ctx();
assert_eq!(last(&array, &mut ctx)?, Scalar::primitive(8i32, Nullable));
Ok(())
}
#[test]
fn last_all_null() -> VortexResult<()> {
let array = PrimitiveArray::from_option_iter::<i32, _>([None, None, None]).into_array();
let mut ctx = LEGACY_SESSION.create_execution_ctx();
let dtype = DType::Primitive(PType::I32, Nullable);
assert_eq!(last(&array, &mut ctx)?, Scalar::null(dtype));
Ok(())
}
#[test]
fn last_empty() -> VortexResult<()> {
let dtype = DType::Primitive(PType::I32, Nullability::NonNullable);
let mut acc = Accumulator::try_new(Last, EmptyOptions, dtype)?;
let result = acc.finish()?;
assert_eq!(result, Scalar::null(DType::Primitive(PType::I32, Nullable)));
Ok(())
}
#[test]
fn last_constant() -> VortexResult<()> {
let array = ConstantArray::new(42i32, 10).into_array();
let mut ctx = LEGACY_SESSION.create_execution_ctx();
assert_eq!(last(&array, &mut ctx)?, Scalar::primitive(42i32, Nullable));
Ok(())
}
#[test]
fn last_constant_null() -> VortexResult<()> {
let dtype = DType::Primitive(PType::I32, Nullable);
let array = ConstantArray::new(Scalar::null(dtype.clone()), 10).into_array();
let mut ctx = LEGACY_SESSION.create_execution_ctx();
assert_eq!(last(&array, &mut ctx)?, Scalar::null(dtype));
Ok(())
}
#[test]
fn last_varbin() -> VortexResult<()> {
let array = VarBinArray::from_iter(
vec![Some("hello"), Some("world"), None],
DType::Utf8(Nullable),
)
.into_array();
let mut ctx = LEGACY_SESSION.create_execution_ctx();
assert_eq!(last(&array, &mut ctx)?, Scalar::utf8("world", Nullable));
Ok(())
}
#[test]
fn last_multi_batch_picks_latest_non_null() -> VortexResult<()> {
let mut ctx = LEGACY_SESSION.create_execution_ctx();
let dtype = DType::Primitive(PType::I32, Nullable);
let mut acc = Accumulator::try_new(Last, EmptyOptions, dtype)?;
let batch1 = PrimitiveArray::from_option_iter([Some(1i32), Some(2)]).into_array();
acc.accumulate(&batch1, &mut ctx)?;
// All-null batch must not clobber the previously-stored value.
let batch2 = PrimitiveArray::from_option_iter::<i32, _>([None, None]).into_array();
acc.accumulate(&batch2, &mut ctx)?;
let batch3 = PrimitiveArray::from_option_iter([Some(99i32), None]).into_array();
acc.accumulate(&batch3, &mut ctx)?;
// Last is never saturated; later batches keep updating it.
assert!(!acc.is_saturated());
let result = acc.finish()?;
assert_eq!(result, Scalar::primitive(99i32, Nullable));
Ok(())
}
#[test]
fn last_finish_resets_state() -> VortexResult<()> {
let mut ctx = LEGACY_SESSION.create_execution_ctx();
let dtype = DType::Primitive(PType::I32, Nullability::NonNullable);
let mut acc = Accumulator::try_new(Last, EmptyOptions, dtype)?;
let batch1 = PrimitiveArray::new(buffer![10i32, 20], Validity::NonNullable).into_array();
acc.accumulate(&batch1, &mut ctx)?;
assert_eq!(acc.finish()?, Scalar::primitive(20i32, Nullable));
let batch2 = PrimitiveArray::new(buffer![3i32, 6, 9], Validity::NonNullable).into_array();
acc.accumulate(&batch2, &mut ctx)?;
assert_eq!(acc.finish()?, Scalar::primitive(9i32, Nullable));
Ok(())
}
#[test]
fn last_state_merge() -> VortexResult<()> {
let dtype = DType::Primitive(PType::I32, Nullability::NonNullable);
let mut state = Last.empty_partial(&EmptyOptions, &dtype)?;
Last.combine_partials(&mut state, Scalar::primitive(5i32, Nullable))?;
assert_eq!(Last.to_scalar(&state)?, Scalar::primitive(5i32, Nullable));
// A later non-null partial replaces the prior value.
Last.combine_partials(&mut state, Scalar::primitive(7i32, Nullable))?;
assert_eq!(Last.to_scalar(&state)?, Scalar::primitive(7i32, Nullable));
// A null partial must not clobber the stored value.
Last.combine_partials(&mut state, Scalar::null(dtype.as_nullable()))?;
assert_eq!(Last.to_scalar(&state)?, Scalar::primitive(7i32, Nullable));
Ok(())
}
#[test]
fn last_chunked() -> VortexResult<()> {
let chunk1 = PrimitiveArray::from_option_iter([Some(42i32), Some(100)]);
let chunk2 = PrimitiveArray::from_option_iter::<i32, _>([None, None]);
let dtype = chunk1.dtype().clone();
let chunked = ChunkedArray::try_new(vec![chunk1.into_array(), chunk2.into_array()], dtype)?;
let mut ctx = LEGACY_SESSION.create_execution_ctx();
assert_eq!(
last(&chunked.into_array(), &mut ctx)?,
Scalar::primitive(100i32, Nullable)
);
Ok(())
}
}