Skip to content

Commit

Permalink
Set table properties with dictionary (#503)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinjqliu committed Mar 8, 2024
1 parent e56326d commit ad40573
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
8 changes: 6 additions & 2 deletions pyiceberg/table/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,17 +294,21 @@ def upgrade_table_version(self, format_version: Literal[1, 2]) -> Transaction:

return self

def set_properties(self, **updates: str) -> Transaction:
def set_properties(self, properties: Properties = EMPTY_DICT, **kwargs: str) -> Transaction:
"""Set properties.
When a property is already set, it will be overwritten.
Args:
updates: The properties set on the table.
properties: The properties set on the table.
kwargs: properties can also be pass as kwargs.
Returns:
The alter table builder.
"""
if properties and kwargs:
raise ValueError("Cannot pass both properties and kwargs")
updates = properties or kwargs
return self._apply((SetPropertiesUpdate(updates=updates),))

def update_schema(self, allow_incompatible_changes: bool = False, case_sensitive: bool = True) -> UpdateSchema:
Expand Down
34 changes: 31 additions & 3 deletions tests/integration/test_reads.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,22 +107,50 @@ def test_table_properties(catalog: Catalog) -> None:

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

assert table.properties == dict(abc="🤪", **DEFAULT_PROPERTIES)

with table.transaction() as transaction:
transaction.remove_properties("abc")

assert table.properties == DEFAULT_PROPERTIES

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

assert table.properties == dict(abc="def", **DEFAULT_PROPERTIES)

table = table.transaction().remove_properties("abc").commit_transaction()
assert table.properties == DEFAULT_PROPERTIES


@pytest.mark.integration
@pytest.mark.parametrize('catalog', [pytest.lazy_fixture('catalog_hive'), pytest.lazy_fixture('catalog_rest')])
def test_table_properties_dict(catalog: Catalog) -> None:
table = create_table(catalog)

assert table.properties == DEFAULT_PROPERTIES

with table.transaction() as transaction:
transaction.set_properties({"abc": "🤪"})
assert table.properties == dict({"abc": "🤪"}, **DEFAULT_PROPERTIES)

with table.transaction() as transaction:
transaction.remove_properties("abc")
assert table.properties == DEFAULT_PROPERTIES

table = table.transaction().set_properties({"abc": "def"}).commit_transaction()
assert table.properties == dict({"abc": "def"}, **DEFAULT_PROPERTIES)

table = table.transaction().remove_properties("abc").commit_transaction()
assert table.properties == DEFAULT_PROPERTIES


@pytest.mark.integration
@pytest.mark.parametrize('catalog', [pytest.lazy_fixture('catalog_hive'), pytest.lazy_fixture('catalog_rest')])
def test_table_properties_error(catalog: Catalog) -> None:
table = create_table(catalog)
properties = {"abc": "def"}
with pytest.raises(ValueError) as e:
table.transaction().set_properties(properties, abc="def").commit_transaction()
assert "Cannot pass both properties and kwargs" in str(e.value)


@pytest.mark.integration
@pytest.mark.parametrize('catalog', [pytest.lazy_fixture('catalog_hive'), pytest.lazy_fixture('catalog_rest')])
Expand Down

0 comments on commit ad40573

Please sign in to comment.