@@ -341,7 +341,10 @@ impl CaseExpr {
341341 . downcast_ref :: < BooleanArray > ( )
342342 . expect ( "predicate should evaluate to a boolean array" ) ;
343343 // invert the bitmask
344- let bit_mask = not ( bit_mask) ?;
344+ let bit_mask = match bit_mask. null_count ( ) {
345+ 0 => not ( & bit_mask) ?,
346+ _ => not ( & prep_null_mask_filter ( & bit_mask) ) ?,
347+ } ;
345348 match then_expr. evaluate ( batch) ? {
346349 ColumnarValue :: Array ( array) => {
347350 Ok ( ColumnarValue :: Array ( nullif ( & array, & bit_mask) ?) )
@@ -913,6 +916,32 @@ mod tests {
913916 Ok ( ( ) )
914917 }
915918
919+ #[ test]
920+ fn test_when_null_and_some_cond_else_null ( ) -> Result < ( ) > {
921+ let batch = case_test_batch ( ) ?;
922+ let schema = batch. schema ( ) ;
923+
924+ let when = binary (
925+ Arc :: new ( Literal :: new ( ScalarValue :: Boolean ( None ) ) ) ,
926+ Operator :: And ,
927+ binary ( col ( "a" , & schema) ?, Operator :: Eq , lit ( "foo" ) , & schema) ?,
928+ & schema,
929+ ) ?;
930+ let then = col ( "a" , & schema) ?;
931+
932+ // SELECT CASE WHEN (NULL AND a = 'foo') THEN a ELSE NULL END
933+ let expr = Arc :: new ( CaseExpr :: try_new ( None , vec ! [ ( when, then) ] , None ) ?) ;
934+ let result = expr
935+ . evaluate ( & batch) ?
936+ . into_array ( batch. num_rows ( ) )
937+ . expect ( "Failed to convert to array" ) ;
938+ let result = as_string_array ( & result) ;
939+
940+ // all result values should be null
941+ assert_eq ! ( result. logical_null_count( ) , batch. num_rows( ) ) ;
942+ Ok ( ( ) )
943+ }
944+
916945 fn case_test_batch ( ) -> Result < RecordBatch > {
917946 let schema = Schema :: new ( vec ! [ Field :: new( "a" , DataType :: Utf8 , true ) ] ) ;
918947 let a = StringArray :: from ( vec ! [ Some ( "foo" ) , Some ( "baz" ) , None , Some ( "bar" ) ] ) ;
0 commit comments