-
Notifications
You must be signed in to change notification settings - Fork 152
/
unary_expr.go
39 lines (34 loc) · 1013 Bytes
/
unary_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
package promql
import (
"fmt"
"github.com/influxdata/flux/ast"
"github.com/influxdata/promql/v2"
)
func (t *Transpiler) transpileUnaryExpr(ue *promql.UnaryExpr) (ast.Expression, error) {
expr, err := t.transpileExpr(ue.Expr)
if err != nil {
return nil, fmt.Errorf("error transpiling expression in unary expression: %s", err)
}
switch ue.Op {
case promql.ItemADD:
return expr, nil
case promql.ItemSUB:
if yieldsTable(ue.Expr) {
// Multiply all table _value columns by -1.
return buildPipeline(
expr,
call("map", map[string]ast.Expression{
"fn": scalarArithBinaryOpFn(ast.MultiplicationOperator, &ast.FloatLiteral{Value: -1}, false)},
),
dropFieldAndTimeCall,
), nil
}
return &ast.UnaryExpression{
Operator: ast.SubtractionOperator,
Argument: expr,
}, nil
default:
// PromQL fails to parse unary operators other than +/-, so this should never happen.
return nil, fmt.Errorf("invalid unary expression operator type (this should never happen)")
}
}