@@ -189,6 +189,56 @@ impl Scalar {
189189 }
190190 }
191191
192+ /// Creates a "zero"-value scalar value for the given data type.
193+ ///
194+ /// For nullable types the zero value is the underlying `DType`'s zero value.
195+ ///
196+ /// # Zero Values
197+ ///
198+ /// Here is the list of zero values for each [`DType`] (when the [`DType`] is non-nullable):
199+ /// - `Bool`: `false`
200+ /// - `Primitive`: `0`
201+ /// - `Decimal`: `0`
202+ /// - `Utf8`: `""`
203+ /// - `Binary`: An empty buffer
204+ /// - `List`: An empty list
205+ /// - `FixedSizeList`: A list (with correct size) of zero values, which is determined by the
206+ /// element [`DType`]
207+ /// - `Struct`: A struct where each field has a zero value, which is determined by the field
208+ /// [`DType`]
209+ /// - `Extension`: The zero value of the storage [`DType`]
210+ ///
211+ /// This is similar to `default_value` except in its handling of nullability.
212+ pub fn zero_value ( dtype : DType ) -> Self {
213+ match dtype {
214+ DType :: Null => Self :: null ( dtype) ,
215+ DType :: Bool ( nullability) => Self :: bool ( false , nullability) ,
216+ DType :: Primitive ( pt, nullability) => {
217+ Self :: primitive_value ( PValue :: zero ( pt) , pt, nullability)
218+ }
219+ DType :: Decimal ( dt, nullability) => {
220+ Self :: decimal ( DecimalValue :: from ( 0i8 ) , dt, nullability)
221+ }
222+ DType :: Utf8 ( nullability) => Self :: utf8 ( "" , nullability) ,
223+ DType :: Binary ( nullability) => Self :: binary ( Buffer :: empty ( ) , nullability) ,
224+ DType :: List ( edt, nullability) => Self :: list ( edt, vec ! [ ] , nullability) ,
225+ DType :: FixedSizeList ( edt, size, nullability) => {
226+ let elements = ( 0 ..size)
227+ . map ( |_| Scalar :: zero_value ( edt. as_ref ( ) . clone ( ) ) )
228+ . collect ( ) ;
229+ Self :: fixed_size_list ( edt, elements, nullability)
230+ }
231+ DType :: Struct ( sf, nullability) => {
232+ let fields: Vec < _ > = sf. fields ( ) . map ( Scalar :: zero_value) . collect ( ) ;
233+ Self :: struct_ ( DType :: Struct ( sf, nullability) , fields)
234+ }
235+ DType :: Extension ( dt) => {
236+ let scalar = Self :: zero_value ( dt. storage_dtype ( ) . clone ( ) ) ;
237+ Self :: extension ( dt, scalar)
238+ }
239+ }
240+ }
241+
192242 /// Creates a "default" scalar value for the given data type.
193243 ///
194244 /// For nullable types, returns null. For non-nullable types, returns an appropriate zero/empty
@@ -222,7 +272,7 @@ impl Scalar {
222272 Self :: primitive_value ( PValue :: zero ( pt) , pt, nullability)
223273 }
224274 DType :: Decimal ( dt, nullability) => {
225- Self :: decimal ( DecimalValue :: from ( 0 ) , dt, nullability)
275+ Self :: decimal ( DecimalValue :: from ( 0i8 ) , dt, nullability)
226276 }
227277 DType :: Utf8 ( nullability) => Self :: utf8 ( "" , nullability) ,
228278 DType :: Binary ( nullability) => Self :: binary ( Buffer :: empty ( ) , nullability) ,
0 commit comments