@@ -39,6 +39,8 @@ use std::{convert::TryFrom, fmt, iter::repeat, sync::Arc};
3939/// This is the single-valued counter-part of arrow’s `Array`.
4040#[ derive( Clone ) ]
4141pub enum ScalarValue {
42+ /// represents `DataType::Null` (castable to/from any other type)
43+ Null ,
4244 /// true or false value
4345 Boolean ( Option < bool > ) ,
4446 /// 32bit float
@@ -170,6 +172,8 @@ impl PartialEq for ScalarValue {
170172 ( IntervalMonthDayNano ( _) , _) => false ,
171173 ( Struct ( v1, t1) , Struct ( v2, t2) ) => v1. eq ( v2) && t1. eq ( t2) ,
172174 ( Struct ( _, _) , _) => false ,
175+ ( Null , Null ) => true ,
176+ ( Null , _) => false ,
173177 }
174178 }
175179}
@@ -270,6 +274,8 @@ impl PartialOrd for ScalarValue {
270274 }
271275 }
272276 ( Struct ( _, _) , _) => None ,
277+ ( Null , Null ) => Some ( Ordering :: Equal ) ,
278+ ( Null , _) => None ,
273279 }
274280 }
275281}
@@ -325,6 +331,8 @@ impl std::hash::Hash for ScalarValue {
325331 v. hash ( state) ;
326332 t. hash ( state) ;
327333 }
334+ // stable hash for Null value
335+ Null => 1 . hash ( state) ,
328336 }
329337 }
330338}
@@ -594,6 +602,7 @@ impl ScalarValue {
594602 DataType :: Interval ( IntervalUnit :: MonthDayNano )
595603 }
596604 ScalarValue :: Struct ( _, fields) => DataType :: Struct ( fields. as_ref ( ) . clone ( ) ) ,
605+ ScalarValue :: Null => DataType :: Null ,
597606 }
598607 }
599608
@@ -623,7 +632,8 @@ impl ScalarValue {
623632 pub fn is_null ( & self ) -> bool {
624633 matches ! (
625634 * self ,
626- ScalarValue :: Boolean ( None )
635+ ScalarValue :: Null
636+ | ScalarValue :: Boolean ( None )
627637 | ScalarValue :: UInt8 ( None )
628638 | ScalarValue :: UInt16 ( None )
629639 | ScalarValue :: UInt32 ( None )
@@ -844,6 +854,7 @@ impl ScalarValue {
844854 ScalarValue :: iter_to_decimal_array ( scalars, precision, scale) ?;
845855 Arc :: new ( decimal_array)
846856 }
857+ DataType :: Null => ScalarValue :: iter_to_null_array ( scalars) ,
847858 DataType :: Boolean => build_array_primitive ! ( BooleanArray , Boolean ) ,
848859 DataType :: Float32 => build_array_primitive ! ( Float32Array , Float32 ) ,
849860 DataType :: Float64 => build_array_primitive ! ( Float64Array , Float64 ) ,
@@ -976,6 +987,17 @@ impl ScalarValue {
976987 Ok ( array)
977988 }
978989
990+ fn iter_to_null_array ( scalars : impl IntoIterator < Item = ScalarValue > ) -> ArrayRef {
991+ let length =
992+ scalars
993+ . into_iter ( )
994+ . fold ( 0usize , |r, element : ScalarValue | match element {
995+ ScalarValue :: Null => r + 1 ,
996+ _ => unreachable ! ( ) ,
997+ } ) ;
998+ new_null_array ( & DataType :: Null , length)
999+ }
1000+
9791001 fn iter_to_decimal_array (
9801002 scalars : impl IntoIterator < Item = ScalarValue > ,
9811003 precision : & usize ,
@@ -1249,6 +1271,7 @@ impl ScalarValue {
12491271 Arc :: new ( StructArray :: from ( field_values) )
12501272 }
12511273 } ,
1274+ ScalarValue :: Null => new_null_array ( & DataType :: Null , size) ,
12521275 }
12531276 }
12541277
@@ -1274,6 +1297,7 @@ impl ScalarValue {
12741297 }
12751298
12761299 Ok ( match array. data_type ( ) {
1300+ DataType :: Null => ScalarValue :: Null ,
12771301 DataType :: Decimal ( precision, scale) => {
12781302 ScalarValue :: get_decimal_value_from_array ( array, index, precision, scale)
12791303 }
@@ -1519,6 +1543,7 @@ impl ScalarValue {
15191543 eq_array_primitive ! ( array, index, IntervalMonthDayNanoArray , val)
15201544 }
15211545 ScalarValue :: Struct ( _, _) => unimplemented ! ( ) ,
1546+ ScalarValue :: Null => array. data ( ) . is_null ( index) ,
15221547 }
15231548 }
15241549
@@ -1740,6 +1765,7 @@ impl TryFrom<&DataType> for ScalarValue {
17401765 DataType :: Struct ( fields) => {
17411766 ScalarValue :: Struct ( None , Box :: new ( fields. clone ( ) ) )
17421767 }
1768+ DataType :: Null => ScalarValue :: Null ,
17431769 _ => {
17441770 return Err ( DataFusionError :: NotImplemented ( format ! (
17451771 "Can't create a scalar from data_type \" {:?}\" " ,
@@ -1832,6 +1858,7 @@ impl fmt::Display for ScalarValue {
18321858 ) ?,
18331859 None => write ! ( f, "NULL" ) ?,
18341860 } ,
1861+ ScalarValue :: Null => write ! ( f, "NULL" ) ?,
18351862 } ;
18361863 Ok ( ( ) )
18371864 }
@@ -1899,6 +1926,7 @@ impl fmt::Debug for ScalarValue {
18991926 None => write ! ( f, "Struct(NULL)" ) ,
19001927 }
19011928 }
1929+ ScalarValue :: Null => write ! ( f, "NULL" ) ,
19021930 }
19031931 }
19041932}
0 commit comments