@@ -776,7 +776,8 @@ fn estimate_join_cardinality(
776776 }
777777 } )
778778 . unzip :: < _ , _ , Vec < _ > , Vec < _ > > ( ) ;
779-
779+
780+ let scalar_null = ScalarValue :: try_new_null ( & DataType :: UInt64 ) . unwrap ( ) ;
780781 match join_type {
781782 JoinType :: Inner | JoinType :: Left | JoinType :: Right | JoinType :: Full => {
782783 let total_byte_size = ProbabilityDistribution :: new_unknown ( & DataType :: UInt64 ) . unwrap_or_default ( ) ;
@@ -809,7 +810,7 @@ fn estimate_join_cardinality(
809810 } ;
810811
811812 Some ( PartialJoinStatistics {
812- num_rows : cardinality. get_value ( ) . unwrap_or ( & ScalarValue :: Null ) . clone ( ) ,
813+ num_rows : cardinality. get_value ( ) . unwrap_or ( & scalar_null ) . clone ( ) ,
813814 // We don't do anything specific here, just combine the existing
814815 // statistics which might yield subpar results (although it is
815816 // true, esp regarding min/max). For a better estimation, we need
@@ -836,7 +837,7 @@ fn estimate_join_cardinality(
836837 } ;
837838
838839 Some ( PartialJoinStatistics {
839- num_rows : cardinality. unwrap_or ( ScalarValue :: Null ) ,
840+ num_rows : cardinality. unwrap_or ( scalar_null ) ,
840841 column_statistics : outer_stats. column_statistics ,
841842 } )
842843 }
@@ -850,7 +851,7 @@ fn estimate_join_cardinality(
850851 } ;
851852
852853 Some ( PartialJoinStatistics {
853- num_rows : outer_stats. num_rows . get_value ( ) . unwrap_or ( & ScalarValue :: Null ) . clone ( ) ,
854+ num_rows : outer_stats. num_rows . get_value ( ) . unwrap_or ( & scalar_null ) . clone ( ) ,
854855 column_statistics : outer_stats. column_statistics ,
855856 } )
856857 }
@@ -859,7 +860,7 @@ fn estimate_join_cardinality(
859860 let mut column_statistics = left_stats. column_statistics ;
860861 column_statistics. push ( ColumnStatistics :: new_unknown ( ) . unwrap_or_default ( ) ) ;
861862 Some ( PartialJoinStatistics {
862- num_rows : left_stats. num_rows . get_value ( ) . unwrap_or ( & ScalarValue :: Null ) . clone ( ) ,
863+ num_rows : left_stats. num_rows . get_value ( ) . unwrap_or ( & scalar_null ) . clone ( ) ,
863864 column_statistics,
864865 } )
865866 }
@@ -910,7 +911,8 @@ fn estimate_inner_join_cardinality(
910911 // With the assumption that the smaller input's domain is generally represented in the bigger
911912 // input's domain, we can estimate the inner join's cardinality by taking the cartesian product
912913 // of the two inputs and normalizing it by the selectivity factor.
913- let value = join_selectivity. get_value ( ) . unwrap_or ( & ScalarValue :: Null ) ;
914+ let null = ScalarValue :: try_new_null ( & DataType :: UInt64 ) . unwrap ( ) ;
915+ let value = join_selectivity. get_value ( ) . unwrap_or ( & null) ;
914916 if value > & ScalarValue :: new_one ( & DataType :: UInt64 ) . unwrap ( ) {
915917 let res = new_generic_from_binary_op ( & Operator :: Multiply , & left_stats. num_rows , & right_stats. num_rows ) . unwrap ( ) ;
916918 Some ( new_generic_from_binary_op ( & Operator :: Divide , & res, & join_selectivity) . unwrap ( ) )
@@ -938,6 +940,7 @@ fn estimate_disjoint_inputs(
938940 left_stats : & TableStatistics ,
939941 right_stats : & TableStatistics ,
940942) -> Option < ProbabilityDistribution > {
943+ let null = ScalarValue :: try_new_null ( & DataType :: UInt64 ) . unwrap ( ) ;
941944 for ( left_stat, right_stat) in left_stats
942945 . column_statistics
943946 . iter ( )
@@ -946,16 +949,16 @@ fn estimate_disjoint_inputs(
946949 // If there is no overlap in any of the join columns, this means the join
947950 // itself is disjoint and the cardinality is 0. Though we can only assume
948951 // this when the statistics are exact (since it is a very strong assumption).
949- let left_min_val = left_stat. min_value . get_value ( ) . unwrap_or ( & ScalarValue :: Null ) ;
950- let right_max_val = right_stat. max_value . get_value ( ) . unwrap_or ( & ScalarValue :: Null ) ;
952+ let left_min_val = left_stat. min_value . get_value ( ) . unwrap_or ( & null ) ;
953+ let right_max_val = right_stat. max_value . get_value ( ) . unwrap_or ( & null ) ;
951954 if left_min_val > right_max_val {
952955 return Some (
953956 ProbabilityDistribution :: new_zero ( & DataType :: UInt64 ) . unwrap_or_default ( )
954957 ) ;
955958 }
956959
957- let left_max_val = left_stat. max_value . get_value ( ) . unwrap_or ( & ScalarValue :: Null ) ;
958- let right_min_val = right_stat. min_value . get_value ( ) . unwrap_or ( & ScalarValue :: Null ) ;
960+ let left_max_val = left_stat. max_value . get_value ( ) . unwrap_or ( & null ) ;
961+ let right_min_val = right_stat. min_value . get_value ( ) . unwrap_or ( & null ) ;
959962 if left_max_val < right_min_val
960963 {
961964 return Some (
@@ -992,7 +995,8 @@ fn max_distinct_count(
992995 . ok ( )
993996 . and_then ( |e| e. cardinality ( ) )
994997 {
995- return if & ScalarValue :: UInt64 ( Some ( range_dc) ) < & result. get_value ( ) . unwrap_or ( & ScalarValue :: Null )
998+ let null = ScalarValue :: try_new_null ( & DataType :: UInt64 ) . unwrap ( ) ;
999+ return if & ScalarValue :: UInt64 ( Some ( range_dc) ) < & result. get_value ( ) . unwrap_or ( & null)
9961000 {
9971001 if stats. min_value . is_exact ( ) . unwrap ( )
9981002 && stats. max_value . is_exact ( ) . unwrap ( )
0 commit comments