Skip to content

fix(window): prevent overflow in value window offsets#25918

Merged
XuPeng-SH merged 4 commits into
matrixorigin:mainfrom
VioletQwQ-0:codex/issue-25906-lead-offset-overflow
Jul 22, 2026
Merged

fix(window): prevent overflow in value window offsets#25918
XuPeng-SH merged 4 commits into
matrixorigin:mainfrom
VioletQwQ-0:codex/issue-25906-lead-offset-overflow

Conversation

@VioletQwQ-0

Copy link
Copy Markdown
Collaborator

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

Fixes #25906

What this PR does / why we need it:

LEAD previously added a BIGINT offset to the current row before checking the partition boundary. INT64_MAX therefore overflowed on later rows and silently read earlier values.

This PR:

  • compares LAG/LEAD offsets with the available partition distance before converting or adding/subtracting them;
  • applies the same bounded-index rule to NTH_VALUE positions;
  • adds focused unit/race coverage for maximum int64 values;
  • adds a distributed SQL regression for LEAD(..., 9223372036854775807).

Validation:

  • go test ./pkg/sql/colexec/window -count=1
  • go test -race ./pkg/sql/colexec/window -run '^(TestProcessValueFunc_LeadWithMaxInt64Offset|TestProcessValueFunc_NthValueWithMaxInt64Position)$' -count=1
  • go vet ./pkg/sql/colexec/window
  • go build ./pkg/sql/colexec/window
  • git diff --check

@mergify mergify Bot added the kind/bug Something isn't working label Jul 20, 2026
@matrix-meow matrix-meow added the size/S Denotes a PR that changes [10,99] lines label Jul 20, 2026
@VioletQwQ-0
VioletQwQ-0 marked this pull request as ready for review July 20, 2026 17:07
@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 LAG/LEAD distance checks are sound, but the NTH_VALUE branch still trusts overflowed ROWS frame bounds before applying its new bounded-index check. A concrete reproducer is attached inline.

Comment thread pkg/sql/colexec/window/window.go

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

Codex automated review

NTH_VALUE still has a silent wrong-result overflow path in ROWS frames.

P1 - Bound ROWS frame offsets before trusting the NTH_VALUE frame length (pkg/sql/colexec/window/window.go:548)

buildInterval can return overflowed ROWS bounds before this check: ROWS BETWEEN 9223372036854775807 FOLLOWING AND UNBOUNDED FOLLOWING on three rows computes rowIdx + int(following), which overflows for rows 1 and 2. Clamping then changes the negative bound to partition start 0, so this check sees a three-row frame and returns row 0 (10), although the frame must be empty and the result NULL. Saturate/validate ROWS-bound arithmetic before clamping and add this regression case.

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

Codex automated review

The prior blocking ROWS-frame overflow is fixed on the current head, and no functional regression remains. Current CI is green; local tests were blocked by missing prebuilt CGo artifacts. One non-blocking regression-test gap remains.

P2 - Make the NTH_VALUE test reach the overflow path (pkg/sql/colexec/window/value_window_test.go:566)

With three rows and ROWS BETWEEN 1 PRECEDING AND CURRENT ROW, left is at most 1, so the old left + MaxInt64 - 1 calculation never overflows and this test passes before the fix. Using four rows makes left == 2 on the last row: the old target wraps to MinInt64 and UnionOne can incorrectly read row 0, while the new length check returns NULL. The implementation is correct, so this is non-blocking, but the regression should exercise the bug it is intended to prevent.

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

Codex automated review

The earlier review on this head contains only non-blocking findings. Approving.

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

Codex automated review

No findings. The prior ROWS-frame overflow review thread is resolved and independently validated against the current head; bounds are checked before conversion/arithmetic. Regression coverage reaches the original LEAD, NTH_VALUE, and frame-bound cases. Local Go tests could not run because the environment has no Go toolchain; current PR CI is green.

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

I checked the overflow fix from correctness / robustness / unhappy-path angles and I did not find a blocker.

The core arithmetic changes look right:

  • lead now checks the partition-local distance before doing j + offset, so the overflowed wraparound path is gone.
  • nth_value now checks n > right-left before computing left + n - 1, which removes the target-row overflow.
  • buildRowsInterval now saturates huge ROWS ... PRECEDING/FOLLOWING bounds to the partition edges instead of overflowing signed int arithmetic.

The new unit + SQL regression coverage is enough for the bug being fixed.

Optional follow-up only: it would still be nice to add a couple more regressions for non-const huge offsets / huge nth_value, but I do not see that as a merge blocker here.

@aunjgr aunjgr 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 current head independently. The LAG/LEAD/NTH_VALUE index checks compare offsets before converting them to int, and ROWS frame bounds saturate against partition limits before arithmetic. Boundary tests cover MaxInt64 offsets and overflowing FOLLOWING frames. I found no merge-blocking correctness, lifecycle, or compatibility issue.

@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 on exact head 7ea630b: no blocking findings.

I rechecked the full arithmetic closure rather than relying on the prior approval. LAG/LEAD compare partition-local distance before int conversion/arithmetic; NTH_VALUE checks frame length before computing the target row; all four finite ROWS-bound directions saturate at partition edges before signed arithmetic.

Fresh validation:

  • full pkg/sql/colexec/window tests: pass
  • focused overflow tests under race, count=10: pass
  • full window package under race: pass
  • go build and go vet: pass
  • exact-head SQL with constant and non-constant offsets, defaults, non-zero partition starts, MaxInt64 and MaxUint64, plus huge PRECEDING/FOLLOWING frames: correct

I also ran the same SQL shapes on the exact PR base. The base returned earlier rows for huge LEAD, FIRST_VALUE empty-frame, and four-row NTH_VALUE cases, while the PR head returned NULL/default as required. This confirms the regressions exercise real pre-fix failures. Q1-Q3 review found no new resource, wait, or growth risks.

@mergify mergify Bot added the queued label Jul 22, 2026
@mergify

mergify Bot commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • Entered queue2026-07-22 06:38 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • 🟠 Checks running · in-place
  • 🚫 Left the queue2026-07-22 08:17 UTC · at f2bc7d47ff6d05ba5241294f3a10320c0762fba6

This pull request spent 1 hour 38 minutes 22 seconds in the queue, with no time running CI.

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

Reason

Pull request #25918 has been dequeued

Pull request from fork cannot be queued. This pull request comes from a fork, and Mergify needs the author's permission to update its branch.

The author needs to enable "Allow edits from maintainers" on this pull request.

Failing checks:

Hint

You should look at the reason for the failure and decide if the pull request needs to be fixed or if you want to requeue it.
If you do update this pull request, it will automatically be requeued once the queue conditions match again.
If you think this was a flaky issue instead, you can requeue the pull request, without updating it, by posting a @mergifyio queue comment.

Tick the box to put this pull request back in the merge queue (same as @mergifyio queue).

  • Requeue this pull request

@XuPeng-SH
XuPeng-SH merged commit 1ce49be into matrixorigin:main Jul 22, 2026
23 of 24 checks passed
@mergify mergify Bot added dequeued and removed queued labels Jul 22, 2026
@VioletQwQ-0
VioletQwQ-0 deleted the codex/issue-25906-lead-offset-overflow branch July 22, 2026 08:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dequeued 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]: LEAD with INT64_MAX offset returns wrong rows due to integer overflow

6 participants