MCPcopy
hub / github.com/grafana/tempo / TestBinaryAndUnaryOperationsRewrites

Function TestBinaryAndUnaryOperationsRewrites

pkg/traceql/parse_test.go:1051–1083  ·  pkg/traceql/parse_test.go::TestBinaryAndUnaryOperationsRewrites

TestBinaryAndUnaryOperationsRewrites tests code in the newBinaryOperation and newUnaryOperation functions that attempts to simplify combinations of static values where possible.

(t *testing.T)

Source from the content-addressed store, hash-verified

1049// TestBinaryAndUnaryOperationsRewrites tests code in the newBinaryOperation and newUnaryOperation functions
1050// that attempts to simplify combinations of static values where possible.
1051func TestBinaryAndUnaryOperationsRewrites(t *testing.T) {
1052 tests := []struct {
1053 in string
1054 expected FieldExpression
1055 }{
1056 // collapse to statics
1057 {in: "{ duration > 1 + 2}", expected: newBinaryOperation(OpGreater, NewIntrinsic(IntrinsicDuration), NewStaticInt(3))},
1058 {in: "{ -1 }", expected: NewStaticInt(-1)},
1059 {in: "{ 1 + 1 > 1 }", expected: NewStaticBool(true)},
1060 {in: "{ `foo` = `bar` }", expected: NewStaticBool(false)},
1061 {in: "{ 1 = 1. }", expected: NewStaticBool(true)}, // this is an interesting case, it returns true even though { span.foo = 1 } would be false if span.foo had the float value 1.0
1062 {in: "{ .1 + 1 }", expected: NewStaticFloat(1.1)},
1063 {in: "{ 1 * -1 = -1 }", expected: NewStaticBool(true)},
1064 {in: "{ .foo * -1. = -1 }", expected: newBinaryOperation(OpEqual, newBinaryOperation(OpMult, NewAttribute("foo"), NewStaticFloat(-1)), NewStaticInt(-1))},
1065 // rewrite != nil to existence
1066 {in: "{ .foo != nil }", expected: newUnaryOperation(OpExists, NewAttribute("foo"))},
1067 {in: "{ .foo = nil }", expected: newUnaryOperation(OpNotExists, NewAttribute("foo"))},
1068 {in: "{ nil != .foo }", expected: newUnaryOperation(OpExists, NewAttribute("foo"))},
1069 {in: "{ nil = .foo }", expected: newUnaryOperation(OpNotExists, NewAttribute("foo"))},
1070 }
1071
1072 test := func(t *testing.T, q string, expected FieldExpression) {
1073 actual, err := Parse(q)
1074 require.NoError(t, err, q)
1075 require.Equal(t, newRootExpr(newPipeline(newSpansetFilter(expected))), actual, q)
1076 }
1077
1078 for _, tc := range tests {
1079 t.Run(tc.in, func(t *testing.T) {
1080 test(t, tc.in, tc.expected)
1081 })
1082 }
1083}
1084
1085func TestAttributes(t *testing.T) {
1086 tests := []struct {

Callers

nothing calls this directly

Calls 14

newBinaryOperationFunction · 0.85
NewIntrinsicFunction · 0.85
NewStaticIntFunction · 0.85
NewStaticBoolFunction · 0.85
NewStaticFloatFunction · 0.85
NewAttributeFunction · 0.85
newUnaryOperationFunction · 0.85
ParseFunction · 0.85
newRootExprFunction · 0.85
newPipelineFunction · 0.85
newSpansetFilterFunction · 0.85
testStruct · 0.85

Tested by

no test coverage detected