mirror of
https://github.com/prometheus/prometheus
synced 2025-01-01 03:52:22 +00:00
promql: refactor: create query object before parsing
Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
This commit is contained in:
parent
1f3821379c
commit
71fc4f1516
@ -409,15 +409,15 @@ 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(_ context.Context, q storage.Queryable, opts *QueryOpts, qs string, ts time.Time) (Query, error) {
|
func (ng *Engine) NewInstantQuery(_ context.Context, q storage.Queryable, opts *QueryOpts, qs string, ts time.Time) (Query, error) {
|
||||||
|
pExpr, qry := ng.newQuery(q, qs, opts, ts, ts, 0)
|
||||||
expr, err := parser.ParseExpr(qs)
|
expr, err := parser.ParseExpr(qs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
qry, err := ng.newQuery(q, opts, expr, ts, ts, 0)
|
if err := ng.validateOpts(expr); err != nil {
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
qry.q = qs
|
*pExpr = PreprocessExpr(expr, ts, ts)
|
||||||
|
|
||||||
return qry, nil
|
return qry, nil
|
||||||
}
|
}
|
||||||
@ -425,27 +425,23 @@ func (ng *Engine) NewInstantQuery(_ context.Context, q storage.Queryable, opts *
|
|||||||
// 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(_ context.Context, q storage.Queryable, opts *QueryOpts, qs string, start, end time.Time, interval time.Duration) (Query, error) {
|
func (ng *Engine) NewRangeQuery(_ context.Context, q storage.Queryable, opts *QueryOpts, qs string, start, end time.Time, interval time.Duration) (Query, error) {
|
||||||
|
pExpr, qry := ng.newQuery(q, qs, opts, start, end, interval)
|
||||||
expr, err := parser.ParseExpr(qs)
|
expr, err := parser.ParseExpr(qs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if err := ng.validateOpts(expr); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
if expr.Type() != parser.ValueTypeVector && expr.Type() != parser.ValueTypeScalar {
|
if expr.Type() != parser.ValueTypeVector && expr.Type() != parser.ValueTypeScalar {
|
||||||
return nil, fmt.Errorf("invalid expression type %q for range query, must be Scalar or instant Vector", parser.DocumentedType(expr.Type()))
|
return nil, fmt.Errorf("invalid expression type %q for range query, must be Scalar or instant Vector", parser.DocumentedType(expr.Type()))
|
||||||
}
|
}
|
||||||
qry, err := ng.newQuery(q, opts, expr, start, end, interval)
|
*pExpr = PreprocessExpr(expr, start, end)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
qry.q = qs
|
|
||||||
|
|
||||||
return qry, nil
|
return qry, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ng *Engine) newQuery(q storage.Queryable, opts *QueryOpts, expr parser.Expr, start, end time.Time, interval time.Duration) (*query, error) {
|
func (ng *Engine) newQuery(q storage.Queryable, qs string, opts *QueryOpts, start, end time.Time, interval time.Duration) (*parser.Expr, *query) {
|
||||||
if err := ng.validateOpts(expr); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Default to empty QueryOpts if not provided.
|
// Default to empty QueryOpts if not provided.
|
||||||
if opts == nil {
|
if opts == nil {
|
||||||
opts = &QueryOpts{}
|
opts = &QueryOpts{}
|
||||||
@ -457,20 +453,20 @@ func (ng *Engine) newQuery(q storage.Queryable, opts *QueryOpts, expr parser.Exp
|
|||||||
}
|
}
|
||||||
|
|
||||||
es := &parser.EvalStmt{
|
es := &parser.EvalStmt{
|
||||||
Expr: PreprocessExpr(expr, start, end),
|
|
||||||
Start: start,
|
Start: start,
|
||||||
End: end,
|
End: end,
|
||||||
Interval: interval,
|
Interval: interval,
|
||||||
LookbackDelta: lookbackDelta,
|
LookbackDelta: lookbackDelta,
|
||||||
}
|
}
|
||||||
qry := &query{
|
qry := &query{
|
||||||
|
q: qs,
|
||||||
stmt: es,
|
stmt: es,
|
||||||
ng: ng,
|
ng: ng,
|
||||||
stats: stats.NewQueryTimers(),
|
stats: stats.NewQueryTimers(),
|
||||||
sampleStats: stats.NewQuerySamples(ng.enablePerStepStats && opts.EnablePerStepStats),
|
sampleStats: stats.NewQuerySamples(ng.enablePerStepStats && opts.EnablePerStepStats),
|
||||||
queryable: q,
|
queryable: q,
|
||||||
}
|
}
|
||||||
return qry, nil
|
return &es.Expr, qry
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
Loading…
Reference in New Issue
Block a user