Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion fuzz/fuzz_targets/file_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ impl FileRoundTrip {
let bool_mask_bool = bool_mask
.execute::<BoolArray>(&mut ctx)
.vortex_expect("execute bool");
let mask = bool_mask_bool.to_mask_fill_null_false(&mut ctx);
let mask = bool_mask_bool
.to_mask_fill_null_false(&mut ctx)
.vortex_expect("bool mask to mask");
let filtered = array_data
.filter(mask)
.vortex_expect("filter operation should succeed in fuzz test");
Expand Down
16 changes: 10 additions & 6 deletions vortex-array/src/arrays/bool/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,20 @@ pub trait BoolArrayExt: TypedArrayRef<Bool> {
.vortex_expect("cannot convert nullable boolean array to mask")
}

fn to_mask_fill_null_false(&self, ctx: &mut ExecutionCtx) -> Mask {
let validity_mask = BoolArrayExt::validity(self)
.execute_mask(self.len(), ctx)
.vortex_expect("Failed to compute validity mask");
/// Converts to a [`Mask`] that treats null values as `false`.
///
/// # Errors
///
/// Returns an error if the validity cannot be executed. Validity can be a lazy array, such as
/// the mask child that `mask` attaches, and executing it can fail.
fn to_mask_fill_null_false(&self, ctx: &mut ExecutionCtx) -> VortexResult<Mask> {
let validity_mask = BoolArrayExt::validity(self).execute_mask(self.len(), ctx)?;
let buffer = match validity_mask {
Mask::AllTrue(_) => self.to_bit_buffer(),
Mask::AllFalse(_) => return Mask::new_false(self.len()),
Mask::AllFalse(_) => return Ok(Mask::new_false(self.len())),
Mask::Values(validity) => validity.bit_buffer() & self.to_bit_buffer(),
};
Mask::from_buffer(buffer)
Ok(Mask::from_buffer(buffer))
}
}
impl<T: TypedArrayRef<Bool>> BoolArrayExt for T {}
Expand Down
4 changes: 3 additions & 1 deletion vortex-array/src/compute/conformance/mask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,9 @@ fn test_nullable_mask_input(array: &ArrayRef, ctx: &mut ExecutionCtx) {
let validity = Validity::from_iter(validity_values.clone());
let nullable_mask = BoolArray::new(bool_array.to_bit_buffer(), validity);

let mask_array = nullable_mask.to_mask_fill_null_false(ctx);
let mask_array = nullable_mask
.to_mask_fill_null_false(ctx)
.vortex_expect("nullable mask to mask");
let masked = array
.clone()
.mask((!&mask_array).into_array())
Expand Down
2 changes: 1 addition & 1 deletion vortex-functions/src/case_when/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ impl ScalarFnVTable for CaseWhen {

let condition = args.get(i * 2)?.filter(remaining.clone())?;
let cond_bool = condition.execute::<BoolArray>(ctx)?;
let cond_mask = cond_bool.to_mask_fill_null_false(ctx);
let cond_mask = cond_bool.to_mask_fill_null_false(ctx)?;
let effective_mask = remaining.intersect_by_rank(&cond_mask);

if effective_mask.all_false() {
Expand Down
27 changes: 27 additions & 0 deletions vortex-functions/src/case_when/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ use vortex_error::VortexExpect;
use vortex_error::VortexResult;
use vortex_expr::ArrayExprExt;
use vortex_expr::Expression;
use vortex_expr::cast;
use vortex_expr::col;
use vortex_expr::eq;
use vortex_expr::gt;
use vortex_expr::is_not_null;
use vortex_expr::lit;
use vortex_expr::mask;
use vortex_expr::proto::ExprSerializeProtoExt;
use vortex_expr::root;
use vortex_session::VortexSession;
Expand Down Expand Up @@ -721,6 +723,31 @@ fn selected_rows_do_not_execute_erroring_branches_or_later_conditions() -> Vorte
Ok(())
}

/// `mask` attaches its mask child as lazy validity, so a mask that fails to execute (here a
/// cast of nulls to non-nullable) only fails once CASE WHEN reads the condition's validity.
/// That failure must surface as an error rather than a panic.
#[test]
fn condition_with_failing_lazy_validity_returns_error() -> VortexResult<()> {
let mut ctx = SESSION.create_execution_ctx();
let input = BoolArray::from_iter([Some(true), None, Some(false)]).into_array();
let condition = mask(
is_not_null(root()),
cast(root(), DType::Bool(Nullability::NonNullable)),
);
let expr = case_when_no_else(condition, lit(1i32));

let Err(error) = input.apply(&expr)?.execute::<Canonical>(&mut ctx) else {
panic!("casting a null mask to non-nullable must fail");
};
assert!(
error
.to_string()
.contains("Cannot cast array with invalid values to non-nullable type"),
"{error}"
);
Ok(())
}

#[rstest]
#[case::with_else(true)]
#[case::without_else(false)]
Expand Down
2 changes: 1 addition & 1 deletion vortex-functions/src/zip/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ impl ScalarFnVTable for Zip {

let mask = mask_array
.execute::<BoolArray>(ctx)?
.to_mask_fill_null_false(ctx);
.to_mask_fill_null_false(ctx)?;

let return_dtype = zip_return_dtype(if_true.dtype(), if_false.dtype())?;

Expand Down
2 changes: 1 addition & 1 deletion vortex-python/src/arrays/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ impl PyArray {
let mut ctx = session().create_execution_ctx();
let mask = mask
.execute::<BoolArray>(&mut ctx)?
.to_mask_fill_null_false(&mut ctx);
.to_mask_fill_null_false(&mut ctx)?;
Ok(slf
.filter(mask)?
.execute::<Canonical>(&mut ctx)?
Expand Down