add negative offset ability

Signed-off-by: schou <pschou@users.noreply.github.com>
This commit is contained in:
schou 2021-02-14 20:53:44 -05:00
parent 7369561305
commit 0c577c547b
3 changed files with 21 additions and 3 deletions

View File

@ -383,7 +383,12 @@ paren_expr : LEFT_PAREN expr RIGHT_PAREN
offset_expr: expr OFFSET duration offset_expr: expr OFFSET duration
{ {
yylex.(*parser).addOffset($1, $3) yylex.(*parser).addOffset($1, $3, 1)
$$ = $1
}
| expr OFFSET SUB duration
{
yylex.(*parser).addOffset($1, $4, -1)
$$ = $1 $$ = $1
} }
| expr OFFSET error | expr OFFSET error

View File

@ -683,7 +683,7 @@ func (p *parser) newLabelMatcher(label Item, operator Item, value Item) *labels.
} }
// addOffset is used to set the offset in the generated parser. // addOffset is used to set the offset in the generated parser.
func (p *parser) addOffset(e Node, offset time.Duration) { func (p *parser) addOffset(e Node, offset time.Duration, direction int) {
var orgoffsetp *time.Duration var orgoffsetp *time.Duration
var endPosp *Pos var endPosp *Pos
@ -711,7 +711,7 @@ func (p *parser) addOffset(e Node, offset time.Duration) {
if *orgoffsetp != 0 { if *orgoffsetp != 0 {
p.addParseErrf(e.PositionRange(), "offset may not be set multiple times") p.addParseErrf(e.PositionRange(), "offset may not be set multiple times")
} else if orgoffsetp != nil { } else if orgoffsetp != nil {
*orgoffsetp = offset *orgoffsetp = offset * time.Duration(direction)
} }
*endPosp = p.lastClosing *endPosp = p.lastClosing

View File

@ -1362,6 +1362,19 @@ var testExpr = []struct {
End: 13, End: 13,
}, },
}, },
}, {
input: "foo offset -7m",
expected: &VectorSelector{
Name: "foo",
OriginalOffset: -7 * time.Minute,
LabelMatchers: []*labels.Matcher{
MustLabelMatcher(labels.MatchEqual, model.MetricNameLabel, "foo"),
},
PosRange: PositionRange{
Start: 0,
End: 14,
},
},
}, { }, {
input: `foo OFFSET 1h30m`, input: `foo OFFSET 1h30m`,
expected: &VectorSelector{ expected: &VectorSelector{