-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathrules.rs
More file actions
317 lines (289 loc) · 10.8 KB
/
Copy pathrules.rs
File metadata and controls
317 lines (289 loc) · 10.8 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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
use vortex_error::VortexResult;
use vortex_error::vortex_ensure;
use vortex_error::vortex_err;
use crate::ArrayRef;
use crate::IntoArray;
use crate::arrays::ConstantArray;
use crate::arrays::Struct;
use crate::arrays::StructArray;
use crate::arrays::dict::TakeReduceAdaptor;
use crate::arrays::scalar_fn::ExactScalarFn;
use crate::arrays::scalar_fn::ScalarFnArrayExt;
use crate::arrays::scalar_fn::ScalarFnArrayView;
use crate::arrays::slice::SliceReduceAdaptor;
use crate::builtins::ArrayBuiltins;
use crate::optimizer::rules::ArrayParentReduceRule;
use crate::optimizer::rules::ParentRuleSet;
use crate::scalar_fn::EmptyOptions;
use crate::scalar_fn::fns::cast::Cast;
use crate::scalar_fn::fns::get_item::GetItem;
use crate::scalar_fn::fns::mask::Mask;
use crate::scalar_fn::fns::mask::MaskReduceAdaptor;
use crate::validity::Validity;
use crate::vtable::ValidityHelper;
pub(crate) const PARENT_RULES: ParentRuleSet<Struct> = ParentRuleSet::new(&[
ParentRuleSet::lift(&StructCastPushDownRule),
ParentRuleSet::lift(&StructGetItemRule),
ParentRuleSet::lift(&MaskReduceAdaptor(Struct)),
ParentRuleSet::lift(&SliceReduceAdaptor(Struct)),
ParentRuleSet::lift(&TakeReduceAdaptor(Struct)),
]);
/// Rule to push down cast into struct fields.
///
/// TODO(joe/rob): should be have this in casts.
///
/// This rule supports schema evolution by allowing new nullable fields to be added
/// at the end of the struct, filled with null values.
#[derive(Debug)]
struct StructCastPushDownRule;
impl ArrayParentReduceRule<Struct> for StructCastPushDownRule {
type Parent = ExactScalarFn<Cast>;
fn reduce_parent(
&self,
array: &StructArray,
parent: ScalarFnArrayView<Cast>,
_child_idx: usize,
) -> VortexResult<Option<ArrayRef>> {
let Some(target_fields) = parent.options.as_struct_fields_opt() else {
return Ok(None);
};
let mut new_fields = Vec::with_capacity(target_fields.nfields());
for (target_name, target_dtype) in target_fields.names().iter().zip(target_fields.fields())
{
match array.unmasked_field_by_name(target_name).ok() {
Some(field) => {
new_fields.push(field.cast(target_dtype)?);
}
None => {
// Not found - create NULL array (schema evolution)
vortex_ensure!(
target_dtype.is_nullable(),
"Cannot add non-nullable field '{}' during struct cast",
target_name
);
new_fields.push(
ConstantArray::new(crate::scalar::Scalar::null(target_dtype), array.len())
.into_array(),
);
}
}
}
let validity = if parent.options.is_nullable() {
array.validity().clone().into_nullable()
} else {
array
.validity()
.clone()
.into_non_nullable(array.len)
.ok_or_else(|| vortex_err!("Failed to cast nullable struct to non-nullable"))?
};
let new_struct = unsafe {
StructArray::new_unchecked(new_fields, target_fields.clone(), array.len(), validity)
};
Ok(Some(new_struct.into_array()))
}
}
/// Rule to flatten get_item from struct by field name
#[derive(Debug)]
pub(crate) struct StructGetItemRule;
impl ArrayParentReduceRule<Struct> for StructGetItemRule {
type Parent = ExactScalarFn<GetItem>;
fn reduce_parent(
&self,
child: &StructArray,
parent: ScalarFnArrayView<'_, GetItem>,
_child_idx: usize,
) -> VortexResult<Option<ArrayRef>> {
let field_name = parent.options;
let Some(field) = child.unmasked_field_by_name_opt(field_name) else {
return Ok(None);
};
match child.validity() {
Validity::NonNullable | Validity::AllValid => {
// If the struct is non-nullable or all valid, the field's validity is unchanged
Ok(Some(field.clone()))
}
Validity::AllInvalid => {
// If everything is invalid, the field is also all invalid
Ok(Some(
ConstantArray::new(
crate::scalar::Scalar::null(field.dtype().clone()),
field.len(),
)
.into_array(),
))
}
Validity::Array(mask) => {
// If the validity is an array, we need to combine it with the field's validity
Mask.try_new_array(field.len(), EmptyOptions, [field.clone(), mask.clone()])
.map(Some)
}
}
}
}
#[cfg(test)]
mod tests {
use vortex_buffer::buffer;
use crate::IntoArray;
use crate::arrays::StructArray;
use crate::arrays::VarBinViewArray;
use crate::arrays::struct_::compute::rules::ConstantArray;
use crate::assert_arrays_eq;
use crate::builtins::ArrayBuiltins;
use crate::canonical::ToCanonical;
use crate::dtype::DType;
use crate::dtype::FieldNames;
use crate::dtype::Nullability;
use crate::dtype::PType;
use crate::dtype::StructFields;
use crate::scalar::Scalar;
use crate::validity::Validity;
#[test]
fn test_struct_cast_field_reorder() {
// Source: {a, b}, Target: {c, b, a} - reordered + new null field
let source = StructArray::try_new(
FieldNames::from(["a", "b"]),
vec![
VarBinViewArray::from_iter_str(["A"]).into_array(),
VarBinViewArray::from_iter_str(["B"]).into_array(),
],
1,
Validity::NonNullable,
)
.unwrap();
let utf8_null = DType::Utf8(Nullability::Nullable);
let target = DType::Struct(
StructFields::new(
FieldNames::from(["c", "b", "a"]),
vec![utf8_null.clone(); 3],
),
Nullability::NonNullable,
);
// Use `ArrayBuiltins::cast` which goes through the optimizer and applies
// `StructCastPushDownRule`.
let result = source.into_array().cast(target).unwrap().to_struct();
assert_arrays_eq!(
result.unmasked_field_by_name("a").unwrap(),
VarBinViewArray::from_iter_nullable_str([Some("A")])
);
assert_arrays_eq!(
result.unmasked_field_by_name("b").unwrap(),
VarBinViewArray::from_iter_nullable_str([Some("B")])
);
assert_arrays_eq!(
result.unmasked_field_by_name("c").unwrap(),
ConstantArray::new(Scalar::null(utf8_null), 1)
);
}
/// Regression test: casting a struct to a non-struct DType must not panic. Previously,
/// `StructCastPushDownRule` called `as_struct_fields()` which panics on non-struct types.
#[test]
fn cast_struct_to_non_struct_does_not_panic() {
let source = StructArray::try_new(
FieldNames::from(["x"]),
vec![buffer![1i32, 2, 3].into_array()],
3,
Validity::NonNullable,
)
.unwrap();
// Casting a struct to a primitive type should not panic. Before the fix,
// `StructCastPushDownRule` would panic via `as_struct_fields()` on the non-struct target.
let result = source
.into_array()
.cast(DType::Primitive(PType::I32, Nullability::NonNullable));
// Whether this errors or succeeds depends on execution, but the key invariant is that the
// optimizer rule does not panic.
if let Ok(arr) = &result {
assert_eq!(
arr.dtype(),
&DType::Primitive(PType::I32, Nullability::NonNullable)
);
}
}
#[test]
fn cast_struct_drop_field() {
// Casting to a struct with a subset of fields should succeed.
let source = StructArray::try_new(
FieldNames::from(["a", "b", "c"]),
vec![
buffer![1i32, 2, 3].into_array(),
buffer![10i64, 20, 30].into_array(),
buffer![100u8, 200, 255].into_array(),
],
3,
Validity::NonNullable,
)
.unwrap();
let target = DType::Struct(
StructFields::new(
FieldNames::from(["a", "c"]),
vec![
DType::Primitive(PType::I32, Nullability::NonNullable),
DType::Primitive(PType::U8, Nullability::NonNullable),
],
),
Nullability::NonNullable,
);
let result = source.into_array().cast(target).unwrap().to_struct();
assert_eq!(result.unmasked_fields().len(), 2);
assert_arrays_eq!(
result.unmasked_field_by_name("a").unwrap(),
buffer![1i32, 2, 3].into_array()
);
assert_arrays_eq!(
result.unmasked_field_by_name("c").unwrap(),
buffer![100u8, 200, 255].into_array()
);
}
#[test]
fn cast_struct_field_type_widening() {
// Casting struct fields to wider types (i32 -> i64).
let source = StructArray::try_new(
FieldNames::from(["val"]),
vec![buffer![1i32, 2, 3].into_array()],
3,
Validity::NonNullable,
)
.unwrap();
let target = DType::Struct(
StructFields::new(
FieldNames::from(["val"]),
vec![DType::Primitive(PType::I64, Nullability::NonNullable)],
),
Nullability::NonNullable,
);
let result = source.into_array().cast(target).unwrap().to_struct();
assert_eq!(
result.unmasked_field_by_name("val").unwrap().dtype(),
&DType::Primitive(PType::I64, Nullability::NonNullable)
);
assert_arrays_eq!(
result.unmasked_field_by_name("val").unwrap(),
buffer![1i64, 2, 3].into_array()
);
}
#[test]
fn cast_struct_add_non_nullable_field_fails() {
// Adding a non-nullable field via cast should fail.
let source = StructArray::try_new(
FieldNames::from(["a"]),
vec![buffer![1i32].into_array()],
1,
Validity::NonNullable,
)
.unwrap();
let target = DType::Struct(
StructFields::new(
FieldNames::from(["a", "b"]),
vec![
DType::Primitive(PType::I32, Nullability::NonNullable),
DType::Primitive(PType::I32, Nullability::NonNullable),
],
),
Nullability::NonNullable,
);
assert!(source.into_array().cast(target).is_err());
}
}