Repository navigation
Fix PromQL edge cases: out-of-range quantile() phi and histogram_quantile() over series without le - #111871
Conversation
…tile() over series without le PromQL evaluates quantile(phi, v) with phi outside [0, 1] to a constant (with a warning) instead of failing the query: phi < 0 -> -Inf, phi > 1 -> +Inf, NaN -> NaN for every group. ClickHouse used to throw "Quantile level is out of range [0..1]". For a constant out-of-range phi we now emit a constant-valued array aligned to the time grid, the same way histogram_quantile already handles an out-of-range phi. histogram_quantile() now excludes input series whose le label is missing or cannot be parsed as a float, matching Prometheus which silently drops such series: a pure non-histogram input yields an empty result. The filter applies before the out-of-range phi short-circuit, also matching Prometheus.
|
Workflow [PR], commit [e903a8b] Summary: ✅
AI ReviewSummaryThis PR fixes two PromQL compatibility gaps: out-of-range or Final Verdict✅ No new findings. LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 69/70 (98.57%) · Uncovered code |
…of range Initialize out_of_range_value at declaration (cppcoreguidelines-init-variables). Extend the out-of-range quantile level handling to runtime scalar levels like quantile(scalar(vector(-0.5)), v): clamp the aggregate function parameter into [0, 1] and substitute -Inf/+Inf/NaN in the result when the original level is out of range or NaN, matching Prometheus.
|
Both findings addressed in 35fb986:
|
…lar phi path clang-tidy (bugprone-use-after-move): std::move(phi) and phi->clone() were unsequenced arguments of the same multiIf() call, so replace the move with one more clone(). The scalar(vector(<literal>)) tests added earlier are converted to a constant phi before the query runs, so they never reached the new runtime out-of-range check. Add tests whose phi provably stays a scalar subquery at runtime: binary scalar arithmetic (never constant-folded by the converter) and scalar() over a stored series, covering phi < 0, phi > 1, NaN and in-range values for instant and multi-step evaluation.
Stateless tests failure is unrelated: server died while running 03915_exchange_tables_race (known flaky, see ClickHouse#110133), same pattern as on other PRs in this series.
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 69/70 (98.57%) · Uncovered code |
scalar() of an empty vector now yields NaN, which is a PromQL-valid quantile() parameter: quantile(NaN, ...) must return NaN (and phi < 0 / phi > 1 must return -Inf / +Inf) with a warning instead of failing the query. Without this, quantile(scalar(clamp(vector(1), 1, -1)), vector(2)) failed with "Quantile level is out of range [0..1]" thrown by quantileExactInclusiveForEach(). Short-circuit a constant out-of-range phi to a constant-valued grid, and clamp a runtime scalar phi for the aggregate function while substituting the Prometheus constant at runtime, the same way histogram_quantile() already treats its phi. These are the same changes as in ClickHouse#111871 which fixes this independently of scalar().
The previous commit fixed the implicit 0-argument branch of applyDateTimeFunction() (e.g. minute()) to synthesize the evaluation time via makeTimeQueryPieceNative(), avoiding Float32 precision loss on a TimeSeries table with a Float32-typed value column. The PromQL spec says f() is equivalent to f(vector(time())), but the explicit vector(time()) carrier still went through the generic conversion path (fromFunctionTime() -> makeTimeQueryPiece()), which casts the evaluation time down to context.scalar_data_type. So on a Float32 table, minute(vector(time())) still rounded 1770582700 to 1770582656 and returned 30, disagreeing with minute() returning 31 - two equivalent PromQL spellings with different answers. Add asVectorOfTime() to detect the vector(time()) argument shape at the AST level, and, when detected, rebuild that argument with makeTimeQueryPieceNative() (the same helper the 0-argument branch already uses) instead of the lossy piece the generic path already built. Extend the existing Float32-table regression test with sibling assertions for minute(vector(time())), both instant and range, expecting the same answer as the zero-argument form. Verified against the built binary: minute()/minute(vector(time())) and hour()/hour(vector(time())) now agree (31 and 20 respectively) at timestamp 1770582700 on a Float32-valued TimeSeries table, both as instant and range queries. The standalone compliance harness shows no regressions from this change (390 pass / 4 fail / 143 unsupported / 2 ref_mismatch); all non-passing queries are quantile()/histogram_quantile()/ label_replace()/label_join() cases fixed by the unrelated PR ClickHouse#111871, which this branch forked before and does not include. Co-Authored-By: Claude Sonnet 5 <[email protected]>
alexey-milovidov
left a comment
There was a problem hiding this comment.
I'm afraid if it makes the expression heavier for little benefit. The logic does not sound logical, but rather like something just happened to be so in Prometheus. Do we aim for 100% bug-compatibility with Prometheus?
I'm agree, that it is a hacky thing. I think it's your call, whether do you want the CH to have the 100% compatibility with Prometheus(even, when the design is pretty wacky). But from user perspective, I'd like to have as close as possible compatibility to Prometheus. Because I don't want to change any implemented Grafana dashboards & alerting that's been in place. If the goal for CH is to achieve the 100% compatibility here: |
|
Okay. While Prometheus is still in active development, we can merge it and decide on a compatibility switch later. |
Resolves the conflict reported by @nikitamikhaylov in ClickHouse#111870. The only conflict was in tests/integration/test_prometheus_protocols/test_evaluation.py, where this branch's own one-off `quantile(time() * 0 - 1, vector(2))` runtime-scalar-phi test collided with a much larger, more thorough test section that landed on `master` via the sibling PR ClickHouse#111871 (`promql/quantile-hq-edge`, merged as `f098c3151b7`). That sibling PR independently implemented and tested the exact same `applyAggregationOperatorQuantile.cpp` runtime-scalar-phi clamping logic this branch's own commit ("Handle out-of-range and NaN quantile() phi per PromQL semantics") added for the same reason (this branch's `scalar()`-of-empty-vector -> NaN change can feed an out-of-range/NaN phi into `quantile()` at runtime). The C++ file auto-merged with zero remaining diff against `master`, confirming the two implementations are identical. `master`'s test section is a strict superset of this branch's single case (it covers phi < 0, phi > 1, NaN, `by`/`without` grouping, a nonexistent metric, and grid preservation, via both `scalar(vector(<literal>))` and runtime arithmetic forms), so this branch's one-off test was dropped in favor of `master`'s comprehensive version.
Related: #57545
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
PromQL:
quantilewith a quantile level outside [0, 1] now returns -Inf/+Inf/NaN like Prometheus instead of throwing an error, andhistogram_quantilenow ignores input series whoselelabel is missing or unparsable, like Prometheus.Description
Two Prometheus compatibility fixes in the PromQL-to-SQL layer:
quantile(phi, v)aggregation with phi outside [0, 1]: previously failed withQuantile level is out of range [0..1](thrown by the underlyingquantileExactInclusive). Prometheus instead returns-Infforphi < 0,+Infforphi > 1andNaNforphi = NaNfor every group. For constant out-of-range/NaN phi the converter now emits a constant grid-aligned array (same pattern as the existinghistogram_quantileout-of-range short-circuit) instead of calling the quantile function. In-range SQL generation is byte-identical to before. Applies to plain,by (...)andwithout (...)forms.histogram_quantile(phi, v)over series without a parsablelelabel: Prometheus drops such series (strconv.ParseFloatfailure leads to the series being skipped); ClickHouse passed them through, e.g.histogram_quantile(0.9, <gauge without le>)returned all input series where Prometheus returns an empty result. Added a filter ontoFloat64OrNull(timeSeriesExtractTag(group, 'le'))to the bucket aggregation query, applied to both the normal and the out-of-range-phi branches — the filter must run before the short-circuit, which also fixeshistogram_quantile(-0.5, <non-histogram>)returning-Infseries instead of an empty result.Series with a valid
le(real buckets) flow through unchanged:histogram_quantile(0.9/-0.5/1.5, rate(..._bucket[1m]))results are identical before and after.Semantics verified against Prometheus 3.5.0, including mixed inputs where only the series carrying
lesurvive.Testing: 10 new cases in
tests/integration/test_prometheus_protocols/test_evaluation.py(6 for aggregation quantile, 4 for histogram_quantile). This flips 3 queries of the PromQL compliance suite from failed to passed.Version info
26.8.1.790(included in26.8and later)