Skip to content

feat: support expression in window aggregate bounds#1105

Open
yongchul wants to merge 2 commits into
mainfrom
window_agg_exprs
Open

feat: support expression in window aggregate bounds#1105
yongchul wants to merge 2 commits into
mainfrom
window_agg_exprs

Conversation

@yongchul

@yongchul yongchul commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

Motivation

Like #748 in FetchRel, offset can be expressions in window spec.

Proposal

  • Extend the offset to expression, the same way we did for FetchRel.
  • Added dialect field checking whether expression is supported or not.
  • Updated documentation.

BREAKING CHANGE: the change in this PR preserves wire format and old consumers can still parse the int64 offset = 1 field produced by a new producer. Also, the offset field must be positive (i.e., the value is always present) thus makes it no difference from implicit and explicit optional.


This change is Reviewable

@nielspardon

Copy link
Copy Markdown
Member

It looks like for FetchRel the decision was to deprecate the old int64 offset field in favor of the expression field. In this PR you seem to propose we continue to support both?

@yongchul

Copy link
Copy Markdown
Contributor Author

It looks like for FetchRel the decision was to deprecate the old int64 offset field in favor of the expression field. In this PR you seem to propose we continue to support both?

I'm trying to separate the deprecation as a separate PR as discussed sometime ago. But for this one, maybe we can do that in one PR... let's see how others think.

@yongchul yongchul added the PMC Ready PRs ready for review by PMCs label Jun 16, 2026
int64 offset = 1;
// the window extends back from the current record. Required.
// Use CurrentRow for offset zero and Following for negative offsets.
oneof offset_mode {

@yongchul yongchul Jun 17, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@vbarua here is a requested SQL example for the scenario. adapted from SQL server documentation.
https://learn.microsoft.com/en-us/sql/t-sql/functions/lag-transact-sql?view=sql-server-ver17#a-compare-values-between-years

USE AdventureWorks2022;  
GO  
SELECT BusinessEntityID, YEAR(QuotaDate) AS SalesYear, SalesQuota AS CurrentQuota,   
       LAG(SalesQuota, LookBack,0) OVER (ORDER BY YEAR(QuotaDate)) AS PreviousQuota  
FROM Sales.SalesPersonQuotaHistory  
WHERE BusinessEntityID = 275 AND YEAR(QuotaDate) IN ('2005','2006');

The modification I made is LookBack in the LAG(), means you differentiate the offset per business entity id (let's say per rank of business entity id, for the senior rank look into further history). You can change LookBack to arbitrary scalar expression except another analytical function.

LookBack creates a new windowing spec including expression.

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

Labels

PMC Ready PRs ready for review by PMCs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants