From 7319ad6a0ba2f8a6450d777530f9aa6248699515 Mon Sep 17 00:00:00 2001 From: Linas Medziunas Date: Fri, 8 Dec 2023 10:59:00 +0200 Subject: [PATCH] promql: simplify Native Histogram arithmetics Signed-off-by: Linas Medziunas --- promql/engine.go | 33 +++++---------------------------- promql/functions.go | 19 +++---------------- 2 files changed, 8 insertions(+), 44 deletions(-) diff --git a/promql/engine.go b/promql/engine.go index 4fa2a513b..4e0900686 100644 --- a/promql/engine.go +++ b/promql/engine.go @@ -2478,22 +2478,12 @@ func vectorElemBinop(op parser.ItemType, lhs, rhs float64, hlhs, hrhs *histogram switch op { case parser.ADD: if hlhs != nil && hrhs != nil { - // The histogram being added must have the larger schema - // code (i.e. the higher resolution). - if hrhs.Schema >= hlhs.Schema { - return 0, hlhs.Copy().Add(hrhs).Compact(0), true - } - return 0, hrhs.Copy().Add(hlhs).Compact(0), true + return 0, hlhs.Copy().Add(hrhs).Compact(0), true } return lhs + rhs, nil, true case parser.SUB: if hlhs != nil && hrhs != nil { - // The histogram being subtracted must have the larger schema - // code (i.e. the higher resolution). - if hrhs.Schema >= hlhs.Schema { - return 0, hlhs.Copy().Sub(hrhs).Compact(0), true - } - return 0, hrhs.Copy().Mul(-1).Add(hlhs).Compact(0), true + return 0, hlhs.Copy().Sub(hrhs).Compact(0), true } return lhs - rhs, nil, true case parser.MUL: @@ -2678,13 +2668,7 @@ func (ev *evaluator) aggregation(e *parser.AggregateExpr, grouping []string, par if s.H != nil { group.hasHistogram = true if group.histogramValue != nil { - // The histogram being added must have - // an equal or larger schema. - if s.H.Schema >= group.histogramValue.Schema { - group.histogramValue.Add(s.H) - } else { - group.histogramValue = s.H.Copy().Add(group.histogramValue) - } + group.histogramValue.Add(s.H) } // Otherwise the aggregation contained floats // previously and will be invalid anyway. No @@ -2701,15 +2685,8 @@ func (ev *evaluator) aggregation(e *parser.AggregateExpr, grouping []string, par if group.histogramMean != nil { left := s.H.Copy().Div(float64(group.groupCount)) right := group.histogramMean.Copy().Div(float64(group.groupCount)) - // The histogram being added/subtracted must have - // an equal or larger schema. - if s.H.Schema >= group.histogramMean.Schema { - toAdd := right.Mul(-1).Add(left) - group.histogramMean.Add(toAdd) - } else { - toAdd := left.Sub(right) - group.histogramMean = toAdd.Add(group.histogramMean) - } + toAdd := left.Sub(right) + group.histogramMean.Add(toAdd) } // Otherwise the aggregation contained floats // previously and will be invalid anyway. No diff --git a/promql/functions.go b/promql/functions.go index 06f6f8c71..64066edbb 100644 --- a/promql/functions.go +++ b/promql/functions.go @@ -532,15 +532,8 @@ func funcAvgOverTime(vals []parser.Value, args parser.Expressions, enh *EvalNode count++ left := h.H.Copy().Div(float64(count)) right := mean.Copy().Div(float64(count)) - // The histogram being added/subtracted must have - // an equal or larger schema. - if h.H.Schema >= mean.Schema { - toAdd := right.Mul(-1).Add(left) - mean.Add(toAdd) - } else { - toAdd := left.Sub(right) - mean = toAdd.Add(mean) - } + toAdd := left.Sub(right) + mean.Add(toAdd) } return mean }), nil @@ -661,13 +654,7 @@ func funcSumOverTime(vals []parser.Value, args parser.Expressions, enh *EvalNode return aggrHistOverTime(vals, enh, func(s Series) *histogram.FloatHistogram { sum := s.Histograms[0].H.Copy() for _, h := range s.Histograms[1:] { - // The histogram being added must have - // an equal or larger schema. - if h.H.Schema >= sum.Schema { - sum.Add(h.H) - } else { - sum = h.H.Copy().Add(sum) - } + sum.Add(h.H) } return sum }), nil