VictoriaMetrics/lib/logstorage/pipe_extract.go

159 lines
3.7 KiB
Go
Raw Normal View History

2024-05-20 02:08:30 +00:00
package logstorage
import (
"fmt"
)
2024-05-22 19:01:20 +00:00
// pipeExtract processes '| extract ...' pipe.
2024-05-20 02:08:30 +00:00
//
// See https://docs.victoriametrics.com/victorialogs/logsql/#extract-pipe
type pipeExtract struct {
fromField string
2024-05-22 19:01:20 +00:00
2024-05-24 22:30:58 +00:00
ptn *pattern
2024-05-22 19:01:20 +00:00
patternStr string
2024-05-20 02:08:30 +00:00
2024-05-24 22:30:58 +00:00
keepOriginalFields bool
skipEmptyResults bool
2024-05-22 19:01:20 +00:00
// iff is an optional filter for skipping the extract func
iff *ifFilter
2024-05-20 02:08:30 +00:00
}
func (pe *pipeExtract) String() string {
s := "extract"
2024-05-22 19:01:20 +00:00
if pe.iff != nil {
s += " " + pe.iff.String()
}
s += " " + quoteTokenIfNeeded(pe.patternStr)
2024-05-20 02:08:30 +00:00
if !isMsgFieldName(pe.fromField) {
s += " from " + quoteTokenIfNeeded(pe.fromField)
}
2024-05-24 22:30:58 +00:00
if pe.keepOriginalFields {
s += " keep_original_fields"
}
if pe.skipEmptyResults {
s += " skip_empty_results"
}
2024-05-20 02:08:30 +00:00
return s
}
func (pe *pipeExtract) updateNeededFields(neededFields, unneededFields fieldsSet) {
if neededFields.contains("*") {
unneededFieldsOrig := unneededFields.clone()
needFromField := false
2024-05-22 19:01:20 +00:00
for _, step := range pe.ptn.steps {
2024-05-20 02:08:30 +00:00
if step.field != "" {
if !unneededFieldsOrig.contains(step.field) {
needFromField = true
}
2024-05-24 22:30:58 +00:00
if !pe.keepOriginalFields && !pe.skipEmptyResults {
unneededFields.add(step.field)
}
2024-05-20 02:08:30 +00:00
}
}
if needFromField {
unneededFields.remove(pe.fromField)
2024-05-22 19:01:20 +00:00
if pe.iff != nil {
unneededFields.removeFields(pe.iff.neededFields)
}
2024-05-20 02:08:30 +00:00
} else {
unneededFields.add(pe.fromField)
}
} else {
2024-05-22 19:01:20 +00:00
neededFieldsOrig := neededFields.clone()
2024-05-20 02:08:30 +00:00
needFromField := false
2024-05-22 19:01:20 +00:00
for _, step := range pe.ptn.steps {
if step.field != "" && neededFieldsOrig.contains(step.field) {
2024-05-20 02:08:30 +00:00
needFromField = true
2024-05-24 22:30:58 +00:00
if !pe.keepOriginalFields && !pe.skipEmptyResults {
neededFields.remove(step.field)
}
2024-05-20 02:08:30 +00:00
}
}
if needFromField {
neededFields.add(pe.fromField)
2024-05-22 19:01:20 +00:00
if pe.iff != nil {
neededFields.addFields(pe.iff.neededFields)
}
2024-05-20 02:08:30 +00:00
}
}
}
func (pe *pipeExtract) newPipeProcessor(workersCount int, _ <-chan struct{}, _ func(), ppBase pipeProcessor) pipeProcessor {
2024-05-22 19:01:20 +00:00
patterns := make([]*pattern, workersCount)
for i := range patterns {
patterns[i] = pe.ptn.clone()
2024-05-20 02:08:30 +00:00
}
2024-05-22 19:01:20 +00:00
unpackFunc := func(uctx *fieldsUnpackerContext, s string) {
ptn := patterns[uctx.workerID]
ptn.apply(s)
for _, f := range ptn.fields {
uctx.addField(f.name, *f.value)
2024-05-20 02:08:30 +00:00
}
}
2024-05-24 22:30:58 +00:00
return newPipeUnpackProcessor(workersCount, unpackFunc, ppBase, pe.fromField, "", pe.keepOriginalFields, pe.skipEmptyResults, pe.iff)
2024-05-20 02:08:30 +00:00
}
func parsePipeExtract(lex *lexer) (*pipeExtract, error) {
if !lex.isKeyword("extract") {
return nil, fmt.Errorf("unexpected token: %q; want %q", lex.token, "extract")
}
lex.nextToken()
2024-05-22 19:01:20 +00:00
// parse optional if (...)
var iff *ifFilter
if lex.isKeyword("if") {
f, err := parseIfFilter(lex)
2024-05-20 02:08:30 +00:00
if err != nil {
2024-05-22 19:01:20 +00:00
return nil, err
2024-05-20 02:08:30 +00:00
}
2024-05-22 19:01:20 +00:00
iff = f
2024-05-20 02:08:30 +00:00
}
2024-05-22 19:01:20 +00:00
// parse pattern
patternStr, err := getCompoundToken(lex)
2024-05-20 02:08:30 +00:00
if err != nil {
return nil, fmt.Errorf("cannot read 'pattern': %w", err)
}
2024-05-22 19:01:20 +00:00
ptn, err := parsePattern(patternStr)
2024-05-20 02:08:30 +00:00
if err != nil {
2024-05-22 19:01:20 +00:00
return nil, fmt.Errorf("cannot parse 'pattern' %q: %w", patternStr, err)
2024-05-20 02:08:30 +00:00
}
2024-05-22 19:01:20 +00:00
// parse optional 'from ...' part
fromField := "_msg"
if lex.isKeyword("from") {
lex.nextToken()
f, err := parseFieldName(lex)
if err != nil {
return nil, fmt.Errorf("cannot parse 'from' field name: %w", err)
2024-05-20 02:08:30 +00:00
}
2024-05-22 19:01:20 +00:00
fromField = f
2024-05-20 02:08:30 +00:00
}
2024-05-24 22:30:58 +00:00
keepOriginalFields := false
skipEmptyResults := false
switch {
case lex.isKeyword("keep_original_fields"):
lex.nextToken()
keepOriginalFields = true
case lex.isKeyword("skip_empty_results"):
lex.nextToken()
skipEmptyResults = true
}
2024-05-22 19:01:20 +00:00
pe := &pipeExtract{
2024-05-24 22:30:58 +00:00
fromField: fromField,
ptn: ptn,
patternStr: patternStr,
keepOriginalFields: keepOriginalFields,
skipEmptyResults: skipEmptyResults,
iff: iff,
2024-05-20 02:08:30 +00:00
}
2024-05-22 19:01:20 +00:00
return pe, nil
2024-05-20 02:08:30 +00:00
}