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

plan, execution: use only internal vector selector #329

Closed
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 0 additions & 9 deletions execution/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,6 @@ func newOperator(expr parser.Expr, storage *engstore.SelectorPool, opts *query.O
case *parser.NumberLiteral:
return scan.NewNumberLiteralSelector(model.NewVectorPool(opts.StepsBatch), opts, e.Val), nil

case *parser.VectorSelector:
start, end := getTimeRangesForVectorSelector(e, opts, 0)
hints.Start = start
hints.End = end
filter := storage.GetSelector(start, end, opts.Step.Milliseconds(), e.LabelMatchers, hints)
return newShardedVectorSelector(filter, opts, e.Offset, 0)

case *logicalplan.VectorSelector:
start, end := getTimeRangesForVectorSelector(e.VectorSelector, opts, 0)
hints.Start = start
Expand Down Expand Up @@ -209,8 +202,6 @@ func newOperator(expr parser.Expr, storage *engstore.SelectorPool, opts *query.O

func unpackVectorSelector(t *parser.MatrixSelector) (int64, *parser.VectorSelector, []*labels.Matcher, error) {
switch t := t.VectorSelector.(type) {
case *parser.VectorSelector:
return 0, t, nil, nil
case *logicalplan.VectorSelector:
return t.BatchSize, t.VectorSelector, t.Filters, nil
default:
Expand Down
6 changes: 3 additions & 3 deletions execution/function/absent.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
"time"

"github.com/prometheus/prometheus/model/labels"

"github.com/prometheus/prometheus/promql/parser"

"github.com/thanos-io/promql-engine/execution/model"
"github.com/thanos-io/promql-engine/logicalplan"
)

type absentOperator struct {
Expand Down Expand Up @@ -49,10 +49,10 @@ func (o *absentOperator) loadSeries() {
// https://github.com/prometheus/prometheus/blob/main/promql/functions.go#L1385
var lm []*labels.Matcher
switch n := o.funcExpr.Args[0].(type) {
case *parser.VectorSelector:
case *logicalplan.VectorSelector:
lm = n.LabelMatchers
case *parser.MatrixSelector:
lm = n.VectorSelector.(*parser.VectorSelector).LabelMatchers
lm = n.VectorSelector.(*logicalplan.VectorSelector).LabelMatchers
default:
o.series = []labels.Labels{labels.EmptyLabels()}
return
Expand Down
4 changes: 2 additions & 2 deletions logicalplan/distribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ func calculateStartOffset(expr *parser.Expr, lookbackDelta time.Duration) time.D
if matrixSelector, ok := (*node).(*parser.MatrixSelector); ok {
selectRange = matrixSelector.Range
}
if vectorSelector, ok := (*node).(*parser.VectorSelector); ok {
if vectorSelector, ok := (*node).(*VectorSelector); ok {
offset = vectorSelector.Offset
}
})
Expand Down Expand Up @@ -415,7 +415,7 @@ func matchesExternalLabelSet(expr parser.Expr, externalLabelSet []labels.Labels)
}
var selectorSet [][]*labels.Matcher
traverse(&expr, func(current *parser.Expr) {
vs, ok := (*current).(*parser.VectorSelector)
vs, ok := (*current).(*VectorSelector)
if ok {
selectorSet = append(selectorSet, vs.LabelMatchers)
}
Expand Down
6 changes: 3 additions & 3 deletions logicalplan/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
)

// VectorSelector is vector selector with additional configuration set by optimizers.
// TODO(fpetkovski): Consider replacing all VectorSelector nodes with this one as the first step in the plan.
// This should help us avoid dealing with both types in the entire codebase.
type VectorSelector struct {
*parser.VectorSelector
Filters []*labels.Matcher
Expand All @@ -33,7 +31,9 @@ func (f VectorSelector) String() string {

func (f VectorSelector) Pretty(level int) string { return f.String() }

func (f VectorSelector) PositionRange() posrange.PositionRange { return posrange.PositionRange{} }
func (f VectorSelector) PositionRange() posrange.PositionRange {
return f.VectorSelector.PositionRange()
}

func (f VectorSelector) Type() parser.ValueType { return parser.ValueTypeVector }

Expand Down
10 changes: 1 addition & 9 deletions logicalplan/merge_selects.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (m MergeSelectsOptimizer) Optimize(expr parser.Expr, _ *query.Options) pars

func extractSelectors(selectors matcherHeap, expr parser.Expr) {
traverse(&expr, func(node *parser.Expr) {
e, ok := (*node).(*parser.VectorSelector)
e, ok := (*node).(*VectorSelector)
if !ok {
return
}
Expand All @@ -48,8 +48,6 @@ func replaceMatchers(selectors matcherHeap, expr *parser.Expr) {
traverse(expr, func(node *parser.Expr) {
var matchers []*labels.Matcher
switch e := (*node).(type) {
case *parser.VectorSelector:
matchers = e.LabelMatchers
case *VectorSelector:
matchers = e.LabelMatchers
default:
Expand Down Expand Up @@ -84,12 +82,6 @@ func replaceMatchers(selectors matcherHeap, expr *parser.Expr) {
}

switch e := (*node).(type) {
case *parser.VectorSelector:
e.LabelMatchers = replacement
*node = &VectorSelector{
VectorSelector: e,
Filters: filters,
}
case *VectorSelector:
e.LabelMatchers = replacement
e.Filters = filters
Expand Down
2 changes: 1 addition & 1 deletion logicalplan/passthrough.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (m PassthroughOptimizer) Optimize(plan parser.Expr, opts *query.Options) pa

matchingLabelsEngines := make([]api.RemoteEngine, 0, len(engines))
TraverseBottomUp(nil, &plan, func(parent, current *parser.Expr) (stop bool) {
if vs, ok := (*current).(*parser.VectorSelector); ok {
if vs, ok := (*current).(*VectorSelector); ok {
for _, e := range engines {
if !labelSetsMatch(vs.LabelMatchers, e.LabelSets()...) {
continue
Expand Down
17 changes: 12 additions & 5 deletions logicalplan/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ func New(expr parser.Expr, opts *query.Options) Plan {
setOffsetForAtModifier(opts.Start.UnixMilli(), expr)
setOffsetForInnerSubqueries(expr, opts)

// replace all vector selectors by our internal ones so we only need to handle them
traverse(&expr, func(e *parser.Expr) {
switch node := (*e).(type) {
case *parser.VectorSelector:
*e = &VectorSelector{VectorSelector: node}
}
})

return &plan{
expr: expr,
opts: opts,
Expand All @@ -67,19 +75,20 @@ func (p *plan) Expr() parser.Expr {
func traverse(expr *parser.Expr, transform func(*parser.Expr)) {
switch node := (*expr).(type) {
case *parser.StepInvariantExpr:
transform(&node.Expr)
case *parser.VectorSelector:
transform(expr)
traverse(&node.Expr, transform)
case *VectorSelector:
var x parser.Expr = node.VectorSelector
transform(expr)
traverse(&x, transform)
case *parser.VectorSelector:
transform(expr)
case *parser.MatrixSelector:
transform(expr)
traverse(&node.VectorSelector, transform)
case *parser.AggregateExpr:
transform(expr)
traverse(&node.Expr, transform)
traverse(&node.Param, transform)
case *parser.Call:
for i := range node.Args {
traverse(&(node.Args[i]), transform)
Expand All @@ -103,8 +112,6 @@ func TraverseBottomUp(parent *parser.Expr, current *parser.Expr, transform func(
return false
case *parser.StepInvariantExpr:
return TraverseBottomUp(current, &node.Expr, transform)
case *parser.VectorSelector:
return transform(parent, current)
case *VectorSelector:
if stop := transform(parent, current); stop {
return stop
Expand Down
6 changes: 3 additions & 3 deletions logicalplan/propagate_selectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ func (m PropagateMatchersOptimizer) Optimize(expr parser.Expr, _ *query.Options)
}

func propagateMatchers(binOp *parser.BinaryExpr) {
lhSelector, ok := binOp.LHS.(*parser.VectorSelector)
lhSelector, ok := binOp.LHS.(*VectorSelector)
if !ok {
return
}
rhSelector, ok := binOp.RHS.(*parser.VectorSelector)
rhSelector, ok := binOp.RHS.(*VectorSelector)
if !ok {
return
}
Expand Down Expand Up @@ -105,7 +105,7 @@ func makeUnion(lhMatchers map[string]*labels.Matcher, rhMatchers map[string]*lab
return union, false
}

func toMatcherMap(lhSelector *parser.VectorSelector) map[string]*labels.Matcher {
func toMatcherMap(lhSelector *VectorSelector) map[string]*labels.Matcher {
lhMatchers := make(map[string]*labels.Matcher)
for _, m := range lhSelector.LabelMatchers {
lhMatchers[m.Name] = m
Expand Down
5 changes: 0 additions & 5 deletions logicalplan/set_batch_size.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ func (m SelectorBatchSize) Optimize(expr parser.Expr, _ *query.Options) parser.E
e.BatchSize = m.Size
}
canBatch = false
case *parser.VectorSelector:
if canBatch {
*current = &VectorSelector{VectorSelector: e, BatchSize: m.Size}
}
canBatch = false
}
})
return expr
Expand Down
2 changes: 1 addition & 1 deletion logicalplan/sort_matchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type SortMatchers struct{}

func (m SortMatchers) Optimize(expr parser.Expr, _ *query.Options) parser.Expr {
traverse(&expr, func(node *parser.Expr) {
e, ok := (*node).(*parser.VectorSelector)
e, ok := (*node).(*VectorSelector)
if !ok {
return
}
Expand Down
Loading