@@ -37,12 +37,16 @@ use datafusion_physical_expr::expressions::{BinaryExpr, Column, NegativeExpr};
3737use datafusion_physical_expr:: intervals:: utils:: check_support;
3838use datafusion_physical_expr:: { EquivalenceProperties , Partitioning , PhysicalExpr } ;
3939use datafusion_physical_expr_common:: sort_expr:: PhysicalSortExpr ;
40+ use datafusion_physical_optimizer:: PhysicalOptimizerContext ;
4041use datafusion_physical_optimizer:: PhysicalOptimizerRule ;
4142use datafusion_physical_optimizer:: join_selection:: JoinSelection ;
4243use datafusion_physical_plan:: displayable;
4344use datafusion_physical_plan:: joins:: utils:: ColumnIndex ;
4445use datafusion_physical_plan:: joins:: utils:: JoinFilter ;
4546use datafusion_physical_plan:: joins:: { HashJoinExec , NestedLoopJoinExec , PartitionMode } ;
47+ use datafusion_physical_plan:: operator_statistics:: {
48+ ClosureStatisticsProvider , StatisticsRegistry , StatisticsResult ,
49+ } ;
4650use datafusion_physical_plan:: projection:: ProjectionExec ;
4751use datafusion_physical_plan:: sorts:: sort_preserving_merge:: SortPreservingMergeExec ;
4852use datafusion_physical_plan:: {
@@ -640,6 +644,72 @@ async fn test_null_aware_left_anti_with_filter_does_not_swap() -> Result<()> {
640644 Ok ( ( ) )
641645}
642646
647+ #[ tokio:: test]
648+ async fn test_join_swap_uses_context_statistics_registry ( ) -> Result < ( ) > {
649+ let left: Arc < dyn ExecutionPlan > = Arc :: new ( StatisticsExec :: new (
650+ big_statistics ( ) ,
651+ Schema :: new ( vec ! [ Field :: new( "left_col" , DataType :: Int32 , false ) ] ) ,
652+ ) ) ;
653+ let right: Arc < dyn ExecutionPlan > = Arc :: new ( StatisticsExec :: new (
654+ big_statistics ( ) ,
655+ Schema :: new ( vec ! [ Field :: new( "right_col" , DataType :: Int32 , false ) ] ) ,
656+ ) ) ;
657+ let join = HashJoinExec :: try_new (
658+ Arc :: clone ( & left) ,
659+ Arc :: clone ( & right) ,
660+ vec ! [ (
661+ Arc :: new( Column :: new_with_schema( "left_col" , & left. schema( ) ) ?) ,
662+ Arc :: new( Column :: new_with_schema( "right_col" , & right. schema( ) ) ?) ,
663+ ) ] ,
664+ None ,
665+ & JoinType :: LeftAnti ,
666+ None ,
667+ PartitionMode :: Auto ,
668+ NullEquality :: NullEqualsNothing ,
669+ false ,
670+ ) ?;
671+
672+ // Reports `right` as much smaller than its real stats: the swap below only
673+ // happens if `context`'s registry is actually threaded through.
674+ let mut registry = StatisticsRegistry :: new ( ) ;
675+ registry. register ( Arc :: new ( ClosureStatisticsProvider :: with_matches (
676+ |plan| plan. schema ( ) . field ( 0 ) . name ( ) == "right_col" ,
677+ |_plan, _child_stats| Ok ( StatisticsResult :: Computed ( small_statistics ( ) . into ( ) ) ) ,
678+ ) ) ) ;
679+
680+ struct ContextWithRegistry {
681+ config : ConfigOptions ,
682+ registry : StatisticsRegistry ,
683+ }
684+
685+ impl PhysicalOptimizerContext for ContextWithRegistry {
686+ fn config_options ( & self ) -> & ConfigOptions {
687+ & self . config
688+ }
689+
690+ fn statistics_registry ( & self ) -> Option < & StatisticsRegistry > {
691+ Some ( & self . registry )
692+ }
693+ }
694+
695+ let context = ContextWithRegistry {
696+ config : ConfigOptions :: new ( ) ,
697+ registry,
698+ } ;
699+
700+ let optimized_join =
701+ JoinSelection :: new ( ) . optimize_with_context ( Arc :: new ( join) , & context) ?;
702+ let swapped_join = optimized_join
703+ . downcast_ref :: < HashJoinExec > ( )
704+ . expect ( "optimize_with_context should return a HashJoinExec" ) ;
705+
706+ assert_eq ! ( * swapped_join. join_type( ) , JoinType :: RightAnti ) ;
707+ assert_eq ! ( swapped_join. left( ) . schema( ) . field( 0 ) . name( ) , "right_col" ) ;
708+ assert_eq ! ( swapped_join. right( ) . schema( ) . field( 0 ) . name( ) , "left_col" ) ;
709+
710+ Ok ( ( ) )
711+ }
712+
643713#[ tokio:: test]
644714async fn test_join_with_swap_mark ( ) {
645715 let join_types = [ JoinType :: LeftMark , JoinType :: RightMark ] ;
0 commit comments