forked from grafana/tempo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast_validate.go
More file actions
310 lines (254 loc) · 7.03 KB
/
ast_validate.go
File metadata and controls
310 lines (254 loc) · 7.03 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
package traceql
import (
"fmt"
"regexp"
)
// unsupportedError is returned for traceql features that are not yet supported.
type unsupportedError struct {
feature string
}
func newUnsupportedError(feature string) *unsupportedError {
return &unsupportedError{feature: feature}
}
func (e *unsupportedError) Error() string {
return e.feature + " not yet supported"
}
func (r RootExpr) validate() error {
err := r.Pipeline.validate()
if err != nil {
return err
}
if r.MetricsPipeline != nil {
err := r.MetricsPipeline.validate()
if err != nil {
return err
}
}
if r.MetricsSecondStage != nil {
err := r.MetricsSecondStage.validate()
if err != nil {
return err
}
}
// extra validation to disallow compare() with second stage functions
// for example: `{} | compare({status=error}) | topk(10)` doesn't make sense
if r.MetricsPipeline != nil && r.MetricsSecondStage != nil {
// cast and check if the first stage is a compare operation
if _, ok := r.MetricsPipeline.(*MetricsCompare); ok {
return fmt.Errorf("`compare()` cannot be used with second stage functions")
}
}
return nil
}
func (p Pipeline) validate() error {
for _, p := range p.Elements {
err := p.validate()
if err != nil {
return err
}
}
return nil
}
func (o GroupOperation) validate() error {
// todo: once grouping is supported the below validation will apply
if !o.Expression.referencesSpan() {
return fmt.Errorf("grouping field expressions must reference the span: %s", o.String())
}
return o.Expression.validate()
}
func (o CoalesceOperation) validate() error {
return nil
}
func (o SelectOperation) validate() error {
for _, e := range o.attrs {
if err := e.validate(); err != nil {
return err
}
}
return nil
}
func (o ScalarOperation) validate() error {
if err := o.LHS.validate(); err != nil {
return err
}
if err := o.RHS.validate(); err != nil {
return err
}
if o.Op == OpNotExists || o.Op == OpExists {
return fmt.Errorf("illegal operation for the given type: %s", o.String())
}
lhsT := o.LHS.impliedType()
rhsT := o.RHS.impliedType()
if !lhsT.isMatchingOperand(rhsT) {
return fmt.Errorf("binary operations must operate on the same type: %s", o.String())
}
if !o.Op.binaryTypesValid(lhsT, rhsT) {
return fmt.Errorf("illegal operation for the given types: %s", o.String())
}
return nil
}
func (a Aggregate) validate() error {
if a.e == nil {
return nil
}
if err := a.e.validate(); err != nil {
return err
}
// aggregate field expressions require a type of a number or attribute
t := a.e.impliedType()
if t != TypeAttribute && !t.isNumeric() {
return fmt.Errorf("aggregate field expressions must resolve to a number type: %s", a.String())
}
if !a.e.referencesSpan() {
return fmt.Errorf("aggregate field expressions must reference the span: %s", a.String())
}
switch a.op {
case aggregateCount, aggregateAvg, aggregateMin, aggregateMax, aggregateSum:
default:
return newUnsupportedError(fmt.Sprintf("aggregate operation (%v)", a.op))
}
return nil
}
func (o SpansetOperation) validate() error {
if err := o.LHS.validate(); err != nil {
return err
}
if o.Op == OpNotExists || o.Op == OpExists {
return fmt.Errorf("illegal operation for the given type: %s", o.String())
}
return o.RHS.validate()
}
func (f SpansetFilter) validate() error {
if err := f.Expression.validate(); err != nil {
return err
}
t := f.Expression.impliedType()
if t != TypeAttribute && t != TypeBoolean {
return fmt.Errorf("span filter field expressions must resolve to a boolean: %s", f.String())
}
return nil
}
func (f ScalarFilter) validate() error {
if err := f.LHS.validate(); err != nil {
return err
}
if err := f.RHS.validate(); err != nil {
return err
}
lhsT := f.LHS.impliedType()
rhsT := f.RHS.impliedType()
if !lhsT.isMatchingOperand(rhsT) {
return fmt.Errorf("binary operations must operate on the same type: %s", f.String())
}
if !f.Op.binaryTypesValid(lhsT, rhsT) {
return fmt.Errorf("illegal operation for the given types: %s", f.String())
}
// Only supported expression types
switch f.LHS.(type) {
case Aggregate:
default:
return newUnsupportedError("scalar filter lhs of type (%v)")
}
switch f.RHS.(type) {
case Static:
default:
return newUnsupportedError("scalar filter rhs of type (%v)")
}
return nil
}
func (o *BinaryOperation) validate() error {
if err := o.LHS.validate(); err != nil {
return err
}
if err := o.RHS.validate(); err != nil {
return err
}
lhsT := o.LHS.impliedType()
rhsT := o.RHS.impliedType()
if o.Op == OpNotExists || o.Op == OpExists {
return fmt.Errorf("illegal operation for the given type: %s", o.String())
}
if !lhsT.isMatchingOperand(rhsT) {
return fmt.Errorf("binary operations must operate on the same type: %s", o.String())
}
if !o.Op.binaryTypesValid(lhsT, rhsT) {
return fmt.Errorf("illegal operation for the given types: %s", o.String())
}
// if this is a regex operator confirm the RHS is a valid regex
if o.Op == OpRegex || o.Op == OpNotRegex {
// Ensure that we validate against the raw/unquoted string when possible.
// When RHS is a hardcoded string in the query which is compiled to a Static.
var text string
if static, ok := o.RHS.(Static); ok {
text = static.EncodeToString(false)
} else {
text = o.RHS.String()
}
_, err := regexp.Compile(text)
if err != nil {
return fmt.Errorf("invalid regex: %s", o.RHS.String())
}
}
// this condition may not be possible to hit since it's not parseable.
// however, if we did somehow end up this situation, it would be good to return
// a reasonable error
switch o.Op {
case OpSpansetChild,
OpSpansetParent,
OpSpansetDescendant,
OpSpansetAncestor,
OpSpansetSibling,
OpSpansetNotChild,
OpSpansetNotParent,
OpSpansetNotSibling,
OpSpansetNotAncestor,
OpSpansetNotDescendant,
OpSpansetUnionChild,
OpSpansetUnionParent,
OpSpansetUnionSibling,
OpSpansetUnionAncestor,
OpSpansetUnionDescendant:
return newUnsupportedError(fmt.Sprintf("binary operation (%v)", o.Op))
}
return nil
}
func (o UnaryOperation) validate() error {
if err := o.Expression.validate(); err != nil {
return err
}
// Disallow intrinsic=nil
if o.Op == OpNotExists {
if attr, ok := o.Expression.(Attribute); ok {
switch {
case attr.Intrinsic != IntrinsicNone:
return fmt.Errorf("%s=nil is not valid because intrinsics cannot be nil", attr.String())
case attr == NewScopedAttribute(AttributeScopeResource, false, "service.name"):
return fmt.Errorf("%s=nil is not valid because resource.service.name cannot be nil", attr.String())
}
}
}
t := o.Expression.impliedType()
if t == TypeAttribute {
return nil
}
if !o.Op.unaryTypesValid(t) {
return fmt.Errorf("illegal operation for the given type: %s", o.String())
}
return nil
}
func (s Static) validate() error {
return nil
}
func (a Attribute) validate() error {
if a.Parent {
return newUnsupportedError("parent")
}
switch a.Intrinsic {
case IntrinsicParent:
return newUnsupportedError(fmt.Sprintf("intrinsic (%v)", a.Intrinsic))
}
return nil
}
func (h *Hints) validate() error {
return nil
}