From 17f197206d9e6f001754d854d26ba6c08b3c6934 Mon Sep 17 00:00:00 2001 From: Trevor Whitney Date: Fri, 6 Dec 2024 16:30:00 -0700 Subject: [PATCH] fix: allow comparison against zero bytes (#15217) Co-authored-by: George Robinson --- pkg/logql/log/metrics_extraction_test.go | 16 ++++++++++++++++ pkg/logql/syntax/lex_test.go | 4 ++++ pkg/logql/syntax/query_scanner.go | 6 ++++-- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/pkg/logql/log/metrics_extraction_test.go b/pkg/logql/log/metrics_extraction_test.go index 9cc5ff4411f51..00ebb3e9dee0c 100644 --- a/pkg/logql/log/metrics_extraction_test.go +++ b/pkg/logql/log/metrics_extraction_test.go @@ -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( diff --git a/pkg/logql/syntax/lex_test.go b/pkg/logql/syntax/lex_test.go index 08878186bd89b..0e9ad2aac3338 100644 --- a/pkg/logql/syntax/lex_test.go +++ b/pkg/logql/syntax/lex_test.go @@ -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{} diff --git a/pkg/logql/syntax/query_scanner.go b/pkg/logql/syntax/query_scanner.go index 8d48e2e33d48d..23f79104591b7 100644 --- a/pkg/logql/syntax/query_scanner.go +++ b/pkg/logql/syntax/query_scanner.go @@ -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