Skip to content

fix: stabilize Top typed-NULL layout on 4.1.6-hotfix - #32239

Merged
XuPeng-SH merged 2 commits into
matrixorigin:4.1.6-hotfixfrom
ck89119:issue-top-panic-4.1.6-hotfix
Nov 5, 2026
Merged

XuPeng-SH merged 2 commits into
matrixorigin:4.1.6-hotfixfrom
ck89119:issue-top-panic-4.1.6-hotfix

Conversation

@ck89119

@ck89119 ck89119 commented Nov 5, 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 matrixorigin/matrixflow#20795

What this PR does / why we need it:

Backport the stable Top ordering layout from #30714 to 4.1.6-hotfix. Direct-column sort keys use their planned slots instead of vector pointer identity, preventing typed-NULL batches from changing the retained column layout and causing an index-out-of-range panic.

Also clear stale borrowed data/area references when Vector.UnmarshalBinary reads empty payloads. The spill regression reproduced this independent wrong-result bug on the unmodified hotfix baseline: a NULL row could retain the previous batch's value. Encoding and ownership contracts are unchanged.

Preserve the first ordering specification for repeated columns in resident Top and MergeTop; spill Top uses each ordering position's own flags. This closes the pre-existing comparator defect identified during review: ORDER BY x DESC, x ASC must not behave as ascending order. The fix covers direction and NULL-placement flags without changing operator lifecycle or adding a new fixture.

Validation (Go 1.26.4, branch-local native libraries):

  • Reproduced the Top panic and six vector-reuse failures before their respective fixes.
  • All 24 new repeated-key UT scenarios failed on the previous PR head and pass after the comparator fix, covering ordinary/typed-NULL input, opposite directions, explicit NULL placement, resident/spill Top and MergeTop.
  • Top and MergeTop package tests passed; changed comparator coverage blocks are 100% covered. Earlier Vector/Batch evidence remains valid because their inputs are unchanged.
  • Incremental formatting/static checks and service build passed.
  • SQL BVT passed twice on the same owned instance: 46/46 each, including order-sensitive comparison and cleanup checks.
  • A query-owned EXPLAIN ANALYZE confirmed three-row spilling for the repeated-key query; results were 2, 1, NULL.

Spill integration tests do not assume map-driven reload order; the Vector reuse UT provides the deterministic empty-payload transition oracle. Validation uses minimal reproductions, not an independent replay of the customer's complete executed statement. No production deployment was performed.

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

The planned-column layout is a focused fix for typed-NULL batch drift, but one correctness case in the affected Top / UNION / NULL path still returns the wrong row. The comparator defect described inline already exists on the base branch; it is not introduced by this PR. Please close it as part of the affected sorting contract before merging.

Validation on the CI-built binary for 5501d9224f14a4ca20d7f76e7808b9f02ea0a84b: reproduced the wrong result in both resident and spill Top; the PR SQL regression passed 38/38 in two runs, including one with result metadata comparison. The reported customer's complete executed statement was not independently replayed.

Non-blocking test note: the spill cases' input order does not guarantee disk reload order because evalSpill iterates over a map. The vector reuse unit test provides the deterministic empty-payload transition check; the Top cases are still useful integration coverage.

Comment thread pkg/sql/colexec/top/top.go Resolved

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

The previous repeated-order-key finding is fixed for this hotfix: resident Top and MergeTop preserve the first specification, while spill Top uses the specification for each ordering position. The typed-NULL layout and vector reuse changes remain focused and reuse the existing execution and ownership paths. No remaining code-review blockers were found on this PR's head.

Evidence for 7f99d53b9ceeea954216778ede22ee4c011da238:

  • The exact-head CI race run executed and passed all 24 new Top and MergeTop repeated-key subtests; the Vector reuse and typed-NULL layout tests also passed.
  • The multi-CN CI SQL regression executed 46/46 statements successfully.
  • An independent test on the CI-built binary passed repeated keys separated by another column or a computed expression, in resident and spill modes. EXPLAIN ANALYZE confirmed four spilled rows.

CI remains a merge gate: the first full UT run failed on a port-32002 bind conflict and two separate timeouts; the second run is still in progress. This approval is for the code review and does not claim that the required CI has passed.

Non-blocking cross-branch follow-up: main 007dd57a6192a6fb7348e45a973fb784d2d5ae67 still contains the same last-specification-wins logic in Top and MergeTop. On a CI-built binary whose source tree is identical to that main revision, SELECT NULL AS x UNION ALL SELECT x FROM t ORDER BY x DESC, x ASC LIMIT 1 returned NULL for t = {1,2} instead of 2; the large-LIMIT spill case was also wrong. Please track a separate main-branch fix and check other maintained branches, reusing the first-specification semantics and existing regressions. This issue predates and is outside the target branch of this PR.

The customer's complete executed SQL was not independently replayed.

@XuPeng-SH
XuPeng-SH merged commit 9bde0b0 into matrixorigin:4.1.6-hotfix Nov 5, 2026
@ck89119
ck89119 deleted the issue-top-panic-4.1.6-hotfix branch November 5, 2026 16:30
XuPeng-SH added a commit that referenced this pull request Nov 9, 2026
## What type of PR is this?

- [ ] API-change
- [x] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

Fixes #32396

## What this PR does / why we need it:

`ORDER BY b DESC, b ASC LIMIT 2` could return the smallest rows because
Top and MergeTop configured the comparator for a repeated physical
column from the last ordering specification. The first specification
determines the order; a later occurrence of the same value cannot break
ties left by the first.

Preserve the first specification when the resident Top or MergeTop
comparator is shared by a physical column. Spill Top stores a separate
vector per logical key, so construct each comparator from its
corresponding specification. Expression evaluation, logical key order,
row ownership, and the leading-key scan-pruning protocol are unchanged.
This ports the corresponding comparator fix from #32239 to `4.2-dev`
while using this branch's ordering comparator.

Validation:

- Reproduced the original and reverse-direction failures on the base and
confirmed the new Top/MergeTop regression tests fail there. The fixed
version returns the correct ordered rows for both directions, NULL
placement, offset, repeated keys separated by another key, and real
spill writes/reads, including a full-heap replacement past the spill
threshold.
- Ran both affected Go packages in normal and `-race` modes, plus
incremental `gofmt`, `go vet`, repository `molint`, and `golangci-lint`
on the complete Go delta.
- Ran the existing UNION/typed-NULL and leading-key-pruning SQL cases
twice each on an isolated two-CN instance: 41/41 and 63/63 statements
per run. A physical plan confirmed local Top on both CNs and coordinator
MergeTop. An independent 36-case comparison against MySQL 8.0.44 matched
exact ordered rows across resident, spill, NULL, offset, computed-key,
and execution-mode controls.
- The reported pruning witness now returns the same correct answer with
the message enabled or disabled; `EXPLAIN ANALYZE` confirmed one input
block with pruning and two without.
- Interleaved local A/B runs on 100,000 identical rows showed median
changes of +0.4% and -3.0% for non-repeated and repeated-leading-key
resident Top (30 pairs), and -0.1% and +0.9% for their spill
counterparts (8 pairs). These small shared-host samples cannot exclude
small regressions. Existing Go benchmarks showed comparable allocations.
No per-row work is added; the spill path no longer builds the position
map.

This branch had an error being deployed

1 failed deployment
ci — 7f99d53b Deployed Nov 5, 2026 by ck89119
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/L Denotes a PR that changes [500,999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants