Repository navigation
Conversation
…ry context A table with a column of type AggregateFunction(sequenceNextNode(...), ...), AggregateFunction(rank, ...) or AggregateFunction(denseRank, ...) could be created legally and then never opened again. On 26.9 the reopen fails with UNKNOWN_AGGREGATE_FUNCTION for the first and NUMBER_OF_ARGUMENTS_DOESNT_MATCH for the other two, wrapped in ASYNC_LOAD_WAIT_FAILED, and no setting restores access: neither a query-level SET on the second run nor a server-level default helps. AggregateFunctionFactory::getImpl resolves the calling thread's query context and passes the creator that context's settings, or nullptr when the thread has none (AggregateFunctionFactory.cpp:258-277). There is no fallback to a global context: ThreadStatus::tryGetQueryContext is a bare query_context.lock() (ThreadStatus.cpp:216-219) and the query context is only ever populated by CurrentThread::attachToGroup, which is what QueryScope::create does. The async `load table` job runs on such a thread, and attaching a table parses its column types, so the parse reaches these creators with a null pointer. All three guarded with `settings == nullptr || !(*settings)[gate]`, reading "no settings" as "the gate is off". That is why the setting's value cannot help on a loader thread: the value is not merely false, it is not visible at all. Applying the gate only when there is a query context is the form five sibling creators already use (the timeSeries* family, whose creators funnel through one choke point in AggregateFunctionTimeseriesHelpers.cpp, and uniqExact's parallel-merge choice), and it is the form PR ClickHouse#121894 applies to the sixth. With no query context there is no session whose setting could be consulted and no principal to authorize, and the no-context path is reachable only by parsing a type name that already exists in metadata, in a part's columns.txt, or in a query plan a peer already validated. Writing that metadata required passing the same gate in a query context, so no user-facing refusal is weakened: with both gates at their defaults, SELECT rank(1) OVER (...), sequenceNextNode(...), CAST to either type name and CREATE TABLE are all still refused, and the guard stays the first statement in each creator so every later validation still runs, including on the loader path. For rank and denseRank this first shipped in 26.5, so such a table loads on 26.4 and stops loading on 26.5 through 26.9. Those two arms are openable and droppable after this change but the column can never hold state, because WindowFunction::serialize throws for a window function used as an aggregate; sequenceNextNode is an ordinary aggregate function, and its stored data becomes reachable again. No setting is added, removed, or has its default changed, so SettingsChangesHistory.cpp needs no entry. Related: ClickHouse#121894
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 1 finding) plus my own cold
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-10:20260924-131900 |
|
Workflow [PR], commit [8f49076] Summary: ✅
AI ReviewSummaryThis PR fixes the Findings
Tests
Final VerdictChanges requested. LLVM Coverage ReportMeasured on commit 8f49076.
Changed lines: Changed C/C++ lines covered: 1/3 (33.33%) · Uncovered code |
|
rank and denseRank are window functions; they should not be stored in the merge tree parts. |
| $CLICKHOUSE_LOCAL --path "$data_path" --query " | ||
| SET enable_funnel_functions = 1, allow_rank_dense_rank_arguments = 1; | ||
| CREATE TABLE t_sequence_next_node (c AggregateFunction(sequenceNextNode('forward', 'head'), DateTime, Nullable(String), UInt8)) ENGINE = Memory; | ||
| CREATE TABLE t_rank (c AggregateFunction(rank, UInt64)) ENGINE = Memory; |
There was a problem hiding this comment.
This SELECT 1::AggregateFunction(rank, UInt64) SETTINGS allow_rank_dense_rank_arguments = 1; gives error because it's a window function, so these statements should also fail.
There was a problem hiding this comment.
Agreed, and the inconsistency is on master rather than in this PR: with the setting on, that cast fails Code: 36 ... The function 'rank' can only be used as a window function, not as an aggregate function (WindowFunction::fail), while CREATE TABLE t (c AggregateFunction(rank, UInt64)) with the same setting succeeds.
So I dropped the rank and denseRank hunks in 8f49076. These two lines went with them, the test now covers sequenceNextNode only, and WindowTransform.cpp is byte identical to master again.
Review on this PR objected that rank and denseRank are window functions and so should not be storable as an aggregate state column type at all, which makes tolerating a null Settings pointer in their creators the wrong direction: ClickHouse#121996 (comment) ClickHouse#121996 (comment) Both WindowTransform.cpp hunks are reverted, so that file is byte identical to master, and the test covers sequenceNextNode only. Measured after the revert, with allow_rank_dense_rank_arguments = 1: creating a column of type AggregateFunction(rank, UInt64) or AggregateFunction(denseRank, UInt64) still succeeds and reopening it still fails with NUMBER_OF_ARGUMENTS_DOESNT_MATCH, exactly as on master. So the arm is dropped in behaviour, not only in text. The changelog category moves from Bug Fix to Improvement. The rank arm carried the released-stable-regression claim, since that guard first shipped in 26.5 and allow_rank_dense_rank_arguments is tier 0, while the remaining sequenceNextNode arm is reachable only behind enable_funnel_functions, which is tier EXPERIMENTAL. 05252_aggregate_state_attach_without_query_context passes 50/50 under randomized settings with the fix and fails on a pristine master binary; an unrelated test passes in the same A-side setup as the control.
|
Agreed. I dropped both Measured after the revert: creating a column of type That leaves the type creatable and not loadable, which is the state before this PR. Rejecting it at creation instead is a separate change, and the predicate for it already exists: |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units8 translation units recompiled, 24 s compile time in total, 8 of them have a recent master baseline. |
|
@groeneai window functions shouldn't be inside the AggregateFunction type, maybe that should be in another PR. |
|
Acknowledged, I will send that as a separate PR. The predicate is already in tree: One surface I want to get right there, because it is the shape of the bug this PR fixes: the argument-bearing spellings already fail, at creation without I will post the PR in this thread. |
|
Opened #122148 for this: it refuses a pure window function state as a column type where the user declares it ( |
|
@groeneai this is too permissive. I want to make sure |
|
Understood, and I will not reopen this: skipping the gate when the pointer is null was the wrong direction. The creator's So enforcing it needs a source for the value outside a query. My default is to resolve the settings from the global context when there is no query context, the way Tell me if you would rather the load keep refusing, and I will leave master as it is. Either way it goes in a new PR. |
|
Opened #122204 with the design I described here: where there is no query context the gate now reads The boundary that follows from that, stated in the PR body: |
DataTypeAggregateFunction's factory creator resolved the named aggregate function and built the type without asking whether that function is a real aggregate function, so a WindowFunction subclass could become the type's function. Such a state has no serialization: WindowFunction::add, mergeImpl, serialize, deserialize and insertResultInto all call fail(). The column was therefore accepted everywhere a type name is parsed, and could never hold a value. Screen it at the creator with isOnlyWindowFunction(), which already carries exactly this meaning and is already consulted at AggregateFunctionFactory.cpp under the comment "Pure window functions are not real aggregate functions". That is one check at the single place a written AggregateFunction(...) becomes a type, so CREATE, ALTER, a full-definition ATTACH, CAST and a table function's structure argument are all covered by it, and nesting is covered for free because Array/Map/Tuple creators and the state's own argument list build their children through DataTypeFactory. The storage-level alternative missed the full-definition ATTACH, missed a table function's structure, and let Array(AggregateFunction(rank)) through. Two supporting hunks make the predicate answer for combinators. The base implementation now forwards one getNestedFunction() hop, which is what 12 singular combinators need, since they override getNestedFunction() and not isOnlyWindowFunction(). AggregateFunctionTuple has no single nested function to forward to, so it folds over nested_functions with any_of, matching the two sibling predicates beside it. BAD_ARGUMENTS rather than ILLEGAL_COLUMN, because this creator also runs for a CAST and for a table function's structure, where there is no column; the same code is what this creator already throws for a malformed function name, what the sibling SimpleAggregateFunction creator throws to refuse an unsupported function, and what WindowFunction::fail() throws for this same rule at runtime. This is a backward incompatible change, not a bug fix: no released version returns a wrong answer, loses data or rejects a valid input here. It removes an input that was accepted, and a table that already stores such a column stops attaching, because the refusal also fires when stored metadata is re-parsed. That cost was stated before the site was chosen. Deliberately out of scope, all three because they do not go through create(): rankState() with zero result groups still mints the type via getStateType(), so such a table will not reattach; the same type name in an empty result's Native header is refused by the receiver; and a view stores its query rather than column types. Gating the constructor would move rankState()'s failure from execution to analysis. Related: ClickHouse#121996
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
A table with a column of type
AggregateFunction(sequenceNextNode, ...)can now be loaded. Previously such a table could be created withenable_funnel_functions = 1and then never opened again, because the loader has no query context and the creator read the absent settings as the feature gate being off.Description
With
enable_funnel_functions = 1a column of typeAggregateFunction(sequenceNextNode(...), ...)can be created, and the table can then never be opened again: the load failsUNKNOWN_AGGREGATE_FUNCTIONwrapped inASYNC_LOAD_WAIT_FAILED, its stored data unreachable, and no setting restores access.Found while censusing the same contract for #121894, whose review asked whether other aggregate functions share it. No related issue.
Root cause.
AggregateFunctionFactory::getImpltakes a creator's settings from the current thread's query context and passesnullptrwhen the thread has none (AggregateFunctionFactory.cpp:258-277). The asyncload tablejob is such a thread. The creator guarded withsettings == nullptr || !(*settings)[enable_funnel_functions], reading "no settings" as "the gate is off". On a loader thread the gate's value is not merelyfalsebut invisible, which is why neither a query-levelSETon reopen nor a server-level default helps.Change. The gate now applies only when a query context exists, the form five sibling creators already use. Having the factory synthesise settings instead would not help: the gate defaults to
false, so the column would stay unloadable.Scope.
rankanddenseRankwere fixed the same way in the first revision and are now dropped, per review: both are window functions, so their state arguably should not be a storable column type at all.WindowTransform.cppis byte identical to master again and those two column types still fail to load exactly as on master, which is why the category moved fromBug FixtoImprovement.Validation.
05252_aggregate_state_attach_without_query_contextreopens the column across twoclickhouse localinvocations. It passes 50/50 under randomized settings and fails on a pristine master binary, with an unrelated test green in the same setup. With the gate at its default,sequenceNextNode(...),CASTto the type name andCREATE TABLEare all still refused.Census: every creator in
src/that reads theconst Settings *, and what it does when the pointer is nullExhaustive
grep -rn '(\*settings)\[' src/, plussettings->across the same surface (no hits). Grouped by guard form.settings == nullptr || ...sequenceNextNode(fixed here), plusrankanddenseRank, left alone per reviewif (settings && ...)timeSeries*family (its ~16 creators funnel through one choke point) anduniqExact's parallel-merge choice. Unchanged; this is the precedentcreateAggregateFunctionFlameGraph, fixed by #121894, so not touched hereNo data type gates on a setting at parse time, and no
window_creatorregistration reads the pointer, so a stored type always resolves through the plain creator this PR changes.Six input shapes were measured for this arm, each failing to reopen before the change and reopening after it: plain; a
LowCardinality(Nullable(String))argument; the('backward','tail')parameters; the state nested inArray(...)and inMap(...);MergeTreeas well asMemory. Data reachability was measured by writing a state, reopening, and reading it back. Hand-corrupted stored metadata (a bad direction parameter, a truncated argument list) still fails on the loader path after the change, so the fix removes one refusal and no validation.Workflow [PR]
Sync PR [sync-upstream/pr/121996]