Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: fix window functions with GROUPS offset FOLLOWING #37222

Merged
merged 1 commit into from
Apr 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/window
Original file line number Diff line number Diff line change
Expand Up @@ -3049,3 +3049,11 @@ query T
SELECT string_agg('foo', s) OVER () FROM (SELECT * FROM kv LIMIT 1)
----
foo

# Regression test for #37201.
query I
SELECT jsonb_agg(a) OVER (ORDER BY a GROUPS BETWEEN 5 FOLLOWING AND UNBOUNDED FOLLOWING) FROM x
----
NULL
NULL
NULL
2 changes: 1 addition & 1 deletion pkg/sql/sem/builtins/window_builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func newFramableAggregateWindow(
agg tree.AggregateFunc, aggConstructor func(*tree.EvalContext, tree.Datums) tree.AggregateFunc,
) tree.WindowFunc {
return &framableAggregateWindowFunc{
agg: &aggregateWindowFunc{agg: agg},
agg: &aggregateWindowFunc{agg: agg, peerRes: tree.DNull},
aggConstructor: aggConstructor,
}
}
Expand Down
15 changes: 1 addition & 14 deletions pkg/sql/sem/tree/window_funcs_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (p *PeerGroupsIndicesHelper) Init(wfr *WindowFrameRun, peerGrouper PeerGrou
p.peerGrouper = peerGrouper
startIdxOfFirstPeerGroupWithinFrame := 0
if wfr.Frame != nil && wfr.Frame.Mode == GROUPS && wfr.Frame.Bounds.StartBound.BoundType == OffsetFollowing {
// In GROUPS mode with OFFSET_PRECEDING as a start bound, 'peerGroupOffset'
// In GROUPS mode with OFFSET_FOLLOWING as a start bound, 'peerGroupOffset'
// number of peer groups needs to be processed upfront before we get to
// peer groups that will be within a frame of the first row.
// If start bound is of type:
Expand Down Expand Up @@ -207,13 +207,6 @@ func (p *PeerGroupsIndicesHelper) Update(wfr *WindowFrameRun) error {
// GetFirstPeerIdx returns index of the first peer within peer group of number
// peerGroupNum (counting from 0).
func (p *PeerGroupsIndicesHelper) GetFirstPeerIdx(peerGroupNum int) int {
if p.allPeerGroupsSkipped {
// Special case: we have skipped all peer groups in Init, so the frame is
// always empty. It happens only with frames like GROUPS 100 FOLLOWING
// which (if we have less than 100 peer groups total) behaves exactly like
// GROUPS UNBOUNDED FOLLOWING (if it were allowed).
return p.unboundedFollowing
}
posInBuffer := peerGroupNum - p.headPeerGroupNum
if posInBuffer < 0 || p.groups.Len() < posInBuffer {
panic("peerGroupNum out of bounds")
Expand All @@ -224,12 +217,6 @@ func (p *PeerGroupsIndicesHelper) GetFirstPeerIdx(peerGroupNum int) int {
// GetRowCount returns the number of rows within peer group of number
// peerGroupNum (counting from 0).
func (p *PeerGroupsIndicesHelper) GetRowCount(peerGroupNum int) int {
if p.allPeerGroupsSkipped {
// Special case: we have skipped all peer groups in Init, so the frame is
// always empty. It happens only with frames like GROUPS 100 FOLLOWING
// if we have less than 100 peer groups total.
return 0
}
posInBuffer := peerGroupNum - p.headPeerGroupNum
if posInBuffer < 0 || p.groups.Len() < posInBuffer {
panic("peerGroupNum out of bounds")
Expand Down