-
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.
plan,execution: make coalesce a logical node
Signed-off-by: Michael Hoffmann <mhoffm@posteo.de>
- Loading branch information
1 parent
c38e829
commit 8de1865
Showing
5 changed files
with
153 additions
and
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package logicalplan | ||
|
||
import ( | ||
"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 { | ||
// We assume to always have at least one expression | ||
Exprs []parser.Expr | ||
} | ||
|
||
func (c Coalesce) String() string { | ||
return c.Exprs[0].String() | ||
} | ||
|
||
func (c Coalesce) Pretty(level int) string { return c.String() } | ||
|
||
func (c Coalesce) PositionRange() posrange.PositionRange { return c.Exprs[0].PositionRange() } | ||
|
||
func (c Coalesce) Type() parser.ValueType { return c.Exprs[0].Type() } | ||
|
||
func (c Coalesce) PromQLExpr() {} | ||
|
||
type CoalesceOptimizer struct{} | ||
|
||
func (c CoalesceOptimizer) Optimize(expr parser.Expr, opts *query.Options) (parser.Expr, annotations.Annotations) { | ||
numShards := opts.NumShards() | ||
|
||
TraverseBottomUp(nil, &expr, func(parent, e *parser.Expr) bool { | ||
switch t := (*e).(type) { | ||
case *VectorSelector: | ||
if parent != nil { | ||
// we coalesce matrix selectors in a different branch | ||
if _, ok := (*parent).(MatrixSelector); ok { | ||
return false | ||
} | ||
// timestamp/absent is a weird function and those workarounds are for it | ||
if _, ok := (*parent).(*parser.StepInvariantExpr); ok { | ||
return false | ||
} | ||
if c, ok := (*parent).(*parser.Call); ok { | ||
if c.Func.Name == "absent" || c.Func.Name == "timestamp" { | ||
return true | ||
} | ||
} | ||
} | ||
exprs := make([]parser.Expr, numShards) | ||
for i := 0; i < numShards; i++ { | ||
exprs[i] = &VectorSelector{ | ||
VectorSelector: t.VectorSelector, | ||
Filters: t.Filters, | ||
BatchSize: t.BatchSize, | ||
Shard: i, | ||
NumShards: numShards, | ||
} | ||
} | ||
*e = Coalesce{Exprs: exprs} | ||
return true | ||
case *parser.Call: | ||
var ( | ||
ms *MatrixSelector | ||
marg int | ||
) | ||
for i := range t.Args { | ||
if arg, ok := t.Args[i].(*MatrixSelector); ok { | ||
ms = arg | ||
marg = i | ||
} | ||
} | ||
if ms == nil { | ||
return false | ||
} | ||
|
||
exprs := make([]parser.Expr, numShards) | ||
for i := 0; i < numShards; i++ { | ||
aux := &MatrixSelector{ | ||
MatrixSelector: ms.MatrixSelector, | ||
OriginalString: ms.OriginalString, | ||
Shard: i, | ||
NumShards: numShards, | ||
} | ||
f := &parser.Call{ | ||
Func: t.Func, | ||
Args: t.Args, | ||
PosRange: t.PosRange, | ||
} | ||
f.Args[marg] = aux | ||
|
||
exprs[i] = f | ||
} | ||
*e = Coalesce{Exprs: exprs} | ||
default: | ||
return true | ||
} | ||
return true | ||
}) | ||
return expr, nil | ||
} |
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