@@ -471,29 +471,41 @@ async fn test_join_with_swap_semi() {
471471 }
472472}
473473
474+ /// A null-aware `LeftAnti` swaps to a null-aware `RightAnti` that builds on
475+ /// the smaller subquery side. Besides the scalar `NOT IN`, this covers a
476+ /// multi-key one (a multi-column `NOT IN`, correlation scope keys, or both),
477+ /// which keeps the number of `NOT IN` value keys and the key order, and a
478+ /// filtered one (a decorrelated correlated `NOT IN`), whose filter sides are
479+ /// swapped with the inputs.
474480#[ rstest]
475- #[ case( PartitionMode :: CollectLeft ) ]
476- #[ case( PartitionMode :: Auto ) ]
477- #[ case( PartitionMode :: Partitioned ) ]
481+ #[ case:: collect_left( PartitionMode :: CollectLeft , 1 , 1 , false ) ]
482+ #[ case:: auto( PartitionMode :: Auto , 1 , 1 , false ) ]
483+ #[ case:: partitioned( PartitionMode :: Partitioned , 1 , 1 , false ) ]
484+ #[ case:: filter( PartitionMode :: CollectLeft , 1 , 1 , true ) ]
485+ #[ case:: scope_key( PartitionMode :: CollectLeft , 2 , 1 , false ) ]
486+ #[ case:: multi_column( PartitionMode :: CollectLeft , 2 , 2 , false ) ]
478487#[ tokio:: test]
479488async fn test_null_aware_left_anti_swaps_to_right_anti (
480489 #[ case] partition_mode : PartitionMode ,
490+ #[ case] num_keys : usize ,
491+ #[ case] value_keys : usize ,
492+ #[ case] with_filter : bool ,
481493) -> Result < ( ) > {
482494 let ( big, small) = create_big_and_small ( ) ;
483- let join = HashJoinExec :: try_new (
495+ let big_col = Arc :: new ( Column :: new_with_schema ( "big_col" , & big. schema ( ) ) ?) as _ ;
496+ let small_col = Arc :: new ( Column :: new_with_schema ( "small_col" , & small. schema ( ) ) ?) as _ ;
497+ let on = vec ! [ ( big_col, small_col) ; num_keys] ;
498+ let join = HashJoinExecBuilder :: new (
484499 Arc :: clone ( & big) ,
485500 Arc :: clone ( & small) ,
486- vec ! [ (
487- Arc :: new( Column :: new_with_schema( "big_col" , & big. schema( ) ) ?) ,
488- Arc :: new( Column :: new_with_schema( "small_col" , & small. schema( ) ) ?) ,
489- ) ] ,
490- None ,
491- & JoinType :: LeftAnti ,
492- None ,
493- partition_mode,
494- NullEquality :: NullEqualsNothing ,
495- true ,
496- ) ?;
501+ on,
502+ JoinType :: LeftAnti ,
503+ )
504+ . with_filter ( if with_filter { nl_join_filter ( ) } else { None } )
505+ . with_partition_mode ( partition_mode)
506+ . with_null_aware ( true )
507+ . with_null_aware_value_keys ( value_keys)
508+ . build ( ) ?;
497509 let original_schema = join. schema ( ) ;
498510
499511 let optimized_join =
@@ -505,9 +517,18 @@ async fn test_null_aware_left_anti_swaps_to_right_anti(
505517 assert_eq ! ( * swapped_join. join_type( ) , JoinType :: RightAnti ) ;
506518 assert_eq ! ( * swapped_join. partition_mode( ) , PartitionMode :: CollectLeft ) ;
507519 assert ! ( swapped_join. null_aware) ;
520+ assert_eq ! ( swapped_join. null_aware_value_keys, value_keys) ;
521+ assert_eq ! ( swapped_join. on( ) . len( ) , num_keys) ;
508522 assert_eq ! ( swapped_join. left( ) . schema( ) . field( 0 ) . name( ) , "small_col" ) ;
509523 assert_eq ! ( swapped_join. right( ) . schema( ) . field( 0 ) . name( ) , "big_col" ) ;
510524 assert_eq ! ( swapped_join. schema( ) , original_schema) ;
525+ let filter_sides = swapped_join
526+ . filter ( )
527+ . map ( |filter| filter. column_indices ( ) . iter ( ) . map ( |c| c. side ) . collect ( ) ) ;
528+ assert_eq ! (
529+ filter_sides,
530+ with_filter. then( || vec![ JoinSide :: Right , JoinSide :: Left ] )
531+ ) ;
511532
512533 Ok ( ( ) )
513534}
@@ -619,97 +640,6 @@ async fn test_null_aware_left_anti_respects_disabled_join_reordering() -> Result
619640 Ok ( ( ) )
620641}
621642
622- /// A filtered null-aware `LeftAnti` (produced by decorrelating a correlated
623- /// `NOT IN`) swaps to a null-aware `RightAnti` that decides `NOT IN` per probe
624- /// row; the filter's sides are swapped with the inputs.
625- #[ tokio:: test]
626- async fn test_null_aware_left_anti_with_filter_swaps ( ) -> Result < ( ) > {
627- let ( big, small) = create_big_and_small ( ) ;
628- let join = HashJoinExec :: try_new (
629- Arc :: clone ( & big) ,
630- Arc :: clone ( & small) ,
631- vec ! [ (
632- Arc :: new( Column :: new_with_schema( "big_col" , & big. schema( ) ) ?) ,
633- Arc :: new( Column :: new_with_schema( "small_col" , & small. schema( ) ) ?) ,
634- ) ] ,
635- nl_join_filter ( ) ,
636- & JoinType :: LeftAnti ,
637- None ,
638- PartitionMode :: CollectLeft ,
639- NullEquality :: NullEqualsNothing ,
640- true ,
641- ) ?;
642-
643- let optimized_join =
644- JoinSelection :: new ( ) . optimize ( Arc :: new ( join) , & ConfigOptions :: new ( ) ) ?;
645- let swapped_join = optimized_join
646- . downcast_ref :: < HashJoinExec > ( )
647- . expect ( "anti join swap should not require a projection" ) ;
648-
649- assert_eq ! ( * swapped_join. join_type( ) , JoinType :: RightAnti ) ;
650- assert_eq ! ( * swapped_join. partition_mode( ) , PartitionMode :: CollectLeft ) ;
651- assert ! ( swapped_join. null_aware) ;
652- let filter = swapped_join. filter ( ) . expect ( "filter should be kept" ) ;
653- assert_eq ! (
654- filter
655- . column_indices( )
656- . iter( )
657- . map( |c| c. side)
658- . collect:: <Vec <_>>( ) ,
659- vec![ JoinSide :: Right , JoinSide :: Left ]
660- ) ;
661- assert_eq ! ( swapped_join. left( ) . schema( ) . field( 0 ) . name( ) , "small_col" ) ;
662- assert_eq ! ( swapped_join. right( ) . schema( ) . field( 0 ) . name( ) , "big_col" ) ;
663-
664- Ok ( ( ) )
665- }
666-
667- /// A multi-key null-aware `LeftAnti` (a multi-column `NOT IN`, correlation
668- /// scope keys, or both) swaps to a `RightAnti` that keeps the number of
669- /// `NOT IN` value keys and the key order.
670- #[ rstest]
671- #[ case( 1 ) ]
672- #[ case( 2 ) ]
673- #[ tokio:: test]
674- async fn test_null_aware_multi_key_left_anti_swaps_to_right_anti (
675- #[ case] value_keys : usize ,
676- ) -> Result < ( ) > {
677- let ( big, small) = create_big_and_small ( ) ;
678- let big_col = Arc :: new ( Column :: new_with_schema ( "big_col" , & big. schema ( ) ) ?) as _ ;
679- let small_col = Arc :: new ( Column :: new_with_schema ( "small_col" , & small. schema ( ) ) ?) as _ ;
680- let join = HashJoinExecBuilder :: new (
681- Arc :: clone ( & big) ,
682- Arc :: clone ( & small) ,
683- vec ! [
684- ( Arc :: clone( & big_col) , Arc :: clone( & small_col) ) ,
685- ( big_col, small_col) ,
686- ] ,
687- JoinType :: LeftAnti ,
688- )
689- . with_partition_mode ( PartitionMode :: CollectLeft )
690- . with_null_aware ( true )
691- . with_null_aware_value_keys ( value_keys)
692- . build ( ) ?;
693- let original_schema = join. schema ( ) ;
694-
695- let optimized_join =
696- JoinSelection :: new ( ) . optimize ( Arc :: new ( join) , & ConfigOptions :: new ( ) ) ?;
697- let swapped_join = optimized_join
698- . downcast_ref :: < HashJoinExec > ( )
699- . expect ( "anti join swap should not require a projection" ) ;
700-
701- assert_eq ! ( * swapped_join. join_type( ) , JoinType :: RightAnti ) ;
702- assert_eq ! ( * swapped_join. partition_mode( ) , PartitionMode :: CollectLeft ) ;
703- assert ! ( swapped_join. null_aware) ;
704- assert_eq ! ( swapped_join. null_aware_value_keys, value_keys) ;
705- assert_eq ! ( swapped_join. on( ) . len( ) , 2 ) ;
706- assert_eq ! ( swapped_join. left( ) . schema( ) . field( 0 ) . name( ) , "small_col" ) ;
707- assert_eq ! ( swapped_join. right( ) . schema( ) . field( 0 ) . name( ) , "big_col" ) ;
708- assert_eq ! ( swapped_join. schema( ) , original_schema) ;
709-
710- Ok ( ( ) )
711- }
712-
713643#[ tokio:: test]
714644async fn test_join_swap_uses_context_statistics_registry ( ) -> Result < ( ) > {
715645 let left: Arc < dyn ExecutionPlan > = Arc :: new ( StatisticsExec :: new (
0 commit comments