@@ -674,6 +674,125 @@ def test_upsert_with_struct_field_as_join_key(catalog: Catalog) -> None:
674674 _ = tbl .upsert (update_data , join_cols = ["nested_type" ])
675675
676676
677+ @pytest .mark .parametrize ("branch" , ["main" , "dev" ])
678+ def test_upsert_after_union_by_name (catalog : Catalog , branch : str ) -> None :
679+ original = pa .table ({"id" : [1 , 2 ], "value" : ["one" , "two" ]})
680+ table = catalog .create_table ("default.test_upsert_after_union_by_name" , original .schema )
681+ table .append (original )
682+ snapshot = table .current_snapshot ()
683+ assert snapshot is not None
684+ table .manage_snapshots ().create_branch (snapshot_id = snapshot .snapshot_id , branch_name = "dev" ).commit ()
685+
686+ source = pa .table ({"id" : [1 , 2 , 3 ], "value" : ["one" , "two" , "three" ], "note" : ["updated" , None , "inserted" ]})
687+ with table .update_schema () as update_schema :
688+ update_schema .union_by_name (source .schema )
689+
690+ result = table .upsert (source , join_cols = ["id" ], branch = branch )
691+
692+ assert_upsert_result (result , expected_updated = 1 , expected_inserted = 1 )
693+ assert table .scan ().use_ref (branch ).to_arrow ().sort_by ([("id" , "ascending" )]).to_pylist () == [
694+ {"id" : 1 , "value" : "one" , "note" : "updated" },
695+ {"id" : 2 , "value" : "two" , "note" : None },
696+ {"id" : 3 , "value" : "three" , "note" : "inserted" },
697+ ]
698+
699+
700+ @pytest .mark .parametrize ("branch" , ["main" , "dev" ])
701+ def test_upsert_without_new_optional_column (catalog : Catalog , branch : str ) -> None :
702+ original = pa .table ({"id" : [1 , 2 ], "value" : ["one" , "two" ]})
703+ table = catalog .create_table ("default.test_upsert_without_new_optional_column" , original .schema )
704+ table .append (original )
705+ snapshot = table .current_snapshot ()
706+ assert snapshot is not None
707+ table .manage_snapshots ().create_branch (snapshot_id = snapshot .snapshot_id , branch_name = "dev" ).commit ()
708+ table .update_schema ().add_column ("note" , StringType ()).commit ()
709+
710+ source = pa .table ({"id" : pa .array ([1 , 2 , 3 ], type = pa .int32 ()), "value" : ["updated" , "two" , "inserted" ]})
711+ result = table .upsert (source , join_cols = ["id" ], branch = branch )
712+
713+ assert_upsert_result (result , expected_updated = 1 , expected_inserted = 1 )
714+ assert table .scan ().use_ref (branch ).to_arrow ().sort_by ([("id" , "ascending" )]).to_pylist () == [
715+ {"id" : 1 , "value" : "updated" , "note" : None },
716+ {"id" : 2 , "value" : "two" , "note" : None },
717+ {"id" : 3 , "value" : "inserted" , "note" : None },
718+ ]
719+
720+
721+ def test_upsert_without_new_optional_struct_field (catalog : Catalog ) -> None :
722+ original = pa .table ({"id" : [1 , 2 ], "payload" : [{"value" : "one" }, {"value" : "two" }]})
723+ table = catalog .create_table ("default.test_upsert_without_new_optional_struct_field" , original .schema )
724+ table .append (original )
725+ table .update_schema ().add_column (("payload" , "note" ), StringType ()).commit ()
726+
727+ source = pa .table ({"id" : [1 , 2 , 3 ], "payload" : [{"value" : "updated" }, {"value" : "two" }, {"value" : "inserted" }]})
728+ result = table .upsert (source , join_cols = ["id" ])
729+
730+ assert_upsert_result (result , expected_updated = 1 , expected_inserted = 1 )
731+ assert table .scan ().to_arrow ().sort_by ([("id" , "ascending" )]).to_pylist () == [
732+ {"id" : 1 , "payload" : {"value" : "updated" , "note" : None }},
733+ {"id" : 2 , "payload" : {"value" : "two" , "note" : None }},
734+ {"id" : 3 , "payload" : {"value" : "inserted" , "note" : None }},
735+ ]
736+
737+
738+ def test_upsert_matches_renamed_struct_fields_by_id (catalog : Catalog ) -> None :
739+ original = pa .table ({"id" : [1 ], "payload" : [{"a" : 1 , "b" : 2 }]})
740+ table = catalog .create_table ("default.test_upsert_matches_renamed_struct_fields_by_id" , original .schema )
741+ table .append (original )
742+ original_schema = table .schema ().as_arrow ()
743+ with table .update_schema () as update_schema :
744+ update_schema .rename_column (("payload" , "a" ), "b" )
745+ update_schema .rename_column (("payload" , "b" ), "a" )
746+
747+ source = pa .Table .from_pylist ([{"id" : 1 , "payload" : {"a" : 2 , "b" : 1 }}], schema = original_schema )
748+ result = table .upsert (source , join_cols = ["id" ])
749+
750+ assert_upsert_result (result , expected_updated = 1 , expected_inserted = 0 )
751+ assert table .scan ().to_arrow ().to_pylist () == [{"id" : 1 , "payload" : {"b" : 2 , "a" : 1 }}]
752+ assert_upsert_result (table .upsert (source , join_cols = ["id" ]), expected_updated = 0 , expected_inserted = 0 )
753+
754+
755+ def test_upsert_matches_renamed_column_by_id (catalog : Catalog ) -> None :
756+ original = pa .table ({"id" : [1 ], "value" : ["old" ]})
757+ table = catalog .create_table ("default.test_upsert_matches_renamed_column_by_id" , original .schema )
758+ table .append (original )
759+ original_schema = table .schema ().as_arrow ()
760+ table .update_schema ().rename_column ("value" , "renamed" ).commit ()
761+
762+ source = pa .Table .from_pylist ([{"id" : 1 , "value" : "updated" }], schema = original_schema )
763+ result = table .upsert (source , join_cols = ["id" ])
764+
765+ assert_upsert_result (result , expected_updated = 1 , expected_inserted = 0 )
766+ assert table .scan ().to_arrow ().to_pylist () == [{"id" : 1 , "renamed" : "updated" }]
767+
768+
769+ def test_upsert_ignores_dropped_field_with_reused_name (catalog : Catalog ) -> None :
770+ original = pa .table ({"id" : [1 , 2 ], "value" : ["old one" , "old two" ], "label" : ["one" , "two" ]})
771+ table = catalog .create_table ("default.test_upsert_ignores_dropped_field_with_reused_name" , original .schema )
772+ table .append (original )
773+ original_schema = table .schema ()
774+ table .update_schema ().delete_column ("value" ).commit ()
775+ table .update_schema ().add_column ("value" , StringType ()).commit ()
776+ assert table .schema ().find_field ("value" ).field_id != original_schema .find_field ("value" ).field_id
777+
778+ source = pa .Table .from_pylist (
779+ [
780+ {"id" : 1 , "value" : "stale update" , "label" : "updated" },
781+ {"id" : 2 , "value" : "stale unchanged" , "label" : "two" },
782+ {"id" : 3 , "value" : "stale insert" , "label" : "inserted" },
783+ ],
784+ schema = original_schema .as_arrow (),
785+ )
786+ result = table .upsert (source , join_cols = ["id" ])
787+
788+ assert_upsert_result (result , expected_updated = 1 , expected_inserted = 1 )
789+ assert table .scan ().to_arrow ().sort_by ([("id" , "ascending" )]).to_pylist () == [
790+ {"id" : 1 , "label" : "updated" , "value" : None },
791+ {"id" : 2 , "label" : "two" , "value" : None },
792+ {"id" : 3 , "label" : "inserted" , "value" : None },
793+ ]
794+
795+
677796def test_upsert_with_nulls (catalog : Catalog ) -> None :
678797 identifier = "default.test_upsert_with_nulls"
679798 _drop_table (catalog , identifier )
0 commit comments