From 612de026da8d68f0708899a33bf9e646a8ac1d62 Mon Sep 17 00:00:00 2001 From: Neeraj Gartia <80708727+NeerajGartia21@users.noreply.github.com> Date: Thu, 11 Apr 2024 16:23:28 +0530 Subject: [PATCH] Adds Inf and NaN as Numbers to Histogram in Promql Testing Framework (#13916) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit includes Inf and NaN as numbers to histogram --------- Signed-off-by: Neeraj Gartia Signed-off-by: Björn Rabenstein Co-authored-by: Björn Rabenstein --- promql/parser/lex.go | 14 ++++++++++---- promql/parser/lex_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/promql/parser/lex.go b/promql/parser/lex.go index 641bedcfc..4e3de2a66 100644 --- a/promql/parser/lex.go +++ b/promql/parser/lex.go @@ -519,7 +519,7 @@ func lexHistogram(l *Lexer) stateFn { return lexHistogram case r == '-': l.emit(SUB) - return lexNumber + return lexHistogram case r == 'x': l.emit(TIMES) return lexNumber @@ -568,10 +568,16 @@ Loop: return lexHistogram } l.errorf("missing `:` for histogram descriptor") - } else { - l.errorf("bad histogram descriptor found: %q", word) + break Loop } - + // Current word is Inf or NaN. + if desc, ok := key[strings.ToLower(word)]; ok { + if desc == NUMBER { + l.emit(desc) + return lexHistogram + } + } + l.errorf("bad histogram descriptor found: %q", word) break Loop } } diff --git a/promql/parser/lex_test.go b/promql/parser/lex_test.go index 4a29351b0..f48c457c0 100644 --- a/promql/parser/lex_test.go +++ b/promql/parser/lex_test.go @@ -561,6 +561,35 @@ var tests = []struct { }, seriesDesc: true, }, + { // Series with sum as -Inf and count as NaN. + input: `{} {{buckets: [5 10 7] sum:Inf count:NaN}}`, + expected: []Item{ + {LEFT_BRACE, 0, `{`}, + {RIGHT_BRACE, 1, `}`}, + {SPACE, 2, ` `}, + {OPEN_HIST, 3, `{{`}, + {BUCKETS_DESC, 5, `buckets`}, + {COLON, 12, `:`}, + {SPACE, 13, ` `}, + {LEFT_BRACKET, 14, `[`}, + {NUMBER, 15, `5`}, + {SPACE, 16, ` `}, + {NUMBER, 17, `10`}, + {SPACE, 19, ` `}, + {NUMBER, 20, `7`}, + {RIGHT_BRACKET, 21, `]`}, + {SPACE, 22, ` `}, + {SUM_DESC, 23, `sum`}, + {COLON, 26, `:`}, + {NUMBER, 27, `Inf`}, + {SPACE, 30, ` `}, + {COUNT_DESC, 31, `count`}, + {COLON, 36, `:`}, + {NUMBER, 37, `NaN`}, + {CLOSE_HIST, 40, `}}`}, + }, + seriesDesc: true, + }, }, }, {