Skip to content

[4.2-dev] perf: backport COUNT(DISTINCT) optimizations (#27693, #28431) - #32646

Merged
XuPeng-SH merged 4 commits into
matrixorigin:4.2-devfrom
XuPeng-SH:codex/backport-27693-28431-42
Nov 13, 2026
Merged

XuPeng-SH merged 4 commits into
matrixorigin:4.2-devfrom
XuPeng-SH:codex/backport-27693-28431-42

Conversation

@XuPeng-SH

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:

Related to #27672.

Backports #27693 (dfef35f7dd94de507d40dd18082b64e7f4b84e40) and #28431 (98171c28dbeb7ad1291ce9b0eb5beff7a84cbc32) together to 4.2-dev.

What this PR does / why we need it:

On 4.2, mixed aggregates containing COUNT(DISTINCT) are forced through one worker/CN, while fixed-width DISTINCT values use ordered skiplist lookups and repeated arena rebuilding. This adapts the two source optimizations to the existing 4.2 aggregate owners:

  • Allow COUNT-only DISTINCT to merge across workers/CNs, cost retained distinct arguments when selecting group shuffle, and bypass normal shuffle only when there is one physical group owner. Other DISTINCT aggregates and ordered GROUP_CONCAT retain their conservative execution path.
  • Add an exact fixed-width (group, value) index and direct merge with MPool-backed arrays, finite probing, checked growth, coherent partial-error state, and deterministic cleanup. Keep the existing aggregate wire format and legacy representation fallback.
  • Include the prerequisites needed on this branch: cached-Inserter successor equality, safe arena relocation/growth, signed-zero canonicalization, and state reuse after Free. Restrict JOIN-to-GROUP shuffle reuse to INNER joins so NULL extension cannot split one logical group between owners.

The versioned design records the ownership, compatibility, failure and resource contracts. The design-only phase was approved at 98387de8fc82d834188df5d42eb84d057cedeecc before implementation. Final implementation: a717091e1d8437911f8e90a17463d18ea668ee5a against base db0a7e76881bba94c2a86504ed8e3b2463fc6835.

Validation: Linux amd64, Go 1.26.4, CGO, GOAMD64=v1, GOMAXPROCS=4; native libraries built in this worktree. Repository mo-cgo-test wrapper, -mod=readonly -p=2 -parallel=2 -short -tags=matrixone_test -count=1:

  • Full arena, aggregate execution, Group, planner and compiler packages passed normally and under -race; the planner retains three existing unrelated skipped subtests. The final compiler runs selected 1,514 tests/subtests each with no skips.
  • Real three-CN TestDistinctBackportMultiCN passed normally and under -race: exact optimized/no-shuffle result equality, executed shuffle on multiple CNs, DOP 1, duplicate/NULL/tuple and non-COUNT controls, and LEFT JOIN NULL-group correctness.
  • Deterministic failure controls reproduce the missing cached-successor check and unsafe outer-join reuse; corrected tests pass. Coverage includes fixed/legacy merges, cross-chunk group identity, old wire readers, signed zeros/NaN payloads, allocation failure, partial merge state and cleanup/reuse.
  • Configured golangci-lint 2.6.2: zero issues across the six affected packages/consumers. Configured molint vet: exit 0, no introduced warnings (nine existing recover diagnostics match the base). Formatting and git diff --check passed.
  • Compiler Parquet fixture failures were traced to hidden .codex in GOTMPDIR, which the existing file enumerator skips. Both full compiler modes passed after setting TMPDIR and GOTMPDIR to a nonhidden disk directory; no tests were skipped or unrelated source changed.

Paired local executor benchmark, identical 65,536 values/128 groups, -benchmem -benchtime=3x -count=3 (median):

Operation 4.2 base This PR
fill 46.86 ms 4.99 ms
merge 16 states 43.10 ms 4.29 ms

This is a small warm microbenchmark on a shared host, not a production SQL latency claim. Go allocations fell from roughly 65–68k to 17 per operation; benchmem excludes MPool off-heap memory.

Branch limits: DISTINCT spill and later-main aggregate preflight/accounting are outside this backport. Existing operator-pool/shared-global allocation limits remain authoritative; parallel non-shuffle states and a live merge destination can increase peak memory. No per-query or tenant-isolated memory bound is added. Upgrade remains a full shutdown.

Review: gpt-6.1-terra with max reasoning approved the design before implementation and the complete committed change at a717091e1d8437911f8e90a17463d18ea668ee5a before push. Final review: PASS, no unresolved material blocker. Base-to-head binary diff SHA-256: 90694283dab3dd1847d4a7e13d095027f5dc14934c64b782899896ed4f4b1c22.

GitHub CI has not been verified; local validation does not imply CI success.

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

@mergify

mergify Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

This pull request is merged.

@XuPeng-SH

XuPeng-SH commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor Author

mo-bot review · mo-bot-local · done

Commit: a717091e1d8437911f8e90a17463d18ea668ee5a

Review completed. Read the assessment.

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

mo-bot review · mo-bot-local · review

Commit: a717091e1d8437911f8e90a17463d18ea668ee5a

Assessment: approve

The backport extends the existing aggregate, serialization and shuffle owners coherently. Exact key/log/count state, cleanup and legacy encoding are preserved; the fixed index is justified by the reported component gains while the skiplist retains unsupported and ordered cases. No actionable findings remain within the documented 4.2 memory and spill limits.

Model: gpt-6.1-sol / max

Session: 01a1a004-9884-72c0-ad39-f096c4209c4a · slot 1 · generation 0

Validation:

  • Reviewed the complete 17-file delta, versioned design and design-phase adjustment, all discussion entries, linked issue context, and producer/consumer/error/cleanup paths. Verified the reported binary diff hash, clean frozen source, gofmt and git diff --check.

  • Private-runtime preflight passed. Qualified native inputs and artifacts, verified task caches and Go 1.26.4 Linux amd64/v1 CGO environment, and reconciled receipts with retained logs.

  • Arena and aggregate packages passed under -race: 16 and 258 test roots respectively, with no skips. All 14 new arena/aggregate behavioral roots passed 100 adaptive race repetitions each, with no failures or race reports.

  • TestDistinctBackportMultiCN passed on the real three-CN fixture: executed multi-CN shuffle, exact no-shuffle result equality, DOP-one, NULL/duplicate/tuple, outer-join NULL-group and non-COUNT controls.

  • Reused the author's exact-head normal/race planner, compiler and Group results, configured lint and paired component benchmark reports within their stated scope.

Limitations:

  • Frozen build, UT, SCA, coverage and Iceberg checks were pending; these are not passes.

  • Broader planner/compiler/Group runs, multi-CN race execution, lint and performance measurements were reported without raw local_validation artifacts and were not repeated locally. The warm executor benchmark does not establish production or full ClickBench Q10 latency.

  • This backport retains the existing DISTINCT-spill limitation and operator-pool/shared-global memory caps. Parallel states plus a live merge destination can increase peak memory; no per-query or tenant-isolated memory bound is added.

@XuPeng-SH

XuPeng-SH commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor Author

mo-bot review · mo-bot-local · superseded

Commit: 85d603b1b23af625eb60a6d8db8c0e8ccb569749

"interrupted"

@XuPeng-SH
XuPeng-SH merged commit 94dd363 into matrixorigin:4.2-dev Nov 13, 2026
@XuPeng-SH
XuPeng-SH deleted the codex/backport-27693-28431-42 branch November 13, 2026 01:19

This branch was successfully deployed

No deployments
ci — 85d603b1 Deployed Nov 13, 2026 by XuPeng-SH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/enhancement size/XXL Denotes a PR that changes 2000+ lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants