Skip to content

Fix legendValue median, percentile and max on series data - #578

Merged
timtofan merged 2 commits into
bookingcom:masterfrom
sebxt:fix/legendvalue-median-reorders-series
Oct 20, 2026
Merged

timtofan merged 2 commits into
bookingcom:masterfrom
sebxt:fix/legendvalue-median-reorders-series

Conversation

@sebxt

@sebxt sebxt commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

What issue is this change attempting to solve?

legendValue(x, 'median') and legendValue(x, 'pNN') reorder the datapoints of the returned series and of the fetched source series, because helper.Percentile sorts its input in place and helper.SummarizeValues passes the series values to it directly. The median, pNN and max branches of SummarizeValues also include absent points (stored as 0), which gives a wrong median for series with gaps and a max of 0 for negative series with gaps. sortByMaxima uses the same max branch.

How does this change solve the problem? Why is this the best approach?

SummarizeValues now builds a new slice of the non absent values before calling Percentile, and the max branch skips absent points like min, avg, last and count already do. This keeps the fix in the shared helper, so every caller gets the same behaviour. summarize already passes only present values with all absent flags false, so its results do not change.

How can we be sure this works as expected?

Added three legendValue cases to TestEvalExpression: median on an unsorted series (checks the values and that the source data is not modified), median on a series with gaps, and max on a negative series with a gap. They fail on master and pass with the change. go test -race ./... passes.

Fixes #577

Signed-off-by: Sebastian Turner <sebastiant3484@gmail.com>
@timtofan
timtofan merged commit def2925 into bookingcom:master Oct 20, 2026
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.

legendValue with median or pNN reorders the series datapoints and counts absent points

2 participants