This commit is contained in:
Aliaksandr Valialkin 2024-05-15 04:03:05 +02:00
parent 12fe2b265c
commit a080c9e4e5
No known key found for this signature in database
GPG key ID: 52C003EE2BCDB9EB

View file

@ -217,9 +217,32 @@ func (q *Query) AddPipeLimit(n uint64) {
// Optimize tries optimizing the query. // Optimize tries optimizing the query.
func (q *Query) Optimize() { func (q *Query) Optimize() {
q.pipes = optimizeSortOffsetPipes(q.pipes)
q.pipes = optimizeSortLimitPipes(q.pipes) q.pipes = optimizeSortLimitPipes(q.pipes)
} }
func optimizeSortOffsetPipes(pipes []pipe) []pipe {
// Merge 'sort ... | offset ...' into 'sort ... offset ...'
i := 1
for i < len(pipes) {
po, ok := pipes[i].(*pipeOffset)
if !ok {
i++
continue
}
ps, ok := pipes[i-1].(*pipeSort)
if !ok {
i++
continue
}
if ps.offset == 0 && ps.limit == 0 {
ps.offset = po.n
}
pipes = append(pipes[:i], pipes[i+1:]...)
}
return pipes
}
func optimizeSortLimitPipes(pipes []pipe) []pipe { func optimizeSortLimitPipes(pipes []pipe) []pipe {
// Merge 'sort ... | limit ...' into 'sort ... limit ...' // Merge 'sort ... | limit ...' into 'sort ... limit ...'
i := 1 i := 1