2015-03-30 09:12:51 -07:00
|
|
|
// Copyright 2015 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package promql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2016-12-24 05:01:10 -08:00
|
|
|
"github.com/prometheus/prometheus/pkg/labels"
|
2016-12-24 16:40:28 -08:00
|
|
|
"github.com/prometheus/prometheus/storage"
|
2015-03-30 09:12:51 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// Node is a generic interface for all nodes in an AST.
|
|
|
|
//
|
|
|
|
// Whenever numerous nodes are listed such as in a switch-case statement
|
|
|
|
// or a chain of function definitions (e.g. String(), expr(), etc.) convention is
|
|
|
|
// to list them as follows:
|
|
|
|
//
|
|
|
|
// - Statements
|
|
|
|
// - statement types (alphabetical)
|
|
|
|
// - ...
|
|
|
|
// - Expressions
|
|
|
|
// - expression types (alphabetical)
|
|
|
|
// - ...
|
|
|
|
//
|
|
|
|
type Node interface {
|
|
|
|
// String representation of the node that returns the given node when parsed
|
|
|
|
// as part of a valid query.
|
|
|
|
String() string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Statement is a generic interface for all statements.
|
|
|
|
type Statement interface {
|
|
|
|
Node
|
|
|
|
|
|
|
|
// stmt ensures that no other type accidentally implements the interface
|
|
|
|
stmt()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Statements is a list of statement nodes that implements Node.
|
|
|
|
type Statements []Statement
|
|
|
|
|
|
|
|
// AlertStmt represents an added alert rule.
|
|
|
|
type AlertStmt struct {
|
|
|
|
Name string
|
|
|
|
Expr Expr
|
|
|
|
Duration time.Duration
|
2016-12-23 04:51:59 -08:00
|
|
|
Labels labels.Labels
|
|
|
|
Annotations labels.Labels
|
2015-03-30 09:12:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// EvalStmt holds an expression and information on the range it should
|
|
|
|
// be evaluated on.
|
|
|
|
type EvalStmt struct {
|
|
|
|
Expr Expr // Expression to be evaluated.
|
|
|
|
|
|
|
|
// The time boundaries for the evaluation. If Start equals End an instant
|
|
|
|
// is evaluated.
|
2016-12-23 04:51:59 -08:00
|
|
|
Start, End time.Time
|
2015-03-30 09:12:51 -07:00
|
|
|
// Time between two evaluated instants for the range [Start:End].
|
|
|
|
Interval time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
// RecordStmt represents an added recording rule.
|
|
|
|
type RecordStmt struct {
|
|
|
|
Name string
|
|
|
|
Expr Expr
|
2016-12-23 04:51:59 -08:00
|
|
|
Labels labels.Labels
|
2015-03-30 09:12:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (*AlertStmt) stmt() {}
|
|
|
|
func (*EvalStmt) stmt() {}
|
|
|
|
func (*RecordStmt) stmt() {}
|
|
|
|
|
|
|
|
// Expr is a generic interface for all expression types.
|
|
|
|
type Expr interface {
|
|
|
|
Node
|
|
|
|
|
|
|
|
// Type returns the type the expression evaluates to. It does not perform
|
|
|
|
// in-depth checks as this is done at parsing-time.
|
2016-12-23 04:51:59 -08:00
|
|
|
Type() ValueType
|
2015-03-30 09:12:51 -07:00
|
|
|
// expr ensures that no other types accidentally implement the interface.
|
|
|
|
expr()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expressions is a list of expression nodes that implements Node.
|
|
|
|
type Expressions []Expr
|
|
|
|
|
2016-12-24 01:42:54 -08:00
|
|
|
// AggregateExpr represents an aggregation operation on a Vector.
|
2015-03-30 09:12:51 -07:00
|
|
|
type AggregateExpr struct {
|
2018-03-08 08:52:44 -08:00
|
|
|
Op ItemType // The used aggregation operation.
|
2017-10-05 05:19:52 -07:00
|
|
|
Expr Expr // The Vector expression over which is aggregated.
|
|
|
|
Param Expr // Parameter used by some aggregators.
|
|
|
|
Grouping []string // The labels by which to group the Vector.
|
|
|
|
Without bool // Whether to drop the given labels rather than keep them.
|
2015-03-30 09:12:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// BinaryExpr represents a binary expression between two child expressions.
|
|
|
|
type BinaryExpr struct {
|
2018-03-08 08:52:44 -08:00
|
|
|
Op ItemType // The operation of the expression.
|
2015-03-30 09:12:51 -07:00
|
|
|
LHS, RHS Expr // The operands on the respective sides of the operator.
|
|
|
|
|
2016-12-24 01:42:54 -08:00
|
|
|
// The matching behavior for the operation if both operands are Vectors.
|
2015-03-30 09:12:51 -07:00
|
|
|
// If they are not this field is nil.
|
|
|
|
VectorMatching *VectorMatching
|
2015-09-02 06:51:44 -07:00
|
|
|
|
|
|
|
// If a comparison operator, return 0/1 rather than filtering.
|
|
|
|
ReturnBool bool
|
2015-03-30 09:12:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Call represents a function call.
|
|
|
|
type Call struct {
|
|
|
|
Func *Function // The function that was called.
|
|
|
|
Args Expressions // Arguments used in the call.
|
|
|
|
}
|
|
|
|
|
2016-12-24 01:42:54 -08:00
|
|
|
// MatrixSelector represents a Matrix selection.
|
2015-03-30 09:12:51 -07:00
|
|
|
type MatrixSelector struct {
|
|
|
|
Name string
|
|
|
|
Range time.Duration
|
|
|
|
Offset time.Duration
|
2016-12-24 16:40:28 -08:00
|
|
|
LabelMatchers []*labels.Matcher
|
2015-03-30 09:12:51 -07:00
|
|
|
|
Optimise PromQL (#3966)
* Move range logic to 'eval'
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make aggregegate range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* PromQL is statically typed, so don't eval to find the type.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Extend rangewrapper to multiple exprs
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Start making function evaluation ranged
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make instant queries a special case of range queries
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Eliminate evalString
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Evaluate range vector functions one series at a time
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make unary operators range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make binops range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Pass time to range-aware functions.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make simple _over_time functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reduce allocs when working with matrix selectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add basic benchmark for range evaluation
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse objects for function arguments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Do dropmetricname and allocating output vector only once.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add range-aware support for range vector functions with params
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise holt_winters, cut cpu and allocs by ~25%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make rate&friends range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make more functions range aware. Document calling convention.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make date functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make simple math functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Convert more functions to be range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make more functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Specialcase timestamp() with vector selector arg for range awareness
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove transition code for functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove the rest of the engine transition code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove more obselete code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove the last uses of the eval* functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove engine finalizers to prevent corruption
The finalizers set by matrixSelector were being called
just before the value they were retruning to the pool
was then being provided to the caller. Thus a concurrent query
could corrupt the data that the user has just been returned.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add new benchmark suite for range functinos
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Migrate existing benchmarks to new system
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Expand promql benchmarks
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Simply test by removing unused range code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* When testing instant queries, check range queries too.
To protect against subsequent steps in a range query being
affected by the previous steps, add a test that evaluates
an instant query that we know works again as a range query
with the tiimestamp we care about not being the first step.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse ring for matrix iters. Put query results back in pool.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse buffer when iterating over matrix selectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Unary minus should remove metric name
Cut down benchmarks for faster runs.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reduce repetition in benchmark test cases
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Work series by series when doing normal vectorSelectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise benchmark setup, cuts time by 60%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Have rangeWrapper use an evalNodeHelper to cache across steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Use evalNodeHelper with functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Cache dropMetricName within a node evaluation.
This saves both the calculations and allocs done by dropMetricName
across steps.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse input vectors in rangewrapper
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse the point slices in the matrixes input/output by rangeWrapper
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make benchmark setup faster using AddFast
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Simplify benchmark code.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add caching in VectorBinop
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Use xor to have one-level resultMetric hash key
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add more benchmarks
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Call Query.Close in apiv1
This allows point slices allocated for the response data
to be reused by later queries, saving allocations.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise histogram_quantile
It's now 5-10% faster with 97% less garbage generated for 1k steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make the input collection in rangeVector linear rather than quadratic
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise label_replace, for 1k steps 15x fewer allocs and 3x faster
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise label_join, 1.8x faster and 11x less memory for 1k steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Expand benchmarks, cleanup comments, simplify numSteps logic.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address Fabian's comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Comments from Alin.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address jrv's comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove dead code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address Simon's comments.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Rename populateIterators, pre-init some sizes
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Handle case where function has non-matrix args first
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Split rangeWrapper out to rangeEval function, improve comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Cleanup and make things more consistent
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make EvalNodeHelper public
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Fabian's comments.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
2018-06-04 06:47:45 -07:00
|
|
|
// The series are populated at query preparation time.
|
|
|
|
series []storage.Series
|
2015-03-30 09:12:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// NumberLiteral represents a number.
|
|
|
|
type NumberLiteral struct {
|
2016-12-23 04:51:59 -08:00
|
|
|
Val float64
|
2015-03-30 09:12:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParenExpr wraps an expression so it cannot be disassembled as a consequence
|
2016-02-09 18:47:00 -08:00
|
|
|
// of operator precedence.
|
2015-03-30 09:12:51 -07:00
|
|
|
type ParenExpr struct {
|
|
|
|
Expr Expr
|
|
|
|
}
|
|
|
|
|
|
|
|
// StringLiteral represents a string.
|
|
|
|
type StringLiteral struct {
|
2015-03-30 10:13:36 -07:00
|
|
|
Val string
|
2015-03-30 09:12:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnaryExpr represents a unary operation on another expression.
|
2016-12-24 01:44:04 -08:00
|
|
|
// Currently unary operations are only supported for Scalars.
|
2015-03-30 09:12:51 -07:00
|
|
|
type UnaryExpr struct {
|
2018-03-08 08:52:44 -08:00
|
|
|
Op ItemType
|
2015-03-30 09:12:51 -07:00
|
|
|
Expr Expr
|
|
|
|
}
|
|
|
|
|
2016-12-24 01:42:54 -08:00
|
|
|
// VectorSelector represents a Vector selection.
|
2015-03-30 09:12:51 -07:00
|
|
|
type VectorSelector struct {
|
|
|
|
Name string
|
|
|
|
Offset time.Duration
|
2016-12-24 16:40:28 -08:00
|
|
|
LabelMatchers []*labels.Matcher
|
2015-03-30 09:12:51 -07:00
|
|
|
|
Optimise PromQL (#3966)
* Move range logic to 'eval'
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make aggregegate range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* PromQL is statically typed, so don't eval to find the type.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Extend rangewrapper to multiple exprs
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Start making function evaluation ranged
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make instant queries a special case of range queries
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Eliminate evalString
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Evaluate range vector functions one series at a time
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make unary operators range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make binops range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Pass time to range-aware functions.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make simple _over_time functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reduce allocs when working with matrix selectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add basic benchmark for range evaluation
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse objects for function arguments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Do dropmetricname and allocating output vector only once.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add range-aware support for range vector functions with params
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise holt_winters, cut cpu and allocs by ~25%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make rate&friends range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make more functions range aware. Document calling convention.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make date functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make simple math functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Convert more functions to be range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make more functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Specialcase timestamp() with vector selector arg for range awareness
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove transition code for functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove the rest of the engine transition code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove more obselete code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove the last uses of the eval* functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove engine finalizers to prevent corruption
The finalizers set by matrixSelector were being called
just before the value they were retruning to the pool
was then being provided to the caller. Thus a concurrent query
could corrupt the data that the user has just been returned.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add new benchmark suite for range functinos
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Migrate existing benchmarks to new system
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Expand promql benchmarks
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Simply test by removing unused range code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* When testing instant queries, check range queries too.
To protect against subsequent steps in a range query being
affected by the previous steps, add a test that evaluates
an instant query that we know works again as a range query
with the tiimestamp we care about not being the first step.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse ring for matrix iters. Put query results back in pool.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse buffer when iterating over matrix selectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Unary minus should remove metric name
Cut down benchmarks for faster runs.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reduce repetition in benchmark test cases
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Work series by series when doing normal vectorSelectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise benchmark setup, cuts time by 60%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Have rangeWrapper use an evalNodeHelper to cache across steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Use evalNodeHelper with functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Cache dropMetricName within a node evaluation.
This saves both the calculations and allocs done by dropMetricName
across steps.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse input vectors in rangewrapper
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse the point slices in the matrixes input/output by rangeWrapper
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make benchmark setup faster using AddFast
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Simplify benchmark code.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add caching in VectorBinop
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Use xor to have one-level resultMetric hash key
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add more benchmarks
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Call Query.Close in apiv1
This allows point slices allocated for the response data
to be reused by later queries, saving allocations.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise histogram_quantile
It's now 5-10% faster with 97% less garbage generated for 1k steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make the input collection in rangeVector linear rather than quadratic
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise label_replace, for 1k steps 15x fewer allocs and 3x faster
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise label_join, 1.8x faster and 11x less memory for 1k steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Expand benchmarks, cleanup comments, simplify numSteps logic.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address Fabian's comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Comments from Alin.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address jrv's comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove dead code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address Simon's comments.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Rename populateIterators, pre-init some sizes
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Handle case where function has non-matrix args first
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Split rangeWrapper out to rangeEval function, improve comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Cleanup and make things more consistent
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make EvalNodeHelper public
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Fabian's comments.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
2018-06-04 06:47:45 -07:00
|
|
|
// The series are populated at query preparation time.
|
|
|
|
series []storage.Series
|
2015-03-30 09:12:51 -07:00
|
|
|
}
|
|
|
|
|
2016-12-23 04:51:59 -08:00
|
|
|
func (e *AggregateExpr) Type() ValueType { return ValueTypeVector }
|
|
|
|
func (e *Call) Type() ValueType { return e.Func.ReturnType }
|
|
|
|
func (e *MatrixSelector) Type() ValueType { return ValueTypeMatrix }
|
|
|
|
func (e *NumberLiteral) Type() ValueType { return ValueTypeScalar }
|
|
|
|
func (e *ParenExpr) Type() ValueType { return e.Expr.Type() }
|
|
|
|
func (e *StringLiteral) Type() ValueType { return ValueTypeString }
|
|
|
|
func (e *UnaryExpr) Type() ValueType { return e.Expr.Type() }
|
|
|
|
func (e *VectorSelector) Type() ValueType { return ValueTypeVector }
|
|
|
|
func (e *BinaryExpr) Type() ValueType {
|
|
|
|
if e.LHS.Type() == ValueTypeScalar && e.RHS.Type() == ValueTypeScalar {
|
|
|
|
return ValueTypeScalar
|
2015-03-30 09:12:51 -07:00
|
|
|
}
|
2016-12-28 00:16:48 -08:00
|
|
|
return ValueTypeVector
|
2015-03-30 09:12:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (*AggregateExpr) expr() {}
|
|
|
|
func (*BinaryExpr) expr() {}
|
|
|
|
func (*Call) expr() {}
|
|
|
|
func (*MatrixSelector) expr() {}
|
|
|
|
func (*NumberLiteral) expr() {}
|
|
|
|
func (*ParenExpr) expr() {}
|
|
|
|
func (*StringLiteral) expr() {}
|
|
|
|
func (*UnaryExpr) expr() {}
|
|
|
|
func (*VectorSelector) expr() {}
|
|
|
|
|
2015-08-24 06:07:27 -07:00
|
|
|
// VectorMatchCardinality describes the cardinality relationship
|
2016-12-24 01:42:54 -08:00
|
|
|
// of two Vectors in a binary operation.
|
2015-03-30 09:12:51 -07:00
|
|
|
type VectorMatchCardinality int
|
|
|
|
|
|
|
|
const (
|
|
|
|
CardOneToOne VectorMatchCardinality = iota
|
|
|
|
CardManyToOne
|
|
|
|
CardOneToMany
|
|
|
|
CardManyToMany
|
|
|
|
)
|
|
|
|
|
|
|
|
func (vmc VectorMatchCardinality) String() string {
|
|
|
|
switch vmc {
|
|
|
|
case CardOneToOne:
|
|
|
|
return "one-to-one"
|
|
|
|
case CardManyToOne:
|
|
|
|
return "many-to-one"
|
|
|
|
case CardOneToMany:
|
|
|
|
return "one-to-many"
|
|
|
|
case CardManyToMany:
|
|
|
|
return "many-to-many"
|
|
|
|
}
|
|
|
|
panic("promql.VectorMatchCardinality.String: unknown match cardinality")
|
|
|
|
}
|
|
|
|
|
2016-12-24 01:42:54 -08:00
|
|
|
// VectorMatching describes how elements from two Vectors in a binary
|
2015-03-30 09:12:51 -07:00
|
|
|
// operation are supposed to be matched.
|
|
|
|
type VectorMatching struct {
|
2016-12-24 01:42:54 -08:00
|
|
|
// The cardinality of the two Vectors.
|
2015-03-30 09:12:51 -07:00
|
|
|
Card VectorMatchCardinality
|
2016-04-26 06:28:36 -07:00
|
|
|
// MatchingLabels contains the labels which define equality of a pair of
|
2016-12-24 01:42:54 -08:00
|
|
|
// elements from the Vectors.
|
2016-12-23 04:51:59 -08:00
|
|
|
MatchingLabels []string
|
2016-06-23 09:23:44 -07:00
|
|
|
// On includes the given label names from matching,
|
|
|
|
// rather than excluding them.
|
|
|
|
On bool
|
2015-03-30 09:12:51 -07:00
|
|
|
// Include contains additional labels that should be included in
|
2016-04-26 06:31:00 -07:00
|
|
|
// the result from the side with the lower cardinality.
|
2016-12-23 04:51:59 -08:00
|
|
|
Include []string
|
2015-03-30 09:12:51 -07:00
|
|
|
}
|
|
|
|
|
2015-08-24 06:07:27 -07:00
|
|
|
// Visitor allows visiting a Node and its child nodes. The Visit method is
|
2018-01-09 08:44:23 -08:00
|
|
|
// invoked for each node with the path leading to the node provided additionally.
|
2018-06-07 09:27:34 -07:00
|
|
|
// If the result visitor w is not nil and no error, Walk visits each of the children
|
2018-01-09 08:44:23 -08:00
|
|
|
// of node with the visitor w, followed by a call of w.Visit(nil, nil).
|
2015-03-30 09:12:51 -07:00
|
|
|
type Visitor interface {
|
2018-06-07 09:27:34 -07:00
|
|
|
Visit(node Node, path []Node) (w Visitor, err error)
|
2015-03-30 09:12:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Walk traverses an AST in depth-first order: It starts by calling
|
2018-01-09 08:44:23 -08:00
|
|
|
// v.Visit(node, path); node must not be nil. If the visitor w returned by
|
2018-06-07 09:27:34 -07:00
|
|
|
// v.Visit(node, path) is not nil and the visitor returns no error, Walk is
|
|
|
|
// invoked recursively with visitor w for each of the non-nil children of node,
|
|
|
|
// followed by a call of w.Visit(nil), returning an error
|
2018-01-09 08:44:23 -08:00
|
|
|
// As the tree is descended the path of previous nodes is provided.
|
2018-06-07 09:27:34 -07:00
|
|
|
func Walk(v Visitor, node Node, path []Node) error {
|
|
|
|
var err error
|
|
|
|
if v, err = v.Visit(node, path); v == nil || err != nil {
|
|
|
|
return err
|
2015-03-30 09:12:51 -07:00
|
|
|
}
|
2018-01-09 08:44:23 -08:00
|
|
|
path = append(path, node)
|
2015-03-30 09:12:51 -07:00
|
|
|
|
|
|
|
switch n := node.(type) {
|
|
|
|
case Statements:
|
|
|
|
for _, s := range n {
|
2018-06-07 09:27:34 -07:00
|
|
|
if err := Walk(v, s, path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-30 09:12:51 -07:00
|
|
|
}
|
|
|
|
case *AlertStmt:
|
2018-06-07 09:27:34 -07:00
|
|
|
if err := Walk(v, n.Expr, path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-30 09:12:51 -07:00
|
|
|
|
|
|
|
case *EvalStmt:
|
2018-06-07 09:27:34 -07:00
|
|
|
if err := Walk(v, n.Expr, path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-30 09:12:51 -07:00
|
|
|
|
|
|
|
case *RecordStmt:
|
2018-06-07 09:27:34 -07:00
|
|
|
if err := Walk(v, n.Expr, path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-30 09:12:51 -07:00
|
|
|
|
|
|
|
case Expressions:
|
|
|
|
for _, e := range n {
|
2018-06-07 09:27:34 -07:00
|
|
|
if err := Walk(v, e, path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-30 09:12:51 -07:00
|
|
|
}
|
|
|
|
case *AggregateExpr:
|
2018-06-07 09:27:34 -07:00
|
|
|
if err := Walk(v, n.Expr, path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-30 09:12:51 -07:00
|
|
|
|
|
|
|
case *BinaryExpr:
|
2018-06-07 09:27:34 -07:00
|
|
|
if err := Walk(v, n.LHS, path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := Walk(v, n.RHS, path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-30 09:12:51 -07:00
|
|
|
|
|
|
|
case *Call:
|
2018-06-07 09:27:34 -07:00
|
|
|
if err := Walk(v, n.Args, path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-30 09:12:51 -07:00
|
|
|
|
|
|
|
case *ParenExpr:
|
2018-06-07 09:27:34 -07:00
|
|
|
if err := Walk(v, n.Expr, path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-30 09:12:51 -07:00
|
|
|
|
|
|
|
case *UnaryExpr:
|
2018-06-07 09:27:34 -07:00
|
|
|
if err := Walk(v, n.Expr, path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-30 09:12:51 -07:00
|
|
|
|
|
|
|
case *MatrixSelector, *NumberLiteral, *StringLiteral, *VectorSelector:
|
|
|
|
// nothing to do
|
|
|
|
|
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("promql.Walk: unhandled node type %T", node))
|
|
|
|
}
|
|
|
|
|
2018-06-07 09:27:34 -07:00
|
|
|
_, err = v.Visit(nil, nil)
|
|
|
|
return err
|
2015-03-30 09:12:51 -07:00
|
|
|
}
|
|
|
|
|
2018-06-07 09:27:34 -07:00
|
|
|
type inspector func(Node, []Node) error
|
2015-03-30 09:12:51 -07:00
|
|
|
|
2018-06-07 09:27:34 -07:00
|
|
|
func (f inspector) Visit(node Node, path []Node) (Visitor, error) {
|
|
|
|
if err := f(node, path); err == nil {
|
|
|
|
return f, nil
|
|
|
|
} else {
|
|
|
|
return nil, err
|
2015-03-30 09:12:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inspect traverses an AST in depth-first order: It starts by calling
|
2018-06-07 09:27:34 -07:00
|
|
|
// f(node, path); node must not be nil. If f returns a nil error, Inspect invokes f
|
2015-03-30 09:12:51 -07:00
|
|
|
// for all the non-nil children of node, recursively.
|
2018-06-07 09:27:34 -07:00
|
|
|
func Inspect(node Node, f inspector) {
|
2018-01-09 08:44:23 -08:00
|
|
|
Walk(inspector(f), node, nil)
|
2015-03-30 09:12:51 -07:00
|
|
|
}
|