Skip to content

perf: add cost-based hash partition for windows - #27972

Merged
XuPeng-SH merged 21 commits into
matrixorigin:mainfrom
iamlinjunhong:m-27943
Sep 4, 2026
Merged

XuPeng-SH merged 21 commits into
matrixorigin:mainfrom
iamlinjunhong:m-27943

Conversation

@iamlinjunhong

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 #27943

What this PR does / why we need it:

perf: add cost-based hash partition for windows

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

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

Reviewed the mandatory design gate for exact head 5167b2c07d23c6f0d521bd864653f94b088c20e9 against base/merge-base 91bfff2cd296850185e5b943a9d53fdf89c1b124. This is a large R3 feature, not an ordinary local optimization: it adds a new blocking execution algorithm and memory/fallback lifecycle, changes planner/compile/operator topology across more than five owning packages, adds plan and pipeline wire fields for mixed-version operation, and materially changes a hot path. I read the complete 307-line design, issue #27943 and its full timeline, the PR's complete review/thread history, commit provenance, changed-package inventory, validation plan, and benchmark record.

The design content usefully states semantic/cardinality invariants, equality guards, coordinator ownership, one-way pre-output fallback, memory accounting, zero-value wire compatibility, alternatives, rollout, and validation. However, the mandatory design-first approval itself is not traceable. The document is authored and marked approved in the same implementation branch, still says Implementation PR: TBD, and the tracking issue contains only the author's benchmark comment. This PR currently has zero reviews/threads, and the design commit is not associated with a separately reviewed PR. A self-written “design review record” cannot satisfy the required independent design decision.

Because design approval must precede implementation review for this scope, I am stopping the implementation approval path at the design gate. Please obtain and link a traceable review/approval of an exact design revision, update the document's implementation-PR/revision record, and then request re-review; the implementation will need to be checked against that approved revision. This is a mandatory delivery/architecture gate rather than a speculative runtime finding.

Comment thread docs/design/window_hash_partition.md Outdated
@@ -0,0 +1,307 @@
# Cost-based hash partitioning for ordinary window functions

- Status: design approved for implementation

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] This “approved” state has no traceable independent design review. The scope triggers the mandatory design-first gate via a new blocking executor lifecycle/memory fallback, planner→compile→operator topology across 5+ owning packages, plan/pipeline wire fields, and a material hot path. On this exact head, GitHub reports zero PR reviews or threads; issue #27943 contains only the author's benchmark comment; the design was authored inside this implementation branch and still says Implementation PR: TBD. Please link an exact reviewed design revision and its reviewer approval (or run a distinct design-first phase here and record that decision) before implementation approval. Self-declaring the document approved cannot close the gate.

@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 review completed on exact head 9c2eff0267417f7aae34c8e9d16e7066072e7454 against merge-base 3a85c337154c710997c26080c629784217a53f27. I read the complete design and implementation closure, including planner selection, compile topology, protobuf compatibility, operator lifecycle, fallback, Window consumer behavior, tests, benchmarks, previous review, issue history, and exact-head CI. The architecture direction is plausible, and the zero-value wire gate plus ordinary hash grouping path are generally coherent, but the mandatory design/implementation gate still has blockers.

  1. [P1][liveness] Runtime fallback can ignore cancellation for an unbounded global sort. The reachable path is stale/underestimated statistics -> planner selects HASH -> actual growth crosses SpillMem -> all remaining rows are retained -> finalizeSortFallback enters sort.SliceStable at pkg/sql/colexec/partition/hash.go:352. A KILL/cancel arriving after the pre-finalize check is not observed until the full O(N log N * K) sort completes. Unlike the legacy local Order path, which sorts bounded runs, this fallback sorts the complete coordinator input and is triggered specifically for large/memory-mispredicted inputs. Make fallback cancellation-aware or otherwise bounded, and add a deterministic cancel-during-finalize regression rather than the current test that cancels before finalization starts.

  2. [P1][correctness evidence] No test proves the automatically selected HASH path reaches and preserves the real Window contract. The new tests exercise a synthetic Partition operator and a hand-built planner tree, but none drives SQL/planner -> HASH selection -> compile/merge -> Window for aggregate, ranking, value, ROWS/RANGE, with/without ORDER BY, or the multi-scope ownership case. Existing green BVT is not selection evidence because no case asserts Hash Partition; therefore it can remain entirely on SORT. Add an exact-selected public/integration regression with semantic result oracles and nearby SORT controls.

  3. [P1][performance gate] The PR has not supplied the benchmark evidence required by its own cost model/design. BenchmarkWindowPartitionAlgorithms fixes N=65,536, one INT32 key and no actual Window consumer. The issue result is from 7d14462147, while current hot-path code changed afterward, including retained-memory accounting in dca0c0e0d2e9. There is no exact-head 1K/64K/1M, low/high NDV, one/three key, fixed/varlen, ordered/unordered, fallback, or multi-scope matrix, nor peak-memory evidence. Since AUTO selection and the constants are the feature, this evidence is a merge requirement, not optional follow-up.

  4. [P2][design/operations] The exact design revision still contradicts delivery. Section 5 says CHAR is allowed, while partitionhash.Compatible and its tests explicitly reject T_char; it also says the compatibility predicate is shared with Partition Top-N, but partitionTopNHashCompatible remains separate. Section 11 says operator statistics account hash growth, while consume reports only retained-batch deltas and never publishes hash/row-index memory to the analyzer. Reconcile the document and implementation, provide the missing observability, and record an independently approved exact design revision before implementation approval.

