forked from VictoriaMetrics/metricsql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimizer.go
414 lines (391 loc) · 10.9 KB
/
optimizer.go
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
package metricsql
import (
"fmt"
"sort"
"strings"
)
// Optimize optimizes e in order to improve its performance.
//
// It performs the following optimizations:
//
// - Adds missing filters to `foo{filters1} op bar{filters2}`
// according to https://utcc.utoronto.ca/~cks/space/blog/sysadmin/PrometheusLabelNonOptimization
// I.e. such query is converted to `foo{filters1, filters2} op bar{filters1, filters2}`
func Optimize(e Expr) Expr {
if !canOptimize(e) {
return e
}
eCopy := Clone(e)
optimizeInplace(eCopy)
return eCopy
}
func canOptimize(e Expr) bool {
switch t := e.(type) {
case *RollupExpr:
return canOptimize(t.Expr) || canOptimize(t.At)
case *FuncExpr:
for _, arg := range t.Args {
if canOptimize(arg) {
return true
}
}
case *AggrFuncExpr:
for _, arg := range t.Args {
if canOptimize(arg) {
return true
}
}
case *BinaryOpExpr:
return true
}
return false
}
// Clone clones the given expression e and returns the cloned copy.
func Clone(e Expr) Expr {
s := e.AppendString(nil)
eCopy, err := Parse(string(s))
if err != nil {
panic(fmt.Errorf("BUG: cannot parse the expression %q: %w", s, err))
}
return eCopy
}
func optimizeInplace(e Expr) {
switch t := e.(type) {
case *RollupExpr:
optimizeInplace(t.Expr)
optimizeInplace(t.At)
case *FuncExpr:
for _, arg := range t.Args {
optimizeInplace(arg)
}
case *AggrFuncExpr:
for _, arg := range t.Args {
optimizeInplace(arg)
}
case *BinaryOpExpr:
optimizeInplace(t.Left)
optimizeInplace(t.Right)
lfs := getCommonLabelFilters(t)
pushdownBinaryOpFiltersInplace(t, lfs)
}
}
func getCommonLabelFilters(e Expr) []LabelFilter {
switch t := e.(type) {
case *MetricExpr:
return getLabelFiltersWithoutMetricName(t.LabelFilters)
case *RollupExpr:
return getCommonLabelFilters(t.Expr)
case *FuncExpr:
arg := getFuncArgForOptimization(t.Name, t.Args)
if arg == nil {
return nil
}
return getCommonLabelFilters(arg)
case *AggrFuncExpr:
arg := getFuncArgForOptimization(t.Name, t.Args)
if arg == nil {
return nil
}
lfs := getCommonLabelFilters(arg)
return trimFiltersByAggrModifier(lfs, t)
case *BinaryOpExpr:
lfsLeft := getCommonLabelFilters(t.Left)
lfsRight := getCommonLabelFilters(t.Right)
var lfs []LabelFilter
switch strings.ToLower(t.Op) {
case "or":
// {fCommon, f1} or {fCommon, f2} -> {fCommon}
// {fCommon, f1} or on() {fCommon, f2} -> {}
// {fCommon, f1} or on(fCommon) {fCommon, f2} -> {fCommon}
// {fCommon, f1} or on(f1) {fCommon, f2} -> {}
// {fCommon, f1} or on(f2) {fCommon, f2} -> {}
// {fCommon, f1} or on(f3) {fCommon, f2} -> {}
lfs = intersectLabelFilters(lfsLeft, lfsRight)
return TrimFiltersByGroupModifier(lfs, t)
case "unless":
// {f1} unless {f2} -> {f1}
// {f1} unless on() {f2} -> {}
// {f1} unless on(f1) {f2} -> {f1}
// {f1} unless on(f2) {f2} -> {}
// {f1} unless on(f1, f2) {f2} -> {f1}
// {f1} unless on(f3) {f2} -> {}
return TrimFiltersByGroupModifier(lfsLeft, t)
default:
switch strings.ToLower(t.JoinModifier.Op) {
case "group_left":
// {f1} * group_left() {f2} -> {f1, f2}
// {f1} * on() group_left() {f2} -> {f1}
// {f1} * on(f1) group_left() {f2} -> {f1}
// {f1} * on(f2) group_left() {f2} -> {f1, f2}
// {f1} * on(f1, f2) group_left() {f2} -> {f1, f2}
// {f1} * on(f3) group_left() {f2} -> {f1}
lfsRight = TrimFiltersByGroupModifier(lfsRight, t)
return unionLabelFilters(lfsLeft, lfsRight)
case "group_right":
// {f1} * group_right() {f2} -> {f1, f2}
// {f1} * on() group_right() {f2} -> {f2}
// {f1} * on(f1) group_right() {f2} -> {f1, f2}
// {f1} * on(f2) group_right() {f2} -> {f2}
// {f1} * on(f1, f2) group_right() {f2} -> {f1, f2}
// {f1} * on(f3) group_right() {f2} -> {f2}
lfsLeft = TrimFiltersByGroupModifier(lfsLeft, t)
return unionLabelFilters(lfsLeft, lfsRight)
default:
// {f1} * {f2} -> {f1, f2}
// {f1} * on() {f2} -> {}
// {f1} * on(f1) {f2} -> {f1}
// {f1} * on(f2) {f2} -> {f2}
// {f1} * on(f1, f2) {f2} -> {f2}
// {f1} * on(f3} {f2} -> {}
lfs = unionLabelFilters(lfsLeft, lfsRight)
return TrimFiltersByGroupModifier(lfs, t)
}
}
default:
return nil
}
}
func trimFiltersByAggrModifier(lfs []LabelFilter, afe *AggrFuncExpr) []LabelFilter {
switch strings.ToLower(afe.Modifier.Op) {
case "by":
return filterLabelFiltersOn(lfs, afe.Modifier.Args)
case "without":
return filterLabelFiltersIgnoring(lfs, afe.Modifier.Args)
default:
return nil
}
}
// TrimFiltersByGroupModifier trims lfs by the specified be.GroupModifier.Op (e.g. on() or ignoring()).
//
// The following cases are possible:
// - It returns lfs as is if be doesn't contain any group modifier
// - It returns only filters specified in on()
// - It drops filters specified inside ignoring()
func TrimFiltersByGroupModifier(lfs []LabelFilter, be *BinaryOpExpr) []LabelFilter {
switch strings.ToLower(be.GroupModifier.Op) {
case "on":
return filterLabelFiltersOn(lfs, be.GroupModifier.Args)
case "ignoring":
return filterLabelFiltersIgnoring(lfs, be.GroupModifier.Args)
default:
return lfs
}
}
func getLabelFiltersWithoutMetricName(lfs []LabelFilter) []LabelFilter {
lfsNew := make([]LabelFilter, 0, len(lfs))
for _, lf := range lfs {
if lf.Label != "__name__" {
lfsNew = append(lfsNew, lf)
}
}
return lfsNew
}
// PushdownBinaryOpFilters pushes down the given commonFilters to e if possible.
//
// e must be a part of binary operation - either left or right.
//
// For example, if e contains `foo + sum(bar)` and commonFilters={x="y"},
// then the returned expression will contain `foo{x="y"} + sum(bar)`.
// The `{x="y"}` cannot be pusehd down to `sum(bar)`, since this may change binary operation results.
func PushdownBinaryOpFilters(e Expr, commonFilters []LabelFilter) Expr {
if len(commonFilters) == 0 {
// Fast path - nothing to push down.
return e
}
eCopy := Clone(e)
pushdownBinaryOpFiltersInplace(eCopy, commonFilters)
return eCopy
}
func pushdownBinaryOpFiltersInplace(e Expr, lfs []LabelFilter) {
if len(lfs) == 0 {
return
}
switch t := e.(type) {
case *MetricExpr:
t.LabelFilters = unionLabelFilters(t.LabelFilters, lfs)
sortLabelFilters(t.LabelFilters)
case *RollupExpr:
pushdownBinaryOpFiltersInplace(t.Expr, lfs)
case *FuncExpr:
arg := getFuncArgForOptimization(t.Name, t.Args)
if arg != nil {
pushdownBinaryOpFiltersInplace(arg, lfs)
}
case *AggrFuncExpr:
lfs = trimFiltersByAggrModifier(lfs, t)
arg := getFuncArgForOptimization(t.Name, t.Args)
if arg != nil {
pushdownBinaryOpFiltersInplace(arg, lfs)
}
case *BinaryOpExpr:
lfs = TrimFiltersByGroupModifier(lfs, t)
pushdownBinaryOpFiltersInplace(t.Left, lfs)
pushdownBinaryOpFiltersInplace(t.Right, lfs)
}
}
func intersectLabelFilters(lfsA, lfsB []LabelFilter) []LabelFilter {
if len(lfsA) == 0 || len(lfsB) == 0 {
return nil
}
m := getLabelFiltersMap(lfsA)
var b []byte
var lfs []LabelFilter
for _, lf := range lfsB {
b = lf.AppendString(b[:0])
if _, ok := m[string(b)]; ok {
lfs = append(lfs, lf)
}
}
return lfs
}
func unionLabelFilters(lfsA, lfsB []LabelFilter) []LabelFilter {
if len(lfsA) == 0 {
return lfsB
}
if len(lfsB) == 0 {
return lfsA
}
m := getLabelFiltersMap(lfsA)
var b []byte
lfs := append([]LabelFilter{}, lfsA...)
for _, lf := range lfsB {
b = lf.AppendString(b[:0])
if _, ok := m[string(b)]; !ok {
lfs = append(lfs, lf)
}
}
return lfs
}
func getLabelFiltersMap(lfs []LabelFilter) map[string]struct{} {
m := make(map[string]struct{}, len(lfs))
var b []byte
for _, lf := range lfs {
b = lf.AppendString(b[:0])
m[string(b)] = struct{}{}
}
return m
}
func sortLabelFilters(lfs []LabelFilter) {
// Make sure the first label filter is __name__ (if any)
if len(lfs) > 0 && lfs[0].isMetricNameFilter() {
lfs = lfs[1:]
}
sort.Slice(lfs, func(i, j int) bool {
a, b := lfs[i], lfs[j]
if a.Label != b.Label {
return a.Label < b.Label
}
return a.Value < b.Value
})
}
func filterLabelFiltersOn(lfs []LabelFilter, args []string) []LabelFilter {
if len(args) == 0 {
return nil
}
m := make(map[string]struct{}, len(args))
for _, arg := range args {
m[arg] = struct{}{}
}
var lfsNew []LabelFilter
for _, lf := range lfs {
if _, ok := m[lf.Label]; ok {
lfsNew = append(lfsNew, lf)
}
}
return lfsNew
}
func filterLabelFiltersIgnoring(lfs []LabelFilter, args []string) []LabelFilter {
if len(args) == 0 {
return lfs
}
m := make(map[string]struct{}, len(args))
for _, arg := range args {
m[arg] = struct{}{}
}
var lfsNew []LabelFilter
for _, lf := range lfs {
if _, ok := m[lf.Label]; !ok {
lfsNew = append(lfsNew, lf)
}
}
return lfsNew
}
func getFuncArgForOptimization(funcName string, args []Expr) Expr {
idx := getFuncArgIdxForOptimization(funcName, args)
if idx < 0 || idx >= len(args) {
return nil
}
return args[idx]
}
func getFuncArgIdxForOptimization(funcName string, args []Expr) int {
funcName = strings.ToLower(funcName)
if IsRollupFunc(funcName) {
return getRollupArgIdxForOptimization(funcName, args)
}
if IsTransformFunc(funcName) {
return getTransformArgIdxForOptimization(funcName, args)
}
if isAggrFunc(funcName) {
return getAggrArgIdxForOptimization(funcName, args)
}
return -1
}
func getAggrArgIdxForOptimization(funcName string, args []Expr) int {
switch strings.ToLower(funcName) {
case "bottomk", "bottomk_avg", "bottomk_max", "bottomk_median", "bottomk_last", "bottomk_min",
"limitk", "outliers_mad", "outliersk", "quantile",
"topk", "topk_avg", "topk_max", "topk_median", "topk_last", "topk_min":
return 1
case "count_values":
return -1
case "quantiles":
return len(args) - 1
default:
return 0
}
}
func getRollupArgIdxForOptimization(funcName string, args []Expr) int {
// This must be kept in sync with GetRollupArgIdx()
switch strings.ToLower(funcName) {
case "absent_over_time":
return -1
case "quantile_over_time", "aggr_over_time",
"hoeffding_bound_lower", "hoeffding_bound_upper":
return 1
case "quantiles_over_time":
return len(args) - 1
default:
return 0
}
}
func getTransformArgIdxForOptimization(funcName string, args []Expr) int {
funcName = strings.ToLower(funcName)
if isLabelManipulationFunc(funcName) {
return -1
}
switch funcName {
case "", "absent", "scalar", "union", "vector", "range_normalize":
return -1
case "end", "now", "pi", "ru", "start", "step", "time":
return -1
case "limit_offset":
return 2
case "buckets_limit", "histogram_quantile", "histogram_share", "range_quantile",
"range_trim_outliers", "range_trim_spikes", "range_trim_zscore":
return 1
case "histogram_quantiles":
return len(args) - 1
default:
return 0
}
}
func isLabelManipulationFunc(funcName string) bool {
switch strings.ToLower(funcName) {
case "alias", "drop_common_labels", "label_copy", "label_del", "label_graphite_group", "label_join", "label_keep", "label_lowercase",
"label_map", "label_match", "label_mismatch", "label_move", "label_replace", "label_set", "label_transform",
"label_uppercase", "label_value":
return true
default:
return false
}
}