forked from influxdata/flux
/
binary_expr.go
353 lines (318 loc) · 9.37 KB
/
binary_expr.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
package promql
import (
"fmt"
"github.com/influxdata/flux/ast"
"github.com/influxdata/promql/v2"
)
var arithBinOps = map[promql.ItemType]ast.OperatorKind{
promql.ItemADD: ast.AdditionOperator,
promql.ItemSUB: ast.SubtractionOperator,
promql.ItemMUL: ast.MultiplicationOperator,
promql.ItemDIV: ast.DivisionOperator,
}
var arithBinOpFns = map[promql.ItemType]string{
promql.ItemPOW: "math.pow",
promql.ItemMOD: "math.mod",
}
var compBinOps = map[promql.ItemType]ast.OperatorKind{
promql.ItemEQL: ast.EqualOperator,
promql.ItemNEQ: ast.NotEqualOperator,
promql.ItemGTR: ast.GreaterThanOperator,
promql.ItemLSS: ast.LessThanOperator,
promql.ItemGTE: ast.GreaterThanEqualOperator,
promql.ItemLTE: ast.LessThanEqualOperator,
}
// Function to apply an arithmetic binary operator to all values in a table and a given float64 operand.
func scalarArithBinaryOpFn(op ast.OperatorKind, operand ast.Expression, swapped bool) *ast.FunctionExpression {
val := member("r", "_value")
var lhs, rhs ast.Expression = val, operand
if swapped {
lhs, rhs = rhs, lhs
}
// (r) => {r with _value: <lhs> <op> <rhs>, _stop: r._stop}
return &ast.FunctionExpression{
Params: []*ast.Property{
{
Key: &ast.Identifier{
Name: "r",
},
},
},
Body: &ast.ObjectExpression{
With: &ast.Identifier{Name: "r"},
Properties: []*ast.Property{
{
Key: &ast.Identifier{Name: "_value"},
Value: &ast.BinaryExpression{
Operator: op,
Left: lhs,
Right: rhs,
},
},
{
Key: &ast.Identifier{Name: "_stop"},
Value: member("r", "_stop"),
},
},
},
}
}
// Function to apply a comparison binary operator to all values in a table and a given float64 operand.
func scalarCompBinaryOpFn(op ast.OperatorKind, operand ast.Expression, swapped bool) *ast.FunctionExpression {
val := member("r", "_value")
var lhs, rhs ast.Expression = val, operand
if swapped {
lhs, rhs = rhs, lhs
}
// (r) => <lhs> <op> <rhs>
return &ast.FunctionExpression{
Params: []*ast.Property{
{
Key: &ast.Identifier{
Name: "r",
},
},
},
Body: &ast.BinaryExpression{
Operator: op,
Left: lhs,
Right: rhs,
},
}
}
// Function to apply a binary arithmetic operator between values of two joined tables.
func vectorArithBinaryOpFn(op ast.OperatorKind) *ast.FunctionExpression {
lhs := member("r", "_value_lhs")
rhs := member("r", "_value_rhs")
// (r) => {r with _value: <lhs> <op> <rhs>, _stop: r._stop}
return &ast.FunctionExpression{
Params: []*ast.Property{
{
Key: &ast.Identifier{
Name: "r",
},
},
},
Body: &ast.ObjectExpression{
With: &ast.Identifier{Name: "r"},
Properties: []*ast.Property{
{
Key: &ast.Identifier{Name: "_value"},
Value: &ast.BinaryExpression{
Operator: op,
Left: lhs,
Right: rhs,
},
},
{
Key: &ast.Identifier{Name: "_stop"},
Value: member("r", "_stop"),
},
},
},
}
}
// Function to apply a binary arithmetic operator math function between values of two joined tables.
func vectorArithBinaryMathFn(mathFn string) *ast.FunctionExpression {
lhs := member("r", "_value_lhs")
rhs := member("r", "_value_rhs")
// (r) => {r with _value: mathFn(<lhs>, <rhs>), _stop: r._stop}
return &ast.FunctionExpression{
Params: []*ast.Property{
{
Key: &ast.Identifier{
Name: "r",
},
},
},
Body: &ast.ObjectExpression{
With: &ast.Identifier{Name: "r"},
Properties: []*ast.Property{
{
Key: &ast.Identifier{Name: "_value"},
Value: call(mathFn, map[string]ast.Expression{"x": lhs, "y": rhs}),
},
{
Key: &ast.Identifier{Name: "_stop"},
Value: member("r", "_stop"),
},
},
},
}
}
// Function to apply a binary comparison operator between values of two joined tables.
func vectorCompBinaryOpFn(op ast.OperatorKind) *ast.FunctionExpression {
// (r) => <lhs> <op> <rhs>
return &ast.FunctionExpression{
Params: []*ast.Property{
{
Key: &ast.Identifier{
Name: "r",
},
},
},
Body: &ast.BinaryExpression{
Operator: op,
Left: member("r", "_value_lhs"),
Right: member("r", "_value_rhs"),
},
}
}
func (t *Transpiler) transpileBinaryExpr(b *promql.BinaryExpr) (ast.Expression, error) {
lhs, err := t.transpileExpr(b.LHS)
if err != nil {
return nil, fmt.Errorf("unable to transpile left-hand side of binary operation: %s", err)
}
rhs, err := t.transpileExpr(b.RHS)
if err != nil {
return nil, fmt.Errorf("unable to transpile right-hand side of binary operation: %s", err)
}
swapped := false
switch {
case yieldsFloat(b.LHS) && yieldsFloat(b.RHS):
if op, ok := arithBinOps[b.Op]; ok {
return &ast.BinaryExpression{
Operator: op,
Left: lhs,
Right: rhs,
}, nil
}
if opFn, ok := arithBinOpFns[b.Op]; ok {
return call(opFn, map[string]ast.Expression{"x": lhs, "y": rhs}), nil
}
if op, ok := compBinOps[b.Op]; ok {
if !b.ReturnBool {
// This is already caught by the PromQL parser.
return nil, fmt.Errorf("scalar-to-scalar binary op is missing 'bool' modifier (this should never happen)")
}
return call("float", map[string]ast.Expression{
"v": &ast.BinaryExpression{
Operator: op,
Left: lhs,
Right: rhs,
},
}), nil
}
return nil, fmt.Errorf("invalid scalar-scalar binary op %q (this should never happen)", b.Op)
case yieldsFloat(b.LHS) && yieldsTable(b.RHS):
lhs, rhs = rhs, lhs
swapped = true
fallthrough
case yieldsTable(b.LHS) && yieldsFloat(b.RHS):
if op, ok := arithBinOps[b.Op]; ok {
return buildPipeline(
lhs,
call("map", map[string]ast.Expression{"fn": scalarArithBinaryOpFn(op, rhs, swapped)}),
dropFieldAndTimeCall,
), nil
}
if opFn, ok := arithBinOpFns[b.Op]; ok {
return buildPipeline(
lhs,
call("map", map[string]ast.Expression{"fn": scalarArithBinaryMathFn(opFn, rhs, swapped)}),
dropFieldAndTimeCall,
), nil
}
if op, ok := compBinOps[b.Op]; ok {
if b.ReturnBool {
return buildPipeline(
lhs,
call("map", map[string]ast.Expression{
"fn": scalarArithBinaryOpFn(op, rhs, swapped),
}),
call("toFloat", nil),
dropFieldAndTimeCall,
), nil
}
return buildPipeline(
lhs,
call("filter", map[string]ast.Expression{"fn": scalarCompBinaryOpFn(op, rhs, swapped)}),
), nil
}
return nil, fmt.Errorf("invalid scalar-vector binary op %q (this should never happen)", b.Op)
default:
if b.VectorMatching == nil {
// We end up in this branch for non-const scalar-typed PromQL nodes,
// which don't have VectorMatching initialized.
b.VectorMatching = &promql.VectorMatching{
On: true,
}
} else if !b.VectorMatching.On || len(b.VectorMatching.MatchingLabels) == 0 {
return nil, fmt.Errorf("vector-to-vector binary expressions without on() clause not supported yet")
}
dropField := true
var opCalls []*ast.CallExpression
if op, ok := arithBinOps[b.Op]; ok {
opCalls = []*ast.CallExpression{
call("map", map[string]ast.Expression{"fn": vectorArithBinaryOpFn(op)}),
}
} else if opFn, ok := arithBinOpFns[b.Op]; ok {
opCalls = []*ast.CallExpression{
call("map", map[string]ast.Expression{"fn": vectorArithBinaryMathFn(opFn)}),
}
} else if op, ok := compBinOps[b.Op]; ok {
if b.ReturnBool {
opCalls = []*ast.CallExpression{
call("map", map[string]ast.Expression{"fn": vectorArithBinaryOpFn(op)}),
call("toFloat", nil),
}
} else {
opCalls = []*ast.CallExpression{
call("filter", map[string]ast.Expression{"fn": vectorCompBinaryOpFn(op)}),
}
if b.LHS.Type() == promql.ValueTypeScalar {
// For <scalar> <comp-op> <vector> filter expressions, we always want to
// return the sample value from the vector, not the scalar.
opCalls = append(
opCalls,
call("duplicate", map[string]ast.Expression{
"column": &ast.StringLiteral{Value: "_value_rhs"},
"as": &ast.StringLiteral{Value: "_value_lhs"},
}),
)
}
dropField = false
}
} else {
return nil, fmt.Errorf("vector set operations not supported yet")
}
onCols := append(b.VectorMatching.MatchingLabels, "_start", "_stop")
outputColTransformCalls := []*ast.CallExpression{
call("keep", map[string]ast.Expression{
"columns": columnList(append(append(onCols, "_value"), b.VectorMatching.Include...)...),
}),
// TODO: Fix binary operations once new join implementation exists.
//
// // Rename x_lhs -> x.
// call("rename", map[string]ast.Expression{"fn": stripSuffixFn("_lhs")}),
// // Drop cols RHS cols, except ones we want to copy into the result via a group_x(...) clause.
// call("drop", map[string]ast.Expression{"columns": columnList(b.VectorMatching.Include...)}),
// // Rename x_rhs -> x.
// call("rename", map[string]ast.Expression{"fn": stripSuffixFn("_rhs")}),
// // Drop any remaining RHS cols.
// call("drop", map[string]ast.Expression{"fn": matchRHSSuffixFn}),
}
postJoinCalls := append(opCalls, outputColTransformCalls...)
if dropField {
postJoinCalls = append(postJoinCalls, dropFieldAndTimeCall)
}
return buildPipeline(
call("join", map[string]ast.Expression{
"tables": &ast.ObjectExpression{
Properties: []*ast.Property{
{
Key: &ast.Identifier{Name: "lhs"},
Value: lhs,
},
{
Key: &ast.Identifier{Name: "rhs"},
Value: rhs,
},
},
},
"on": columnList(onCols...),
}),
postJoinCalls...,
), nil
}
}