From 28097c9476badab01e6dacad7558c3da306a7cc2 Mon Sep 17 00:00:00 2001 From: Charles Korn Date: Wed, 28 Jun 2023 15:08:48 +1000 Subject: [PATCH] Extract timestamp special case to its own method. Signed-off-by: Charles Korn --- promql/engine.go | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/promql/engine.go b/promql/engine.go index 8a64fdf394..d798a75e34 100644 --- a/promql/engine.go +++ b/promql/engine.go @@ -1353,15 +1353,7 @@ func (ev *evaluator) eval(expr parser.Expr) (parser.Value, storage.Warnings) { unwrapParenExpr(&arg) vs, ok := arg.(*parser.VectorSelector) if ok { - return ev.rangeEval(nil, func(v []parser.Value, _ [][]EvalSeriesHelper, enh *EvalNodeHelper) (Vector, storage.Warnings) { - if vs.Timestamp != nil { - // This is a special case only for "timestamp" since the offset - // needs to be adjusted for every point. - vs.Offset = time.Duration(enh.Ts-*vs.Timestamp) * time.Millisecond - } - val, ws := ev.vectorSelector(vs, enh.Ts) - return call([]parser.Value{val}, e.Args, enh), ws - }) + return ev.evalTimestampFunctionOverVectorSelector(vs, call, e) } } @@ -1799,6 +1791,18 @@ func (ev *evaluator) eval(expr parser.Expr) (parser.Value, storage.Warnings) { panic(fmt.Errorf("unhandled expression of type: %T", expr)) } +func (ev *evaluator) evalTimestampFunctionOverVectorSelector(vs *parser.VectorSelector, call FunctionCall, e *parser.Call) (parser.Value, storage.Warnings) { + return ev.rangeEval(nil, func(v []parser.Value, _ [][]EvalSeriesHelper, enh *EvalNodeHelper) (Vector, storage.Warnings) { + if vs.Timestamp != nil { + // This is a special case only for "timestamp" since the offset + // needs to be adjusted for every point. + vs.Offset = time.Duration(enh.Ts-*vs.Timestamp) * time.Millisecond + } + val, ws := ev.vectorSelector(vs, enh.Ts) + return call([]parser.Value{val}, e.Args, enh), ws + }) +} + // vectorSelector evaluates a *parser.VectorSelector expression. func (ev *evaluator) vectorSelector(node *parser.VectorSelector, ts int64) (Vector, storage.Warnings) { ws, err := checkAndExpandSeriesSet(ev.ctx, node)