Skip to content

[core] Fix partial-update sums when a merge starts with a retraction - #11274

Open
houlinchn wants to merge 3 commits into
apache:masterfrom
houlinchn:fix-partial-update-first-retract
Open

houlinchn wants to merge 3 commits into
apache:masterfrom
houlinchn:fix-partial-update-first-retract

Conversation

@houlinchn

Copy link
Copy Markdown

Purpose

Fixes #11273

A partial-update table with sequence groups and a sum aggregate double-counts when a merge starts with a retraction. With amount = 90 committed, a later commit of -U(90), +U(90) should leave 90 but reads 180.

Since #7645, initRow copies every field of the first retract record. The retracted value therefore becomes the starting accumulator, retract(90, 90) cancels it, and the +U is added on top.

This change:

Tests

  • PartialUpdateFirstRetractTest (merge-function level):
    • a leading UPDATE_BEFORE or DELETE followed by +U, then compaction with the old value;
    • NOT NULL aggregate fields;
    • consecutive retractions;
    • a null sequence group;
    • ignore-retract fields;
    • ignore-delete with historical retractions, including a key with only retractions;
    • remove-record-on-delete payloads.
  • PartialUpdateFirstRetractTableTest (table level): commits +I(90) and then -U(90), +U(90 | 60). It checks the data read and the changelog for changelog-producer = none, input and lookup.

Results on JDK 11:

  • On master df565c64f7 without the main-code change, 14 of the 16 new cases fail. The table test reads amount = 180 instead of 90 with all three changelog producers. The other 2 cases pass on master too; they are compatibility controls for the DELETE-only payload and remove-record-on-delete.
  • With the change, PartialUpdateFirstRetractTest, PartialUpdateFirstRetractTableTest, PartialUpdateMergeFunctionTest and LookupChangelogMergeFunctionWrapperTest pass: 94 tests. The run used Maven verify with -pl paimon-core -am, which also passed Checkstyle, Spotless, Enforcer and RAT.

Start sequence-group aggregates from an empty accumulator so a leading retraction contributes the negative of its input. When deletes are ignored, preserve the delete payload until the first positive record starts aggregation.

Cover both merge behavior and committed table reads across changelog producers.

@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 new cases fix the retraction-plus-update batch from #11273, and all 84 focused merge and persisted-table tests passed. A separate persisted-table case showed that the fix still depends on where the write buffer flushes.

[P1] Keep the sign of a retraction-only buffer when its result is merged again. I used the PR's Parquet primary-key partial-update table with sum and sequence groups. I committed +I(40), +I(50), then -U(40), -U(50) in a second commit, then +U(60) in a third. The total should be 60. Master read 200, and this PR increased it to 240.

The second buffer now combines the retractions into an accumulator of -90, and getResult() emits a DELETE when there was no insert. On readback the merge treats that DELETE's negative accumulator as a raw contribution and subtracts it again, increasing the total by 90. Please preserve a representation that applies the correct sign at subsequent merge levels and test a retraction-only flush before a later positive update.

@houlinchn

houlinchn commented Nov 20, 2026 •

Copy link
Copy Markdown
Author

Thanks for the repro, @JingsongLi. I reproduced 240 with the same three commits. I updated getResult() so the retraction-only buffer keeps -90 internally and writes DELETE(90). The next merge then subtracts 90 as intended.

I added a table-level regression test that checks the amount after the second and third commits. It reads 0 after the two retractions and 60 after the +U(60) update. The test passes with changelog-producer set to none, input, and lookup.

I also tried all 16 ways to split those five records across commits. Every split produced 60 with each changelog producer.

@houlinchn
houlinchn force-pushed the fix-partial-update-first-retract branch from 14d9f6f to e9e9baa Compare November 20, 2026 14:34
this.initialSumValues = new HashMap<>();
fieldAggregators.forEach(
(i, aggregator) -> {
if (aggregator instanceof FieldSumAgg) {

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.

This looks very bad. Put FieldSumAgg into common merge func.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks, moved it into FieldAggregator. It now has emptyAccumulator() and retractionResult(), and FieldSumAgg overrides both. PartialUpdateMergeFunction no longer checks for FieldSumAgg.

This also fixes NOT NULL ignore-retract sums. They're wrapped in FieldIgnoreRetractAgg, so the old instanceof check missed them.

Add emptyAccumulator() and retractionResult() to FieldAggregator so PartialUpdateMergeFunction no longer special-cases FieldSumAgg. FieldIgnoreRetractAgg delegates emptyAccumulator(), so NOT NULL ignore-retract sums also start from zero.
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 sequence-group sum double-counts when a merge starts with a retraction

2 participants