-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathmod.rs
More file actions
321 lines (279 loc) · 10 KB
/
mod.rs
File metadata and controls
321 lines (279 loc) · 10 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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
use std::hash::Hash;
use std::sync::Arc;
use itertools::Itertools;
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::AnyCanonical;
use crate::ArrayRef;
use crate::Canonical;
use crate::EmptyMetadata;
use crate::ExecutionCtx;
use crate::ExecutionResult;
use crate::IntoArray;
use crate::Precision;
use crate::ToCanonical;
use crate::arrays::ChunkedArray;
use crate::arrays::PrimitiveArray;
use crate::arrays::chunked::compute::kernel::PARENT_KERNELS;
use crate::arrays::chunked::compute::rules::PARENT_RULES;
use crate::arrays::chunked::vtable::canonical::_canonicalize;
use crate::buffer::BufferHandle;
use crate::builders::ArrayBuilder;
use crate::dtype::DType;
use crate::dtype::Nullability;
use crate::dtype::PType;
use crate::hash::ArrayEq;
use crate::hash::ArrayHash;
use crate::serde::ArrayChildren;
use crate::stats::StatsSetRef;
use crate::validity::Validity;
use crate::vtable;
use crate::vtable::Array;
use crate::vtable::ArrayId;
use crate::vtable::VTable;
mod canonical;
mod operations;
mod validity;
vtable!(Chunked);
#[derive(Clone, Debug)]
pub struct Chunked;
impl Chunked {
pub const ID: ArrayId = ArrayId::new_ref("vortex.chunked");
}
impl VTable for Chunked {
type Array = ChunkedArray;
type Metadata = EmptyMetadata;
type OperationsVTable = Self;
type ValidityVTable = Self;
fn vtable(_array: &Self::Array) -> &Self {
&Chunked
}
fn id(&self) -> ArrayId {
Self::ID
}
fn len(array: &ChunkedArray) -> usize {
array.len
}
fn dtype(array: &ChunkedArray) -> &DType {
&array.dtype
}
fn stats(array: &ChunkedArray) -> StatsSetRef<'_> {
array.stats_set.to_ref(array.as_ref())
}
fn array_hash<H: std::hash::Hasher>(array: &ChunkedArray, state: &mut H, precision: Precision) {
array.dtype.hash(state);
array.len.hash(state);
array
.chunk_offsets_array()
.as_ref()
.array_hash(state, precision);
for chunk in array.iter_chunks() {
chunk.array_hash(state, precision);
}
}
fn array_eq(array: &ChunkedArray, other: &ChunkedArray, precision: Precision) -> bool {
array.dtype == other.dtype
&& array.len == other.len
&& array
.chunk_offsets_array()
.as_ref()
.array_eq(other.chunk_offsets_array().as_ref(), precision)
&& array.nchunks() == other.nchunks()
&& array
.iter_chunks()
.zip(other.iter_chunks())
.all(|(a, b)| a.array_eq(b, precision))
}
fn nbuffers(_array: &ChunkedArray) -> usize {
0
}
fn buffer(_array: &ChunkedArray, idx: usize) -> BufferHandle {
vortex_panic!("ChunkedArray buffer index {idx} out of bounds")
}
fn buffer_name(_array: &ChunkedArray, _idx: usize) -> Option<String> {
None
}
fn metadata(_array: &ChunkedArray) -> VortexResult<Self::Metadata> {
Ok(EmptyMetadata)
}
fn serialize(_metadata: Self::Metadata) -> VortexResult<Option<Vec<u8>>> {
Ok(Some(vec![]))
}
fn deserialize(
_bytes: &[u8],
_dtype: &DType,
_len: usize,
_buffers: &[BufferHandle],
_session: &VortexSession,
) -> VortexResult<Self::Metadata> {
Ok(EmptyMetadata)
}
fn build(
dtype: &DType,
_len: usize,
_metadata: &Self::Metadata,
_buffers: &[BufferHandle],
children: &dyn ArrayChildren,
) -> VortexResult<ChunkedArray> {
if children.is_empty() {
vortex_bail!("Chunked array needs at least one child");
}
let nchunks = children.len() - 1;
// The first child contains the row offsets of the chunks
let chunk_offsets_array = children
.get(
0,
&DType::Primitive(PType::U64, Nullability::NonNullable),
// 1 extra offset for the end of the last chunk
nchunks + 1,
)?
.to_primitive();
let chunk_offsets_buf = chunk_offsets_array.to_buffer::<u64>();
// The remaining children contain the actual data of the chunks
let chunks: Vec<ArrayRef> = chunk_offsets_buf
.iter()
.tuple_windows()
.enumerate()
.map(|(idx, (start, end))| {
let chunk_len = usize::try_from(end - start)
.map_err(|_| vortex_err!("chunk_len {} exceeds usize range", end - start))?;
children.get(idx + 1, dtype, chunk_len)
})
.try_collect()?;
let chunk_offsets = PrimitiveArray::new(chunk_offsets_buf.clone(), Validity::NonNullable);
let total_len = chunk_offsets_buf
.last()
.ok_or_else(|| vortex_err!("chunk_offsets must not be empty"))?;
let len = usize::try_from(*total_len)
.map_err(|_| vortex_err!("total length {} exceeds usize range", total_len))?;
// Construct directly using slots to avoid recomputing chunk_offsets
let mut slots = Vec::with_capacity(1 + chunks.len());
slots.push(Some(chunk_offsets.into_array()));
slots.extend(chunks.into_iter().map(Some));
Ok(ChunkedArray {
dtype: dtype.clone(),
len,
slots,
stats_set: Default::default(),
})
}
fn append_to_builder(
array: &ChunkedArray,
builder: &mut dyn ArrayBuilder,
ctx: &mut ExecutionCtx,
) -> VortexResult<()> {
for chunk in array.iter_chunks() {
chunk.append_to_builder(builder, ctx)?;
}
Ok(())
}
fn execute(array: Arc<Array<Self>>, ctx: &mut ExecutionCtx) -> VortexResult<ExecutionResult> {
// Iteratively request execution of each chunk until it reaches canonical form.
// This gives the scheduler visibility into child execution and enables
// cross-step optimizations between chunk decoding steps.
for i in 0..array.nchunks() {
if !array.chunk(i).is::<AnyCanonical>() {
return Ok(ExecutionResult::execute_slot::<AnyCanonical>(array, i + 1));
}
}
// All chunks are now canonical — combine them.
Ok(ExecutionResult::done(
_canonicalize(&array, ctx)?.into_array(),
))
}
fn slots(array: &ChunkedArray) -> &[Option<ArrayRef>] {
&array.slots
}
fn slot_name(_array: &ChunkedArray, idx: usize) -> String {
match idx {
0 => "chunk_offsets".to_string(),
n => format!("chunks[{}]", n - 1),
}
}
fn with_slots(array: &mut ChunkedArray, slots: Vec<Option<ArrayRef>>) -> VortexResult<()> {
vortex_ensure!(!slots.is_empty(), "Chunked array needs at least one slot");
let chunk_offsets = slots[0]
.as_ref()
.ok_or_else(|| vortex_err!("Chunked array chunk_offsets slot must be present"))?;
let chunk_offsets_dtype = DType::Primitive(PType::U64, Nullability::NonNullable);
vortex_ensure!(
chunk_offsets.dtype() == &chunk_offsets_dtype,
MismatchedTypes: &chunk_offsets_dtype,
chunk_offsets.dtype()
);
#[cfg(debug_assertions)]
{
let chunk_offsets = chunk_offsets
.as_opt::<crate::arrays::Primitive>()
.unwrap_or_else(|| {
vortex_panic!("Chunked array chunk_offsets slot must be primitive")
});
let chunk_offsets_buf = chunk_offsets.to_buffer::<u64>();
debug_assert_eq!(
chunk_offsets_buf.len(),
slots.len(),
"Expected {} chunk offsets, found {}",
slots.len(),
chunk_offsets_buf.len(),
);
debug_assert_eq!(
chunk_offsets_buf.last().copied().unwrap_or_default(),
array.len as u64,
"Chunked array replacement changed logical len: expected {}, got {}",
array.len,
chunk_offsets_buf.last().copied().unwrap_or_default(),
);
let mut total_len = 0usize;
for (idx, chunk_slot) in slots[1..].iter().enumerate() {
let chunk = chunk_slot.as_ref().unwrap_or_else(|| {
vortex_panic!("Chunked array chunk slot {idx} must be present")
});
debug_assert!(
chunk.dtype() == array.dtype(),
"Chunked array chunk slot {} has dtype {:?}, expected {:?}",
idx,
chunk.dtype(),
array.dtype(),
);
total_len = total_len.checked_add(chunk.len()).unwrap_or_else(|| {
vortex_panic!("Chunked array chunk lengths exceed usize range")
});
}
debug_assert_eq!(
total_len, array.len,
"Chunked array replacement changed logical len: expected {}, got {}",
array.len, total_len,
);
}
array.slots = slots;
Ok(())
}
fn reduce(array: &Array<Self>) -> VortexResult<Option<ArrayRef>> {
Ok(match array.nchunks() {
0 => Some(Canonical::empty(array.dtype()).into_array()),
1 => Some(array.chunk(0).clone()),
_ => None,
})
}
fn reduce_parent(
array: &Array<Self>,
parent: &ArrayRef,
child_idx: usize,
) -> VortexResult<Option<ArrayRef>> {
PARENT_RULES.evaluate(array, parent, child_idx)
}
fn execute_parent(
array: &Array<Self>,
parent: &ArrayRef,
child_idx: usize,
ctx: &mut ExecutionCtx,
) -> VortexResult<Option<ArrayRef>> {
PARENT_KERNELS.execute(array, parent, child_idx, ctx)
}
}