-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy pathcast.rs
More file actions
335 lines (304 loc) · 12 KB
/
cast.rs
File metadata and controls
335 lines (304 loc) · 12 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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
use num_traits::AsPrimitive;
use vortex_buffer::Buffer;
use vortex_buffer::BufferMut;
use vortex_error::VortexResult;
use vortex_error::vortex_bail;
use crate::ArrayRef;
use crate::ExecutionCtx;
use crate::IntoArray;
use crate::aggregate_fn;
use crate::array::ArrayView;
use crate::arrays::Primitive;
use crate::arrays::PrimitiveArray;
use crate::arrays::primitive::PrimitiveArrayExt;
use crate::dtype::DType;
use crate::dtype::NativePType;
use crate::dtype::Nullability;
use crate::dtype::PType;
use crate::match_each_native_ptype;
use crate::scalar_fn::fns::cast::CastKernel;
impl CastKernel for Primitive {
fn cast(
array: ArrayView<'_, Primitive>,
dtype: &DType,
ctx: &mut ExecutionCtx,
) -> VortexResult<Option<ArrayRef>> {
let DType::Primitive(new_ptype, new_nullability) = dtype else {
return Ok(None);
};
let (new_ptype, new_nullability) = (*new_ptype, *new_nullability);
// First, check that the cast is compatible with the source array's validity
let new_validity = array
.validity()?
.cast_nullability(new_nullability, array.len())?;
// Same ptype: zero-copy, just update validity.
if array.ptype() == new_ptype {
// SAFETY: validity and data buffer still have same length
return Ok(Some(unsafe {
PrimitiveArray::new_unchecked_from_handle(
array.buffer_handle().clone(),
array.ptype(),
new_validity,
)
.into_array()
}));
}
if !values_fit_in(array, new_ptype, ctx) {
vortex_bail!(
Compute: "Cannot cast {} to {} — values exceed target range",
array.ptype(),
new_ptype,
);
}
// Same-width integers have identical bit representations due to 2's
// complement. If all values fit in the target range, reinterpret with
// no allocation.
if array.ptype().is_int()
&& new_ptype.is_int()
&& array.ptype().byte_width() == new_ptype.byte_width()
{
// SAFETY: both types are integers with the same size and alignment, and
// min/max confirm all valid values are representable in the target type.
return Ok(Some(unsafe {
PrimitiveArray::new_unchecked_from_handle(
array.buffer_handle().clone(),
new_ptype,
new_validity,
)
.into_array()
}));
}
// Otherwise, cast the values element-wise.
Ok(Some(match_each_native_ptype!(new_ptype, |T| {
match_each_native_ptype!(array.ptype(), |F| {
PrimitiveArray::new(cast::<F, T>(array.as_slice()), new_validity).into_array()
})
})))
}
}
/// Returns `true` if all valid values in `array` are representable as `target_ptype`.
fn values_fit_in(
array: ArrayView<'_, Primitive>,
target_ptype: PType,
ctx: &mut ExecutionCtx,
) -> bool {
let target_dtype = DType::Primitive(target_ptype, Nullability::NonNullable);
aggregate_fn::fns::min_max::min_max(array.array(), ctx)
.ok()
.flatten()
.is_none_or(|mm| mm.min.cast(&target_dtype).is_ok() && mm.max.cast(&target_dtype).is_ok())
}
/// Caller must ensure all valid values are representable via `values_fit_in`.
/// Out-of-range values at invalid positions are truncated/wrapped by `as`,
/// which is fine because they are masked out by validity.
fn cast<F: NativePType + AsPrimitive<T>, T: NativePType>(array: &[F]) -> Buffer<T> {
BufferMut::from_trusted_len_iter(array.iter().map(|&src| src.as_())).freeze()
}
#[cfg(test)]
mod test {
use rstest::rstest;
use vortex_buffer::BitBuffer;
use vortex_buffer::buffer;
use vortex_error::VortexError;
use vortex_mask::Mask;
use crate::IntoArray;
use crate::arrays::PrimitiveArray;
use crate::assert_arrays_eq;
use crate::builtins::ArrayBuiltins;
use crate::canonical::ToCanonical;
use crate::compute::conformance::cast::test_cast_conformance;
use crate::dtype::DType;
use crate::dtype::Nullability;
use crate::dtype::PType;
use crate::validity::Validity;
#[test]
fn cast_u32_u8() {
let arr = buffer![0u32, 10, 200].into_array();
// cast from u32 to u8
let p = arr.cast(PType::U8.into()).unwrap().to_primitive();
assert_arrays_eq!(p, PrimitiveArray::from_iter([0u8, 10, 200]));
assert!(matches!(p.validity(), Ok(Validity::NonNullable)));
// to nullable
let p = p
.into_array()
.cast(DType::Primitive(PType::U8, Nullability::Nullable))
.unwrap()
.to_primitive();
assert_arrays_eq!(
p,
PrimitiveArray::new(buffer![0u8, 10, 200], Validity::AllValid)
);
assert!(matches!(p.validity(), Ok(Validity::AllValid)));
// back to non-nullable
let p = p
.into_array()
.cast(DType::Primitive(PType::U8, Nullability::NonNullable))
.unwrap()
.to_primitive();
assert_arrays_eq!(p, PrimitiveArray::from_iter([0u8, 10, 200]));
assert!(matches!(p.validity(), Ok(Validity::NonNullable)));
// to nullable u32
let p = p
.into_array()
.cast(DType::Primitive(PType::U32, Nullability::Nullable))
.unwrap()
.to_primitive();
assert_arrays_eq!(
p,
PrimitiveArray::new(buffer![0u32, 10, 200], Validity::AllValid)
);
assert!(matches!(p.validity(), Ok(Validity::AllValid)));
// to non-nullable u8
let p = p
.into_array()
.cast(DType::Primitive(PType::U8, Nullability::NonNullable))
.unwrap()
.to_primitive();
assert_arrays_eq!(p, PrimitiveArray::from_iter([0u8, 10, 200]));
assert!(matches!(p.validity(), Ok(Validity::NonNullable)));
}
#[test]
fn cast_u32_f32() {
let arr = buffer![0u32, 10, 200].into_array();
let u8arr = arr.cast(PType::F32.into()).unwrap().to_primitive();
assert_arrays_eq!(u8arr, PrimitiveArray::from_iter([0.0f32, 10., 200.]));
}
#[test]
fn cast_i32_u32() {
let arr = buffer![-1i32].into_array();
let error = arr
.cast(PType::U32.into())
.and_then(|a| a.to_canonical().map(|c| c.into_array()))
.unwrap_err();
assert!(matches!(error, VortexError::Compute(..)));
assert!(error.to_string().contains("values exceed target range"));
}
#[test]
fn cast_array_with_nulls_to_nonnullable() {
let arr = PrimitiveArray::from_option_iter([Some(-1i32), None, Some(10)]);
let err = arr
.into_array()
.cast(PType::I32.into())
.and_then(|a| a.to_canonical().map(|c| c.into_array()))
.unwrap_err();
assert!(matches!(err, VortexError::InvalidArgument(..)));
assert!(
err.to_string()
.contains("Cannot cast array with invalid values to non-nullable type.")
);
}
#[test]
fn cast_with_invalid_nulls() {
let arr = PrimitiveArray::new(
buffer![-1i32, 0, 10],
Validity::from_iter([false, true, true]),
);
let p = arr
.into_array()
.cast(DType::Primitive(PType::U32, Nullability::Nullable))
.unwrap()
.to_primitive();
assert_arrays_eq!(
p,
PrimitiveArray::from_option_iter([None, Some(0u32), Some(10)])
);
assert_eq!(
p.validity_mask().unwrap(),
Mask::from(BitBuffer::from(vec![false, true, true]))
);
}
/// Same-width integer cast where all values fit: should reinterpret the
/// buffer without allocation (pointer identity).
#[test]
fn cast_same_width_int_reinterprets_buffer() -> vortex_error::VortexResult<()> {
let src = PrimitiveArray::from_iter([0u32, 10, 100]);
let src_ptr = src.as_slice::<u32>().as_ptr();
let dst = src.into_array().cast(PType::I32.into())?.to_primitive();
let dst_ptr = dst.as_slice::<i32>().as_ptr();
// Zero-copy: the data pointer should be identical.
assert_eq!(src_ptr as usize, dst_ptr as usize);
assert_arrays_eq!(dst, PrimitiveArray::from_iter([0i32, 10, 100]));
Ok(())
}
/// Same-width integer cast where values don't fit: should fall through
/// to the allocating path and produce an error.
#[test]
fn cast_same_width_int_out_of_range_errors() {
let arr = buffer![u32::MAX].into_array();
let err = arr
.cast(PType::I32.into())
.and_then(|a| a.to_canonical().map(|c| c.into_array()))
.unwrap_err();
assert!(matches!(err, VortexError::Compute(..)));
}
/// All-null array cast between same-width types should succeed without
/// touching the buffer contents.
#[test]
fn cast_same_width_all_null() -> vortex_error::VortexResult<()> {
let arr = PrimitiveArray::new(buffer![0xFFu8, 0xFF], Validity::AllInvalid);
let casted = arr
.into_array()
.cast(DType::Primitive(PType::I8, Nullability::Nullable))?
.to_primitive();
assert_eq!(casted.len(), 2);
assert!(matches!(casted.validity(), Ok(Validity::AllInvalid)));
Ok(())
}
/// Same-width integer cast with nullable values: out-of-range nulls should
/// not prevent the cast from succeeding.
#[test]
fn cast_same_width_int_nullable_with_out_of_range_nulls() -> vortex_error::VortexResult<()> {
// The null position holds u32::MAX which doesn't fit in i32, but it's
// masked as invalid so the cast should still succeed via reinterpret.
let arr = PrimitiveArray::new(
buffer![u32::MAX, 0u32, 42u32],
Validity::from_iter([false, true, true]),
);
let casted = arr
.into_array()
.cast(DType::Primitive(PType::I32, Nullability::Nullable))?
.to_primitive();
assert_arrays_eq!(
casted,
PrimitiveArray::from_option_iter([None, Some(0i32), Some(42)])
);
Ok(())
}
#[test]
fn cast_u32_to_u8_with_out_of_range_nulls() -> vortex_error::VortexResult<()> {
let arr = PrimitiveArray::new(
buffer![1000u32, 10u32, 42u32],
Validity::from_iter([false, true, true]),
);
let casted = arr
.into_array()
.cast(DType::Primitive(PType::U8, Nullability::Nullable))?
.to_primitive();
assert_arrays_eq!(
casted,
PrimitiveArray::from_option_iter([None, Some(10u8), Some(42)])
);
Ok(())
}
#[rstest]
#[case(buffer![0u8, 1, 2, 3, 255].into_array())]
#[case(buffer![0u16, 100, 1000, 65535].into_array())]
#[case(buffer![0u32, 100, 1000, 1000000].into_array())]
#[case(buffer![0u64, 100, 1000, 1000000000].into_array())]
#[case(buffer![-128i8, -1, 0, 1, 127].into_array())]
#[case(buffer![-1000i16, -1, 0, 1, 1000].into_array())]
#[case(buffer![-1000000i32, -1, 0, 1, 1000000].into_array())]
#[case(buffer![-1000000000i64, -1, 0, 1, 1000000000].into_array())]
#[case(buffer![0.0f32, 1.5, -2.5, 100.0, 1e6].into_array())]
#[case(buffer![f32::NAN, f32::INFINITY, f32::NEG_INFINITY, 0.0f32].into_array())]
#[case(buffer![0.0f64, 1.5, -2.5, 100.0, 1e12].into_array())]
#[case(buffer![f64::NAN, f64::INFINITY, f64::NEG_INFINITY, 0.0f64].into_array())]
#[case(PrimitiveArray::from_option_iter([Some(1u8), None, Some(255), Some(0), None]).into_array())]
#[case(PrimitiveArray::from_option_iter([Some(1i32), None, Some(-100), Some(0), None]).into_array())]
#[case(buffer![42u32].into_array())]
fn test_cast_primitive_conformance(#[case] array: crate::ArrayRef) {
test_cast_conformance(&array);
}
}