From a9fd130980269c132f0d23bc4b7ea7a336fa1a31 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Tue, 16 Jan 2024 21:02:31 +0200 Subject: [PATCH] app/vmselect/graphite: properly handle -N index for the array of N items This is a follow-up for 70cd09e736752d8b22697b57e24d600bcf358fee Updates https://github.com/VictoriaMetrics/VictoriaMetrics/pull/5581 --- app/vmselect/graphite/transform.go | 8 ++++---- app/vmselect/graphite/transform_test.go | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/app/vmselect/graphite/transform.go b/app/vmselect/graphite/transform.go index 7047edf85..9232cca8c 100644 --- a/app/vmselect/graphite/transform.go +++ b/app/vmselect/graphite/transform.go @@ -3600,11 +3600,11 @@ func groupSeriesByNodes(ss []*series, nodes []graphiteql.Expr) map[string][]*ser } func getAbsoluteNodeIndex(index, size int) int { - // handling the negative index case - if index < 0 && index+size > 0 { - index = index + size + // Handle the negative index case as Python does + if index < 0 { + index = size + index } - if index >= size || index < 0 { + if index < 0 || index >= size { return -1 } return index diff --git a/app/vmselect/graphite/transform_test.go b/app/vmselect/graphite/transform_test.go index 22b918010..ae54e4ed7 100644 --- a/app/vmselect/graphite/transform_test.go +++ b/app/vmselect/graphite/transform_test.go @@ -92,4 +92,18 @@ func TestGetAbsoluteNodeIndex(t *testing.T) { f(0, 1, 0) f(-1, 3, 2) f(-3, 1, -1) + f(-1, 1, 0) + f(-2, 1, -1) + f(3, 2, -1) + f(2, 2, -1) + f(1, 2, 1) + f(0, 2, 0) + f(-1, 2, 1) + f(-2, 2, 0) + f(-3, 2, -1) + f(-5, 2, -1) + f(-1, 100, 99) + f(-99, 100, 1) + f(-100, 100, 0) + f(-101, 100, -1) }