Skip to content

Commit 3ccdc44

Browse files
authored
Bug Fix: metadata_location to be optional in TableResponse (#1321)
* make metadata_location optional * add test for staged creation * revert * revert from testing
1 parent 2ba86b5 commit 3ccdc44

File tree

2 files changed

+76
-2
lines changed

2 files changed

+76
-2
lines changed

pyiceberg/catalog/rest.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def _retry_hook(retry_state: RetryCallState) -> None:
156156

157157

158158
class TableResponse(IcebergBaseModel):
159-
metadata_location: Optional[str] = Field(alias="metadata-location")
159+
metadata_location: Optional[str] = Field(alias="metadata-location", default=None)
160160
metadata: TableMetadata
161161
config: Properties = Field(default_factory=dict)
162162

@@ -599,7 +599,6 @@ def _create_table(
599599
response.raise_for_status()
600600
except HTTPError as exc:
601601
self._handle_non_200_response(exc, {409: TableAlreadyExistsError})
602-
603602
return TableResponse(**response.json())
604603

605604
@retry(**_RETRY_ARGS)

tests/catalog/test_rest.py

+75
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,17 @@ def example_table_metadata_with_snapshot_v1_rest_json(example_table_metadata_wit
7979
}
8080

8181

82+
@pytest.fixture
83+
def example_table_metadata_with_no_location(example_table_metadata_with_snapshot_v1: Dict[str, Any]) -> Dict[str, Any]:
84+
return {
85+
"metadata": example_table_metadata_with_snapshot_v1,
86+
"config": {
87+
"client.factory": "io.tabular.iceberg.catalog.TabularAwsClientFactory",
88+
"region": "us-west-2",
89+
},
90+
}
91+
92+
8293
@pytest.fixture
8394
def example_table_metadata_no_snapshot_v1_rest_json(example_table_metadata_no_snapshot_v1: Dict[str, Any]) -> Dict[str, Any]:
8495
return {
@@ -899,6 +910,70 @@ def test_create_table_with_given_location_removes_trailing_slash_200(
899910
assert rest_mock.last_request.json()["location"] == location
900911

901912

913+
def test_create_staged_table_200(
914+
rest_mock: Mocker,
915+
table_schema_simple: Schema,
916+
example_table_metadata_with_no_location: Dict[str, Any],
917+
example_table_metadata_no_snapshot_v1_rest_json: Dict[str, Any],
918+
) -> None:
919+
rest_mock.post(
920+
f"{TEST_URI}v1/namespaces/fokko/tables",
921+
json=example_table_metadata_with_no_location,
922+
status_code=200,
923+
request_headers=TEST_HEADERS,
924+
)
925+
rest_mock.post(
926+
f"{TEST_URI}v1/namespaces/fokko/tables/fokko2",
927+
json=example_table_metadata_no_snapshot_v1_rest_json,
928+
status_code=200,
929+
request_headers=TEST_HEADERS,
930+
)
931+
identifier = ("fokko", "fokko2")
932+
catalog = RestCatalog("rest", uri=TEST_URI, token=TEST_TOKEN)
933+
txn = catalog.create_table_transaction(
934+
identifier=identifier,
935+
schema=table_schema_simple,
936+
location=None,
937+
partition_spec=PartitionSpec(
938+
PartitionField(source_id=1, field_id=1000, transform=TruncateTransform(width=3), name="id"), spec_id=1
939+
),
940+
sort_order=SortOrder(SortField(source_id=2, transform=IdentityTransform())),
941+
properties={"owner": "fokko"},
942+
)
943+
txn.commit_transaction()
944+
945+
actual_response = rest_mock.last_request.json()
946+
expected = {
947+
"identifier": {"namespace": ["fokko"], "name": "fokko2"},
948+
"requirements": [{"type": "assert-create"}],
949+
"updates": [
950+
{"action": "assign-uuid", "uuid": "b55d9dda-6561-423a-8bfc-787980ce421f"},
951+
{"action": "upgrade-format-version", "format-version": 1},
952+
{
953+
"action": "add-schema",
954+
"schema": {
955+
"type": "struct",
956+
"fields": [
957+
{"id": 1, "name": "id", "type": "int", "required": False},
958+
{"id": 2, "name": "data", "type": "string", "required": False},
959+
],
960+
"schema-id": 0,
961+
"identifier-field-ids": [],
962+
},
963+
"last-column-id": 2,
964+
},
965+
{"action": "set-current-schema", "schema-id": -1},
966+
{"action": "add-spec", "spec": {"spec-id": 0, "fields": []}},
967+
{"action": "set-default-spec", "spec-id": -1},
968+
{"action": "add-sort-order", "sort-order": {"order-id": 0, "fields": []}},
969+
{"action": "set-default-sort-order", "sort-order-id": -1},
970+
{"action": "set-location", "location": "s3://warehouse/database/table"},
971+
{"action": "set-properties", "updates": {"owner": "bryan", "write.metadata.compression-codec": "gzip"}},
972+
],
973+
}
974+
assert actual_response == expected
975+
976+
902977
def test_create_table_409(rest_mock: Mocker, table_schema_simple: Schema) -> None:
903978
rest_mock.post(
904979
f"{TEST_URI}v1/namespaces/fokko/tables",

0 commit comments

Comments
 (0)