Skip to content

Commit 17f1972

Browse files
fix: allow comparison against zero bytes (#15217)
Co-authored-by: George Robinson <george.robinson@grafana.com>
1 parent 457c662 commit 17f1972

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

‎pkg/logql/log/metrics_extraction_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,22 @@ func Test_labelSampleExtractor_Extract(t *testing.T) {
162162
),
163163
wantOk: true,
164164
},
165+
{
166+
name: "convert bytes without spaces",
167+
ex: mustSampleExtractor(LabelExtractorWithStages(
168+
"foo", ConvertBytes, []string{"bar", "buzz"}, false, false, nil, NoopStage,
169+
)),
170+
in: labels.FromStrings("foo", "13MiB",
171+
"bar", "foo",
172+
"buzz", "blip",
173+
"namespace", "dev",
174+
),
175+
want: 13 * 1024 * 1024,
176+
wantLbs: labels.FromStrings("bar", "foo",
177+
"buzz", "blip",
178+
),
179+
wantOk: true,
180+
},
165181
{
166182
name: "convert bytes with structured metadata",
167183
ex: mustSampleExtractor(LabelExtractorWithStages(

‎pkg/logql/syntax/lex_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ func TestLex(t *testing.T) {
108108
{`34+-123`, []int{NUMBER, ADD, SUB, NUMBER}},
109109
{`34-123`, []int{NUMBER, SUB, NUMBER}},
110110
{`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}},
111+
{`{foo="bar"} | logfmt | bytes < 0B`, []int{OPEN_BRACE, IDENTIFIER, EQ, STRING, CLOSE_BRACE, PIPE, LOGFMT, PIPE, IDENTIFIER, LT, BYTES}},
112+
{`{foo="bar"} | logfmt | bytes < 1B`, []int{OPEN_BRACE, IDENTIFIER, EQ, STRING, CLOSE_BRACE, PIPE, LOGFMT, PIPE, IDENTIFIER, LT, BYTES}},
113+
{`0b01`, []int{NUMBER}},
114+
{`0b10`, []int{NUMBER}},
111115
} {
112116
t.Run(tc.input, func(t *testing.T) {
113117
actual := []int{}

‎pkg/logql/syntax/query_scanner.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,10 @@ func (s *Scanner) scanNumber(ch rune, seenDot bool) (rune, rune) {
409409
ch = s.next()
410410
base, prefix = 8, 'o'
411411
case 'b':
412-
ch = s.next()
413-
base, prefix = 2, 'b'
412+
if peek := s.Peek(); peek == '0' || peek == '1' {
413+
ch = s.next()
414+
base, prefix = 2, 'b'
415+
}
414416
default:
415417
base, prefix = 8, '0'
416418
digsep = 1 // leading 0

0 commit comments

Comments
 (0)