Repository navigation
Fix TTL ... GROUP BY ... SET with an expression or a subcolumn as an aggregate function argument - #128758
Fix TTL ... GROUP BY ... SET with an expression or a subcolumn as an aggregate function argument#128758groeneai wants to merge 6 commits into
TTL ... GROUP BY ... SET with an expression or a subcolumn as an aggregate function argument#128758Conversation
…e argument A `TTL ... GROUP BY ... SET` assignment such as `value = sum(value * 2)`, `sumIf(value, value > 15)`, `sum(1)` or `tup = tuple(max(tup.ts), 0)` was accepted by CREATE TABLE and ALTER TABLE ... MODIFY TTL, but every merge and every MODIFY TTL materialization that applied it failed with NOT_FOUND_COLUMN_IN_BLOCK, so expired rows were never aggregated and the parts stopped merging. `TTLDescription::getTTLFromAST` keeps only the post-aggregation actions of an assignment (`getActions(false)`) and the aggregate descriptions, whose `argument_names` are the column names of the argument expressions. The actions that compute those arguments are built only into the local `temp_actions` of the `ExpressionAnalyzer` constructor and dropped, while `TTLAggregationAlgorithm` builds its `Aggregator` on the stored columns. A plain column argument works, and a sorting key expression works on the merge path only because a merge header carries the precomputed key column. `TTLAggregateDescription` now keeps the assignment AST before analysis and the columns it reads, and `buildArgumentsExpression` builds the arguments of its aggregates from them with the same `TreeRewriter` and `ExpressionAnalyzer` (public `appendExpression`), like `TTLDescription::buildExpression` does for the TTL expression. It is called: - in `TTLAggregationAlgorithm`, only when some argument is not a column of the header, so every rule that works today takes the same path. The aggregator header is extended with the computed arguments (subcolumns extracted as for the sorting key), `calculateAggregates` computes them for each aggregated range with an explicit row count (an argument may have no inputs), and `finalizeAggregates` passes the extended header to `Params::getHeader`; - at CREATE / MODIFY TTL (validation mode), where it replaces the separate per-argument analysis `checkTTLGroupBySetForAggregateFunctions`, which also planned an `IN` subquery of an argument before any check. It is never called while loading metadata, so a stored rule (for example one created with `allow_suspicious_ttl_expressions` whose literal set does not fit the column type) still attaches and only its TTL merge fails. An `IN` whose set comes from a subquery or a table cannot be evaluated during a TTL merge, so such an argument is rejected with BAD_ARGUMENTS before anything is planned. The build ends with the existing arrayJoin screen, so a rule stored before the CREATE-time arrayJoin check fails its merge instead of handing the aggregator columns of another length. ClickHouse#128596 Also fixes the shapes of ClickHouse#63800 and ClickHouse#53230. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ction state Add the shape of ClickHouse#53230 (`sumForEachState(CAST(finalizeAggregation(s), ...))`) to 05487. Update two comments in TTLDescription.cpp that still said a GROUP BY SET assignment has no AST. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The own `IN` predicate inspected the arguments only after `TreeRewriter`, which executes a scalar subquery and folds it into a literal under a freshly numbered `_subqueryN` alias. A scalar subquery in an aggregate argument (`sum(value * (SELECT max(factor) FROM factors))`) was therefore accepted, re-executed by every TTL merge, and its argument name no longer matched the stored aggregate description, so the merge failed with NOT_FOUND_COLUMN_IN_BLOCK. Use the shared `checkExpressionDoesntContainSubqueries`, which this file already applies to the TTL expression, on the arguments before the analysis (scalar subqueries) and after it (SQL user-defined functions are expanded there). Restore master's `collectAggregateFunctionArguments` for the pre-analysis walk and drop the now-unused includes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The rejection is claimed for CREATE TABLE and ALTER TABLE ... MODIFY TTL; only CREATE had an arm. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…us_ttl_expressions too allow_suspicious_ttl_expressions skips the TTL validator, but a subquery, an IN with a table or an arrayJoin inside an aggregate argument cannot run in any TTL merge, so it is rejected for every user DDL, like a subquery in the TTL expression. Metadata load still builds nothing. Also correct a comment in MergeTreeData that this branch made stale. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Fold the buildArgumentsExpression doc into one line and merge the two new member comments of TTLAggregationAlgorithm. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 9 findings over five rounds, none in the last) plus my own review.
Severity: ❌ blocker / |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-41:20261102-133200 |
|
cc @CurtizJ @Michicosun, could you review this and add the |
|
Workflow [PR], commit [769417f] Summary: ✅
AI ReviewSummaryThe PR computes expression and subcolumn arguments before Final VerdictStatus: ✅ Approve. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes146 object files changed (-26.92 KiB total), 0 added.
719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units852 translation units recompiled, 6530 s compile time in total, 852 of them have a recent master baseline. |
Closes: #128596
Closes: #63800
Closes: #53230
Related: #121268
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
TTL ... GROUP BY ... SETrules whose aggregate function takes an expression or a subcolumn as an argument, e.g.SET value = sum(value * 2),sumIf(value, value > 15)ormax(tup.ts): such a rule was accepted, but every merge andALTER TABLE ... MODIFY TTLthat applied it failed withNOT_FOUND_COLUMN_IN_BLOCK. A subquery, or anINwith a table, inside such an argument is now rejected byCREATE TABLEandALTER TABLE ... MODIFY TTL. Closes #128596.Description
On 26.10.2.103 and 26.3.60.2, a rule like
TTL event + INTERVAL 1 SECOND GROUP BY k SET value = sum(value * 2)is accepted, but every merge andMODIFY TTLmaterialization that applies it fails withNot found column multiply(value, 2) in block. (NOT_FOUND_COLUMN_IN_BLOCK), so expired rows are never aggregated and those parts stop merging. Other non-column arguments fail the same way (a sorting key expression only inMODIFY TTL):sumIf(value, value > 15),sum(1), a subcolumnmax(tup.ts), and the shapes of #63800 and #53230.Root cause.
TTLDescriptionkeeps only the actions applied after the aggregation. The actions that compute the aggregate arguments are built insideExpressionAnalyzerand dropped, whileTTLAggregationAlgorithmfeeds theAggregatoronly the stored columns.The change.
TTLAggregateDescriptionkeeps the assignment AST and builds the argument actions from it where they run, asTTLDescription::buildExpressiondoes for the TTL expression: inTTLAggregationAlgorithm, only when an argument is not a column of the header (working rules are unchanged), and atCREATE/MODIFY TTLfor validation, replacing the separate per-argument analysis. Nothing is built while loading metadata, so every stored table still attaches. The arguments are computed beforeexecuteOnBlock; subcolumns are extracted as for the sorting key. A subquery or anINwith a table in an argument is rejected at DDL: a TTL merge builds no set for it and re-runs a scalar subquery under a new name; #121268 covers the rest of the assignment.Validation. The new test
05487_ttl_group_by_set_aggregate_argument_expressionfails on master and passes 50/50 with randomized settings; the statelessttltests show no new failures; the repros of #63800 and #53230 now produce the aggregated row.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/128758]