Skip to content

Commit

Permalink
fix: allow comparison against zero bytes (#15217)
Browse files Browse the repository at this point in the history
Co-authored-by: George Robinson <[email protected]>
  • Loading branch information
trevorwhitney and grobinson-grafana authored Dec 6, 2024
1 parent 457c662 commit 17f1972
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
16 changes: 16 additions & 0 deletions pkg/logql/log/metrics_extraction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,22 @@ func Test_labelSampleExtractor_Extract(t *testing.T) {
),
wantOk: true,
},
{
name: "convert bytes without spaces",
ex: mustSampleExtractor(LabelExtractorWithStages(
"foo", ConvertBytes, []string{"bar", "buzz"}, false, false, nil, NoopStage,
)),
in: labels.FromStrings("foo", "13MiB",
"bar", "foo",
"buzz", "blip",
"namespace", "dev",
),
want: 13 * 1024 * 1024,
wantLbs: labels.FromStrings("bar", "foo",
"buzz", "blip",
),
wantOk: true,
},
{
name: "convert bytes with structured metadata",
ex: mustSampleExtractor(LabelExtractorWithStages(
Expand Down
4 changes: 4 additions & 0 deletions pkg/logql/syntax/lex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ func TestLex(t *testing.T) {
{`34+-123`, []int{NUMBER, ADD, SUB, NUMBER}},
{`34-123`, []int{NUMBER, SUB, NUMBER}},
{`sum(rate({foo="bar"}[5m])-1 > 30`, []int{SUM, OPEN_PARENTHESIS, RATE, OPEN_PARENTHESIS, OPEN_BRACE, IDENTIFIER, EQ, STRING, CLOSE_BRACE, RANGE, CLOSE_PARENTHESIS, SUB, NUMBER, GT, NUMBER}},
{`{foo="bar"} | logfmt | bytes < 0B`, []int{OPEN_BRACE, IDENTIFIER, EQ, STRING, CLOSE_BRACE, PIPE, LOGFMT, PIPE, IDENTIFIER, LT, BYTES}},
{`{foo="bar"} | logfmt | bytes < 1B`, []int{OPEN_BRACE, IDENTIFIER, EQ, STRING, CLOSE_BRACE, PIPE, LOGFMT, PIPE, IDENTIFIER, LT, BYTES}},
{`0b01`, []int{NUMBER}},
{`0b10`, []int{NUMBER}},
} {
t.Run(tc.input, func(t *testing.T) {
actual := []int{}
Expand Down
6 changes: 4 additions & 2 deletions pkg/logql/syntax/query_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,10 @@ func (s *Scanner) scanNumber(ch rune, seenDot bool) (rune, rune) {
ch = s.next()
base, prefix = 8, 'o'
case 'b':
ch = s.next()
base, prefix = 2, 'b'
if peek := s.Peek(); peek == '0' || peek == '1' {
ch = s.next()
base, prefix = 2, 'b'
}
default:
base, prefix = 8, '0'
digsep = 1 // leading 0
Expand Down

0 comments on commit 17f1972

Please sign in to comment.