Skip to content

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

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

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

Conversation

@houlinchn

@houlinchn houlinchn commented Nov 19, 2026 •

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:

  • Initializes only NOT NULL fields when a sequence-group merge starts with a retraction. Retractable aggregate fields (aggregators in a sequence group that are not ignore-retract) start from an empty accumulator: null for nullable fields, and FieldAggregator#emptyAccumulator() for NOT NULL fields (0 for sum). A leading -U(90) now contributes -90, as it did before [core] Fix initRow in AggregateMergeFunction and PartialUpdateMergeFunction to set all fields including nullable ones #7645.
  • When a merge only sees retractions, getResult() writes the retracted amount in the DELETE record, for example DELETE(90) instead of the internal accumulator -90. The next merge retracts this record again, so a retraction-only flush followed by a later update keeps the right sum. FieldAggregator#retractionResult() does the conversion: sum negates the accumulator and other aggregators return it unchanged. The accumulator itself is kept for later calls to add().
  • FieldIgnoreRetractAgg delegates emptyAccumulator(), so NOT NULL ignore-retract sums also start from zero.
  • With ignore-delete, keeps the retract payload when a key only has retractions, so the DELETE result is unchanged, and resets the row before the first positive record so the ignored value is not aggregated.
  • Leaves the whole-row delete paths (partial-update.remove-record-on-delete, partial-update.remove-record-on-sequence-group) on the full initRow that [core] Fix initRow in AggregateMergeFunction and PartialUpdateMergeFunction to set all fields including nullable ones #7645 introduced.
  • Does not change product. Its retraction-only result can't be represented in the field type (1 / acc truncates to 0 for integer types), so +I(5) followed by -U(5), +U(5) still reads 25, as on master. Tracked in [Bug] Partial-update product counts a retracted value twice when a merge starts with a retraction #11465.

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 retraction-only result merged again with the old value;
    • a null sequence group;
    • NOT NULL ignore-retract fields, including an ignore-retract sum;
    • ignore-delete with historical retractions, including a key with only retractions;
    • remove-record-on-delete payloads.
  • PartialUpdateFirstRetractTableTest (table level), with changelog-producer = none, input and lookup where applicable:
    • commits +I(90) and then -U(90), +U(90 | 60), and checks the data read and the changelog;
    • commits +I(40), +I(50), then -U(40), -U(50), then +U(60), and checks that it reads 0 after the second commit and 60 after the third;
    • a NOT NULL sum whose sequence group is null in some records;
    • a NOT NULL product with a retraction-only commit.
  • FieldAggregatorTest: emptyAccumulator() and retractionResult() for sum and product.

Results on JDK 11 at 55e140d:

  • With the main-code change reverted, 21 of the 24 new cases in PartialUpdateFirstRetractTest and PartialUpdateFirstRetractTableTest fail. The table tests read 180 instead of 90, and 140 instead of 0 after the retraction-only commit. The other 3 cases pass without the change too; they are compatibility controls for the DELETE-only payload, remove-record-on-delete and the NOT NULL product case.
  • With the change, PartialUpdateFirstRetractTest, PartialUpdateFirstRetractTableTest, PartialUpdateMergeFunctionTest, LookupChangelogMergeFunctionWrapperTest and FieldAggregatorTest pass: 222 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.

@JeonDaehong JeonDaehong left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks @houlinchn. I ran 55e140d merged with current master against random +I/+U/-U/-D sequences on one key, each written in one commit, in random commits, and in random commits with full compactions in between. It covered changelog-producer none, input and lookup, nullable and NOT NULL sums, ignore-delete, null sequence values and null amounts. All 9,000 runs read the expected sum, and the lookup changelog added up to it. Master missed it in 804 runs.

* retractions.
*/
@Nullable
public Object retractionResult(@Nullable Object accumulator) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

