From f83e964231cf9fe57ea11d54ccf47c97635099fe Mon Sep 17 00:00:00 2001 From: Neeraj Gartia Date: Sat, 5 Apr 2025 23:37:39 +0530 Subject: [PATCH] fix NH load expression with incrementation more than once Signed-off-by: Neeraj Gartia --- promql/parser/lex.go | 2 +- promql/parser/lex_test.go | 36 +++++++++++++++++++ promql/parser/parse_test.go | 24 +++++++++++++ .../testdata/native_histograms.test | 2 +- 4 files changed, 62 insertions(+), 2 deletions(-) diff --git a/promql/parser/lex.go b/promql/parser/lex.go index 52658f318c..0b76911e39 100644 --- a/promql/parser/lex.go +++ b/promql/parser/lex.go @@ -512,7 +512,7 @@ func lexHistogram(l *Lexer) stateFn { l.histogramState = histogramStateNone l.next() l.emit(TIMES) - return lexNumber + return lexValueSequence case histogramStateAdd: l.histogramState = histogramStateNone l.next() diff --git a/promql/parser/lex_test.go b/promql/parser/lex_test.go index c5475a8b94..ffcfc8aac9 100644 --- a/promql/parser/lex_test.go +++ b/promql/parser/lex_test.go @@ -691,6 +691,42 @@ var tests = []struct { }, seriesDesc: true, }, + { + input: `{} {{sum:1}}+{{sum:0}}x2 {{sum:1}}+{{sum:0}}x3`, + expected: []Item{ + {LEFT_BRACE, 0, `{`}, + {RIGHT_BRACE, 1, `}`}, + {SPACE, 2, ` `}, + {OPEN_HIST, 3, `{{`}, + {SUM_DESC, 5, `sum`}, + {COLON, 8, `:`}, + {NUMBER, 9, `1`}, + {CLOSE_HIST, 10, `}}`}, + {ADD, 12, `+`}, + {OPEN_HIST, 13, `{{`}, + {SUM_DESC, 15, `sum`}, + {COLON, 18, `:`}, + {NUMBER, 19, `0`}, + {CLOSE_HIST, 20, `}}`}, + {TIMES, 22, `x`}, + {NUMBER, 23, `2`}, + {SPACE, 24, ` `}, + {OPEN_HIST, 25, `{{`}, + {SUM_DESC, 27, `sum`}, + {COLON, 30, `:`}, + {NUMBER, 31, `1`}, + {CLOSE_HIST, 32, `}}`}, + {ADD, 34, `+`}, + {OPEN_HIST, 35, `{{`}, + {SUM_DESC, 37, `sum`}, + {COLON, 40, `:`}, + {NUMBER, 41, `0`}, + {CLOSE_HIST, 42, `}}`}, + {TIMES, 44, `x`}, + {NUMBER, 45, `3`}, + }, + seriesDesc: true, + }, }, }, { diff --git a/promql/parser/parse_test.go b/promql/parser/parse_test.go index 64ce97304c..06f41c3f77 100644 --- a/promql/parser/parse_test.go +++ b/promql/parser/parse_test.go @@ -4371,6 +4371,30 @@ func TestParseHistogramSeries(t *testing.T) { }, }, }, + { + name: "series with two different increments", + input: `{} {{sum:1}}+{{sum:1}}x2 {{sum:2}}+{{sum:2}}x2`, + expected: []histogram.FloatHistogram{ + { + Sum: 1, + }, + { + Sum: 2, + }, + { + Sum: 3, + }, + { + Sum: 2, + }, + { + Sum: 4, + }, + { + Sum: 6, + }, + }, + }, { name: "series with decrement", input: `{} {{buckets:[5 10 7] schema:1}}-{{buckets:[1 2 3] schema:1}}x2`, diff --git a/promql/promqltest/testdata/native_histograms.test b/promql/promqltest/testdata/native_histograms.test index dd119c0617..751039e029 100644 --- a/promql/promqltest/testdata/native_histograms.test +++ b/promql/promqltest/testdata/native_histograms.test @@ -57,7 +57,7 @@ clear # Repeat the same histogram 10 times. load 5m - multi_histogram {{schema:0 sum:5 count:4 buckets:[1 2 1]}}x10 + multi_histogram {{schema:0 sum:5 count:4 buckets:[1 2 1]}}x10 {{schema:0 sum:5 count:4 buckets:[1 2 1]}}+{{}}x10 eval instant at 5m histogram_count(multi_histogram) {} 4