Skip to content

[4.2] fix(top): order floating NaNs deterministically - #27532

Merged
mergify[bot] merged 1 commit into
matrixorigin:4.2-devfrom
aunjgr:backport/26956-4.2-dev
Aug 24, 2026
Merged

mergify[bot] merged 1 commit into
matrixorigin:4.2-devfrom
aunjgr:backport/26956-4.2-dev

Conversation

@aunjgr

@aunjgr aunjgr commented Aug 24, 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:

issue #26941

Backport of #26956 to 4.2-dev.

What this PR does / why we need it:

Backports the explicit SQL ordering relation for scalar FLOAT and DOUBLE:

  • ASC: -Infinity < finite values < +Infinity < NaN
  • DESC: +Infinity > finite values > -Infinity, with NaN still last
  • all NaN payloads are ordering peers, allowing later ORDER BY keys to break ties
  • -0 and +0 are ordering peers
  • NULL placement remains controlled independently by NULLS FIRST/LAST

The 4.2 full-sort, Top, MergeTop, MergeOrder, and window/range paths use this SQL relation. Generic comparison and storage sorting keep their existing contracts. Regular secondary indexes remain access paths for float keys, but cannot replace the logical sort or drive early LIMIT because their encoded NaN ordering is not SQL-equivalent.

Release-branch adaptation:

  • 4.2-dev does not contain main's PartitionTopN reducer/operator contract, so those two source/test files and their PartitionTopN-only window cases are intentionally omitted rather than resurrecting a removed subsystem.
  • The 4.2 window operator receives only ORDER BY vectors after partitioning; the backport applies SQL ordering directly to that release-specific shape.
  • 4.2 plan types do not carry the newer charset field, so MergeOrder preserves its existing type construction and changes only the comparator entry point.

Validation:

  • full tests pass for pkg/container/types, pkg/compare, pkg/partition, pkg/sort, pkg/sql/colexec/order, top, mergetop, mergeorder, window, aggexec, and pkg/sql/plan
  • affected packages pass go build and go vet against 4.2's locally built CGo/third-party artifacts
  • git diff --check passes

(cherry picked from commit 99f4da5a092895fad6123a1862cf5e067e89a397)

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

issue matrixorigin#26941

Defines one explicit SQL ordering relation for scalar `FLOAT` and
`DOUBLE`:

- ASC: `-Infinity < finite values < +Infinity < NaN`
- DESC: `+Infinity > finite values > -Infinity`, with NaN still last
- all NaN payloads are ordering peers, so later ORDER BY keys break
their ties
- `-0` and `+0` are ordering peers
- NULL placement remains controlled independently by the existing NULLS
FIRST/LAST flags

NaN and infinities remain stored values. This change does not convert
NaN to NULL,
change scalar predicate semantics, or migrate physical/storage ordering.

The SQL full-sort, Top, MergeTop, MergeOrder, and window peer-partition
paths now
share this relation. Generic comparison and storage sorting retain their
existing
contracts through separate entry points.

Regular secondary-index hidden keys are not proven to have the same
ordering at
NaN boundaries. For a logical float sort key, the optimizer therefore
keeps the
logical Sort and suppresses hidden-key ORDER BY substitution,
cursor-range rewrite,
reader-side LIMIT, and TopValue messaging. The index can still be used
as an access
path.

- focused named regressions for both float widths, infinities, signed
zero, multiple NaN payloads, NULL placement, secondary-key tie breaking,
Top/MergeTop/MergeOrder, window rank peers, and planner typed state
- public SQL regression:
`test/distributed/cases/optimizer/float_nan_order.sql`
- `.agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -timeout=240s
./pkg/container/types ./pkg/compare ./pkg/partition ./pkg/sort
./pkg/sql/colexec/order ./pkg/sql/colexec/top ./pkg/sql/colexec/mergetop
./pkg/sql/colexec/mergeorder ./pkg/sql/colexec/window`
- `.agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -timeout=300s
./pkg/sql/colexec/aggexec ./pkg/sql/plan`

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Co-authored-by: XuPeng-SH <[email protected]>
(cherry picked from commit 99f4da5)
@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 →

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

已基于 4.2-dev 的当前 base 05f8a29 审查 head 1bdf8a6 的完整 diff、相关执行/优化调用链和测试。未发现 P0/P1/P2/P3 问题。

重点核对了:

  • FLOAT/DOUBLE 的 SQL 排序关系在 full Sort、Top、MergeTop、MergeOrder、ordered aggregate 与 window peer/RANGE 路径中的一致性;
  • 通用比较与物理/存储排序仍保留原有契约;
  • 二级索引隐藏键、reader LIMIT、cursor range 与 TopValue 在 float 排序键上的保守禁用;
  • 4.2 分支独立 PARTITION 节点下的 window backport 适配;
  • NaN payload、±Inf、signed zero、NULL、次级排序键及索引访问回归覆盖。

当前 27 个 checks 均已结束,无失败项。隔离 worktree 未重新生成本机缺失的 libmo/third-party native artifacts,因此没有把本机非代表性的 CGo 编译作为额外通过证据。

@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 against exact head 1bdf8a624fdcf8e4003d2118c3fe44e050f677e6 (base 05f8a2938df56e8911701fe0276a37a0b2832f22). No blocking correctness, completeness, unhappy-path, or performance issue found.

Verified the explicit scalar FLOAT/DOUBLE SQL order is a transitive total preorder with NaNs last in both directions, all NaN payloads and signed zero as ordering peers, and NULL placement remaining independent. The relation is consistently wired through full Sort, Top, MergeTop, MergeOrder, multi-key peer partitioning, window rank/RANGE boundaries, and the 4.2-specific window shape. Generic/storage ordering remains unchanged. Planner handling conservatively keeps the logical float sort and disables hidden-key substitution, cursor/reader early LIMIT, and TopValue messaging while retaining secondary indexes as access paths.

Validation at this exact head:

  • git diff --check: pass
  • full affected package tests: pkg/container/types, pkg/compare, pkg/partition, pkg/sort, order, top, mergetop, mergeorder, window, aggexec, and pkg/sql/plan: pass
  • focused NaN/float-order tests under -race -count=5: pass
  • go vet for all affected packages: pass

Hot-path impact is bounded to scalar float SQL ordering comparisons (no allocation or unbounded state); integer/non-SQL/storage sort paths are unchanged.

@mergify mergify Bot added the queued label Aug 24, 2026
@mergify

mergify Bot commented Aug 24, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-08-24 08:15 UTC · Rule: release-4.2 · triggered by rule Automatic queue on approval for release-4.2
  • ✅ Checks skipped · PR is already up-to-date
  • ✅ Merged — 2026-08-24 08:16 UTC · at 1bdf8a624fdcf8e4003d2118c3fe44e050f677e6 · squash

This pull request spent 41 seconds in the queue, including 3 seconds running CI.

Required conditions to merge
  • #review-threads-unresolved = 0 [🛡 GitHub branch protection]
  • github-review-approved [🛡 GitHub branch protection]
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / SCA Test on Linux/arm64
    • check-neutral = Matrixone CI / SCA Test on Linux/arm64
    • check-skipped = Matrixone CI / SCA Test on Linux/arm64
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Utils CI / Coverage
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
    • check-neutral = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)

@mergify
mergify Bot merged commit 399e1a1 into matrixorigin:4.2-dev Aug 24, 2026
27 of 28 checks passed
@mergify mergify Bot removed the queued label Aug 24, 2026

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

kind/bug Something isn't working kind/enhancement kind/refactor Code refactor kind/test-ci size/XL Denotes a PR that changes [1000, 1999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants