Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ func DurationValue(s string, step int64) (int64, error) {
}
isMinus := false
d := float64(0)
s = strings.ToLower(s)
for len(s) > 0 {
n := scanSingleDuration(s, true)
if n <= 0 {
Expand Down Expand Up @@ -656,6 +657,7 @@ func scanSingleDuration(s string, canBeNegative bool) int {
if len(s) == 0 {
return -1
}
s = strings.ToLower(s)
i := 0
if s[0] == '-' && canBeNegative {
i++
Expand Down
15 changes: 15 additions & 0 deletions lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,14 @@ func TestPositiveDurationSuccess(t *testing.T) {
f("1.23", 45, 1230)
f("0.56", 12, 560)
f(".523e2", 21, 52300)

// duration suffixes in mixed case
f("1Ms", 45, 1)
f("1mS", 45, 1)
f("1M", 45, 1*60*1000)
f("1H", 45, 1*60*60*1000)
f("1D", 45, 1*24*60*60*1000)
f("1Y", 45, 1*365*24*60*60*1000)
}

func TestPositiveDurationError(t *testing.T) {
Expand Down Expand Up @@ -588,6 +596,13 @@ func TestDurationSuccess(t *testing.T) {
f("1.23", 45, 1230)
f("-0.56", 12, -560)
f("-.523e2", 21, -52300)

f("1Ms", 10, 1)
f("1mS", 10, 1)
f("1M", 10, 60*1000)
f("1H", 10, 1*60*60*1000)
f("1D", 10, 1*24*60*60*1000)
f("1Y", 10, 1*365*24*60*60*1000)
}

func TestDurationError(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1341,6 +1341,7 @@ func (p *parser) parsePositiveDuration() (*DurationExpr, error) {
return nil, fmt.Errorf(`duration: parse error: %s`, err)
}
}

de := &DurationExpr{
s: s,
}
Expand Down
9 changes: 9 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,15 @@ func TestParseSuccess(t *testing.T) {
// funcName with escape chars
same(`foo\(ba\-r()`)

// duration converts to lower case
another(`{}[5m:3S]`, `{}[5m:3S]`)
another(`{}[5H:]`, `{}[5H:]`)
another(`{}[5M:3s]`, `{}[5M:3s]`)
another(`m[10M]`, `m[10M]`)
another(`m[10MS]`, `m[10MS]`)
another(`foo offset 5M`, `foo offset 5M`)
another(`foo offset 5S`, `foo offset 5S`)

// aggrFuncExpr
same(`sum(http_server_request) by ()`)
same(`sum(http_server_request) by (job)`)
Expand Down