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

[Windowing] Handle multiple sort expressions #142

Merged
merged 1 commit into from
Jan 30, 2024
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
47 changes: 24 additions & 23 deletions internal/function_window_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,32 +406,33 @@ func (s *WindowFuncAggregatedStatus) Done(cb func([]Value, int, int) error) erro
sortedValues := make([]*WindowOrderedValue, len(values))
copy(sortedValues, values)
if len(sortedValues) != 0 {
for orderBy := 0; orderBy < len(sortedValues[0].OrderBy); orderBy++ {
isAsc := sortedValues[0].OrderBy[orderBy].IsAsc
if isAsc {
sort.Slice(sortedValues, func(i, j int) bool {
if sortedValues[i].OrderBy[orderBy].Value == nil {
return true
}
if sortedValues[j].OrderBy[orderBy].Value == nil {
return false
}
cond, _ := sortedValues[i].OrderBy[orderBy].Value.LT(sortedValues[j].OrderBy[orderBy].Value)
sort.Slice(sortedValues, func(i, j int) bool {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We should share this implementation with function_aggregate.go

Copy link
Owner

Choose a reason for hiding this comment

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

sort.SliceStable not available here ? ( https://pkg.go.dev/sort#SliceStable )

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could use a stable sort, I don't think it'd make any difference.

The previous implementation was re-sorting the entire list for each ORDER BY expression that was provided. The new implementation maintains sort order within each group.

i.e. 2024-02-01 dt group in the test below has its rows sorted by letter when the dt fields are equal.

Copy link
Owner

Choose a reason for hiding this comment

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

OK, It's good. thank you.

for orderBy := 0; orderBy < len(sortedValues[0].OrderBy); orderBy++ {
iV := sortedValues[i].OrderBy[orderBy].Value
jV := sortedValues[j].OrderBy[orderBy].Value
isAsc := sortedValues[0].OrderBy[orderBy].IsAsc
if iV == nil {
return true
}
if jV == nil {
return false
}
isEqual, _ := iV.EQ(jV)
if isEqual {
// break tie with subsequent fields
continue
}
if isAsc {
cond, _ := iV.LT(jV)
return cond
})
} else {
sort.Slice(sortedValues, func(i, j int) bool {
if sortedValues[i].OrderBy[orderBy].Value == nil {
return true
}
if sortedValues[j].OrderBy[orderBy].Value == nil {
return false
}
cond, _ := sortedValues[i].OrderBy[orderBy].Value.GT(sortedValues[j].OrderBy[orderBy].Value)
} else {
cond, _ := iV.GT(jV)
return cond
})
}
}
}
return false
})

}
s.SortedValues = sortedValues
start, err := s.getIndexFromBoundary(s.Start)
Expand Down
30 changes: 30 additions & 0 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1612,6 +1612,36 @@ FROM finishers`,
{"Suzy Slane", "03:06:24", "F35-39", "Nobody"},
},
},
{
name: `window order by`,
query: `WITH toks AS (
SELECT DATE '2024-01-01' AS dt, 'c' AS letter
UNION ALL SELECT DATE '2024-02-01', 'b'
UNION ALL SELECT DATE '2024-02-01', 'c'
UNION ALL SELECT DATE '2024-03-01', 'a'
)
SELECT dt, letter, ROW_NUMBER() OVER (ORDER BY dt, letter) AS rn FROM toks
`,
expectedRows: [][]interface{}{
{"2024-01-01", "c", int64(1)},
{"2024-02-01", "b", int64(2)},
{"2024-02-01", "c", int64(3)},
{"2024-03-01", "a", int64(4)},
},
},
{
name: `window order by handles nil`,
query: `WITH toks AS (
SELECT DATE '2024-01-01' AS dt, 'c' AS letter
UNION ALL SELECT DATE '2024-02-01', null
)
SELECT dt, letter, ROW_NUMBER() OVER (ORDER BY dt, letter) AS rn FROM toks
`,
expectedRows: [][]interface{}{
{"2024-01-01", "c", int64(1)},
{"2024-02-01", nil, int64(2)},
},
},
{
name: `percentile_cont`,
query: `
Expand Down
Loading