@@ -688,3 +688,75 @@ async fn asof_join_unparser_preserves_right_preselection() -> Result<()> {
688688 }
689689 Ok ( ( ) )
690690}
691+
692+ /// The `NOT IN` value key count of every decorrelated mark join in `sql`'s
693+ /// optimized plan, with whether the join is null-aware.
694+ async fn mark_join_value_keys (
695+ ctx : & SessionContext ,
696+ sql : & str ,
697+ ) -> Result < Vec < ( bool , usize ) > > {
698+ use datafusion:: common:: tree_node:: { TreeNode , TreeNodeRecursion } ;
699+ use datafusion:: logical_expr:: { JoinType , LogicalPlan } ;
700+
701+ let plan = ctx. sql ( sql) . await ?. into_optimized_plan ( ) ?;
702+ let mut joins = vec ! [ ] ;
703+ plan. apply ( |plan| {
704+ if let LogicalPlan :: Join ( join) = plan
705+ && join. join_type == JoinType :: LeftMark
706+ {
707+ joins. push ( ( join. null_aware , join. null_aware_value_keys ) ) ;
708+ }
709+ Ok ( TreeNodeRecursion :: Continue )
710+ } ) ?;
711+ Ok ( joins)
712+ }
713+
714+ /// Decorrelation sets the value key count only on a null-aware join, which is
715+ /// the only one that reads it. Any other join keeps it unset (`0`), the same as a
716+ /// join built through `Join::try_new` or `LogicalPlanBuilder`, so equal plans
717+ /// compare equal however they were built.
718+ #[ tokio:: test]
719+ async fn decorrelated_mark_join_sets_value_keys_only_when_null_aware ( ) -> Result < ( ) > {
720+ let ctx = SessionContext :: new ( ) ;
721+ for ( name, nullable) in [ ( "t" , false ) , ( "n" , true ) ] {
722+ let schema = Arc :: new ( Schema :: new ( vec ! [
723+ Field :: new( "a" , DataType :: Int32 , nullable) ,
724+ Field :: new( "b" , DataType :: Int32 , nullable) ,
725+ ] ) ) ;
726+ let batch = RecordBatch :: try_new (
727+ Arc :: clone ( & schema) ,
728+ vec ! [
729+ Arc :: new( Int32Array :: from( vec![ 1 , 2 ] ) ) ,
730+ Arc :: new( Int32Array :: from( vec![ 3 , 4 ] ) ) ,
731+ ] ,
732+ ) ?;
733+ ctx. register_table (
734+ name,
735+ Arc :: new ( MemTable :: try_new ( schema, vec ! [ vec![ batch] ] ) ?) ,
736+ ) ?;
737+ }
738+
739+ // Non-nullable keys: neither the scalar nor the multi-column `IN` is
740+ // null-aware.
741+ for sql in [
742+ "SELECT a IN (SELECT a FROM t AS s) FROM t" ,
743+ "SELECT (a, b) IN (SELECT a, b FROM t AS s) FROM t" ,
744+ ] {
745+ assert_eq ! (
746+ mark_join_value_keys( & ctx, sql) . await ?,
747+ [ ( false , 0 ) ] ,
748+ "{sql}"
749+ ) ;
750+ }
751+ // Nullable keys: both are null-aware and state their count.
752+ assert_eq ! (
753+ mark_join_value_keys( & ctx, "SELECT a IN (SELECT a FROM n AS s) FROM n" ) . await ?,
754+ [ ( true , 1 ) ]
755+ ) ;
756+ assert_eq ! (
757+ mark_join_value_keys( & ctx, "SELECT (a, b) IN (SELECT a, b FROM n AS s) FROM n" )
758+ . await ?,
759+ [ ( true , 2 ) ]
760+ ) ;
761+ Ok ( ( ) )
762+ }
0 commit comments