Skip to content

fix: make aggregate states version compatible - #28885

Merged
XuPeng-SH merged 11 commits into
matrixorigin:mainfrom
XuPeng-SH:fix/issue-28647-top1-2
Sep 17, 2026
Merged

XuPeng-SH merged 11 commits into
matrixorigin:mainfrom
XuPeng-SH:fix/issue-28647-top1-2

Conversation

@XuPeng-SH

@XuPeng-SH XuPeng-SH commented Sep 15, 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:

issue #28778
issue #28854
issue #28864
issue #28865

What this PR does / why we need it:

  • Preserves aggregate representatives and state compatibility across stable
    intermediate output, spill, restore, repartition, and partial recovery.
  • Keeps exact COUNT(DISTINCT ...) FLOAT32/FLOAT64 equality consistent across
    fixed-index, skiplist, merge, restore, and spill paths. Modern state
    canonicalizes NaN payloads; legacy remote state retains the old bit-pattern
    contract until the protocol capability is available.
  • Rejects a modern FLOAT DISTINCT partial before mutation when a legacy target
    would otherwise misinterpret its canonicalized keys.
  • Keeps the existing MORPC capability gates for the aggregate wire contracts;
    no protocol threshold or coverage exclusion was changed.

Root cause of the CI failure

The aggregate implementation tests passed, but the PR coverage gate measured
148/211 = 70.14% changed lines, below the required 75%. The missing coverage
was in newly added FLOAT32/FLOAT64 compatibility branches, tuple normalization,
cross-representation merge preflight, policy freezing, and the real Group
partial-output downgrade guard.

This update adds behavior-focused tests for those paths. It does not lower the
threshold, add an ignore rule, or create unreachable code only for coverage.

Validation

  • Rebased onto main=8e90e412647eee460772068efa127daa23d09828.
  • go test -count=1 -coverprofile=... ./pkg/sql/colexec/aggexec ./pkg/sql/colexec/group ./pkg/sql/compile — PASS.
  • Package coverage in that run: aggexec 80.7%, group 82.0%, compile 66.8%.
  • CI-equivalent changed-line replay using the exact origin/main...HEAD diff,
    the collected UT/BVT profiles, and the current affected-package profile:
    198/224 = 88.39% — PASS (threshold >75%).
  • gofmt and git diff --check — PASS.
  • Official CI, including the repository-level merged coverage evaluator on a
    fresh producer set, will still run for the pushed head; it is not claimed as
    passed here.

No new BVT or full slow UT was added.

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

@matrix-meow matrix-meow added the size/XL Denotes a PR that changes [1000, 1999] lines label Sep 15, 2026

@XuPeng-SH XuPeng-SH left a comment

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.

GPT-6 medium deep review at exact head 81d586d.

Local decision: BLOCKED (COMMENT ONLY).

P1 — upgrading breaks incremental aggregation of persisted HLL sketches. Before upgrade, a non-empty HLL_ADD_AGG(user_id) result can be persisted as version 2. After upgrade, the same producer creates version 3, including for integer inputs. HLL_MERGE_AGG adopts the first non-empty version and rejects the other with incompatible HLL hash versions, so the existing daily-rollup path (see test/distributed/cases/function/func_hll.sql) fails when it appends post-upgrade data and merges old plus new sketches.

The version transition needs an explicit compatibility/upgrade path before changing the default producer. Please add a regression covering base-produced sketch -> upgrade -> append -> merge, including an integer control, and document or implement the transition for existing persisted sketches. The current HLL/CI coverage does not prove this upgrade path; upgrade compatibility jobs were skipped.

No APPROVE/REQUEST_CHANGES action is possible on an author-owned PR; this is the COMMENT-only review result.

@aptend aptend 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 review at exact head 81d586d0db73750fc0d831a4e33793acc5f2f8b7: REQUEST_CHANGES.

I independently reproduced two correctness blockers below. The CGo-aware package suite passed for aggexec, group, compile, plan/function, and datasync; focused aggregate/compile race tests and TestLoopWork/valid_entries race repetitions also passed. Those results do not exercise persisted-state upgrade compatibility, and the PR CI shows both upgrade-compatibility jobs skipped.

