PromQL: min/max/step: add more tests

Signed-off-by: Julien Pivotto <291750+roidelapluie@users.noreply.github.com>
This commit is contained in:
Julien Pivotto 2025-07-01 11:46:03 +02:00
parent ee7d5158a7
commit 3af0bdee68
6 changed files with 47 additions and 21 deletions

View File

@ -1231,7 +1231,7 @@ duration_expr : number_duration_literal
$$ = &DurationExpr{
Op: STEP,
StartPos: $1.PositionRange().Start,
EndPos: $3.PositionRange().Start,
EndPos: $3.PositionRange().End,
}
}
| min_max LEFT_PAREN duration_expr COMMA duration_expr RIGHT_PAREN
@ -1239,7 +1239,7 @@ duration_expr : number_duration_literal
$$ = &DurationExpr{
Op: $1.Typ,
StartPos: $1.PositionRange().Start,
EndPos: $6.PositionRange().Start,
EndPos: $6.PositionRange().End,
LHS: $3.(Expr),
RHS: $5.(Expr),
}

View File

@ -2257,7 +2257,7 @@ yydefault:
yyVAL.node = &DurationExpr{
Op: STEP,
StartPos: yyDollar[1].item.PositionRange().Start,
EndPos: yyDollar[3].item.PositionRange().Start,
EndPos: yyDollar[3].item.PositionRange().End,
}
}
case 270:
@ -2266,7 +2266,7 @@ yydefault:
yyVAL.node = &DurationExpr{
Op: yyDollar[1].item.Typ,
StartPos: yyDollar[1].item.PositionRange().Start,
EndPos: yyDollar[6].item.PositionRange().Start,
EndPos: yyDollar[6].item.PositionRange().End,
LHS: yyDollar[3].node.(Expr),
RHS: yyDollar[5].node.(Expr),
}

View File

@ -4184,7 +4184,7 @@ var testExpr = []struct {
RangeExpr: &DurationExpr{
Op: STEP,
StartPos: 4,
EndPos: 9,
EndPos: 10,
},
EndPos: 11,
},
@ -4208,7 +4208,7 @@ var testExpr = []struct {
RHS: &DurationExpr{
Op: STEP,
StartPos: 9,
EndPos: 18,
EndPos: 19,
},
},
EndPos: 22,
@ -4230,7 +4230,7 @@ var testExpr = []struct {
RangeExpr: &DurationExpr{
Op: STEP,
StartPos: 7,
EndPos: 16,
EndPos: 17,
},
EndPos: 20,
},
@ -4251,7 +4251,7 @@ var testExpr = []struct {
RangeExpr: &DurationExpr{
Op: SUB,
StartPos: 4,
RHS: &DurationExpr{Op: STEP, StartPos: 5, EndPos: 10},
RHS: &DurationExpr{Op: STEP, StartPos: 5, EndPos: 11},
},
EndPos: 12,
},
@ -4310,7 +4310,7 @@ var testExpr = []struct {
LHS: &DurationExpr{
Op: STEP,
StartPos: 8,
EndPos: 13,
EndPos: 14,
},
RHS: &NumberLiteral{
Val: 5,
@ -4321,7 +4321,7 @@ var testExpr = []struct {
},
},
StartPos: 4,
EndPos: 17,
EndPos: 18,
},
EndPos: 19,
},
@ -4342,7 +4342,7 @@ var testExpr = []struct {
LHS: &DurationExpr{
Op: STEP,
StartPos: 15,
EndPos: 20,
EndPos: 21,
},
RHS: &NumberLiteral{
Val: 5,
@ -4385,7 +4385,7 @@ var testExpr = []struct {
LHS: &DurationExpr{
Op: STEP,
StartPos: 19,
EndPos: 24,
EndPos: 25,
},
RHS: &NumberLiteral{
Val: 8,

View File

@ -84,8 +84,8 @@ func (e *DurationExpr) Pretty(int) string {
fmt.Println("e.LHS", e.LHS)
fmt.Println("e.RHS", e.RHS)
if e.LHS == nil {
// This is a unary negative duration expression.
s = fmt.Sprintf("%s %s", e.Op, e.RHS.Pretty(0))
// This is a unary duration expression.
s = fmt.Sprintf("%s%s", e.Op, e.RHS.Pretty(0))
} else {
s = fmt.Sprintf("%s %s %s", e.LHS.Pretty(0), e.Op, e.RHS.Pretty(0))
}

View File

@ -195,18 +195,38 @@ func TestExprString(t *testing.T) {
{
in: "foo offset -(step())",
},
{
in: "foo offset +(5*2)",
out: "foo offset (5 * 2)",
},
{
in: "foo offset -min(10s, 20s)",
},
{
in: "foo offset -min(10s, +max(step() ^ 2, 2))",
out: "foo offset -min(10s, max(step() ^ 2, 2))",
},
{
in: "foo[200-min(-step()^+step(),1)]",
out: "foo[200 - min(-step() ^ step(), 1)]",
},
{
in: "foo[200 - min(step() + 10s, -max(step() ^ 2, 3))]",
},
}
for _, test := range inputs {
expr, err := ParseExpr(test.in)
require.NoError(t, err)
t.Run(test.in, func(t *testing.T) {
expr, err := ParseExpr(test.in)
require.NoError(t, err)
exp := test.in
if test.out != "" {
exp = test.out
}
exp := test.in
if test.out != "" {
exp = test.out
}
require.Equal(t, exp, expr.String())
require.Equal(t, exp, expr.String())
})
}
}

View File

@ -215,6 +215,12 @@ eval range from 50s to 60s step 5s metric1_total offset -(min(step(), 1s))+8000
eval range from 50s to 60s step 5s metric1_total offset -min(step(), 1s)^0
{} 1 1 1
eval range from 50s to 60s step 5s metric1_total offset +min(step(), 1s)^0
{} 1 1 1
eval range from 50s to 60s step 5s metric1_total offset min(step(), 1s)^0
{} 1 1 1
eval range from 50s to 60s step 5s metric1_total offset max(3s,min(step(), 1s))+8000
{} 8047 8052 8057