Repository navigation
fix(top): order floating NaNs deterministically - #26956
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? |
XuPeng-SH
left a comment
There was a problem hiding this comment.
Blocking correctness gaps remain on exact head dc8e3b41a218b97d18dbb9007b31ddfcc778ad05:
-
[P1] The new float total order is not used by ordinary full ORDER BY.
pkg/sort.Sortstill routes FLOAT32/FLOAT64 through raw</>, where NaN is unordered. A focused counterexample on this head leaves1before NaN in ASC whileFloat64AscComparerequires NaN first. Full Sort, Top, and merge can therefore still disagree. Please wire the same float order intopkg/sortand add full-sort-versus-Top prefix tests for both widths/directions, interspersed NaNs, infinities, signed zero, and distinct NaN payloads. -
[P1] The regular-index paths only suppress the reader-side LIMIT for floats, but still rewrite the SQL Sort/Top key to the serialized hidden index key.
applyRegularIndexTopSortperforms that rewrite even whenregularIndexOrderedLimitSafeis false;handleMessageFromTopToScanhas the same shape. Keeping all candidates cannot recover correctness when Top then compares varbinary keys instead of the logical float. The packer order is observably different at NaNs (for example positive NaNs serialize after finite values, while the new ASC comparator puts all NaNs first). Skip the hidden-key ordering rewrite for unsafe float keys, or retain a logical float Sort, and cover the actual indexed SQL plan/result.
XuPeng-SH
left a comment
There was a problem hiding this comment.
Deep review completed on exact head c3be86a86be9b5a3b16f6a0af097c6aea58c5ab4.
[P1] The bounded PARTITION Top-N path still uses the non-SQL float comparator — pkg/sql/colexec/partition/topn.go:156.
This path is publicly reachable when the MORPC v19 optimization is enabled: rule.PartitionTopN annotates the PARTITION child for ROW_NUMBER() OVER (PARTITION BY ... ORDER BY ...) with a literal rn <= N bound, and compilePartition executes this heap before Window. Its orderSpecs still call compare.New, so NaN comparisons are not antisymmetric and the heap can discard rows from the true SQL-order prefix.
I reproduced it on this head through the real Partition operator with one integer partition, ORDER BY f ASC, id ASC, limit 2, and input [NaN, +Inf, 1, 0, -1, -Inf]. The expected retained ids are [10,20] (-Inf,-1); the operator retained [70,40] (NaN,1). Window’s later SortForSQLOrder cannot recover candidates already removed upstream.
The added TestWindowPartitionTopNUsesSQLOrderForFloatNaNPeers sends all candidates directly to Window, so it does not exercise this pre-reducer. Please use the SQL-order comparator for the partition Top-N order keys and add both an operator regression and a public SQL regression with a filtered row number (rn <= N), covering FLOAT/DOUBLE, ASC/DESC, secondary-key ties, and NaNs appearing before finite rows.
iamlinjunhong
left a comment
There was a problem hiding this comment.
Deep review completed on exact head c3be86a86be9b5a3b16f6a0af097c6aea58c5ab4.
Two blocking correctness gaps remain:
- [P1] Use the SQL float comparator in bounded PARTITION Top-N —
pkg/sql/colexec/partition/topn.go:156.
PartitionTopN appends the window ORDER BY expressions to the PARTITION child, and on MORPC v19 compilePartition executes this heap before Window. The heap still builds its ORDER BY comparators with compare.New, so NaN is unordered and it can discard rows that belong to the true SQL-order prefix. For example, with one integer partition, ORDER BY f ASC, id ASC, limit 2, and input beginning with NaNs before +Inf, 1, 0, -1, -Inf, the retained candidates can be NaN/finite rows instead of -Inf,-1; Window cannot recover discarded rows. TestWindowPartitionTopNUsesSQLOrderForFloatNaNPeers feeds all rows directly into Window and therefore bypasses the faulty pre-reducer. Please switch only the order-key comparators here to compare.NewOrder and add an operator/public SQL regression for ROW_NUMBER() ... WHERE rn <= N, covering both float widths and directions.
- [P1] RANGE offset frames still search float boundaries with native NaN comparisons —
pkg/sql/colexec/window/window.go:1689-1712(called frombuildRangeIntervalat line 1196).
For RANGE BETWEEN 1 PRECEDING AND CURRENT ROW, a NaN key produces a NaN target. The current genericEqual plus native </> predicates all return false for that target, so genericSearchLeft advances to the partition end. The subsequent CURRENT ROW peer lookup is then clamped to that already-empty interval. A minimal [1, NaN(payload1), NaN(payload2)] input therefore gives the NaN rows an empty frame (COUNT(*) = 0) even though the new SQL relation makes the two NaNs peers and the frame must include that current peer group (COUNT(*) = 2). The same omission affects FLOAT/DOUBLE and both directions. Please use the SQL float relation for RANGE boundary search and add offset-frame regressions with multiple NaN payloads.
aptend
left a comment
There was a problem hiding this comment.
Deep review completed on exact head c3be86a86be9b5a3b16f6a0af097c6aea58c5ab4, including all historical review comments and the full current diff. The full Sort/Top/merge/index rewrite and partitioned-window peer fixes are consistent, but two public correctness paths still bypass the new SQL float order:
-
[P1] Bounded PARTITION Top-N can discard the true SQL-order prefix —
pkg/sql/colexec/partition/topn.go:156.prepareTopNstill creates every ORDER BY comparator withcompare.New, whose FLOAT/DOUBLE branches deliberately retain native identity comparison. This operator runs before Window for the MORPC-v19ROW_NUMBER() OVER (PARTITION BY ... ORDER BY ...) ... rn <= Noptimization, so Window cannot recover candidates the heap dropped. I reproduced this through the exact operator with one integer partition,ORDER BY f ASC, id ASC, limit 2, and[NaN,+Inf,1,0,-1,-Inf]: SQL order requires ids[10,20](-Inf,-1), but this head returns[70,40](NaN,1). The added WindowPartitionTopNtest feeds all rows directly to Window and bypasses this reducer. Usecompare.NewOrderfororderSpecs(not the identity partition keys) and cover both widths/directions plus the public filtered-row-number plan. -
[P1] FLOAT/DOUBLE RANGE-offset frames can become empty for a NaN current row —
pkg/sql/colexec/window/window.go:1689-1712,2050-2075(the float branches ofsearchLeftandsearchRight). Sorting/peer detection now makes all NaNs one SQL peer group, but RANGE boundary search still usesgenericEqualand native</>. ForRANGE BETWEEN 1 PRECEDING AND CURRENT ROW, a NaN current value produces a NaN target; all three native relations are false and the left search advances to the partition end. A focused exact-head control over sorted[1, NaN(payload1), NaN(payload2)]expected interval[1,3)for the first NaN and got start3, yielding an empty frame instead of both current peers. Apply the same SQL float relation to both boundary searches and add FLOAT/DOUBLE, ASC/DESC offset-frame regressions.
Validation: both focused counterexamples fail exactly as described with the required CGO/thirdparties setup; the current required GitHub checks are green; and git diff --check is clean.
|
Addressed in
Validation after the merge: all owning packages ( |
|
Added public distributed coverage in |
|
@XuPeng-SH @iamlinjunhong @aptend All P1 findings from the |
aptend
left a comment
There was a problem hiding this comment.
Deep re-review completed on exact head 5a8b7cd, including the full review/comment history, the incremental fixes since c3be86a86a, and the full current diff. The two prior P1 correctness findings are resolved: bounded PARTITION Top-N now applies compare.NewOrder only to its order-key comparators, and FLOAT/DOUBLE RANGE searches use the same SQL relation in both directions. The focused affected-package tests all pass with the required CGO/thirdparties setup.
One blocking test issue remains:
[P1] Regenerate the multiline BVT result commands — test/distributed/cases/optimizer/float_nan_order.result:82
The two newly added filtered ROW_NUMBER commands retain two-space indentation on their inner SELECT/FROM lines in the result file, but mo-tester canonicalizes the SQL command by stripping that leading whitespace. On this exact head, the required PROXY BVT fails at ResultParser before executing the file: it reports the command at float_nan_order.sql:51 “does not exist in result file,” then marks all 19 statements ABNORMAL. The second new command has the same mismatch. Please regenerate or normalize both result entries so this public regression actually executes and the required check is green.
|
@aptend Fixed in a98e58b (current head). The two filtered ROW_NUMBER result entries now match mo-tester’s canonical command text with no leading indentation on the inner SELECT/FROM lines. I validated the exact mo-tester ResultParser against the case: 19 commands, success=true. The latest upstream/main was merged before the push; the new Compose/coverage checks are running on a98e58b. Please re-review the current head. |
aptend
left a comment
There was a problem hiding this comment.
Deep re-review completed on exact head a98e58b, including the full review/comment history, the incremental changes since 5a8b7cd, and the full current diff. The prior bounded PARTITION Top-N and FLOAT/DOUBLE RANGE-frame P1s remain correctly closed, and the final BVT change now matches mo-tester canonical multiline command text; the current PROXY BVT is green. Focused NaN/order/index/window race tests (3x) and the complete ten affected Go packages pass with the required CGo environment; git diff --check is clean.
XuPeng-SH
left a comment
There was a problem hiding this comment.
Deep re-review of head a98e58b: approved.
All previously reported blocking paths are now closed: full Sort uses the SQL float relation; Window peer/range and bounded partition Top-N use the same relation; later ORDER BY keys correctly break NaN peers; and regular-index order/limit/cursor/TopValue rewrites are conservatively disabled for logical float keys while retaining the index as an access path.
I re-enumerated the sort/comparator/partition call sites and found no remaining SQL ordering consumer using the legacy float identity relation. The relation keeps NaN last in both directions, treats all NaN payloads and signed zero as peers, and leaves NULL handling independent.
Independent validation passed: 19 focused regressions under -race, the complete 12-package affected test set, go list/build/vet, and diff whitespace checks. The exact remote head was rechecked before approval.
Merge Queue Status
This pull request spent 49 seconds in the queue, with no time running CI. Required conditions to merge
ReasonThe pull request #26956 has been manually updated Tick the box to put this pull request back in the merge queue (same as
|
What type of PR is this?
Which issue(s) this PR fixes:
issue #26941
What this PR does / why we need it:
Defines one explicit SQL ordering relation for scalar
FLOATandDOUBLE:-Infinity < finite values < +Infinity < NaN+Infinity > finite values > -Infinity, with NaN still last-0and+0are ordering peersNaN 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.
Validation
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