The bitmap bounds/error propagation, approximate-percentile ordering/direction and legacy remote gating, ordered-percentile comparator changes, and datasync synchronization otherwise look sound in the reviewed paths.

Separately, this PR body does not follow the repository's current .github/PULL_REQUEST_TEMPLATE.md; please restore the type checklist, issue section, and what/why section when updating it.

hllHeaderSize = 8
hllEncodedSize = hllHeaderSize + hllRegisterCnt
hllLegacyVersion = byte(2)
hllVersion = byte(3)

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] Preserve persisted HLL states across the v2→v3 transition

Changing the default producer to v3 makes every newly produced non-empty sketch v3, including integer inputs whose hashing/registers are unchanged, while useWireVersion rejects any non-empty v2/v3 pair. This breaks the supported persisted-rollup pattern already exercised by test/distributed/cases/function/func_hll.sql: create/store a non-empty HLL_ADD_AGG(user_id) row on the old build (v2), upgrade, append another row from the same integer producer (now v3), then run HLL_CARDINALITY(HLL_MERGE_AGG(sketch)); the query fails with incompatible HLL hash versions: destination 2, input 3 instead of merging the rollup. I reproduced exactly that producer→persist→upgrade→append→merge flow at this head. The MORPC gate only protects live remote states, not blobs already stored in user tables. Please provide an explicit compatible transition/migration path before changing the default producer and cover the persisted upgrade flow with an integer control.

return
}
case types.T_float64:
decoded := types.DecodeFixed[float64](value)

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] Canonicalize NaN peers as well as signed zero

The new type-aware path canonicalizes only values equal to zero. MatrixOne's float contract treats all NaN sign/payload encodings as peers (Float64OrderAscCompare returns 0), and SQL can construct different payloads with BIT_CAST. At this exact head, feeding 0x7ff8000000000000 and 0x7ff8000000000001 to APPROX_COUNT_DISTINCT(DOUBLE) returns 2 even though the comparator says they are one SQL equivalence class; the same raw-byte hashing affects HLL_ADD_AGG. A SQL reproducer can insert the two payloads using the existing bit_cast(unhex(... ) as double) pattern from float_nan_order.sql and compare COUNT(DISTINCT x) with both HLL results. This also leaves the NaN-payload regression explicitly requested by #28856 uncovered. Please normalize every NaN to a stable representation for both float widths before hashing and add aggregate/BVT coverage.

@XuPeng-SH XuPeng-SH left a comment

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.

GPT-6 medium deep review at exact head 1948060\n\nPASS — no blocking findings across correctness, concurrency/lifecycle, liveness, resource bounds, compatibility, and test closures. Persisted HLL remains v2 compatible; DISTINCT representative and spill/recovery paths are consistent. Exact-head CI passed, and the four SQL regressions passed 8/8, 5/5, 9/9, and 10/10 assertions. Upgrade jobs were skipped and excluded from evidence. COMMENT only because this PR is self-authored.

@XuPeng-SH XuPeng-SH left a comment

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.

Deep review 发现一个 P1(当前 head 18dd061):

MORPC v74 已被 base 用于 HEX,但本 PR 在 v74 打开了与旧 reader 不兼容的 aggregate 行为。pkg/sql/colexec/group/helper.go:1947 附近:HLL payload 改为 v3,而 base reader 仍拒绝该格式(整数输入也会失败);新的 percentile ordering 也让旧 reader 拒绝 [1, NaN] extrema,并可能把远程 finalization 的 p0 从 NaN 改为 1。针对 v74 的 focused local regressions 失败,v73 对照通过;现有 CI 虽绿,但两个 upgrade job 被 skip。

请保持 v74 的旧 aggregate 行为,改用未占用的 capability version,并用真实 base reader 的 producer/consumer compatibility tests 对齐 gate。自有 PR 按规则只提交 COMMENT。

@XuPeng-SH

Copy link
Copy Markdown
Contributor Author

Addressed the P1 found on head 18dd06181d9e805570acdf9829ac6c9e74498985.

  • MORPC v74 remains reserved for main's numeric HEX compatibility.
  • Aggregate capability gates are now v75 (percentile/compatible HLL), v76 (typed HLL), v77 (variable-length GROUP), and v78 (canonical DISTINCT); payload versions and algorithms are unchanged.
  • v74 current producer → pinned main@5aa23c reader compatibility passed for HLL integer/empty/signed-zero states and percentile [1, NaN].
  • CGo package closure, HEX tests, vet, and incremental lint passed; lint reported 0 issues.
  • GPT-6 medium final review of exact head 92b4f8dff41d7d2e8f927a036a6d05a1a2a5e4c8: PASS, no blocking finding.

Official CI is pending; upgrade jobs remain skipped and are not counted as green evidence.

@XuPeng-SH XuPeng-SH left a comment

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.

Deep review 结果(当前 head 92b4f8d):

此前 v74 HLL/percentile 兼容性 P1 已修复:v74 保持 HLL v2 和旧 percentile ordering,新 capability 从 v75/v76 开始。

仍有 P2:pkg/sql/colexec/aggexec/aggState.go:2797 的 fixed index 在达到 1,024 groups 后 canonicalize FLOAT64 NaN,但 writer 没保留原始 representative。v74 coordinator 合并 upgraded/base worker partial 时,同一个 noncanonical NaN 可能作为两个不同 key 被计数;v78 opaque-DISTINCT gate 不覆盖该路径。请对旧 coordinator 保持 legacy float key(含 fixed-index 和 spill),并用 pinned-base reader、>=1024 groups、noncanonical NaN 的 old/new partial 回归验证。

Exact-head CI 通过,但 upgrade jobs skipped;该 finding 为 source-traced,尚未本地复现。

@XuPeng-SH

Copy link
Copy Markdown
Contributor Author

Rebased the PR onto current main 76099145e701eb6c0412d3c3c73e8faf262fde3d and published head ac8d3613d6e3abbd6993252aa39ca66e1eeb5851.

  • range-diff confirms all 15 PR commits were preserved one-to-one; no conflict or aggregate protocol semantic change was introduced.
  • New-head compile/HEX/protocol, JSON_MERGE boundary, and plan/function tests pass; full pkg/sql/compile and pkg/sql/plan/function tests pass.
  • New-head compile race tests pass; incremental gofmt/vet/lint pass with 0 issues.
  • Existing aggregate/hashmap/group/datasync CGo, pinned base-reader, and BVT evidence remains valid because the rebase changed only unrelated JSON/collation base paths.
  • GPT-6 medium final review of the exact new head: PASS/ACCEPT, no P0-P2 blocker.

Official CI for the new head is now running/pending.

@XuPeng-SH XuPeng-SH left a comment

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.

Force deep review 结果(当前 head ac8d361):

此前 DISTINCT NaN 跨版本兼容 P2 仍未修复。pkg/sql/colexec/aggexec/aggState.go:2797 canonicalize FLOAT64 NaN,fixed-index writer 在 line 374 输出该 key 但不保留 raw representative。达到 >=1,024 groups 后,升级 worker 与 base worker 处理同一个 noncanonical NaN 时,v74 coordinator 可能收到不同 key,按 raw bytes 比较后将同一值计数为 2 而不是 1;v78 opaque-DISTINCT gate 不覆盖 fixed-width path。

请对旧 coordinator 保持 legacy float-key semantics,覆盖 fixed-index 及 spill/recovery,并增加 pinned-base reader 的 mixed old/new partial 回归:>=1,024 groups + noncanonical NaN。

此前 HLL/percentile capability collision 已修复:v74 保留 legacy behavior,新 gate 从 v75/v76 开始。当前 CI pending,upgrade compatibility jobs skipped;本 finding 为 source-traced,尚未本地复现。

@XuPeng-SH

Copy link
Copy Markdown
Contributor Author

Addressed the fixed-index FLOAT DISTINCT compatibility blocker from the previous review. Current head: f4691bfce68c7737579dfcde3b7b0a2c7140a821.

  • A legacy protocol producer now keeps the legacy FLOAT key policy in ordinary, fixed-index, spill, restore, and partial-merge paths. Signed zero remains equivalent, while distinct non-zero NaN payloads remain distinct for the old wire contract.
  • Modern receivers canonicalize legacy partials before membership checks. A modern partial sent to a legacy receiver is rejected before destination state is mutated.
  • The fixed-index fast path remains enabled for legacy mode; the fix does not disable the index or add per-row locks/heap allocation.
  • The planner and serializer gate the modern FLOAT/canonical DISTINCT wire format at MORPC v79, so a pre-v79 peer cannot receive the new key representation.
  • Rebased onto current main 8ab6ea6e6f53e7901729a4f0c28e5e805b75e2cc; main's v75 data-branch identity is preserved and aggregate capability versions are v76-v79.

Validation on the exact head:

  • focused normal tests: PASS
  • focused race tests: PASS
  • focused go vet: PASS
  • gofmt and git diff --check: PASS

Official CI is pending. Please re-review the current head; no approval is being asserted from the author side.

@XuPeng-SH XuPeng-SH left a comment

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.

Force deep review 结果(当前 head f4691bf):

此前 DISTINCT NaN mixed-version P2 在源码和测试中看起来已修复:aggState.go:3076-3140 在 legacy mode 保留非零 float bits(含 fixed-index key),group/exec2.go:123 在 admission 前选择 policy,spill constructors 继续传播;count2_review_test.go:163 覆盖 >=1,024 groups、noncanonical NaN wire bits、partial merge 和 spill recovery。

协议版本冲突也已修复:defines/const.go:113-118 保留 v75,并将 aggregate capabilities 分配到 v76-v79,producer/planner gates 一致。

仍需补 pinned-base old/new mixed-reader regression;当前 regression 使用 current-code readers,不能单独证明旧 reader 互操作。CI 当时仍 pending,upgrade jobs skipped。暂未发现新的 code defect,建议待上述证据和 exact-head CI 完成后再 merge。自有 PR按规则提交 COMMENT。

@XuPeng-SH

Copy link
Copy Markdown
Contributor Author

Updated head: 1af25ee7bc9a140ae4b96797a687698cdc3e2ba5 (rebased onto main 76e1a9bd9f75480ee76f686c43994f938a15a8d5).

The prior SCA failure was the unused canonicalDistinctArgumentSize; it was removed. The prior UT failure was stale v78/v79 test expectations; production keeps v78 as legacy and v79 as modern, and the tests now assert that exact boundary without changing the production gate.

Post-rebase focused aggregate/group/compile tests, legacy/modern fixed-index + spill/restore + cross-representation merge tests, and changed-package golangci-lint (0 issues) pass. I also validated the final producer wire with a reader pinned to base efaf58544c3528bfba6cc070041c9f69181b0882: 1024-group noncanonical-NaN legacy partial read/merge and spill/recovery passed without double counting. The new remote CI run is pending.

@XuPeng-SH XuPeng-SH left a comment

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.

Astra medium deep review at exact head 948403b. COMMENT ONLY (self-authored PR).\n\nNo confirmed P0/P1/P2 or code blocker. Reviewed legacy/modern aggregate-state behavior, FLOAT/NaN canonicalization, spill serialization/restore, fixed<->skiplist merge and capacity admission, capability gates, decoder failure cleanup, drain/abort/rehome, and operator teardown. No confirmed leak, double cleanup, new unbounded wait, policy loss, or flaky timing mechanism.\n\nExact-head CI: Ubuntu race UT, coverage UT, Linux/arm64 SCA, shared build, and enabled multi-CN BVTs passed; changed-code coverage 261/323 = 80.80%. Tests use explicit inputs/gate transitions without sleeps.\n\nResidual validation note: mixed-version upgrade jobs were skipped, so a real old-binary/new-binary rollout smoke test remains advisable.

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 kind/enhancement kind/test-ci size/XXL Denotes a PR that changes 2000+ lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants