Export sequenceValue

Signed-off-by: Tobias Guggenmos <tguggenm@redhat.com>
This commit is contained in:
Tobias Guggenmos 2020-02-03 17:45:41 +01:00
parent 228967a507
commit 6b1b323558
6 changed files with 55 additions and 55 deletions

View file

@ -411,8 +411,8 @@ type testStmt func(context.Context) error
func (testStmt) String() string { return "test statement" } func (testStmt) String() string { return "test statement" }
func (testStmt) stmt() {} func (testStmt) stmt() {}
func (testStmt) PositionRange() PositionRange { func (testStmt) PositionRange() parser.PositionRange {
return PositionRange{ return parser.PositionRange{
Start: -1, Start: -1,
End: -1, End: -1,
} }
@ -644,7 +644,7 @@ func (ng *Engine) populateSeries(ctx context.Context, q storage.Queryable, s *pa
if n.Offset+ng.lookbackDelta+subqOffset > maxOffset { if n.Offset+ng.lookbackDelta+subqOffset > maxOffset {
maxOffset = n.Offset + ng.lookbackDelta + subqOffset maxOffset = n.Offset + ng.lookbackDelta + subqOffset
} }
case *MatrixSelector: case *parser.MatrixSelector:
if maxOffset < n.Range+subqOffset { if maxOffset < n.Range+subqOffset {
maxOffset = n.Range + subqOffset maxOffset = n.Range + subqOffset
} }
@ -664,7 +664,7 @@ func (ng *Engine) populateSeries(ctx context.Context, q storage.Queryable, s *pa
var warnings storage.Warnings var warnings storage.Warnings
// Whenever a MatrixSelector is evaluated this variable is set to the corresponding range. // Whenever a parser.MatrixSelector is evaluated this variable is set to the corresponding range.
// The evaluation of the parser.VectorSelector inside then evaluates the given range and unsets // The evaluation of the parser.VectorSelector inside then evaluates the given range and unsets
// the variable. // the variable.
var evalRange time.Duration var evalRange time.Duration
@ -714,7 +714,7 @@ func (ng *Engine) populateSeries(ctx context.Context, q storage.Queryable, s *pa
} }
n.unexpandedSeriesSet = set n.unexpandedSeriesSet = set
case *MatrixSelector: case *parser.MatrixSelector:
evalRange = n.Range evalRange = n.Range
} }
return nil return nil
@ -755,7 +755,7 @@ func extractGroupsFromPath(p []parser.Node) (bool, []string) {
func checkForSeriesSetExpansion(ctx context.Context, expr parser.Expr) { func checkForSeriesSetExpansion(ctx context.Context, expr parser.Expr) {
switch e := expr.(type) { switch e := expr.(type) {
case *MatrixSelector: case *parser.MatrixSelector:
checkForSeriesSetExpansion(ctx, e.parser.VectorSelector) checkForSeriesSetExpansion(ctx, e.parser.VectorSelector)
case *parser.VectorSelector: case *parser.VectorSelector:
if e.series == nil { if e.series == nil {
@ -1012,14 +1012,14 @@ func (ev *evaluator) rangeEval(f func([]parser.Value, *EvalNodeHelper) Vector, e
} }
// evalSubquery evaluates given parser.SubqueryExpr and returns an equivalent // evalSubquery evaluates given parser.SubqueryExpr and returns an equivalent
// evaluated MatrixSelector in its place. Note that the Name and LabelMatchers are not set. // evaluated parser.MatrixSelector in its place. Note that the Name and LabelMatchers are not set.
func (ev *evaluator) evalSubquery(subq *parser.SubqueryExpr) *MatrixSelector { func (ev *evaluator) evalSubquery(subq *parser.SubqueryExpr) *parser.MatrixSelector {
val := ev.eval(subq).(Matrix) val := ev.eval(subq).(Matrix)
vs := &parser.VectorSelector{ vs := &parser.VectorSelector{
Offset: subq.Offset, Offset: subq.Offset,
series: make([]storage.Series, 0, len(val)), series: make([]storage.Series, 0, len(val)),
} }
ms := &MatrixSelector{ ms := &parser.MatrixSelector{
Range: subq.Range, Range: subq.Range,
parser.VectorSelector: vs, parser.VectorSelector: vs,
} }
@ -1073,16 +1073,16 @@ func (ev *evaluator) eval(expr parser.Expr) parser.Value {
for i := range e.Args { for i := range e.Args {
unwrapParenExpr(&e.Args[i]) unwrapParenExpr(&e.Args[i])
a := e.Args[i] a := e.Args[i]
if _, ok := a.(*MatrixSelector); ok { if _, ok := a.(*parser.MatrixSelector); ok {
matrixArgIndex = i matrixArgIndex = i
matrixArg = true matrixArg = true
break break
} }
// parser.SubqueryExpr can be used in place of MatrixSelector. // parser.SubqueryExpr can be used in place of parser.MatrixSelector.
if subq, ok := a.(*parser.SubqueryExpr); ok { if subq, ok := a.(*parser.SubqueryExpr); ok {
matrixArgIndex = i matrixArgIndex = i
matrixArg = true matrixArg = true
// Replacing parser.SubqueryExpr with MatrixSelector. // Replacing parser.SubqueryExpr with parser.MatrixSelector.
e.Args[i] = ev.evalSubquery(subq) e.Args[i] = ev.evalSubquery(subq)
break break
} }
@ -1106,7 +1106,7 @@ func (ev *evaluator) eval(expr parser.Expr) parser.Value {
} }
} }
sel := e.Args[matrixArgIndex].(*MatrixSelector) sel := e.Args[matrixArgIndex].(*parser.MatrixSelector)
selVS := sel.parser.VectorSelector.(*parser.VectorSelector) selVS := sel.parser.VectorSelector.(*parser.VectorSelector)
checkForSeriesSetExpansion(ev.ctx, sel) checkForSeriesSetExpansion(ev.ctx, sel)
@ -1250,19 +1250,19 @@ func (ev *evaluator) eval(expr parser.Expr) parser.Value {
switch e.Op { switch e.Op {
case LAND: case LAND:
return ev.rangeEval(func(v []parser.Value, enh *EvalNodeHelper) Vector { return ev.rangeEval(func(v []parser.Value, enh *EvalNodeHelper) Vector {
return ev.VectorAnd(v[0].(Vector), v[1].(Vector), e.VectorMatching, enh) return ev.VectorAnd(v[0].(Vector), v[1].(Vector), e.parser.VectorMatching, enh)
}, e.LHS, e.RHS) }, e.LHS, e.RHS)
case LOR: case LOR:
return ev.rangeEval(func(v []parser.Value, enh *EvalNodeHelper) Vector { return ev.rangeEval(func(v []parser.Value, enh *EvalNodeHelper) Vector {
return ev.VectorOr(v[0].(Vector), v[1].(Vector), e.VectorMatching, enh) return ev.VectorOr(v[0].(Vector), v[1].(Vector), e.parser.VectorMatching, enh)
}, e.LHS, e.RHS) }, e.LHS, e.RHS)
case LUNLESS: case LUNLESS:
return ev.rangeEval(func(v []parser.Value, enh *EvalNodeHelper) Vector { return ev.rangeEval(func(v []parser.Value, enh *EvalNodeHelper) Vector {
return ev.VectorUnless(v[0].(Vector), v[1].(Vector), e.VectorMatching, enh) return ev.VectorUnless(v[0].(Vector), v[1].(Vector), e.parser.VectorMatching, enh)
}, e.LHS, e.RHS) }, e.LHS, e.RHS)
default: default:
return ev.rangeEval(func(v []parser.Value, enh *EvalNodeHelper) Vector { return ev.rangeEval(func(v []parser.Value, enh *EvalNodeHelper) Vector {
return ev.VectorBinop(e.Op, v[0].(Vector), v[1].(Vector), e.VectorMatching, e.ReturnBool, enh) return ev.VectorBinop(e.Op, v[0].(Vector), v[1].(Vector), e.parser.VectorMatching, e.ReturnBool, enh)
}, e.LHS, e.RHS) }, e.LHS, e.RHS)
} }
@ -1314,7 +1314,7 @@ func (ev *evaluator) eval(expr parser.Expr) parser.Value {
} }
return mat return mat
case *MatrixSelector: case *parser.MatrixSelector:
if ev.startTimestamp != ev.endTimestamp { if ev.startTimestamp != ev.endTimestamp {
panic(errors.New("cannot do range evaluation of matrix selector")) panic(errors.New("cannot do range evaluation of matrix selector"))
} }
@ -1431,8 +1431,8 @@ func putPointSlice(p []Point) {
pointPool.Put(p[:0]) pointPool.Put(p[:0])
} }
// matrixSelector evaluates a *MatrixSelector expression. // matrixSelector evaluates a *parser.MatrixSelector expression.
func (ev *evaluator) matrixSelector(node *MatrixSelector) Matrix { func (ev *evaluator) matrixSelector(node *parser.MatrixSelector) Matrix {
checkForSeriesSetExpansion(ev.ctx, node) checkForSeriesSetExpansion(ev.ctx, node)
vs := node.parser.VectorSelector.(*parser.VectorSelector) vs := node.parser.VectorSelector.(*parser.VectorSelector)
@ -1529,7 +1529,7 @@ func (ev *evaluator) matrixIterSlice(it *storage.BufferedSeriesIterator, mint, m
return out return out
} }
func (ev *evaluator) VectorAnd(lhs, rhs Vector, matching *VectorMatching, enh *EvalNodeHelper) Vector { func (ev *evaluator) VectorAnd(lhs, rhs Vector, matching *parser.VectorMatching, enh *EvalNodeHelper) Vector {
if matching.Card != CardManyToMany { if matching.Card != CardManyToMany {
panic("set operations must only use many-to-many matching") panic("set operations must only use many-to-many matching")
} }
@ -1551,7 +1551,7 @@ func (ev *evaluator) VectorAnd(lhs, rhs Vector, matching *VectorMatching, enh *E
return enh.out return enh.out
} }
func (ev *evaluator) VectorOr(lhs, rhs Vector, matching *VectorMatching, enh *EvalNodeHelper) Vector { func (ev *evaluator) VectorOr(lhs, rhs Vector, matching *parser.VectorMatching, enh *EvalNodeHelper) Vector {
if matching.Card != CardManyToMany { if matching.Card != CardManyToMany {
panic("set operations must only use many-to-many matching") panic("set operations must only use many-to-many matching")
} }
@ -1572,7 +1572,7 @@ func (ev *evaluator) VectorOr(lhs, rhs Vector, matching *VectorMatching, enh *Ev
return enh.out return enh.out
} }
func (ev *evaluator) VectorUnless(lhs, rhs Vector, matching *VectorMatching, enh *EvalNodeHelper) Vector { func (ev *evaluator) VectorUnless(lhs, rhs Vector, matching *parser.VectorMatching, enh *EvalNodeHelper) Vector {
if matching.Card != CardManyToMany { if matching.Card != CardManyToMany {
panic("set operations must only use many-to-many matching") panic("set operations must only use many-to-many matching")
} }
@ -1592,7 +1592,7 @@ func (ev *evaluator) VectorUnless(lhs, rhs Vector, matching *VectorMatching, enh
} }
// VectorBinop evaluates a binary operation between two Vectors, excluding set operators. // VectorBinop evaluates a binary operation between two Vectors, excluding set operators.
func (ev *evaluator) VectorBinop(op ItemType, lhs, rhs Vector, matching *VectorMatching, returnBool bool, enh *EvalNodeHelper) Vector { func (ev *evaluator) VectorBinop(op parser.ItemType, lhs, rhs Vector, matching *parser.VectorMatching, returnBool bool, enh *EvalNodeHelper) Vector {
if matching.Card == CardManyToMany { if matching.Card == CardManyToMany {
panic("many-to-many only allowed for set operators") panic("many-to-many only allowed for set operators")
} }
@ -1719,7 +1719,7 @@ func signatureFunc(on bool, names ...string) func(labels.Labels) uint64 {
// resultMetric returns the metric for the given sample(s) based on the Vector // resultMetric returns the metric for the given sample(s) based on the Vector
// binary operation and the matching options. // binary operation and the matching options.
func resultMetric(lhs, rhs labels.Labels, op ItemType, matching *VectorMatching, enh *EvalNodeHelper) labels.Labels { func resultMetric(lhs, rhs labels.Labels, op parser.ItemType, matching *parser.VectorMatching, enh *EvalNodeHelper) labels.Labels {
if enh.resultMetric == nil { if enh.resultMetric == nil {
enh.resultMetric = make(map[uint64]labels.Labels, len(enh.out)) enh.resultMetric = make(map[uint64]labels.Labels, len(enh.out))
} }
@ -1769,7 +1769,7 @@ func resultMetric(lhs, rhs labels.Labels, op ItemType, matching *VectorMatching,
} }
// VectorscalarBinop evaluates a binary operation between a Vector and a Scalar. // VectorscalarBinop evaluates a binary operation between a Vector and a Scalar.
func (ev *evaluator) VectorscalarBinop(op ItemType, lhs Vector, rhs Scalar, swap, returnBool bool, enh *EvalNodeHelper) Vector { func (ev *evaluator) VectorscalarBinop(op parser.ItemType, lhs Vector, rhs Scalar, swap, returnBool bool, enh *EvalNodeHelper) Vector {
for _, lhsSample := range lhs { for _, lhsSample := range lhs {
lv, rv := lhsSample.V, rhs.V lv, rv := lhsSample.V, rhs.V
// lhs always contains the Vector. If the original position was different // lhs always contains the Vector. If the original position was different
@ -1807,7 +1807,7 @@ func dropMetricName(l labels.Labels) labels.Labels {
} }
// scalarBinop evaluates a binary operation between two Scalars. // scalarBinop evaluates a binary operation between two Scalars.
func scalarBinop(op ItemType, lhs, rhs float64) float64 { func scalarBinop(op parser.ItemType, lhs, rhs float64) float64 {
switch op { switch op {
case ADD: case ADD:
return lhs + rhs return lhs + rhs
@ -1838,7 +1838,7 @@ func scalarBinop(op ItemType, lhs, rhs float64) float64 {
} }
// vectorElemBinop evaluates a binary operation between two Vector elements. // vectorElemBinop evaluates a binary operation between two Vector elements.
func vectorElemBinop(op ItemType, lhs, rhs float64) (float64, bool) { func vectorElemBinop(op parser.ItemType, lhs, rhs float64) (float64, bool) {
switch op { switch op {
case ADD: case ADD:
return lhs + rhs, true return lhs + rhs, true
@ -1878,7 +1878,7 @@ type groupedAggregation struct {
} }
// aggregation evaluates an aggregation operation on a Vector. // aggregation evaluates an aggregation operation on a Vector.
func (ev *evaluator) aggregation(op ItemType, grouping []string, without bool, param interface{}, vec Vector, enh *EvalNodeHelper) Vector { func (ev *evaluator) aggregation(op parser.ItemType, grouping []string, without bool, param interface{}, vec Vector, enh *EvalNodeHelper) Vector {
result := map[uint64]*groupedAggregation{} result := map[uint64]*groupedAggregation{}
var k int64 var k int64
@ -2098,7 +2098,7 @@ func btos(b bool) float64 {
// shouldDropMetricName returns whether the metric name should be dropped in the // shouldDropMetricName returns whether the metric name should be dropped in the
// result of the op operation. // result of the op operation.
func shouldDropMetricName(op ItemType) bool { func shouldDropMetricName(op parser.ItemType) bool {
switch op { switch op {
case ADD, SUB, DIV, MUL, POW, MOD: case ADD, SUB, DIV, MUL, POW, MOD:
return true return true

View file

@ -64,7 +64,7 @@ func funcTime(vals []parser.Value, args parser.Expressions, enh *EvalNodeHelper)
// extrapolates if the first/last sample is close to the boundary, and returns // extrapolates if the first/last sample is close to the boundary, and returns
// the result as either per-second (if isRate is true) or overall. // the result as either per-second (if isRate is true) or overall.
func extrapolatedRate(vals []parser.Value, args parser.Expressions, enh *EvalNodeHelper, isCounter bool, isRate bool) Vector { func extrapolatedRate(vals []parser.Value, args parser.Expressions, enh *EvalNodeHelper, isCounter bool, isRate bool) Vector {
ms := args[0].(*MatrixSelector) ms := args[0].(*parser.MatrixSelector)
vs := ms.parser.VectorSelector.(*parser.VectorSelector) vs := ms.parser.VectorSelector.(*parser.VectorSelector)
var ( var (
@ -1244,7 +1244,7 @@ func createLabelsForAbsentFunction(expr parser.Expr) labels.Labels {
switch n := expr.(type) { switch n := expr.(type) {
case *parser.VectorSelector: case *parser.VectorSelector:
lm = n.LabelMatchers lm = n.LabelMatchers
case *MatrixSelector: case *parser.MatrixSelector:
lm = n.parser.VectorSelector.(*parser.VectorSelector).LabelMatchers lm = n.parser.VectorSelector.(*parser.VectorSelector).LabelMatchers
default: default:
return m return m

View file

@ -27,7 +27,7 @@ type yySymType struct {
label labels.Label label labels.Label
labels labels.Labels labels labels.Labels
strings []string strings []string
series []sequenceValue series []SequenceValue
uint uint64 uint uint64
float float64 float float64
duration time.Duration duration time.Duration
@ -1402,7 +1402,7 @@ yydefault:
yyDollar = yyS[yypt-0 : yypt+1] yyDollar = yyS[yypt-0 : yypt+1]
//line promql/generated_parser.y:580 //line promql/generated_parser.y:580
{ {
yyVAL.series = []sequenceValue{} yyVAL.series = []SequenceValue{}
} }
case 104: case 104:
yyDollar = yyS[yypt-3 : yypt+1] yyDollar = yyS[yypt-3 : yypt+1]
@ -1427,39 +1427,39 @@ yydefault:
yyDollar = yyS[yypt-1 : yypt+1] yyDollar = yyS[yypt-1 : yypt+1]
//line promql/generated_parser.y:590 //line promql/generated_parser.y:590
{ {
yyVAL.series = []sequenceValue{{omitted: true}} yyVAL.series = []SequenceValue{{omitted: true}}
} }
case 108: case 108:
yyDollar = yyS[yypt-3 : yypt+1] yyDollar = yyS[yypt-3 : yypt+1]
//line promql/generated_parser.y:592 //line promql/generated_parser.y:592
{ {
yyVAL.series = []sequenceValue{} yyVAL.series = []SequenceValue{}
for i := uint64(0); i < yyDollar[3].uint; i++ { for i := uint64(0); i < yyDollar[3].uint; i++ {
yyVAL.series = append(yyVAL.series, sequenceValue{omitted: true}) yyVAL.series = append(yyVAL.series, SequenceValue{omitted: true})
} }
} }
case 109: case 109:
yyDollar = yyS[yypt-1 : yypt+1] yyDollar = yyS[yypt-1 : yypt+1]
//line promql/generated_parser.y:599 //line promql/generated_parser.y:599
{ {
yyVAL.series = []sequenceValue{{value: yyDollar[1].float}} yyVAL.series = []SequenceValue{{value: yyDollar[1].float}}
} }
case 110: case 110:
yyDollar = yyS[yypt-3 : yypt+1] yyDollar = yyS[yypt-3 : yypt+1]
//line promql/generated_parser.y:601 //line promql/generated_parser.y:601
{ {
yyVAL.series = []sequenceValue{} yyVAL.series = []SequenceValue{}
for i := uint64(0); i <= yyDollar[3].uint; i++ { for i := uint64(0); i <= yyDollar[3].uint; i++ {
yyVAL.series = append(yyVAL.series, sequenceValue{value: yyDollar[1].float}) yyVAL.series = append(yyVAL.series, SequenceValue{value: yyDollar[1].float})
} }
} }
case 111: case 111:
yyDollar = yyS[yypt-4 : yypt+1] yyDollar = yyS[yypt-4 : yypt+1]
//line promql/generated_parser.y:608 //line promql/generated_parser.y:608
{ {
yyVAL.series = []sequenceValue{} yyVAL.series = []SequenceValue{}
for i := uint64(0); i <= yyDollar[4].uint; i++ { for i := uint64(0); i <= yyDollar[4].uint; i++ {
yyVAL.series = append(yyVAL.series, sequenceValue{value: yyDollar[1].float}) yyVAL.series = append(yyVAL.series, SequenceValue{value: yyDollar[1].float})
yyDollar[1].float += yyDollar[2].float yyDollar[1].float += yyDollar[2].float
} }
} }

View file

@ -178,13 +178,13 @@ func newParser(input string) *parser {
return p return p
} }
// sequenceValue is an omittable value in a sequence of time series values. // SequenceValue is an omittable value in a sequence of time series values.
type sequenceValue struct { type SequenceValue struct {
value float64 value float64
omitted bool omitted bool
} }
func (v sequenceValue) String() string { func (v SequenceValue) String() string {
if v.omitted { if v.omitted {
return "_" return "_"
} }
@ -193,11 +193,11 @@ func (v sequenceValue) String() string {
type seriesDescription struct { type seriesDescription struct {
labels labels.Labels labels labels.Labels
values []sequenceValue values []SequenceValue
} }
// parseSeriesDesc parses the description of a time series. // parseSeriesDesc parses the description of a time series.
func parseSeriesDesc(input string) (labels labels.Labels, values []sequenceValue, err error) { func parseSeriesDesc(input string) (labels labels.Labels, values []SequenceValue, err error) {
p := newParser(input) p := newParser(input)
p.lex.seriesDesc = true p.lex.seriesDesc = true

View file

@ -2525,7 +2525,7 @@ func mustGetFunction(name string) *Function {
var testSeries = []struct { var testSeries = []struct {
input string input string
expectedMetric labels.Labels expectedMetric labels.Labels
expectedValues []sequenceValue expectedValues []SequenceValue
fail bool fail bool
}{ }{
{ {
@ -2602,12 +2602,12 @@ var testSeries = []struct {
// For these tests only, we use the smallest float64 to signal an omitted value. // For these tests only, we use the smallest float64 to signal an omitted value.
const none = math.SmallestNonzeroFloat64 const none = math.SmallestNonzeroFloat64
func newSeq(vals ...float64) (res []sequenceValue) { func newSeq(vals ...float64) (res []SequenceValue) {
for _, v := range vals { for _, v := range vals {
if v == none { if v == none {
res = append(res, sequenceValue{omitted: true}) res = append(res, SequenceValue{omitted: true})
} else { } else {
res = append(res, sequenceValue{value: v}) res = append(res, SequenceValue{value: v})
} }
} }
return res return res

View file

@ -182,7 +182,7 @@ func (t *Test) parseEval(lines []string, i int) (int, *evalCmd, error) {
break break
} }
if f, err := parseNumber(defLine); err == nil { if f, err := parseNumber(defLine); err == nil {
cmd.expect(0, nil, sequenceValue{value: f}) cmd.expect(0, nil, parser.SequenceValue{value: f})
break break
} }
metric, vals, err := parseSeriesDesc(defLine) metric, vals, err := parseSeriesDesc(defLine)
@ -276,7 +276,7 @@ func (cmd loadCmd) String() string {
} }
// set a sequence of sample values for the given metric. // set a sequence of sample values for the given metric.
func (cmd *loadCmd) set(m labels.Labels, vals ...sequenceValue) { func (cmd *loadCmd) set(m labels.Labels, vals ...parser.SequenceValue) {
h := m.Hash() h := m.Hash()
samples := make([]Point, 0, len(vals)) samples := make([]Point, 0, len(vals))
@ -323,7 +323,7 @@ type evalCmd struct {
type entry struct { type entry struct {
pos int pos int
vals []sequenceValue vals []parser.SequenceValue
} }
func (e entry) String() string { func (e entry) String() string {
@ -347,7 +347,7 @@ func (ev *evalCmd) String() string {
// expect adds a new metric with a sequence of values to the set of expected // expect adds a new metric with a sequence of values to the set of expected
// results for the query. // results for the query.
func (ev *evalCmd) expect(pos int, m labels.Labels, vals ...sequenceValue) { func (ev *evalCmd) expect(pos int, m labels.Labels, vals ...parser.SequenceValue) {
if m == nil { if m == nil {
ev.expected[0] = entry{pos: pos, vals: vals} ev.expected[0] = entry{pos: pos, vals: vals}
return return