-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7e2f0a1
commit ccd2596
Showing
5 changed files
with
157 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Copyright (c) The Thanos Community Authors. | ||
// Licensed under the Apache License 2.0. | ||
|
||
package logicalplan | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/prometheus/prometheus/promql/parser" | ||
"github.com/prometheus/prometheus/promql/parser/posrange" | ||
"github.com/prometheus/prometheus/util/annotations" | ||
|
||
"github.com/thanos-io/promql-engine/query" | ||
) | ||
|
||
type Coalesce struct { | ||
Shards []parser.Expr | ||
} | ||
|
||
func (r Coalesce) String() string { | ||
parts := make([]string, len(r.Shards)) | ||
for i, r := range r.Shards { | ||
parts[i] = r.String() | ||
} | ||
return fmt.Sprintf("coalesce(%s)", strings.Join(parts, ", ")) | ||
} | ||
|
||
func (r Coalesce) Pretty(level int) string { return r.String() } | ||
|
||
func (r Coalesce) PositionRange() posrange.PositionRange { return posrange.PositionRange{} } | ||
|
||
func (r Coalesce) Type() parser.ValueType { return r.Shards[0].Type() } | ||
|
||
func (r Coalesce) PromQLExpr() {} | ||
|
||
type ShardedAggregations struct{ Shards int } | ||
|
||
func (m ShardedAggregations) Optimize(plan parser.Expr, _ *query.Options) (parser.Expr, annotations.Annotations) { | ||
TraverseBottomUp(nil, &plan, func(parent, current *parser.Expr) (stop bool) { | ||
if parent == nil { | ||
return false | ||
} | ||
aggr, ok := (*parent).(*parser.AggregateExpr) | ||
if !ok { | ||
return false | ||
} | ||
// TODO: only care about sum now | ||
if aggr.Op != parser.SUM { | ||
return false | ||
} | ||
call, ok := (*current).(*parser.Call) | ||
if !ok { | ||
return false | ||
} | ||
if len(call.Args) != 1 { | ||
return false | ||
} | ||
vs, ok := call.Args[0].(*parser.VectorSelector) | ||
if !ok { | ||
return false | ||
} | ||
|
||
coalesce := Coalesce{make([]parser.Expr, m.Shards)} | ||
for i := range coalesce.Shards { | ||
coalesce.Shards[i] = &parser.Call{ | ||
Func: call.Func, | ||
PosRange: call.PosRange, | ||
Args: []parser.Expr{vectorSelectorForShard(vs, i, m.Shards)}, | ||
} | ||
} | ||
|
||
*parent = &parser.AggregateExpr{ | ||
Op: aggr.Op, | ||
Expr: coalesce, | ||
Param: aggr.Param, | ||
Grouping: aggr.Grouping, | ||
Without: aggr.Without, | ||
PosRange: aggr.PosRange, | ||
} | ||
return true | ||
}) | ||
return plan, nil | ||
} | ||
|
||
func vectorSelectorForShard(expr *parser.VectorSelector, n, shards int) parser.Expr { | ||
return &VectorSelector{ | ||
VectorSelector: expr, | ||
N: n, | ||
Shards: shards, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright (c) The Thanos Community Authors. | ||
// Licensed under the Apache License 2.0. | ||
|
||
package logicalplan | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/efficientgo/core/testutil" | ||
"github.com/prometheus/prometheus/promql/parser" | ||
|
||
"github.com/thanos-io/promql-engine/query" | ||
) | ||
|
||
func TestShardedAggregations(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
expr string | ||
expected string | ||
}{ | ||
{ | ||
name: "sum exp", | ||
expr: `sum(exp(X))`, | ||
expected: ``, | ||
}, | ||
} | ||
|
||
optimizers := []Optimizer{ShardedAggregations{Shards: 2}} | ||
for _, tcase := range cases { | ||
t.Run(tcase.expr, func(t *testing.T) { | ||
expr, err := parser.ParseExpr(tcase.expr) | ||
testutil.Ok(t, err) | ||
|
||
plan := New(expr, &query.Options{}) | ||
optimizedPlan, _ := plan.Optimize(optimizers) | ||
testutil.Equals(t, tcase.expected, optimizedPlan.Expr().String()) | ||
}) | ||
} | ||
} |