@@ -884,12 +884,18 @@ def _natural_join_step(self, op: data_algebra.data_ops_types.OperatorPlatform, *
884884 how = op .jointype .lower ()
885885 if how == "full" :
886886 how = "outer"
887- coalesce_columns = (
888- set (op .sources [0 ].columns_produced ()).intersection (op .sources [1 ].columns_produced ())
889- - set (op .on_a ))
890887 if how != "right" :
888+ coalesce_columns = (
889+ set (op .sources [0 ].columns_produced ()).intersection (op .sources [1 ].columns_produced ())
890+ - set (op .on_a ))
891+ orphan_keys = [c for c in op .on_b if c not in set (op .on_a )]
892+ input_right = inputs [1 ]
893+ if len (orphan_keys ) > 0 :
894+ input_right = input_right .with_columns ([
895+ pl .col (c ).alias (f"{ c } _da_join_tmp_key" ) for c in orphan_keys
896+ ])
891897 res = inputs [0 ].join (
892- inputs [ 1 ] ,
898+ input_right ,
893899 left_on = op .on_a ,
894900 right_on = op .on_b ,
895901 how = how ,
@@ -903,10 +909,21 @@ def _natural_join_step(self, op: data_algebra.data_ops_types.OperatorPlatform, *
903909 .alias (c )
904910 for c in coalesce_columns
905911 ])
912+ if len (orphan_keys ) > 0 :
913+ res = res .rename ({f"{ c } _da_join_tmp_key" : c for c in orphan_keys })
906914 else :
907915 # simulate right join with left join
916+ coalesce_columns = (
917+ set (op .sources [0 ].columns_produced ()).intersection (op .sources [1 ].columns_produced ())
918+ - set (op .on_b ))
919+ orphan_keys = [c for c in op .on_a if c not in set (op .on_b )]
920+ input_right = inputs [0 ]
921+ if len (orphan_keys ) > 0 :
922+ input_right = input_right .with_columns ([
923+ pl .col (c ).alias (f"{ c } _da_join_tmp_key" ) for c in orphan_keys
924+ ])
908925 res = inputs [1 ].join (
909- inputs [ 0 ] ,
926+ input_right ,
910927 left_on = op .on_b ,
911928 right_on = op .on_a ,
912929 how = "left" ,
@@ -920,6 +937,8 @@ def _natural_join_step(self, op: data_algebra.data_ops_types.OperatorPlatform, *
920937 .alias (c )
921938 for c in coalesce_columns
922939 ])
940+ if len (orphan_keys ) > 0 :
941+ res = res .rename ({f"{ c } _da_join_tmp_key" : c for c in orphan_keys })
923942 res = res .select (op .columns_produced ())
924943 return res
925944
0 commit comments