mirror of
https://github.com/prometheus/prometheus.git
synced 2025-08-05 21:57:09 +02:00
PromQL: min/max/step: add more tests
Signed-off-by: Julien Pivotto <291750+roidelapluie@users.noreply.github.com>
This commit is contained in:
parent
ee7d5158a7
commit
3af0bdee68
@ -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),
|
||||
}
|
||||
|
@ -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),
|
||||
}
|
||||
|
@ -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,
|
||||
|
@ -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))
|
||||
}
|
||||
|
@ -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())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user