|
| 1 | +// SPDX-License-Identifier: AGPL-3.0-only |
| 2 | + |
| 3 | +package operators |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + |
| 9 | + "github.com/prometheus/prometheus/model/histogram" |
| 10 | + "github.com/prometheus/prometheus/promql" |
| 11 | + "github.com/prometheus/prometheus/promql/parser" |
| 12 | + "github.com/prometheus/prometheus/promql/parser/posrange" |
| 13 | + "github.com/prometheus/prometheus/util/annotations" |
| 14 | + |
| 15 | + "github.com/grafana/mimir/pkg/streamingpromql/compat" |
| 16 | + "github.com/grafana/mimir/pkg/streamingpromql/functions" |
| 17 | + "github.com/grafana/mimir/pkg/streamingpromql/limiting" |
| 18 | + "github.com/grafana/mimir/pkg/streamingpromql/types" |
| 19 | +) |
| 20 | + |
| 21 | +// VectorScalarBinaryOperation represents a binary operation between an instant vector and a scalar such as "<expr> + 2" or "3 * <expr>". |
| 22 | +type VectorScalarBinaryOperation struct { |
| 23 | + Scalar types.ScalarOperator |
| 24 | + Vector types.InstantVectorOperator |
| 25 | + ScalarIsLeftSide bool |
| 26 | + Op parser.ItemType |
| 27 | + MemoryConsumptionTracker *limiting.MemoryConsumptionTracker |
| 28 | + |
| 29 | + start int64 // Milliseconds since Unix epoch |
| 30 | + end int64 // Milliseconds since Unix epoch |
| 31 | + interval int64 // In milliseconds |
| 32 | + stepCount int |
| 33 | + |
| 34 | + opFunc vectorScalarBinaryOperationFunc |
| 35 | + |
| 36 | + expressionPosition posrange.PositionRange |
| 37 | + emitAnnotation functions.EmitAnnotationFunc |
| 38 | + scalarData types.ScalarData |
| 39 | + vectorIterator types.InstantVectorSeriesDataIterator |
| 40 | +} |
| 41 | + |
| 42 | +type vectorScalarBinaryOperationFunc func(scalar float64, vectorF float64, vectorH *histogram.FloatHistogram) (float64, *histogram.FloatHistogram, bool, error) |
| 43 | + |
| 44 | +func NewVectorScalarBinaryOperation( |
| 45 | + scalar types.ScalarOperator, |
| 46 | + vector types.InstantVectorOperator, |
| 47 | + scalarIsLeftSide bool, |
| 48 | + op parser.ItemType, |
| 49 | + start int64, |
| 50 | + end int64, |
| 51 | + interval int64, |
| 52 | + memoryConsumptionTracker *limiting.MemoryConsumptionTracker, |
| 53 | + annotations *annotations.Annotations, |
| 54 | + expressionPosition posrange.PositionRange, |
| 55 | +) (*VectorScalarBinaryOperation, error) { |
| 56 | + f := arithmeticOperationFuncs[op] |
| 57 | + if f == nil { |
| 58 | + return nil, compat.NewNotSupportedError(fmt.Sprintf("binary expression with '%s'", op)) |
| 59 | + } |
| 60 | + |
| 61 | + b := &VectorScalarBinaryOperation{ |
| 62 | + Scalar: scalar, |
| 63 | + Vector: vector, |
| 64 | + ScalarIsLeftSide: scalarIsLeftSide, |
| 65 | + Op: op, |
| 66 | + MemoryConsumptionTracker: memoryConsumptionTracker, |
| 67 | + |
| 68 | + start: start, |
| 69 | + end: end, |
| 70 | + interval: interval, |
| 71 | + stepCount: stepCount(start, end, interval), |
| 72 | + |
| 73 | + expressionPosition: expressionPosition, |
| 74 | + } |
| 75 | + |
| 76 | + b.emitAnnotation = func(generator functions.AnnotationGenerator) { |
| 77 | + annotations.Add(generator("", expressionPosition)) |
| 78 | + } |
| 79 | + |
| 80 | + if b.ScalarIsLeftSide { |
| 81 | + b.opFunc = func(scalar float64, vectorF float64, vectorH *histogram.FloatHistogram) (float64, *histogram.FloatHistogram, bool, error) { |
| 82 | + return f(scalar, vectorF, nil, vectorH) |
| 83 | + } |
| 84 | + } else { |
| 85 | + b.opFunc = func(scalar float64, vectorF float64, vectorH *histogram.FloatHistogram) (float64, *histogram.FloatHistogram, bool, error) { |
| 86 | + return f(vectorF, scalar, vectorH, nil) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return b, nil |
| 91 | +} |
| 92 | + |
| 93 | +func (v *VectorScalarBinaryOperation) SeriesMetadata(ctx context.Context) ([]types.SeriesMetadata, error) { |
| 94 | + // Get the scalar values once, now, rather than having to do this later in NextSeries. |
| 95 | + var err error |
| 96 | + v.scalarData, err = v.Scalar.GetValues(ctx) |
| 97 | + if err != nil { |
| 98 | + return nil, err |
| 99 | + } |
| 100 | + |
| 101 | + metadata, err := v.Vector.SeriesMetadata(ctx) |
| 102 | + if err != nil { |
| 103 | + return nil, err |
| 104 | + } |
| 105 | + |
| 106 | + // We don't need to do deduplication and merging of series in this operator: we expect that this operator |
| 107 | + // is wrapped in a DeduplicateAndMerge. |
| 108 | + metadata, err = functions.DropSeriesName(metadata, v.MemoryConsumptionTracker) |
| 109 | + if err != nil { |
| 110 | + return nil, err |
| 111 | + } |
| 112 | + |
| 113 | + return metadata, nil |
| 114 | +} |
| 115 | + |
| 116 | +func (v *VectorScalarBinaryOperation) NextSeries(ctx context.Context) (types.InstantVectorSeriesData, error) { |
| 117 | + series, err := v.Vector.NextSeries(ctx) |
| 118 | + if err != nil { |
| 119 | + return types.InstantVectorSeriesData{}, err |
| 120 | + } |
| 121 | + |
| 122 | + returnInputFPointSlice := true |
| 123 | + returnInputHPointSlice := true |
| 124 | + |
| 125 | + // We cannot re-use any slices when the series contains a mix of floats and histograms. |
| 126 | + // For example, imagine the series has [histogram, histogram, float, float, histogram] and we're performing the |
| 127 | + // operation "2 + series". |
| 128 | + // Histograms are treated as 0, so each input point produces a float. |
| 129 | + // If we reuse the input series' FPoint slice, we'll overwrite the later float points while processing the earlier |
| 130 | + // histogram points. |
| 131 | + // This shouldn't happen often, so we don't mind the cost of allocating a new slice in this case. |
| 132 | + // It should be pretty uncommon that metric contains both histograms and floats, so we will accept the cost of a new |
| 133 | + // slice. |
| 134 | + haveMixedFloatsAndHistograms := len(series.Histograms) > 0 && len(series.Floats) > 0 |
| 135 | + pointCount := len(series.Histograms) + len(series.Floats) |
| 136 | + |
| 137 | + var fPoints []promql.FPoint |
| 138 | + var hPoints []promql.HPoint |
| 139 | + |
| 140 | + prepareFPointSlice := func() error { |
| 141 | + if haveMixedFloatsAndHistograms || cap(series.Floats) < pointCount { |
| 142 | + // We have to get a new slice. |
| 143 | + var err error |
| 144 | + fPoints, err = types.FPointSlicePool.Get(pointCount, v.MemoryConsumptionTracker) |
| 145 | + return err |
| 146 | + } |
| 147 | + |
| 148 | + // We can reuse the existing slice. |
| 149 | + returnInputFPointSlice = false |
| 150 | + fPoints = series.Floats[:0] |
| 151 | + return nil |
| 152 | + } |
| 153 | + |
| 154 | + prepareHPointSlice := func() error { |
| 155 | + if haveMixedFloatsAndHistograms || cap(series.Histograms) < pointCount { |
| 156 | + // We have to get a new slice. |
| 157 | + var err error |
| 158 | + hPoints, err = types.HPointSlicePool.Get(pointCount, v.MemoryConsumptionTracker) |
| 159 | + return err |
| 160 | + } |
| 161 | + |
| 162 | + // We can reuse the existing slice. |
| 163 | + returnInputHPointSlice = false |
| 164 | + hPoints = series.Histograms[:0] |
| 165 | + return nil |
| 166 | + } |
| 167 | + |
| 168 | + v.vectorIterator.Reset(series) |
| 169 | + |
| 170 | + for { |
| 171 | + t, vectorF, vectorH, ok := v.vectorIterator.Next() |
| 172 | + |
| 173 | + if !ok { |
| 174 | + // We are done. |
| 175 | + break |
| 176 | + } |
| 177 | + |
| 178 | + scalarIdx := (t - v.start) / v.interval // Scalars always have a value at every step, so we can just compute the index of the corresponding scalar value from the timestamp. |
| 179 | + scalarValue := v.scalarData.Samples[scalarIdx].F |
| 180 | + |
| 181 | + f, h, ok, err := v.opFunc(scalarValue, vectorF, vectorH) |
| 182 | + if err != nil { |
| 183 | + err = functions.NativeHistogramErrorToAnnotation(err, v.emitAnnotation) |
| 184 | + if err == nil { |
| 185 | + // Error was converted to an annotation, continue without emitting a sample here. |
| 186 | + ok = false |
| 187 | + } else { |
| 188 | + return types.InstantVectorSeriesData{}, err |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + if ok { |
| 193 | + if h != nil { |
| 194 | + if hPoints == nil { |
| 195 | + // First histogram for this series, get a slice for it. |
| 196 | + if err := prepareHPointSlice(); err != nil { |
| 197 | + return types.InstantVectorSeriesData{}, err |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + hPoints = append(hPoints, promql.HPoint{T: t, H: h}) |
| 202 | + } else { |
| 203 | + // We have a float value. |
| 204 | + if fPoints == nil { |
| 205 | + // First float for this series, get a slice for it. |
| 206 | + if err := prepareFPointSlice(); err != nil { |
| 207 | + return types.InstantVectorSeriesData{}, err |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + fPoints = append(fPoints, promql.FPoint{T: t, F: f}) |
| 212 | + } |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + if returnInputFPointSlice { |
| 217 | + types.FPointSlicePool.Put(series.Floats, v.MemoryConsumptionTracker) |
| 218 | + } |
| 219 | + |
| 220 | + if returnInputHPointSlice { |
| 221 | + types.HPointSlicePool.Put(series.Histograms, v.MemoryConsumptionTracker) |
| 222 | + } |
| 223 | + |
| 224 | + return types.InstantVectorSeriesData{ |
| 225 | + Floats: fPoints, |
| 226 | + Histograms: hPoints, |
| 227 | + }, nil |
| 228 | +} |
| 229 | + |
| 230 | +func (v *VectorScalarBinaryOperation) ExpressionPosition() posrange.PositionRange { |
| 231 | + return v.expressionPosition |
| 232 | +} |
| 233 | + |
| 234 | +func (v *VectorScalarBinaryOperation) Close() { |
| 235 | + v.Scalar.Close() |
| 236 | + v.Vector.Close() |
| 237 | + types.FPointSlicePool.Put(v.scalarData.Samples, v.MemoryConsumptionTracker) |
| 238 | +} |
| 239 | + |
| 240 | +var _ types.InstantVectorOperator = &VectorScalarBinaryOperation{} |
0 commit comments