Skip to content

[core] Remove partial-update row when a key ends with a lone UPDATE_BEFORE - #11178

Open
dragonknightk wants to merge 4 commits into
apache:masterfrom
dragonknightk:fix-partial-update-lone-update-before
Open

dragonknightk wants to merge 4 commits into
apache:masterfrom
dragonknightk:fix-partial-update-lone-update-before

Conversation

@dragonknightk

@dragonknightk dragonknightk commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Purpose

Fixes #6862

With partial-update.remove-record-on-delete, a read now drops the row when the last message of a key is a lone UPDATE_BEFORE (-U). For sequences without DELETE, the result does not depend on how the records are split across write buffers. This is a behavior contract change.

The trigger is a common Flink pattern: INSERT INTO t SELECT * FROM src WHERE delete = 0. When a row flips to delete = 1, the filter passes the -U and drops the +U. On master, the merge function ignores -U under this option, so the stale row survives.

A -U is handled as a retraction, not as a DELETE. It marks the row removed and does not re-initialize it, so the columns of other writers stay for a later +I or +U:

+I(1, 10, null), +I(1, null, 20), -U(1, 10, null) -> master: +I(1, 10, 20) stale row; new: row removed
then +U(1, 11, null)                               -> master and new: +I(1, 11, 20)
  • A merge result removed only by -U stays UPDATE_BEFORE, and a read drops it like DELETE. The write buffer and minor compactions keep retractions, so a DELETE result there would re-initialize the row in a later merge and drop the columns of other writers in older files.
  • Without a sequence group, a later +I or +U in the same merge clears the DELETE state, so a trailing -U produces UPDATE_BEFORE instead of DELETE. The INSERT result of a write buffer does not carry a prior DELETE either, so over an older file this makes -D, +I, -U behave the same whether flushed after +I or not, and keeps the columns of other writers.
  • A -U also writes its non-null fields into the row, as an INSERT does. A merge result carries the values merged before the -U, so a later merge must apply them to rebuild the same row. Without this, +I(1, 10, null) and -U(1, 10, null) flushed before a +I(1, null, 30) of another writer read (1, NULL, 30) instead of (1, 10, 30). The cost is that a retracted value comes back when another writer restores the row, as with a DELETE on master: after +I(1, 10, null) and then -D(1, 10, null) or -U(1, 10, null), a +I(1, null, 30) reads (1, 10, 30).
  • A removed row is dropped like a DELETE by a compaction whose output level is above 0 and at least the highest non-empty level, or by any compaction with an output level above 0 when deletion vectors are enabled. A later write then starts from an empty row, so with the lookup or full-compaction changelog producer or deletion vectors, the columns of other writers are usually kept only within one commit.
  • Out of scope: partial-update.remove-record-on-sequence-group keeps its current behavior, where a -U of a configured group only retracts the group fields, so [Bug] Partial-update merge-function keeps stale row when the last message is a lone UPDATE_BEFORE (-U) #6862 stays open there. aggregation.remove-record-on-delete is also unchanged, since its docs define -U as a per-field retraction. Pre-existing split dependence when a DELETE is followed by +I or +U in the same write buffer (where the resulting INSERT cannot carry the prior DELETE reset to older files) is also unchanged, since fixing it would require a format or protocol change beyond the current RowKind states. These need their own discussion.

For sequence groups, a row removal has to reach a later merge as one record, and neither result kind keeps an aggregated field right. Measured with b summed in another group, 5 committed, then +I(b = 11) with a -U and +U of the configured group in one commit, the write buffer flushed after the -U:

expected:                   b = 16
-U result as UPDATE_BEFORE: b = -6, a later merge retracts the merged 11
-U result as DELETE:        b = NULL

The root cause was confirmed in the review of #6844, which was closed without merging after a rebase request.

Tests

  • In PartialUpdateMergeFunctionTest, a lone -U gives UPDATE_BEFORE with the merged row, and a following +U restores the row on top of it. A key with only -U records also gives UPDATE_BEFORE, and a DELETE before a -U still gives DELETE.
  • testRemoveRecordOnDeleteWithUpdateBeforeAfterDeleteAndInsert proves that a +I after a DELETE clears the DELETE state in one merge: -D, +I, -U gives INSERT after the +I and UPDATE_BEFORE with the merged row after the -U.
  • testPartialUpdateRemoveRecordOnDeleteWithUpdateBeforeAcrossFlushes runs 7 flush layouts at table level with write-only. +I, -U and +U of one writer over a column committed by another writer read the same row whether the records share one write buffer or are flushed after any record. A key that ends with -U reads no row, and a -U followed by the other writer's +I reads the same row in every layout. On master, the keys that end with -U read (1, 4, 10, 20) and (1, 5, 10, 20).
  • testPartialUpdateRemoveRecordOnDeleteMixedDeleteAndUpdateBeforeAcrossFlushes proves that, over a committed c = 20 from another writer, -D, +I, -U in one write buffer followed by +U reads (1, 1, 11, 20), matching a key flushed after +I and after -U.
  • A read after a committed lone -U returns no row at table level, and in a chain table a read drops the row in the partition of a -U whose +U lands in another partition.