Evidence reused: exact-head Linux UT, build, SCA, coverage and substantive BVT checks are green. Those checks establish compile/regression health but do not close the missing selected-path, cancellation, or performance proofs above.

@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-review of exact head dc687f0 against base fa829e5: APPROVE for the gated implementation only.

I rechecked the full design and implementation from planner admission through compile topology, protobuf transport, coordinator ownership, hash equality, fallback, final materialization, Window consumption, Reset/Free, and the post-approval v47 rebase. Automatic selection remains hard-disabled, so this does not route production queries to HASH; enabling it still requires a separate exact-head real-Window latency and peak-memory acceptance review.

Unhappy-path audit:

  • Q1 ownership: retained input, evaluated key vectors, hash table, group IDs/boundaries, selection/sort scratch, unpublished materialization, and borrowed output each have a terminal Reset/Free or deferred error owner. Publication occurs only after complete shuffle.
  • Q2 waits/liveness: no new goroutine/channel wait graph is introduced. Receive, hash chunks, group counting, stable merge sort, tail copies, and materialization all poll cancellation in bounded units.
  • Q3 growth: the blocking whole-input requirement is explicit; actual hash growth triggers one-way pre-output SORT fallback, hash-only state is freed immediately, and later input is rejected before retention when it would exceed the row/byte threshold. Peak overlap is accounted. Empty input, allocation/expression failure, fallback, cancellation, and reuse are covered.

The v47 compile gate restores both local Order operators and coordinator SORT for predecessor peers. Other open v47 proposals are serialized by mandatory branch update after the first owner merges; this approval becomes stale if another owner lands first. Exact-head CI is terminal green. No new correctness, performance-within-the-disabled-rollout, compatibility, leak, OOM-growth, or hang blocker found.

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

Re-reviewed the actual current head bdc043a6bb62ab21e22882db7cbe75007fc21e0f after the late merge from main. No blocker for the gated implementation.

The merge's combined conflict-resolution hunks are confined to unrelated data-branch files; none touches the window hash design, protocol allocation, plan/pipeline fields, planner selection, compile topology, partition/hash operators, or BVT. The complete effective feature patch before and after the merge has the same stable patch-id (c40f0c6a8e72db435d51d75b74aef2fbd56962af). Current base owns subscription metadata at v46 and this feature remains the unique branch-local v47 capability with v46 fallback restoring local Order plus coordinator SORT.

The prior full review therefore remains valid: AUTO is hard-disabled; correctness covers NULL/grouping/CHAR/equality domains and complete Window consumption; Q1 — retained input, evaluated keys, hash/index/group IDs, sort/materialization scratch, and borrowed output have terminal owners; Q2 — receive, grouping, fallback sort/copy, and final materialization poll cancellation without adding a goroutine/channel wait graph; Q3 — hash growth has one-way pre-output bounded SORT fallback, subsequent over-budget input is rejected, and peak overlap is accounted. Enabling AUTO still requires a separate exact-head real-Window latency and peak-memory review. CI is running; per request I did not wait.

This approval becomes stale if another open v47 owner merges first; normal mandatory branch update must then reallocate/retest the next cumulative protocol version.

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

size/XXL Denotes a PR that changes 2000+ lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants