Repository navigation
perf(plan): eliminate bounded aggregates grouped by primary key - #27850
Conversation
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
|
Design review record Change scope: complete #27730 grouped-aggregate finalization to bounded TopK feature.
|
|
Implementation self-review completed after the design gate. Two review findings were fixed before this update:
The complete owning-package CGo suite and focused race suite pass after both fixes. The stable 100x, count=5 benchmark shows no material regression at the multi-chunk activation boundary, while the single-chunk control now falls back. No late fallback, asynchronous lifecycle, protocol change, or alternate comparator was introduced. |
fengttt
left a comment
There was a problem hiding this comment.
Design doc and/or issue, this kind of issue should have a full example query, not just a ... group by order by limit 10.
Give an example query -- I think you already had it from the numbers, but it is much clear to read the full query, and point out where exactly the top is pushed down, from which operator to which, and where is the saving really happens.
ba49a68 to
4c1c416
Compare
Add exact chunk finalization for COUNT/SUM/MIN/MAX and a generation-safe local Group/MergeGroup-to-Top handshake. Preserve existing Top comparison semantics, fall back conservatively for unsupported or single-chunk shapes, and cover cancellation, ownership, spill/reuse, equivalence, allocation failure, race, benchmarks, and public SQL behavior.
LeftHandCold
left a comment
There was a problem hiding this comment.
Deep-reviewed exact head 0011a8e against merge-base 4dedea1. I traced the complete-PK proof through aggregate replacement, WHERE/HAVING remapping, pagination composition, ORDER BY and SQL_CALC_FOUND_ROWS barriers, and the shared PK consumers (HashOnPK, group hash keys, aggregate pullup, stats, and ONLY_FULL_GROUP_BY). I also checked signed-zero, CHAR/collation, composite/malformed PK metadata, fallible/volatile expressions, dynamic limits, overflow, and atomic fallback paths. Focused regressions and the full pkg/sql/plan suite passed through the repository CGo wrapper; go build, go vet, gofmt, and diff checks passed. All executed GitHub CI checks on this head are green. No merge-blocking correctness, compatibility, lifecycle, or performance issue found.
Closes #27730.
This PR does not close or directly optimize ClickBench Q35 in #27685:
hits.URLis not a declared unique key.What changed
A direct scan grouped by every component of its declared primary key has exactly one row per surviving group. For bounded queries this PR replaces proven singleton aggregates with row expressions, deletes the hash Aggregate, and lets existing LIMIT pushdown stop an unordered scan early.
The rule:
Resolved deep-review findings
Two candidate-only correctness regressions were found by base/candidate differential review and fixed in
c77fb38aeb:AVG(DECIMAL(38,10))could change base success intoDecimal128 scale overflowbecause the direct-cast result domain was narrower. Decimal AVG now retains Aggregate unless the complete source precision/scale domain fits the declared result type. The underlying base AVG value bug is tracked by [Bug]: AVG on high-precision DECIMAL128 silently returns an incorrect value #27855.-0.0changed later predicates because Aggregate canonicalizes signed zero to+0, while the direct cast preserved-0. Floating SUM/AVG now retain Aggregate. Floating MIN/MAX/ANY_VALUE remain eligible and were differentially verified.The BVT now consumes the wide-decimal AVG result so projection pruning cannot hide the regression. Focused tests cover exact/unsafe decimal boundaries, malformed precision/scale, signed zero, and atomic mixed fallback.
Earlier execution-time Group/Top and GROUP_CONCAT-finalization prototypes were rejected and removed: they did not remove enough work and either regressed COUNT or improved memory by only about 2%. The decision record is
docs/design/grouped_aggregate_topk_finalization.md.Validation evidence
All runtime and performance validation was run on
10.222.1.55, against one MatrixOne data directory on NVMe; only the binary changed. Final source files on that host are SHA-identical to local head5b92fe455d9c425bf3dd299f12799e51d65a6d8c.5b92fe455dchanges only tests/docs after the production fix inc77fb38aeb. Comparison base:a15139da62b938b0918c19675c619b259cd86460.Performance
A non-eligible Join plan compiled 5,000 times in about 1.14-1.17s on final versus 1.16-1.18s on base after warm-up; no planner regression was measured.
Correctness and counterexamples
pk_group_limit.sqlpassed realmo-testerwith metadata comparison three consecutive times: 15/15, 100%.pkg/sql/planpassed through the repository CGo wrapper underTZ=UTC; completemo-servicebuild passed.7a87bd6989is conflict-free.Confirmed pre-existing issues found during review do not block this PR because final behavior matches base:
CI on exact head
5b92fe455dremains a merge gate.QA test plan
Same-data A/B
mo-datais on NVMe and record commit/binary SHA, config, disk, client, andgo version -m.Correctness matrix
Compare values and metadata for:
-0.0, CHAR/VARCHAR/BINARY, and temporal values;Decimal gate: test AVG on at least DECIMAL(38,0), (38,5), (38,10), (38,12), (38,20), Decimal256, extrema, zero, and NULL. Any non-total cast domain must retain Aggregate and match base result/error behavior.
Signed-zero gate: FLOAT/DOUBLE SUM/AVG must retain Aggregate and preserve base HAVING behavior for
-0.0; MIN/MAX/ANY_VALUE should remain eligible and byte-identical.Plan assertions
Using EXPLAIN VERBOSE:
Performance gate
On at least 5M rows, use warm-ups plus seven timed runs. Record result count, median, scan rows/bytes, group memory/spill, CPU/peak RSS, and wall time for small LIMIT, LIMIT/OFFSET, selective WHERE/HAVING, ORDER BY aggregate, safe decimal AVG, floating fallback, unbounded, and unsupported controls.
Acceptance: one high-cardinality eligible case improves by at least 5x; eligible cases do not materially regress; fallback/unbounded controls retain the base plan and stay within 3% outside documented noise.
Repository gates
Focused planner tests, full
pkg/sql/planvia the CGo wrapper,pk_group_limit.sqlvia mo-tester with metadata comparison, complete build, SCA, and all required CI jobs must pass on the exact PR head.Follow-up