@@ -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 )
@@ -847,6 +857,7 @@ impl ScalarValue {
847857 ScalarValue :: iter_to_decimal_array ( scalars, precision, scale) ?;
848858 Arc :: new ( decimal_array)
849859 }
860+ DataType :: Null => ScalarValue :: iter_to_null_array ( scalars) ,
850861 DataType :: Boolean => build_array_primitive ! ( BooleanArray , Boolean ) ,
851862 DataType :: Float32 => build_array_primitive ! ( Float32Array , Float32 ) ,
852863 DataType :: Float64 => build_array_primitive ! ( Float64Array , Float64 ) ,
@@ -979,6 +990,17 @@ impl ScalarValue {
979990 Ok ( array)
980991 }
981992
993+ fn iter_to_null_array ( scalars : impl IntoIterator < Item = ScalarValue > ) -> ArrayRef {
994+ let length =
995+ scalars
996+ . into_iter ( )
997+ . fold ( 0usize , |r, element : ScalarValue | match element {
998+ ScalarValue :: Null => r + 1 ,
999+ _ => unreachable ! ( ) ,
1000+ } ) ;
1001+ new_null_array ( & DataType :: Null , length)
1002+ }
1003+
9821004 fn iter_to_decimal_array (
9831005 scalars : impl IntoIterator < Item = ScalarValue > ,
9841006 precision : & usize ,
@@ -1252,6 +1274,7 @@ impl ScalarValue {
12521274 Arc :: new ( StructArray :: from ( field_values) )
12531275 }
12541276 } ,
1277+ ScalarValue :: Null => new_null_array ( & DataType :: Null , size) ,
12551278 }
12561279 }
12571280
@@ -1277,6 +1300,7 @@ impl ScalarValue {
12771300 }
12781301
12791302 Ok ( match array. data_type ( ) {
1303+ DataType :: Null => ScalarValue :: Null ,
12801304 DataType :: Decimal ( precision, scale) => {
12811305 ScalarValue :: get_decimal_value_from_array ( array, index, precision, scale)
12821306 }
@@ -1530,6 +1554,7 @@ impl ScalarValue {
15301554 eq_array_primitive ! ( array, index, IntervalMonthDayNanoArray , val)
15311555 }
15321556 ScalarValue :: Struct ( _, _) => unimplemented ! ( ) ,
1557+ ScalarValue :: Null => array. data ( ) . is_null ( index) ,
15331558 }
15341559 }
15351560
@@ -1760,6 +1785,7 @@ impl TryFrom<&DataType> for ScalarValue {
17601785 DataType :: Interval ( IntervalUnit :: MonthDayNano ) => {
17611786 ScalarValue :: IntervalMonthDayNano ( None )
17621787 }
1788+ DataType :: Null => ScalarValue :: Null ,
17631789 _ => {
17641790 return Err ( DataFusionError :: NotImplemented ( format ! (
17651791 "Can't create a scalar from data_type \" {:?}\" " ,
@@ -1852,6 +1878,7 @@ impl fmt::Display for ScalarValue {
18521878 ) ?,
18531879 None => write ! ( f, "NULL" ) ?,
18541880 } ,
1881+ ScalarValue :: Null => write ! ( f, "NULL" ) ?,
18551882 } ;
18561883 Ok ( ( ) )
18571884 }
@@ -1919,6 +1946,7 @@ impl fmt::Debug for ScalarValue {
19191946 None => write ! ( f, "Struct(NULL)" ) ,
19201947 }
19211948 }
1949+ ScalarValue :: Null => write ! ( f, "NULL" ) ,
19221950 }
19231951 }
19241952}
0 commit comments