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

Python API - delta.appendOnly enforcement #590

Merged
Merged
Show file tree
Hide file tree
Changes from 7 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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ tlaplus/*.toolbox/*/[0-9]*-[0-9]*-[0-9]*-[0-9]*-[0-9]*-[0-9]*/
.vscode
.env
**/.DS_Store
**/.python-version
**/.python-version
.coverage
11 changes: 8 additions & 3 deletions python/deltalake/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,18 @@ def write_deltalake(
:param filesystem: Optional filesystem to pass to PyArrow. If not provided will
be inferred from uri.
:param mode: How to handle existing data. Default is to error if table
already exists. If 'append', will add new data. If 'overwrite', will
replace table with new data. If 'ignore', will not write anything if
table already exists.
already exists.
If 'append', will add new data.
If 'overwrite', will replace table with new data.
If 'ignore', will not write anything if table already exists.
:param name: User-provided identifier for this table.
:param description: User-provided description for this table.
:param configuration: A map containing configuration options for the metadata action.
"""
config_delta_append_only = configuration and configuration.get("delta.appendOnly", "false") == "true"
if config_delta_append_only and mode != "append":
raise ValueError("If configuration has delta.appendOnly = 'true', mode must be 'append'")

if isinstance(data, pd.DataFrame):
data = pa.Table.from_pandas(data)

Expand Down
18 changes: 18 additions & 0 deletions python/tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,24 @@ def test_write_modes(tmp_path: pathlib.Path, sample_data: pa.Table):
assert DeltaTable(path).to_pyarrow_table() == sample_data


def test_append_only(tmp_path: pathlib.Path, sample_data: pa.Table):
WarSame marked this conversation as resolved.
Show resolved Hide resolved
path = str(tmp_path)

config = {"delta.appendOnly": "true"}
write_deltalake(path, sample_data, mode="append", configuration=config)

with pytest.raises(ValueError):
write_deltalake(path, sample_data, configuration=config)

with pytest.raises(ValueError):
write_deltalake(path, sample_data, configuration=config, mode="overwrite")

with pytest.raises(ValueError):
write_deltalake(path, sample_data, configuration=config, mode="ignore")

assert DeltaTable(path).to_pyarrow_table() == sample_data


def test_writer_with_table(existing_table: DeltaTable, sample_data: pa.Table):
write_deltalake(existing_table, sample_data, mode="overwrite")
existing_table.update_incremental()
Expand Down