Repository navigation
Implement PromQL range functions present_over_time, absent_over_time, quantile_over_time and predict_linear - #112842
Conversation
|
Workflow [PR], commit [fa79b65] Summary: ❌
AI ReviewSummaryThis PR adds Findings❌ Blockers
Tests
Final Verdict❌ Not ready to merge: the current head can still return silently wrong results for LLVM Coverage ReportMeasured on commit fa79b65.
Changed lines: Changed C/C++ lines covered: 828/942 (87.90%) · Uncovered code |
Build profile diff (arm_release)Comparing commit See the job log for details. |
predict_linear(v range-vector, t scalar) rejected every `t` that was not a literal constant (StoreMethod::CONST_SCALAR), even though `t` is advertised as a general PromQL scalar. A query like predict_linear(up[3m], scalar(sum(vector(60)))) type-checks as a scalar and is represented as StoreMethod::SINGLE_SCALAR elsewhere in the translator (see applyFunctionScalar.cpp), but still failed with NOT_IMPLEMENTED on the guard in applyFunctionPredictLinear.cpp. The prediction horizon is only ever used to build the fifth (constant) parameter of the `timeSeriesPredictLinearToGrid` aggregate function, so there is no need for its value to be known at translation time — it only needs to be a single constant-foldable SQL expression. Extend the guard to accept StoreMethod::SINGLE_SCALAR too, and for that case materialize the scalar subquery as a named scalar CTE (`WITH (SELECT ...) AS name`) referenced via `assumeNotNull(name)`, mirroring the existing StoreMethod::SINGLE_SCALAR handling for the `phi` parameter of quantile_over_time in applyFunctionQuantileOverTime.cpp. Add a regression test exercising predict_linear() with a genuinely non-literal scalar `t` argument. Addresses review comment on PR ClickHouse#112842.
|
Dear @vitlibar, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself. |
98d9307 to
ce94de1
Compare
… quantile_over_time and predict_linear Co-Authored-By: Claude Opus 5 <[email protected]>
ce94de1 to
c99ab7f
Compare
- `timeSeriesQuantileToGrid` takes the quantile level as an argument after the samples (a number or an array with one level per grid point) instead of a parameter, keeps the window's values sorted in a running summary, and computes the Prometheus edge cases of the level (-Inf, +Inf, NaN) itself. - New `timeSeriesLinearRegressionToGrid` returns `(intercept, slope)` for every grid point. `predict_linear` is built on it and applies the horizon in SQL, so the horizon may vary per step, and under a fixed `@` modifier it is evaluated at every step like in Prometheus. - The base class lets a derived class add arguments after the samples and extend the state. `timeSeriesPredictLinearVaryingToGrid` and `timeSeriesQuantileVaryingToGrid` are removed. - `absent` and `absent_over_time` share one translator; a neutral row added with UNION ALL makes the result independent of `empty_result_for_aggregation_by_empty_set`. - The precision machinery for time()-derived scalar arguments on Float32 tables is removed. - The stateless tests are organized per function and use `prometheusQuery` instead of shell scripts; the integration test cases are merged into `test_function_over_time`.
c99ab7f to
fa79b65
Compare
|
I reworked the solution of this PR, my changes:
|
PromQL Compliance ReportBaseline: S3 master
Uploaded compliance JSON for this PR commit Baseline updateThis run reports 96.47% vs master baseline 84.23% ( |
|
@groeneai Analyze CI failures |
|
Analyzed the CI of the current head The only red is The one real test failure in this PR's history is already fixed by your rework. against the single-line spelling in the reference, because Everything else on the head is green, including the jobs that were red before the rework: no failing test rows in CIDB, and 7 AST fuzzer variants, 4 BuzzHouse variants, 10 Stress test variants, Upgrade check, |
3a43db4
Master release builds are red on this PR's merge commit
arm_release reports This PR's own release builds passed at head Consequences on master at that SHA:
Logs:
Two shapes of fix: split the file so no single object exceeds 50 MB, or add |
Related: #57545
Important
Depends on #115073.
04551_promql_phase3_range_functionsrunsabsent_over_time({__name__=~"up|up2",instance="host1"}[3m])through--dialect promql, and the SQLlexer rejects
=~before the PromQL parser sees the text:Code: 62 ... Unrecognized token: Syntax error: failed at position 28 (~).#115073 is what lets a raw-text dialect keep tokens the SQL lexer rejects. It is the only failing
test here, and it accounts for every red check on this PR; the rest of the suite is green. Existing
PromQL tests do not hit this because they pass the expression as a string literal to
prometheusQuery(...), where the lexer never sees it.Merge #115073 first, then a master merge here turns this green. The test is deliberately left as-is
rather than rewritten to dodge the lexer bug.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
PromQL: implemented range functions
present_over_time(),absent_over_time(),quantile_over_time()andpredict_linear().Description
Implements four PromQL functions over range vectors that were previously rejected with a not-implemented error:
present_over_time(v range-vector)— for each grid point,1if its staleness window contains at least one sample, otherwise the series is dropped.absent_over_time(v range-vector)— an empty vector if the range vector has any samples, otherwise a 1-element synthetic series with value1and the labels inferred from the selector's equality matchers (matching Prometheusabsent_over_time/absentsemantics).quantile_over_time(phi scalar, v range-vector)— the φ-quantile of the sample values in each grid point's window, computed with the R-7 (inclusive) method (quantileExactInclusive). φ outside[0, 1]yields ±Inf and NaN φ yields NaN, as in Prometheus.predict_linear(v range-vector, t scalar)— simple linear regression over the in-window samples, extrapolatedtseconds past the evaluation time.Implementation
Two new grid aggregate functions:
timeSeriesPresentToGrid—1if a window has any sample, elseNULL.timeSeriesQuantileToGrid— R-7 inclusive quantile over a window (non-invertible summary that collects window values and sorts them ingetResult).Both are registered inline in
AggregateFunctionTimeseriesHelpers.cpp.predict_linearreuses the existingtimeSeriesPredictLinearToGridaggregate (theis_predictvariant of the linear-regression aggregate).present_over_timeis wired throughapplyFunctionOverRange'simpl_map.quantile_over_timeandpredict_linearhave dedicated translators because they take an extra scalar argument.absent_over_timebuilds a per-series presence grid viapresent_over_time, then at runtime counts present series per grid point (countForEach) and inverts the result, so it works whether or not the metric exists (a selector on a missing metric is a runtime subquery, not a compile-time-empty one). Output labels are derived from the selector's matchers, likeabsent().No parser changes were needed — the grammar already accepts these function names and
getFunctionResultTypealready returns the correct result types.Testing: new parser cases in
gtest_PromQLParser.cppforpresent_over_timeandabsent_over_time(quantile_over_time/predict_linearwere already covered); new stateless test04551_promql_phase3_range_functions.shexercising all four functions (present/absent, quantile at φ = 0 / 0.5 / 1, predict_linear at offsets 0 and 60) against a multi-series TimeSeries table, with values verified against Prometheus semantics.Update: time-varying
predict_offset/phipredict_linear'standquantile_over_time'sphinow also accept a per-grid-point-varying PromQL scalar (e.g.time()in a range query), not just a constant or single-row scalar. This is handled by two new, self-contained aggregate functions,timeSeriesPredictLinearVaryingToGrid/timeSeriesQuantileVaryingToGrid, which take the varying value as a 3rdArray(Float64)argument instead of a fixed aggregate parameter (ClickHouse aggregate parameters are bound once per instance and can't vary per row). They're isolated fromAggregateFunctionTimeseriesBase(shared by ~10 othertimeSeries*ToGridfunctions) to avoid regression risk to those.Since these are public SQL aggregate functions, not just an internal translator detail, they validate that the 3rd-argument array is identical across every row/merged state and throw
BAD_ARGUMENTSotherwise, rather than silently using whichever array was seen first.Scope note: the varying value is normalized to
Array(Float64)in the translator so it works forFloat32-valuedTimeSeriestables too, not justFloat64. However, the PromQL scalar's own grid representation (fromFunctionTime.cpp'sSCALAR_GRIDoutput fortime()) is cast toArray(scalar_data_type), which is the table's value type — for aFloat32-valued table, that meanstime()'s own raw values (Unix timestamps, ~1.7e9) already lose precision (~128-unit rounding at that magnitude) before my code ever sees them. This is a pre-existing property ofscalar_data_typebeing shared between value-precision and timestamp-scalar-precision, not something this change introduces, but this change is what makes it reachable forpredict_offset/phi. It only matters when the varying value is derived fromtime()on a non-Float64table. A baretime()(also insidescalar()/vector()) is rebuilt at native timestamp precision and works; any arithmetic around it (e.g.predict_linear(m[5m], time() - 60)or aphinormalized fromtime()) would see the already-rounded evaluation time, somakeVaryingScalarPrecisionSaferejects it withCANNOT_EXECUTE_PROMQL_QUERYinstead of returning silently wrong results — this is documented in the supported-functions tables. Fixing the root cause would mean changingfromFunctionTime.cpp's sharedSCALAR_GRIDrepresentation, which has ~15 other callers — out of scope here.Workflow [PR]
Sync PR [sync-upstream/pr/112842]
Version info
26.9.1.1564(included in26.9and later)