2024-05-12 14:33:29 +00:00
|
|
|
package logstorage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
|
|
|
)
|
|
|
|
|
|
|
|
// pipeDelete implements '| delete ...' pipe.
|
|
|
|
//
|
|
|
|
// See https://docs.victoriametrics.com/victorialogs/logsql/#delete-pipe
|
|
|
|
type pipeDelete struct {
|
|
|
|
// fields contains a list of fields to delete
|
|
|
|
fields []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pd *pipeDelete) String() string {
|
|
|
|
if len(pd.fields) == 0 {
|
|
|
|
logger.Panicf("BUG: pipeDelete must contain at least a single field")
|
|
|
|
}
|
|
|
|
|
|
|
|
return "delete " + fieldNamesString(pd.fields)
|
|
|
|
}
|
|
|
|
|
2024-06-27 12:18:42 +00:00
|
|
|
func (pd *pipeDelete) canLiveTail() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2024-05-12 14:33:29 +00:00
|
|
|
func (pd *pipeDelete) updateNeededFields(neededFields, unneededFields fieldsSet) {
|
|
|
|
if neededFields.contains("*") {
|
2024-05-20 02:08:30 +00:00
|
|
|
unneededFields.addFields(pd.fields)
|
2024-05-12 14:33:29 +00:00
|
|
|
} else {
|
2024-05-20 02:08:30 +00:00
|
|
|
neededFields.removeFields(pd.fields)
|
2024-05-12 14:33:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-25 19:36:16 +00:00
|
|
|
func (pd *pipeDelete) optimize() {
|
|
|
|
// nothing to do
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pd *pipeDelete) hasFilterInWithQuery() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-05-26 00:01:32 +00:00
|
|
|
func (pd *pipeDelete) initFilterInValues(_ map[string][]string, _ getFieldValuesFunc) (pipe, error) {
|
2024-05-25 19:36:16 +00:00
|
|
|
return pd, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pd *pipeDelete) newPipeProcessor(_ int, _ <-chan struct{}, _ func(), ppNext pipeProcessor) pipeProcessor {
|
2024-05-12 14:33:29 +00:00
|
|
|
return &pipeDeleteProcessor{
|
|
|
|
pd: pd,
|
2024-05-25 19:36:16 +00:00
|
|
|
ppNext: ppNext,
|
2024-05-12 14:33:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type pipeDeleteProcessor struct {
|
|
|
|
pd *pipeDelete
|
2024-05-25 19:36:16 +00:00
|
|
|
ppNext pipeProcessor
|
2024-05-12 14:33:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pdp *pipeDeleteProcessor) writeBlock(workerID uint, br *blockResult) {
|
|
|
|
if len(br.timestamps) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
br.deleteColumns(pdp.pd.fields)
|
2024-05-25 19:36:16 +00:00
|
|
|
pdp.ppNext.writeBlock(workerID, br)
|
2024-05-12 14:33:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pdp *pipeDeleteProcessor) flush() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parsePipeDelete(lex *lexer) (*pipeDelete, error) {
|
2024-05-28 17:29:41 +00:00
|
|
|
if !lex.isKeyword("delete", "del", "rm", "drop") {
|
|
|
|
return nil, fmt.Errorf("expecting 'delete', 'del', 'rm' or 'drop'; got %q", lex.token)
|
2024-05-12 14:33:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var fields []string
|
|
|
|
for {
|
|
|
|
lex.nextToken()
|
|
|
|
field, err := parseFieldName(lex)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot parse field name: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fields = append(fields, field)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case lex.isKeyword("|", ")", ""):
|
|
|
|
pd := &pipeDelete{
|
|
|
|
fields: fields,
|
|
|
|
}
|
|
|
|
return pd, nil
|
|
|
|
case lex.isKeyword(","):
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unexpected token: %q; expecting ',', '|' or ')'", lex.token)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|