Skip to content

[3.0-dev] fix lag/lead/value window functions losing partition info - #23972

Closed
ck89119 wants to merge 1 commit into
matrixorigin:3.0-devfrom
ck89119:fix-lag-lead-partition-3.0-dev
Closed

ck89119 wants to merge 1 commit into
matrixorigin:3.0-devfrom
ck89119:fix-lag-lead-partition-3.0-dev

Conversation

@ck89119

@ck89119 ck89119 commented Mar 27, 2026

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • Bug fix

Which issue(s) does this PR fix?

Fixes #23969

What this PR does

Cherry-pick of #23971 to 3.0-dev.

processOrder was clearing ctr.ps (partition boundaries) at the end of the method. When processValueFunc (which handles lag/lead/first_value/last_value/nth_value) was called afterwards, ctr.ps was already nil, so all rows were treated as a single partition.

The fix removes the erroneous ctr.ps = nil at the end of processOrder. The cleanup of ctr.ps is already handled in processFunc after the value function results are computed.

…in#23969)

processOrder was clearing ctr.ps at the end, causing processValueFunc
to treat all rows as a single partition. Remove the erroneous ctr.ps = nil
since cleanup is already handled in processFunc.
@ck89119
ck89119 requested a review from a user March 27, 2026 08:13
@matrix-meow matrix-meow added the size/M Denotes a PR that changes [100,499] lines label Mar 27, 2026
@mergify
mergify Bot requested a review from XuPeng-SH March 27, 2026 08:14
@mergify mergify Bot added the kind/bug Something isn't working label Mar 27, 2026
@ck89119
ck89119 requested a review from Copilot March 27, 2026 08:14

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Fixes an execution-order bug in the window operator where LAG/LEAD/FIRST_VALUE/LAST_VALUE/NTH_VALUE could lose PARTITION BY boundaries after sorting, causing all rows to be treated as one partition.

Changes:

  • Stop clearing ctr.ps (partition boundaries) at the end of processOrder, so value window functions can still read partition info.
  • Add distributed SQL test cases covering lag/lead offsets, defaults, and PARTITION BY + ORDER BY.
  • Update expected results to include the new test outputs.

Reviewed changes

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

File Description
pkg/sql/colexec/window/window.go Preserves partition boundary state across processOrder → value window function evaluation.
test/distributed/cases/window/value_window_functions.sql Adds regression tests reproducing the lag/lead partition bug.
test/distributed/cases/window/value_window_functions.result Updates golden outputs for the new regression coverage.

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

@ck89119 ck89119 linked an issue Mar 27, 2026 that may be closed by this pull request
1 task done
@ck89119 ck89119 closed this Apr 1, 2026
@ck89119
ck89119 deleted the fix-lag-lead-partition-3.0-dev branch April 30, 2026 07:57

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 size/M Denotes a PR that changes [100,499] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: result of window function with lag() and lead() is incorrect

3 participants