From a080c9e4e5aa4c5ab73517b42adb6cdac1615561 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Wed, 15 May 2024 04:03:05 +0200 Subject: [PATCH] wip --- lib/logstorage/parser.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/lib/logstorage/parser.go b/lib/logstorage/parser.go index 9d4e76e09..b5e00033e 100644 --- a/lib/logstorage/parser.go +++ b/lib/logstorage/parser.go @@ -217,9 +217,32 @@ func (q *Query) AddPipeLimit(n uint64) { // Optimize tries optimizing the query. func (q *Query) Optimize() { + q.pipes = optimizeSortOffsetPipes(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 { // Merge 'sort ... | limit ...' into 'sort ... limit ...' i := 1