AI Disclosure

Human-led contribution. Grok Bot, working for dragonknightk, assisted with this change, including the code, tests, and description. All submitted content was reviewed by a human before submission.

…EFORE

### Purpose

Closes issue 6862

With `partial-update.remove-record-on-delete`, the merge result now becomes DELETE when the last message of a key is a lone UPDATE_BEFORE (-U). This is a behavior contract change.

On -U the merge function keeps the merged row, while DELETE re-initializes the row from its own value. A -U only carries one writer's view, so re-initializing from it would let a following +U drop columns written by other writers.

The trigger is a common Flink pattern: `INSERT INTO t SELECT * FROM src WHERE delete = 0`. When a row flips to `delete = 1`, the filter passes the -U and drops the +U. Before this change, the merge function only reacted to DELETE, so the -U was ignored and the stale row survived.

Old and new results within one merge for `+I(1, 10, null)` and `+I(1, null, 20)` from two writers, then `-U(1, 10, null)`:

```
old: -U ignored           -> +I(1, 10, 20)  stale row
new: -U                   -> -D(1, 10, 20)  merged row kept in the DELETE
new: then +U(1, 11, null) -> +I(1, 11, 20)  f2 from the other writer kept
```

- With `partial-update.remove-record-on-sequence-group`, the merge result also becomes DELETE after an accepted -U of a configured group. The group fields are retracted as before instead of re-initialized from the -U.
- A later accepted +I or +U restores the row, under the same rules as after a DELETE.
- When -U and +U carry different partition values, they reach different keys, so the merge result in the old partition becomes DELETE.
- `aggregation.remove-record-on-delete` is unchanged. Its docs define -U as a per-field retraction, so changing it is outside issue 6862 and needs its own discussion.

The root cause was confirmed in the review of PR 6844, which was closed without merging after a rebase request.

### Tests

- Without sequence groups, the merge result becomes DELETE after a lone -U and carries the merged row. A following +U in the same merge restores the row on top of the merged row.
- With a configured sequence group, the merge result becomes DELETE after an accepted lone -U. The row kind stays INSERT after an older -U of that group or a -U of another group. An accepted +U restores the row.
- At table level, a read after a committed lone -U returns no row for both options.
- In a chain table, the merge result becomes DELETE in the partition of a -U whose +U lands in another partition.
- With `remove-record-on-delete`, a key that receives only one -U and no prior INSERT merges to DELETE. This already holds on the code before this change, and a test locks it in. Every other new test and each test with a changed expectation fails on the code before this change.

## AI Disclosure

Human-led contribution. Grok Bot, working for dragonknightk, assisted with this change, including the code, tests, and description. All submitted content was reviewed by a human before submission.

Signed-off-by: KK <37928252+dragonknightk@users.noreply.github.com>
@dragonknightk

Copy link
Copy Markdown
Contributor Author

Hi @JingsongLi, #6844 was closed without merging after your rebase request, so I picked up this fix. It follows the points from your review there:

  • The root cause is that the merge function only reacted to DELETE, so a trailing -U was ignored.
  • Only the last message of a key decides the result: a lone -U at the end gives DELETE, and a later +I or +U restores the row.
  • With partial-update.remove-record-on-sequence-group, an accepted -U of a configured group also gives DELETE, while the group fields are retracted as before.
  • A key that receives only one -U and no prior INSERT gives DELETE, and a test now locks this in.

