Skip to content

Commit be91f8a

Browse files
author
Alexandra Popova
committed
Check storage-credentials before config in table response
1 parent 6020f24 commit be91f8a

2 files changed

Lines changed: 48 additions & 2 deletions

File tree

pyiceberg/catalog/rest/__init__.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ class TableResponse(IcebergBaseModel):
156156
metadata_location: Optional[str] = Field(alias="metadata-location", default=None)
157157
metadata: TableMetadata
158158
config: Properties = Field(default_factory=dict)
159+
storage_credentials: Optional[Properties] = Field(alias="storage-credentials", default=None)
159160

160161

161162
class CreateTableRequest(IcebergBaseModel):
@@ -454,7 +455,11 @@ def _response_to_table(self, identifier_tuple: Tuple[str, ...], table_response:
454455
metadata_location=table_response.metadata_location, # type: ignore
455456
metadata=table_response.metadata,
456457
io=self._load_file_io(
457-
{**table_response.metadata.properties, **table_response.config}, table_response.metadata_location
458+
{
459+
**table_response.metadata.properties,
460+
**(table_response.storage_credentials or table_response.config),
461+
},
462+
table_response.metadata_location,
458463
),
459464
catalog=self,
460465
config=table_response.config,
@@ -466,7 +471,11 @@ def _response_to_staged_table(self, identifier_tuple: Tuple[str, ...], table_res
466471
metadata_location=table_response.metadata_location, # type: ignore
467472
metadata=table_response.metadata,
468473
io=self._load_file_io(
469-
{**table_response.metadata.properties, **table_response.config}, table_response.metadata_location
474+
{
475+
**table_response.metadata.properties,
476+
**(table_response.storage_credentials or table_response.config),
477+
},
478+
table_response.metadata_location,
470479
),
471480
catalog=self,
472481
)

tests/catalog/test_rest.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import base64
1919
import os
2020
from typing import Any, Callable, Dict, cast
21+
from copy import deepcopy
2122
from unittest import mock
2223

2324
import pytest
@@ -858,6 +859,42 @@ def test_load_table_200(rest_mock: Mocker, example_table_metadata_with_snapshot_
858859
assert actual == expected
859860

860861

862+
def test_load_table_prefers_storage_credentials_over_config(
863+
rest_mock: Mocker, example_table_metadata_with_snapshot_v1_rest_json: Dict[str, Any], monkeypatch: pytest.MonkeyPatch
864+
) -> None:
865+
table_resp = deepcopy(example_table_metadata_with_snapshot_v1_rest_json)
866+
table_resp["config"] = {"some.key": "from-config", "only.config": "only-config"}
867+
table_resp["storage-credentials"] = {"some.key": "from-cred", "only.creds": "only-creds"}
868+
869+
table_resp["metadata"].setdefault("properties", {})["meta.key"] = "from-metadata"
870+
871+
captured: Dict[str, Any] = {}
872+
873+
def _capture_io(_self: Any, properties: Dict[str, Any], location: Any) -> Any: # type: ignore
874+
captured["properties"] = dict(properties)
875+
captured["location"] = location
876+
class _DummyIO:
877+
pass
878+
return _DummyIO()
879+
880+
monkeypatch.setattr(RestCatalog, "_load_file_io", _capture_io, raising=True)
881+
882+
rest_mock.get(
883+
f"{TEST_URI}v1/namespaces/fokko/tables/table",
884+
json=table_resp,
885+
status_code=200,
886+
request_headers=TEST_HEADERS,
887+
)
888+
889+
catalog = RestCatalog("rest", uri=TEST_URI, token=TEST_TOKEN)
890+
_ = catalog.load_table(("fokko", "table"))
891+
892+
props = cast(Dict[str, Any], captured["properties"])
893+
assert props["meta.key"] == "from-metadata"
894+
assert props["some.key"] == "from-cred"
895+
assert props["only.creds"] == "only-creds"
896+
897+
861898
def test_load_table_200_loading_mode(
862899
rest_mock: Mocker, example_table_metadata_with_snapshot_v1_rest_json: Dict[str, Any]
863900
) -> None:

0 commit comments

Comments
 (0)