Skip to content

fix(sql): honor first ordering key in Top and MergeTop - #33042

Merged
XuPeng-SH merged 1 commit into
matrixorigin:mainfrom
XuPeng-SH:codex/32396-top-order
Nov 21, 2026
Merged

XuPeng-SH merged 1 commit into
matrixorigin:mainfrom
XuPeng-SH:codex/32396-top-order

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:

Fixes #32396. Related to #32897 item 1 and the 4.2-dev fix #32402.

What this PR does / why we need it:

With repeated physical columns in ORDER BY, resident Top and heap MergeTop configured the shared comparator from the last occurrence. This could reverse a LIMIT result and give scan pruning a cutoff inconsistent with the leading key. Resident Top and heap MergeTop now use the first ordering specification for each physical slot, including its NULL placement.

Spill Top stores one vector per logical sort key. Direct spill and resident-to-spill migration now give each logical key an independent comparator. This also prevents repeated keys from aliasing mutable comparator bindings during migration. The change adds no work to row comparison and avoids building a physical-slot map on direct spill.

Validation

  • On the exact main base, 13 of 16 independent SQL checks returned wrong rows. The candidate matched all 17 checks, covering directions, NULLs, aliases, intervening keys, OFFSET, pruning on and off, SQL PREPARE and binary prepared reuse.
  • An isolated two-CN probe verified remote execution and actual Top spill. EXPLAIN ANALYZE reported 40,001 spilled rows and 318.13 KiB in the spilling Top.
  • Both owning Go packages passed normal and race suites. Regression checks cover repeated keys, migration bindings, cutoff publication, Reset and reuse, and NULL placement.
  • The leading-key-pruning BVT passed 94/94 statements. Incremental gofmt, diff check, err-check, vet, golangci-lint, and molint passed.

@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 21, 2026 •

Copy link
Copy Markdown
Contributor

This pull request is merged.

@XuPeng-SH

XuPeng-SH commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor Author

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

Commit: f34c94bcda34d305048aef92b6e1fecdc885dbb9

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: f34c94bcda34d305048aef92b6e1fecdc885dbb9

Assessment: approve

Top and MergeTop now use the first ordering specification for each shared column slot. Spill Top gives each logical key its own comparator, preserving tie-breakers and keeping scan cutoffs consistent with the leading key.

Model: gpt-6.2-sol / xhigh

Session: 01a1f0a0-5126-78f3-9ab8-ef4e87f2f119 · slot 0 · generation 3

Validation:

  • The full Top and MergeTop Go suites passed. The repeated-key tests returned wrong rows against the base operators, while the unique-key control passed.

  • An isolated single-CN MySQL probe passed both added SQL cases and binary prepared reuse across the spill threshold. EXPLAIN ANALYZE showed Top spilling 17,000 rows and 136.19 KiB.

  • The fixture oracle, isolation preflight, native input verification, and formatting checks passed. Receipts and logs are indexed in review-evidence.md.

Limitations:

  • The full BVT, race suites, and remote-CN probe are author-reported. The frozen unit and multi-CN CI checks were pending.

  • The frozen evidence references the related 4.2-dev PR #32402 without including its patch.

@XuPeng-SH
XuPeng-SH merged commit 6d45e4b into matrixorigin:main Nov 21, 2026
@XuPeng-SH
XuPeng-SH deleted the codex/32396-top-order branch November 21, 2026 17:02
fengttt added a commit to fengttt/matrixone that referenced this pull request Nov 21, 2026
main now carries the squashed forms of the PRs this branch was stacked on,
through matrixorigin#33027. The merged tree is this branch's own commits
(4de8e4e..1cc5610) replayed onto main, with three conflicts and one
semantic follow-up resolved:

- top: processBatchResident takes main's ordering specs (matrixorigin#33042) and keeps
  this branch's retained-pool prologue.
- external/reader_zonemap: main's rewritten saved-result reader (matrixorigin#33045)
  writes output vectors, including filled missing columns, through the
  operator's output pool; the loaded block stays on proc's pool.
- group/collation_metadata_test: main's form of the same offset fix.
- external/reader_zonemap_test (matrixorigin#33046): the capacity-failure cases bind
  an accounted output view instead of a per-vector allocation account, and
  pre-size the first column so the failure still lands on the second.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

This branch was previously deployed

1 inactive deployment
ci — f34c94bc Deployed Nov 21, 2026 by XuPeng-SH
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/M Denotes a PR that changes [100,499] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: ORDER BY with a repeated column and LIMIT uses the last sort direction

2 participants