Skip to content

Fix LOGICAL_ERROR in AggregateFunctionArray for timeSeries*ToGrid functions - #103079

Closed
groeneai wants to merge 1 commit into
ClickHouse:masterfrom
groeneai:fix-stid-4870-timeseries-params-v2
Closed

groeneai wants to merge 1 commit into
ClickHouse:masterfrom
groeneai:fix-stid-4870-timeseries-params-v2

Conversation

@groeneai

Copy link
Copy Markdown
Collaborator

The timeSeries*ToGrid aggregate function family normalizes its four timestamp parameters internally to Decimal64 (scaled to match the timestamp column) and, for timeSeriesPredictLinearToGrid, stores predict_offset as a member instead of in the parameter array. This normalization is intentional — it lets states serialized with different timestamp scales merge together.

When the Array combinator wrapped such a function, its constructor fired LOGICAL_ERROR (AggregateFunctionArray: parameters mismatch): it was initializing its own parameter array from the user-typed params_ (UInt64, sometimes 5 entries) but then comparing against the nested function's normalized parameters (Decimal64, always 4 entries). The assertion was originally a chassert that compiled out in release; #102868 promoted it to a real LOGICAL_ERROR for diagnostics, which then surfaced as a master-level exception in serverfuzz / AST fuzzer stress runs (STID 4870 family, ~10 hits across 60 days; ~7 master, ~20 PR hits in the last 30 days).

Fix the Array combinator by initializing its own parameter array from nested_->getParameters() instead of the user-supplied params_. The Array wrapper is just an adapter over the nested function — its parameters should match the nested function's. Almost every aggregate function preserves params_ verbatim, so nested_->getParameters() == params_ and there is no behaviour change. For the timeSeries*ToGrid family the two differ, and using the nested function's parameters makes timeSeries*ToGridArray(...) states byte-compatible with the unwrapped timeSeries*ToGrid(...) states of the same scale.

Reproducer (exception before, succeeds after):

SET allow_experimental_time_series_aggregate_functions = 1;
CREATE TABLE ts (ts Array(DateTime64(3, 'UTC')), v Array(Float64)) ENGINE=Memory;
INSERT INTO ts VALUES ([toDateTime64('1970-01-01 00:01:00', 3, 'UTC')], [10.0]);
SELECT timeSeriesPredictLinearToGridArray(60, 180, 10, 20, -60)(ts, v) FROM ts;
SELECT timeSeriesDerivToGridArray(60, 180, 10, 20)(ts, v) FROM ts;

Regression test: 04102_timeseries_array_combinator_params.

Related:

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fix Logical error: AggregateFunctionArray: parameters mismatch when timeSeries*ToGrid aggregate functions are used with the Array combinator (e.g. timeSeriesPredictLinearToGridArray, timeSeriesDerivToGridArray, timeSeriesRateToGridArray). The Array wrapper was storing the user-typed parameters while the nested function stored normalized Decimal64 parameters, so their types and counts could differ. The wrapper now inherits the nested function's parameters, matching the behaviour of the unwrapped function.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

…eSeries*ToGrid functions

The `timeSeries*ToGrid` aggregate function family normalizes its parameters
internally — 4 user-typed literals (start, end, step, window) are stored as
`Decimal64` fields scaled to match the timestamp column, and the 5-parameter
`timeSeriesPredictLinearToGrid` variant stores `predict_offset` as a member
instead of in the parameter array. This produces an intentional normalization
that lets states serialized with different timestamp scales merge together.

When the `Array` combinator wrapped such a function, its constructor fired
`LOGICAL_ERROR` ("parameters mismatch"): it was initializing its own parameter
array from the user-typed `params_` (`UInt64`, possibly 5 entries) but then
comparing against the nested function's normalized parameters (`Decimal64`,
always 4 entries). The assertion was originally a `chassert` that silently
compiled out in release; ClickHouse#102868 made it a real `LOGICAL_ERROR` to aid
diagnosis, which then surfaced as a master-level failure in serverfuzz /
`AST fuzzer` runs (STID 4870 family, ~10 hits across 60 days).

Fix the Array combinator by initializing its own parameter array from
`nested_->getParameters()` instead of the user-supplied `params_`. This makes
the wrapper transparent to whatever normalization the nested function performs:
`timeSeries*ToGridArray(...)` states now match the corresponding
`timeSeries*ToGrid(...)` states, preserving the scale-independent state
compatibility that `timeSeries*ToGrid` relies on.

For every other aggregate function the user-typed `params_` and
`nested_->getParameters()` are identical — no behavior change.

Reproducer (previously threw `LOGICAL_ERROR`, now succeeds):
```
SET allow_experimental_time_series_aggregate_functions = 1;
SELECT timeSeriesPredictLinearToGridArray(60, 180, 10, 20, -60)(ts, val) FROM ts;
SELECT timeSeriesDerivToGridArray(60, 180, 10, 20)(ts, val) FROM ts;
```

Regression test: `04102_timeseries_array_combinator_params`.

