Repository: VictoriaMetrics/VictoriaMetrics
Pull request #6807 (2024-08-14 16:18:25)

app/vmseleсt/promql: fix calculation of histogram buckets
### Describe Your Changes

This issue was introduced in 6a4bd5049b5d95194faa1956a3b72c8804bd02cd

See: https://github.com/VictoriaMetrics/VictoriaMetrics/issues/6714

### Checklist

The following checks are **mandatory**:

- [x] My change adheres [VictoriaMetrics contributing guidelines](https://docs.victoriametrics.com/contributing/).



Patch:
diff --git a/app/vmselect/promql/transform.go b/app/vmselect/promql/transform.go
index 16531f4a0..9cac71c79 100644
--- a/app/vmselect/promql/transform.go
+++ b/app/vmselect/promql/transform.go
@@ -1052,7 +1052,7 @@ func fixBrokenBuckets(i int, xss []leTimeseries) {
 
 	// Substitute upper bucket values with lower bucket values if the upper values are NaN
 	// or are bigger than the lower bucket values.
-	vNext := xss[0].ts.Values[0]
+	vNext := xss[0].ts.Values[i]
 	for j := 1; j < len(xss); j++ {
 		v := xss[j].ts.Values[i]
 		if math.IsNaN(v) || vNext > v {
diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md
index 07a5f2523..7b169e2b0 100644
--- a/docs/CHANGELOG.md
+++ b/docs/CHANGELOG.md
@@ -43,6 +43,7 @@ See also [LTS releases](https://docs.victoriametrics.com/lts-releases/).
 * BUGFIX: [graphite](https://docs.victoriametrics.com/#graphite-render-api-usage): respect `-search.denyPartialResponse` cmd-line flag and `deny_partial_response` query GET param when serving requests via Graphite API. Before, partial responses were always denied. Thanks to @penguinlav for the [pull request](https://github.com/VictoriaMetrics/VictoriaMetrics/pull/6748).
 * BUGFIX: [vmagent](https://docs.victoriametrics.com/vmagent/): account for `-usePromCompatibleNaming` cmd-line flag during when pushing data to remote storages. Thanks to @12345XXX  for the [pull request](https://github.com/VictoriaMetrics/VictoriaMetrics/pull/6776).
 * BUGFIX: `vminsert` in [VictoriaMetrics cluster](https://docs.victoriametrics.com/cluster-victoriametrics/): reduce CPU usage by limiting the number of concurrently running inserts. The issue was introduced in [this commit](https://github.com/VictoriaMetrics/VictoriaMetrics/commit/498fe1cfa523be5bfecaa372293c3cded85e75ab) starting from v1.101.0. See [this](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/6733) issue for details.
+* BUGFIX: [MetricsQL](https://docs.victoriametrics.com/metricsql/): fix calculation [histogram_quantile](https://docs.victoriametrics.com/metricsql/#histogram_quantile) over Prometheus buckets with inconsistent values. It was producing incorrect results in case lower buckets. The issue was introduced in [v1.102.0](https://docs.victoriametrics.com/changelog/#v11020) release, see [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/6714) for the details.
 
 ## [v1.102.1](https://github.com/VictoriaMetrics/VictoriaMetrics/releases/tag/v1.102.1)
 


Test patch:
diff --git a/app/vmselect/promql/transform_test.go b/app/vmselect/promql/transform_test.go
index 7e4646f00..2c104a005 100644
--- a/app/vmselect/promql/transform_test.go
+++ b/app/vmselect/promql/transform_test.go
@@ -39,6 +39,30 @@ func TestFixBrokenBuckets(t *testing.T) {
 	f([]float64{5, 10, 4, 3}, []float64{5, 10, 10, 10})
 }
 
+func TestFixBrokenBucketsMultipleValues(t *testing.T) {
+	f := func(values, expectedResult [][]float64) {
+		t.Helper()
+		xss := make([]leTimeseries, len(values))
+		for i, v := range values {
+
+			xss[i].ts = &timeseries{
+				Values: v,
+			}
+		}
+		for i := range len(values) - 1 {
+			fixBrokenBuckets(i, xss)
+		}
+		result := make([][]float64, len(values))
+		for i, xs := range xss {
+			result[i] = xs.ts.Values
+		}
+		if !reflect.DeepEqual(result, expectedResult) {
+			t.Fatalf("unexpected result for values=%v\ngot\n%v\nwant\n%v", values, result, expectedResult)
+		}
+	}
+	f([][]float64{{10, 1}, {11, 2}, {13, 3}}, [][]float64{{10, 1}, {11, 2}, {13, 3}})
+}
+
 func TestVmrangeBucketsToLE(t *testing.T) {
 	f := func(buckets, bucketsExpected string) {
 		t.Helper()