Multi-Key Examples¶
This guide provides practical recipes for composite-key (multi-column) operations: incremental merges and deduplication of an existing dataset. For the composite-key semantics reference, see Multi-Key API.
The PyArrow backend requires the datasets extra. See the
extras matrix.
Composite-key merge¶
Both DuckDBDatasetIO and PyarrowDatasetIO accept a list of column names for
key_columns. Pass a composite key as a list; a single string is equivalent to
a one-column list.
All strategies (insert, update, upsert) support composite keys. The merge
rewrites only the files that contain matching keys.
Deduplicate an existing dataset¶
deduplicate_parquet_dataset_pyarrow() removes duplicate rows from an existing
dataset on disk. Pass key_columns to deduplicate by a composite key, and
dedup_order_by to control which record survives.
- Pass
key_columns=Noneto remove exact duplicate rows across all columns. - Use a leading
-indedup_order_by(for example"-updated_at") to keep the most recent record for each key.
The function returns a dictionary with deduplication statistics and file-level
metrics. Inspect deduplicated_rows to audit the operation. See
Multi-Key API for the result details.
Memory-aware deduplication¶
For datasets larger than memory, control chunking and peak memory with
chunk_size_rows and max_memory_mb:
Deduplicate-then-compact¶
optimize_parquet_dataset_pyarrow() combines deduplication with compaction.
Pass deduplicate_key_columns to deduplicate before compacting:
How composite keys are matched¶
Composite-key matching is vectorized in PyArrow:
- Composite keys are built as StructArrays for efficient comparison.
- Key membership is resolved with semi-join and anti-join operations.
- When native joins fail on heterogeneous type combinations, the engine falls back to string-based key serialization.
These mechanisms are internal; they activate automatically when you pass a list
to key_columns. See Multi-Key API.
Key requirements¶
- Key columns must be present in the data.
- Keys cannot contain null values.
- For a composite key, the full column combination must identify a row uniquely.
- Use
dedup_order_byto make survivor selection deterministic when the source contains duplicate keys.
Related documentation¶
- Multi-Key API - composite-key semantics and deduplication reference.
- Merge Datasets - strategy reference.
- Adaptive Key Tracking - memory-bounded key tracking for streaming merges.