Skip to content

fix: eliminate O(n²) memory hotspot in value window functions#23963

Merged
mergify[bot] merged 8 commits intomatrixorigin:mainfrom
ck89119:fix/value-window-memory-hotspot
Mar 26, 2026
Merged

fix: eliminate O(n²) memory hotspot in value window functions#23963
mergify[bot] merged 8 commits intomatrixorigin:mainfrom
ck89119:fix/value-window-memory-hotspot

Conversation

@ck89119
Copy link
Copy Markdown
Contributor

@ck89119 ck89119 commented Mar 26, 2026

What type of PR is this?

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

Which issue(s) this PR fixes:

issue #23962

What this PR does / why we need it:

Extract lag/lead/first_value/last_value/nth_value from the AggFuncExec/Fill/Flush path and compute results directly via index lookup in the window operator, eliminating O(n²) frame materialization.

Key changes:

  • Add a dedicated WIN_VALUE branch in processFunc(), calling processValueFunc() for direct index-based evaluation
  • lag/lead: O(n) partition-based index calculation with offset and default value support
  • first_value/last_value: direct frame boundary lookup
  • nth_value: proper n-th element semantics (previously broken, defaulting to first_value)
  • Fix valueWindowExec.Free() to actively clean up frameValues/currentRowPosition
  • Fix valueWindowExec.Size() to account for slice headers, backing arrays and struct overhead
  • Add 14 unit tests covering partition boundaries, explicit frames, offset/default params, and varlen types

…ad/first_value/last_value/nth_value)

Bypass AggFuncExec Fill/Flush path for WIN_VALUE functions. Instead,
compute results directly via index lookup in the window operator:

- lag/lead: O(n) partition-based index calculation with offset/default support
- first_value/last_value: direct frame boundary lookup
- nth_value: proper n-th element semantics (was broken, defaulting to first_value)

Also fix valueWindowExec.Free() to actively clear frameValues/currentRowPosition,
and fix Size() to account for slice headers, backing arrays and struct overhead.
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 7 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/sql/colexec/window/window.go Outdated
Comment thread pkg/sql/colexec/window/value_window_test.go
Comment thread pkg/sql/colexec/window/window.go Outdated
Comment thread pkg/sql/colexec/window/window.go Outdated
Comment thread pkg/sql/colexec/window/window.go Outdated
Comment thread pkg/sql/colexec/window/window.go
Comment thread pkg/sql/colexec/window/window.go Outdated
- Support non-const offset/n vectors (read per-row for lag/lead/nth_value)
- Remove float type support from getInt64FromVec (only integer types)
- Add uint64 overflow check (> MaxInt64 returns ok=false)
- Shuffle all agg argument vectors in processOrder (not just Vec[0])
- Fix tests to use const vectors for offset/n parameters
- Use unsafe.Sizeof instead of hardcoded struct sizes in valueWindowExec.Size()
- Add bat.Clean(mp) and mpool leak assertions to processValueFunc tests
@mergify
Copy link
Copy Markdown
Contributor

mergify Bot commented Mar 26, 2026

Merge Queue Status

  • Entered queue2026-03-26 15:56 UTC · Rule: main
  • Checks passed · in-place
  • Merged2026-03-26 16:56 UTC · at ff08f6e436a490179a1dd83cbf1fd2311bc624c0

This pull request spent 59 minutes 57 seconds in the queue, including 58 minutes 54 seconds running CI.

Required conditions to merge
  • #approved-reviews-by >= 1 [🛡 GitHub branch protection]
  • #changes-requested-reviews-by = 0 [🛡 GitHub branch protection]
  • #review-threads-unresolved = 0 [🛡 GitHub branch protection]
  • branch-protection-review-decision = APPROVED [🛡 GitHub branch protection]
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
    • 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)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
    • 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)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • 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)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / SCA Test on Ubuntu/x86
    • check-neutral = Matrixone CI / SCA Test on Ubuntu/x86
    • check-skipped = Matrixone CI / SCA Test on Ubuntu/x86
  • 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 Compose CI / multi cn e2e bvt test docker compose(Optimistic/PUSH)
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(Optimistic/PUSH)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(Optimistic/PUSH)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH,Optimistic)
    • check-neutral = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH,Optimistic)
    • check-skipped = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH,Optimistic)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Upgrade CI / Compatibility Test With Target on Linux/x64(LAUNCH)
    • check-neutral = Matrixone Upgrade CI / Compatibility Test With Target on Linux/x64(LAUNCH)
    • check-skipped = Matrixone Upgrade CI / Compatibility Test With Target on Linux/x64(LAUNCH)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Utils CI / Coverage
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/enhancement 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