Skip to content

Commit 1ec5a1b

Browse files
authored
Cast bool scalars to primitive types (#11171)
## Summary `Scalar::cast` now casts booleans to primitive types (true → 1, false → 0), matching the bool array cast. It used to reject them, so array casts that cast a scalar failed. For example, a `Sparse<bool>` cast to `f64` errored on its fill value. --------- Signed-off-by: Matt Katz <[email protected]>
1 parent 93524de commit 1ec5a1b

5 files changed

Lines changed: 77 additions & 6 deletions

File tree

‎encodings/sparse/src/compute/cast.rs‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ mod tests {
4040
use rstest::rstest;
4141
use vortex_array::IntoArray;
4242
use vortex_array::VortexSessionExecute;
43+
use vortex_array::arrays::BoolArray;
4344
use vortex_array::arrays::PrimitiveArray;
4445
use vortex_array::assert_arrays_eq;
4546
use vortex_array::builtins::ArrayBuiltins;
@@ -147,6 +148,26 @@ mod tests {
147148
Ok(())
148149
}
149150

151+
#[test]
152+
fn test_cast_bool_sparse_to_f64() -> vortex_error::VortexResult<()> {
153+
let mut ctx = SESSION.create_execution_ctx();
154+
let sparse = Sparse::try_new(
155+
buffer![1u64, 3].into_array(),
156+
BoolArray::from_iter([false, false]).into_array(),
157+
5,
158+
Scalar::bool(true, Nullability::NonNullable),
159+
&mut ctx,
160+
)?;
161+
162+
let casted = sparse
163+
.into_array()
164+
.cast(DType::Primitive(PType::F64, Nullability::NonNullable))?;
165+
166+
let expected = PrimitiveArray::from_iter([1.0f64, 0.0, 1.0, 0.0, 1.0]);
167+
assert_arrays_eq!(casted, expected, &mut ctx);
168+
Ok(())
169+
}
170+
150171
#[test]
151172
fn test_fill_null_sparse_with_null_fill() -> vortex_error::VortexResult<()> {
152173
let mut ctx = SESSION.create_execution_ctx();

‎vortex-array/src/scalar/cast.rs‎

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@
33

44
//! Scalar casting between [`DType`]s.
55
6+
use num_traits::One;
7+
use num_traits::Zero;
68
use vortex_error::VortexExpect;
79
use vortex_error::VortexResult;
810
use vortex_error::vortex_bail;
911
use vortex_error::vortex_ensure;
1012
use vortex_error::vortex_err;
1113

1214
use crate::dtype::DType;
15+
use crate::dtype::Nullability;
16+
use crate::dtype::PType;
17+
use crate::match_each_native_ptype;
1318
use crate::scalar::Scalar;
1419

1520
impl Scalar {
@@ -62,9 +67,18 @@ impl Scalar {
6267

6368
match &self.dtype() {
6469
DType::Null => unreachable!("Handled by the if case above"),
65-
DType::Bool(_) => vortex_bail!(
66-
"Cannot cast bool to {target_dtype}: boolean scalars can only be cast to boolean types with different nullability"
67-
),
70+
DType::Bool(_) => {
71+
let DType::Primitive(ptype, nullability) = target_dtype else {
72+
vortex_bail!(
73+
"Cannot cast bool to {target_dtype}: boolean scalars can only be cast to boolean or primitive types"
74+
);
75+
};
76+
let value = self
77+
.as_bool()
78+
.value()
79+
.vortex_expect("null scalars are handled above");
80+
Ok(cast_bool_to_primitive(value, *ptype, *nullability))
81+
}
6882
DType::Primitive(..) => self.as_primitive().cast(target_dtype),
6983
DType::Decimal(..) => self.as_decimal().cast(target_dtype),
7084
DType::Utf8(_) => vortex_bail!(
@@ -94,3 +108,17 @@ impl Scalar {
94108
.vortex_expect("Casting to nullable should always succeed")
95109
}
96110
}
111+
112+
/// Casts `value` to `ptype` like the bool array cast: true is one, false is zero.
113+
fn cast_bool_to_primitive(value: bool, ptype: PType, nullability: Nullability) -> Scalar {
114+
match_each_native_ptype!(ptype, |T| {
115+
Scalar::primitive(
116+
if value {
117+
<T as One>::one()
118+
} else {
119+
<T as Zero>::zero()
120+
},
121+
nullability,
122+
)
123+
})
124+
}

‎vortex-array/src/scalar/tests/casting.rs‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,27 @@ mod tests {
199199
);
200200
}
201201

202+
#[rstest]
203+
#[case::u8(PType::U8)]
204+
#[case::i32(PType::I32)]
205+
#[case::u64(PType::U64)]
206+
#[case::f16(PType::F16)]
207+
#[case::f64(PType::F64)]
208+
fn cast_bool_to_primitive(#[case] ptype: PType) -> VortexResult<()> {
209+
let target = DType::Primitive(ptype, Nullability::Nullable);
210+
for (value, expected) in [(true, 1u8), (false, 0)] {
211+
assert_eq!(
212+
Scalar::bool(value, Nullability::NonNullable).cast(&target)?,
213+
Scalar::primitive(expected, Nullability::NonNullable).cast(&target)?
214+
);
215+
}
216+
assert_eq!(
217+
Scalar::null(DType::Bool(Nullability::Nullable)).cast(&target)?,
218+
Scalar::null(target)
219+
);
220+
Ok(())
221+
}
222+
202223
#[test]
203224
fn test_fake_coercion_for_matching_type() {
204225
// Test that when types already match, no coercion happens.

‎vortex-array/src/scalar/typed_view/bool.rs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,11 @@ mod test {
174174
.cast(&DType::Bool(NonNullable))
175175
.is_err()
176176
);
177-
assert!(
177+
assert_eq!(
178178
scalar
179179
.cast(&DType::Primitive(PType::I32, NonNullable))
180-
.is_err()
180+
.unwrap(),
181+
Scalar::primitive(1i32, NonNullable)
181182
);
182183
}
183184

‎vortex-array/src/scalar_fn/fns/cast/mod.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ impl ScalarFnVTable for Cast {
149149
}
150150
ColumnarView::Constant(constant) => match cast_constant(constant, target_dtype)? {
151151
Some(result) => Ok(result),
152-
// Some array conversions, including bool to numeric, have no scalar cast.
152+
// Some array conversions have no scalar cast.
153153
None => input
154154
.execute::<Canonical>(ctx)?
155155
.into_array()

0 commit comments

Comments
 (0)