From 76c859969d85d1ed03990283f61686535bd08f34 Mon Sep 17 00:00:00 2001 From: Julien Pivotto <291750+roidelapluie@users.noreply.github.com> Date: Wed, 13 May 2026 17:35:52 +0200 Subject: [PATCH] promql: rename min/max duration expr functions to least/greatest Rename the `min()` and `max()` duration expression functions to `least()` and `greatest()` to avoid conflicts with the existing PromQL aggregate functions `min` and `max`. Update documentation and tests accordingly. Signed-off-by: Julien Pivotto <291750+roidelapluie@users.noreply.github.com> --- docs/feature_flags.md | 9 +- promql/durations.go | 4 +- promql/durations_test.go | 4 +- promql/parser/generated_parser.y | 16 +- promql/parser/generated_parser.y.go | 1072 +++++++++-------- promql/parser/lex.go | 18 +- promql/parser/parse_test.go | 60 +- promql/parser/printer.go | 8 +- promql/parser/printer_test.go | 18 +- .../testdata/duration_expression.test | 26 +- web/api/v1/translate_ast_test.go | 16 +- 11 files changed, 638 insertions(+), 613 deletions(-) diff --git a/docs/feature_flags.md b/docs/feature_flags.md index 2806e4baa7..5bee9814d3 100644 --- a/docs/feature_flags.md +++ b/docs/feature_flags.md @@ -223,7 +223,10 @@ For a **range query**, it resolves to the full range of the query (end time - st For an **instant query**, it resolves to `0s`. This is particularly useful in combination with `@end()` to look back over the entire query range, e.g., `max_over_time(metric[range()] @ end())`. -`min(, )` and `max(, )` can be used to find the minimum or maximum of two duration expressions. +`least(, )` and `greatest(, )` select between two duration expressions. +`least` returns the smaller of the two, which is useful for capping a duration at a maximum value. +`greatest` returns the larger of the two, which is useful for enforcing a minimum value. +For example, `greatest(step(), 5s)` ensures the duration is never shorter than `5s`, while `least(range(), 1h)` caps the duration at `1h`. **Note**: Duration expressions are not supported in the @ timestamp operator. @@ -245,8 +248,8 @@ Examples of equivalent durations: * `4h % 3h` is equivalent to `1h` or `3600s` * `(2 ^ 3) * 1m` is equivalent to `8m` or `480s` * `step() + 1` is equivalent to the query step width increased by 1s. -* `max(step(), 5s)` is equivalent to the larger of the query step width and `5s`. -* `min(2 * step() + 5s, 5m)` is equivalent to the smaller of twice the query step increased by `5s` and `5m`. +* `greatest(step(), 5s)` is equivalent to the larger of the query step width and `5s`. +* `least(2 * step() + 5s, 5m)` is equivalent to the smaller of twice the query step increased by `5s` and `5m`. ## OTLP Native Delta Support diff --git a/promql/durations.go b/promql/durations.go index 3dc44fbbc6..94099888c0 100644 --- a/promql/durations.go +++ b/promql/durations.go @@ -135,9 +135,9 @@ func (v *durationVisitor) evaluateDurationExpr(expr parser.Expr) (float64, error return float64(v.step.Seconds()), nil case parser.RANGE: return float64(v.queryRange.Seconds()), nil - case parser.MIN: + case parser.LEAST: return math.Min(lhs, rhs), nil - case parser.MAX: + case parser.GREATEST: return math.Max(lhs, rhs), nil case parser.ADD: if n.LHS == nil { diff --git a/promql/durations_test.go b/promql/durations_test.go index b8225ca8fc..d11ec3885a 100644 --- a/promql/durations_test.go +++ b/promql/durations_test.go @@ -228,7 +228,7 @@ func TestCalculateDuration(t *testing.T) { expected: 150 * time.Second, }, { - name: "max of step and range", + name: "greatest of step and range", expr: &parser.DurationExpr{ LHS: &parser.DurationExpr{ Op: parser.STEP, @@ -236,7 +236,7 @@ func TestCalculateDuration(t *testing.T) { RHS: &parser.DurationExpr{ Op: parser.RANGE, }, - Op: parser.MAX, + Op: parser.GREATEST, }, expected: 5 * time.Minute, }, diff --git a/promql/parser/generated_parser.y b/promql/parser/generated_parser.y index 34ce028c1f..2ad96b53c9 100644 --- a/promql/parser/generated_parser.y +++ b/promql/parser/generated_parser.y @@ -159,6 +159,8 @@ START END STEP RANGE +GREATEST +LEAST %token preprocessorEnd // Counter reset hints. @@ -183,7 +185,7 @@ START_METRIC_SELECTOR // Type definitions for grammar rules. %type label_match_list %type label_matcher -%type aggregate_op grouping_label match_op maybe_label metric_identifier unary_op at_modifier_preprocessors string_identifier counter_reset_hint min_max +%type aggregate_op grouping_label match_op maybe_label metric_identifier unary_op at_modifier_preprocessors string_identifier counter_reset_hint greatest_least %type label_set metric %type label_set_list %type