@@ -13,6 +13,7 @@ use vortex_dtype::PType;
1313use vortex_error:: VortexResult ;
1414use vortex_error:: vortex_ensure;
1515use vortex_error:: vortex_ensure_eq;
16+ use vortex_error:: vortex_panic;
1617
1718use crate :: PValue ;
1819use crate :: ScalarValue ;
@@ -127,10 +128,6 @@ impl Scalar {
127128 Self { dtype, value }
128129 }
129130
130- // TODO(connor): Add an `is_zero` method to `Scalar`. Note that it is **not** as simple as
131- // calling `is_zero` on the `ScalarValue` since each variant can represent multiple things
132- // (notably `Struct` and `FixedSizeList` are represented as non-empty `List`s).
133-
134131 /// Returns a default value for the given [`DType`].
135132 ///
136133 /// For nullable types, this returns a null scalar. For non-nullable and non-nested types, this
@@ -275,7 +272,22 @@ impl Scalar {
275272 /// Returns `None` if the scalar is null, otherwise returns `Some(true)` if the value is zero
276273 /// and `Some(false)` otherwise.
277274 pub fn is_zero ( & self ) -> Option < bool > {
278- self . value . as_ref ( ) . map ( |v| v. is_zero ( ) )
275+ let value = self . value ( ) ?;
276+
277+ let is_zero = match self . dtype ( ) {
278+ DType :: Null => vortex_panic ! ( "non-null value somehow had `DType::Null`" ) ,
279+ DType :: Bool ( _) => !value. as_bool ( ) ,
280+ DType :: Primitive ( ..) => value. as_primitive ( ) . is_zero ( ) ,
281+ DType :: Decimal ( ..) => value. as_decimal ( ) . is_zero ( ) ,
282+ DType :: Utf8 ( _) => value. as_utf8 ( ) . is_empty ( ) ,
283+ DType :: Binary ( _) => value. as_binary ( ) . is_empty ( ) ,
284+ DType :: List ( ..) => value. as_list ( ) . is_empty ( ) ,
285+ DType :: FixedSizeList ( _, list_size, _) => value. as_list ( ) . len ( ) == * list_size as usize ,
286+ DType :: Struct ( struct_fields, _) => value. as_list ( ) . len ( ) == struct_fields. nfields ( ) ,
287+ DType :: Extension ( _) => self . as_extension ( ) . storage ( ) . is_zero ( ) ?,
288+ } ;
289+
290+ Some ( is_zero)
279291 }
280292
281293 /// Reinterprets the bytes of this scalar as a different primitive type.
0 commit comments