Commit Graph

1321 Commits

Author SHA1 Message Date
George Krajcsovits
457a2381f9
fix(parser): revert pr 16041 and 16754 (#16996)
* fix(parser): revert pr 16041 and 16754

Fix for https://github.com/prometheus/prometheus/issues/16053
turned out to be too complicated and lead to more errors than it solved.
See https://github.com/prometheus/prometheus/pull/16875.

* add TODOs to wrong end positions
* restore test and fix initialization bug

Signed-off-by: György Krajcsovits <gyorgy.krajcsovits@grafana.com>
2025-08-05 10:48:08 +02:00
Bryan Boreham
e9cf792c47
Merge pull request #16895 from bboreham/noinline-kahan
[BUGFIX] PromQL: Noinline kahanSumInc, to reduce numerical errors
2025-07-28 15:55:19 +01:00
Carrie Edwards
44b0fbba1e
No info annotation for rate/increase when type is histogram (#16915)
Signed-off-by: Carrie Edwards <edwrdscarrie@gmail.com>
2025-07-24 10:11:37 -07:00
Darshan Chaudhary
9b00c296a8
[PERF] PromQL: Pass concrete types to promql functions (#16797)
Currently, the promql functions take the interface slice []parser.Value as an argument,
which is being implemented by the conrete types Vector, Matrix etc. This PR replaces
the interface with the concrete types, resulting in improved performance.
The inspiration for this PR came from #16698 which does this for binops. 
I extended the idea to all promql functions

Signed-off-by: darshanime <deathbullet@gmail.com>

* pass single Matrix

Signed-off-by: darshanime <deathbullet@gmail.com>

---------

Signed-off-by: darshanime <deathbullet@gmail.com>
2025-07-23 17:30:03 +01:00
Harry John
b09cf6be8d
fix(promql): Ensure native histogram values copied in subqueries (#16879)
Signed-off-by: 🌲 Harry 🌊 John 🏔 <johrry@amazon.com>
2025-07-21 08:54:54 +02:00
Bryan Boreham
358499f868 [BUGFIX] PromQL: Noinline kahanSum, to avoid compiler optimisations
Observed on go1.24.4 darwin/arm64, the compensation variable is not correctly calculated.

Note you may have to run the tests several times to see it, to get the right ordering of series.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
2025-07-19 15:19:29 +01:00
George Krajcsovits
4662722a8e
chore(promql): improve doc string in promql/durations.go (#16863)
Signed-off-by: György Krajcsovits <gyorgy.krajcsovits@grafana.com>
2025-07-11 12:07:54 +02:00
Björn Rabenstein
b7f984d6d2
Merge pull request #16585 from kapillamba4/fix/16393-strict
Convert PromQL tests to new syntax via basic migration mode
2025-07-10 15:45:38 +02:00
Björn Rabenstein
eb3ea163fa
promqltest: add tests for histogram_count(increase(...)) (#16854)
As `histogram_count` is playing tricks to improve performance, we
better make sure that the limitation of extrapolation below zero still
works as expected.

Signed-off-by: beorn7 <beorn@grafana.com>
2025-07-10 15:44:02 +02:00
Björn Rabenstein
362141370d
Merge pull request #16828 from prometheus/beorn7/histogram2
promql(histograms): scale a histogram the same as the count
2025-07-10 13:26:15 +02:00
beorn7
107e4a00c3 promqltest: Test NaN sample values for quantile aggregator
Signed-off-by: beorn7 <beorn@grafana.com>
2025-07-09 13:38:19 +02:00
liangmulu
b1a7df2c0c chore: fix some minor issues in comments
Signed-off-by: liangmulu <liangmulu@outlook.com>
2025-07-09 18:05:41 +08:00
Kapil Lamba
df0e034314 address code review comments
Signed-off-by: Kapil Lamba <kapillamba4@gmail.com>
2025-07-09 07:25:31 +05:30
Björn Rabenstein
dbee82267a
Merge pull request #16725 from MichaHoffmann/mhoffmann/fix-topk-nan-arg-error-on-nonexisting-series
promql: fix topk error on NaN argument for non-existing series
2025-07-08 19:42:20 +02:00
beorn7
bcf7a822a0 promql: Prevent extrapolation below zero for histogram count
This deals with the count field of native histograms in the same way
as with simple float counters. It then scale the whole histogram with
the same factor as it has scaled the count. This will still allow
individual buckets to get extrapolated below zero, but maybe that is
fine.

This implements approach (2) as described in
https://github.com/prometheus/prometheus/issues/15976#issuecomment-3032095158

Signed-off-by: beorn7 <beorn@grafana.com>
2025-07-08 19:01:31 +02:00
Michael Hoffmann
44ee5e2ad6 promql: fix topk error on NaN argument for non-existing series
Signed-off-by: Michael Hoffmann <mhoffmann@cloudflare.com>
2025-07-07 06:19:39 +00:00
beorn7
c0a13223e7 promql: add tests to demonstrate extrapolation below zero
This shows how float counters cannot go below zero when extrapolationg
for rate/increase, and how histograms do not have that protection yet,
leading to an overestimation of the rate/increase.

This also demonstrates edge cases where the count extrapolation does
not need to be limited, but an individual bucket still goes below
zero.

Signed-off-by: beorn7 <beorn@grafana.com>
2025-07-06 23:42:55 +02:00
Julien Pivotto
984c8de0da PromQL: Fix printing +min()
Signed-off-by: Julien Pivotto <291750+roidelapluie@users.noreply.github.com>
2025-07-02 11:17:17 +02:00
Julien Pivotto
3af0bdee68 PromQL: min/max/step: add more tests
Signed-off-by: Julien Pivotto <291750+roidelapluie@users.noreply.github.com>
2025-07-02 11:17:17 +02:00
Julien Pivotto
ee7d5158a7 Add step(), min(a,b) and max(a,b) in promql duration expressions
step() is a new keyword introduced to represent the query step width in duration expressions.

min(a,b) and max(a,b) return the min and max from two duration expressions.

Signed-off-by: Julien Pivotto <291750+roidelapluie@users.noreply.github.com>
2025-07-02 11:17:17 +02:00
Björn Rabenstein
9e73fb43b3
Merge pull request #16773 from prometheus/beorn7/promql
promql: Re-introduce direct mean calculation
2025-06-27 14:57:12 +02:00
beorn7
ce809e625f promql: Re-introduce direct mean calculation for better accuracy
This commit brings back direct mean calculation (for `avg` and
`avg_over_time`) but isn't an outright revert of #16569. It keeps the
improved incremental mean calculation and features generally a bit
cleaner code than before.

Also, this commit...

- ...updates the lengthy comment explaining the whole situation and
  trade-offs.

- ...divides the running sum and the Kahan compensation term
  separately (in direct mean calculation) to avoid the (unlikely)
  possibility that sum and Kahan compensation together ovorflow
  float64.

- ...uncomments the tests that should now work again on darwin/arm64.

- ...uncomments the test that should now reliably yield the
  (inaccurate) value 0 on all hardware platforms. Also, the test
  description has been updated accordingly.

- ...adds avg_over_time tests for zero and one sample in the range.

Signed-off-by: beorn7 <beorn@grafana.com>
2025-06-27 14:34:46 +02:00
beorn7
f71daa7977 promql: Remove falsified comment from test
The test in question actually worked fine even before #16569. The
finding reported in the comment has turned out to be caused by
something else.

Signed-off-by: beorn7 <beorn@grafana.com>
2025-06-27 14:34:46 +02:00
beorn7
2b3fc1f115 promql: Add test cases for direct mean calculation
These demonstrate that direct mean calculation has some merits after
all.

Signed-off-by: beorn7 <beorn@grafana.com>
2025-06-27 14:34:46 +02:00
George Krajcsovits
5b7ff92d95
fix(promql): histogram_quantile and histogram_fraction NaN observed in native histogram (#16724)
* fix(promql): histogram_quantile NaN observed in native histogram

Fixes: #16578

See the issue for detailed explanation.
When a histogram had only NaN observations and no normal observations,
we returned 0 from the quantile, which is completely wrong. If there were
normal observations but we went over them, we returned the upper bound of
the existing buckets, however that contradicts expectations on
histogram_fraction. Now we return NaN if the quantile is calculated to be
over all normal observations, falling into NaNs (in a virtual +Inf bucket).

We also return info level annotations if we see any NaN observations.
The annotation calls out if we returned NaN or even if we took the
virtual +Inf bucket into account.

Signed-off-by: György Krajcsovits <gyorgy.krajcsovits@grafana.com>

* fix(promql): histogram_fraction NaN observed in native histogram

Fixes: #16580

According to the specification we should not take NaN observations
into account when calculating the fraction. This commit fixes that
and adds an info level annotation to let the user know about this.

Signed-off-by: György Krajcsovits <gyorgy.krajcsovits@grafana.com>
2025-06-25 13:37:43 +02:00
Björn Rabenstein
8ed37d3c09
Merge pull request #16744 from harry671003/fix_last_over_time
PromQL: Fix native histogram last_over_time
2025-06-23 00:56:08 +02:00
Kapil Lamba
030835560b promqltest: add/remove explicit annotation expectations to relevant tests
Signed-off-by: Kapil Lamba <kapillamba4@gmail.com>
2025-06-21 18:46:50 +05:30
Kapil Lamba
658e9c5155 promqltest: migrate test files to new syntax using basic mode
Signed-off-by: Kapil Lamba <kapillamba4@gmail.com>
2025-06-21 18:46:33 +05:30
Julien
8f75da3fd6
Merge pull request #16754 from simonpasquier/fix-promql-panic
fix: prevent invalid array access in aggregate expression
2025-06-20 13:53:59 +02:00
Simon Pasquier
71217a6e43
fix: prevent invalid array access in aggregate expression
This commit fixes the evaluation of invalid expressions like
`sum(rate(`. Before that, it would trigger a panic in the PromQL engine
because it tried to access an index which is out of range.

The bug was probably introduced by 06d0b063ea.

Signed-off-by: Simon Pasquier <spasquie@redhat.com>
2025-06-19 15:11:38 +02:00
Kapil Lamba
69906bb4f5
Add script for converting PromQL tests to new syntax format (#16562)
Signed-off-by: Kapil Lamba <kapillamba4@gmail.com>
Co-authored-by: Neeraj Gartia <80708727+NeerajGartia21@users.noreply.github.com>
2025-06-18 23:57:39 +02:00
Charles Korn
8d9dfa075d
promql: reuse histogramStatsIterator where possible, and expose it for other implementations to use (#16686)
* Expose type
* Add `Reset` method

---------

Signed-off-by: Charles Korn <charles.korn@grafana.com>
2025-06-18 23:53:46 +02:00
Björn Rabenstein
f69c83f5f1
Merge pull request #16702 from bboreham/simpler-detect
[Refactor] PromQL: Simplify detectHistogramStatsDecoding
2025-06-18 13:02:55 +02:00
🌲 Harry 🌊 John 🏔
59bf3d442b PromQL: Fix native histogram last_over_time with offset
Signed-off-by: 🌲 Harry 🌊 John 🏔 <johrry@amazon.com>
2025-06-17 10:49:06 -07:00
Michael Hoffmann
ec98e7f069 promql: fix ts_of_last_over_time for histogram vectors
This PR fixes a bug in ts_of_last_over_time where the float samples
where used when computing the last timestamp of the histogram samples.

Signed-off-by: Michael Hoffmann <mhoffmann@cloudflare.com>
2025-06-16 13:09:49 +00:00
Julien
998840c496
Merge pull request #16713 from roidelapluie/fix-pow-dur-expr
promql: Fix unary operator precedence in duration expressions
2025-06-16 11:13:34 +02:00
Julien Pivotto
6d56e77e0e promql: Fix unary operator precedence in duration expressions
Split unary operator handling in duration expressions into two specific
cases to fix precedence conflicts:
- Handle unary operators with number literals directly
- Handle unary operators with parenthesized expressions separately

This prevents unary minus from incorrectly binding to subsequent
operators in expressions like `foo offset -1^1`, ensuring it parses
as `(foo offset -1) ^ 1` rather than `foo offset (-1^1)`.

Fixes #16711

Signed-off-by: Julien Pivotto <291750+roidelapluie@users.noreply.github.com>
2025-06-13 13:40:50 +02:00
Michael Hoffmann
a5fa9431d8 promql: add ts_of_(max,min,last)_over_time functions
This commit adds the ts_of_(max,min,last)_over_time functions behind the experimental feature flag.

Signed-off-by: Michael Hoffmann <mhoffmann@cloudflare.com>
2025-06-12 15:03:28 +00:00
Innokentii Konstantinov
94d5e0f41c
engine: fix PossibleNonCounterInfo annotation for rate and increase. (#16718)
* Comment exposed field

Signed-off-by: Innokentii Konstantinov <innokenty.konstantinov@grafana.com>

* Remove invalid test

Signed-off-by: Innokentii Konstantinov <innokenty.konstantinov@grafana.com>

* Fix PossibleNonCounterInfo annotation.

Signed-off-by: Innokentii Konstantinov <innokenty.konstantinov@grafana.com>

* lint

Signed-off-by: Innokentii Konstantinov <innokenty.konstantinov@grafana.com>

---------

Signed-off-by: Innokentii Konstantinov <innokenty.konstantinov@grafana.com>
2025-06-11 14:09:57 +01:00
beorn7
68b1bcc648 promql: typo fix in comment
Signed-off-by: beorn7 <beorn@grafana.com>
2025-06-10 22:06:04 +02:00
beorn7
c3da460474 promql: Deactivate three failing tests for the time being
These tests fail on darwin/arm64.

One is expected, because the test demonstrates the limits of the
numerical accuracy of our methods, and different inaccurate outcomes
on different hardware are expected.

The other two are mysterious at the moment, see
https://github.com/prometheus/prometheus/issues/16714 for detailed
discussion and debugging.

Signed-off-by: beorn7 <beorn@grafana.com>
2025-06-10 21:48:52 +02:00
Björn Rabenstein
8fc1750bcc
Merge pull request #16569 from prometheus/beorn7/promql2
promql: Simplify avg aggregation and avg_over_time
2025-06-07 00:16:20 +02:00
beorn7
64431830b8 Add more avg_over_time test cases with extreme values
These tests were initially created by @crush-on-anechka. I modified
them slightly.

Signed-off-by: beorn7 <beorn@grafana.com>
2025-06-06 23:50:08 +02:00
Bryan Boreham
91eab63d38 [Refactor] PromQL: Simplify detectHistogramStatsDecoding
Restarting the depth-first walk on each leg of a binary expression is
convoluted. ISTM the correct logic is to walk the path backwards to the
first relevant function.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
2025-06-06 14:52:59 +01:00
Arthur Silva Sens
8d0a747237
PROM-39: Provide PromQL info annotations when rate()/increase() over series without __type__="counter" label (#16632)
* Provide PromQL info annotations when rate()/increase() over series without counter label

Signed-off-by: Arthur Silva Sens <arthursens2005@gmail.com>

* Address comments

Signed-off-by: Arthur Silva Sens <arthursens2005@gmail.com>

---------

Signed-off-by: Arthur Silva Sens <arthursens2005@gmail.com>
2025-06-06 10:01:20 -03:00
beorn7
ff67596a82 promql: Simplify avg aggregation and avg_over_time
As it turns out, if we combine Kahan summation and incremental mean
calculation properly, it works quite well and we do not need to switch
between simple mean calculation and incremental calculation based on
overflow.

This simplifies the code quite a bit.

Signed-off-by: beorn7 <beorn@grafana.com>
2025-06-05 23:26:56 +02:00
Björn Rabenstein
41594cebb4
Merge pull request #16566 from prometheus/beorn7/promql
promqltest: Adjust more tests to "left-open" ranges
2025-06-05 16:45:38 +02:00
beorn7
a4d57fce74 promqltest: Adjust more tests to "left-open" ranges
This was an oversight because the old tests still happened to pass
with the new behavior, but important test data was excluded at the
left end of the interval, rendering some tests not actually testing
what we want to test.

In the past, we also applied different strategies to adjust the test
(extend the range from 1m to 2m, or set the evaluation timestamp to
45s). This commit unifies things and reduces redundancy.

Signed-off-by: beorn7 <beorn@grafana.com>
2025-06-05 14:06:51 +02:00
György Krajcsovits
bccb1d6459
fix(promql): do not loose information about buckets when doing the detect
Signed-off-by: György Krajcsovits <gyorgy.krajcsovits@grafana.com>
2025-06-04 10:24:50 +02:00
György Krajcsovits
df94ecebab
fix(promql): histogram_count inconsistent
The problem is in the counter reset detection. The code that loads the
samples is matrixIterSlice which uses the typed Buffer iterator, which
will preload the integer histogram samples, however the last sample is
always(!) loaded as a float histogram sample in matrixIterSlice and the
optimized iterator fails to detect counter resets in that case.

Also the iterator does not reset lastH, lastFH properly.

Ref: https://github.com/prometheus/prometheus/issues/16681

Signed-off-by: György Krajcsovits <gyorgy.krajcsovits@grafana.com>
2025-06-03 12:31:27 +02:00