Skip to content
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
46 changes: 45 additions & 1 deletion pkg/logql/count_min_sketch.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import (
"container/heap"
"context"
"fmt"
"slices"
"strings"
Expand Down Expand Up @@ -322,7 +323,7 @@
buffer []byte
}

var _ StepEvaluator = NewQuantileSketchVectorStepEvaluator(nil, 0)
var _ StepEvaluator = NewCountMinSketchVectorStepEvaluator(nil)

func NewCountMinSketchVectorStepEvaluator(vec *CountMinSketchVector) *CountMinSketchVectorStepEvaluator {
return &CountMinSketchVectorStepEvaluator{
Expand Down Expand Up @@ -357,3 +358,46 @@
func (*CountMinSketchVectorStepEvaluator) Close() error { return nil }

func (*CountMinSketchVectorStepEvaluator) Error() error { return nil }

var _ StepEvaluator = (*countMinSketchEvalStepEvaluator)(nil)

// countMinSketchEvalStepEvaluator transforms a CountMinSketchEvalExpr into a CountMinSketchVector.
type countMinSketchEvalStepEvaluator struct {
ctx context.Context
nextEvFactory SampleEvaluatorFactory
expr *CountMinSketchEvalExpr
params Params
}

func NewCountMinSketchEvalStepEvaluator(ctx context.Context, nextEvFactory SampleEvaluatorFactory, expr *CountMinSketchEvalExpr, params Params) (*countMinSketchEvalStepEvaluator, error) {

Check warning on line 372 in pkg/logql/count_min_sketch.go

View workflow job for this annotation

GitHub Actions / check / golangciLint

unexported-return: exported func NewCountMinSketchEvalStepEvaluator returns unexported type *logql.countMinSketchEvalStepEvaluator, which can be annoying to use (revive)
return &countMinSketchEvalStepEvaluator{
ctx: ctx,
nextEvFactory: nextEvFactory,
expr: expr,
params: params,
}, nil
}

func (e *countMinSketchEvalStepEvaluator) Next() (bool, int64, StepResult) {
nextEv, err := e.nextEvFactory.NewStepEvaluator(e.ctx, e.nextEvFactory, e.expr.SampleExpr, e.params)
if err != nil {
return false, 0, CountMinSketchVector{}
}

ok, _, results := nextEv.Next()
if !ok {
return false, 0, CountMinSketchVector{}
}

data := results.CountMinSketchVec()
handler := NewCountMinSketchVectorStepEvaluator(&data)

return handler.Next()
}

func (*countMinSketchEvalStepEvaluator) Close() error { return nil }

func (*countMinSketchEvalStepEvaluator) Error() error { return nil }

func (e *countMinSketchEvalStepEvaluator) Explain(parent Node) {

Check warning on line 402 in pkg/logql/count_min_sketch.go

View workflow job for this annotation

GitHub Actions / check / golangciLint

unused-parameter: parameter 'parent' seems to be unused, consider removing or renaming it as _ (revive)
}
3 changes: 3 additions & 0 deletions pkg/logql/downstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,9 @@ func (e CountMinSketchEvalExpr) String() string {

sb.WriteString(d.String())
}
if len(e.downstreams) == 0 {
sb.WriteString(e.SampleExpr.String())
}
return fmt.Sprintf("CountMinSketchEval<%s>", sb.String())
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/logql/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,8 @@ func (ev *DefaultEvaluator) NewStepEvaluator(
})
}
return newVectorAggEvaluator(ctx, nextEvFactory, e, q, ev.maxCountMinSketchHeapSize)
case *CountMinSketchEvalExpr:
return NewCountMinSketchEvalStepEvaluator(ctx, nextEvFactory, e, q)
case *syntax.RangeAggregationExpr:
it, err := ev.querier.SelectSamples(ctx, SelectSampleParams{
&logproto.SampleQueryRequest{
Expand Down
26 changes: 26 additions & 0 deletions pkg/logql/optimize.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,36 @@ func optimizeSampleExpr(expr syntax.SampleExpr) (syntax.SampleExpr, error) {
if err != nil {
return nil, err
}
replaceApproxTopK(expr)
removeLineformat(expr)
return expr, nil
}

// replaceApproxTopKWithTopk replaces all ApproxTopKExpr with TopKExpr.
// ApproxTopKExpr is not supported by the querier, so we replace it with the implementation if this function reaches the querier.
func replaceApproxTopK(expr syntax.SampleExpr) {
expr.Walk(func(e syntax.Expr) bool {
vectorExpr, ok := e.(*syntax.VectorAggregationExpr)
if !ok {
return true
}
if vectorExpr.Operation != syntax.OpTypeApproxTopK {
return true
}

vectorExpr.Operation = syntax.OpTypeTopK
vectorExpr.Left = &CountMinSketchEvalExpr{
SampleExpr: &syntax.VectorAggregationExpr{
Operation: syntax.OpTypeCountMinSketch,
Params: 0,
Grouping: vectorExpr.Grouping,
Left: vectorExpr.Left,
},
}
return true
})
}

// removeLineformat removes unnecessary line_format within a SampleExpr.
func removeLineformat(expr syntax.SampleExpr) {
expr.Walk(func(e syntax.Expr) bool {
Expand Down
4 changes: 4 additions & 0 deletions pkg/logql/optimize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ func Test_optimizeSampleExpr(t *testing.T) {
{`sum by(name)(rate({region="us-east1"} | json | line_format "something else" | unwrap foo[5m]))`, `sum by (name)(rate({region="us-east1"} | json | unwrap foo[5m]))`},
{`quantile_over_time(1,{region="us-east1"} | json | line_format "something else" | unwrap foo[5m])`, `quantile_over_time(1,{region="us-east1"} | json | unwrap foo[5m])`},
{`sum by(name)(count_over_time({region="us-east1"} | json | line_format "something else" | label_format foo=bar | line_format "boo"[5m]))`, `sum by (name)(count_over_time({region="us-east1"} | json | label_format foo=bar[5m]))`},

// Replace approx_topk with __count_min_sketch__ variant
{`approx_topk(3, 1+1)`, `topk(3,CountMinSketchEval<__count_min_sketch__(2)>)`},
{`approx_topk(3, sum by(name)(rate({region="us-east1"}[5m])))`, `topk(3,CountMinSketchEval<__count_min_sketch__(sum by (name)(rate({region="us-east1"}[5m])))>)`},
}
for _, tt := range tests {
t.Run(tt.in, func(t *testing.T) {
Expand Down
Loading