|
| 1 | +// Copyright 2025 The Prometheus Authors |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package promql |
| 15 | + |
| 16 | +import ( |
| 17 | + "fmt" |
| 18 | + "math" |
| 19 | + "time" |
| 20 | + |
| 21 | + "github.com/prometheus/prometheus/promql/parser" |
| 22 | +) |
| 23 | + |
| 24 | +// durationVisitor is a visitor that visits a duration expression and calculates the duration. |
| 25 | +type durationVisitor struct{} |
| 26 | + |
| 27 | +func (v *durationVisitor) Visit(node parser.Node, _ []parser.Node) (parser.Visitor, error) { |
| 28 | + switch n := node.(type) { |
| 29 | + case *parser.VectorSelector: |
| 30 | + if n.OriginalOffsetExpr != nil { |
| 31 | + duration, err := calculateDuration(n.OriginalOffsetExpr, true) |
| 32 | + if err != nil { |
| 33 | + return nil, err |
| 34 | + } |
| 35 | + n.OriginalOffset = duration |
| 36 | + } |
| 37 | + case *parser.MatrixSelector: |
| 38 | + if n.RangeExpr != nil { |
| 39 | + duration, err := calculateDuration(n.RangeExpr, false) |
| 40 | + if err != nil { |
| 41 | + return nil, err |
| 42 | + } |
| 43 | + n.Range = duration |
| 44 | + } |
| 45 | + case *parser.SubqueryExpr: |
| 46 | + if n.OriginalOffsetExpr != nil { |
| 47 | + duration, err := calculateDuration(n.OriginalOffsetExpr, true) |
| 48 | + if err != nil { |
| 49 | + return nil, err |
| 50 | + } |
| 51 | + n.OriginalOffset = duration |
| 52 | + } |
| 53 | + if n.StepExpr != nil { |
| 54 | + duration, err := calculateDuration(n.StepExpr, false) |
| 55 | + if err != nil { |
| 56 | + return nil, err |
| 57 | + } |
| 58 | + n.Step = duration |
| 59 | + } |
| 60 | + if n.RangeExpr != nil { |
| 61 | + duration, err := calculateDuration(n.RangeExpr, false) |
| 62 | + if err != nil { |
| 63 | + return nil, err |
| 64 | + } |
| 65 | + n.Range = duration |
| 66 | + } |
| 67 | + } |
| 68 | + return v, nil |
| 69 | +} |
| 70 | + |
| 71 | +// calculateDuration computes the duration from a duration expression. |
| 72 | +func calculateDuration(expr parser.Expr, allowedNegative bool) (time.Duration, error) { |
| 73 | + duration, err := evaluateDurationExpr(expr) |
| 74 | + if err != nil { |
| 75 | + return 0, err |
| 76 | + } |
| 77 | + if duration <= 0 && !allowedNegative { |
| 78 | + return 0, fmt.Errorf("%d:%d: duration must be greater than 0", expr.PositionRange().Start, expr.PositionRange().End) |
| 79 | + } |
| 80 | + if duration > 1<<63-1 || duration < -1<<63 { |
| 81 | + return 0, fmt.Errorf("%d:%d: duration is out of range", expr.PositionRange().Start, expr.PositionRange().End) |
| 82 | + } |
| 83 | + return time.Duration(duration*1000) * time.Millisecond, nil |
| 84 | +} |
| 85 | + |
| 86 | +// evaluateDurationExpr recursively evaluates a duration expression to a float64 value. |
| 87 | +func evaluateDurationExpr(expr parser.Expr) (float64, error) { |
| 88 | + switch n := expr.(type) { |
| 89 | + case *parser.NumberLiteral: |
| 90 | + return n.Val, nil |
| 91 | + case *parser.DurationExpr: |
| 92 | + var lhs, rhs float64 |
| 93 | + var err error |
| 94 | + |
| 95 | + if n.LHS != nil { |
| 96 | + lhs, err = evaluateDurationExpr(n.LHS) |
| 97 | + if err != nil { |
| 98 | + return 0, err |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + rhs, err = evaluateDurationExpr(n.RHS) |
| 103 | + if err != nil { |
| 104 | + return 0, err |
| 105 | + } |
| 106 | + |
| 107 | + switch n.Op { |
| 108 | + case parser.ADD: |
| 109 | + return lhs + rhs, nil |
| 110 | + case parser.SUB: |
| 111 | + if n.LHS == nil { |
| 112 | + // Unary negative duration expression. |
| 113 | + return -rhs, nil |
| 114 | + } |
| 115 | + return lhs - rhs, nil |
| 116 | + case parser.MUL: |
| 117 | + return lhs * rhs, nil |
| 118 | + case parser.DIV: |
| 119 | + if rhs == 0 { |
| 120 | + return 0, fmt.Errorf("%d:%d: division by zero", expr.PositionRange().Start, expr.PositionRange().End) |
| 121 | + } |
| 122 | + return lhs / rhs, nil |
| 123 | + case parser.MOD: |
| 124 | + if rhs == 0 { |
| 125 | + return 0, fmt.Errorf("%d:%d: modulo by zero", expr.PositionRange().Start, expr.PositionRange().End) |
| 126 | + } |
| 127 | + return math.Mod(lhs, rhs), nil |
| 128 | + case parser.POW: |
| 129 | + return math.Pow(lhs, rhs), nil |
| 130 | + default: |
| 131 | + return 0, fmt.Errorf("unexpected duration expression operator %q", n.Op) |
| 132 | + } |
| 133 | + default: |
| 134 | + return 0, fmt.Errorf("unexpected duration expression type %T", n) |
| 135 | + } |
| 136 | +} |
0 commit comments