-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathscalar.go
More file actions
255 lines (223 loc) · 6.21 KB
/
scalar.go
File metadata and controls
255 lines (223 loc) · 6.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright (c) The Thanos Community Authors.
// Licensed under the Apache License 2.0.
package binary
import (
"context"
"fmt"
"sync"
"github.com/thanos-io/promql-engine/execution/model"
"github.com/thanos-io/promql-engine/execution/telemetry"
"github.com/thanos-io/promql-engine/extlabels"
"github.com/thanos-io/promql-engine/query"
"github.com/thanos-io/promql-engine/warnings"
"github.com/efficientgo/core/errors"
"github.com/prometheus/prometheus/model/histogram"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/promql/parser"
)
// scalarOperator evaluates expressions where one operand is a scalarOperator.
type scalarOperator struct {
lhs model.VectorOperator
rhs model.VectorOperator
lhsType parser.ValueType
rhsType parser.ValueType
opType parser.ItemType
returnBool bool
stepsBatch int
once sync.Once
series []labels.Labels
lhsBuf []model.StepVector
rhsBuf []model.StepVector
}
func NewScalar(
lhs model.VectorOperator,
rhs model.VectorOperator,
lhsType parser.ValueType,
rhsType parser.ValueType,
opType parser.ItemType,
returnBool bool,
opts *query.Options,
) (model.VectorOperator, error) {
op := &scalarOperator{
lhs: lhs,
rhs: rhs,
lhsType: lhsType,
rhsType: rhsType,
opType: opType,
returnBool: returnBool,
stepsBatch: opts.StepsBatch,
}
return telemetry.NewOperator(telemetry.NewTelemetry(op, opts.EnableAnalysis, opts.EnablePerStepStats, opts.Start.UnixMilli(), opts.End.UnixMilli(), opts.Step, opts.SampleLimiter), op), nil
}
func (o *scalarOperator) Explain() (next []model.VectorOperator) {
return []model.VectorOperator{o.lhs, o.rhs}
}
func (o *scalarOperator) Series(ctx context.Context) ([]labels.Labels, error) {
var err error
o.once.Do(func() { err = o.loadSeries(ctx) })
if err != nil {
return nil, err
}
return o.series, nil
}
func (o *scalarOperator) String() string {
return fmt.Sprintf("[vectorScalarBinary] %s", parser.ItemTypeStr[o.opType])
}
func (o *scalarOperator) Next(ctx context.Context, buf []model.StepVector) (int, error) {
select {
case <-ctx.Done():
return 0, ctx.Err()
default:
}
var err error
o.once.Do(func() { err = o.loadSeries(ctx) })
if err != nil {
return 0, err
}
var lhsN int
var lerrChan = make(chan error, 1)
go func() {
defer func() {
if r := recover(); r != nil {
lerrChan <- errors.Newf("unexpected panic: %v", r)
}
close(lerrChan)
}()
var err error
lhsN, err = o.lhs.Next(ctx, o.lhsBuf)
if err != nil {
lerrChan <- err
}
}()
rhsN, rerr := o.rhs.Next(ctx, o.rhsBuf)
lerr := <-lerrChan
if rerr != nil {
return 0, rerr
}
if lerr != nil {
return 0, lerr
}
// TODO(fpetkovski): When one operator becomes empty,
// we might want to drain or close the other one.
// We don't have a concept of closing an operator yet.
if lhsN == 0 || rhsN == 0 {
return 0, nil
}
n := 0
minN := min(rhsN, lhsN)
for i := 0; i < minN && n < len(buf); i++ {
o.execBinaryOperation(ctx, o.lhsBuf[i], o.rhsBuf[i], &buf[n])
n++
}
return n, nil
}
func (o *scalarOperator) loadSeries(ctx context.Context) error {
vectorSide := o.lhs
if o.lhsType == parser.ValueTypeScalar {
vectorSide = o.rhs
}
vectorSeries, err := vectorSide.Series(ctx)
if err != nil {
return err
}
series := make([]labels.Labels, len(vectorSeries))
var b labels.ScratchBuilder
for i := range vectorSeries {
if !vectorSeries[i].IsEmpty() {
lbls := vectorSeries[i]
if shouldDropMetricName(o.opType, o.returnBool) {
lbls = extlabels.DropReserved(lbls, b)
}
series[i] = lbls
} else {
series[i] = vectorSeries[i]
}
}
o.series = series
// Pre-allocate buffers with appropriate inner slice capacities.
// One side is a scalar (1 sample), the other is a vector (len(vectorSeries) samples).
o.lhsBuf = make([]model.StepVector, o.stepsBatch)
o.rhsBuf = make([]model.StepVector, o.stepsBatch)
var lhsSeriesCount, rhsSeriesCount int
if o.lhsType == parser.ValueTypeScalar {
lhsSeriesCount = 1
rhsSeriesCount = len(vectorSeries)
} else {
lhsSeriesCount = len(vectorSeries)
rhsSeriesCount = 1
}
// Pre-allocate float sample slices; histogram slices will grow on demand.
for i := range o.lhsBuf {
o.lhsBuf[i].SampleIDs = make([]uint64, 0, lhsSeriesCount)
o.lhsBuf[i].Samples = make([]float64, 0, lhsSeriesCount)
}
for i := range o.rhsBuf {
o.rhsBuf[i].SampleIDs = make([]uint64, 0, rhsSeriesCount)
o.rhsBuf[i].Samples = make([]float64, 0, rhsSeriesCount)
}
return nil
}
func (o *scalarOperator) execBinaryOperation(ctx context.Context, lhs, rhs model.StepVector, step *model.StepVector) {
ts := lhs.T
step.Reset(ts)
scalar, other := lhs, rhs
if o.lhsType != parser.ValueTypeScalar {
scalar, other = rhs, lhs
}
var (
v float64
h *histogram.FloatHistogram
keep bool
err error
)
var warn warnings.Warnings
sampleHint := len(other.Samples)
for i, otherVal := range other.Samples {
scalarVal := scalar.Samples[0]
if o.lhsType == parser.ValueTypeScalar {
v, _, keep, warn, err = binOp(o.opType, scalarVal, otherVal, nil, nil)
} else {
v, _, keep, warn, err = binOp(o.opType, otherVal, scalarVal, nil, nil)
}
if err != nil {
warnings.AddToContext(err, ctx)
continue
}
if warn != 0 {
emitBinaryOpWarnings(ctx, warn, o.opType)
}
// in comparison operations between scalars and vectors, the vectors are filtered, regardless if lhs or rhs
if keep && o.opType.IsComparisonOperator() && (o.lhsType == parser.ValueTypeVector || o.rhsType == parser.ValueTypeVector) {
v = otherVal
}
if o.returnBool {
v = 0.0
if keep {
v = 1.0
}
} else if !keep {
continue
}
step.AppendSampleWithSizeHint(other.SampleIDs[i], v, sampleHint)
}
histogramHint := len(other.Histograms)
for i, otherVal := range other.Histograms {
scalarVal := scalar.Samples[0]
if o.lhsType == parser.ValueTypeScalar {
_, h, keep, warn, err = binOp(o.opType, scalarVal, 0., nil, otherVal)
} else {
_, h, keep, warn, err = binOp(o.opType, 0., scalarVal, otherVal, nil)
}
if err != nil {
warnings.AddToContext(err, ctx)
continue
}
if warn != 0 {
emitBinaryOpWarnings(ctx, warn, o.opType)
}
if !keep {
continue
}
step.AppendHistogramWithSizeHint(other.HistogramIDs[i], h, histogramHint)
}
}