Skip to content

Commit 9814173

Browse files
connortsui20claude
andauthored
Fix is_zero() for FixedSizeList and Struct scalar types (#8500)
## Summary Closes: #7891 Fixes incorrect `is_zero()` implementations for `FixedSizeList` and `Struct` scalar types that were only checking the number of elements/fields rather than recursively validating that all contained values are themselves zero. Signed-off-by: Connor Tsui <[email protected]> Co-authored-by: Claude Opus 4.8 <[email protected]>
1 parent 4187c1a commit 9814173

1 file changed

Lines changed: 167 additions & 4 deletions

File tree

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

Lines changed: 167 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,19 @@ impl Scalar {
188188
DType::Utf8(_) => value.as_utf8().is_empty(),
189189
DType::Binary(_) => value.as_binary().is_empty(),
190190
DType::List(..) => value.as_list().is_empty(),
191-
// TODO(connor): This seems wrong...
192-
DType::FixedSizeList(_, list_size, _) => value.as_list().len() == *list_size as usize,
193-
// TODO(connor): This also seems wrong...
194-
DType::Struct(struct_fields, _) => value.as_list().len() == struct_fields.nfields(),
191+
// A fixed-size list is zero only if it has the expected number of elements and every
192+
// element is itself a non-null zero value.
193+
DType::FixedSizeList(_, list_size, _) => {
194+
let list = self.as_list();
195+
list.len() == *list_size as usize
196+
&& (0..list.len())
197+
.all(|i| list.element(i).is_some_and(|e| e.is_zero() == Some(true)))
198+
}
199+
// A struct is zero only if every one of its fields is itself a non-null zero value.
200+
DType::Struct(..) => self
201+
.as_struct()
202+
.fields_iter()
203+
.is_some_and(|mut fields| fields.all(|f| f.is_zero() == Some(true))),
195204
DType::Union(..) => todo!("TODO(connor)[Union]: unimplemented"),
196205
DType::Variant(_) => self.as_variant().is_zero()?,
197206
DType::Extension(_) => self.as_extension().to_storage_scalar().is_zero()?,
@@ -428,3 +437,157 @@ fn partial_cmp_struct_values(
428437

429438
Some(Ordering::Equal)
430439
}
440+
441+
#[cfg(test)]
442+
mod tests {
443+
use std::sync::Arc;
444+
445+
use rstest::rstest;
446+
447+
use crate::dtype::DType;
448+
use crate::dtype::Nullability;
449+
use crate::dtype::PType;
450+
use crate::dtype::StructFields;
451+
use crate::scalar::Scalar;
452+
453+
fn i32_scalar(value: i32) -> Scalar {
454+
Scalar::primitive::<i32>(value, Nullability::NonNullable)
455+
}
456+
457+
fn nullable_i32(value: Option<i32>) -> Scalar {
458+
match value {
459+
Some(value) => Scalar::primitive::<i32>(value, Nullability::Nullable),
460+
None => Scalar::null(DType::Primitive(PType::I32, Nullability::Nullable)),
461+
}
462+
}
463+
464+
fn ab_struct_dtype(nullability: Nullability) -> DType {
465+
DType::Struct(
466+
StructFields::new(
467+
["a", "b"].into(),
468+
vec![
469+
DType::Primitive(PType::I32, Nullability::NonNullable),
470+
DType::Utf8(Nullability::NonNullable),
471+
],
472+
),
473+
nullability,
474+
)
475+
}
476+
477+
#[rstest]
478+
// A fixed-size list of all-zero elements is itself zero.
479+
#[case(vec![0, 0], Some(true))]
480+
#[case(vec![0], Some(true))]
481+
// A single non-zero element makes the whole list non-zero. On `develop` these incorrectly
482+
// returned `Some(true)` because only the element count was checked.
483+
#[case(vec![0, 5], Some(false))]
484+
#[case(vec![5, 0], Some(false))]
485+
#[case(vec![1, 2], Some(false))]
486+
fn fixed_size_list_is_zero(#[case] values: Vec<i32>, #[case] expected: Option<bool>) {
487+
let element_dtype = DType::Primitive(PType::I32, Nullability::NonNullable);
488+
let children: Vec<Scalar> = values.into_iter().map(i32_scalar).collect();
489+
let scalar = Scalar::fixed_size_list(element_dtype, children, Nullability::NonNullable);
490+
assert_eq!(scalar.is_zero(), expected);
491+
}
492+
493+
#[test]
494+
fn null_fixed_size_list_is_zero_is_none() {
495+
let element_dtype = Arc::new(DType::Primitive(PType::I32, Nullability::NonNullable));
496+
let scalar = Scalar::null(DType::FixedSizeList(
497+
element_dtype,
498+
2,
499+
Nullability::Nullable,
500+
));
501+
assert_eq!(scalar.is_zero(), None);
502+
}
503+
504+
#[test]
505+
fn fixed_size_list_with_null_element_is_not_zero() {
506+
// A non-null fixed-size list containing a null element is not a zero value. On `develop`
507+
// this incorrectly returned `Some(true)`.
508+
let element_dtype = DType::Primitive(PType::I32, Nullability::Nullable);
509+
let children = vec![nullable_i32(Some(0)), nullable_i32(None)];
510+
let scalar = Scalar::fixed_size_list(element_dtype, children, Nullability::NonNullable);
511+
assert_eq!(scalar.is_zero(), Some(false));
512+
}
513+
514+
#[test]
515+
fn struct_with_all_zero_fields_is_zero() {
516+
let scalar = Scalar::struct_(
517+
ab_struct_dtype(Nullability::NonNullable),
518+
vec![i32_scalar(0), Scalar::utf8("", Nullability::NonNullable)],
519+
);
520+
assert_eq!(scalar.is_zero(), Some(true));
521+
}
522+
523+
#[rstest]
524+
// A non-zero primitive field, a non-empty string field, or both, make the struct non-zero. On
525+
// `develop` all of these incorrectly returned `Some(true)`.
526+
#[case(5, "")]
527+
#[case(0, "x")]
528+
#[case(7, "y")]
529+
fn struct_with_non_zero_field_is_not_zero(#[case] a: i32, #[case] b: &str) {
530+
let scalar = Scalar::struct_(
531+
ab_struct_dtype(Nullability::NonNullable),
532+
vec![i32_scalar(a), Scalar::utf8(b, Nullability::NonNullable)],
533+
);
534+
assert_eq!(scalar.is_zero(), Some(false));
535+
}
536+
537+
#[test]
538+
fn null_struct_is_zero_is_none() {
539+
let scalar = Scalar::null(ab_struct_dtype(Nullability::Nullable));
540+
assert_eq!(scalar.is_zero(), None);
541+
}
542+
543+
#[test]
544+
fn struct_with_null_field_is_not_zero() {
545+
// A non-null struct with a null field is not a zero value. On `develop` this incorrectly
546+
// returned `Some(true)`.
547+
let dtype = DType::Struct(
548+
StructFields::new(
549+
["a", "b"].into(),
550+
vec![
551+
DType::Primitive(PType::I32, Nullability::Nullable),
552+
DType::Primitive(PType::I32, Nullability::Nullable),
553+
],
554+
),
555+
Nullability::NonNullable,
556+
);
557+
let scalar = Scalar::struct_(dtype, vec![nullable_i32(Some(0)), nullable_i32(None)]);
558+
assert_eq!(scalar.is_zero(), Some(false));
559+
}
560+
561+
#[test]
562+
fn nested_struct_of_fixed_size_list_recurses() {
563+
// Zero-checking must recurse through both structs and fixed-size lists. On `develop` the
564+
// non-zero case incorrectly returned `Some(true)`.
565+
let element_dtype = Arc::new(DType::Primitive(PType::I32, Nullability::NonNullable));
566+
let fsl_dtype =
567+
DType::FixedSizeList(Arc::clone(&element_dtype), 2, Nullability::NonNullable);
568+
let struct_dtype = DType::Struct(
569+
StructFields::new(["fsl"].into(), vec![fsl_dtype]),
570+
Nullability::NonNullable,
571+
);
572+
573+
let all_zero = Scalar::struct_(
574+
struct_dtype.clone(),
575+
vec![Scalar::fixed_size_list(
576+
Arc::clone(&element_dtype),
577+
vec![i32_scalar(0), i32_scalar(0)],
578+
Nullability::NonNullable,
579+
)],
580+
);
581+
assert_eq!(all_zero.is_zero(), Some(true));
582+
583+
let with_non_zero = Scalar::struct_(
584+
struct_dtype,
585+
vec![Scalar::fixed_size_list(
586+
element_dtype,
587+
vec![i32_scalar(0), i32_scalar(9)],
588+
Nullability::NonNullable,
589+
)],
590+
);
591+
assert_eq!(with_non_zero.is_zero(), Some(false));
592+
}
593+
}

0 commit comments

Comments
 (0)