Skip to content

fix lag/lead/value window functions losing partition info - #23971

Closed
ck89119 wants to merge 1 commit into
matrixorigin:mainfrom
ck89119:fix-lag-lead-partition
Closed

ck89119 wants to merge 1 commit into
matrixorigin:mainfrom
ck89119:fix-lag-lead-partition

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

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.

Non-value window functions (rank, row_number, etc.) were not affected because processFunc rebuilds ctr.ps internally for those cases.

BVT

Added test cases covering:

  • lag/lead with various offsets (1, 3, 5)
  • lag/lead with default values
  • lag/lead with partition by + order by

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

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 a bug in the window operator where value window functions (LAG/LEAD/FIRST_VALUE/LAST_VALUE/NTH_VALUE) could lose PARTITION BY boundaries after ORDER processing, causing rows to be treated as a single partition.

Changes:

  • Stop clearing ctr.ps (partition boundaries) at the end of processOrder, so subsequent value window function evaluation can use it.
  • Add distributed SQL test coverage for LAG/LEAD with multiple offsets, default values, and PARTITION BY + ORDER BY.
  • Update expected results accordingly.

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 computed partition boundaries across processOrder → processFunc for value window functions.
test/distributed/cases/window/value_window_functions.sql Adds regression tests for LAG/LEAD offsets/defaults and partitioned evaluation.
test/distributed/cases/window/value_window_functions.result Records expected outputs for the new regression cases.

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

@ck89119 ck89119 closed this Apr 1, 2026
@ck89119
ck89119 deleted the fix-lag-lead-partition 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

4 participants