VictoriaMetrics/lib/logstorage/pipe_math_test.go

170 lines
2.5 KiB
Go
Raw Normal View History

2024-05-28 00:06:40 +00:00
package logstorage
import (
"testing"
)
func TestParsePipeMathSuccess(t *testing.T) {
f := func(pipeStr string) {
t.Helper()
expectParsePipeSuccess(t, pipeStr)
}
2024-05-28 12:21:29 +00:00
f(`math b as a`)
f(`math -123 as a`)
f(`math 12.345KB as a`)
f(`math (-2 + 2) as a`)
f(`math x as a, z as y`)
f(`math (foo / bar + baz * abc % -45ms) as a`)
f(`math (foo / (bar + baz) * abc ^ 2) as a`)
f(`math (foo / ((bar + baz) * abc) ^ -2) as a`)
f(`math (foo + bar / baz - abc) as a`)
2024-05-28 12:51:54 +00:00
f(`math min(3, foo, (1 + bar) / baz) as a, max(a, b) as b, (abs(c) + 5) as d`)
f(`math round(foo, 0.1) as y`)
2024-05-28 00:06:40 +00:00
}
func TestParsePipeMathFailure(t *testing.T) {
f := func(pipeStr string) {
t.Helper()
expectParsePipeFailure(t, pipeStr)
}
f(`math`)
f(`math x`)
2024-05-28 12:21:29 +00:00
f(`math x as`)
f(`math abs() as x`)
f(`math abs(a, b) as x`)
f(`math min() as x`)
f(`math min(a) as x`)
f(`math max() as x`)
f(`math max(a) as x`)
2024-05-28 12:51:54 +00:00
f(`math round() as x`)
f(`math round(a) as x`)
f(`math round(a, b, c) as x`)
2024-05-28 00:06:40 +00:00
}
func TestPipeMath(t *testing.T) {
f := func(pipeStr string, rows, rowsExpected [][]Field) {
t.Helper()
expectPipeResults(t, pipeStr, rows, rowsExpected)
}
2024-05-28 12:21:29 +00:00
f("math 1 as a", [][]Field{
2024-05-28 00:06:40 +00:00
{
{"a", "v1"},
{"b", "2"},
{"c", "3"},
},
}, [][]Field{
{
{"a", "1"},
{"b", "2"},
{"c", "3"},
},
})
2024-05-28 12:51:54 +00:00
f("math 10 * 5 - 3 a", [][]Field{
2024-05-28 00:06:40 +00:00
{
{"a", "v1"},
{"b", "2"},
{"c", "3"},
},
}, [][]Field{
{
{"a", "47"},
{"b", "2"},
{"c", "3"},
},
})
2024-05-28 12:21:29 +00:00
f("math -1.5K as a", [][]Field{
2024-05-28 00:06:40 +00:00
{
{"a", "v1"},
{"b", "2"},
{"c", "3"},
},
}, [][]Field{
{
{"a", "-1500"},
{"b", "2"},
{"c", "3"},
},
})
2024-05-28 12:21:29 +00:00
f("math b as a", [][]Field{
2024-05-28 00:06:40 +00:00
{
{"a", "v1"},
{"b", "2"},
{"c", "3"},
},
}, [][]Field{
{
{"a", "2"},
{"b", "2"},
{"c", "3"},
},
})
2024-05-28 12:21:29 +00:00
f("math a as a", [][]Field{
2024-05-28 00:06:40 +00:00
{
{"a", "v1"},
{"b", "2"},
{"c", "3"},
},
}, [][]Field{
{
{"a", "NaN"},
{"b", "2"},
{"c", "3"},
},
})
2024-05-28 12:21:29 +00:00
f("math 2*c + b as x", [][]Field{
2024-05-28 00:06:40 +00:00
{
{"a", "v1"},
{"b", "2"},
{"c", "3"},
},
}, [][]Field{
{
{"a", "v1"},
{"b", "2"},
{"c", "3"},
{"x", "8"},
},
})
2024-05-28 12:51:54 +00:00
f("math round((2*c + (b%c))/(c-b)^(b-1), 0.001) as a", [][]Field{
2024-05-28 00:06:40 +00:00
{
{"a", "v"},
{"b", "2"},
{"c", "3"},
},
{
{"a", "x"},
{"b", "3"},
{"c", "5"},
},
{
{"b", "3"},
{"c", "6"},
},
}, [][]Field{
{
{"a", "8"},
{"b", "2"},
{"c", "3"},
},
{
2024-05-28 12:51:54 +00:00
{"a", "3.25"},
2024-05-28 00:06:40 +00:00
{"b", "3"},
{"c", "5"},
},
{
2024-05-28 12:51:54 +00:00
{"a", "1.667"},
2024-05-28 00:06:40 +00:00
{"b", "3"},
{"c", "6"},
},
})
}