STID 4870 family: 4870-4f07, 4870-4f21, 4870-4d73, 5480-49e6.
Diagnostic PR that made the error visible: ClickHouse#102868.
@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate (self-check)

Session: cron:clickhouse-ci-task-worker:20260418-204500

a) Deterministic repro? Yes. SELECT timeSeriesPredictLinearToGridArray(60, 180, 10, 20, -60)(ts, v) FROM ts; throws LOGICAL_ERROR on every run of the stock master binary without the fix (the diagnostic error message added in #102868); my local reproduction shows the exact stack trace matching the CI reports.

b) Root cause explained? The Array combinator asserted parameters == nested_func->getParameters(). The timeSeries*ToGrid family stores internally-normalized Decimal64 parameters (scale = column's timestamp scale), and the 5-arg timeSeriesPredictLinearToGrid stores predict_offset as a member instead of in the parameter array. So the wrapper held [UInt64_60, UInt64_180, UInt64_10, UInt64_20, Int64_-60] (5 entries, user-typed) while the nested function held [Decimal64_'60', Decimal64_'180', Decimal64_'10', Decimal64_'20'] (4 entries, normalized) — mismatch in both count and type.

c) Fix matches root cause? Yes. The Array wrapper is just an adapter — its parameters should match the nested function's, not the user's. Initializing from nested_->getParameters() removes the mismatch at the source (not by widening the check or silencing it).

d) Test intent preserved? / New tests added? No existing tests loosened. Added a new regression test 04102_timeseries_array_combinator_params that covers: 4-arg variants with UInt64/Decimal/String literals, the 5-arg predict variant, stacked combinators (ArrayIf), and the non-Array reference path. Ran it 20× with full randomization — 20/20 pass.

e) Demonstrated in both directions? Yes. Manual test confirmed: the reproducer query throws LOGICAL_ERROR on master HEAD (without the fix, built 19:18 UTC), and passes with the patch applied. The regression test's reference file is only generatable from the fixed binary.

f) Fix is general, not a narrow patch? Yes. The change applies to the Array combinator universally — not just to timeSeries*ToGrid. Other aggregate functions preserve params_ verbatim, so nested_->getParameters() == params_ for them and behaviour is unchanged. Verified by running 10+ unrelated Array-combinator tests (sumArray, quantilesArray, uniqArray, etc.) — all pass.

I kept the scope minimal: only AggregateFunctionArray changed. I explicitly did not touch AggregateFunctionTimeseriesBase itself because its Decimal64 normalization is intentional — it is what allows states serialized with different timestamp scales to merge (tested in 03254_timeseries_instant_value_aggregate_functions), and the existing plain-call path depends on it.

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @alexey-milovidov @Algunenano @vitlibar — could you review this? It fixes LOGICAL_ERROR: AggregateFunctionArray parameters mismatch (STID 4870 family on master serverfuzz / AST fuzzer) by having the Array combinator inherit the nested function's parameters instead of the user-typed ones. This is intentionally scoped narrowly to the Array combinator — I deliberately do not touch AggregateFunctionTimeseriesBase's Decimal64 normalization, since #102358 made it clear that normalization is required for state-merge compatibility across timestamp scales. Issue #102319 (the deeper CREATE TABLE vs INSERT type-equality problem) is unchanged.

@vitlibar vitlibar self-assigned this Apr 18, 2026
@nikitamikhaylov nikitamikhaylov added the can be tested Allows running workflows for external contributors label Apr 20, 2026
@clickhouse-gh

clickhouse-gh Bot commented Apr 20, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [15a5f6b]

Summary: ❌

job_name test_name status info comment
Integration tests (amd_msan, 1/6) FAIL
test_distributed_frozen_replica/test.py::test_cluster_all_replicas_query[1-test_cluster] FAIL cidb
Performance Comparison (arm_release, master_head, 5/6) FAIL
Check Results FAIL

AI Review

Summary

This PR fixes a real mismatch in AggregateFunctionArray when wrapping timeSeries*ToGrid functions: the wrapper previously kept user-typed parameters while nested functions may normalize/store them differently, which could trigger LOGICAL_ERROR. The change to initialize wrapper parameters from nested_->getParameters() is consistent with wrapper semantics and aligns state representation with the nested aggregate function. Added stateless regression coverage is targeted and meaningful. High-level verdict: no blocker or major issue found in this diff.

Missing context
  • ⚠️ Full CI signal is incomplete at review time (Fast test, Build (arm_tidy), and overall PR status are still pending), so this review is code-centric rather than post-CI-log validated.
ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ✅
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ✅
Compilation time ✅
No large/binary files ✅
Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Apr 20, 2026
@clickhouse-gh

clickhouse-gh Bot commented Apr 20, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.00% 84.00% +0.00%
Functions 91.10% 91.10% +0.00%
Branches 76.50% 76.50% +0.00%

Changed lines: 80.00% (4/5) · Uncovered code

Full report · Diff report

@vitlibar

Copy link
Copy Markdown
Member

Doesn't look good for now, needs investigation.
I will look into it.

@vitlibar

Copy link
Copy Markdown
Member

The issue will be fixed in #103428, this PR is not needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants