Skip to content

Commit 0213dab

Browse files
authored
Fix issues related to having catalog_name in identifier (#964)
* first attempt * add license * refactor new tests
1 parent dd8d76d commit 0213dab

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

Diff for: pyiceberg/catalog/rest.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,10 @@ def _identifier_to_validated_tuple(self, identifier: Union[str, Identifier]) ->
354354

355355
def _split_identifier_for_path(self, identifier: Union[str, Identifier, TableIdentifier]) -> Properties:
356356
if isinstance(identifier, TableIdentifier):
357-
return {"namespace": NAMESPACE_SEPARATOR.join(identifier.namespace.root[1:]), "table": identifier.name}
357+
if identifier.namespace.root[0] == self.name:
358+
return {"namespace": NAMESPACE_SEPARATOR.join(identifier.namespace.root[1:]), "table": identifier.name}
359+
else:
360+
return {"namespace": NAMESPACE_SEPARATOR.join(identifier.namespace.root), "table": identifier.name}
358361
identifier_tuple = self._identifier_to_validated_tuple(identifier)
359362
return {"namespace": NAMESPACE_SEPARATOR.join(identifier_tuple[:-1]), "table": identifier_tuple[-1]}
360363

@@ -675,6 +678,17 @@ def rename_table(self, from_identifier: Union[str, Identifier], to_identifier: U
675678

676679
return self.load_table(to_identifier)
677680

681+
def _remove_catalog_name_from_table_request_identifier(self, table_request: CommitTableRequest) -> CommitTableRequest:
682+
if table_request.identifier.namespace.root[0] == self.name:
683+
return table_request.model_copy(
684+
update={
685+
"identifier": TableIdentifier(
686+
namespace=table_request.identifier.namespace.root[1:], name=table_request.identifier.name
687+
).model_dump()
688+
}
689+
)
690+
return table_request
691+
678692
@retry(**_RETRY_ARGS)
679693
def _commit_table(self, table_request: CommitTableRequest) -> CommitTableResponse:
680694
"""Update the table.
@@ -692,7 +706,7 @@ def _commit_table(self, table_request: CommitTableRequest) -> CommitTableRespons
692706
"""
693707
response = self._session.post(
694708
self.url(Endpoints.update_table, prefixed=True, **self._split_identifier_for_path(table_request.identifier)),
695-
data=table_request.model_dump_json().encode(UTF8),
709+
data=self._remove_catalog_name_from_table_request_identifier(table_request).model_dump_json().encode(UTF8),
696710
)
697711
try:
698712
response.raise_for_status()

Diff for: tests/catalog/test_rest.py

+23-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
from pyiceberg.io import load_file_io
3838
from pyiceberg.partitioning import PartitionField, PartitionSpec
3939
from pyiceberg.schema import Schema
40-
from pyiceberg.table import Table
40+
from pyiceberg.table import CommitTableRequest, Table, TableIdentifier
4141
from pyiceberg.table.metadata import TableMetadataV1
4242
from pyiceberg.table.sorting import SortField, SortOrder
4343
from pyiceberg.transforms import IdentityTransform, TruncateTransform
@@ -1226,3 +1226,25 @@ def test_catalog_from_parameters_empty_env(rest_mock: Mocker) -> None:
12261226

12271227
catalog = cast(RestCatalog, load_catalog("production", uri="https://other-service.io/api"))
12281228
assert catalog.uri == "https://other-service.io/api"
1229+
1230+
1231+
def test_table_identifier_in_commit_table_request(rest_mock: Mocker, example_table_metadata_v2: Dict[str, Any]) -> None:
1232+
test_table_request = CommitTableRequest(
1233+
identifier=TableIdentifier(namespace=("catalog_name", "namespace"), name="table_name"),
1234+
updates=[],
1235+
requirements=[],
1236+
)
1237+
rest_mock.post(
1238+
url=f"{TEST_URI}v1/namespaces/namespace/tables/table_name",
1239+
json={
1240+
"metadata": example_table_metadata_v2,
1241+
"metadata-location": "test",
1242+
},
1243+
status_code=200,
1244+
request_headers=TEST_HEADERS,
1245+
)
1246+
RestCatalog("catalog_name", uri=TEST_URI, token=TEST_TOKEN)._commit_table(test_table_request)
1247+
assert (
1248+
rest_mock.last_request.text
1249+
== """{"identifier":{"namespace":["namespace"],"name":"table_name"},"requirements":[],"updates":[]}"""
1250+
)

Diff for: tests/integration/test_writes/test_writes.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
from pyspark.sql import SparkSession
3434
from pytest_mock.plugin import MockerFixture
3535

36-
from pyiceberg.catalog import Catalog
36+
from pyiceberg.catalog import Catalog, load_catalog
3737
from pyiceberg.catalog.hive import HiveCatalog
3838
from pyiceberg.catalog.rest import RestCatalog
3939
from pyiceberg.catalog.sql import SqlCatalog
@@ -1282,3 +1282,14 @@ def test_merge_manifests_file_content(session_catalog: Catalog, arrow_table_with
12821282
(11, 3),
12831283
(12, 3),
12841284
]
1285+
1286+
1287+
@pytest.mark.integration
1288+
def test_rest_catalog_with_empty_catalog_name_append_data(session_catalog: Catalog, arrow_table_with_null: pa.Table) -> None:
1289+
identifier = "default.test_rest_append"
1290+
test_catalog = load_catalog(
1291+
"", # intentionally empty
1292+
**session_catalog.properties,
1293+
)
1294+
tbl = _create_table(test_catalog, identifier, data=[])
1295+
tbl.append(arrow_table_with_null)

0 commit comments

Comments
 (0)