Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes vector grouping injection. #2975

Merged
merged 2 commits into from
Nov 25, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion pkg/logql/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -711,12 +711,25 @@ func (e *vectorAggregationExpr) Selector() LogSelectorExpr {
func (e *vectorAggregationExpr) Extractor() (log.SampleExtractor, error) {
// inject in the range vector extractor the outer groups to improve performance.
// This is only possible if the operation is a sum. Anything else needs all labels.
if r, ok := e.left.(*rangeAggregationExpr); ok && e.operation == OpTypeSum {
if r, ok := e.left.(*rangeAggregationExpr); ok && canInjectVectorGrouping(e.operation, r.operation) {
return r.extractor(e.grouping, len(e.grouping.groups) == 0)
}
return e.left.Extractor()
}

// canInjectVectorGrouping tells if a vector operation can inject grouping into the nested range vector.
func canInjectVectorGrouping(vecOp, rangeOp string) bool {
if vecOp != OpTypeSum {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should work with other types as well (min, max, count), right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know from fact that it doesn't work with count, we have a test in sharding that fails.

It might work for max and min but I don't have a test backing me up.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This basically deserves a big test harness.

return false
}
switch rangeOp {
case OpRangeTypeBytes, OpRangeTypeBytesRate, OpRangeTypeSum, OpRangeTypeRate, OpRangeTypeCount:
return true
default:
return false
}
}

func (e *vectorAggregationExpr) String() string {
var params []string
if e.params != 0 {
Expand Down
38 changes: 37 additions & 1 deletion pkg/logql/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"

"github.com/grafana/loki/pkg/logql/log"

"github.com/prometheus/prometheus/pkg/labels"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -340,3 +339,40 @@ func mustNewRegexParser(re string) log.Stage {
}
return r
}

func Test_canInjectVectorGrouping(t *testing.T) {

tests := []struct {
vecOp string
rangeOp string
want bool
}{
{OpTypeSum, OpRangeTypeBytes, true},
{OpTypeSum, OpRangeTypeBytesRate, true},
{OpTypeSum, OpRangeTypeSum, true},
{OpTypeSum, OpRangeTypeRate, true},
{OpTypeSum, OpRangeTypeCount, true},

{OpTypeSum, OpRangeTypeAvg, false},
{OpTypeSum, OpRangeTypeMax, false},
{OpTypeSum, OpRangeTypeQuantile, false},
{OpTypeSum, OpRangeTypeStddev, false},
{OpTypeSum, OpRangeTypeStdvar, false},
{OpTypeSum, OpRangeTypeMin, false},
{OpTypeSum, OpRangeTypeMax, false},

{OpTypeAvg, OpRangeTypeBytes, false},
{OpTypeCount, OpRangeTypeBytesRate, false},
{OpTypeBottomK, OpRangeTypeSum, false},
{OpTypeMax, OpRangeTypeRate, false},
{OpTypeMin, OpRangeTypeCount, false},
{OpTypeTopK, OpRangeTypeCount, false},
}
for _, tt := range tests {
t.Run(tt.vecOp+"_"+tt.rangeOp, func(t *testing.T) {
if got := canInjectVectorGrouping(tt.vecOp, tt.rangeOp); got != tt.want {
t.Errorf("canInjectVectorGrouping() = %v, want %v", got, tt.want)
}
})
}
}