diff --git a/promql/engine.go b/promql/engine.go index ea4bc1af85..7db23674f3 100644 --- a/promql/engine.go +++ b/promql/engine.go @@ -1442,9 +1442,7 @@ func (ev *evaluator) eval(expr parser.Expr) (parser.Value, annotations.Annotatio sortedGrouping := e.Grouping slices.Sort(sortedGrouping) - unwrapParenExpr(&e.Param) - param := unwrapStepInvariantExpr(e.Param) - unwrapParenExpr(¶m) + param := unwrapStepInvariantAndParenExprs(&e.Param) if e.Op == parser.COUNT_VALUES { valueLabel := param.(*parser.StringLiteral) @@ -1487,9 +1485,7 @@ func (ev *evaluator) eval(expr parser.Expr) (parser.Value, annotations.Annotatio // Matrix evaluation always returns the evaluation time, // so this function needs special handling when given // a vector selector. - unwrapParenExpr(&e.Args[0]) - arg := unwrapStepInvariantExpr(e.Args[0]) - unwrapParenExpr(&arg) + arg := unwrapStepInvariantAndParenExprs(&e.Args[0]) vs, ok := arg.(*parser.VectorSelector) if ok { return ev.rangeEvalTimestampFunctionOverVectorSelector(vs, call, e) @@ -1503,9 +1499,7 @@ func (ev *evaluator) eval(expr parser.Expr) (parser.Value, annotations.Annotatio warnings annotations.Annotations ) for i := range e.Args { - unwrapParenExpr(&e.Args[i]) - a := unwrapStepInvariantExpr(e.Args[i]) - unwrapParenExpr(&a) + a := unwrapStepInvariantAndParenExprs(&e.Args[i]) if _, ok := a.(*parser.MatrixSelector); ok { matrixArgIndex = i matrixArg = true @@ -1558,9 +1552,7 @@ func (ev *evaluator) eval(expr parser.Expr) (parser.Value, annotations.Annotatio } } - unwrapParenExpr(&e.Args[matrixArgIndex]) - arg := unwrapStepInvariantExpr(e.Args[matrixArgIndex]) - unwrapParenExpr(&arg) + arg := unwrapStepInvariantAndParenExprs(&e.Args[matrixArgIndex]) sel := arg.(*parser.MatrixSelector) selVS := sel.VectorSelector.(*parser.VectorSelector) @@ -3169,11 +3161,13 @@ func unwrapParenExpr(e *parser.Expr) { } } -func unwrapStepInvariantExpr(e parser.Expr) parser.Expr { - if p, ok := e.(*parser.StepInvariantExpr); ok { +func unwrapStepInvariantAndParenExprs(e *parser.Expr) parser.Expr { + unwrapParenExpr(e) + if p, ok := (*e).(*parser.StepInvariantExpr); ok { + unwrapParenExpr(&p.Expr) return p.Expr } - return e + return *e } // PreprocessExpr wraps all possible step invariant parts of the given expression with diff --git a/promql/functions.go b/promql/functions.go index 9b3be22874..85acf751e5 100644 --- a/promql/functions.go +++ b/promql/functions.go @@ -1698,8 +1698,7 @@ func createLabelsForAbsentFunction(expr parser.Expr) labels.Labels { } func stringFromArg(e parser.Expr) string { - tmp := unwrapStepInvariantExpr(e) // Unwrap StepInvariant - unwrapParenExpr(&tmp) // Optionally unwrap ParenExpr + tmp := unwrapStepInvariantAndParenExprs(&e) // Unwrap StepInvariant return tmp.(*parser.StringLiteral).Val }