Skip to content

[core] Do not count a retracted partial-update sum twice - #1549

Open
JeonDaehong wants to merge 3 commits into
apache:mainfrom
JeonDaehong:fix-pu-retract-sum
Open

JeonDaehong wants to merge 3 commits into
apache:mainfrom
JeonDaehong:fix-pu-retract-sum

Conversation

@JeonDaehong

Copy link
Copy Markdown
Contributor

Purpose

Closes #1543.

With a sequence group and a sum aggregate, a merge that starts with a retraction seeded the row from the retracted record, so the retraction cancelled its own payload and the old value was counted again. +I(90) and then -U(90), +U(90) read 180. This follows apache/paimon#11274, which fixes the same behavior in Java.

Brief change log

  • Under sequence groups without ignore-delete, a merge that starts with a retraction seeds only the required fields. A required sum starts at zero and nullable fields stay NULL.
  • A result made only of retractions emits each aggregate through retraction_result. For sum it flips the sign, so a later merge that retracts it again subtracts the right amount.
  • Other aggregates keep their current behavior, as in Java.

Tests

pk_partial_update_first_retract_test covers a retraction and an update in one commit, a retraction-only commit before an update, and a required sum whose sequence group is skipped. The first two also read again after a compaction. All three fail on main.

Two existing tests pinned the old seeding and now expect what Java returns with apache/paimon#11274 (checked on that branch merged with master):

  • partial_update_retract_only_aggregate_state_refuses_lossy_compaction: -D(10), -D(5), +I(3) on a summed field read -2, which is the double count itself. It now reads -12.
  • partial_update_ignore_retract_first_value_preserves_uninitialized_state: -D(version 3, 20) then +I(version 2, 5) on an ignore-retract first_value read (3, 20). The DELETE no longer seeds the value, so it reads (3, NULL).

I also wrote random +I/+U/-U/-D sequences on one key and split them into commits in different ways, with and without full compactions, covering nullable and required sums, ignore-delete, null sequence values and amounts, and the lookup changelog producer. Main read the wrong sum for 326 of 1,400 sequences, this branch for none of 2,100.

API and Format

No public API or storage format changes.

Documentation

None.

A partial-update merge that started with a retraction under sequence
groups seeded the row from that record, so the retraction cancelled its
own payload and the old value was counted again. Seed only the required
fields, start a required sum at zero, and emit a retraction-only sum with
its sign flipped, so a later merge retracts it again. Follows
apache/paimon#11274.

Closes apache#1543

@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 double-counting fix addresses a real write and read issue. I ran 148 partial-update, primary-key compaction, and row-kind integration tests on this head. A separate public write/commit/read test exposed the overflow case below.

[P2] Remove the generated Linux Go library from this source change. This commit adds the 25 MiB bindings/go/libpaimon_c.linux.amd64.so.zst to the source tree. bindings/go/RELEASE.md puts these platform binaries in the Go release commit built from the reviewed source ref. Please drop the binary so the release workflow builds the matching artifacts from source.

// seed only the required fields, with aggregates
// empty: seeding the retracted value would let the
// retraction cancel it and keep the old value.
self.init_from_retraction(

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 a checked sum writable when the retraction cancels a valid existing value. On a partial-update table with fields.seq.sequence-group=amount, a required BIGINT sum, and fields.amount.sum.fail-on-overflow=true, I committed +I(amount=i64::MIN, seq=1). Committing -U(i64::MIN, 2) and +U(i64::MIN, 3) separately reads i64::MIN as expected. Sending the same update pair in one commit on this head fails at prepare_commit with Aggregate function 'sum' overflowed on field 'amount'. Pre-merging the pair starts at zero and computes 0 - i64::MIN before the compensating add. Please keep the original records when that intermediate merge cannot be represented, or represent the intermediate delta exactly, and test both flush layouts.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks @JingsongLi. The .so.zst came from building the Go bindings in the same checkout, sorry about that. It's dropped in 801ff48. The overflow case came up on apache/paimon#11274 today as well, so I'll follow the representation that lands there and update this PR once it's settled.

@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 retested 801ff488 integrated with 8105e1eb. The Go binary is gone, and 5,311 core and table tests passed, including the normal partial-update sum and compaction regressions. The earlier checked SUM issue still reproduces. Two additional table tests show where the overflow leaks into the postpone writer and the retraction-only flush. The Java port discussion covers the latter boundary too. apache/paimon#11274

// seed only the required fields, with aggregates
// empty: seeding the retracted value would let the
// retraction cancel it and keep the old value.
self.init_from_retraction(

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 raw retractions in the postpone writer. On a bucket=-2 partial-update table with a sequence group and checked BIGINT sum, I committed +I(i64::MIN) and then wrote a single -U(i64::MIN) in a fresh writer. The same two commits succeed on 8105e1eb. On this head, write_arrow_batch fails with Aggregate function 'sum' overflowed on field 'amount'. The postpone writer stores the raw row, but its first-retract validator calls merge_unreduced, which now tries to compute 0 - i64::MIN. Please preserve validation of supported retractions without requiring an intermediate sum for this raw write, and cover the standalone retraction in the postpone writer tests.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks @JingsongLi. The postpone writer only needs to know that the retraction is supported, so its check now starts the row from the retracted value as it did before this PR, and 0 - i64::MIN is never computed. postpone_writer_keeps_a_retraction_of_the_minimum writes the standalone -U(i64::MIN) after +I(i64::MIN); it fails on the previous head.

match $acc {
Some(value) if self.fail_on_overflow => Some(
value
.checked_neg()

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 checked sums across a retraction-only flush. With a required BIGINT sum and fields.amount.sum.fail-on-overflow=true, I wrote +I(-1), +I(i64::MAX), +I(1), -U(i64::MAX), -U(1) on one key. One commit succeeded and read -1. Committing the inserts first and the retractions second failed in prepare_commit. The retraction-only accumulator reaches the representable value i64::MIN, and encoding it as a positive DELETE payload overflows here. Preserve the raw retracts or use a representation that carries the exact delta when it cannot be encoded as a DELETE payload. Please cover both flush layouts.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

When the retractions add up to the minimum of a checked sum, no DELETE value can undo them, so that result is now written as an add of the sum, +I(i64::MIN), which the next merge adds back exactly. Every other retraction-only result is still a DELETE row. checked_sum_retracted_to_its_minimum runs your rows in one commit and split into two, for required and nullable sums, and reads -1 before and after a full compaction. One difference to note: a key that only ever gets such retractions now reads as a row with the minimum instead of no row.

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.

Partial-update sum counts a retracted value twice when a merge starts with a retraction

2 participants