VictoriaMetrics/lib/logstorage/pipe_extract.go

179 lines
3.8 KiB
Go
Raw Normal View History

2024-05-19 02:24:32 +00:00
package logstorage
import (
"fmt"
2024-05-19 10:56:08 +00:00
"unsafe"
2024-05-19 02:24:32 +00:00
)
2024-05-19 13:11:17 +00:00
// pipeExtract processes '| extract from <field> <pattern>' pipe.
2024-05-19 10:56:08 +00:00
//
// See https://docs.victoriametrics.com/victorialogs/logsql/#extract-pipe
type pipeExtract struct {
2024-05-19 11:47:30 +00:00
fromField string
2024-05-20 10:43:46 +00:00
steps []patternStep
2024-05-19 10:56:08 +00:00
2024-05-19 13:11:17 +00:00
pattern string
2024-05-19 10:56:08 +00:00
}
func (pe *pipeExtract) String() string {
2024-05-19 11:47:30 +00:00
s := "extract"
if !isMsgFieldName(pe.fromField) {
s += " from " + quoteTokenIfNeeded(pe.fromField)
}
2024-05-19 13:11:17 +00:00
s += " " + quoteTokenIfNeeded(pe.pattern)
2024-05-19 11:47:30 +00:00
return s
2024-05-19 10:56:08 +00:00
}
func (pe *pipeExtract) updateNeededFields(neededFields, unneededFields fieldsSet) {
2024-05-19 13:11:17 +00:00
if neededFields.contains("*") {
2024-05-19 19:25:52 +00:00
unneededFieldsOrig := unneededFields.clone()
2024-05-19 13:11:17 +00:00
needFromField := false
for _, step := range pe.steps {
if step.field != "" {
2024-05-19 19:25:52 +00:00
if !unneededFieldsOrig.contains(step.field) {
2024-05-19 13:11:17 +00:00
needFromField = true
}
2024-05-19 19:25:52 +00:00
unneededFields.add(step.field)
2024-05-19 13:11:17 +00:00
}
}
if needFromField {
unneededFields.remove(pe.fromField)
} else {
unneededFields.add(pe.fromField)
}
} else {
needFromField := false
for _, step := range pe.steps {
2024-05-19 19:25:52 +00:00
if step.field != "" && neededFields.contains(step.field) {
needFromField = true
neededFields.remove(step.field)
2024-05-19 13:11:17 +00:00
}
}
if needFromField {
neededFields.add(pe.fromField)
2024-05-19 10:56:08 +00:00
}
}
}
2024-05-19 19:27:53 +00:00
func (pe *pipeExtract) newPipeProcessor(workersCount int, _ <-chan struct{}, _ func(), ppBase pipeProcessor) pipeProcessor {
2024-05-19 10:56:08 +00:00
shards := make([]pipeExtractProcessorShard, workersCount)
for i := range shards {
2024-05-20 10:43:46 +00:00
ef := newPattern(pe.steps)
2024-05-19 11:23:27 +00:00
rcs := make([]resultColumn, len(ef.fields))
for j := range rcs {
rcs[j].name = ef.fields[j].name
}
2024-05-19 10:56:08 +00:00
shards[i] = pipeExtractProcessorShard{
pipeExtractProcessorShardNopad: pipeExtractProcessorShardNopad{
2024-05-19 11:23:27 +00:00
ef: ef,
rcs: rcs,
2024-05-19 10:56:08 +00:00
},
}
}
pep := &pipeExtractProcessor{
pe: pe,
ppBase: ppBase,
shards: shards,
}
return pep
}
type pipeExtractProcessor struct {
pe *pipeExtract
ppBase pipeProcessor
shards []pipeExtractProcessorShard
}
type pipeExtractProcessorShard struct {
pipeExtractProcessorShardNopad
// The padding prevents false sharing on widespread platforms with 128 mod (cache line size) = 0 .
_ [128 - unsafe.Sizeof(pipeExtractProcessorShardNopad{})%128]byte
}
type pipeExtractProcessorShardNopad struct {
2024-05-20 10:43:46 +00:00
ef *pattern
2024-05-19 11:23:27 +00:00
rcs []resultColumn
2024-05-19 10:56:08 +00:00
}
func (pep *pipeExtractProcessor) writeBlock(workerID uint, br *blockResult) {
if len(br.timestamps) == 0 {
return
}
shard := &pep.shards[workerID]
ef := shard.ef
2024-05-19 11:23:27 +00:00
rcs := shard.rcs
2024-05-19 12:22:09 +00:00
c := br.getColumnByName(pep.pe.fromField)
if c.isConst {
v := c.valuesEncoded[0]
2024-05-19 10:56:08 +00:00
ef.apply(v)
2024-05-19 11:23:27 +00:00
for i, f := range ef.fields {
2024-05-19 12:22:09 +00:00
fieldValue := *f.value
rc := &rcs[i]
for range br.timestamps {
rc.addValue(fieldValue)
}
}
} else {
values := c.getValues(br)
for i, v := range values {
if i == 0 || values[i-1] != v {
ef.apply(v)
}
for j, f := range ef.fields {
rcs[j].addValue(*f.value)
}
2024-05-19 11:23:27 +00:00
}
}
2024-05-19 12:22:09 +00:00
2024-05-19 11:23:27 +00:00
br.addResultColumns(rcs)
pep.ppBase.writeBlock(workerID, br)
for i := range rcs {
2024-05-19 19:25:52 +00:00
rcs[i].resetValues()
2024-05-19 10:56:08 +00:00
}
}
func (pep *pipeExtractProcessor) flush() error {
return nil
}
2024-05-19 11:47: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()
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)
}
fromField = f
}
2024-05-19 13:11:17 +00:00
pattern, err := getCompoundToken(lex)
2024-05-19 11:47:30 +00:00
if err != nil {
2024-05-19 13:11:17 +00:00
return nil, fmt.Errorf("cannot read 'pattern': %w", err)
2024-05-19 11:47:30 +00:00
}
2024-05-20 10:43:46 +00:00
steps, err := parsePatternSteps(pattern)
2024-05-19 11:47:30 +00:00
if err != nil {
2024-05-19 13:11:17 +00:00
return nil, fmt.Errorf("cannot parse 'pattern' %q: %w", pattern, err)
2024-05-19 11:47:30 +00:00
}
pe := &pipeExtract{
fromField: fromField,
steps: steps,
2024-05-19 13:11:17 +00:00
pattern: pattern,
2024-05-19 11:47:30 +00:00
}
return pe, nil
}