2024-05-12 16:33:29 +02:00
|
|
|
package logstorage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"slices"
|
2024-05-20 04:08:30 +02:00
|
|
|
"strings"
|
2024-05-12 16:33:29 +02:00
|
|
|
"sync/atomic"
|
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/encoding"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/memory"
|
|
|
|
)
|
|
|
|
|
|
|
|
// pipeUniq processes '| uniq ...' queries.
|
|
|
|
//
|
|
|
|
// See https://docs.victoriametrics.com/victorialogs/logsql/#uniq-pipe
|
|
|
|
type pipeUniq struct {
|
|
|
|
// fields contains field names for returning unique values
|
|
|
|
byFields []string
|
|
|
|
|
2024-05-24 03:06:55 +02:00
|
|
|
// if hitsFieldName isn't empty, then the number of hits per each unique value is stored in this field.
|
|
|
|
hitsFieldName string
|
|
|
|
|
2024-05-12 16:33:29 +02:00
|
|
|
limit uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pu *pipeUniq) String() string {
|
|
|
|
s := "uniq"
|
|
|
|
if len(pu.byFields) > 0 {
|
|
|
|
s += " by (" + fieldNamesString(pu.byFields) + ")"
|
|
|
|
}
|
2024-05-24 03:06:55 +02:00
|
|
|
if pu.hitsFieldName != "" {
|
|
|
|
s += " hits"
|
|
|
|
}
|
2024-05-12 16:33:29 +02:00
|
|
|
if pu.limit > 0 {
|
|
|
|
s += fmt.Sprintf(" limit %d", pu.limit)
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pu *pipeUniq) updateNeededFields(neededFields, unneededFields fieldsSet) {
|
|
|
|
neededFields.reset()
|
|
|
|
unneededFields.reset()
|
|
|
|
|
|
|
|
if len(pu.byFields) == 0 {
|
|
|
|
neededFields.add("*")
|
|
|
|
} else {
|
2024-05-20 04:08:30 +02:00
|
|
|
neededFields.addFields(pu.byFields)
|
2024-05-12 16:33:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pu *pipeUniq) newPipeProcessor(workersCount int, stopCh <-chan struct{}, cancel func(), ppBase pipeProcessor) pipeProcessor {
|
|
|
|
maxStateSize := int64(float64(memory.Allowed()) * 0.2)
|
|
|
|
|
|
|
|
shards := make([]pipeUniqProcessorShard, workersCount)
|
|
|
|
for i := range shards {
|
2024-05-20 04:08:30 +02:00
|
|
|
shards[i] = pipeUniqProcessorShard{
|
|
|
|
pipeUniqProcessorShardNopad: pipeUniqProcessorShardNopad{
|
|
|
|
pu: pu,
|
|
|
|
stateSizeBudget: stateSizeBudgetChunk,
|
|
|
|
},
|
|
|
|
}
|
2024-05-12 16:33:29 +02:00
|
|
|
maxStateSize -= stateSizeBudgetChunk
|
|
|
|
}
|
|
|
|
|
|
|
|
pup := &pipeUniqProcessor{
|
|
|
|
pu: pu,
|
|
|
|
stopCh: stopCh,
|
|
|
|
cancel: cancel,
|
|
|
|
ppBase: ppBase,
|
|
|
|
|
|
|
|
shards: shards,
|
|
|
|
|
|
|
|
maxStateSize: maxStateSize,
|
|
|
|
}
|
|
|
|
pup.stateSizeBudget.Store(maxStateSize)
|
|
|
|
|
|
|
|
return pup
|
|
|
|
}
|
|
|
|
|
|
|
|
type pipeUniqProcessor struct {
|
|
|
|
pu *pipeUniq
|
|
|
|
stopCh <-chan struct{}
|
|
|
|
cancel func()
|
|
|
|
ppBase pipeProcessor
|
|
|
|
|
|
|
|
shards []pipeUniqProcessorShard
|
|
|
|
|
|
|
|
maxStateSize int64
|
|
|
|
stateSizeBudget atomic.Int64
|
|
|
|
}
|
|
|
|
|
|
|
|
type pipeUniqProcessorShard struct {
|
|
|
|
pipeUniqProcessorShardNopad
|
|
|
|
|
|
|
|
// The padding prevents false sharing on widespread platforms with 128 mod (cache line size) = 0 .
|
|
|
|
_ [128 - unsafe.Sizeof(pipeUniqProcessorShardNopad{})%128]byte
|
|
|
|
}
|
|
|
|
|
|
|
|
type pipeUniqProcessorShardNopad struct {
|
|
|
|
// pu points to the parent pipeUniq.
|
|
|
|
pu *pipeUniq
|
|
|
|
|
2024-05-24 03:06:55 +02:00
|
|
|
// m holds per-row hits.
|
|
|
|
m map[string]*uint64
|
2024-05-12 16:33:29 +02:00
|
|
|
|
|
|
|
// keyBuf is a temporary buffer for building keys for m.
|
|
|
|
keyBuf []byte
|
|
|
|
|
|
|
|
// columnValues is a temporary buffer for the processed column values.
|
|
|
|
columnValues [][]string
|
|
|
|
|
|
|
|
// stateSizeBudget is the remaining budget for the whole state size for the shard.
|
|
|
|
// The per-shard budget is provided in chunks from the parent pipeUniqProcessor.
|
|
|
|
stateSizeBudget int
|
|
|
|
}
|
|
|
|
|
|
|
|
// writeBlock writes br to shard.
|
|
|
|
//
|
|
|
|
// It returns false if the block cannot be written because of the exceeded limit.
|
|
|
|
func (shard *pipeUniqProcessorShard) writeBlock(br *blockResult) bool {
|
|
|
|
if limit := shard.pu.limit; limit > 0 && uint64(len(shard.m)) >= limit {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-05-24 03:06:55 +02:00
|
|
|
needHits := shard.pu.hitsFieldName != ""
|
2024-05-12 16:33:29 +02:00
|
|
|
byFields := shard.pu.byFields
|
|
|
|
if len(byFields) == 0 {
|
|
|
|
// Take into account all the columns in br.
|
|
|
|
keyBuf := shard.keyBuf
|
|
|
|
cs := br.getColumns()
|
|
|
|
for i := range br.timestamps {
|
|
|
|
keyBuf = keyBuf[:0]
|
|
|
|
for _, c := range cs {
|
|
|
|
v := c.getValueAtRow(br, i)
|
|
|
|
keyBuf = encoding.MarshalBytes(keyBuf, bytesutil.ToUnsafeBytes(c.name))
|
|
|
|
keyBuf = encoding.MarshalBytes(keyBuf, bytesutil.ToUnsafeBytes(v))
|
|
|
|
}
|
2024-05-24 03:06:55 +02:00
|
|
|
shard.updateState(bytesutil.ToUnsafeString(keyBuf), 1)
|
2024-05-12 16:33:29 +02:00
|
|
|
}
|
|
|
|
shard.keyBuf = keyBuf
|
|
|
|
return true
|
|
|
|
}
|
2024-05-20 04:08:30 +02:00
|
|
|
if len(byFields) == 1 {
|
|
|
|
// Fast path for a single field.
|
|
|
|
c := br.getColumnByName(byFields[0])
|
|
|
|
if c.isConst {
|
|
|
|
v := c.valuesEncoded[0]
|
2024-05-24 03:06:55 +02:00
|
|
|
shard.updateState(v, uint64(len(br.timestamps)))
|
2024-05-20 04:08:30 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
if c.valueType == valueTypeDict {
|
2024-05-24 03:06:55 +02:00
|
|
|
if needHits {
|
|
|
|
a := encoding.GetUint64s(len(c.dictValues))
|
|
|
|
hits := a.A
|
|
|
|
valuesEncoded := c.getValuesEncoded(br)
|
|
|
|
for _, v := range valuesEncoded {
|
|
|
|
idx := unmarshalUint8(v)
|
|
|
|
hits[idx]++
|
|
|
|
}
|
|
|
|
for i, v := range c.dictValues {
|
|
|
|
shard.updateState(v, hits[i])
|
|
|
|
}
|
|
|
|
encoding.PutUint64s(a)
|
|
|
|
} else {
|
|
|
|
for _, v := range c.dictValues {
|
|
|
|
shard.updateState(v, 0)
|
|
|
|
}
|
2024-05-20 04:08:30 +02:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
values := c.getValues(br)
|
|
|
|
for i, v := range values {
|
2024-05-24 03:06:55 +02:00
|
|
|
if needHits || i == 0 || values[i-1] != values[i] {
|
|
|
|
shard.updateState(v, 1)
|
2024-05-20 04:08:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2024-05-12 16:33:29 +02:00
|
|
|
|
|
|
|
// Take into account only the selected columns.
|
|
|
|
columnValues := shard.columnValues[:0]
|
|
|
|
for _, f := range byFields {
|
|
|
|
c := br.getColumnByName(f)
|
2024-05-20 04:08:30 +02:00
|
|
|
values := c.getValues(br)
|
|
|
|
columnValues = append(columnValues, values)
|
2024-05-12 16:33:29 +02:00
|
|
|
}
|
|
|
|
shard.columnValues = columnValues
|
|
|
|
|
|
|
|
keyBuf := shard.keyBuf
|
|
|
|
for i := range br.timestamps {
|
|
|
|
seenValue := true
|
|
|
|
for _, values := range columnValues {
|
2024-05-24 03:06:55 +02:00
|
|
|
if needHits || i == 0 || values[i-1] != values[i] {
|
2024-05-12 16:33:29 +02:00
|
|
|
seenValue = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if seenValue {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
keyBuf = keyBuf[:0]
|
|
|
|
for _, values := range columnValues {
|
|
|
|
keyBuf = encoding.MarshalBytes(keyBuf, bytesutil.ToUnsafeBytes(values[i]))
|
|
|
|
}
|
2024-05-24 03:06:55 +02:00
|
|
|
shard.updateState(bytesutil.ToUnsafeString(keyBuf), 1)
|
2024-05-12 16:33:29 +02:00
|
|
|
}
|
|
|
|
shard.keyBuf = keyBuf
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2024-05-24 03:06:55 +02:00
|
|
|
func (shard *pipeUniqProcessorShard) updateState(v string, hits uint64) {
|
|
|
|
m := shard.getM()
|
|
|
|
pHits, ok := m[v]
|
|
|
|
if !ok {
|
2024-05-20 04:08:30 +02:00
|
|
|
vCopy := strings.Clone(v)
|
2024-05-24 03:06:55 +02:00
|
|
|
hits := uint64(0)
|
|
|
|
pHits = &hits
|
|
|
|
m[vCopy] = pHits
|
|
|
|
shard.stateSizeBudget -= len(vCopy) + int(unsafe.Sizeof(vCopy)+unsafe.Sizeof(hits)+unsafe.Sizeof(pHits))
|
2024-05-20 04:08:30 +02:00
|
|
|
}
|
2024-05-24 03:06:55 +02:00
|
|
|
*pHits += hits
|
|
|
|
}
|
|
|
|
|
|
|
|
func (shard *pipeUniqProcessorShard) getM() map[string]*uint64 {
|
|
|
|
if shard.m == nil {
|
|
|
|
shard.m = make(map[string]*uint64)
|
|
|
|
}
|
|
|
|
return shard.m
|
2024-05-20 04:08:30 +02:00
|
|
|
}
|
|
|
|
|
2024-05-12 16:33:29 +02:00
|
|
|
func (pup *pipeUniqProcessor) writeBlock(workerID uint, br *blockResult) {
|
|
|
|
if len(br.timestamps) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
shard := &pup.shards[workerID]
|
|
|
|
|
|
|
|
for shard.stateSizeBudget < 0 {
|
|
|
|
// steal some budget for the state size from the global budget.
|
|
|
|
remaining := pup.stateSizeBudget.Add(-stateSizeBudgetChunk)
|
|
|
|
if remaining < 0 {
|
|
|
|
// The state size is too big. Stop processing data in order to avoid OOM crash.
|
|
|
|
if remaining+stateSizeBudgetChunk >= 0 {
|
|
|
|
// Notify worker goroutines to stop calling writeBlock() in order to save CPU time.
|
|
|
|
pup.cancel()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
shard.stateSizeBudget += stateSizeBudgetChunk
|
|
|
|
}
|
|
|
|
|
|
|
|
if !shard.writeBlock(br) {
|
|
|
|
pup.cancel()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pup *pipeUniqProcessor) flush() error {
|
|
|
|
if n := pup.stateSizeBudget.Load(); n <= 0 {
|
|
|
|
return fmt.Errorf("cannot calculate [%s], since it requires more than %dMB of memory", pup.pu.String(), pup.maxStateSize/(1<<20))
|
|
|
|
}
|
|
|
|
|
|
|
|
// merge state across shards
|
|
|
|
shards := pup.shards
|
2024-05-24 03:06:55 +02:00
|
|
|
m := shards[0].getM()
|
2024-05-12 16:33:29 +02:00
|
|
|
shards = shards[1:]
|
|
|
|
for i := range shards {
|
2024-05-15 04:55:44 +02:00
|
|
|
if needStop(pup.stopCh) {
|
2024-05-12 16:33:29 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-24 03:06:55 +02:00
|
|
|
for k, pHitsSrc := range shards[i].getM() {
|
|
|
|
pHits, ok := m[k]
|
|
|
|
if !ok {
|
|
|
|
m[k] = pHitsSrc
|
|
|
|
} else {
|
|
|
|
*pHits += *pHitsSrc
|
|
|
|
}
|
2024-05-12 16:33:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-24 03:06:55 +02:00
|
|
|
// There is little sense in returning partial hits when the limit on the number of unique entries is reached.
|
|
|
|
// It is better from UX experience is to return zero hits instead.
|
|
|
|
resetHits := pup.pu.limit > 0 && uint64(len(m)) >= pup.pu.limit
|
|
|
|
|
2024-05-12 16:33:29 +02:00
|
|
|
// write result
|
|
|
|
wctx := &pipeUniqWriteContext{
|
|
|
|
pup: pup,
|
|
|
|
}
|
|
|
|
byFields := pup.pu.byFields
|
|
|
|
var rowFields []Field
|
|
|
|
|
2024-05-24 03:06:55 +02:00
|
|
|
addHitsFieldIfNeeded := func(dst []Field, hits uint64) []Field {
|
|
|
|
if pup.pu.hitsFieldName == "" {
|
|
|
|
return dst
|
|
|
|
}
|
|
|
|
if resetHits {
|
|
|
|
hits = 0
|
|
|
|
}
|
|
|
|
hitsStr := string(marshalUint64String(nil, hits))
|
|
|
|
dst = append(dst, Field{
|
|
|
|
Name: pup.pu.hitsFieldName,
|
|
|
|
Value: hitsStr,
|
|
|
|
})
|
|
|
|
return dst
|
|
|
|
}
|
|
|
|
|
2024-05-12 16:33:29 +02:00
|
|
|
if len(byFields) == 0 {
|
2024-05-24 03:06:55 +02:00
|
|
|
for k, pHits := range m {
|
2024-05-15 04:55:44 +02:00
|
|
|
if needStop(pup.stopCh) {
|
2024-05-12 16:33:29 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
rowFields = rowFields[:0]
|
|
|
|
keyBuf := bytesutil.ToUnsafeBytes(k)
|
|
|
|
for len(keyBuf) > 0 {
|
2024-05-14 01:23:44 +02:00
|
|
|
name, nSize := encoding.UnmarshalBytes(keyBuf)
|
|
|
|
if nSize <= 0 {
|
|
|
|
logger.Panicf("BUG: cannot unmarshal field name")
|
2024-05-12 16:33:29 +02:00
|
|
|
}
|
2024-05-14 01:23:44 +02:00
|
|
|
keyBuf = keyBuf[nSize:]
|
2024-05-12 16:33:29 +02:00
|
|
|
|
2024-05-14 01:23:44 +02:00
|
|
|
value, nSize := encoding.UnmarshalBytes(keyBuf)
|
|
|
|
if nSize <= 0 {
|
|
|
|
logger.Panicf("BUG: cannot unmarshal field value")
|
2024-05-12 16:33:29 +02:00
|
|
|
}
|
2024-05-14 01:23:44 +02:00
|
|
|
keyBuf = keyBuf[nSize:]
|
2024-05-12 16:33:29 +02:00
|
|
|
|
|
|
|
rowFields = append(rowFields, Field{
|
|
|
|
Name: bytesutil.ToUnsafeString(name),
|
|
|
|
Value: bytesutil.ToUnsafeString(value),
|
|
|
|
})
|
|
|
|
}
|
2024-05-24 03:06:55 +02:00
|
|
|
rowFields = addHitsFieldIfNeeded(rowFields, *pHits)
|
2024-05-12 16:33:29 +02:00
|
|
|
wctx.writeRow(rowFields)
|
|
|
|
}
|
2024-05-20 04:08:30 +02:00
|
|
|
} else if len(byFields) == 1 {
|
|
|
|
fieldName := byFields[0]
|
2024-05-24 03:06:55 +02:00
|
|
|
for k, pHits := range m {
|
2024-05-20 04:08:30 +02:00
|
|
|
if needStop(pup.stopCh) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
rowFields = append(rowFields[:0], Field{
|
|
|
|
Name: fieldName,
|
|
|
|
Value: k,
|
|
|
|
})
|
2024-05-24 03:06:55 +02:00
|
|
|
rowFields = addHitsFieldIfNeeded(rowFields, *pHits)
|
2024-05-20 04:08:30 +02:00
|
|
|
wctx.writeRow(rowFields)
|
|
|
|
}
|
2024-05-12 16:33:29 +02:00
|
|
|
} else {
|
2024-05-24 03:06:55 +02:00
|
|
|
for k, pHits := range m {
|
2024-05-15 04:55:44 +02:00
|
|
|
if needStop(pup.stopCh) {
|
2024-05-12 16:33:29 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
rowFields = rowFields[:0]
|
|
|
|
keyBuf := bytesutil.ToUnsafeBytes(k)
|
|
|
|
fieldIdx := 0
|
|
|
|
for len(keyBuf) > 0 {
|
2024-05-14 01:23:44 +02:00
|
|
|
value, nSize := encoding.UnmarshalBytes(keyBuf)
|
|
|
|
if nSize <= 0 {
|
|
|
|
logger.Panicf("BUG: cannot unmarshal field value")
|
2024-05-12 16:33:29 +02:00
|
|
|
}
|
2024-05-14 01:23:44 +02:00
|
|
|
keyBuf = keyBuf[nSize:]
|
2024-05-12 16:33:29 +02:00
|
|
|
|
|
|
|
rowFields = append(rowFields, Field{
|
|
|
|
Name: byFields[fieldIdx],
|
|
|
|
Value: bytesutil.ToUnsafeString(value),
|
|
|
|
})
|
|
|
|
fieldIdx++
|
|
|
|
}
|
2024-05-24 03:06:55 +02:00
|
|
|
rowFields = addHitsFieldIfNeeded(rowFields, *pHits)
|
2024-05-12 16:33:29 +02:00
|
|
|
wctx.writeRow(rowFields)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wctx.flush()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type pipeUniqWriteContext struct {
|
|
|
|
pup *pipeUniqProcessor
|
|
|
|
rcs []resultColumn
|
|
|
|
br blockResult
|
|
|
|
|
2024-05-22 21:01:20 +02:00
|
|
|
// rowsWritten is the total number of rows passed to writeRow.
|
2024-05-12 16:33:29 +02:00
|
|
|
rowsWritten uint64
|
|
|
|
|
2024-05-22 21:01:20 +02:00
|
|
|
// rowsCount is the number of rows in the current block
|
|
|
|
rowsCount int
|
|
|
|
|
|
|
|
// valuesLen is the total length of values in the current block
|
2024-05-12 16:33:29 +02:00
|
|
|
valuesLen int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wctx *pipeUniqWriteContext) writeRow(rowFields []Field) {
|
|
|
|
if limit := wctx.pup.pu.limit; limit > 0 && wctx.rowsWritten >= limit {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
wctx.rowsWritten++
|
|
|
|
|
|
|
|
rcs := wctx.rcs
|
|
|
|
|
|
|
|
areEqualColumns := len(rcs) == len(rowFields)
|
|
|
|
if areEqualColumns {
|
|
|
|
for i, f := range rowFields {
|
|
|
|
if rcs[i].name != f.Name {
|
|
|
|
areEqualColumns = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !areEqualColumns {
|
2024-05-22 21:01:20 +02:00
|
|
|
// send the current block to ppBase and construct a block with new set of columns
|
2024-05-12 16:33:29 +02:00
|
|
|
wctx.flush()
|
|
|
|
|
|
|
|
rcs = wctx.rcs[:0]
|
|
|
|
for _, f := range rowFields {
|
2024-05-20 04:08:30 +02:00
|
|
|
rcs = appendResultColumnWithName(rcs, f.Name)
|
2024-05-12 16:33:29 +02:00
|
|
|
}
|
|
|
|
wctx.rcs = rcs
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, f := range rowFields {
|
|
|
|
v := f.Value
|
|
|
|
rcs[i].addValue(v)
|
|
|
|
wctx.valuesLen += len(v)
|
|
|
|
}
|
2024-05-22 21:01:20 +02:00
|
|
|
|
|
|
|
wctx.rowsCount++
|
2024-05-12 16:33:29 +02:00
|
|
|
if wctx.valuesLen >= 1_000_000 {
|
|
|
|
wctx.flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wctx *pipeUniqWriteContext) flush() {
|
|
|
|
rcs := wctx.rcs
|
|
|
|
br := &wctx.br
|
|
|
|
|
|
|
|
wctx.valuesLen = 0
|
|
|
|
|
|
|
|
// Flush rcs to ppBase
|
2024-05-22 21:01:20 +02:00
|
|
|
br.setResultColumns(rcs, wctx.rowsCount)
|
|
|
|
wctx.rowsCount = 0
|
2024-05-12 16:33:29 +02:00
|
|
|
wctx.pup.ppBase.writeBlock(0, br)
|
|
|
|
br.reset()
|
|
|
|
for i := range rcs {
|
2024-05-20 04:08:30 +02:00
|
|
|
rcs[i].resetValues()
|
2024-05-12 16:33:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func parsePipeUniq(lex *lexer) (*pipeUniq, error) {
|
|
|
|
if !lex.isKeyword("uniq") {
|
|
|
|
return nil, fmt.Errorf("expecting 'uniq'; got %q", lex.token)
|
|
|
|
}
|
|
|
|
lex.nextToken()
|
|
|
|
|
|
|
|
var pu pipeUniq
|
2024-05-20 04:08:30 +02:00
|
|
|
if lex.isKeyword("by", "(") {
|
|
|
|
if lex.isKeyword("by") {
|
|
|
|
lex.nextToken()
|
|
|
|
}
|
2024-05-12 16:33:29 +02:00
|
|
|
bfs, err := parseFieldNamesInParens(lex)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot parse 'by' clause: %w", err)
|
|
|
|
}
|
|
|
|
if slices.Contains(bfs, "*") {
|
|
|
|
bfs = nil
|
|
|
|
}
|
|
|
|
pu.byFields = bfs
|
|
|
|
}
|
|
|
|
|
2024-05-24 03:06:55 +02:00
|
|
|
if lex.isKeyword("hits") {
|
|
|
|
lex.nextToken()
|
|
|
|
hitsFieldName := "hits"
|
|
|
|
for slices.Contains(pu.byFields, hitsFieldName) {
|
|
|
|
hitsFieldName += "s"
|
|
|
|
}
|
|
|
|
|
|
|
|
pu.hitsFieldName = hitsFieldName
|
|
|
|
}
|
|
|
|
|
2024-05-12 16:33:29 +02:00
|
|
|
if lex.isKeyword("limit") {
|
|
|
|
lex.nextToken()
|
|
|
|
n, ok := tryParseUint64(lex.token)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("cannot parse 'limit %s'", lex.token)
|
|
|
|
}
|
|
|
|
lex.nextToken()
|
|
|
|
pu.limit = n
|
|
|
|
}
|
|
|
|
|
|
|
|
return &pu, nil
|
|
|
|
}
|