-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy patharray.rs
More file actions
354 lines (307 loc) · 10.2 KB
/
array.rs
File metadata and controls
354 lines (307 loc) · 10.2 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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
use std::fmt::Debug;
use std::hash::Hash;
use vortex_array::Array;
use vortex_array::ArrayEq;
use vortex_array::ArrayHash;
use vortex_array::ArrayRef;
use vortex_array::DeserializeMetadata;
use vortex_array::ExecutionCtx;
use vortex_array::ExecutionStep;
use vortex_array::IntoArray;
use vortex_array::Precision;
use vortex_array::ProstMetadata;
use vortex_array::SerializeMetadata;
use vortex_array::buffer::BufferHandle;
use vortex_array::dtype::DType;
use vortex_array::dtype::Nullability;
use vortex_array::dtype::PType;
use vortex_array::serde::ArrayChildren;
use vortex_array::stats::ArrayStats;
use vortex_array::stats::StatsSetRef;
use vortex_array::vtable;
use vortex_array::vtable::ArrayId;
use vortex_array::vtable::VTable;
use vortex_array::vtable::ValidityChild;
use vortex_array::vtable::ValidityVTableFromChild;
use vortex_error::VortexExpect;
use vortex_error::VortexResult;
use vortex_error::vortex_bail;
use vortex_error::vortex_ensure;
use vortex_error::vortex_err;
use vortex_error::vortex_panic;
use vortex_session::VortexSession;
use crate::canonical::decode_to_temporal;
use crate::compute::kernel::PARENT_KERNELS;
use crate::compute::rules::PARENT_RULES;
vtable!(DateTimeParts);
#[derive(Clone, prost::Message)]
#[repr(C)]
pub struct DateTimePartsMetadata {
// Validity lives in the days array
// TODO(ngates): we should actually model this with a Tuple array when we have one.
#[prost(enumeration = "PType", tag = "1")]
pub days_ptype: i32,
#[prost(enumeration = "PType", tag = "2")]
pub seconds_ptype: i32,
#[prost(enumeration = "PType", tag = "3")]
pub subseconds_ptype: i32,
}
impl DateTimePartsMetadata {
pub fn get_days_ptype(&self) -> VortexResult<PType> {
PType::try_from(self.days_ptype)
.map_err(|_| vortex_err!("Invalid PType {}", self.days_ptype))
}
pub fn get_seconds_ptype(&self) -> VortexResult<PType> {
PType::try_from(self.seconds_ptype)
.map_err(|_| vortex_err!("Invalid PType {}", self.seconds_ptype))
}
pub fn get_subseconds_ptype(&self) -> VortexResult<PType> {
PType::try_from(self.subseconds_ptype)
.map_err(|_| vortex_err!("Invalid PType {}", self.subseconds_ptype))
}
}
impl VTable for DateTimePartsVTable {
type Array = DateTimePartsArray;
type Metadata = ProstMetadata<DateTimePartsMetadata>;
type OperationsVTable = Self;
type ValidityVTable = ValidityVTableFromChild;
fn id(_array: &Self::Array) -> ArrayId {
Self::ID
}
fn len(array: &DateTimePartsArray) -> usize {
array.days.len()
}
fn dtype(array: &DateTimePartsArray) -> &DType {
&array.dtype
}
fn stats(array: &DateTimePartsArray) -> StatsSetRef<'_> {
array.stats_set.to_ref(array.as_ref())
}
fn array_hash<H: std::hash::Hasher>(
array: &DateTimePartsArray,
state: &mut H,
precision: Precision,
) {
array.dtype.hash(state);
array.days.array_hash(state, precision);
array.seconds.array_hash(state, precision);
array.subseconds.array_hash(state, precision);
}
fn array_eq(
array: &DateTimePartsArray,
other: &DateTimePartsArray,
precision: Precision,
) -> bool {
array.dtype == other.dtype
&& array.days.array_eq(&other.days, precision)
&& array.seconds.array_eq(&other.seconds, precision)
&& array.subseconds.array_eq(&other.subseconds, precision)
}
fn nbuffers(_array: &DateTimePartsArray) -> usize {
0
}
fn buffer(_array: &DateTimePartsArray, idx: usize) -> BufferHandle {
vortex_panic!("DateTimePartsArray buffer index {idx} out of bounds")
}
fn buffer_name(_array: &DateTimePartsArray, idx: usize) -> Option<String> {
vortex_panic!("DateTimePartsArray buffer_name index {idx} out of bounds")
}
fn nchildren(_array: &DateTimePartsArray) -> usize {
3
}
fn child(array: &DateTimePartsArray, idx: usize) -> ArrayRef {
match idx {
0 => array.days().clone(),
1 => array.seconds().clone(),
2 => array.subseconds().clone(),
_ => vortex_panic!("DateTimePartsArray child index {idx} out of bounds"),
}
}
fn child_name(_array: &DateTimePartsArray, idx: usize) -> String {
match idx {
0 => "days".to_string(),
1 => "seconds".to_string(),
2 => "subseconds".to_string(),
_ => vortex_panic!("DateTimePartsArray child_name index {idx} out of bounds"),
}
}
fn metadata(array: &DateTimePartsArray) -> VortexResult<Self::Metadata> {
Ok(ProstMetadata(DateTimePartsMetadata {
days_ptype: PType::try_from(array.days().dtype())? as i32,
seconds_ptype: PType::try_from(array.seconds().dtype())? as i32,
subseconds_ptype: PType::try_from(array.subseconds().dtype())? as i32,
}))
}
fn serialize(metadata: Self::Metadata) -> VortexResult<Option<Vec<u8>>> {
Ok(Some(metadata.serialize()))
}
fn deserialize(
bytes: &[u8],
_dtype: &DType,
_len: usize,
_buffers: &[BufferHandle],
_session: &VortexSession,
) -> VortexResult<Self::Metadata> {
Ok(ProstMetadata(
<ProstMetadata<DateTimePartsMetadata> as DeserializeMetadata>::deserialize(bytes)?,
))
}
fn build(
dtype: &DType,
len: usize,
metadata: &Self::Metadata,
_buffers: &[BufferHandle],
children: &dyn ArrayChildren,
) -> VortexResult<DateTimePartsArray> {
if children.len() != 3 {
vortex_bail!(
"Expected 3 children for datetime-parts encoding, found {}",
children.len()
)
}
let days = children.get(
0,
&DType::Primitive(metadata.0.get_days_ptype()?, dtype.nullability()),
len,
)?;
let seconds = children.get(
1,
&DType::Primitive(metadata.0.get_seconds_ptype()?, Nullability::NonNullable),
len,
)?;
let subseconds = children.get(
2,
&DType::Primitive(metadata.0.get_subseconds_ptype()?, Nullability::NonNullable),
len,
)?;
DateTimePartsArray::try_new(dtype.clone(), days, seconds, subseconds)
}
fn with_children(array: &mut Self::Array, children: Vec<ArrayRef>) -> VortexResult<()> {
vortex_ensure!(
children.len() == 3,
"DateTimePartsArray expects exactly 3 children (days, seconds, subseconds), got {}",
children.len()
);
let mut children_iter = children.into_iter();
array.days = children_iter.next().vortex_expect("checked");
array.seconds = children_iter.next().vortex_expect("checked");
array.subseconds = children_iter.next().vortex_expect("checked");
Ok(())
}
fn execute(array: &Self::Array, ctx: &mut ExecutionCtx) -> VortexResult<ExecutionStep> {
Ok(ExecutionStep::Done(
decode_to_temporal(array, ctx)?.into_array(),
))
}
fn reduce_parent(
array: &Self::Array,
parent: &ArrayRef,
child_idx: usize,
) -> VortexResult<Option<ArrayRef>> {
PARENT_RULES.evaluate(array, parent, child_idx)
}
fn execute_parent(
array: &Self::Array,
parent: &ArrayRef,
child_idx: usize,
ctx: &mut ExecutionCtx,
) -> VortexResult<Option<ArrayRef>> {
PARENT_KERNELS.execute(array, parent, child_idx, ctx)
}
}
#[derive(Clone, Debug)]
pub struct DateTimePartsArray {
dtype: DType,
days: ArrayRef,
seconds: ArrayRef,
subseconds: ArrayRef,
stats_set: ArrayStats,
}
#[derive(Clone, Debug)]
pub struct DateTimePartsArrayParts {
pub dtype: DType,
pub days: ArrayRef,
pub seconds: ArrayRef,
pub subseconds: ArrayRef,
}
#[derive(Debug)]
pub struct DateTimePartsVTable;
impl DateTimePartsVTable {
pub const ID: ArrayId = ArrayId::new_ref("vortex.datetimeparts");
}
impl DateTimePartsArray {
pub fn try_new(
dtype: DType,
days: ArrayRef,
seconds: ArrayRef,
subseconds: ArrayRef,
) -> VortexResult<Self> {
if !days.dtype().is_int() || (dtype.is_nullable() != days.dtype().is_nullable()) {
vortex_bail!(
"Expected integer with nullability {}, got {}",
dtype.is_nullable(),
days.dtype()
);
}
if !seconds.dtype().is_int() || seconds.dtype().is_nullable() {
vortex_bail!(MismatchedTypes: "non-nullable integer", seconds.dtype());
}
if !subseconds.dtype().is_int() || subseconds.dtype().is_nullable() {
vortex_bail!(MismatchedTypes: "non-nullable integer", subseconds.dtype());
}
let length = days.len();
if length != seconds.len() || length != subseconds.len() {
vortex_bail!(
"Mismatched lengths {} {} {}",
days.len(),
seconds.len(),
subseconds.len()
);
}
Ok(Self {
dtype,
days,
seconds,
subseconds,
stats_set: Default::default(),
})
}
pub(crate) unsafe fn new_unchecked(
dtype: DType,
days: ArrayRef,
seconds: ArrayRef,
subseconds: ArrayRef,
) -> Self {
Self {
dtype,
days,
seconds,
subseconds,
stats_set: Default::default(),
}
}
pub fn into_parts(self) -> DateTimePartsArrayParts {
DateTimePartsArrayParts {
dtype: self.dtype,
days: self.days,
seconds: self.seconds,
subseconds: self.subseconds,
}
}
pub fn days(&self) -> &ArrayRef {
&self.days
}
pub fn seconds(&self) -> &ArrayRef {
&self.seconds
}
pub fn subseconds(&self) -> &ArrayRef {
&self.subseconds
}
}
impl ValidityChild<DateTimePartsVTable> for DateTimePartsVTable {
fn validity_child(array: &DateTimePartsArray) -> &ArrayRef {
array.days()
}
}