-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathutils.go
More file actions
214 lines (205 loc) · 5.13 KB
/
utils.go
File metadata and controls
214 lines (205 loc) · 5.13 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
package metricsql
import (
"fmt"
"strings"
)
// ExpandWithExprs expands WITH expressions inside q and returns the resulting
// PromQL without WITH expressions.
func ExpandWithExprs(q string) (string, error) {
e, err := Parse(q)
if err != nil {
return "", err
}
buf := e.AppendString(nil)
return string(buf), nil
}
// VisitAll recursively calls f for all the Expr children in e.
//
// It visits leaf children at first and then visits parent nodes.
// It is safe modifying expr in f.
func VisitAll(e Expr, f func(expr Expr)) {
switch expr := e.(type) {
case *BinaryOpExpr:
VisitAll(expr.Left, f)
VisitAll(expr.Right, f)
VisitAll(&expr.GroupModifier, f)
VisitAll(&expr.JoinModifier, f)
case *FuncExpr:
for _, arg := range expr.Args {
VisitAll(arg, f)
}
case *AggrFuncExpr:
for _, arg := range expr.Args {
VisitAll(arg, f)
}
VisitAll(&expr.Modifier, f)
case *RollupExpr:
VisitAll(expr.Expr, f)
if expr.Window != nil {
VisitAll(expr.Window, f)
}
if expr.Step != nil {
VisitAll(expr.Step, f)
}
if expr.Offset != nil {
VisitAll(expr.Offset, f)
}
if expr.At != nil {
VisitAll(expr.At, f)
}
}
f(e)
}
// VisitAll recursively calls f for all the Expr children in e, unless `false` is return
// (in that case, it stops visiting). It returns `true` only if the caller never returned
// `false` in the callback
//
// It visits leaf children at first and then visits parent nodes.
// It is safe modifying expr in f.
func VisitAllBreakable(e Expr, f func(expr Expr) bool) bool {
switch expr := e.(type) {
case *BinaryOpExpr:
if VisitAllBreakable(expr.Left, f) == false {
return false
}
if VisitAllBreakable(expr.Right, f) == false {
return false
}
if VisitAllBreakable(&expr.GroupModifier, f) == false {
return false
}
if VisitAllBreakable(&expr.JoinModifier, f) == false {
return false
}
case *FuncExpr:
for _, arg := range expr.Args {
if VisitAllBreakable(arg, f) == false {
return false
}
}
case *AggrFuncExpr:
for _, arg := range expr.Args {
if VisitAllBreakable(arg, f) == false {
return false
}
}
if VisitAllBreakable(&expr.Modifier, f) == false {
return false
}
case *RollupExpr:
if VisitAllBreakable(expr.Expr, f) == false {
return false
}
if expr.Window != nil {
if VisitAllBreakable(expr.Window, f) == false {
return false
}
}
if expr.Step != nil {
if VisitAllBreakable(expr.Step, f) == false {
return false
}
}
if expr.Offset != nil {
if VisitAllBreakable(expr.Offset, f) == false {
return false
}
}
if expr.At != nil {
if VisitAllBreakable(expr.At, f) == false {
return false
}
}
}
return f(e)
}
// IsLikelyInvalid returns true if e contains tricky implicit conversion, which is invalid most of the time.
//
// Examples of invalid expressions:
//
// rate(sum(foo))
// rate(abs(foo))
// rate(foo + bar)
// rate(foo > 10)
//
// These expressions are implicitly converted into another expressions, which returns unexpected results most of the time:
//
// rate(sum(default_rollup(foo[1i:1i])))
// rate(abs(default_rollup(foo[1i:1i])))
// rate(default_rollup(foo[1i:1i]) + default_rollup(bar[1i:1i]))
// rate(default_rollup(foo[1i:1i]) > 10)
//
// See https://docs.victoriametrics.com/victoriametrics/metricsql/#implicit-query-conversions
//
// Note that rate(foo) is valid expression, since it returns the expected results most of the time, e.g. rate(foo[1i]).
func IsLikelyInvalid(e Expr) bool {
hasImplicitConversion := false
VisitAll(e, func(expr Expr) {
if hasImplicitConversion {
return
}
fe, ok := expr.(*FuncExpr)
if !ok {
return
}
if fe.Name == `timestamp` {
// In Prometheus, timestamp is defined as a transform function on instant vectors,
// but its behavior is closer to a rollup since it returns raw sample timestamps.
// VictoriaMetrics explicitly defines timestamp as a rollup function.
// To remain consistent with Prometheus, IsLikelyInvalid does not treat timestamp
// as an implicit conversion even when applied to non-metric expressions, like timestamp(sum(foo)).
//
// See more in https://github.com/VictoriaMetrics/VictoriaMetrics/issues/9527#issuecomment-3191439447
return
}
idx := GetRollupArgIdx(fe)
if idx < 0 || idx >= len(fe.Args) {
return
}
arg := fe.Args[idx]
re, ok := arg.(*RollupExpr)
if !ok {
if _, ok = arg.(*MetricExpr); !ok {
hasImplicitConversion = true
}
return
}
if _, ok := re.Expr.(*MetricExpr); ok {
return
}
if re.Window == nil {
hasImplicitConversion = true
}
})
return hasImplicitConversion
}
// IsSupportedFunction returns true if funcName contains supported MetricsQL function
func IsSupportedFunction(funcName string) bool {
funcName = strings.ToLower(funcName)
if IsRollupFunc(funcName) {
return true
}
if IsTransformFunc(funcName) {
return true
}
if IsAggrFunc(funcName) {
return true
}
return false
}
func checkSupportedFunctions(e Expr) error {
var err error
VisitAll(e, func(expr Expr) {
if err != nil {
return
}
fe, ok := expr.(*FuncExpr)
if !ok {
return
}
if !IsSupportedFunction(fe.Name) {
err = fmt.Errorf("unsupported function %q", fe.Name)
}
})
return err
}