@@ -183,6 +183,10 @@ impl PredicateBoundsEvaluator<'_> {
183183 Expr :: BinaryExpr ( BinaryExpr { op, .. } ) if op. returns_null_on_null ( ) => {
184184 self . is_null_if_any_child_null ( expr)
185185 }
186+ // Strict scalar functions return NULL when any argument is NULL.
187+ Expr :: ScalarFunction ( func) if func. func . is_strict ( ) => {
188+ self . is_null_if_any_child_null ( expr)
189+ }
186190 Expr :: Alias ( _)
187191 | Expr :: Cast ( _)
188192 | Expr :: Like ( _)
@@ -235,8 +239,9 @@ mod tests {
235239 use crate :: expr:: ScalarFunction ;
236240 use crate :: predicate_bounds:: evaluate_bounds;
237241 use crate :: {
238- Expr , binary_expr, col, create_udf, is_false, is_not_false, is_not_null,
239- is_not_true, is_not_unknown, is_null, is_true, is_unknown, lit, not,
242+ Expr , ScalarFunctionArgs , ScalarUDF , ScalarUDFImpl , Signature , binary_expr, col,
243+ create_udf, is_false, is_not_false, is_not_null, is_not_true, is_not_unknown,
244+ is_null, is_true, is_unknown, lit, not,
240245 } ;
241246 use arrow:: datatypes:: { DataType , Field , Schema } ;
242247 use datafusion_common:: { DFSchema , Result , ScalarValue } ;
@@ -666,6 +671,29 @@ mod tests {
666671 }
667672 }
668673
674+ #[ test]
675+ fn evaluate_bounds_strict_udf_is_null_when_child_null ( ) {
676+ let col = col ( "col" ) ;
677+ let strict_func = make_test_udf_expr ( "strict_test" , true , vec ! [ col. clone( ) ] ) ;
678+ let non_strict_func =
679+ make_test_udf_expr ( "non_strict_test" , false , vec ! [ col. clone( ) ] ) ;
680+ let schema = DFSchema :: try_from ( Schema :: new ( vec ! [ Field :: new(
681+ "col" ,
682+ DataType :: UInt8 ,
683+ true ,
684+ ) ] ) )
685+ . unwrap ( ) ;
686+
687+ assert_eq ! (
688+ evaluate_bounds( & is_not_null( strict_func) , Some ( & col) , & schema) . unwrap( ) ,
689+ NullableInterval :: FALSE ,
690+ ) ;
691+ assert_eq ! (
692+ evaluate_bounds( & is_not_null( non_strict_func) , Some ( & col) , & schema) . unwrap( ) ,
693+ NullableInterval :: TRUE_OR_FALSE ,
694+ ) ;
695+ }
696+
669697 fn make_scalar_func_expr ( ) -> Expr {
670698 let scalar_func_impl =
671699 |_: & [ ColumnarValue ] | Ok ( ColumnarValue :: Scalar ( ScalarValue :: Null ) ) ;
@@ -678,4 +706,51 @@ mod tests {
678706 ) ;
679707 Expr :: ScalarFunction ( ScalarFunction :: new_udf ( Arc :: new ( udf) , vec ! [ ] ) )
680708 }
709+
710+ #[ derive( Debug , PartialEq , Eq , Hash ) ]
711+ struct TestUdf {
712+ name : & ' static str ,
713+ signature : Signature ,
714+ strict : bool ,
715+ }
716+
717+ impl TestUdf {
718+ fn new ( name : & ' static str , strict : bool ) -> Self {
719+ Self {
720+ name,
721+ signature : Signature :: uniform (
722+ 1 ,
723+ vec ! [ DataType :: UInt8 ] ,
724+ Volatility :: Immutable ,
725+ ) ,
726+ strict,
727+ }
728+ }
729+ }
730+
731+ impl ScalarUDFImpl for TestUdf {
732+ fn name ( & self ) -> & str {
733+ self . name
734+ }
735+
736+ fn signature ( & self ) -> & Signature {
737+ & self . signature
738+ }
739+
740+ fn return_type ( & self , _arg_types : & [ DataType ] ) -> Result < DataType > {
741+ Ok ( DataType :: UInt8 )
742+ }
743+
744+ fn is_strict ( & self ) -> bool {
745+ self . strict
746+ }
747+
748+ fn invoke_with_args ( & self , _args : ScalarFunctionArgs ) -> Result < ColumnarValue > {
749+ unimplemented ! ( )
750+ }
751+ }
752+
753+ fn make_test_udf_expr ( name : & ' static str , strict : bool , args : Vec < Expr > ) -> Expr {
754+ ScalarUDF :: from ( TestUdf :: new ( name, strict) ) . call ( args)
755+ }
681756}
0 commit comments