Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set table properties with dictionary #503

Merged
merged 2 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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