2019-05-22 23:16:55 +02:00
|
|
|
package promql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/storage"
|
2020-04-28 14:28:22 +02:00
|
|
|
"github.com/VictoriaMetrics/metricsql"
|
|
|
|
"github.com/VictoriaMetrics/metricsql/binaryop"
|
2019-05-22 23:16:55 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var binaryOpFuncs = map[string]binaryOpFunc{
|
2019-12-25 20:35:47 +01:00
|
|
|
"+": newBinaryOpArithFunc(binaryop.Plus),
|
|
|
|
"-": newBinaryOpArithFunc(binaryop.Minus),
|
|
|
|
"*": newBinaryOpArithFunc(binaryop.Mul),
|
|
|
|
"/": newBinaryOpArithFunc(binaryop.Div),
|
|
|
|
"%": newBinaryOpArithFunc(binaryop.Mod),
|
|
|
|
"^": newBinaryOpArithFunc(binaryop.Pow),
|
2019-05-22 23:16:55 +02:00
|
|
|
|
2021-10-11 20:15:51 +02:00
|
|
|
// See https://github.com/prometheus/prometheus/pull/9248
|
|
|
|
"atan2": newBinaryOpArithFunc(binaryop.Atan2),
|
|
|
|
|
2019-05-22 23:16:55 +02:00
|
|
|
// cmp ops
|
2019-12-25 20:35:47 +01:00
|
|
|
"==": newBinaryOpCmpFunc(binaryop.Eq),
|
|
|
|
"!=": newBinaryOpCmpFunc(binaryop.Neq),
|
|
|
|
">": newBinaryOpCmpFunc(binaryop.Gt),
|
|
|
|
"<": newBinaryOpCmpFunc(binaryop.Lt),
|
|
|
|
">=": newBinaryOpCmpFunc(binaryop.Gte),
|
|
|
|
"<=": newBinaryOpCmpFunc(binaryop.Lte),
|
2019-05-22 23:16:55 +02:00
|
|
|
|
|
|
|
// logical set ops
|
|
|
|
"and": binaryOpAnd,
|
|
|
|
"or": binaryOpOr,
|
|
|
|
"unless": binaryOpUnless,
|
|
|
|
|
2020-01-31 17:52:30 +01:00
|
|
|
// New ops
|
2019-12-25 20:35:47 +01:00
|
|
|
"if": newBinaryOpArithFunc(binaryop.If),
|
|
|
|
"ifnot": newBinaryOpArithFunc(binaryop.Ifnot),
|
|
|
|
"default": newBinaryOpArithFunc(binaryop.Default),
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func getBinaryOpFunc(op string) binaryOpFunc {
|
|
|
|
op = strings.ToLower(op)
|
|
|
|
return binaryOpFuncs[op]
|
|
|
|
}
|
|
|
|
|
|
|
|
type binaryOpFuncArg struct {
|
2019-12-25 20:35:47 +01:00
|
|
|
be *metricsql.BinaryOpExpr
|
2019-05-22 23:16:55 +02:00
|
|
|
left []*timeseries
|
|
|
|
right []*timeseries
|
|
|
|
}
|
|
|
|
|
|
|
|
type binaryOpFunc func(bfa *binaryOpFuncArg) ([]*timeseries, error)
|
|
|
|
|
|
|
|
func newBinaryOpCmpFunc(cf func(left, right float64) bool) binaryOpFunc {
|
|
|
|
cfe := func(left, right float64, isBool bool) float64 {
|
|
|
|
if !isBool {
|
|
|
|
if cf(left, right) {
|
|
|
|
return left
|
|
|
|
}
|
|
|
|
return nan
|
|
|
|
}
|
2020-12-01 23:28:16 +01:00
|
|
|
if math.IsNaN(left) {
|
|
|
|
return nan
|
|
|
|
}
|
2021-09-30 11:24:17 +02:00
|
|
|
if cf(left, right) {
|
|
|
|
return 1
|
|
|
|
}
|
2019-05-22 23:16:55 +02:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return newBinaryOpFunc(cfe)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newBinaryOpArithFunc(af func(left, right float64) float64) binaryOpFunc {
|
|
|
|
afe := func(left, right float64, isBool bool) float64 {
|
|
|
|
return af(left, right)
|
|
|
|
}
|
|
|
|
return newBinaryOpFunc(afe)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newBinaryOpFunc(bf func(left, right float64, isBool bool) float64) binaryOpFunc {
|
|
|
|
return func(bfa *binaryOpFuncArg) ([]*timeseries, error) {
|
2022-04-20 21:18:39 +02:00
|
|
|
left := bfa.left
|
|
|
|
right := bfa.right
|
|
|
|
switch bfa.be.Op {
|
|
|
|
case "ifnot":
|
|
|
|
left = removeEmptySeries(left)
|
|
|
|
// Do not remove empty series on the right side,
|
|
|
|
// so the left-side series could be matched against them.
|
|
|
|
case "default":
|
2022-04-21 10:42:42 +02:00
|
|
|
// Do not remove empty series on the left and the right side,
|
|
|
|
// since this may result in missing result:
|
|
|
|
// - if empty time series are removed on the left side,
|
|
|
|
// then they won't be substituted by time series from the right side.
|
|
|
|
// - if empty time series are removed on the right side,
|
|
|
|
// then this may result in missing time series from the left side.
|
2022-04-20 21:18:39 +02:00
|
|
|
default:
|
|
|
|
left = removeEmptySeries(left)
|
|
|
|
right = removeEmptySeries(right)
|
|
|
|
}
|
|
|
|
if len(left) == 0 || len(right) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
left, right, dst, err := adjustBinaryOpTags(bfa.be, left, right)
|
2019-05-22 23:16:55 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(left) != len(right) || len(left) != len(dst) {
|
|
|
|
logger.Panicf("BUG: len(left) must match len(right) and len(dst); got %d vs %d vs %d", len(left), len(right), len(dst))
|
|
|
|
}
|
2022-04-20 21:18:39 +02:00
|
|
|
isBool := bfa.be.Bool
|
2019-05-22 23:16:55 +02:00
|
|
|
for i, tsLeft := range left {
|
|
|
|
leftValues := tsLeft.Values
|
|
|
|
rightValues := right[i].Values
|
|
|
|
dstValues := dst[i].Values
|
|
|
|
if len(leftValues) != len(rightValues) || len(leftValues) != len(dstValues) {
|
|
|
|
logger.Panicf("BUG: len(leftVaues) must match len(rightValues) and len(dstValues); got %d vs %d vs %d",
|
|
|
|
len(leftValues), len(rightValues), len(dstValues))
|
|
|
|
}
|
|
|
|
for j, a := range leftValues {
|
|
|
|
b := rightValues[j]
|
|
|
|
dstValues[j] = bf(a, b, isBool)
|
|
|
|
}
|
|
|
|
}
|
2021-07-16 00:43:01 +02:00
|
|
|
// Do not remove time series containing only NaNs, since then the `(foo op bar) default N`
|
|
|
|
// won't work as expected if `(foo op bar)` results to NaN series.
|
2019-05-22 23:16:55 +02:00
|
|
|
return dst, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-25 20:35:47 +01:00
|
|
|
func adjustBinaryOpTags(be *metricsql.BinaryOpExpr, left, right []*timeseries) ([]*timeseries, []*timeseries, []*timeseries, error) {
|
2019-05-22 23:16:55 +02:00
|
|
|
if len(be.GroupModifier.Op) == 0 && len(be.JoinModifier.Op) == 0 {
|
|
|
|
if isScalar(left) {
|
|
|
|
// Fast path: `scalar op vector`
|
|
|
|
rvsLeft := make([]*timeseries, len(right))
|
|
|
|
tsLeft := left[0]
|
|
|
|
for i, tsRight := range right {
|
2019-06-21 12:07:59 +02:00
|
|
|
resetMetricGroupIfRequired(be, tsRight)
|
2019-05-22 23:16:55 +02:00
|
|
|
rvsLeft[i] = tsLeft
|
|
|
|
}
|
|
|
|
return rvsLeft, right, right, nil
|
|
|
|
}
|
|
|
|
if isScalar(right) {
|
|
|
|
// Fast path: `vector op scalar`
|
|
|
|
rvsRight := make([]*timeseries, len(left))
|
|
|
|
tsRight := right[0]
|
|
|
|
for i, tsLeft := range left {
|
2019-06-21 12:07:59 +02:00
|
|
|
resetMetricGroupIfRequired(be, tsLeft)
|
2019-05-22 23:16:55 +02:00
|
|
|
rvsRight[i] = tsRight
|
|
|
|
}
|
|
|
|
return left, rvsRight, left, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Slow path: `vector op vector` or `a op {on|ignoring} {group_left|group_right} b`
|
|
|
|
var rvsLeft, rvsRight []*timeseries
|
|
|
|
mLeft, mRight := createTimeseriesMapByTagSet(be, left, right)
|
|
|
|
joinOp := strings.ToLower(be.JoinModifier.Op)
|
2019-09-13 16:40:14 +02:00
|
|
|
groupOp := strings.ToLower(be.GroupModifier.Op)
|
|
|
|
if len(groupOp) == 0 {
|
|
|
|
groupOp = "ignoring"
|
|
|
|
}
|
|
|
|
groupTags := be.GroupModifier.Args
|
2019-05-22 23:16:55 +02:00
|
|
|
for k, tssLeft := range mLeft {
|
|
|
|
tssRight := mRight[k]
|
|
|
|
if len(tssRight) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch joinOp {
|
|
|
|
case "group_left":
|
2019-09-13 16:40:14 +02:00
|
|
|
var err error
|
|
|
|
rvsLeft, rvsRight, err = groupJoin("right", be, rvsLeft, rvsRight, tssLeft, tssRight)
|
|
|
|
if err != nil {
|
2019-05-22 23:16:55 +02:00
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
case "group_right":
|
2019-09-13 16:40:14 +02:00
|
|
|
var err error
|
|
|
|
rvsRight, rvsLeft, err = groupJoin("left", be, rvsRight, rvsLeft, tssRight, tssLeft)
|
|
|
|
if err != nil {
|
2019-05-22 23:16:55 +02:00
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
case "":
|
2019-09-13 16:40:14 +02:00
|
|
|
if err := ensureSingleTimeseries("left", be, tssLeft); err != nil {
|
2019-05-22 23:16:55 +02:00
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
2019-09-13 16:40:14 +02:00
|
|
|
if err := ensureSingleTimeseries("right", be, tssRight); err != nil {
|
2019-05-22 23:16:55 +02:00
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
2019-09-13 16:40:14 +02:00
|
|
|
tsLeft := tssLeft[0]
|
|
|
|
resetMetricGroupIfRequired(be, tsLeft)
|
|
|
|
switch groupOp {
|
|
|
|
case "on":
|
|
|
|
tsLeft.MetricName.RemoveTagsOn(groupTags)
|
|
|
|
case "ignoring":
|
|
|
|
tsLeft.MetricName.RemoveTagsIgnoring(groupTags)
|
|
|
|
default:
|
|
|
|
logger.Panicf("BUG: unexpected binary op modifier %q", groupOp)
|
|
|
|
}
|
|
|
|
rvsLeft = append(rvsLeft, tsLeft)
|
2019-05-22 23:16:55 +02:00
|
|
|
rvsRight = append(rvsRight, tssRight[0])
|
|
|
|
default:
|
2019-09-13 16:40:14 +02:00
|
|
|
logger.Panicf("BUG: unexpected join modifier %q", joinOp)
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
dst := rvsLeft
|
|
|
|
if joinOp == "group_right" {
|
|
|
|
dst = rvsRight
|
|
|
|
}
|
|
|
|
return rvsLeft, rvsRight, dst, nil
|
|
|
|
}
|
|
|
|
|
2019-12-25 20:35:47 +01:00
|
|
|
func ensureSingleTimeseries(side string, be *metricsql.BinaryOpExpr, tss []*timeseries) error {
|
2019-09-13 16:40:14 +02:00
|
|
|
if len(tss) == 0 {
|
|
|
|
logger.Panicf("BUG: tss must contain at least one value")
|
|
|
|
}
|
|
|
|
for len(tss) > 1 {
|
|
|
|
if !mergeNonOverlappingTimeseries(tss[0], tss[len(tss)-1]) {
|
|
|
|
return fmt.Errorf(`duplicate time series on the %s side of %s %s: %s and %s`, side, be.Op, be.GroupModifier.AppendString(nil),
|
|
|
|
stringMetricTags(&tss[0].MetricName), stringMetricTags(&tss[len(tss)-1].MetricName))
|
|
|
|
}
|
|
|
|
tss = tss[:len(tss)-1]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-25 20:35:47 +01:00
|
|
|
func groupJoin(singleTimeseriesSide string, be *metricsql.BinaryOpExpr, rvsLeft, rvsRight, tssLeft, tssRight []*timeseries) ([]*timeseries, []*timeseries, error) {
|
2019-09-13 16:40:14 +02:00
|
|
|
joinTags := be.JoinModifier.Args
|
2022-04-20 21:18:39 +02:00
|
|
|
type tsPair struct {
|
|
|
|
left *timeseries
|
|
|
|
right *timeseries
|
|
|
|
}
|
|
|
|
m := make(map[string]*tsPair)
|
2019-09-13 16:40:14 +02:00
|
|
|
for _, tsLeft := range tssLeft {
|
|
|
|
resetMetricGroupIfRequired(be, tsLeft)
|
|
|
|
if len(tssRight) == 1 {
|
|
|
|
// Easy case - right part contains only a single matching time series.
|
2020-06-21 15:32:00 +02:00
|
|
|
tsLeft.MetricName.SetTags(joinTags, &tssRight[0].MetricName)
|
2019-09-13 16:40:14 +02:00
|
|
|
rvsLeft = append(rvsLeft, tsLeft)
|
|
|
|
rvsRight = append(rvsRight, tssRight[0])
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hard case - right part contains multiple matching time series.
|
|
|
|
// Verify it doesn't result in duplicate MetricName values after adding missing tags.
|
2022-04-20 21:18:39 +02:00
|
|
|
for k := range m {
|
|
|
|
delete(m, k)
|
2019-09-13 16:40:14 +02:00
|
|
|
}
|
|
|
|
bb := bbPool.Get()
|
|
|
|
for _, tsRight := range tssRight {
|
|
|
|
var tsCopy timeseries
|
|
|
|
tsCopy.CopyFromShallowTimestamps(tsLeft)
|
2020-06-21 15:32:00 +02:00
|
|
|
tsCopy.MetricName.SetTags(joinTags, &tsRight.MetricName)
|
2019-09-13 16:40:14 +02:00
|
|
|
bb.B = marshalMetricTagsSorted(bb.B[:0], &tsCopy.MetricName)
|
2022-04-20 21:18:39 +02:00
|
|
|
pair, ok := m[string(bb.B)]
|
|
|
|
if !ok {
|
|
|
|
m[string(bb.B)] = &tsPair{
|
|
|
|
left: &tsCopy,
|
|
|
|
right: tsRight,
|
2019-09-13 16:40:14 +02:00
|
|
|
}
|
2022-04-20 21:18:39 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Try merging pair.right with tsRight if they don't overlap.
|
|
|
|
var tmp timeseries
|
|
|
|
tmp.CopyFromShallowTimestamps(pair.right)
|
|
|
|
if !mergeNonOverlappingTimeseries(&tmp, tsRight) {
|
2019-09-13 16:40:14 +02:00
|
|
|
return nil, nil, fmt.Errorf("duplicate time series on the %s side of `%s %s %s`: %s and %s",
|
|
|
|
singleTimeseriesSide, be.Op, be.GroupModifier.AppendString(nil), be.JoinModifier.AppendString(nil),
|
2022-04-20 21:18:39 +02:00
|
|
|
stringMetricTags(&tmp.MetricName), stringMetricTags(&tsRight.MetricName))
|
2019-09-13 16:40:14 +02:00
|
|
|
}
|
2022-04-20 21:18:39 +02:00
|
|
|
pair.right = &tmp
|
2019-09-13 16:40:14 +02:00
|
|
|
}
|
|
|
|
bbPool.Put(bb)
|
2022-04-20 21:18:39 +02:00
|
|
|
for _, pair := range m {
|
|
|
|
rvsLeft = append(rvsLeft, pair.left)
|
|
|
|
rvsRight = append(rvsRight, pair.right)
|
|
|
|
}
|
2019-09-13 16:40:14 +02:00
|
|
|
}
|
|
|
|
return rvsLeft, rvsRight, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func mergeNonOverlappingTimeseries(dst, src *timeseries) bool {
|
|
|
|
// Verify whether the time series can be merged.
|
|
|
|
srcValues := src.Values
|
|
|
|
dstValues := dst.Values
|
2021-02-18 21:07:29 +01:00
|
|
|
overlaps := 0
|
2019-09-13 16:40:14 +02:00
|
|
|
_ = dstValues[len(srcValues)-1]
|
|
|
|
for i, v := range srcValues {
|
|
|
|
if math.IsNaN(v) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !math.IsNaN(dstValues[i]) {
|
2021-02-18 21:07:29 +01:00
|
|
|
overlaps++
|
2019-09-13 16:40:14 +02:00
|
|
|
}
|
|
|
|
}
|
2021-02-18 21:07:29 +01:00
|
|
|
// Allow up to two overlapping datapoints, which can appear due to staleness algorithm,
|
|
|
|
// which can add a few datapoints in the end of time series.
|
|
|
|
if overlaps > 2 {
|
|
|
|
return false
|
|
|
|
}
|
2021-03-25 12:55:54 +01:00
|
|
|
// Do not merge time series with too small number of datapoints.
|
|
|
|
// This can be the case during evaluation of instant queries (alerting or recording rules).
|
|
|
|
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1141
|
|
|
|
if len(srcValues) <= 2 && len(dstValues) <= 2 {
|
|
|
|
return false
|
|
|
|
}
|
2019-09-13 16:40:14 +02:00
|
|
|
// Time series can be merged. Merge them.
|
|
|
|
for i, v := range srcValues {
|
|
|
|
if math.IsNaN(v) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
dstValues[i] = v
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-12-25 20:35:47 +01:00
|
|
|
func resetMetricGroupIfRequired(be *metricsql.BinaryOpExpr, ts *timeseries) {
|
|
|
|
if metricsql.IsBinaryOpCmp(be.Op) && !be.Bool {
|
2019-06-21 12:07:59 +02:00
|
|
|
// Do not reset MetricGroup for non-boolean `compare` binary ops like Prometheus does.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch be.Op {
|
|
|
|
case "default", "if", "ifnot":
|
|
|
|
// Do not reset MetricGroup for these ops.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ts.MetricName.ResetMetricGroup()
|
|
|
|
}
|
|
|
|
|
2019-05-22 23:16:55 +02:00
|
|
|
func binaryOpAnd(bfa *binaryOpFuncArg) ([]*timeseries, error) {
|
|
|
|
mLeft, mRight := createTimeseriesMapByTagSet(bfa.be, bfa.left, bfa.right)
|
|
|
|
var rvs []*timeseries
|
2020-01-31 17:52:30 +01:00
|
|
|
for k, tssRight := range mRight {
|
|
|
|
tssLeft := mLeft[k]
|
|
|
|
if tssLeft == nil {
|
|
|
|
continue
|
|
|
|
}
|
2021-09-23 23:46:22 +02:00
|
|
|
// Add gaps to tssLeft if there are gaps at tssRight.
|
2020-07-09 12:05:42 +02:00
|
|
|
for _, tsLeft := range tssLeft {
|
|
|
|
valuesLeft := tsLeft.Values
|
2021-09-23 23:46:22 +02:00
|
|
|
for i := range valuesLeft {
|
|
|
|
hasValue := false
|
|
|
|
for _, tsRight := range tssRight {
|
|
|
|
if !math.IsNaN(tsRight.Values[i]) {
|
|
|
|
hasValue = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !hasValue {
|
2020-07-09 12:05:42 +02:00
|
|
|
valuesLeft[i] = nan
|
|
|
|
}
|
2020-01-31 17:52:30 +01:00
|
|
|
}
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
2022-04-20 18:53:24 +02:00
|
|
|
tssLeft = removeEmptySeries(tssLeft)
|
2020-01-31 17:52:30 +01:00
|
|
|
rvs = append(rvs, tssLeft...)
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
return rvs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func binaryOpOr(bfa *binaryOpFuncArg) ([]*timeseries, error) {
|
|
|
|
mLeft, mRight := createTimeseriesMapByTagSet(bfa.be, bfa.left, bfa.right)
|
|
|
|
var rvs []*timeseries
|
|
|
|
for _, tss := range mLeft {
|
|
|
|
rvs = append(rvs, tss...)
|
|
|
|
}
|
2020-06-18 22:04:59 +02:00
|
|
|
for k, tssRight := range mRight {
|
|
|
|
tssLeft := mLeft[k]
|
|
|
|
if tssLeft == nil {
|
|
|
|
rvs = append(rvs, tssRight...)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Fill gaps in tssLeft with values from tssRight as Prometheus does.
|
|
|
|
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/552
|
|
|
|
for _, tsLeft := range tssLeft {
|
|
|
|
valuesLeft := tsLeft.Values
|
|
|
|
for i, v := range valuesLeft {
|
2021-09-23 23:46:22 +02:00
|
|
|
if !math.IsNaN(v) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, tsRight := range tssRight {
|
|
|
|
vRight := tsRight.Values[i]
|
|
|
|
if !math.IsNaN(vRight) {
|
|
|
|
valuesLeft[i] = vRight
|
|
|
|
break
|
|
|
|
}
|
2020-06-18 22:04:59 +02:00
|
|
|
}
|
|
|
|
}
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return rvs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func binaryOpUnless(bfa *binaryOpFuncArg) ([]*timeseries, error) {
|
|
|
|
mLeft, mRight := createTimeseriesMapByTagSet(bfa.be, bfa.left, bfa.right)
|
|
|
|
var rvs []*timeseries
|
2020-01-31 17:52:30 +01:00
|
|
|
for k, tssLeft := range mLeft {
|
|
|
|
tssRight := mRight[k]
|
|
|
|
if tssRight == nil {
|
|
|
|
rvs = append(rvs, tssLeft...)
|
|
|
|
continue
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
2021-09-23 23:46:22 +02:00
|
|
|
// Add gaps to tssLeft if the are no gaps at tssRight.
|
2020-07-09 12:05:42 +02:00
|
|
|
for _, tsLeft := range tssLeft {
|
|
|
|
valuesLeft := tsLeft.Values
|
2021-09-23 23:46:22 +02:00
|
|
|
for i := range valuesLeft {
|
|
|
|
for _, tsRight := range tssRight {
|
|
|
|
if !math.IsNaN(tsRight.Values[i]) {
|
|
|
|
valuesLeft[i] = nan
|
|
|
|
break
|
|
|
|
}
|
2020-07-09 12:05:42 +02:00
|
|
|
}
|
2020-01-31 17:52:30 +01:00
|
|
|
}
|
|
|
|
}
|
2022-04-20 18:53:24 +02:00
|
|
|
tssLeft = removeEmptySeries(tssLeft)
|
2020-01-31 17:52:30 +01:00
|
|
|
rvs = append(rvs, tssLeft...)
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
return rvs, nil
|
|
|
|
}
|
|
|
|
|
2019-12-25 20:35:47 +01:00
|
|
|
func createTimeseriesMapByTagSet(be *metricsql.BinaryOpExpr, left, right []*timeseries) (map[string][]*timeseries, map[string][]*timeseries) {
|
2019-05-22 23:16:55 +02:00
|
|
|
groupTags := be.GroupModifier.Args
|
|
|
|
groupOp := strings.ToLower(be.GroupModifier.Op)
|
|
|
|
if len(groupOp) == 0 {
|
|
|
|
groupOp = "ignoring"
|
|
|
|
}
|
|
|
|
getTagsMap := func(arg []*timeseries) map[string][]*timeseries {
|
|
|
|
bb := bbPool.Get()
|
|
|
|
m := make(map[string][]*timeseries, len(arg))
|
|
|
|
mn := storage.GetMetricName()
|
|
|
|
for _, ts := range arg {
|
|
|
|
mn.CopyFrom(&ts.MetricName)
|
|
|
|
mn.ResetMetricGroup()
|
|
|
|
switch groupOp {
|
|
|
|
case "on":
|
|
|
|
mn.RemoveTagsOn(groupTags)
|
|
|
|
case "ignoring":
|
|
|
|
mn.RemoveTagsIgnoring(groupTags)
|
|
|
|
default:
|
|
|
|
logger.Panicf("BUG: unexpected binary op modifier %q", groupOp)
|
|
|
|
}
|
|
|
|
bb.B = marshalMetricTagsSorted(bb.B[:0], mn)
|
|
|
|
m[string(bb.B)] = append(m[string(bb.B)], ts)
|
|
|
|
}
|
|
|
|
storage.PutMetricName(mn)
|
|
|
|
bbPool.Put(bb)
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
mLeft := getTagsMap(left)
|
|
|
|
mRight := getTagsMap(right)
|
|
|
|
return mLeft, mRight
|
|
|
|
}
|
|
|
|
|
|
|
|
func isScalar(arg []*timeseries) bool {
|
|
|
|
if len(arg) != 1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
mn := &arg[0].MetricName
|
|
|
|
if len(mn.MetricGroup) > 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return len(mn.Tags) == 0
|
|
|
|
}
|