From 65dd15238715ac55d120dcff240f053e263537e1 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Thu, 16 May 2024 00:35:49 +0200 Subject: [PATCH] wip --- lib/logstorage/parser_test.go | 6 ++++-- lib/logstorage/pipe_stats.go | 13 +++---------- lib/logstorage/pipe_stats_test.go | 6 +++--- 3 files changed, 10 insertions(+), 15 deletions(-) diff --git a/lib/logstorage/parser_test.go b/lib/logstorage/parser_test.go index 2a021908a..4ecfd7006 100644 --- a/lib/logstorage/parser_test.go +++ b/lib/logstorage/parser_test.go @@ -1457,11 +1457,10 @@ func TestQueryGetNeededColumns(t *testing.T) { f(`* | sort by (f1) | sort by (f2,f3 desc) desc | fields f4 | rm f1,f2,f5`, `f1,f2,f3,f4`, ``) f(`* | stats by(f1) count(f2) r1, count(f3,f4) r2`, `f1,f2,f3,f4`, ``) - f(`* | stats by(f1) count(f2) r1, count(f3,f4) r2 | fields f5,f6`, ``, ``) + f(`* | stats by(f1) count(f2) r1, count(f3,f4) r2 | fields f5,f6`, `f1`, ``) f(`* | stats by(f1) count(f2) r1, count(f3,f4) r2 | fields f1,f5`, `f1`, ``) f(`* | stats by(f1) count(f2) r1, count(f3,f4) r2 | fields r1`, `f1,f2`, ``) f(`* | stats by(f1) count(f2) r1, count(f3,f4) r2 | fields r2,r3`, `f1,f3,f4`, ``) - f(`_time:5m | stats by(_time:day) count() r1 | stats values(_time) r2`, `_time`, ``) f(`* | stats count(f1) r1 | stats count() r1`, ``, ``) f(`* | stats count(f1) r1 | stats count() r2`, ``, ``) f(`* | stats count(f1) r1 | stats count(r1) r2`, `f1`, ``) @@ -1473,6 +1472,9 @@ func TestQueryGetNeededColumns(t *testing.T) { f(`* | stats by(f3,f4) count(f1,f2) r1 | stats count(f2) r1, count(r1) r2 | fields r2`, `f1,f2,f3,f4`, ``) f(`* | stats by(f3,f4) count(f1,f2) r1 | stats count(f3) r1, count(r1) r2 | fields r1`, `f3,f4`, ``) + f(`_time:5m | stats by(_time:day) count() r1 | stats values(_time) r2`, `_time`, ``) + f(`_time:1y | stats (_time:1w) count() r1 | stats count() r2`, `_time`, ``) + f(`* | uniq`, `*`, ``) f(`* | uniq by (f1,f2)`, `f1,f2`, ``) f(`* | uniq by (f1,f2) | fields f1,f3`, `f1,f2`, ``) diff --git a/lib/logstorage/pipe_stats.go b/lib/logstorage/pipe_stats.go index 62b3def5b..bdde63db3 100644 --- a/lib/logstorage/pipe_stats.go +++ b/lib/logstorage/pipe_stats.go @@ -87,21 +87,14 @@ func (ps *pipeStats) updateNeededFields(neededFields, unneededFields fieldsSet) neededFieldsOrig := neededFields.clone() neededFields.reset() - byFields := make([]string, len(ps.byFields)) - for i, bf := range ps.byFields { - byFields[i] = bf.name - } - - for _, f := range byFields { - if neededFieldsOrig.contains(f) && !unneededFields.contains(f) { - neededFields.addAll(byFields) - } + // byFields are needed unconditionally, since the output number of rows depends on them. + for _, bf := range ps.byFields { + neededFields.add(bf.name) } for i, resultName := range ps.resultNames { if neededFieldsOrig.contains(resultName) && !unneededFields.contains(resultName) { funcFields := ps.funcs[i].neededFields() - neededFields.addAll(byFields) neededFields.addAll(funcFields) } } diff --git a/lib/logstorage/pipe_stats_test.go b/lib/logstorage/pipe_stats_test.go index ef328adec..c5a3f5481 100644 --- a/lib/logstorage/pipe_stats_test.go +++ b/lib/logstorage/pipe_stats_test.go @@ -44,7 +44,7 @@ func TestPipeStatsUpdateNeededFields(t *testing.T) { f("stats count(f1,f2) r1, sum(f3,f4) r2", "*", "r1,r3", "f3,f4", "") f("stats by (b1,b2) count(f1,f2) r1", "*", "r1,r2", "b1,b2", "") f("stats by (b1,b2) count(f1,f2) r1", "*", "r1,r2,b1", "b1,b2", "") - f("stats by (b1,b2) count(f1,f2) r1", "*", "r1,r2,b1,b2", "", "") + f("stats by (b1,b2) count(f1,f2) r1", "*", "r1,r2,b1,b2", "b1,b2", "") f("stats by (b1,b2) count(f1,f2) r1, count(f1,f3) r2", "*", "r1,r3", "b1,b2,f1,f3", "") // needed fields do not intersect with stats fields @@ -52,8 +52,8 @@ func TestPipeStatsUpdateNeededFields(t *testing.T) { f("stats count(*) r1", "r2", "", "", "") f("stats count(f1,f2) r1", "r2", "", "", "") f("stats count(f1,f2) r1, sum(f3,f4) r2", "r3", "", "", "") - f("stats by (b1,b2) count(f1,f2) r1", "r2", "", "", "") - f("stats by (b1,b2) count(f1,f2) r1, count(f1,f3) r2", "r3", "", "", "") + f("stats by (b1,b2) count(f1,f2) r1", "r2", "", "b1,b2", "") + f("stats by (b1,b2) count(f1,f2) r1, count(f1,f3) r2", "r3", "", "b1,b2", "") // needed fields intersect with stats fields f("stats count() r1", "r1,r2", "", "", "")