Merge Operations Examples¶
This page walks through end-to-end merge scenarios using the public merge()
method on both backends. It focuses on strategy selection, composite keys, and
interpreting the MergeResult.
Both backends require the datasets extra. See the
extras matrix. For the shared contract,
see Dataset Handlers.
PyArrow upsert¶
DuckDB update¶
The DuckDB backend takes a connection. The same strategies apply:
Choosing a strategy¶
| Strategy | Inserts new keys? | Rewrites existing files? | Typical use |
|---|---|---|---|
insert |
Yes (new files) | No | Append-only loads, event logs |
update |
No | Yes (only affected files) | Dimension updates, price changes |
upsert |
Yes (new files) | Yes (only affected files) | CDC, synchronization |
All three strategies require key_columns and reject null keys. insert and
upsert work on an empty target; update requires an existing target dataset.
Composite-key merge¶
A composite key identifies a row by several columns. Pass them as a list:
Key matching is vectorized in PyArrow: composite keys are compared as StructArrays, with semi-join and anti-join operations, falling back to string-based key serialization for heterogeneous type combinations. See Multi-Key API.
Interpreting the result¶
Every merge() returns a MergeResult. Audit the operation from its fields
rather than re-reading the dataset:
Key column requirements¶
- Key columns must be present in both source and target.
- Keys cannot contain null values; the merge raises if any are found.
- For a composite key, the full column combination must identify a row
uniquely. Use
dedup_order_by(ondeduplicate_parquet_dataset_pyarrow) to control which duplicate survives during deduplication.
Error handling¶
Common failures and their fixes:
- Key column not found: verify the column names match the source schema exactly.
- Null values in key columns: clean the source so keys are non-null.
updateon a missing target:updaterequires an existing dataset. Useinsertorupsertto seed a new one.
Related documentation¶
- Merge Datasets - strategy reference and backend notes.
- Dataset Handlers - shared interface and result types.
- Multi-Key API - composite-key semantics.