gabeiglio commented on code in PR #2478:
URL: https://github.com/apache/iceberg-python/pull/2478#discussion_r2365902430


##########
tests/integration/test_catalog.py:
##########
@@ -343,3 +345,67 @@ def test_update_namespace_properties(test_catalog: 
Catalog, database_name: str)
         else:
             assert k in update_report.removed
     assert "updated test description" == 
test_catalog.load_namespace_properties(database_name)["comment"]
+
+
[email protected]
[email protected]("test_catalog", CATALOGS)
+def test_update_table_schema(test_catalog: Catalog, table_schema_nested: 
Schema, table_name: str, database_name: str) -> None:
+    identifier = (database_name, table_name)
+    test_catalog.create_namespace(database_name)
+    table = test_catalog.create_table(identifier, table_schema_nested)
+
+    update = table.update_schema().add_column("new_col", LongType())
+    update.commit()
+
+    loaded = test_catalog.load_table(identifier)
+
+    assert loaded.schema().find_field("new_col", case_sensitive=False)
+
+
[email protected]
[email protected]("test_catalog", CATALOGS)
+def test_update_table_schema_conflict(
+    test_catalog: Catalog, table_schema_nested: Schema, table_name: str, 
database_name: str
+) -> None:
+    identifier = (database_name, table_name)
+    test_catalog.create_namespace(database_name)
+    table = test_catalog.create_table(identifier, table_schema_nested)
+
+    update = table.update_schema().add_column("new_col", LongType())
+
+    # update the schema concurrently so that the original update fails
+    concurrent_table = test_catalog.load_table(identifier)
+    # The test schema is assumed to have a `bar` column that can be deleted.
+    concurrent_update = 
concurrent_table.update_schema(allow_incompatible_changes=True)
+    concurrent_update.set_identifier_fields("foo")
+    concurrent_update.update_column("foo", required=True)
+    concurrent_update.delete_column("bar")
+    concurrent_update.commit()
+
+    # attempt to commit the original update
+    with pytest.raises(CommitFailedException, match="Requirement failed: 
current schema"):
+        update.commit()
+
+    loaded = test_catalog.load_table(identifier)
+    assert loaded.schema() == concurrent_table.schema()
+
+
[email protected]
[email protected]("test_catalog", CATALOGS)
+def test_update_table_schema_then_revert(
+    test_catalog: Catalog, table_schema_nested: Schema, table_name: str, 
database_name: str
+) -> None:
+    identifier = (database_name, table_name)
+    test_catalog.create_namespace(database_name)
+    table = test_catalog.create_table(identifier, table_schema_nested)
+    original_schema_struct = table.schema().as_struct()
+
+    table.update_schema().add_column("col1", StringType()).add_column("col2", 
StringType()).add_column(
+        "col3", StringType()
+    ).commit()
+
+    table_with_cols = test_catalog.load_table(identifier)
+    
table_with_cols.update_schema().delete_column("col1").delete_column("col2").delete_column("col3").commit()

Review Comment:
   same here (again, if the linter allows it)
   ```suggestion
       table_with_cols.update_schema()
         .delete_column("col1")
         .delete_column("col2")
         .delete_column("col3")
         .commit()
   ```



##########
tests/integration/test_catalog.py:
##########
@@ -343,3 +345,67 @@ def test_update_namespace_properties(test_catalog: 
Catalog, database_name: str)
         else:
             assert k in update_report.removed
     assert "updated test description" == 
test_catalog.load_namespace_properties(database_name)["comment"]
+
+
[email protected]
[email protected]("test_catalog", CATALOGS)
+def test_update_table_schema(test_catalog: Catalog, table_schema_nested: 
Schema, table_name: str, database_name: str) -> None:
+    identifier = (database_name, table_name)
+    test_catalog.create_namespace(database_name)
+    table = test_catalog.create_table(identifier, table_schema_nested)
+
+    update = table.update_schema().add_column("new_col", LongType())
+    update.commit()
+
+    loaded = test_catalog.load_table(identifier)
+
+    assert loaded.schema().find_field("new_col", case_sensitive=False)
+
+
[email protected]
[email protected]("test_catalog", CATALOGS)
+def test_update_table_schema_conflict(
+    test_catalog: Catalog, table_schema_nested: Schema, table_name: str, 
database_name: str
+) -> None:
+    identifier = (database_name, table_name)
+    test_catalog.create_namespace(database_name)
+    table = test_catalog.create_table(identifier, table_schema_nested)
+
+    update = table.update_schema().add_column("new_col", LongType())
+
+    # update the schema concurrently so that the original update fails
+    concurrent_table = test_catalog.load_table(identifier)
+    # The test schema is assumed to have a `bar` column that can be deleted.
+    concurrent_update = 
concurrent_table.update_schema(allow_incompatible_changes=True)
+    concurrent_update.set_identifier_fields("foo")
+    concurrent_update.update_column("foo", required=True)
+    concurrent_update.delete_column("bar")
+    concurrent_update.commit()

Review Comment:
   I think this is a bit more readable wdyt?
   ```suggestion
       with concurrent_table.update_schema(allow_incompatible_changes=True) as 
concurrent_update:
         concurrent_update.set_identifier_fields("foo")
         concurrent_update.update_column("foo", required=True)
         concurrent_update.delete_column("bar")
   ```



##########
tests/integration/test_catalog.py:
##########
@@ -343,3 +345,67 @@ def test_update_namespace_properties(test_catalog: 
Catalog, database_name: str)
         else:
             assert k in update_report.removed
     assert "updated test description" == 
test_catalog.load_namespace_properties(database_name)["comment"]
+
+
[email protected]
[email protected]("test_catalog", CATALOGS)
+def test_update_table_schema(test_catalog: Catalog, table_schema_nested: 
Schema, table_name: str, database_name: str) -> None:
+    identifier = (database_name, table_name)
+    test_catalog.create_namespace(database_name)
+    table = test_catalog.create_table(identifier, table_schema_nested)
+
+    update = table.update_schema().add_column("new_col", LongType())
+    update.commit()
+
+    loaded = test_catalog.load_table(identifier)
+
+    assert loaded.schema().find_field("new_col", case_sensitive=False)
+
+
[email protected]
[email protected]("test_catalog", CATALOGS)
+def test_update_table_schema_conflict(
+    test_catalog: Catalog, table_schema_nested: Schema, table_name: str, 
database_name: str
+) -> None:
+    identifier = (database_name, table_name)
+    test_catalog.create_namespace(database_name)
+    table = test_catalog.create_table(identifier, table_schema_nested)
+
+    update = table.update_schema().add_column("new_col", LongType())
+
+    # update the schema concurrently so that the original update fails
+    concurrent_table = test_catalog.load_table(identifier)
+    # The test schema is assumed to have a `bar` column that can be deleted.
+    concurrent_update = 
concurrent_table.update_schema(allow_incompatible_changes=True)
+    concurrent_update.set_identifier_fields("foo")
+    concurrent_update.update_column("foo", required=True)
+    concurrent_update.delete_column("bar")
+    concurrent_update.commit()
+
+    # attempt to commit the original update
+    with pytest.raises(CommitFailedException, match="Requirement failed: 
current schema"):
+        update.commit()
+
+    loaded = test_catalog.load_table(identifier)
+    assert loaded.schema() == concurrent_table.schema()
+
+
[email protected]
[email protected]("test_catalog", CATALOGS)
+def test_update_table_schema_then_revert(
+    test_catalog: Catalog, table_schema_nested: Schema, table_name: str, 
database_name: str
+) -> None:
+    identifier = (database_name, table_name)
+    test_catalog.create_namespace(database_name)
+    table = test_catalog.create_table(identifier, table_schema_nested)
+    original_schema_struct = table.schema().as_struct()
+
+    table.update_schema().add_column("col1", StringType()).add_column("col2", 
StringType()).add_column(
+        "col3", StringType()
+    ).commit()

Review Comment:
   small nit to format for better readability (if the linter allows it)
   ```suggestion
       table.update_schema()
         .add_column("col1", StringType())
         .add_column("col2", StringType())
         .add_column( "col3", StringType())
         .commit()
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to