Sub topic 14
Appending, overwriting and deleting existing data
This chapter assumes you can already call ds.write_dataset and know what a Hive-partitioned directory looks like. Here you will see what happens when the destination already holds files.
The three behaviors
write_dataset takes an existing_data_behavior argument. It has three values.
| Value | What it does |
|---|---|
| `"error"` | Default. Raises an error if the destination directory already contains data. |
| `"overwrite_or_ignore"` | Writes new files and leaves other existing files alone. A file is replaced only if it has the same name as a file being written. |
| `"delete_matching"` | Deletes the existing contents of each partition directory the write touches, then writes the new files. Other partitions are untouched. |
Appending safely
overwrite_or_ignore does not append by itself. Each write names its files with the default template part-{i}.parquet. A second write therefore produces part-0.parquet again and replaces the first one. To append, give every write a unique basename_template. The template must contain {i}.
import uuid
import pyarrow as pa
import pyarrow.dataset as ds
part = ds.partitioning(pa.schema([("year", pa.int64())]), flavor="hive")
def append(table):
ds.write_dataset(
table,
"sales_log",
format="parquet",
partitioning=part,
basename_template=f"batch-{uuid.uuid4().hex}-{{i}}.parquet",
existing_data_behavior="overwrite_or_ignore",
)
append(pa.table({"year": [2024], "amount": [1.0]}))
append(pa.table({"year": [2024], "amount": [2.0]}))
print(ds.dataset("sales_log", format="parquet", partitioning="hive").count_rows())This prints 2, because the year=2024 directory now holds two files.
Replacing partitions
Use delete_matching to reload data for specific partitions, for example when you reprocess one year.
import pyarrow as pa
import pyarrow.dataset as ds
fixed = pa.table({"year": [2024], "amount": [99.0]})
ds.write_dataset(
fixed,
"sales_log",
format="parquet",
partitioning=["year"],
partitioning_flavor="hive",
existing_data_behavior="delete_matching",
)Only year=2024 is cleared and rewritten. A year=2023 directory would stay as it is.
Gotchas
- Deleting happens as the writer reaches each partition, not in a single step. If the write fails partway, you can be left with deleted or partial data.
- Nothing here is transactional. A reader that scans while you write can see a mix of old and new files.
- With an unpartitioned dataset,
delete_matchingclears the whole destination directory. - Check the partition values in your table before you run
delete_matching. The partitions you write are the ones that get deleted.
Exercise
A nightly job writes new rows for the current day into a dataset partitioned by date. It must never touch earlier days, and a rerun for the same day must replace that day's data instead of duplicating it. Which existing_data_behavior fits, and why?
Show answer
delete_matching. It clears and rewrites only the date partitions present in the table being written, so earlier days are untouched and a rerun replaces the day's data. overwrite_or_ignore with unique file names would add duplicate rows on a rerun.