2024-05-12 14:33:29 +00:00
|
|
|
package logstorage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
|
|
|
)
|
|
|
|
|
|
|
|
// pipeRename implements '| rename ...' pipe.
|
|
|
|
//
|
|
|
|
// See https://docs.victoriametrics.com/victorialogs/logsql/#rename-pipe
|
|
|
|
type pipeRename struct {
|
|
|
|
// srcFields contains a list of source fields to rename
|
|
|
|
srcFields []string
|
|
|
|
|
|
|
|
// dstFields contains a list of destination fields
|
|
|
|
dstFields []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pr *pipeRename) String() string {
|
|
|
|
if len(pr.srcFields) == 0 {
|
|
|
|
logger.Panicf("BUG: pipeRename must contain at least a single srcField")
|
|
|
|
}
|
|
|
|
|
|
|
|
a := make([]string, len(pr.srcFields))
|
|
|
|
for i, srcField := range pr.srcFields {
|
|
|
|
dstField := pr.dstFields[i]
|
|
|
|
a[i] = quoteTokenIfNeeded(srcField) + " as " + quoteTokenIfNeeded(dstField)
|
|
|
|
}
|
|
|
|
return "rename " + strings.Join(a, ", ")
|
|
|
|
}
|
|
|
|
|
2024-06-27 12:18:42 +00:00
|
|
|
func (pr *pipeRename) canLiveTail() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2024-05-12 14:33:29 +00:00
|
|
|
func (pr *pipeRename) updateNeededFields(neededFields, unneededFields fieldsSet) {
|
2024-05-22 19:01:20 +00:00
|
|
|
for i := len(pr.srcFields) - 1; i >= 0; i-- {
|
|
|
|
srcField := pr.srcFields[i]
|
|
|
|
dstField := pr.dstFields[i]
|
|
|
|
|
|
|
|
if neededFields.contains("*") {
|
|
|
|
if unneededFields.contains(dstField) {
|
2024-05-12 14:33:29 +00:00
|
|
|
unneededFields.add(srcField)
|
2024-05-22 19:01:20 +00:00
|
|
|
} else {
|
|
|
|
unneededFields.add(dstField)
|
|
|
|
unneededFields.remove(srcField)
|
2024-05-12 14:33:29 +00:00
|
|
|
}
|
2024-05-22 19:01:20 +00:00
|
|
|
} else {
|
|
|
|
if neededFields.contains(dstField) {
|
|
|
|
neededFields.remove(dstField)
|
2024-05-12 14:33:29 +00:00
|
|
|
neededFields.add(srcField)
|
|
|
|
} else {
|
|
|
|
neededFields.remove(srcField)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-25 19:36:16 +00:00
|
|
|
func (pr *pipeRename) optimize() {
|
|
|
|
// nothing to do
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pr *pipeRename) hasFilterInWithQuery() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-05-26 00:01:32 +00:00
|
|
|
func (pr *pipeRename) initFilterInValues(_ map[string][]string, _ getFieldValuesFunc) (pipe, error) {
|
2024-05-25 19:36:16 +00:00
|
|
|
return pr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pr *pipeRename) newPipeProcessor(_ int, _ <-chan struct{}, _ func(), ppNext pipeProcessor) pipeProcessor {
|
2024-05-12 14:33:29 +00:00
|
|
|
return &pipeRenameProcessor{
|
|
|
|
pr: pr,
|
2024-05-25 19:36:16 +00:00
|
|
|
ppNext: ppNext,
|
2024-05-12 14:33:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type pipeRenameProcessor struct {
|
|
|
|
pr *pipeRename
|
2024-05-25 19:36:16 +00:00
|
|
|
ppNext pipeProcessor
|
2024-05-12 14:33:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (prp *pipeRenameProcessor) writeBlock(workerID uint, br *blockResult) {
|
2024-09-25 14:16:53 +00:00
|
|
|
if br.rowsLen == 0 {
|
2024-05-12 14:33:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
br.renameColumns(prp.pr.srcFields, prp.pr.dstFields)
|
2024-05-25 19:36:16 +00:00
|
|
|
prp.ppNext.writeBlock(workerID, br)
|
2024-05-12 14:33:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (prp *pipeRenameProcessor) flush() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parsePipeRename(lex *lexer) (*pipeRename, error) {
|
|
|
|
if !lex.isKeyword("rename", "mv") {
|
|
|
|
return nil, fmt.Errorf("expecting 'rename' or 'mv'; got %q", lex.token)
|
|
|
|
}
|
|
|
|
|
|
|
|
var srcFields []string
|
|
|
|
var dstFields []string
|
|
|
|
for {
|
|
|
|
lex.nextToken()
|
|
|
|
srcField, err := parseFieldName(lex)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot parse src field name: %w", err)
|
|
|
|
}
|
|
|
|
if lex.isKeyword("as") {
|
|
|
|
lex.nextToken()
|
|
|
|
}
|
|
|
|
dstField, err := parseFieldName(lex)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot parse dst field name: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
srcFields = append(srcFields, srcField)
|
|
|
|
dstFields = append(dstFields, dstField)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case lex.isKeyword("|", ")", ""):
|
|
|
|
pr := &pipeRename{
|
|
|
|
srcFields: srcFields,
|
|
|
|
dstFields: dstFields,
|
|
|
|
}
|
|
|
|
return pr, nil
|
|
|
|
case lex.isKeyword(","):
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unexpected token: %q; expecting ',', '|' or ')'", lex.token)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|