VictoriaMetrics/lib/logstorage/stats_min.go

113 lines
2.2 KiB
Go
Raw Normal View History

2024-05-01 00:08:37 +00:00
package logstorage
import (
"slices"
2024-05-15 11:07:15 +00:00
"strings"
2024-05-01 00:08:37 +00:00
"unsafe"
)
type statsMin struct {
fields []string
containsStar bool
}
func (sm *statsMin) String() string {
return "min(" + fieldNamesString(sm.fields) + ")"
}
func (sm *statsMin) neededFields() []string {
return sm.fields
}
func (sm *statsMin) newStatsProcessor() (statsProcessor, int) {
smp := &statsMinProcessor{
2024-05-15 11:07:15 +00:00
sm: sm,
2024-05-01 00:08:37 +00:00
}
return smp, int(unsafe.Sizeof(*smp))
}
type statsMinProcessor struct {
sm *statsMin
2024-05-15 11:07:15 +00:00
min string
hasMin bool
2024-05-01 00:08:37 +00:00
}
func (smp *statsMinProcessor) updateStatsForAllRows(br *blockResult) int {
2024-05-15 11:07:15 +00:00
minLen := len(smp.min)
2024-05-01 00:08:37 +00:00
if smp.sm.containsStar {
// Find the minimum value across all the columns
for _, c := range br.getColumns() {
2024-05-15 11:07:15 +00:00
for _, v := range c.getValues(br) {
smp.updateState(v)
2024-05-01 00:08:37 +00:00
}
}
2024-05-03 12:03:17 +00:00
} else {
// Find the minimum value across the requested columns
for _, field := range smp.sm.fields {
c := br.getColumnByName(field)
2024-05-15 11:07:15 +00:00
for _, v := range c.getValues(br) {
smp.updateState(v)
2024-05-03 12:03:17 +00:00
}
2024-05-01 00:08:37 +00:00
}
}
2024-05-15 11:07:15 +00:00
return len(smp.min) - minLen
2024-05-01 00:08:37 +00:00
}
func (smp *statsMinProcessor) updateStatsForRow(br *blockResult, rowIdx int) int {
2024-05-15 11:07:15 +00:00
minLen := len(smp.min)
2024-05-01 00:08:37 +00:00
if smp.sm.containsStar {
// Find the minimum value across all the fields for the given row
for _, c := range br.getColumns() {
2024-05-15 11:07:15 +00:00
v := c.getValueAtRow(br, rowIdx)
smp.updateState(v)
2024-05-01 00:08:37 +00:00
}
2024-05-03 12:03:17 +00:00
} else {
// Find the minimum value across the requested fields for the given row
for _, field := range smp.sm.fields {
c := br.getColumnByName(field)
2024-05-15 11:07:15 +00:00
v := c.getValueAtRow(br, rowIdx)
smp.updateState(v)
2024-05-01 00:08:37 +00:00
}
}
2024-05-15 11:07:15 +00:00
return minLen - len(smp.min)
2024-05-01 00:08:37 +00:00
}
func (smp *statsMinProcessor) mergeState(sfp statsProcessor) {
src := sfp.(*statsMinProcessor)
2024-05-15 11:07:15 +00:00
if src.hasMin {
smp.updateState(src.min)
2024-05-01 00:08:37 +00:00
}
}
2024-05-15 11:07:15 +00:00
func (smp *statsMinProcessor) updateState(v string) {
if smp.hasMin && !lessString(v, smp.min) {
return
}
smp.min = strings.Clone(v)
smp.hasMin = true
}
2024-05-01 00:08:37 +00:00
func (smp *statsMinProcessor) finalizeStats() string {
2024-05-15 11:07:15 +00:00
if !smp.hasMin {
return "NaN"
}
return smp.min
2024-05-01 00:08:37 +00:00
}
func parseStatsMin(lex *lexer) (*statsMin, error) {
2024-05-03 12:03:17 +00:00
fields, err := parseFieldNamesForStatsFunc(lex, "min")
2024-05-01 00:08:37 +00:00
if err != nil {
2024-05-03 09:15:09 +00:00
return nil, err
2024-05-01 00:08:37 +00:00
}
sm := &statsMin{
fields: fields,
containsStar: slices.Contains(fields, "*"),
}
return sm, nil
}