VictoriaMetrics/lib/logstorage/pipe_first.go
Aliaksandr Valialkin ad6c587494
lib/logstorage: properly propagate extra filters to all the subqueries
The purpose of extra filters ( https://docs.victoriametrics.com/victorialogs/querying/#extra-filters )
is to limit the subset of logs, which can be queried. For example, it is expected that all the queries
with `extra_filters={tenant=123}` can access only logs, which contain `123` value for the `tenant` field.

Previously this wasn't the case, since the provided extra filters weren't applied to subqueries.
For example, the following query could be used to select all the logs outside `tenant=123`, for any `extra_filters` arg:

    * | union({tenant!=123})

This commit fixes this by propagating extra filters to all the subqueries.

While at it, this commit also properly propagates [start, end] time range filter from HTTP querying APIs
into all the subqueries, since this is what most users expect. This behaviour can be overriden on per-subquery
basis with the `options(ignore_global_time_filter=true)` option - see https://docs.victoriametrics.com/victorialogs/logsql/#query-options

Also properly apply apply optimizations across all the subqueries. Previously the optimizations at Query.optimize()
function were applied only to the top-level query.
2025-01-24 18:49:25 +01:00

60 lines
1.3 KiB
Go

package logstorage
import (
"fmt"
)
// pipeFirst processes '| first ...' queries.
//
// See https://docs.victoriametrics.com/victorialogs/logsql/#first-pipe
type pipeFirst struct {
ps *pipeSort
}
func (pf *pipeFirst) String() string {
return pipeLastFirstString(pf.ps)
}
func (pf *pipeFirst) canLiveTail() bool {
return false
}
func (pf *pipeFirst) updateNeededFields(neededFields, unneededFields fieldsSet) {
pf.ps.updateNeededFields(neededFields, unneededFields)
}
func (pf *pipeFirst) hasFilterInWithQuery() bool {
return false
}
func (pf *pipeFirst) initFilterInValues(_ *inValuesCache, _ getFieldValuesFunc) (pipe, error) {
return pf, nil
}
func (pf *pipeFirst) visitSubqueries(_ func(q *Query)) {
// nothing to do
}
func (pf *pipeFirst) newPipeProcessor(workersCount int, stopCh <-chan struct{}, cancel func(), ppNext pipeProcessor) pipeProcessor {
return newPipeTopkProcessor(pf.ps, workersCount, stopCh, cancel, ppNext)
}
func (pf *pipeFirst) addPartitionByTime(step int64) {
pf.ps.addPartitionByTime(step)
}
func parsePipeFirst(lex *lexer) (pipe, error) {
if !lex.isKeyword("first") {
return nil, fmt.Errorf("expecting 'first'; got %q", lex.token)
}
lex.nextToken()
ps, err := parsePipeLastFirst(lex)
if err != nil {
return nil, err
}
pf := &pipeFirst{
ps: ps,
}
return pf, nil
}