@@ -346,7 +346,10 @@ impl CaseExpr {
346346 . downcast_ref :: < BooleanArray > ( )
347347 . expect ( "predicate should evaluate to a boolean array" ) ;
348348 // invert the bitmask
349- let bit_mask = not ( bit_mask) ?;
349+ let bit_mask = match bit_mask. null_count ( ) {
350+ 0 => not ( bit_mask) ?,
351+ _ => not ( & prep_null_mask_filter ( bit_mask) ) ?,
352+ } ;
350353 match then_expr. evaluate ( batch) ? {
351354 ColumnarValue :: Array ( array) => {
352355 Ok ( ColumnarValue :: Array ( nullif ( & array, & bit_mask) ?) )
@@ -885,6 +888,32 @@ mod tests {
885888 Ok ( ( ) )
886889 }
887890
891+ #[ test]
892+ fn test_when_null_and_some_cond_else_null ( ) -> Result < ( ) > {
893+ let batch = case_test_batch ( ) ?;
894+ let schema = batch. schema ( ) ;
895+
896+ let when = binary (
897+ Arc :: new ( Literal :: new ( ScalarValue :: Boolean ( None ) ) ) ,
898+ Operator :: And ,
899+ binary ( col ( "a" , & schema) ?, Operator :: Eq , lit ( "foo" ) , & schema) ?,
900+ & schema,
901+ ) ?;
902+ let then = col ( "a" , & schema) ?;
903+
904+ // SELECT CASE WHEN (NULL AND a = 'foo') THEN a ELSE NULL END
905+ let expr = Arc :: new ( CaseExpr :: try_new ( None , vec ! [ ( when, then) ] , None ) ?) ;
906+ let result = expr
907+ . evaluate ( & batch) ?
908+ . into_array ( batch. num_rows ( ) )
909+ . expect ( "Failed to convert to array" ) ;
910+ let result = as_string_array ( & result) ;
911+
912+ // all result values should be null
913+ assert_eq ! ( result. logical_null_count( ) , batch. num_rows( ) ) ;
914+ Ok ( ( ) )
915+ }
916+
888917 fn case_test_batch ( ) -> Result < RecordBatch > {
889918 let schema = Schema :: new ( vec ! [ Field :: new( "a" , DataType :: Utf8 , true ) ] ) ;
890919 let a = StringArray :: from ( vec ! [ Some ( "foo" ) , Some ( "baz" ) , None , Some ( "bar" ) ] ) ;
0 commit comments