Fix more identifiers

Signed-off-by: Tobias Guggenmos <tguggenm@redhat.com>
This commit is contained in:
Tobias Guggenmos 2020-02-03 18:00:41 +01:00
parent 6b1b323558
commit ff0ea1c1ac
8 changed files with 62 additions and 62 deletions

View file

@ -252,7 +252,7 @@ func BenchmarkParser(b *testing.B) {
b.Run(c, func(b *testing.B) { b.Run(c, func(b *testing.B) {
b.ReportAllocs() b.ReportAllocs()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
ParseExpr(c) parser.ParseExpr(c)
} }
}) })
} }
@ -261,7 +261,7 @@ func BenchmarkParser(b *testing.B) {
b.Run(name, func(b *testing.B) { b.Run(name, func(b *testing.B) {
b.ReportAllocs() b.ReportAllocs()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
ParseExpr(c) parser.ParseExpr(c)
} }
}) })
} }

View file

@ -189,7 +189,7 @@ func (q *query) Exec(ctx context.Context) *Result {
// Exec query. // Exec query.
res, warnings, err := q.ng.exec(ctx, q) res, warnings, err := q.ng.exec(ctx, q)
return &Result{Err: err, parser.Value: res, Warnings: warnings} return &Result{Err: err, Value: res, Warnings: warnings}
} }
// contextDone returns an error if the context was canceled or timed out. // contextDone returns an error if the context was canceled or timed out.
@ -362,7 +362,7 @@ func (ng *Engine) SetQueryLogger(l QueryLogger) {
// NewInstantQuery returns an evaluation query for the given expression at the given time. // NewInstantQuery returns an evaluation query for the given expression at the given time.
func (ng *Engine) NewInstantQuery(q storage.Queryable, qs string, ts time.Time) (Query, error) { func (ng *Engine) NewInstantQuery(q storage.Queryable, qs string, ts time.Time) (Query, error) {
expr, err := ParseExpr(qs) expr, err := parser.ParseExpr(qs)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -375,12 +375,12 @@ func (ng *Engine) NewInstantQuery(q storage.Queryable, qs string, ts time.Time)
// NewRangeQuery returns an evaluation query for the given time range and with // NewRangeQuery returns an evaluation query for the given time range and with
// the resolution set by the interval. // the resolution set by the interval.
func (ng *Engine) NewRangeQuery(q storage.Queryable, qs string, start, end time.Time, interval time.Duration) (Query, error) { func (ng *Engine) NewRangeQuery(q storage.Queryable, qs string, start, end time.Time, interval time.Duration) (Query, error) {
expr, err := ParseExpr(qs) expr, err := parser.ParseExpr(qs)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if expr.Type() != parser.ValueTypeVector && expr.Type() != parser.ValueTypeScalar { if expr.Type() != parser.ValueTypeVector && expr.Type() != parser.ValueTypeScalar {
return nil, errors.Errorf("invalid expression type %q for range query, must be Scalar or instant Vector", documentedType(expr.Type())) return nil, errors.Errorf("invalid expression type %q for range query, must be Scalar or instant Vector", parser.DocumentedType(expr.Type()))
} }
qry := ng.newQuery(q, expr, start, end, interval) qry := ng.newQuery(q, expr, start, end, interval)
qry.q = qs qry.q = qs
@ -390,7 +390,7 @@ func (ng *Engine) NewRangeQuery(q storage.Queryable, qs string, start, end time.
func (ng *Engine) newQuery(q storage.Queryable, expr parser.Expr, start, end time.Time, interval time.Duration) *query { func (ng *Engine) newQuery(q storage.Queryable, expr parser.Expr, start, end time.Time, interval time.Duration) *query {
es := &parser.EvalStmt{ es := &parser.EvalStmt{
parser.Expr: expr, Expr: expr,
Start: start, Start: start,
End: end, End: end,
Interval: interval, Interval: interval,
@ -404,24 +404,10 @@ func (ng *Engine) newQuery(q storage.Queryable, expr parser.Expr, start, end tim
return qry return qry
} }
// testStmt is an internal helper statement that allows execution
// of an arbitrary function during handling. It is used to test the Engine.
type testStmt func(context.Context) error
func (testStmt) String() string { return "test statement" }
func (testStmt) stmt() {}
func (testStmt) PositionRange() parser.PositionRange {
return parser.PositionRange{
Start: -1,
End: -1,
}
}
func (ng *Engine) newTestQuery(f func(context.Context) error) Query { func (ng *Engine) newTestQuery(f func(context.Context) error) Query {
qry := &query{ qry := &query{
q: "test statement", q: "test statement",
stmt: testStmt(f), stmt: parser.TestStmt(f),
ng: ng, ng: ng,
stats: stats.NewQueryTimers(), stats: stats.NewQueryTimers(),
} }
@ -444,7 +430,7 @@ func (ng *Engine) exec(ctx context.Context, q *query) (v parser.Value, w storage
if l := ng.queryLogger; l != nil { if l := ng.queryLogger; l != nil {
params := make(map[string]interface{}, 4) params := make(map[string]interface{}, 4)
params["query"] = q.q params["query"] = q.q
if eq, ok := q.parser.Statement().(*parser.EvalStmt); ok { if eq, ok := q.Statement().(*parser.EvalStmt); ok {
params["start"] = formatDate(eq.Start) params["start"] = formatDate(eq.Start)
params["end"] = formatDate(eq.End) params["end"] = formatDate(eq.End)
// The step provided by the user is in seconds. // The step provided by the user is in seconds.
@ -455,7 +441,7 @@ func (ng *Engine) exec(ctx context.Context, q *query) (v parser.Value, w storage
f = append(f, "error", err) f = append(f, "error", err)
} }
f = append(f, "stats", stats.NewQueryStats(q.Stats())) f = append(f, "stats", stats.NewQueryStats(q.Stats()))
if origin := ctx.parser.Value(queryOrigin); origin != nil { if origin := ctx.Value(queryOrigin); origin != nil {
for k, v := range origin.(map[string]interface{}) { for k, v := range origin.(map[string]interface{}) {
f = append(f, k, v) f = append(f, k, v)
} }
@ -497,14 +483,14 @@ func (ng *Engine) exec(ctx context.Context, q *query) (v parser.Value, w storage
return nil, nil, err return nil, nil, err
} }
switch s := q.parser.Statement().(type) { switch s := q.Statement().(type) {
case *parser.EvalStmt: case *parser.EvalStmt:
return ng.execEvalStmt(ctx, q, s) return ng.execEvalStmt(ctx, q, s)
case testStmt: case parser.TestStmt:
return nil, nil, s(ctx) return nil, nil, s(ctx)
} }
panic(errors.Errorf("promql.Engine.exec: unhandled statement of type %T", q.parser.Statement())) panic(errors.Errorf("promql.Engine.exec: unhandled statement of type %T", q.Statement()))
} }
func timeMilliseconds(t time.Time) int64 { func timeMilliseconds(t time.Time) int64 {
@ -547,7 +533,7 @@ func (ng *Engine) execEvalStmt(ctx context.Context, query *query, s *parser.Eval
lookbackDelta: ng.lookbackDelta, lookbackDelta: ng.lookbackDelta,
} }
val, err := evaluator.Eval(s.parser.Expr) val, err := evaluator.Eval(s.Expr)
if err != nil { if err != nil {
return nil, warnings, err return nil, warnings, err
} }
@ -566,7 +552,7 @@ func (ng *Engine) execEvalStmt(ctx context.Context, query *query, s *parser.Eval
} }
query.matrix = mat query.matrix = mat
switch s.parser.Expr.Type() { switch s.Expr.Type() {
case parser.ValueTypeVector: case parser.ValueTypeVector:
// Convert matrix with one value per series into vector. // Convert matrix with one value per series into vector.
vector := make(Vector, len(mat)) vector := make(Vector, len(mat))
@ -581,7 +567,7 @@ func (ng *Engine) execEvalStmt(ctx context.Context, query *query, s *parser.Eval
case parser.ValueTypeMatrix: case parser.ValueTypeMatrix:
return mat, warnings, nil return mat, warnings, nil
default: default:
panic(errors.Errorf("promql.Engine.exec: unexpected expression type %q", s.parser.Expr.Type())) panic(errors.Errorf("promql.Engine.exec: unexpected expression type %q", s.Expr.Type()))
} }
} }
@ -596,7 +582,7 @@ func (ng *Engine) execEvalStmt(ctx context.Context, query *query, s *parser.Eval
logger: ng.logger, logger: ng.logger,
lookbackDelta: ng.lookbackDelta, lookbackDelta: ng.lookbackDelta,
} }
val, err := evaluator.Eval(s.parser.Expr) val, err := evaluator.Eval(s.Expr)
if err != nil { if err != nil {
return nil, warnings, err return nil, warnings, err
} }
@ -634,7 +620,7 @@ func (ng *Engine) cumulativeSubqueryOffset(path []parser.Node) time.Duration {
func (ng *Engine) populateSeries(ctx context.Context, q storage.Queryable, s *parser.EvalStmt) (storage.Querier, storage.Warnings, error) { func (ng *Engine) populateSeries(ctx context.Context, q storage.Queryable, s *parser.EvalStmt) (storage.Querier, storage.Warnings, error) {
var maxOffset time.Duration var maxOffset time.Duration
Inspect(s.parser.Expr, func(node parser.Node, path []parser.Node) error { parser.Inspect(s.Expr, func(node parser.Node, path []parser.Node) error {
subqOffset := ng.cumulativeSubqueryOffset(path) subqOffset := ng.cumulativeSubqueryOffset(path)
switch n := node.(type) { switch n := node.(type) {
case *parser.VectorSelector: case *parser.VectorSelector:
@ -648,7 +634,7 @@ func (ng *Engine) populateSeries(ctx context.Context, q storage.Queryable, s *pa
if maxOffset < n.Range+subqOffset { if maxOffset < n.Range+subqOffset {
maxOffset = n.Range + subqOffset maxOffset = n.Range + subqOffset
} }
if m := n.parser.VectorSelector.(*parser.VectorSelector).Offset + n.Range + subqOffset; m > maxOffset { if m := n.VectorSelector.(*parser.VectorSelector).Offset + n.Range + subqOffset; m > maxOffset {
maxOffset = m maxOffset = m
} }
} }
@ -669,7 +655,7 @@ func (ng *Engine) populateSeries(ctx context.Context, q storage.Queryable, s *pa
// the variable. // the variable.
var evalRange time.Duration var evalRange time.Duration
Inspect(s.parser.Expr, func(node parser.Node, path []parser.Node) error { parser.Inspect(s.Expr, func(node parser.Node, path []parser.Node) error {
var set storage.SeriesSet var set storage.SeriesSet
var wrn storage.Warnings var wrn storage.Warnings
params := &storage.SelectParams{ params := &storage.SelectParams{
@ -729,11 +715,11 @@ func extractFuncFromPath(p []parser.Node) string {
return "" return ""
} }
switch n := p[len(p)-1].(type) { switch n := p[len(p)-1].(type) {
case *AggregateExpr: case *parser.AggregateExpr:
return n.Op.String() return n.Op.String()
case *Call: case *parser.Call:
return n.Func.Name return n.Func.Name
case *BinaryExpr: case *parser.BinaryExpr:
// If we hit a binary expression we terminate since we only care about functions // If we hit a binary expression we terminate since we only care about functions
// or aggregations over a single metric. // or aggregations over a single metric.
return "" return ""
@ -747,7 +733,7 @@ func extractGroupsFromPath(p []parser.Node) (bool, []string) {
return false, nil return false, nil
} }
switch n := p[len(p)-1].(type) { switch n := p[len(p)-1].(type) {
case *AggregateExpr: case *parser.AggregateExpr:
return !n.Without, n.Grouping return !n.Without, n.Grouping
} }
return false, nil return false, nil
@ -756,7 +742,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 *parser.MatrixSelector: case *parser.MatrixSelector:
checkForSeriesSetExpansion(ctx, e.parser.VectorSelector) checkForSeriesSetExpansion(ctx, e.VectorSelector)
case *parser.VectorSelector: case *parser.VectorSelector:
if e.series == nil { if e.series == nil {
series, err := expandSeriesSet(ctx, e.unexpandedSeriesSet) series, err := expandSeriesSet(ctx, e.unexpandedSeriesSet)

View file

@ -614,7 +614,7 @@ func funcHistogramQuantile(vals []parser.Value, args parser.Expressions, enh *Ev
} }
} }
for _, el := range inVec { for _, el := range inVec {
upperBound, err := strconv.ParseFloat( upperBound, err := strconv.parser.ParseFloat(
el.Metric.Get(model.BucketLabel), 64, el.Metric.Get(model.BucketLabel), 64,
) )
if err != nil { if err != nil {

View file

@ -32,9 +32,9 @@ import (
// //
// And then run the tests with the appropriate inputs // And then run the tests with the appropriate inputs
// //
// go-fuzz -bin FuzzParseMetric.zip -workdir fuzz-data/ParseMetric // go-fuzz -bin FuzzParseMetric.zip -workdir fuzz-data/parser.ParseMetric
// //
// Further input samples should go in the folders fuzz-data/ParseMetric/corpus. // Further input samples should go in the folders fuzz-data/parser.ParseMetric/corpus.
// //
// Repeat for FuzzParseOpenMetric, FuzzParseMetricSelector and FuzzParseExpr. // Repeat for FuzzParseOpenMetric, FuzzParseMetricSelector and FuzzParseExpr.
@ -83,7 +83,7 @@ func FuzzParseOpenMetric(in []byte) int {
// Fuzz the metric selector parser. // Fuzz the metric selector parser.
func FuzzParseMetricSelector(in []byte) int { func FuzzParseMetricSelector(in []byte) int {
_, err := ParseMetricSelector(string(in)) _, err := parser.ParseMetricSelector(string(in))
if err == nil { if err == nil {
return fuzzInteresting return fuzzInteresting
} }
@ -93,7 +93,7 @@ func FuzzParseMetricSelector(in []byte) int {
// Fuzz the expression parser. // Fuzz the expression parser.
func FuzzParseExpr(in []byte) int { func FuzzParseExpr(in []byte) int {
_, err := ParseExpr(string(in)) _, err := parser.ParseExpr(string(in))
if err == nil { if err == nil {
return fuzzInteresting return fuzzInteresting
} }

View file

@ -14,6 +14,7 @@
package parser package parser
import ( import (
"context"
"time" "time"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -173,6 +174,19 @@ type VectorSelector struct {
PosRange PositionRange PosRange PositionRange
} }
// TestStmt is an internal helper statement that allows execution
// of an arbitrary function during handling. It is used to test the Engine.
type TestStmt func(context.Context) error
func (TestStmt) String() string { return "test statement" }
func (TestStmt) stmt() {}
func (TestStmt) PositionRange() PositionRange {
return PositionRange{
Start: -1,
End: -1,
}
}
func (e *AggregateExpr) Type() ValueType { return ValueTypeVector } func (e *AggregateExpr) Type() ValueType { return ValueTypeVector }
func (e *Call) Type() ValueType { return e.Func.ReturnType } func (e *Call) Type() ValueType { return e.Func.ReturnType }
func (e *MatrixSelector) Type() ValueType { return ValueTypeMatrix } func (e *MatrixSelector) Type() ValueType { return ValueTypeMatrix }

View file

@ -416,7 +416,7 @@ func (p *parser) number(val string) float64 {
func (p *parser) expectType(node Node, want ValueType, context string) { func (p *parser) expectType(node Node, want ValueType, context string) {
t := p.checkAST(node) t := p.checkAST(node)
if t != want { if t != want {
p.addParseErrf(node.PositionRange(), "expected type %s in %s, got %s", documentedType(want), context, documentedType(t)) p.addParseErrf(node.PositionRange(), "expected type %s in %s, got %s", DocumentedType(want), context, DocumentedType(t))
} }
} }
@ -439,14 +439,14 @@ func (p *parser) checkAST(node Node) (typ ValueType) {
case *EvalStmt: case *EvalStmt:
ty := p.checkAST(n.Expr) ty := p.checkAST(n.Expr)
if ty == ValueTypeNone { if ty == ValueTypeNone {
p.addParseErrf(n.Expr.PositionRange(), "evaluation statement must have a valid expression type but got %s", documentedType(ty)) p.addParseErrf(n.Expr.PositionRange(), "evaluation statement must have a valid expression type but got %s", DocumentedType(ty))
} }
case Expressions: case Expressions:
for _, e := range n { for _, e := range n {
ty := p.checkAST(e) ty := p.checkAST(e)
if ty == ValueTypeNone { if ty == ValueTypeNone {
p.addParseErrf(e.PositionRange(), "expression must have a valid expression type but got %s", documentedType(ty)) p.addParseErrf(e.PositionRange(), "expression must have a valid expression type but got %s", DocumentedType(ty))
} }
} }
case *AggregateExpr: case *AggregateExpr:
@ -557,7 +557,7 @@ func (p *parser) checkAST(node Node) (typ ValueType) {
p.addParseErrf(n.PositionRange(), "only + and - operators allowed for unary expressions") p.addParseErrf(n.PositionRange(), "only + and - operators allowed for unary expressions")
} }
if t := p.checkAST(n.Expr); t != ValueTypeScalar && t != ValueTypeVector { if t := p.checkAST(n.Expr); t != ValueTypeScalar && t != ValueTypeVector {
p.addParseErrf(n.PositionRange(), "unary expression only allowed on expressions of type scalar or instant vector, got %q", documentedType(t)) p.addParseErrf(n.PositionRange(), "unary expression only allowed on expressions of type scalar or instant vector, got %q", DocumentedType(t))
} }
case *SubqueryExpr: case *SubqueryExpr:

View file

@ -31,9 +31,9 @@ const (
ValueTypeString = "string" ValueTypeString = "string"
) )
// documentedType returns the internal type to the equivalent // DocumentedType returns the internal type to the equivalent
// user facing terminology as defined in the documentation. // user facing terminology as defined in the documentation.
func documentedType(t ValueType) string { func DocumentedType(t ValueType) string {
switch t { switch t {
case "vector": case "vector":
return "instant vector" return "instant vector"

View file

@ -102,7 +102,7 @@ func (t *Test) Storage() storage.Storage {
} }
func raise(line int, format string, v ...interface{}) error { func raise(line int, format string, v ...interface{}) error {
return &ParseErr{ return &parser.ParseErr{
lineOffset: line, lineOffset: line,
Err: errors.Errorf(format, v...), Err: errors.Errorf(format, v...),
} }
@ -114,7 +114,7 @@ func parseLoad(lines []string, i int) (int, *loadCmd, error) {
} }
parts := patLoad.FindStringSubmatch(lines[i]) parts := patLoad.FindStringSubmatch(lines[i])
gap, err := model.ParseDuration(parts[1]) gap, err := model.parser.ParseDuration(parts[1])
if err != nil { if err != nil {
return i, nil, raise(i, "invalid step definition %q: %s", parts[1], err) return i, nil, raise(i, "invalid step definition %q: %s", parts[1], err)
} }
@ -128,7 +128,7 @@ func parseLoad(lines []string, i int) (int, *loadCmd, error) {
} }
metric, vals, err := parseSeriesDesc(defLine) metric, vals, err := parseSeriesDesc(defLine)
if err != nil { if err != nil {
if perr, ok := err.(*ParseErr); ok { if perr, ok := err.(*parser.ParseErr); ok {
perr.lineOffset = i perr.lineOffset = i
} }
return i, nil, err return i, nil, err
@ -148,9 +148,9 @@ func (t *Test) parseEval(lines []string, i int) (int, *evalCmd, error) {
at = parts[2] at = parts[2]
expr = parts[3] expr = parts[3]
) )
_, err := ParseExpr(expr) _, err := parser.ParseExpr(expr)
if err != nil { if err != nil {
if perr, ok := err.(*ParseErr); ok { if perr, ok := err.(*parser.ParseErr); ok {
perr.lineOffset = i perr.lineOffset = i
posOffset := Pos(strings.Index(lines[i], expr)) posOffset := Pos(strings.Index(lines[i], expr))
perr.PositionRange.Start += posOffset perr.PositionRange.Start += posOffset
@ -160,7 +160,7 @@ func (t *Test) parseEval(lines []string, i int) (int, *evalCmd, error) {
return i, nil, err return i, nil, err
} }
offset, err := model.ParseDuration(at) offset, err := model.parser.ParseDuration(at)
if err != nil { if err != nil {
return i, nil, raise(i, "invalid step definition %q: %s", parts[1], err) return i, nil, raise(i, "invalid step definition %q: %s", parts[1], err)
} }
@ -187,7 +187,7 @@ func (t *Test) parseEval(lines []string, i int) (int, *evalCmd, error) {
} }
metric, vals, err := parseSeriesDesc(defLine) metric, vals, err := parseSeriesDesc(defLine)
if err != nil { if err != nil {
if perr, ok := err.(*ParseErr); ok { if perr, ok := err.(*parser.ParseErr); ok {
perr.lineOffset = i perr.lineOffset = i
} }
return i, nil, err return i, nil, err
@ -559,10 +559,10 @@ func almostEqual(a, b float64) bool {
} }
func parseNumber(s string) (float64, error) { func parseNumber(s string) (float64, error) {
n, err := strconv.ParseInt(s, 0, 64) n, err := strconv.parser.ParseInt(s, 0, 64)
f := float64(n) f := float64(n)
if err != nil { if err != nil {
f, err = strconv.ParseFloat(s, 64) f, err = strconv.parser.ParseFloat(s, 64)
} }
if err != nil { if err != nil {
return 0, errors.Wrap(err, "error parsing number") return 0, errors.Wrap(err, "error parsing number")