mirror of
https://github.com/lancedb/lancedb.git
synced 2026-12-03 07:45:17 +00:00
resolves #4258 ## Summary - document how indexed partial-column merges patch columns across touched fragments - explain how full-scan merges rewrite rows and can materialize the target table - describe the index-coverage, batching, and memory tradeoffs - align the Python, Rust, and TypeScript `use_index` API documentation ## Verification - `git diff --check` - Ruff 0.15.20 formatting check for `python/python/lancedb/merge.py` - Biome 1.9.4 check for `nodejs/lancedb/merge.ts` - reviewed the rendered MDX structure and cross-SDK wording The TypeDoc generation command was also attempted. It cannot complete in this checkout because the Rust-generated `nodejs/lancedb/native.d.ts` artifact is absent and Rust tooling is not installed; TypeDoc stops on the resulting pre-existing module-resolution errors before generating output. --------- Signed-off-by: Himanshu S <me.mrhimanshu@gmail.com> Co-authored-by: Himanshu <18554133+mrhimanshu@users.noreply.github.com> Co-authored-by: Himanshu S <me.mrhimanshu@gmail.com>
314 lines
12 KiB
Plaintext
314 lines
12 KiB
Plaintext
---
|
|
title: "Merge"
|
|
sidebarTitle: "Merge"
|
|
description: "Match incoming records by key and choose which rows to update, insert or remove."
|
|
icon: code-merge
|
|
availability:
|
|
embedded: available
|
|
enterprise: available
|
|
---
|
|
|
|
import {
|
|
TsMergeMatchedUpdateOnly,
|
|
TsInsertIfNotExists,
|
|
TsMergeUpdateInsert,
|
|
TsMergeDeleteMissingBySource,
|
|
TsMergePartialColumns,
|
|
RsMergeMatchedUpdateOnly,
|
|
RsInsertIfNotExists,
|
|
RsMergeUpdateInsert,
|
|
RsMergeDeleteMissingBySource,
|
|
RsMergePartialColumns,
|
|
} from '/snippets/tables.mdx';
|
|
import {
|
|
PyMergeMatchedUpdateOnly as MergeMatchedUpdateOnly,
|
|
PyInsertIfNotExists as InsertIfNotExists,
|
|
PyMergeUpdateInsert as MergeUpdateInsert,
|
|
PyMergeDeleteMissingBySource as MergeDeleteMissingBySource,
|
|
PyMergePartialColumns as MergePartialColumns,
|
|
} from '/snippets/ingestion_examples.mdx';
|
|
|
|
import { PyWriteWorkflow, TsWriteWorkflowBody, TsWriteWorkflowTail, RsWriteWorkflow } from '/snippets/ingestion_examples.mdx';
|
|
|
|
<Badge color="purple">Early access in Enterprise</Badge>
|
|
|
|
Use merge insert when incoming records should be compared with existing rows.
|
|
Combine update and insert policies for an **upsert**. Use [append](/ingestion/insert)
|
|
when every incoming row should simply be added.
|
|
|
|
## Merge incoming rows by key {#merge-incoming-rows-by-key}
|
|
|
|
Merging is different from updating because it involves comparing incoming rows to existing rows by key, and then choosing what to do based on whether the key exists in the target table or not.
|
|
Use `merge_insert("id")` in Python or the corresponding SDK builder.
|
|
|
|
In merge operations, rows are split into three groups:
|
|
|
|
- **Matched**: key exists in both source and target.
|
|
- **Not matched**: key exists only in source.
|
|
- **Not matched by source**: key exists only in target.
|
|
|
|
Conditional merge clauses can compare old and new values. Use the `target.` prefix for the
|
|
existing table row and `source.` for the incoming row, for example
|
|
`target.last_update < source.last_update`.
|
|
|
|
Choose a key that identifies the same record in the source and target. Check
|
|
for duplicate and null keys before merging. A primary-key declaration does not
|
|
enforce uniqueness on ordinary writes, and append operations do not deduplicate
|
|
records for you.
|
|
|
|
A scalar index on the key can help matching. Keep it current and measure the
|
|
actual workload rather than assuming every merge uses an index. Enterprise may
|
|
reject a merge when the unindexed scan exceeds its configured limit; inspect
|
|
that error and the index state before retrying.
|
|
|
|
## Choose a merge policy {#policies}
|
|
|
|
| Source and target relationship | Available action |
|
|
| --- | --- |
|
|
| Key appears in both | Update matching target rows, optionally with a condition. |
|
|
| Key appears only in the source | Insert it, or leave it out. |
|
|
| Key appears only in the target | Keep it, or explicitly delete it under a synchronization policy. |
|
|
|
|
The following examples are independent. Each starts from the table shown in
|
|
its code, so the expected results do not depend on running the previous example.
|
|
Python blocks include their own temporary connection. For a full TypeScript or
|
|
Rust program, see [Complete write workflow](#complete-example).
|
|
|
|
### Update matched rows only {#update-matched-rows-only}
|
|
|
|
This updates keys that already exist in the target table. Source rows with new keys are ignored.
|
|
|
|
<CodeGroup>
|
|
<CodeBlock filename="Python" language="Python" icon="python">
|
|
{MergeMatchedUpdateOnly}
|
|
</CodeBlock>
|
|
|
|
<CodeBlock filename="TypeScript" language="TypeScript" icon="square-js">
|
|
{TsMergeMatchedUpdateOnly}
|
|
</CodeBlock>
|
|
|
|
<CodeBlock filename="Rust" language="Rust" icon="rust">
|
|
{RsMergeMatchedUpdateOnly}
|
|
</CodeBlock>
|
|
</CodeGroup>
|
|
|
|
Expected table contents:
|
|
|
|
| id | name | login_count |
|
|
| --- | --- | --- |
|
|
| 1 | Alice | 10 |
|
|
| 2 | Bobby | 21 |
|
|
|
|
### Insert unmatched rows only {#insert-unmatched-rows-only}
|
|
|
|
This inserts only brand-new keys from the source. Existing keys are left unchanged.
|
|
|
|
<CodeGroup>
|
|
<CodeBlock filename="Python" language="Python" icon="python">
|
|
{InsertIfNotExists}
|
|
</CodeBlock>
|
|
|
|
<CodeBlock filename="TypeScript" language="TypeScript" icon="square-js">
|
|
{TsInsertIfNotExists}
|
|
</CodeBlock>
|
|
|
|
<CodeBlock filename="Rust" language="Rust" icon="rust">
|
|
{RsInsertIfNotExists}
|
|
</CodeBlock>
|
|
</CodeGroup>
|
|
|
|
Expected table contents:
|
|
|
|
| id | name | login_count |
|
|
| --- | --- | --- |
|
|
| 1 | Alice | 10 |
|
|
| 2 | Bob | 20 |
|
|
| 3 | Charlie | 5 |
|
|
|
|
### Update matched rows and insert unmatched rows {#update-matched-rows-and-insert-unmatched-rows}
|
|
|
|
Use both `when_matched_update_all()` and `when_not_matched_insert_all()` when you want to update existing keys and insert missing keys in one operation.
|
|
|
|
<Info>
|
|
This is a conventional **upsert**.
|
|
</Info>
|
|
|
|
<CodeGroup>
|
|
<CodeBlock filename="Python" language="Python" icon="python">
|
|
{MergeUpdateInsert}
|
|
</CodeBlock>
|
|
|
|
<CodeBlock filename="TypeScript" language="TypeScript" icon="square-js">
|
|
{TsMergeUpdateInsert}
|
|
</CodeBlock>
|
|
|
|
<CodeBlock filename="Rust" language="Rust" icon="rust">
|
|
{RsMergeUpdateInsert}
|
|
</CodeBlock>
|
|
</CodeGroup>
|
|
|
|
Expected table contents:
|
|
|
|
| id | name | login_count |
|
|
| --- | --- | --- |
|
|
| 1 | Alice | 10 |
|
|
| 2 | Bobby | 21 |
|
|
| 3 | Charlie | 5 |
|
|
|
|
### Delete target rows that are missing from source {#delete-target-rows-that-are-missing-from-source}
|
|
|
|
Use `when_not_matched_by_source_delete()` only when the incoming data is the
|
|
complete set for the target scope you intend to synchronize. An incremental
|
|
batch is usually not a complete source: deleting everything absent from it
|
|
could remove unrelated target records. Inspect the unmatched target keys first.
|
|
|
|
<CodeGroup>
|
|
<CodeBlock filename="Python" language="Python" icon="python">
|
|
{MergeDeleteMissingBySource}
|
|
</CodeBlock>
|
|
|
|
<CodeBlock filename="TypeScript" language="TypeScript" icon="square-js">
|
|
{TsMergeDeleteMissingBySource}
|
|
</CodeBlock>
|
|
|
|
<CodeBlock filename="Rust" language="Rust" icon="rust">
|
|
{RsMergeDeleteMissingBySource}
|
|
</CodeBlock>
|
|
</CodeGroup>
|
|
|
|
Expected table contents:
|
|
|
|
| id | name | login_count |
|
|
| --- | --- | --- |
|
|
| 2 | Bobby | 21 |
|
|
| 3 | Charlie | 5 |
|
|
|
|
Here, IDs 2 and 3 are updated and ID 1 is deleted. This policy synchronizes
|
|
the target to the supplied source set.
|
|
|
|
### Use partial columns in merge updates {#use-partial-columns-in-merge-updates}
|
|
|
|
Merge updates do not require you to provide values for all columns. You can provide only a subset of columns in source rows. For matched rows, only the provided columns are updated.
|
|
|
|
<CodeGroup>
|
|
<CodeBlock filename="Python" language="Python" icon="python">
|
|
{MergePartialColumns}
|
|
</CodeBlock>
|
|
|
|
<CodeBlock filename="TypeScript" language="TypeScript" icon="square-js">
|
|
{TsMergePartialColumns}
|
|
</CodeBlock>
|
|
|
|
<CodeBlock filename="Rust" language="Rust" icon="rust">
|
|
{RsMergePartialColumns}
|
|
</CodeBlock>
|
|
</CodeGroup>
|
|
|
|
Expected table contents:
|
|
|
|
| id | name | login_count |
|
|
| --- | --- | --- |
|
|
| 1 | Alice | 10 |
|
|
| 2 | Bobby | 20 |
|
|
| 3 | Charlie | null |
|
|
|
|
Note that in the example above, when `merge_insert` creates a new row, any missing columns are written as `null`. If a missing column is non-nullable in your schema, the insert will fail.
|
|
|
|
## How merge insert executes {#how-merge-insert-executes}
|
|
|
|
`merge_insert` first joins the incoming rows to the target table on the columns passed to
|
|
`merge_insert(...)`. Its physical write strategy depends on whether that join uses a scalar index:
|
|
|
|
- With `use_index=True` (the default), LanceDB can use scalar indexes on the join keys when the
|
|
operation supports the indexed path and all join keys have usable indexes. When a partial-column
|
|
update actually takes this path, it patches the supplied non-key columns of the matched rows in
|
|
their existing fragments. Every updated column is read and rewritten for every row in each
|
|
fragment that contains a match, so a small but scattered update can still touch much of the table.
|
|
- With `use_index=False`, LanceDB performs a full-scan join and rewrites matched rows by deleting
|
|
them and appending complete replacement rows. This can also happen with `use_index=True`, for
|
|
example when a join key lacks a usable index or a partial-column update includes a blob column.
|
|
|
|
Within the fragments touched by a merge, column patching is generally favorable when the fraction
|
|
of rows matched is greater than the fraction of each row represented by the updated columns. For
|
|
sparse updates spread across many fragments, grouping work by destination fragment can reduce how
|
|
much data is rewritten more than simply reducing the number of rows in each batch.
|
|
|
|
### Index coverage after a merge {#index-coverage-after-a-merge}
|
|
|
|
How a merge affects existing indexes depends on how each row is written:
|
|
|
|
- **Matched rows patched by an indexed partial update.** The join keys are not rewritten, so their
|
|
existing index coverage on the patched fragments is preserved. The supplied non-key columns are
|
|
rewritten, so indexes on those columns, including vector indexes, lose coverage for the patched
|
|
fragments until the indexes are optimized.
|
|
- **Newly written rows.** Inserted rows and matched rows that are replaced by deleting and appending
|
|
them are written to new fragments, which existing indexes do not cover until the indexes are
|
|
optimized. A full-row replacement can write new rows even when the lookup used an index.
|
|
|
|
<Warning>
|
|
Do not use `use_index=False` as a general-purpose row-rewrite switch for large tables. The full-scan
|
|
plan can materialize the entire target table on the hash-join build side, so memory use grows with
|
|
the target table rather than with the incoming batch and can exhaust the server. This warning also
|
|
applies to full-scan fallbacks with `use_index=True`: the default flag alone is not a memory
|
|
safeguard. Confirm that the merge can use the indexed path and profile representative data under
|
|
your memory limits.
|
|
</Warning>
|
|
|
|
## Check the merged records {#verify}
|
|
|
|
Inspect the keys that should have changed, confirm that unrelated rows remain,
|
|
and check inserted or deleted counts when the SDK returns them. Repeating a
|
|
merge is only safe when its keys, conditions and update values produce the
|
|
intended result on retry.
|
|
|
|
To override the transport retry budget for one merge, pass `retries=0` to
|
|
Python's synchronous or asynchronous `builder.execute(data, retries=0)`, or
|
|
use TypeScript's `builder.execute(data, { retries: 0 })`. The override applies
|
|
only to that execution. A larger budget does not make an ambiguous 5xx result
|
|
safe to replay. See [write retry boundaries](/ingestion/insert#performance)
|
|
for the distinction between retryable failures and an unknown commit outcome.
|
|
|
|
### Embeddings and expression predicates {#advanced}
|
|
|
|
When a table uses an [embedding definition](/embedding), behavior depends on
|
|
which source and vector fields the incoming records provide. Do not assume a
|
|
partial metadata update recomputes embeddings. Inspect the resulting vector
|
|
when changing the input used to generate it.
|
|
|
|
Rust's `when_matched_update_all_expr` and
|
|
`when_not_matched_by_source_delete_expr` accept DataFusion expressions for
|
|
LanceDB Embedded tables. Use the string predicate variants for Enterprise tables.
|
|
|
|
## Complete write workflow {#complete-example}
|
|
|
|
<Accordion title="Complete examples: create, append, update, upsert and delete">
|
|
|
|
Each program creates a temporary local database, performs the five operations,
|
|
prints the result, and removes its temporary data when finished.
|
|
|
|
- **Python:** install `lancedb`, save the code as `ingestion.py`, and run
|
|
`python ingestion.py`.
|
|
- **TypeScript:** install `@lancedb/lancedb`, save the code as `ingestion.mts`,
|
|
and run `node ingestion.mts` with Node.js 22.18 or later.
|
|
- **Rust:** download the [complete Rust project](https://raw.githubusercontent.com/lancedb/lancedb/2af85bea8999c8cd23ec01ff62ba110c54652c10/docs/web/static/code/ingestion-rust.zip),
|
|
extract it, and run `cargo run --locked`. It includes `src/main.rs`, the Cargo
|
|
manifest and a lockfile for the example's dependencies. The code is shown below.
|
|
|
|
<CodeGroup>
|
|
<CodeBlock filename="ingestion.py" language="python" icon="python">
|
|
{PyWriteWorkflow}
|
|
</CodeBlock>
|
|
<CodeBlock filename="ingestion.mts" language="typescript" icon="square-js">
|
|
{TsWriteWorkflowBody}
|
|
{TsWriteWorkflowTail}
|
|
</CodeBlock>
|
|
<CodeBlock filename="src/main.rs" language="rust" icon="rust">
|
|
{RsWriteWorkflow}
|
|
</CodeBlock>
|
|
</CodeGroup>
|
|
|
|
After the operations, Alice has 10 logins, Bobby has 21, and Charlie has 6.
|
|
Dana is selected for deletion and is absent from the final result.
|
|
|
|
</Accordion>
|