Skip to content

promql: preserve equal values during quantile interpolation - #20180

Merged
bwplotka merged 1 commit into
mainfrom
perfloop-pr-open-pwh58x022h
Nov 3, 2026
Merged

bwplotka merged 1 commit into
mainfrom
perfloop-pr-open-pwh58x022h

Conversation

@perfloop

@perfloop perfloop Bot commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Note

Triggered and guided by @bwplotka -- let me know if this is too spammy etc!

Which issue(s) does the PR fix:

The shared quantile helper now returns an identical sample directly instead of multiplying it by interpolation weights. This preserves subnormal values in quantile_over_time and quantile aggregation while leaving unequal endpoints on the existing weighted formula. mad_over_time uses the same helper for its median.

TestQuantileInterpolationPreservesExactValues runs both query operations against TSDB data and compares float64 bits, including non-finite, signed-zero, invalid-q, and unequal-endpoint controls. It fails at the base because quantile_over_time(0.5, ...) returns zero for the smallest positive subnormal; it passes with this change. TestMADOverTimeUsesExactSubnormalMedian checks the other shared consumer: at base it returns one subnormal unit instead of two, and it passes with this change.

A small Go program used for the base/final correctness assertion is not included in this pull request. The full go test -count=1 ./promql passed on the final source.

The 32-bit Go test job (GOARCH=386 go test -parallel=1 ./...) did not run locally; its github.com/prometheus/prometheus/promql tests remain target-CI work because this sandbox cannot execute 32-bit ELF binaries.

This condition holds with this change and does not hold at a2fda0ef4638518a6a6fb27fc48be88fbd732f4b: For the specified two-sample constant finite inputs, both `quantile_over_time` and quantile aggregation return the exact input float64 bits for both signs of the smallest subnormal, three subnormal units, largest subnormal, and smallest normal at q=0.25, 0.5, and 0.75; `mad_over_time` returns two smallest-subnormal units for `[0, 3, 3, 6]`; mixed signed zero returns +0, equal infinities retain their sign, mixed infinities and NaN samples return NaN, and q=NaN, -1, and 2 return NaN, -Inf, and +Inf respectively with invalid-quantile warnings; opposite MaxFloat64 endpoints return +0, and adjacent endpoints near MaxFloat64 remain finite..

Release notes for end users (ALL commits must be considered).

[BUGFIX] PromQL: Preserve equal subnormal samples during quantile interpolation, including the median used by `mad_over_time`.

Generated by Perfloop.

@perfloop
perfloop Bot requested review from a team, roidelapluie and vpranckaitis as code owners October 24, 2026 22:57
Comment thread CHANGELOG.md Outdated

## main / unreleased

- [BUGFIX] PromQL: Preserve equal subnormal samples during quantile interpolation, including the median used by `mad_over_time`.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CHANGELOG.md is generated at release time from PR release-notes blocks via scripts/generate_release_notes.sh, so please revert the direct edit here to avoid merge conflicts and duplicate entries.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR head: 0b49224f29628d487d187468c39e5a808f768ca4. Response to this feedback.

Removed the direct CHANGELOG.md edit; the PR release-notes block remains for release generation.

Comment thread promql/quantile.go Resolved
Comment thread promql/quantile_interpolation_test.go Resolved
Comment thread promql/quantile_interpolation_test.go Resolved
Comment thread promql/quantile_interpolation_test.go Resolved
@perfloop
perfloop Bot force-pushed the perfloop-pr-open-pwh58x022h branch from 8f74c64 to 0b49224 Compare October 26, 2026 08:01
@perfloop

perfloop Bot commented Oct 26, 2026

Copy link
Copy Markdown
Contributor Author

Which issue(s) does the PR fix:

The shared quantile helper now returns one endpoint without weighting when the two endpoints compare equal and have identical float64 bits. This avoids subnormal underflow and ordinary-float rounding drift. Other endpoint pairs keep the existing weighted interpolation, preserving mixed signed-zero and unequal-endpoint behavior. The helper also serves both medians in mad_over_time.

TestQuantileInterpolationPreservesExactValues inserts float samples into TSDB and executes both quantile_over_time and quantile aggregation. It compares bits for signed subnormals around the normal boundary and for 3 at q=0.2, with signed-zero, infinity, NaN, invalid-q, and unequal-large-value controls. On the base revision, the exact comparison fails for 3 at q=0.2; the final source passes the full set. TestMADOverTimeUsesExactSubnormalMedian uses [0, 0, 2u, 2u] to make all deviations one subnormal unit; the result is zero on the base revision and one unit on the final source.

The standalone comparison program used for the base/final assertion is not included in this pull request. The 32-bit promql package tests were not run locally because this sandbox cannot execute 32-bit ELF binaries; they remain target-CI work. The full promql package tests, scoped lint, affected-target build, and release-note check passed here.

This condition holds with this change and does not hold at e471c7708c6b0b13e2050222bbd5a907b36f4113: For two-sample constant finite inputs, both `quantile_over_time` and quantile aggregation return the exact input float64 bits for both signs of the smallest subnormal, three subnormal units, largest subnormal, and smallest normal at q=0.25, 0.5, and 0.75, and for the ordinary value 3 at q=0.2; `mad_over_time` returns one smallest-subnormal unit for `[0, 0, 2u, 2u]`; mixed signed zero returns +0, equal infinities retain their sign, mixed infinities and NaN samples return NaN, and q=NaN, -1, and 2 return NaN, -Inf, and +Inf respectively with invalid-quantile warnings; opposite MaxFloat64 endpoints return +0, and adjacent endpoints near MaxFloat64 remain finite..

Release notes for end users (ALL commits must be considered).

[BUGFIX] PromQL: Preserve bit-identical samples during quantile interpolation to avoid rounding error and subnormal underflow, including in `mad_over_time`.

Generated by Perfloop.

@bwplotka bwplotka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks solid 👍🏽

@bwplotka
bwplotka merged commit c41aaf8 into main Nov 3, 2026
39 checks passed
@bwplotka
bwplotka deleted the perfloop-pr-open-pwh58x022h branch November 3, 2026 05:08
zenador added a commit to grafana/mimir that referenced this pull request Nov 23, 2026
…7444)

#### What this PR does

Ports prometheus/prometheus#20180 to MQE. That change is already in the
vendored Prometheus, so since the last vendoring the two engines can
return different values for `quantile`, `quantile_over_time` and
`mad_over_time` when the quantile falls between two identical values.

When the quantile falls between two samples, both engines interpolate
with `lower*(1-weight) + upper*weight`. If the two samples are
identical, this can return a value that differs from the samples by a
rounding error, for example `quantile_over_time(0.3, ...)` over two
samples of `0.1` returns `0.09999999999999999`, or `0` for very small
(subnormal) values because of underflow. The Prometheus engine now
returns the sample value in this case, and MQE now does the same. The
bits are compared so that negative zero is kept.

`TestQuantile` now has the cases from the upstream change, and a case
with a rounding error for ordinary values. The comparison compares bits,
because `require.Equal` treats negative and positive zero as equal.

#### Which issue(s) this PR fixes or relates to

Related to prometheus/prometheus#20180

#### Checklist

- [x] Tests updated.
- [ ] Documentation added.
- [x] `CHANGELOG.md` updated - the order of entries should be
`[CHANGE]`, `[FEATURE]`, `[ENHANCEMENT]`, `[BUGFIX]`. If changelog entry
is not needed, please add the `changelog-not-needed` label to the PR.
- [ ]
[`about-versioning.md`](https://github.com/grafana/mimir/blob/main/docs/sources/mimir/configure/about-versioning.md)
updated with experimental features.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants