2024-05-12 14:33:29 +00:00
|
|
|
package logstorage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
2024-05-20 02:08:30 +00:00
|
|
|
"strings"
|
2024-05-12 14:33:29 +00:00
|
|
|
"unsafe"
|
2024-05-20 02:08:30 +00:00
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
2024-05-12 14:33:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type statsMin struct {
|
2024-05-22 19:01:20 +00:00
|
|
|
fields []string
|
2024-05-12 14:33:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sm *statsMin) String() string {
|
2024-05-22 19:01:20 +00:00
|
|
|
return "min(" + statsFuncFieldsToString(sm.fields) + ")"
|
2024-05-12 14:33:29 +00:00
|
|
|
}
|
|
|
|
|
2024-05-20 02:08:30 +00:00
|
|
|
func (sm *statsMin) updateNeededFields(neededFields fieldsSet) {
|
2024-05-22 19:01:20 +00:00
|
|
|
updateNeededFieldsForStatsFunc(neededFields, sm.fields)
|
2024-05-12 14:33:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sm *statsMin) newStatsProcessor() (statsProcessor, int) {
|
|
|
|
smp := &statsMinProcessor{
|
2024-05-20 02:08:30 +00:00
|
|
|
sm: sm,
|
2024-05-12 14:33:29 +00:00
|
|
|
}
|
|
|
|
return smp, int(unsafe.Sizeof(*smp))
|
|
|
|
}
|
|
|
|
|
|
|
|
type statsMinProcessor struct {
|
|
|
|
sm *statsMin
|
|
|
|
|
2024-05-22 19:01:20 +00:00
|
|
|
min string
|
2024-05-12 14:33:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (smp *statsMinProcessor) updateStatsForAllRows(br *blockResult) int {
|
2024-05-20 02:08:30 +00:00
|
|
|
minLen := len(smp.min)
|
|
|
|
|
2024-05-22 19:01:20 +00:00
|
|
|
fields := smp.sm.fields
|
|
|
|
if len(fields) == 0 {
|
2024-05-12 14:33:29 +00:00
|
|
|
// Find the minimum value across all the columns
|
|
|
|
for _, c := range br.getColumns() {
|
2024-05-20 02:08:30 +00:00
|
|
|
smp.updateStateForColumn(br, c)
|
2024-05-12 14:33:29 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Find the minimum value across the requested columns
|
2024-05-22 19:01:20 +00:00
|
|
|
for _, field := range fields {
|
2024-05-12 14:33:29 +00:00
|
|
|
c := br.getColumnByName(field)
|
2024-05-20 02:08:30 +00:00
|
|
|
smp.updateStateForColumn(br, c)
|
2024-05-12 14:33:29 +00:00
|
|
|
}
|
|
|
|
}
|
2024-05-20 02:08:30 +00:00
|
|
|
|
|
|
|
return len(smp.min) - minLen
|
2024-05-12 14:33:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (smp *statsMinProcessor) updateStatsForRow(br *blockResult, rowIdx int) int {
|
2024-05-20 02:08:30 +00:00
|
|
|
minLen := len(smp.min)
|
|
|
|
|
2024-05-22 19:01:20 +00:00
|
|
|
fields := smp.sm.fields
|
|
|
|
if len(fields) == 0 {
|
2024-05-12 14:33:29 +00:00
|
|
|
// Find the minimum value across all the fields for the given row
|
|
|
|
for _, c := range br.getColumns() {
|
2024-05-20 02:08:30 +00:00
|
|
|
v := c.getValueAtRow(br, rowIdx)
|
|
|
|
smp.updateStateString(v)
|
2024-05-12 14:33:29 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Find the minimum value across the requested fields for the given row
|
2024-05-22 19:01:20 +00:00
|
|
|
for _, field := range fields {
|
2024-05-12 14:33:29 +00:00
|
|
|
c := br.getColumnByName(field)
|
2024-05-20 02:08:30 +00:00
|
|
|
v := c.getValueAtRow(br, rowIdx)
|
|
|
|
smp.updateStateString(v)
|
2024-05-12 14:33:29 +00:00
|
|
|
}
|
|
|
|
}
|
2024-05-20 02:08:30 +00:00
|
|
|
|
|
|
|
return minLen - len(smp.min)
|
2024-05-12 14:33:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (smp *statsMinProcessor) mergeState(sfp statsProcessor) {
|
|
|
|
src := sfp.(*statsMinProcessor)
|
2024-05-22 19:01:20 +00:00
|
|
|
smp.updateStateString(src.min)
|
2024-05-12 14:33:29 +00:00
|
|
|
}
|
|
|
|
|
2024-05-20 02:08:30 +00:00
|
|
|
func (smp *statsMinProcessor) updateStateForColumn(br *blockResult, c *blockResultColumn) {
|
2024-09-25 14:16:53 +00:00
|
|
|
if br.rowsLen == 0 {
|
2024-05-20 02:08:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.isTime {
|
2024-09-26 20:22:21 +00:00
|
|
|
timestamp, ok := TryParseTimestampRFC3339Nano(smp.min)
|
|
|
|
if !ok {
|
|
|
|
timestamp = (1 << 63) - 1
|
|
|
|
}
|
|
|
|
minTimestamp := br.getMinTimestamp(timestamp)
|
|
|
|
if minTimestamp >= timestamp {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-05-20 02:08:30 +00:00
|
|
|
bb := bbPool.Get()
|
|
|
|
bb.B = marshalTimestampRFC3339NanoString(bb.B[:0], minTimestamp)
|
|
|
|
smp.updateStateBytes(bb.B)
|
|
|
|
bbPool.Put(bb)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if c.isConst {
|
|
|
|
// Special case for const column
|
|
|
|
v := c.valuesEncoded[0]
|
|
|
|
smp.updateStateString(v)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch c.valueType {
|
|
|
|
case valueTypeString:
|
|
|
|
for _, v := range c.getValuesEncoded(br) {
|
|
|
|
smp.updateStateString(v)
|
|
|
|
}
|
|
|
|
case valueTypeDict:
|
|
|
|
for _, v := range c.dictValues {
|
|
|
|
smp.updateStateString(v)
|
|
|
|
}
|
|
|
|
case valueTypeUint8, valueTypeUint16, valueTypeUint32, valueTypeUint64:
|
|
|
|
bb := bbPool.Get()
|
|
|
|
bb.B = marshalUint64String(bb.B[:0], c.minValue)
|
|
|
|
smp.updateStateBytes(bb.B)
|
|
|
|
bbPool.Put(bb)
|
|
|
|
case valueTypeFloat64:
|
|
|
|
f := math.Float64frombits(c.minValue)
|
|
|
|
bb := bbPool.Get()
|
|
|
|
bb.B = marshalFloat64String(bb.B[:0], f)
|
|
|
|
smp.updateStateBytes(bb.B)
|
|
|
|
bbPool.Put(bb)
|
|
|
|
case valueTypeIPv4:
|
|
|
|
bb := bbPool.Get()
|
|
|
|
bb.B = marshalIPv4String(bb.B[:0], uint32(c.minValue))
|
|
|
|
smp.updateStateBytes(bb.B)
|
|
|
|
bbPool.Put(bb)
|
|
|
|
case valueTypeTimestampISO8601:
|
|
|
|
bb := bbPool.Get()
|
|
|
|
bb.B = marshalTimestampISO8601String(bb.B[:0], int64(c.minValue))
|
|
|
|
smp.updateStateBytes(bb.B)
|
|
|
|
bbPool.Put(bb)
|
|
|
|
default:
|
|
|
|
logger.Panicf("BUG: unknown valueType=%d", c.valueType)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (smp *statsMinProcessor) updateStateBytes(b []byte) {
|
|
|
|
v := bytesutil.ToUnsafeString(b)
|
|
|
|
smp.updateStateString(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (smp *statsMinProcessor) updateStateString(v string) {
|
2024-05-22 19:01:20 +00:00
|
|
|
if v == "" {
|
|
|
|
// Skip empty strings
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if smp.min != "" && !lessString(v, smp.min) {
|
2024-05-20 02:08:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
smp.min = strings.Clone(v)
|
|
|
|
}
|
|
|
|
|
2024-05-12 14:33:29 +00:00
|
|
|
func (smp *statsMinProcessor) finalizeStats() string {
|
2024-05-20 02:08:30 +00:00
|
|
|
return smp.min
|
2024-05-12 14:33:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseStatsMin(lex *lexer) (*statsMin, error) {
|
2024-05-22 19:01:20 +00:00
|
|
|
fields, err := parseStatsFuncFields(lex, "min")
|
2024-05-12 14:33:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sm := &statsMin{
|
2024-05-22 19:01:20 +00:00
|
|
|
fields: fields,
|
2024-05-12 14:33:29 +00:00
|
|
|
}
|
|
|
|
return sm, nil
|
|
|
|
}
|