Could you take a look when you have time? Thanks (´。• ᵕ •。`)

initRow(row, kv.value());
}
return;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One subtlety with setting currentDeleteRow = true on RowKind.UPDATE_BEFORE (both here and at lines 388–390 in retractWithSequenceGroup):

In add(), -U intentionally preserves existing columns in row so a later +U keeps columns written by other streams, whereas RowKind.DELETE (-D) re-initializes row via initRow(row, kv.value()) (line 211 / line 381). However, getResult() (line 439) always collapses currentDeleteRow == true into RowKind.DELETE:

RowKind rowKind = currentDeleteRow || !meetInsert ? RowKind.DELETE : RowKind.INSERT;

Because PartialUpdateMergeFunction is also used to pre-merge records inside SortBufferWriteBuffer (whenever 2+ records for the same key appear in the same write buffer) and during minor compactions (dropDelete = false), any pre-merge ending with -U is flushed to the L0/compacted file as RowKind.DELETE instead of RowKind.UPDATE_BEFORE.

For example:

  1. Commit 0 (SST 1): Stream 2 writes +I(1, null, 20).
  2. Commit 1 (SST 2): Stream 1 writes +I(1, 10, null) followed by -U(1, 10, null) in the same write buffer before its +U arrives in Commit 2. SortBufferWriteBuffer merges the two records via PartialUpdateMergeFunction and flushes -D(1, 10, null) (RowKind.DELETE) to SST 2.
  3. Commit 2 (SST 3): Stream 1 writes +U(1, 11, null).
  4. When SST 1, SST 2, and SST 3 are merged on read/compaction, SST 2 now has kv.valueKind() == RowKind.DELETE, which triggers initRow(row, kv.value()) and wipes out f2 = 20 from SST 1 (producing +I(1, 11, null) instead of +I(1, 11, 20)).

By contrast, when -U is the sole record for that key in Commit 1's write buffer, ReducerMergeFunctionWrapper bypasses PartialUpdateMergeFunction and writes RowKind.UPDATE_BEFORE to SST 2, which preserves f2 = 20. Should we preserve RowKind.UPDATE_BEFORE (or avoid wiping row when the delete state came from -U) across merge boundaries?

commit.commit(1, write.prepareCommit(true, 1));
result = getResult(read, toSplits(snapshotReader.read().dataSplits()), rowToString);
assertThat(result).containsExactlyInAnyOrder("+I[1, 1, NULL, 2, NULL, NULL, 2]");
assertThat(result).isEmpty();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In testPartialUpdateRemoveRecordOnSequenceGroup, partial-update.remove-record-on-sequence-group is only configured for seq2, while Step 2's UPDATE_BEFORE(1, 1, 11, 2, 29, 29, 2) and Step 3's UPDATE_AFTER(1, 1, 11, 2, 30, 30, 3) pass non-null values for both seq1 (b = 11, seq1 = 2) and seq2.

Consider:

  1. Passing null, null for b, seq1 in Step 2 and Step 3 (e.g. UPDATE_BEFORE(1, 1, null, null, 29, 29, 2) and UPDATE_AFTER(1, 1, null, null, 30, 30, 3)) so this table-level test verifies that b = 11, seq1 = 2 from Step 1 is preserved across seq2's -U and +U commits rather than re-supplied by Step 3.
  2. Optionally keeping a step where -U only retracts seq1 (UPDATE_BEFORE(1, 1, 11, 2, null, null, null)) to verify at the table level that -U on a non-configured sequence group only clears b to NULL without deleting the whole row.


// A key that receives only one -U, with no INSERT before it, should still produce a
// DELETE. The remove-record-on-delete docs say UPDATE_BEFORE removes the whole row.
add(function, RowKind.UPDATE_BEFORE, 1, 10);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: when add(function, RowKind.UPDATE_BEFORE, 1, 10) is called with no prior INSERT/UPDATE_AFTER, meetInsert remains false, so getResult() (currentDeleteRow || !meetInsert ? RowKind.DELETE : RowKind.INSERT) already returned RowKind.DELETE before this change. Keeping the test is fine, though the comment might clarify that !meetInsert also covers a lone -U with no prior insert.

@JingsongLi JingsongLi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requirement fit: SUPPORTED. Implementation: FINDINGS. The filtered-retraction and partition-move use cases have end-to-end value. The 75 selected existing/new Java tests pass with normal Maven checks, but additional persisted-table probes expose two correctness gaps.

I reproduced the data-preservation issue already reported in #11178 (comment), so I am adding evidence here instead of duplicating that inline finding: with write-only=true, first commit the other writer's b=20, then write +I(a=10,b=NULL), -U(a=10,b=NULL), and +U(a=11,b=NULL). Keeping these three records in one buffer preserves b=20. Flushing the buffer after -U using the public write.compact(..., false) call, with no compaction and with -U/+U still in the same committed checkpoint, returns b=NULL. The raw single--U buffer control preserves b=20. Thus the result depends on a write-buffer boundary even without an intervening published deletion or tombstone pruning. The clean merge-base preserves b=20 in all controls. Please preserve the distinction between a retraction and a real DELETE through pre-merging, and add a persisted-file regression.

There is also an independent supported-Native-reader incompatibility below. CI is green, but these additional paths are not covered by it.


if (removeRecordOnDelete) {
// -U also removes the row, a later +I or +U restores it
currentDeleteRow = true;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] Gate Native readers until their retraction semantics match

This changes the interpretation of files that the currently supported Native backend still reads with the old semantics. I wrote a real Parquet partial-update table with remove-record-on-delete=true and write-only=true, committing +I(1,10,20) and then -U(1,10,NULL) separately. Reading those exact same files with this Java head returns no row; pypaimon-rust 0.5.0 returns the stale (1,10,20) row, with Python fallback explicitly forbidden. The clean Java merge-base also returns that row, confirming the divergence is introduced here. pyproject.toml still accepts Rust >=0.5.0, Native reads default to enabled, and the bridge has no capability gate for this option. Consequently Java/Flink and supported PyPaimon Native readers disagree on whether a committed key exists. Align Rust and require its compatible version/capability, or explicitly reject affected Native configurations before enabling this contract change. Pure Python already rejects this option, so silently falling back is insufficient.

…ve-record options

Signed-off-by: KK <37928252+dragonknightk@users.noreply.github.com>

@JingsongLi JingsongLi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revalidated b09eae7 after the Java semantic revision. Requirement fit: SUPPORTED. Implementation: FINDINGS (the existing Native P1 remains).

The accepted UPDATE_BEFORE now deliberately follows the full DELETE reset path under the whole-row removal options, and the documentation explicitly describes loss of other-writer columns/batching dependence. I reviewed that revised contract rather than repeating the earlier column-preservation request unchanged; no additional Java sequence-group regression was established.

75 merge-function/real-table cases and the public chain-table partial-update deletion case passed with normal JDK11 Maven checks. However, the existing compatibility finding is still reproducible on this exact head: reading the same persisted Parquet +I then -U files, Java b09eae7 returns zero rows while supported pypaimon-rust 0.5.0 returns the stale (1,10,20) row with Python fallback forbidden. File hashes are unchanged and the Java code-source points to the newly compiled b09eae7 worktree. Python/Native capability routing is unchanged by this revision. Please address #11178 (comment) before production use; I am not duplicating that inline finding. Current exact-head CI is queued.

…riters' columns

Signed-off-by: dragonknightk <37928252+dragonknightk@users.noreply.github.com>
@dragonknightk

Copy link
Copy Markdown
Contributor Author

@JingsongLi @Akash3121 Thanks to both of you for the careful reviews. You found cases I had not thought through, especially how a pre-merged -U behaves once it is flushed to a file.

The goal. In Flink a -U means "this row leaves the result". When a filter drops the matching +U, as with WHERE delete = 0, the -U is effectively a delete. partial-update.remove-record-on-delete is the option that makes deletes take effect, and a -D already removes the row under it, but a lone -U is ignored, so the stale row stays. This PR makes a lone -U take effect under this option.

How it works, from the basics.

  • A key's records are merged in segments: the write buffer pre-merges before a flush, and minor compactions merge again. Each segment's result is written as one record and merged again later, so where the write buffer is flushed should not change the row.
  • Under partial-update, a row is assembled from the columns of several writers, so a -U must remove the row from reads without wiping the columns that others wrote.
  • A -D result cannot carry both facts, because a later merge re-initializes the row when it sees a -D. That is the buffer-boundary data loss you both reproduced, and it is why the previous revision was wrong.
  • So a -U is now handled as a retraction. It marks the row removed, keeps the merged fields, and stays UPDATE_BEFORE in the merge result, so a read drops it like a DELETE and a later merge still sees a retraction. A -U also applies its non-null fields, so the rebuilt row is the same however the records are split.

What this gives.

  • A key whose last message is a lone -U reads no row, which fixes issue 6862 for this option. When the -U and +U carry different partition values, the row is dropped from the old partition.
  • A later +I or +U restores the row together with the columns of other writers.
  • The result no longer depends on write-buffer boundaries. Your probe is now a table-level test with write-only: another writer commits b = 20, then +I(a = 10), -U(a = 10) and +U(a = 11) go into one commit. In one buffer, flushed after the -U, or flushed after each record, all three read a = 11, b = 20.
  • The known costs are the same as for -D on master and are written in the docs: a retracted value comes back when another writer restores the row, and a compaction that drops the removed row also drops the columns of other writers. This affects -D on master in the same way, and this PR leaves it unchanged.
  • partial-update.remove-record-on-sequence-group is back to the behavior on master. With an aggregated field, neither result kind keeps the sum right after a flush (expected 16, UPDATE_BEFORE gives -6, DELETE gives NULL), so I would like to discuss it separately.

Native readers. Since this changes how a lone -U in committed files is read, Java and paimon-rust have to agree, or the same table reads differently. My suggestion is to align paimon-rust with the same rule, so that a lone -U removes the row under this option and both sides read the same result. @JingsongLi, does that direction work for you?

Thanks again ʕ•ᴥ•ʔ

@JingsongLi

Copy link
Copy Markdown
Contributor

Revalidated 7955643468 after the retraction/flush-boundary revision. Requirement fit: SUPPORTED. Implementation: FINDINGS (the existing Native P1 remains).

The revised Java contract preserves other writers' columns across the tested buffer boundaries, and sequence-group removal is again outside this change. All 76 selected Java merge-function, real-table and chain-table tests passed on JDK 11 with normal Maven checks; no additional Java regression was established.

The cross-reader disagreement is still reproducible on this exact head. A real fixed-bucket Parquet partial-update table with partial-update.remove-record-on-delete=true contains a committed +I(1,10,20) followed by a lone -U(1,10,NULL). Java classes compiled from this worktree return zero rows, while supported pypaimon-rust 0.5.0 returns the stale (1,10,20) row with Python fallback forbidden. Both readers used the identical persisted files, whose SHA-256 hashes stayed unchanged. The current dependency still permits pypaimon-rust>=0.5.0, and the Native routing has no semantic capability/version guard.

Aligning Rust is necessary; please also require the compatible runtime or explicitly reject the incompatible path before shipping this Java behavior. The Python fallback currently rejects these removal options, so an assumed automatic fallback is insufficient. This is the same existing P1, rather than a duplicate inline finding. The exact-head CI is still queued.

@dragonknightk

Copy link
Copy Markdown
Contributor Author

@JingsongLi Thanks for revalidating. For the Native P1, I opened the paimon-rust side first: apache/paimon-rust PR 1434, tracked by apache/paimon-rust issue 1430. It applies the same -U rules as this PR, so under partial-update.remove-record-on-delete a lone -U reads no row in Rust as well, and the write pre-merge and compaction keep it as UPDATE_BEFORE.

For the runtime, I would like to take the "require the compatible runtime" option. Once the Rust fix is merged and released, I will raise the pypaimon-rust lower bound in this PR to that release, so this Java behavior never ships with an older Native reader. Until then this PR waits on the Rust side.

Does this plan work for you? (。•̀ᴗ-)✧

@JingsongLi

Copy link
Copy Markdown
Contributor

Rechecked 7955643468. Requiring a compatible Native runtime is a viable direction, but the existing Native P1 remains until that requirement is actually enforced. apache/paimon-rust#1434 is still open, and this PR still permits pypaimon-rust>=0.5.0.

I reran 79 selected Java merge/real-table/chain-table cases with normal JDK 8 Maven checks; all passed. The same persisted Parquet files still read as zero rows using Java classes from this worktree, versus the stale (1,10,20) row using Native 0.5.0 with Python fallback forbidden. File SHA-256 hashes were unchanged across the reads.

Please keep the Java change waiting until the Rust fix is merged and released and the minimum supported runtime (or an equivalent capability guard) is updated here. Then verify the lone -U and split-buffer -U/+U cases across Native write, read and compaction against Java. No additional Java defect was established in this pass.

@dragonknightk

Copy link
Copy Markdown
Contributor Author

@JingsongLi Thanks for the detailed repro. The Rust side is fixed in apache/paimon-rust#1434, which is merged and will ship in pypaimon-rust 0.6.0.

I tried the explicit rejection route locally. Before 0.6.0, the Native reader, the Native writer and a table source of Native MERGE INTO can all raise a clear error for these tables. SQL is the gap: PyPaimon passes the whole statement to pypaimon-rust, which parses it and resolves the tables itself, so PyPaimon cannot tell which tables a query touches. Before 0.6.0, SQL queries would still return the stale row, and SQL writes would persist it.

So I'd prefer to wait for pypaimon-rust 0.6.0, raise the minimum version here, and merge this PR then. That covers every path at once and avoids a guard we would remove later. (๑•̀ㅂ•́)و✧

@JingsongLi JingsongLi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a new Java P1 even after the Native fixes are released. I also rechecked the existing Native P1 using direct Native SQL against the same two Parquet files. Java returned zero rows and the currently published 0.5.0 returned the stale row. Rust #1434 has merged, and this Java head still allows 0.5.0. Waiting for the fixed release and raising the floor is still needed.

RowKind rowKind = currentDeleteRow || !meetInsert ? RowKind.DELETE : RowKind.INSERT;
RowKind rowKind;
if (currentDeleteRow) {
rowKind = meetDelete ? RowKind.DELETE : RowKind.UPDATE_BEFORE;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] The same valid input can lose another writer's column depending on an extra memtable flush. I reproduced this on a real fixed-bucket partial-update table with write-only=true, partial-update.remove-record-on-delete=true, and no sequence groups. Commit +I(1,NULL,20), then in the next commit write -D(1,5,NULL), +I(1,10,NULL), -U(1,10,NULL), flush, and write +U(1,11,NULL). This branch reads (1,11,NULL). Adding one extra flush immediately after +I(1,10,NULL) reads (1,11,20). Both layouts read (1,11,20) with the base implementation. meetDelete persists within a buffer, but an INSERT result does not preserve it across one; the final -U therefore emits DELETE in one layout and UPDATE_BEFORE in the other. Please add a real-table regression for this mixed DELETE/UPDATE_BEFORE sequence and make the result invariant across flush boundaries.

…EFORE

### Purpose

Closes issue 6862

With `partial-update.remove-record-on-delete`, a read now drops the row when the last message of a key is a lone UPDATE_BEFORE (-U), and the result does not depend on how the records are split across write buffers. This is a behavior contract change.

The trigger is a common Flink pattern: `INSERT INTO t SELECT * FROM src WHERE delete = 0`. When a row flips to `delete = 1`, the filter passes the -U and drops the +U. On master, the merge function ignores -U under this option, so the stale row survives.

A -U is handled as a retraction, not as a DELETE. It marks the row removed and does not re-initialize it, so the columns of other writers stay for a later +I or +U:

```
+I(1, 10, null), +I(1, null, 20), -U(1, 10, null) -> master: +I(1, 10, 20) stale row; new: row removed
then +U(1, 11, null)                               -> master and new: +I(1, 11, 20)
```

- A merge result removed only by -U stays UPDATE_BEFORE, and a read drops it like DELETE. The write buffer and minor compactions keep retractions, so a DELETE result there would re-initialize the row in a later merge and drop the columns of other writers in older files.
- Without a sequence group, a later +I or +U in the same merge clears the DELETE state, so the result is UPDATE_BEFORE instead of DELETE. The INSERT result of a write buffer does not keep a DELETE either, so this makes the result of `-D, +I, -U` the same for every flush layout and keeps the columns of other writers.
- A -U also writes its non-null fields into the row, as an INSERT does. A merge result carries the values merged before the -U, so a later merge must apply them to rebuild the same row. Without this, `+I(1, 10, null)` and `-U(1, 10, null)` flushed before a `+I(1, null, 30)` of another writer read `(1, NULL, 30)` instead of `(1, 10, 30)`. The cost is that a retracted value comes back when another writer restores the row, as with a DELETE on master: after `+I(1, 10, null)` and then `-D(1, 10, null)` or `-U(1, 10, null)`, a `+I(1, null, 30)` reads `(1, 10, 30)`.
- A removed row is dropped like a DELETE by a compaction whose output level is above 0 and at least the highest non-empty level, or by any compaction with an output level above 0 when deletion vectors are enabled. A later write then starts from an empty row, so with the `lookup` or `full-compaction` changelog producer or deletion vectors, the columns of other writers are usually kept only within one commit.
- Out of scope: `partial-update.remove-record-on-sequence-group` keeps its current behavior, where a -U of a configured group only retracts the group fields, so issue 6862 stays open there. `aggregation.remove-record-on-delete` is also unchanged, since its docs define -U as a per-field retraction. Both need their own discussion.

For sequence groups, a row removal has to reach a later merge as one record, and neither result kind keeps an aggregated field right. Measured with `b` summed in another group, 5 committed, then `+I(b = 11)` with a -U and +U of the configured group in one commit, the write buffer flushed after the -U:

```
expected:                   b = 16
-U result as UPDATE_BEFORE: b = -6, a later merge retracts the merged 11
-U result as DELETE:        b = NULL
```

The root cause was confirmed in the review of PR 6844, which was closed without merging after a rebase request.

### Tests

- In `PartialUpdateMergeFunctionTest`, a lone -U gives UPDATE_BEFORE with the merged row, and a following +U restores the row on top of it. A key with only -U records also gives UPDATE_BEFORE, and a DELETE before a -U still gives DELETE.
- `testRemoveRecordOnDeleteWithUpdateBeforeAfterDeleteAndInsert` proves that a +I after a DELETE clears the DELETE state in one merge: `-D, +I, -U` gives INSERT after the +I and UPDATE_BEFORE with the merged row after the -U.
- `testPartialUpdateRemoveRecordOnDeleteWithUpdateBeforeAcrossFlushes` runs 7 flush layouts at table level with `write-only`. +I, -U and +U of one writer over a column committed by another writer read the same row whether the records share one write buffer or are flushed after any record. A key that ends with -U reads no row, and a -U followed by the other writer's +I reads the same row in every layout. On master, the keys that end with -U read `(1, 4, 10, 20)` and `(1, 5, 10, 20)`.
- `testPartialUpdateRemoveRecordOnDeleteMixedDeleteAndUpdateBeforeAcrossFlushes` proves that `-D, +I, -U, +U` of one key reads `(1, 1, 11, 20)` in one write buffer, and the same row as a key flushed after the +I and after the -U, so the other writer's column survives in every layout.
- A read after a committed lone -U returns no row at table level, and in a chain table a read drops the row in the partition of a -U whose +U lands in another partition.

## AI Disclosure

Human-led contribution. Grok Bot, working for dragonknightk, assisted with this change, including the code, tests, and description. All submitted content was reviewed by a human before submission.

Signed-off-by: dragonknightk <37928252+dragonknightk@users.noreply.github.com>
@dragonknightk

Copy link
Copy Markdown
Contributor Author

@JingsongLi Thanks! Fixed: an insert after a DELETE now clears the DELETE state, so both flush layouts of your sequence read (1,11,20), the same as master. I added a unit test and a real-table regression covering both layouts. The Native version floor will be raised once 0.6.0 is released. PTAL (๑•̀ㅂ•́)و✧

@JingsongLi JingsongLi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The latest Java revision passed 290 selected partial-update and table tests, including the mixed DELETE and UPDATE_BEFORE flush layouts. That addresses my Java finding.

The existing P1 for Native 0.5 remains on this head. I reran the upgraded Java reader and the supported PyPaimon Native reader against the same two committed Parquet files. The files contain +I(1,10,20) followed by -U(1,10,NULL) with partial-update.remove-record-on-delete=true. The latest Java reader returns zero rows. Native 0.5.0 returns (1,10,20) with Python fallback disabled. PyPaimon still permits pypaimon-rust>=0.5.0, so both results are possible on supported deployments. Please enforce a compatible Native version or disable Native for this table configuration on older versions before releasing the changed Java semantics. Earlier compatibility finding

@JeonDaehong

Copy link
Copy Markdown

I wrote 400 random +I/+U/-U/-D sequences from two writers on one key, one writing a and one writing b, with write-only, and read each after six different splits into commits. Master read different rows across splits for 35 sequences, 10 of them without a -D. This branch fixes all 10 and differs for 17, all with a -D.

For example, this one depends on the split, on master too. The other writer commits b = 22, then one commit writes +I(12, null), -D(10, null), +U(10, null). That reads (10, 22), while the same four records in one commit read (10, NULL). The +U clears the DELETE inside the write buffer, so the reset never reaches the older file. Since the description says the result does not depend on how records are split, should this be covered here or tracked separately?

@dragonknightk

Copy link
Copy Markdown
Contributor Author

@JeonDaehong Thanks for running those 400 random sequences — it is good to see all 10 non--D split differences on master are gone.

I think the -D followed by +I/+U case should be tracked separately rather than covered in this PR:

  • As you noted, this is a pre-existing limitation on master rather than something introduced here.
  • It cannot be fixed inside PartialUpdateMergeFunction alone. A pre-merged segment needs to represent four states (keep vs. reset older columns, combined with alive vs. removed). Paimon's KV files only have four RowKind values, and upstream writers already use both +I and +U for normal non-resetting partial updates. Because a single LSM file stores at most one record per key, there is currently no way to encode "reset older files and stay alive" without a format or protocol change (for example, persisting a reset marker).

Good catch on the PR description — the wording there was too broad. I've tightened it to state that split independence holds for sequences without -D (and that -D, +I, -U over an older file matches flushing after +I), and noted this pre-existing -D followed by +I/+U limitation as out of scope.

Comment on lines +286 to +290
A row left removed is dropped like a `DELETE` by a compaction whose output level is above 0 and at
least the highest non-empty level, or by any compaction with an output level above 0 when deletion
vectors are enabled. A write after that compaction starts from an empty row, so with the `lookup`
or `full-compaction` changelog producer or deletion vectors, the columns of other writers are
usually kept only within one commit.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Mention the chain-table exception. Merge-tree compaction keeps chain-table retractions, so a later write can still recover their field values.

Suggested change
A row left removed is dropped like a `DELETE` by a compaction whose output level is above 0 and at
least the highest non-empty level, or by any compaction with an output level above 0 when deletion
vectors are enabled. A write after that compaction starts from an empty row, so with the `lookup`
or `full-compaction` changelog producer or deletion vectors, the columns of other writers are
usually kept only within one commit.
A compaction that rewrites data drops a removed row when its output level is above 0 and either
reaches the highest non-empty level or deletion vectors are enabled. Chain-table merge-tree
compaction keeps retractions and their field values.
Once compaction drops a removed row, a later write starts from an empty row. With the `lookup`
or `full-compaction` changelog producer or with deletion vectors enabled, compaction often drops
retractions between commits.

options.set("merge-engine", "partial-update");
options.set("partial-update.remove-record-on-delete", "true");
// no compaction, so every flushed file is merged again on read
options.set("write-only", "true");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Add a test that rewrites an UPDATE_BEFORE while leaving an older file outside the compaction. Check that the key stays hidden and a later partial update restores its older column.

initRow(row, kv.value());
} else {
// -U only marks the row removed and keeps the fields of other writers
updateNonNullFields(kv);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Preserve sequence.field ordering when buffering whole-row retractions. A merged UPDATE_BEFORE can carry an older column value at a later sequence and overwrite a newer value from another file. Add the interleaved-version case to the flush tests.

@@ -151,6 +151,15 @@ public static List<String> sequenceGroupProtectedFields(String optionValue) {
*/
private boolean meetInsert;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Update the meetInsert comment to account for the UPDATE_BEFORE result.

    /**
     * Whether this merge received an INSERT or UPDATE_AFTER. A retract-only merge returns DELETE
     * when no whole-row removal is pending.
     */
    private boolean meetInsert;


@Test
public void testRemoveRecordOnDeleteIgnoresUpdateBeforeUntilInsert() {
public void testRemoveRecordOnDeleteStaysDeletedOnUpdateBeforeUntilInsert() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Give UPDATE_BEFORE a distinct value and assert it while DELETE is pending. The current test passes if the merge skips applying the retraction payload.

        add(function, RowKind.UPDATE_BEFORE, 1, 15);
        assertThat(function.getResult().valueKind()).isEqualTo(RowKind.DELETE);
        validate(function, 1, 15);

Comment on lines +2151 to +2155
// Both keys read c = 20 of the other writer. This is the result of a = 2, where the -D and
// +I merge into an INSERT that does not re-initialize the row, and the lone -U keeps the
// fields. It is also the result of the base implementation for both layouts. A -D that an
// +I in the same write buffer has already overridden must not make the later -U a DELETE
// that wipes c, which is what a = 1 read before.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Explain the expected result directly. The opening reference to the base implementation leaves readers guessing which revision lost c for key 1.

Suggested change
// Both keys read c = 20 of the other writer. This is the result of a = 2, where the -D and
// +I merge into an INSERT that does not re-initialize the row, and the lone -U keeps the
// fields. It is also the result of the base implementation for both layouts. A -D that an
// +I in the same write buffer has already overridden must not make the later -U a DELETE
// that wipes c, which is what a = 1 read before.
// Both layouts preserve c = 20. The buffered INSERT clears the pending DELETE,
// so the later UPDATE_BEFORE preserves fields supplied by the other writer.

}

if (removeRecordOnDelete) {
currentDeleteRow = true;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Require a Native runtime with matching UPDATE_BEFORE semantics before enabling this change. PyPaimon still permits 0.5.0, whose SQL returns keys that Java removes under partial-update.remove-record-on-delete.

@JeonDaehong

Copy link
Copy Markdown

Thanks @dragonknightk, agreed. I'll open a separate issue with the repro.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Partial-update merge-function keeps stale row when the last message is a lone UPDATE_BEFORE (-U)

5 participants