product also implements retract but keeps these defaults, so it still starts from the retracted value. Committing +I(5) and then -U(5), +U(5) reads 25 on master and on this branch. Is product meant to stay out of scope here? A line in the description would help if so.

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.

Yes, product stays out of scope. Its retraction-only state can't be stored in the field type: retractionResult() would have to return 1 / acc, which truncates to 0 for integer types. retract(null, x) also returns null, so a leading retraction is dropped. +I(5) followed by -U(5), +U(5) reads 25 on master and on this branch, so this PR doesn't change it. I opened #11465 to track it and added a note to the description.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks @houlinchn, that makes sense. The note and #11465 cover it.

@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.

Reviewed 55e140d65e. All 223 focused Core and 9 Flink 1.20.1 tests passed. I verified the ordinary sum correction across two full Parquet compactions and reader reopenings. A separate checked-overflow case still makes the result depend on the flush boundary.


@Override
public Object retractionResult(Object accumulator) {
return negative(accumulator);

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.

[P2] Preserve a checked sum across a retraction-only flush at the signed minimum

On a real Parquet primary-key table with amount INT NOT NULL, a sequence group, and fields.amount.sum.fail-on-overflow=true, I wrote +I(-1), +I(2147483647), +I(1), -U(2147483647), -U(1). In one commit, it succeeds and reads -1. If I commit the three inserts first, the two retractions accumulate to the representable value -2147483648, then the second commit fails in retractionResult() when it tries to negate that value into a DELETE payload. The same split succeeds with overflow checking off. Please preserve a representation for this retraction-only buffer that does not require an unrepresentable positive payload, and test it across separate commits.

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. As you noted in the latest review, this still reproduces on the current head; the postpone fix doesn't touch it.

I tested this on a TINYINT NOT NULL sum with fail-on-overflow, writing to real tables, and compared master with prototypes of a few options. The table shows the read result, or ✗ when a commit or read fails.

Case Expected master Negate fallback (paimon-rust#1549) Wider accumulator Wider accumulator + raw records
Your case: +I(-1), +I(127), +I(1), then -U(127), -U(1) -1 ✗ -1 -1 -1
+I(-128), then -U(-128), +U(-128) in one commit -128 ✗ ✗ -128 -128
Sum is 0, then -U(-128), +U(-128) in one commit 0 -128 ✗ 0 0
+I(127), +I(1), +I(-1) in one commit 127 ✗ ✗ 127 127
+I(-128), then -U(-128), +U(0) in one commit 0 -128 ✗ ✗ 0
Same, then a compaction of only the new files 0 -128 ✗ ✗ 0
+I(127), then +I(1) ✗ ✗ ✗ ✗ ✗

With overflow checking off, all three options read the expected values. Master double counts in most of these cases.

The wider accumulator keeps a checked integer sum as a BigInteger during a merge and narrows it once in getResult(). A retraction-only result that can't be negated is written as an add of the sum, as in paimon-rust#1549. This still fails when the net change of the merged records doesn't fit the type. In the fifth row the batch adds 128 to a TINYINT, and no record of the field type can carry that.

The last option also keeps raw records for that case. When the merged result of a key doesn't fit, flush and compaction write the key's original records to separate level-0 files, one record per file, instead of failing. The read merges them with the older data using the wider accumulator. With both, a checked sum fails only when the final value overflows. I tested both flush layouts, partial compactions, and the lookup changelog, which matched the expected -U/+U pairs.

Two options:

  1. Wider accumulator only. A batch whose net change doesn't fit still fails.
  2. Wider accumulator plus raw records. This also changes flush and compaction output. The result no longer depends on the flush boundary.

Both need FieldAggregator methods so PartialUpdateMergeFunction has no sum-specific code. Both also change one result. Take a key with no earlier rows that only receives -U(127), -U(1). The retractions add up to -128, and the DELETE payload would need +128, which a TINYINT can't hold. The result is written as INSERT(-128) instead, so the key reads as a row with -128 where it now reads as no row. If the key has an earlier row, the result is the same as with a DELETE. Which would you prefer?

@JeonDaehong

Copy link
Copy Markdown

The postpone writer hits the same overflow. With bucket = -2, a required BIGINT sum with fail-on-overflow, committing +I(Long.MIN_VALUE) and then writing -U(Long.MIN_VALUE) from a new writer fails in validateRetract with long overflow: 0 - -9223372036854775808. Master accepts it, and bucket = 1 is fine. In paimon-rust I let that check start from the retracted value as before, since it only needs to know the retraction is supported (apache/paimon-rust#1549).

@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.

I reran 219 Core cases and independently verified the postponed-writer failure reported above against the same Parquet data on pinned master and this head. The earlier fixed-bucket flush-boundary problem still reproduces as well.


@Override
public Object emptyAccumulator() {
return TypeUtils.castFromString("0", fieldType);

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.

[P2] Keep postpone's retraction validation independent of accumulator overflow

With bucket=-2, a required BIGINT sum with overflow checking, and a committed Long.MIN_VALUE, a fresh writer rejects UPDATE_BEFORE(Long.MIN_VALUE) in PostponeBucketWriter.validateRetract() at 0 - Long.MIN_VALUE. Pinned master accepts and commits the same retraction. The fixed-bucket writer accepts the record into its buffer, so this is specifically an eager validation failure. Please distinguish testing whether the operation supports retractions from computing the merged result. This is separate from the fixed-bucket failure when encoding an already representable Integer.MIN_VALUE retraction-only accumulator as a DELETE.

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, done in c4c3db1. The postpone writer calls a new MergeFunction.validateRetract(RowKind), which only checks the configuration and never computes a merged result:

  • PartialUpdateMergeFunction follows the retract branches of add(): ignore-delete, sequence groups, remove-record-on-delete, otherwise the existing error.
  • AggregateMergeFunction accepts a DELETE with aggregation.remove-record-on-delete, and otherwise checks every field aggregator.
  • FieldAggregator.validateRetract() throws the same error as retract(), and each aggregator that overrides retract() overrides it too.

Tests:

  • ValidateRetractTest compares the check with merging a single retraction for every merge engine, both retract kinds, and 13 aggregates with and without ignore-retract. Results and error messages match.
  • FieldAggregatorValidateRetractTest does the same for every aggregate found through SPI, so a new aggregator that supports retract() but doesn't declare it fails the test.
  • At the table level, bucket = -2 accepts your Long.MIN_VALUE case and still rejects a retraction on max without ignore-retract.

The postpone writer only checks that a retraction is supported. Since the first-retract fix, that check started the sum from zero and computed 0 - MIN for a checked sum, so writing -U(MIN) failed. Add MergeFunction.validateRetract and let PartialUpdateMergeFunction start the check from the retracted value.
@houlinchn

houlinchn commented Nov 30, 2026 •

Copy link
Copy Markdown
Author

Thanks @JeonDaehong, reproduced. The postpone writer now calls MergeFunction.validateRetract(RowKind), which decides from the merge function's configuration whether a retraction is supported, without merging anything, so it can't overflow. PartialUpdateMergeFunction, AggregateMergeFunction and FirstRowMergeFunction follow the same branches as add(), and each field aggregator that supports retract declares it. testPostponeWriterAcceptsRetractionOfCheckedSumMinimum covers your case. ValidateRetractTest checks that the new method rejects exactly the retractions that a merge rejects, for every merge engine and the built-in aggregates.

The postpone writer only needs to know whether a retraction is supported. Instead of merging the record, MergeFunction.validateRetract(RowKind) now decides from the merge function configuration, so a checked sum can not overflow during the check. PartialUpdateMergeFunction, AggregateMergeFunction and FirstRowMergeFunction follow the same branches as add(), and FieldAggregator.validateRetract() is overridden by every aggregator that supports retract().
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

3 participants