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) }