forked from vortex-data/vortex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnormalize.rs
More file actions
232 lines (198 loc) · 7.82 KB
/
normalize.rs
File metadata and controls
232 lines (198 loc) · 7.82 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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
use smallvec::SmallVec;
use vortex_error::VortexResult;
use vortex_error::vortex_bail;
use vortex_session::registry::Id;
use vortex_utils::aliases::hash_set::HashSet;
use crate::ArrayRef;
use crate::ExecutionCtx;
/// Options for normalizing an array.
pub struct NormalizeOptions<'a> {
/// The set of allowed array encodings (in addition to the canonical ones) that are permitted
/// in the normalized array.
pub allowed: &'a HashSet<Id>,
/// The operation to perform when a non-allowed encoding is encountered.
pub operation: Operation<'a>,
}
/// The operation to perform when a non-allowed encoding is encountered.
pub enum Operation<'a> {
Error,
Execute(&'a mut ExecutionCtx),
}
impl ArrayRef {
/// Normalize the array according to given options.
///
/// This operation performs a recursive traversal of the array. Any non-allowed encoding is
/// normalized per the configured operation.
pub fn normalize(self, options: &mut NormalizeOptions) -> VortexResult<ArrayRef> {
match &mut options.operation {
Operation::Error => {
self.normalize_with_error(options.allowed)?;
// Note this takes ownership so we can at a later date remove non-allowed encodings.
Ok(self)
}
Operation::Execute(ctx) => self.normalize_with_execution(options.allowed, ctx),
}
}
fn normalize_with_error(&self, allowed: &HashSet<Id>) -> VortexResult<()> {
if !self.is_allowed_encoding(allowed) {
vortex_bail!(AssertionFailed: "normalize forbids encoding ({})", self.encoding_id())
}
for child in self.children() {
child.normalize_with_error(allowed)?
}
Ok(())
}
fn normalize_with_execution(
self,
allowed: &HashSet<Id>,
ctx: &mut ExecutionCtx,
) -> VortexResult<ArrayRef> {
let mut normalized = self;
// Top-first execute the array tree while we hit non-allowed encodings.
while !normalized.is_allowed_encoding(allowed) {
normalized = normalized.execute(ctx)?;
}
// Now we've normalized the root, we need to ensure the children are normalized also.
let slots = normalized.slots();
let mut normalized_slots = SmallVec::with_capacity(slots.len());
let mut any_slot_changed = false;
for slot in slots {
match slot {
Some(child) => {
let normalized_child = child.clone().normalize(&mut NormalizeOptions {
allowed,
operation: Operation::Execute(ctx),
})?;
any_slot_changed |= !ArrayRef::ptr_eq(child, &normalized_child);
normalized_slots.push(Some(normalized_child));
}
None => normalized_slots.push(None),
}
}
if any_slot_changed {
normalized = normalized.with_slots(normalized_slots)?;
}
Ok(normalized)
}
fn is_allowed_encoding(&self, allowed: &HashSet<Id>) -> bool {
allowed.contains(&self.encoding_id()) || self.is_canonical()
}
}
#[cfg(test)]
mod tests {
use vortex_error::VortexResult;
use vortex_session::VortexSession;
use vortex_utils::aliases::hash_set::HashSet;
use super::NormalizeOptions;
use super::Operation;
use crate::ArrayRef;
use crate::ExecutionCtx;
use crate::IntoArray;
use crate::array::VTable;
use crate::arrays::Dict;
use crate::arrays::DictArray;
use crate::arrays::Primitive;
use crate::arrays::PrimitiveArray;
use crate::arrays::Slice;
use crate::arrays::SliceArray;
use crate::arrays::StructArray;
use crate::assert_arrays_eq;
use crate::validity::Validity;
#[test]
fn normalize_with_execution_keeps_parent_when_children_are_unchanged() -> VortexResult<()> {
let field = PrimitiveArray::from_iter(0i32..4).into_array();
let array = StructArray::try_new(
["field"].into(),
vec![field.clone()],
field.len(),
Validity::NonNullable,
)?
.into_array();
let allowed = HashSet::from_iter([array.encoding_id(), field.encoding_id()]);
let mut ctx = ExecutionCtx::new(VortexSession::empty());
let normalized = array.clone().normalize(&mut NormalizeOptions {
allowed: &allowed,
operation: Operation::Execute(&mut ctx),
})?;
assert!(ArrayRef::ptr_eq(&array, &normalized));
Ok(())
}
#[test]
fn normalize_with_error_allows_canonical_arrays() -> VortexResult<()> {
let field = PrimitiveArray::from_iter(0i32..4).into_array();
let array = StructArray::try_new(
["field"].into(),
vec![field.clone()],
field.len(),
Validity::NonNullable,
)?
.into_array();
let allowed = HashSet::default();
let normalized = array.clone().normalize(&mut NormalizeOptions {
allowed: &allowed,
operation: Operation::Error,
})?;
assert!(ArrayRef::ptr_eq(&array, &normalized));
Ok(())
}
#[test]
fn normalize_with_execution_rebuilds_parent_when_a_child_changes() -> VortexResult<()> {
let unchanged = PrimitiveArray::from_iter(0i32..4).into_array();
let sliced =
SliceArray::new(PrimitiveArray::from_iter(10i32..20).into_array(), 2..6).into_array();
let array = StructArray::try_new(
["lhs", "rhs"].into(),
vec![unchanged.clone(), sliced],
unchanged.len(),
Validity::NonNullable,
)?
.into_array();
let allowed = HashSet::from_iter([array.encoding_id(), unchanged.encoding_id()]);
let mut ctx = ExecutionCtx::new(VortexSession::empty());
let normalized = array.clone().normalize(&mut NormalizeOptions {
allowed: &allowed,
operation: Operation::Execute(&mut ctx),
})?;
assert!(!ArrayRef::ptr_eq(&array, &normalized));
let original_children = array.children();
let normalized_children = normalized.children();
assert!(ArrayRef::ptr_eq(
&original_children[0],
&normalized_children[0]
));
assert!(!ArrayRef::ptr_eq(
&original_children[1],
&normalized_children[1]
));
assert_arrays_eq!(normalized_children[1], PrimitiveArray::from_iter(12i32..16));
Ok(())
}
#[test]
fn normalize_slice_of_dict_returns_dict() -> VortexResult<()> {
let codes = PrimitiveArray::from_iter(vec![0u32, 1, 0, 1, 2]).into_array();
let values = PrimitiveArray::from_iter(vec![10i32, 20, 30]).into_array();
let dict = DictArray::try_new(codes, values)?.into_array();
// Slice the dict array to get a SliceArray wrapping a DictArray.
let sliced = SliceArray::new(dict, 1..4).into_array();
assert_eq!(sliced.encoding_id(), Slice.id());
let allowed = HashSet::from_iter([Dict.id(), Primitive.id()]);
let mut ctx = ExecutionCtx::new(VortexSession::empty());
let normalized = sliced.normalize(&mut NormalizeOptions {
allowed: &allowed,
operation: Operation::Execute(&mut ctx),
})?;
// The normalized result should be a DictArray, not a SliceArray.
assert_eq!(normalized.encoding_id(), Dict.id());
assert_eq!(normalized.len(), 3);
// Verify the data: codes [1,0,1] -> values [20, 10, 20]
#[expect(deprecated)]
let normalized_canonical = normalized.to_canonical()?;
assert_arrays_eq!(
normalized_canonical,
PrimitiveArray::from_iter(vec![20i32, 10, 20])
);
Ok(())
}
}