Skip to content

fix: canonicalize signed zero for distinct aggregates - #27434

Merged
XuPeng-SH merged 8 commits into
matrixorigin:mainfrom
Lundomn:fix/count-distinct-signed-zero
Aug 21, 2026
Merged

XuPeng-SH merged 8 commits into
matrixorigin:mainfrom
Lundomn:fix/count-distinct-signed-zero

Conversation

@Lundomn

@Lundomn Lundomn commented Aug 21, 2026 •

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

Fixes #27425

What this PR does / why we need it:

Fixes incorrect COUNT(DISTINCT FLOAT/DOUBLE) results when signed zero values are combined with another aggregate.

Before this change, the saved-argument DISTINCT path compared raw IEEE-754 bytes. As a result, -0.0 and +0.0 were treated as different values when an ordinary aggregate was present, even though MatrixOne SQL equality and GROUP BY already treat them as equal.

This change:

  • Canonicalizes signed zero only for FLOAT32/FLOAT64 DISTINCT argument keys.
  • Applies the same canonicalization to single-column, multi-column, capacity-preflight, merge, and serialized saved-argument key paths.
  • Writes canonical zero directly into existing mpool-accounted scratch buffers, avoiding per-row Go heap allocations.
  • Preserves non-zero floating-point payloads and existing NULL semantics.
  • Adds result, preflight, and allocation regressions for FLOAT32/FLOAT64 and multi-column DISTINCT arguments.

Validated locally with:

  • go test -mod=readonly -vet=off -count=1 -short -tags matrixone_test -p 1 ./pkg/sql/colexec/aggexec
  • go test -mod=readonly -vet=off -count=1 -short -race -tags matrixone_test -p 1 ./pkg/sql/colexec/aggexec
  • go vet -mod=readonly ./pkg/sql/colexec/aggexec
  • git diff --check
  • make build
  • Kafka -> MatrixOne consistency regression with 590,187 GH Archive rows
  • The original 12,000-row Kafka pipeline suite

Expected result for the issue reproducer:

select count(*), count(distinct x) from t;
-- 2, 1

@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

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

Deep-reviewed exact head f2aab03b9b79eb1575f1e16f1e3397954e4072c7 against base 344d852ac3288e391a3ae6be3e3c7108caf2b75d. The signed-zero equivalence is applied consistently to single-column, multi-column, preflight, merge, and serialized saved-argument keys, but the implementation introduces a blocking hot-path allocation defect.

[P1] Keep signed-zero DISTINCT key construction allocation-free and inside the aggregate allocation contract. canonicalDistinctArgumentBytes returns a slice of a local [4]byte or [8]byte. The function is not inlineable, and escape analysis reports both local zero arrays escaping to the Go heap. A deterministic testing.AllocsPerRun probe measured exactly 1,000 allocations for 1,000 calls.

This helper is called from preflightArgumentRowsEqual (twice per candidate comparison), preflight key construction, and the real batchFillArgs path. A signed-zero-heavy DISTINCT batch can therefore create a large number of row-frequency Go objects outside mpool, outside the allocation account, and after capacity preflight was supposed to make mutation safe. That reopens GC/OOM behavior the preflight/accounting work is designed to prevent.

Please write the canonical zero directly into the existing accounted key scratch, or reuse an immutable/non-escaping representation without exposing mutable shared bytes. Add an allocation regression that exercises both PreflightBatchFill and BatchFill, not only result correctness. The current signed-zero result regression passes, but it cannot detect this resource-bound violation.

The temporary allocation probe was removed afterward. git diff --check is clean and all current GitHub checks are complete with allowed conclusions.

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

Deep re-reviewed exact head db88a4b09c089bac387a73febdfed2c2f704f894 after my requested changes on f2aab03b9b79eb1575f1e16f1e3397954e4072c7. The hot-path allocation blocker is closed: signed zero is canonicalized directly in the existing mpool-accounted scratch key, while preflight duplicate comparison avoids temporary byte slices. The single-column and multi-column key layouts remain consistent across preflight, fill, merge, and serialized saved-argument paths; non-zero payloads and NULL behavior are unchanged.

Validation on the exact head: focused signed-zero/preflight tests at -count=10, focused race tests at -race -count=3, the full pkg/sql/colexec/aggexec package, and git diff --check all pass. The new allocation regression covers FLOAT32, FLOAT64, and multi-column keys in both PreflightBatchFill and BatchFill. No remaining blocking issue found.

@mergify mergify Bot added the queued label Aug 21, 2026
@mergify

mergify Bot commented Aug 21, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-08-21 12:01 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • ❌ Checks failed · in-place
  • 🚫 Left the queue — 2026-08-21 12:53 UTC · at c53a38f3a8b1963b03114777091422392c4606de

This pull request spent 51 minutes 43 seconds in the queue, with no time running CI.

Waiting for
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone CI / SCA Test on Linux/arm64
    • check-skipped = Matrixone CI / SCA Test on Linux/arm64
    • check-success = Matrixone CI / SCA Test on Linux/arm64
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
    • check-success = Matrixone Utils CI / Coverage
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-skipped = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-success = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
    • check-success = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
All conditions
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone CI / SCA Test on Linux/arm64
    • check-skipped = Matrixone CI / SCA Test on Linux/arm64
    • check-success = Matrixone CI / SCA Test on Linux/arm64
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
    • check-success = Matrixone Utils CI / Coverage
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-skipped = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-success = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
    • check-success = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
  • #review-threads-unresolved = 0 [🛡 GitHub branch protection]
  • github-review-approved [🛡 GitHub branch protection]

Reason

The merge conditions cannot be satisfied due to failing checks

Failing checks:

Hint

You may have to fix your CI before adding the pull request to the queue again.
If you update this pull request, to fix the CI, it will automatically be requeued once the queue conditions match again.
If you think this was a flaky issue instead, you can requeue the pull request, without updating it, by posting a @mergifyio queue comment.

Tick the box to put this pull request back in the merge queue (same as @mergifyio queue).

  • Requeue this pull request

This branch was previously deployed

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

Labels

kind/bug Something isn't working size/M Denotes a PR that changes [100,499] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: COUNT(DISTINCT FLOAT/DOUBLE) splits signed zero when combined with another aggregate

4 participants