2024-04-30 23:58:35 +00:00
|
|
|
package logstorage
|
|
|
|
|
|
|
|
import (
|
2024-05-15 13:46:42 +00:00
|
|
|
"math"
|
2024-04-30 23:58:35 +00:00
|
|
|
"slices"
|
2024-05-15 11:07:15 +00:00
|
|
|
"strings"
|
2024-04-30 23:58:35 +00:00
|
|
|
"unsafe"
|
2024-05-15 11:23:51 +00:00
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
|
2024-05-15 13:46:42 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
2024-04-30 23:58:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type statsMax struct {
|
|
|
|
fields []string
|
|
|
|
containsStar bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sm *statsMax) String() string {
|
|
|
|
return "max(" + fieldNamesString(sm.fields) + ")"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sm *statsMax) neededFields() []string {
|
|
|
|
return sm.fields
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sm *statsMax) newStatsProcessor() (statsProcessor, int) {
|
|
|
|
smp := &statsMaxProcessor{
|
2024-05-15 11:07:15 +00:00
|
|
|
sm: sm,
|
2024-04-30 23:58:35 +00:00
|
|
|
}
|
|
|
|
return smp, int(unsafe.Sizeof(*smp))
|
|
|
|
}
|
|
|
|
|
|
|
|
type statsMaxProcessor struct {
|
|
|
|
sm *statsMax
|
|
|
|
|
2024-05-15 11:07:15 +00:00
|
|
|
max string
|
|
|
|
hasMax bool
|
2024-04-30 23:58:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (smp *statsMaxProcessor) updateStatsForAllRows(br *blockResult) int {
|
2024-05-15 11:07:15 +00:00
|
|
|
maxLen := len(smp.max)
|
|
|
|
|
2024-04-30 23:58:35 +00:00
|
|
|
if smp.sm.containsStar {
|
2024-05-15 11:07:15 +00:00
|
|
|
// Find the minimum value across all the columns
|
2024-04-30 23:58:35 +00:00
|
|
|
for _, c := range br.getColumns() {
|
2024-05-15 11:23:51 +00:00
|
|
|
smp.updateStateForColumn(br, c)
|
2024-04-30 23:58:35 +00:00
|
|
|
}
|
2024-05-03 12:03:17 +00:00
|
|
|
} else {
|
2024-05-15 11:07:15 +00:00
|
|
|
// Find the minimum value across the requested columns
|
2024-05-03 12:03:17 +00:00
|
|
|
for _, field := range smp.sm.fields {
|
|
|
|
c := br.getColumnByName(field)
|
2024-05-15 11:23:51 +00:00
|
|
|
smp.updateStateForColumn(br, c)
|
2024-04-30 23:58:35 +00:00
|
|
|
}
|
|
|
|
}
|
2024-05-15 11:07:15 +00:00
|
|
|
|
|
|
|
return len(smp.max) - maxLen
|
2024-04-30 23:58:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (smp *statsMaxProcessor) updateStatsForRow(br *blockResult, rowIdx int) int {
|
2024-05-15 11:07:15 +00:00
|
|
|
maxLen := len(smp.max)
|
|
|
|
|
2024-04-30 23:58:35 +00:00
|
|
|
if smp.sm.containsStar {
|
2024-05-15 11:07:15 +00:00
|
|
|
// Find the minimum value across all the fields for the given row
|
2024-04-30 23:58:35 +00:00
|
|
|
for _, c := range br.getColumns() {
|
2024-05-15 11:07:15 +00:00
|
|
|
v := c.getValueAtRow(br, rowIdx)
|
2024-05-15 13:46:42 +00:00
|
|
|
smp.updateStateString(v)
|
2024-04-30 23:58:35 +00:00
|
|
|
}
|
2024-05-03 12:03:17 +00:00
|
|
|
} else {
|
2024-05-15 11:07:15 +00:00
|
|
|
// Find the minimum value across the requested fields for the given row
|
2024-05-03 12:03:17 +00:00
|
|
|
for _, field := range smp.sm.fields {
|
|
|
|
c := br.getColumnByName(field)
|
2024-05-15 11:07:15 +00:00
|
|
|
v := c.getValueAtRow(br, rowIdx)
|
2024-05-15 13:46:42 +00:00
|
|
|
smp.updateStateString(v)
|
2024-04-30 23:58:35 +00:00
|
|
|
}
|
|
|
|
}
|
2024-05-15 11:07:15 +00:00
|
|
|
|
|
|
|
return maxLen - len(smp.max)
|
2024-04-30 23:58:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (smp *statsMaxProcessor) mergeState(sfp statsProcessor) {
|
|
|
|
src := sfp.(*statsMaxProcessor)
|
2024-05-15 11:07:15 +00:00
|
|
|
if src.hasMax {
|
2024-05-15 13:46:42 +00:00
|
|
|
smp.updateStateString(src.max)
|
2024-04-30 23:58:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-15 11:23:51 +00:00
|
|
|
func (smp *statsMaxProcessor) updateStateForColumn(br *blockResult, c *blockResultColumn) {
|
2024-05-15 13:46:42 +00:00
|
|
|
if len(br.timestamps) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-05-15 11:23:51 +00:00
|
|
|
if c.isTime {
|
|
|
|
// Special case for time column
|
|
|
|
timestamps := br.timestamps
|
|
|
|
maxTimestamp := timestamps[len(timestamps)-1]
|
|
|
|
for _, timestamp := range timestamps[:len(timestamps)-1] {
|
|
|
|
if timestamp > maxTimestamp {
|
|
|
|
maxTimestamp = timestamp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bb := bbPool.Get()
|
2024-05-15 13:46:42 +00:00
|
|
|
bb.B = marshalTimestampRFC3339NanoString(bb.B[:0], maxTimestamp)
|
|
|
|
smp.updateStateBytes(bb.B)
|
2024-05-15 11:23:51 +00:00
|
|
|
bbPool.Put(bb)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if c.isConst {
|
|
|
|
// Special case for const column
|
|
|
|
v := c.encodedValues[0]
|
2024-05-15 13:46:42 +00:00
|
|
|
smp.updateStateString(v)
|
2024-05-15 11:23:51 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-05-15 13:46:42 +00:00
|
|
|
switch c.valueType {
|
|
|
|
case valueTypeString:
|
|
|
|
for _, v := range c.encodedValues {
|
|
|
|
smp.updateStateString(v)
|
|
|
|
}
|
|
|
|
case valueTypeDict:
|
|
|
|
for _, v := range c.dictValues {
|
|
|
|
smp.updateStateString(v)
|
|
|
|
}
|
|
|
|
case valueTypeUint8:
|
|
|
|
maxN := unmarshalUint8(c.encodedValues[0])
|
|
|
|
for _, v := range c.encodedValues[1:] {
|
|
|
|
n := unmarshalUint8(v)
|
|
|
|
if n > maxN {
|
|
|
|
maxN = n
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bb := bbPool.Get()
|
|
|
|
bb.B = marshalUint8String(bb.B[:0], maxN)
|
|
|
|
smp.updateStateBytes(bb.B)
|
|
|
|
bbPool.Put(bb)
|
|
|
|
case valueTypeUint16:
|
|
|
|
maxN := unmarshalUint16(c.encodedValues[0])
|
|
|
|
for _, v := range c.encodedValues[1:] {
|
|
|
|
n := unmarshalUint16(v)
|
|
|
|
if n > maxN {
|
|
|
|
maxN = n
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bb := bbPool.Get()
|
|
|
|
bb.B = marshalUint16String(bb.B[:0], maxN)
|
|
|
|
smp.updateStateBytes(bb.B)
|
|
|
|
bbPool.Put(bb)
|
|
|
|
case valueTypeUint32:
|
|
|
|
maxN := unmarshalUint32(c.encodedValues[0])
|
|
|
|
for _, v := range c.encodedValues[1:] {
|
|
|
|
n := unmarshalUint32(v)
|
|
|
|
if n > maxN {
|
|
|
|
maxN = n
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bb := bbPool.Get()
|
|
|
|
bb.B = marshalUint32String(bb.B[:0], maxN)
|
|
|
|
smp.updateStateBytes(bb.B)
|
|
|
|
bbPool.Put(bb)
|
|
|
|
case valueTypeUint64:
|
|
|
|
maxN := unmarshalUint64(c.encodedValues[0])
|
|
|
|
for _, v := range c.encodedValues[1:] {
|
|
|
|
n := unmarshalUint64(v)
|
|
|
|
if n > maxN {
|
|
|
|
maxN = n
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bb := bbPool.Get()
|
|
|
|
bb.B = marshalUint64String(bb.B[:0], maxN)
|
|
|
|
smp.updateStateBytes(bb.B)
|
|
|
|
bbPool.Put(bb)
|
|
|
|
case valueTypeFloat64:
|
|
|
|
maxF := unmarshalFloat64(c.encodedValues[0])
|
|
|
|
for _, v := range c.encodedValues[1:] {
|
|
|
|
f := unmarshalFloat64(v)
|
|
|
|
if math.IsNaN(maxF) || f > maxF {
|
|
|
|
maxF = f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bb := bbPool.Get()
|
|
|
|
bb.B = marshalFloat64String(bb.B[:0], maxF)
|
|
|
|
smp.updateStateBytes(bb.B)
|
|
|
|
bbPool.Put(bb)
|
|
|
|
case valueTypeIPv4:
|
|
|
|
maxIP := unmarshalIPv4(c.encodedValues[0])
|
|
|
|
for _, v := range c.encodedValues[1:] {
|
|
|
|
ip := unmarshalIPv4(v)
|
|
|
|
if ip > maxIP {
|
|
|
|
maxIP = ip
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bb := bbPool.Get()
|
|
|
|
bb.B = marshalIPv4String(bb.B[:0], maxIP)
|
|
|
|
smp.updateStateBytes(bb.B)
|
|
|
|
bbPool.Put(bb)
|
|
|
|
case valueTypeTimestampISO8601:
|
|
|
|
maxTimestamp := unmarshalTimestampISO8601(c.encodedValues[0])
|
|
|
|
for _, v := range c.encodedValues[1:] {
|
|
|
|
timestamp := unmarshalTimestampISO8601(v)
|
|
|
|
if timestamp > maxTimestamp {
|
|
|
|
maxTimestamp = timestamp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bb := bbPool.Get()
|
|
|
|
bb.B = marshalTimestampISO8601String(bb.B[:0], maxTimestamp)
|
|
|
|
smp.updateStateBytes(bb.B)
|
|
|
|
bbPool.Put(bb)
|
|
|
|
default:
|
|
|
|
logger.Panicf("BUG: unknown valueType=%d", c.valueType)
|
2024-05-15 11:23:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-15 13:46:42 +00:00
|
|
|
func (smp *statsMaxProcessor) updateStateBytes(b []byte) {
|
|
|
|
v := bytesutil.ToUnsafeString(b)
|
|
|
|
smp.updateStateString(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (smp *statsMaxProcessor) updateStateString(v string) {
|
2024-05-15 11:07:15 +00:00
|
|
|
if smp.hasMax && !lessString(smp.max, v) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
smp.max = strings.Clone(v)
|
|
|
|
smp.hasMax = true
|
|
|
|
}
|
|
|
|
|
2024-04-30 23:58:35 +00:00
|
|
|
func (smp *statsMaxProcessor) finalizeStats() string {
|
2024-05-15 11:07:15 +00:00
|
|
|
if !smp.hasMax {
|
|
|
|
return "NaN"
|
|
|
|
}
|
|
|
|
return smp.max
|
2024-04-30 23:58:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseStatsMax(lex *lexer) (*statsMax, error) {
|
2024-05-03 12:03:17 +00:00
|
|
|
fields, err := parseFieldNamesForStatsFunc(lex, "max")
|
2024-04-30 23:58:35 +00:00
|
|
|
if err != nil {
|
2024-05-03 09:15:09 +00:00
|
|
|
return nil, err
|
2024-04-30 23:58:35 +00:00
|
|
|
}
|
|
|
|
sm := &statsMax{
|
|
|
|
fields: fields,
|
|
|
|
containsStar: slices.Contains(fields, "*"),
|
|
|
|
}
|
|
|
|
return sm, nil
|
|
|
|
}
|