Skip to content

Commit 6e9b716

Browse files
authored
Fix the TableIdentifier (#44)
* Fix the TableIdentifier * Make the rest of the tests happy * Add default properties
1 parent 6a77195 commit 6e9b716

4 files changed

Lines changed: 41 additions & 11 deletions

File tree

pyiceberg/catalog/rest.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
CommitTableRequest,
6363
CommitTableResponse,
6464
Table,
65+
TableIdentifier,
6566
TableMetadata,
6667
)
6768
from pyiceberg.table.sorting import UNSORTED_SORT_ORDER, SortOrder
@@ -301,7 +302,10 @@ def _fetch_config(self) -> None:
301302
# Update URI based on overrides
302303
self.uri = config[URI]
303304

304-
def _split_identifier_for_path(self, identifier: Union[str, Identifier]) -> Properties:
305+
def _split_identifier_for_path(self, identifier: Union[str, Identifier, TableIdentifier]) -> Properties:
306+
if isinstance(identifier, TableIdentifier):
307+
return {"namespace": NAMESPACE_SEPARATOR.join(identifier.namespace.root[1:]), "table": identifier.name}
308+
305309
identifier_tuple = self.identifier_to_tuple(identifier)
306310
if len(identifier_tuple) <= 1:
307311
raise NoSuchTableError(f"Missing namespace or invalid identifier: {'.'.join(identifier_tuple)}")

pyiceberg/table/__init__.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
from pyiceberg.typedef import (
7676
EMPTY_DICT,
7777
IcebergBaseModel,
78+
IcebergRootModel,
7879
Identifier,
7980
KeyDefaultDict,
8081
Properties,
@@ -403,8 +404,25 @@ class AssertDefaultSortOrderId(TableRequirement):
403404
default_sort_order_id: int = Field(..., alias="default-sort-order-id")
404405

405406

407+
class Namespace(IcebergRootModel[List[str]]):
408+
"""Reference to one or more levels of a namespace."""
409+
410+
root: List[str] = Field(
411+
...,
412+
description='Reference to one or more levels of a namespace',
413+
example=['accounting', 'tax'],
414+
)
415+
416+
417+
class TableIdentifier(IcebergBaseModel):
418+
"""Fully Qualified identifier to a table."""
419+
420+
namespace: Namespace
421+
name: str
422+
423+
406424
class CommitTableRequest(IcebergBaseModel):
407-
identifier: Identifier = Field()
425+
identifier: TableIdentifier = Field()
408426
requirements: Tuple[SerializeAsAny[TableRequirement], ...] = Field(default_factory=tuple)
409427
updates: Tuple[SerializeAsAny[TableUpdate], ...] = Field(default_factory=tuple)
410428

@@ -535,7 +553,11 @@ def update_schema(self, allow_incompatible_changes: bool = False, case_sensitive
535553

536554
def _do_commit(self, updates: Tuple[TableUpdate, ...], requirements: Tuple[TableRequirement, ...]) -> None:
537555
response = self.catalog._commit_table( # pylint: disable=W0212
538-
CommitTableRequest(identifier=self.identifier[1:], updates=updates, requirements=requirements)
556+
CommitTableRequest(
557+
identifier=TableIdentifier(namespace=self.identifier[:-1], name=self.identifier[-1]),
558+
updates=updates,
559+
requirements=requirements,
560+
)
539561
) # pylint: disable=W0212
540562
self.metadata = response.metadata
541563
self.metadata_location = response.metadata_location

tests/catalog/test_base.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@
4646
AddSchemaUpdate,
4747
CommitTableRequest,
4848
CommitTableResponse,
49+
Namespace,
4950
SetCurrentSchemaUpdate,
5051
Table,
52+
TableIdentifier,
5153
)
5254
from pyiceberg.table.metadata import TableMetadata, TableMetadataV1, new_table_metadata
5355
from pyiceberg.table.sorting import UNSORTED_SORT_ORDER, SortOrder
@@ -119,8 +121,8 @@ def _commit_table(self, table_request: CommitTableRequest) -> CommitTableRespons
119121
for update in table_request.updates:
120122
if isinstance(update, AddSchemaUpdate):
121123
add_schema_update: AddSchemaUpdate = update
122-
identifier = Catalog.identifier_to_tuple(table_request.identifier)
123-
table = self.__tables[("com", *identifier)]
124+
identifier = tuple(table_request.identifier.namespace.root) + (table_request.identifier.name,)
125+
table = self.__tables[identifier]
124126
new_metadata = new_table_metadata(
125127
add_schema_update.schema_,
126128
table.metadata.partition_specs[0],
@@ -528,7 +530,7 @@ def test_commit_table(catalog: InMemoryCatalog) -> None:
528530
# When
529531
response = given_table.catalog._commit_table( # pylint: disable=W0212
530532
CommitTableRequest(
531-
identifier=given_table.identifier[1:],
533+
identifier=TableIdentifier(namespace=Namespace(given_table.identifier[:-1]), name=given_table.identifier[-1]),
532534
updates=[
533535
AddSchemaUpdate(schema=new_schema, last_column_id=new_schema.highest_field_id),
534536
SetCurrentSchemaUpdate(schema_id=-1),

tests/test_integration.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
TimestampType,
4747
)
4848

49+
DEFAULT_PROPERTIES = {'write.parquet.compression-codec': 'zstd'}
50+
4951

5052
@pytest.fixture()
5153
def catalog() -> Catalog:
@@ -104,25 +106,25 @@ def table(catalog: Catalog) -> Table:
104106

105107
@pytest.mark.integration
106108
def test_table_properties(table: Table) -> None:
107-
assert table.properties == {}
109+
assert table.properties == DEFAULT_PROPERTIES
108110

109111
with table.transaction() as transaction:
110112
transaction.set_properties(abc="🤪")
111113

112-
assert table.properties == {"abc": "🤪"}
114+
assert table.properties == dict(**{"abc": "🤪"}, **DEFAULT_PROPERTIES)
113115

114116
with table.transaction() as transaction:
115117
transaction.remove_properties("abc")
116118

117-
assert table.properties == {}
119+
assert table.properties == DEFAULT_PROPERTIES
118120

119121
table = table.transaction().set_properties(abc="def").commit_transaction()
120122

121-
assert table.properties == {"abc": "def"}
123+
assert table.properties == dict(**{"abc": "def"}, **DEFAULT_PROPERTIES)
122124

123125
table = table.transaction().remove_properties("abc").commit_transaction()
124126

125-
assert table.properties == {}
127+
assert table.properties == DEFAULT_PROPERTIES
126128

127129

128130
@pytest.fixture()

0 commit comments

Comments
 (0)