From 54b42b64a9287616cfab832b301a321476e1cee4 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Tue, 21 May 2024 14:53:03 +0200 Subject: [PATCH] wip --- lib/logstorage/stats_quantile.go | 4 +- lib/logstorage/stats_quantile_test.go | 337 ++++++++++++++++++++++++++ 2 files changed, 339 insertions(+), 2 deletions(-) diff --git a/lib/logstorage/stats_quantile.go b/lib/logstorage/stats_quantile.go index a9fcbdfb0..685281bb4 100644 --- a/lib/logstorage/stats_quantile.go +++ b/lib/logstorage/stats_quantile.go @@ -17,7 +17,7 @@ type statsQuantile struct { fields []string containsStar bool - phi float64 + phi float64 phiStr string } @@ -206,7 +206,7 @@ func parseStatsQuantile(lex *lexer) (*statsQuantile, error) { fields: fields, containsStar: slices.Contains(fields, "*"), - phi: phi, + phi: phi, phiStr: phiStr, } return sq, nil diff --git a/lib/logstorage/stats_quantile_test.go b/lib/logstorage/stats_quantile_test.go index 8ce3847b6..9cd8baa9c 100644 --- a/lib/logstorage/stats_quantile_test.go +++ b/lib/logstorage/stats_quantile_test.go @@ -30,6 +30,343 @@ func TestParseStatsQuantileFailure(t *testing.T) { f(`quantile(0.5, b) c`) } +func TestStatsQuantile(t *testing.T) { + f := func(pipeStr string, rows, rowsExpected [][]Field) { + t.Helper() + expectPipeResults(t, pipeStr, rows, rowsExpected) + } + + f("stats quantile(0.9, *) as x", [][]Field{ + { + {"_msg", `abc`}, + {"a", `2`}, + {"b", `3`}, + }, + { + {"_msg", `def`}, + {"a", `1`}, + }, + { + {"a", `3`}, + {"b", `54`}, + }, + }, [][]Field{ + { + {"x", "54"}, + }, + }) + + f("stats quantile(0.9, a) as x", [][]Field{ + { + {"_msg", `abc`}, + {"a", `2`}, + {"b", `3`}, + }, + { + {"_msg", `def`}, + {"a", `1`}, + }, + { + {"a", `3`}, + {"b", `54`}, + }, + }, [][]Field{ + { + {"x", "3"}, + }, + }) + + f("stats quantile(0.9, a, b) as x", [][]Field{ + { + {"_msg", `abc`}, + {"a", `2`}, + {"b", `3`}, + }, + { + {"_msg", `def`}, + {"a", `1`}, + }, + { + {"a", `3`}, + {"b", `54`}, + }, + }, [][]Field{ + { + {"x", "54"}, + }, + }) + + f("stats quantile(0.9, b) as x", [][]Field{ + { + {"_msg", `abc`}, + {"a", `2`}, + {"b", `3`}, + }, + { + {"_msg", `def`}, + {"a", `1`}, + }, + { + {"a", `3`}, + {"b", `54`}, + }, + }, [][]Field{ + { + {"x", "54"}, + }, + }) + + f("stats quantile(0.9, c) as x", [][]Field{ + { + {"_msg", `abc`}, + {"a", `2`}, + {"b", `3`}, + }, + { + {"_msg", `def`}, + {"a", `1`}, + }, + { + {"a", `3`}, + {"b", `54`}, + }, + }, [][]Field{ + { + {"x", "NaN"}, + }, + }) + + f("stats quantile(0.9, a) if (b:*) as x", [][]Field{ + { + {"_msg", `abc`}, + {"a", `2`}, + {"b", `3`}, + }, + { + {"_msg", `def`}, + {"a", `1`}, + }, + { + {"a", `3`}, + {"b", `54`}, + }, + }, [][]Field{ + { + {"x", "3"}, + }, + }) + + f("stats by (b) quantile(0.9, a) if (b:*) as x", [][]Field{ + { + {"_msg", `abc`}, + {"a", `2`}, + {"b", `3`}, + }, + { + {"_msg", `def`}, + {"a", `1`}, + {"b", "3"}, + }, + { + {"a", `3`}, + {"c", `54`}, + }, + }, [][]Field{ + { + {"b", "3"}, + {"x", "2"}, + }, + { + {"b", ""}, + {"x", "NaN"}, + }, + }) + + f("stats by (a) quantile(0.9, b) as x", [][]Field{ + { + {"_msg", `abc`}, + {"a", `1`}, + {"b", `3`}, + }, + { + {"_msg", `def`}, + {"a", `1`}, + }, + { + {"a", `3`}, + {"b", `5`}, + }, + { + {"a", `3`}, + {"b", `7`}, + }, + }, [][]Field{ + { + {"a", "1"}, + {"x", "3"}, + }, + { + {"a", "3"}, + {"x", "7"}, + }, + }) + + f("stats by (a) quantile(0.9, *) as x", [][]Field{ + { + {"_msg", `abc`}, + {"a", `1`}, + {"b", `3`}, + }, + { + {"_msg", `def`}, + {"a", `1`}, + {"c", "3"}, + }, + { + {"a", `3`}, + {"b", `5`}, + }, + { + {"a", `3`}, + {"b", `7`}, + }, + }, [][]Field{ + { + {"a", "1"}, + {"x", "3"}, + }, + { + {"a", "3"}, + {"x", "7"}, + }, + }) + + f("stats by (a) quantile(0.9, c) as x", [][]Field{ + { + {"_msg", `abc`}, + {"a", `1`}, + {"b", `3`}, + }, + { + {"_msg", `def`}, + {"a", `1`}, + }, + { + {"a", `3`}, + {"c", `5`}, + }, + { + {"a", `3`}, + {"b", `7`}, + }, + }, [][]Field{ + { + {"a", "1"}, + {"x", "NaN"}, + }, + { + {"a", "3"}, + {"x", "5"}, + }, + }) + + f("stats by (a) quantile(0.9, a, b, c) as x", [][]Field{ + { + {"_msg", `abc`}, + {"a", `1`}, + {"b", `3`}, + }, + { + {"_msg", `def`}, + {"a", `1`}, + {"c", "3"}, + }, + { + {"a", `3`}, + {"b", `5`}, + }, + { + {"a", `3`}, + {"b", `7`}, + }, + }, [][]Field{ + { + {"a", "1"}, + {"x", "3"}, + }, + { + {"a", "3"}, + {"x", "7"}, + }, + }) + + f("stats by (a, b) quantile(0.9, a) as x", [][]Field{ + { + {"_msg", `abc`}, + {"a", `1`}, + {"b", `3`}, + }, + { + {"_msg", `def`}, + {"a", `1`}, + {"c", "3"}, + }, + { + {"a", `3`}, + {"b", `5`}, + }, + }, [][]Field{ + { + {"a", "1"}, + {"b", "3"}, + {"x", "1"}, + }, + { + {"a", "1"}, + {"b", ""}, + {"x", "1"}, + }, + { + {"a", "3"}, + {"b", "5"}, + {"x", "3"}, + }, + }) + + f("stats by (a, b) quantile(0.9, c) as x", [][]Field{ + { + {"_msg", `abc`}, + {"a", `1`}, + {"b", `3`}, + }, + { + {"_msg", `def`}, + {"a", `1`}, + {"c", "3"}, + }, + { + {"a", `3`}, + {"b", `5`}, + }, + }, [][]Field{ + { + {"a", "1"}, + {"b", "3"}, + {"x", "NaN"}, + }, + { + {"a", "1"}, + {"b", ""}, + {"x", "3"}, + }, + { + {"a", "3"}, + {"b", "5"}, + {"x", "NaN"}, + }, + }) +} + func TestHistogramQuantile(t *testing.T) { f := func(a []float64, phi, qExpected float64) { t.Helper()