app/vmselect/graphite: properly handle -N index for the array of N items

This is a follow-up for 70cd09e736
Updates https://github.com/VictoriaMetrics/VictoriaMetrics/pull/5581
This commit is contained in:
Aliaksandr Valialkin 2024-01-16 21:02:31 +02:00
parent 1340ab1c07
commit 2b67944eb4
No known key found for this signature in database
GPG Key ID: 52C003EE2BCDB9EB
2 changed files with 18 additions and 4 deletions

View File

@ -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

View File

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