-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathexecution.go
More file actions
419 lines (380 loc) · 16.2 KB
/
Copy pathexecution.go
File metadata and controls
419 lines (380 loc) · 16.2 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// Copyright (c) The Thanos Community Authors.
// Licensed under the Apache License 2.0.
// Copyright 2013 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package execution
import (
"context"
"sort"
"time"
"github.com/thanos-io/promql-engine/execution/aggregate"
"github.com/thanos-io/promql-engine/execution/binary"
"github.com/thanos-io/promql-engine/execution/exchange"
"github.com/thanos-io/promql-engine/execution/function"
"github.com/thanos-io/promql-engine/execution/model"
"github.com/thanos-io/promql-engine/execution/noop"
"github.com/thanos-io/promql-engine/execution/parse"
"github.com/thanos-io/promql-engine/execution/remote"
"github.com/thanos-io/promql-engine/execution/scan"
"github.com/thanos-io/promql-engine/execution/step_invariant"
"github.com/thanos-io/promql-engine/execution/unary"
"github.com/thanos-io/promql-engine/logicalplan"
"github.com/thanos-io/promql-engine/query"
"github.com/thanos-io/promql-engine/storage"
"github.com/efficientgo/core/errors"
"github.com/prometheus/prometheus/promql"
"github.com/prometheus/prometheus/promql/parser"
promstorage "github.com/prometheus/prometheus/storage"
)
// New creates new physical query execution for a given query expression which represents logical plan.
// TODO(bwplotka): Add definition (could be parameters for each execution operator) we can optimize - it would represent physical plan.
func New(ctx context.Context, expr logicalplan.Node, storage storage.Scanners, opts *query.Options) (model.VectorOperator, error) {
hints := promstorage.SelectHints{
Start: opts.Start.UnixMilli(),
End: opts.End.UnixMilli(),
Step: opts.Step.Milliseconds(),
}
return newOperator(ctx, expr, storage, opts, hints)
}
func newOperator(ctx context.Context, expr logicalplan.Node, storage storage.Scanners, opts *query.Options, hints promstorage.SelectHints) (model.VectorOperator, error) {
switch e := expr.(type) {
case *logicalplan.NumberLiteral:
return scan.NewNumberLiteralSelector(opts, e.Val), nil
case *logicalplan.VectorSelector:
return newVectorSelector(ctx, e, storage, opts, hints)
case *logicalplan.FunctionCall:
return newCall(ctx, e, storage, opts, hints)
case *logicalplan.Aggregation:
return newAggregateExpression(ctx, e, storage, opts, hints)
case *logicalplan.Binary:
return newBinaryExpression(ctx, e, storage, opts, hints)
case *logicalplan.Parens:
return newOperator(ctx, e.Expr, storage, opts, hints)
case *logicalplan.Unary:
return newUnaryExpression(ctx, e, storage, opts, hints)
case *logicalplan.StepInvariantExpr:
return newStepInvariantExpression(ctx, e, storage, opts, hints)
case logicalplan.Deduplicate:
return newDeduplication(ctx, e, storage, opts, hints)
case logicalplan.RemoteExecution:
return newRemoteExecution(ctx, e, opts, hints)
case *logicalplan.CheckDuplicateLabels:
return newDuplicateLabelCheck(ctx, e, storage, opts, hints)
case logicalplan.Noop:
return noop.NewOperator(opts), nil
case logicalplan.UserDefinedExpr:
return e.MakeExecutionOperator(ctx, opts, hints)
default:
return nil, errors.Wrapf(parse.ErrNotSupportedExpr, "got: %s (%T)", e, e)
}
}
func newVectorSelector(ctx context.Context, e *logicalplan.VectorSelector, scanners storage.Scanners, opts *query.Options, hints promstorage.SelectHints) (model.VectorOperator, error) {
start, end := getTimeRangesForVectorSelector(e, opts, 0)
hints.Start = start
hints.End = end
return scanners.NewVectorSelector(ctx, opts, hints, *e)
}
func newCall(ctx context.Context, e *logicalplan.FunctionCall, scanners storage.Scanners, opts *query.Options, hints promstorage.SelectHints) (model.VectorOperator, error) {
hints.Func = e.Func.Name
hints.Grouping = nil
hints.By = false
if e.Func.Name == "absent_over_time" {
return newAbsentOverTimeOperator(ctx, e, scanners, opts, hints)
}
if e.Func.Name == "timestamp" {
switch arg := e.Args[0].(type) {
case *logicalplan.VectorSelector:
arg.SelectTimestamp = true
return newVectorSelector(ctx, arg, scanners, opts, hints)
case *logicalplan.StepInvariantExpr:
// Step invariant expressions on vector selectors need to be unwrapped so that we
// can return the original timestamp rather than the step invariant one.
switch vs := arg.Expr.(type) {
case *logicalplan.VectorSelector:
// Prometheus weirdness.
if vs.Timestamp != nil {
vs.OriginalOffset = 0
}
vs.SelectTimestamp = true
return newVectorSelector(ctx, vs, scanners, opts, hints)
}
return newInstantVectorFunction(ctx, e, scanners, opts, hints)
}
return newInstantVectorFunction(ctx, e, scanners, opts, hints)
}
// TODO(saswatamcode): Range vector result might need new operator
// before it can be non-nested. https://github.com/thanos-io/promql-engine/issues/39
for i := range e.Args {
switch t := e.Args[i].(type) {
case *logicalplan.Subquery:
return newSubqueryFunction(ctx, e, t, scanners, opts, hints)
case *logicalplan.MatrixSelector:
return newRangeVectorFunction(ctx, e, t, scanners, opts, hints)
}
}
return newInstantVectorFunction(ctx, e, scanners, opts, hints)
}
func newAbsentOverTimeOperator(ctx context.Context, call *logicalplan.FunctionCall, scanners storage.Scanners, opts *query.Options, hints promstorage.SelectHints) (model.VectorOperator, error) {
switch arg := call.Args[0].(type) {
case *logicalplan.Subquery:
matrixCall := &logicalplan.FunctionCall{
Func: parser.Function{Name: "last_over_time"},
}
argOp, err := newSubqueryFunction(ctx, matrixCall, arg, scanners, opts, hints)
if err != nil {
return nil, err
}
f := &logicalplan.FunctionCall{
Func: parser.Function{Name: "absent"},
Args: []logicalplan.Node{matrixCall},
}
return function.NewFunctionOperator(f, []model.VectorOperator{argOp}, opts.StepsBatch, opts)
case *logicalplan.MatrixSelector:
matrixCall := &logicalplan.FunctionCall{
Func: parser.Function{Name: "last_over_time"},
Args: call.Args,
}
argOp, err := newRangeVectorFunction(ctx, matrixCall, arg, scanners, opts, hints)
if err != nil {
return nil, err
}
f := &logicalplan.FunctionCall{
Func: parser.Function{Name: "absent"},
Args: []logicalplan.Node{&logicalplan.MatrixSelector{
VectorSelector: arg.VectorSelector,
Range: arg.Range,
OriginalString: arg.String(),
}},
}
return function.NewFunctionOperator(f, []model.VectorOperator{argOp}, opts.StepsBatch, opts)
default:
return nil, parse.ErrNotSupportedExpr
}
}
func newRangeVectorFunction(ctx context.Context, e *logicalplan.FunctionCall, t *logicalplan.MatrixSelector, scanners storage.Scanners, opts *query.Options, hints promstorage.SelectHints) (model.VectorOperator, error) {
// TODO(saswatamcode): Range vector result might need new operator
// before it can be non-nested. https://github.com/thanos-io/promql-engine/issues/39
milliSecondRange := t.Range.Milliseconds()
if parse.IsExtFunction(e.Func.Name) {
milliSecondRange += opts.ExtLookbackDelta.Milliseconds()
}
start, end := getTimeRangesForVectorSelector(t.VectorSelector, opts, milliSecondRange)
hints.Start = start
hints.End = end
hints.Range = milliSecondRange
return scanners.NewMatrixSelector(ctx, opts, hints, *t, *e)
}
func newSubqueryFunction(ctx context.Context, e *logicalplan.FunctionCall, t *logicalplan.Subquery, storage storage.Scanners, opts *query.Options, hints promstorage.SelectHints) (model.VectorOperator, error) {
// TODO: We dont implement ext functions
if parse.IsExtFunction(e.Func.Name) {
return nil, parse.ErrNotImplemented
}
nOpts := query.NestedOptionsForSubquery(opts, t.Step, t.Range, t.Offset)
hints.Start = nOpts.Start.UnixMilli()
hints.End = nOpts.End.UnixMilli()
hints.Step = nOpts.Step.Milliseconds()
inner, err := newOperator(ctx, t.Expr, storage, nOpts, hints)
if err != nil {
return nil, err
}
outerOpts := *opts
if t.Timestamp != nil {
outerOpts.Start = time.UnixMilli(*t.Timestamp)
outerOpts.End = time.UnixMilli(*t.Timestamp)
}
var scalarArg model.VectorOperator
var scalarArg2 model.VectorOperator
switch e.Func.Name {
case "quantile_over_time":
// quantile_over_time(scalar, range-vector)
scalarArg, err = newOperator(ctx, e.Args[0], storage, opts, hints)
if err != nil {
return nil, err
}
case "predict_linear":
// predict_linear(range-vector, scalar)
scalarArg, err = newOperator(ctx, e.Args[1], storage, opts, hints)
if err != nil {
return nil, err
}
case "double_exponential_smoothing":
// double_exponential_smoothing(range-vector, scalar, scalar)
scalarArg, err = newOperator(ctx, e.Args[1], storage, opts, hints)
if err != nil {
return nil, err
}
scalarArg2, err = newOperator(ctx, e.Args[2], storage, opts, hints)
if err != nil {
return nil, err
}
}
return scan.NewSubqueryOperator(inner, scalarArg, scalarArg2, &outerOpts, e, t)
}
func newInstantVectorFunction(ctx context.Context, e *logicalplan.FunctionCall, storage storage.Scanners, opts *query.Options, hints promstorage.SelectHints) (model.VectorOperator, error) {
nextOperators := make([]model.VectorOperator, 0, len(e.Args))
for i := range e.Args {
// Strings don't need an operator
if e.Args[i].ReturnType() == parser.ValueTypeString {
continue
}
next, err := newOperator(ctx, e.Args[i], storage, opts, hints)
if err != nil {
return nil, err
}
nextOperators = append(nextOperators, next)
}
return function.NewFunctionOperator(e, nextOperators, opts.StepsBatch, opts)
}
func newAggregateExpression(ctx context.Context, e *logicalplan.Aggregation, scanners storage.Scanners, opts *query.Options, hints promstorage.SelectHints) (model.VectorOperator, error) {
hints.Func = e.Op.String()
hints.Grouping = e.Grouping
hints.By = !e.Without
next, err := newOperator(ctx, e.Expr, scanners, opts, hints)
if err != nil {
return nil, err
}
if e.Op == parser.COUNT_VALUES {
param := logicalplan.UnsafeUnwrapString(e.Param)
return aggregate.NewCountValues(next, param, !e.Without, e.Grouping, opts), nil
}
// parameter is only required for count_values, quantile, topk, bottomk, limitk, and limit_ratio.
var paramOp model.VectorOperator
switch e.Op {
case parser.QUANTILE, parser.TOPK, parser.BOTTOMK, parser.LIMITK, parser.LIMIT_RATIO:
paramOp, err = newOperator(ctx, e.Param, scanners, opts, hints)
if err != nil {
return nil, err
}
}
if e.Op == parser.TOPK || e.Op == parser.BOTTOMK || e.Op == parser.LIMITK || e.Op == parser.LIMIT_RATIO {
next, err = aggregate.NewKHashAggregate(next, paramOp, e.Op, !e.Without, e.Grouping, opts)
} else {
next, err = aggregate.NewHashAggregate(next, paramOp, e.Op, !e.Without, e.Grouping, opts)
}
if err != nil {
return nil, err
}
return exchange.NewConcurrent(next, 2, opts), nil
}
func newBinaryExpression(ctx context.Context, e *logicalplan.Binary, scanners storage.Scanners, opts *query.Options, hints promstorage.SelectHints) (model.VectorOperator, error) {
if e.LHS.ReturnType() == parser.ValueTypeScalar || e.RHS.ReturnType() == parser.ValueTypeScalar {
return newScalarBinaryOperator(ctx, e, scanners, opts, hints)
}
return newVectorBinaryOperator(ctx, e, scanners, opts, hints)
}
func newVectorBinaryOperator(ctx context.Context, e *logicalplan.Binary, storage storage.Scanners, opts *query.Options, hints promstorage.SelectHints) (model.VectorOperator, error) {
leftOperator, err := newOperator(ctx, e.LHS, storage, opts, hints)
if err != nil {
return nil, err
}
rightOperator, err := newOperator(ctx, e.RHS, storage, opts, hints)
if err != nil {
return nil, err
}
return binary.NewVectorOperator(leftOperator, rightOperator, e.VectorMatching, e.Op, e.ReturnBool, opts)
}
func newScalarBinaryOperator(ctx context.Context, e *logicalplan.Binary, storage storage.Scanners, opts *query.Options, hints promstorage.SelectHints) (model.VectorOperator, error) {
lhs, err := newOperator(ctx, e.LHS, storage, opts, hints)
if err != nil {
return nil, err
}
rhs, err := newOperator(ctx, e.RHS, storage, opts, hints)
if err != nil {
return nil, err
}
return binary.NewScalar(lhs, rhs, e.LHS.ReturnType(), e.RHS.ReturnType(), e.Op, e.ReturnBool, opts)
}
func newUnaryExpression(ctx context.Context, e *logicalplan.Unary, scanners storage.Scanners, opts *query.Options, hints promstorage.SelectHints) (model.VectorOperator, error) {
next, err := newOperator(ctx, e.Expr, scanners, opts, hints)
if err != nil {
return nil, err
}
switch e.Op {
case parser.ADD:
return next, nil
case parser.SUB:
return unary.NewUnaryNegation(next, opts)
default:
// This shouldn't happen as Op was validated when parsing already
// https://github.com/prometheus/prometheus/blob/v2.38.0/promql/parser/parse.go#L573.
return nil, errors.Wrapf(parse.ErrNotSupportedExpr, "got: %s", e)
}
}
func newStepInvariantExpression(ctx context.Context, e *logicalplan.StepInvariantExpr, scanners storage.Scanners, opts *query.Options, hints promstorage.SelectHints) (model.VectorOperator, error) {
switch t := e.Expr.(type) {
case *logicalplan.NumberLiteral:
return scan.NewNumberLiteralSelector(opts, t.Val), nil
}
next, err := newOperator(ctx, e.Expr, scanners, opts.WithEndTime(opts.Start), hints)
if err != nil {
return nil, err
}
return step_invariant.NewStepInvariantOperator(next, e.Expr, opts)
}
func newDeduplication(ctx context.Context, e logicalplan.Deduplicate, scanners storage.Scanners, opts *query.Options, hints promstorage.SelectHints) (model.VectorOperator, error) {
// The Deduplicate operator will deduplicate samples using a last-sample-wins strategy.
// Sorting engines by MaxT ensures that samples produced due to
// staleness will be overwritten and corrected by samples coming from
// engines with a higher max time.
sort.Slice(e.Expressions, func(i, j int) bool {
return e.Expressions[i].Engine.MaxT() < e.Expressions[j].Engine.MaxT()
})
operators := make([]model.VectorOperator, len(e.Expressions))
for i, expr := range e.Expressions {
operator, err := newOperator(ctx, expr, scanners, opts, hints)
if err != nil {
return nil, err
}
operators[i] = operator
}
coalesce := exchange.NewCoalesce(opts, operators...)
dedup := exchange.NewDedupOperator(coalesce, opts)
return exchange.NewConcurrent(dedup, 2, opts), nil
}
func newRemoteExecution(ctx context.Context, e logicalplan.RemoteExecution, opts *query.Options, hints promstorage.SelectHints) (model.VectorOperator, error) {
// Create a new remote query scoped to the calculated start time.
qry, err := e.Engine.NewRangeQuery(ctx, promql.NewPrometheusQueryOpts(false, opts.LookbackDelta), e.Query, e.QueryRangeStart, e.QueryRangeEnd, opts.Step)
if err != nil {
return nil, err
}
// The selector uses the original query time to make sure that steps from different
// operators have the same timestamps.
// We need to set the lookback for the selector to 0 since the remote query already applies one lookback.
selectorOpts := *opts
selectorOpts.LookbackDelta = 0
remoteExec := remote.NewExecution(qry, e.QueryRangeStart, e.QueryRangeEnd, e.Engine.LabelSets(), &selectorOpts, hints)
return exchange.NewConcurrent(remoteExec, 2, opts), nil
}
func newDuplicateLabelCheck(ctx context.Context, e *logicalplan.CheckDuplicateLabels, storage storage.Scanners, opts *query.Options, hints promstorage.SelectHints) (model.VectorOperator, error) {
op, err := newOperator(ctx, e.Expr, storage, opts, hints)
if err != nil {
return nil, err
}
return exchange.NewDuplicateLabelCheck(op, opts), nil
}
// Copy from https://github.com/prometheus/prometheus/blob/v2.39.1/promql/engine.go#L791.
func getTimeRangesForVectorSelector(n *logicalplan.VectorSelector, opts *query.Options, evalRange int64) (int64, int64) {
start := opts.Start.UnixMilli()
end := opts.End.UnixMilli()
if n.Timestamp != nil {
start = *n.Timestamp
end = *n.Timestamp
}
if evalRange == 0 {
start -= opts.LookbackDelta.Milliseconds() - 1
} else {
start -= evalRange - 1
}
offset := n.OriginalOffset.Milliseconds()
return start - offset, end - offset
}