Merge Datasets¶
This guide covers incremental dataset merges with fsspeckit's merge() method.
Both the DuckDB and PyArrow backends share the same merge interface and result
type.
merge() is distinct from write_dataset(): write_dataset() writes files
and does not reconcile existing rows, while merge() reconciles a source
against the existing dataset by rewriting only the files that contain matching
keys. Use write_dataset(mode="overwrite") to clear existing dataset files
first. For the shared contract and backend differences, see
Dataset Handlers.
Both backends require the datasets extra. See the
extras matrix.
Set up a handler¶
For DuckDB, create a connection first and pass it to the handler:
Merge strategies¶
merge() takes a strategy and key_columns. A single string is equivalent
to a one-column list.
insert¶
Appends only rows whose keys do not already exist in the target. Existing files are never rewritten. Use it for append-only loads where duplicates should be ignored.
update¶
Rewrites only the files that contain keys being updated. Rows with keys absent from the target are ignored. Use it for dimension-table maintenance where new records should be rejected.
upsert¶
Rewrites affected files for existing keys and appends inserted keys as new files. Use it for change-data-capture and synchronization where you want both inserts and updates.
Interpreting the result¶
merge() returns a MergeResult with row-level and file-level counts. Use
these fields to audit the operation without re-reading the dataset:
Composite keys¶
Pass a list of column names for a composite key. All strategies support composite keys.
See Multi-Key API for how composite keys are resolved internally.
Partition-aware merges¶
When the source is partitioned, pass partition_columns so the merge prunes
partitions and rewrites only the relevant ones. Files in untouched partitions
appear in preserved_files.
Schema on new files¶
Pass schema to enforce a schema for newly written files during the merge.
This lets you evolve a dataset by adding columns to appended or rewritten
files.
Backend differences¶
The two backends share the same core parameters and result type. They differ in the knobs that are honored:
- DuckDB: uses SQL-based merge logic. The streaming parameters
(
enable_streaming_merge,merge_max_memory_mb, and friends) are accepted for compatibility but ignored. - PyArrow: honors the streaming controls
(
merge_chunk_size_rows,enable_streaming_merge,merge_max_memory_mb,merge_max_process_memory_mb,merge_min_system_available_mb,merge_progress_callback) for memory-bounded merges.
For the full parameter list and the result-type reference, see Dataset Handlers. For memory tuning, see Memory-Constrained Environments.
Related documentation¶
- Dataset Handlers - shared interface and result types.
- Merge Operations Examples - end-to-end scenarios.
- Multi-Key API - composite-key semantics.
- Read and Write Datasets -
write_dataset().