Repository navigation
Reject a TTL expression containing arrayJoin - #117266
Conversation
`arrayJoin` is the one action that changes the number of rows in a block, while every consumer of a TTL expression indexes its result column positionally against the block's rows: `TTLDeleteAlgorithm::execute` sizes its loop by `block.rows()` and reads `timestamps[i]`. With a multi-element array a row was judged by an earlier row's timestamp, so rows whose own TTL was far in the future were deleted; with an empty array the read went past the end of the column and garbage decided deletion. The loss happened both in the insert-time TTL filter and during TTL merges, silently and with exit code 0. Validation only inspected `FUNCTION` nodes plus the result type, so an `ARRAY_JOIN` node passed. `allow_suspicious_ttl_expressions` does not lift this one - it is not a matter of taste - but loading already stored metadata still does, because a rejection there fails the whole load rather than the one table. The `WHERE` and `GROUP BY` set expressions are checked too. Closes: #117014
|
Workflow [PR], commit [1efa7a3] Summary: ✅
AI ReviewSummaryThis PR closes the real Tests
Final VerdictStatus:
LLVM Coverage ReportMeasured on commit 1efa7a3.
Changed lines: Changed C/C++ lines covered: 53/54 (98.15%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes4 object files changed (+15.72 KiB total), 1 added.
716 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 units9 translation units recompiled, 15 s compile time in total, 8 of them have a recent master baseline. |
|
@rschu1ze — tagging you on this one as JOIN-related, as requested. Review request sent too. |
The check only looked for an `ARRAY_JOIN` node in the built DAG, which `ActionsVisitor` creates only for the literal name `arrayJoin`. With `normalize_function_names = 0` the `unnest` alias is left uncanonicalized in the AST, so the expression is built as an ordinary call of a special function instead: the table was accepted under `allow_suspicious_ttl_expressions = 1` and every later TTL evaluation - the insert-time filter and every background TTL merge - threw `FUNCTION_IS_SPECIAL`. The AST is now checked too, by canonical function name, so the verdict does not depend on that setting or on the spelling of the alias. `checkRowPolicyFilterExpression` needs exactly the same predicate for the same reason, so its private helper is moved to `expressionContainsArrayJoin` in `src/Interpreters/ExpressionContainsArrayJoin.h` and shared - including its descent into a SQL UDF body and its skipping of nested subqueries, which have their own scope. Test `05073_ttl_array_join_rejected` covers `unnest` and `UNNEST`, both with and without `normalize_function_names`, in the TTL expression, the `WHERE` form and a column TTL.
|
🕵 @groeneai, investigate the failure: Stress test (arm_msan) — The two |
|
The Its stack ends in Of the two issues, #117806 is this same site: identical frames down to The two integration rows built on master |
`TTLValidationMode::Attach` cannot reject an `arrayJoin` in a TTL while the metadata is being read: a rejection there fails the whole load rather than the one table, so the server would not start after an upgrade and a replica could not read the metadata another replica wrote. A TTL stored before the check existed therefore still loads - and then still executed, which is the whole bug. On a table loaded from such metadata, `OPTIMIZE TABLE ... FINAL` deleted a row whose own TTL was a day in the future along with an earlier expired one, silently and with exit code 0. A background TTL merge and an `INSERT` do the same. `TTLDescription::buildExpression` and `buildWhereExpression` are the only way a consumer turns a stored description back into a runnable expression - `TTLTransform`, `TTLCalcTransform`, `TTLDeleteFilterTransform` and `MergeTreeDataWriter::updateTTL` all go through them - so the same check runs there now. The silent row loss becomes a failed `INSERT` or TTL merge, the table still attaches, and `ALTER TABLE ... REMOVE TTL` or a `MODIFY TTL` to a sane expression repairs it. A `GROUP BY ... SET` assignment is stored as prebuilt `ExpressionActions` and not as an AST, so it is screened through the built expression, in `buildExpression`, which setting up the `TTLAggregationAlgorithm` that executes it also goes through. Test `05099_ttl_array_join_stored_metadata` prepares such metadata with `clickhouse-local` over a data directory - the table is created with an ordinary TTL and its stored definition is then edited into the form an older server would have written - and covers the load, the `INSERT`, the TTL merge, and the `ALTER` that repairs the table.
| } | ||
|
|
||
| if (where_expression && !skip_validation) | ||
| checkTTLExpressionForAggregateFunctions(where_expression, /*expression_kind=*/ "WHERE "); |
There was a problem hiding this comment.
The new tests only cover the main TTL expression and the stored main-TTL carrier, but this GROUP BY ... SET carrier is now part of the fix too and it still has no focused regression. That is the trickiest path in the patch: on ATTACH the assignment survives only as prebuilt ExpressionActions, and the safety guarantee relies on TTLDescription::buildExpression noticing it before TTLAggregationAlgorithm executes the stored actions. Please add a GROUP BY TTL case here (ideally one fresh DDL rejection and one legacy stored-metadata execution failure) so removing either of these checks would actually break a test.
Closes: #117014
Related: #117022
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Reject a TTL expression that contains
arrayJoin. Such a TTL was accepted and then silently deletedrows whose own TTL was far in the future, and read past the end of the expression column when an array
was empty.
Description
arrayJoinis the one action that changes the number of rows in a block, while every consumer of aTTL expression indexes its result column positionally by block row:
TTLDeleteAlgorithm::executesizes its loop by
block.rows()and readstimestamps[i], wheretimestampshas one entry perflattened array element.
iis judged by an earlier row's timestamp, so a row whose TTL ishours in the future is deleted.
timestamps[i]reads past theend of a
PaddedPODArray; in practice whole blocks with nothing expired were dropped.The loss happens both in the insert-time TTL filter — the rows never land in a part — and during a
background TTL merge or
OPTIMIZE ... FINAL, with exit code 0 and nothing logged.ReplicatedMergeTreeshares the code.checkTTLExpressiononly inspectedFUNCTIONnodes (for determinism) plus the result type, so anARRAY_JOINnode passed. The new check is not lifted byallow_suspicious_ttl_expressions— this isa row-count contract violation with an out-of-bounds read, not a matter of taste. The
WHEREexpression and the
GROUP BYset expressions are checked the same way.The check is alias-insensitive.
ActionsVisitorbuilds anARRAY_JOINnode only for the literalname
arrayJoin, so withnormalize_function_names = 0theunnestalias was left uncanonicalizedand built as an ordinary call of a special function instead - the table was accepted and every later
TTL evaluation threw
FUNCTION_IS_SPECIAL. The AST is therefore checked as well, by canonicalfunction name.
checkRowPolicyFilterExpressionneeds exactly the same predicate, so its privatehelper is now shared as
expressionContainsArrayJoininsrc/Interpreters/ExpressionContainsArrayJoin.h, including its descent into a SQL UDF body and itsskipping of nested subqueries, which have their own scope.
TTLValidationMode::Attachis the one mode that still has to accept such a TTL: a rejection whilestored metadata is being read fails the whole load rather than the one table, so the server would not
start after an upgrade and a replica could not read the metadata another replica wrote. That leaves a
TTL stored before this check existed loadable — and it used to still execute. On a table loaded from
such metadata,
OPTIMIZE TABLE ... FINALdeleted a row whose own TTL was a day in the future alongwith an earlier expired one; an
INSERTand a background TTL merge do the same. So the check runsagain in
TTLDescription::buildExpressionandbuildWhereExpression, which are the only way aconsumer turns a stored description back into a runnable expression (
TTLTransform,TTLCalcTransform,TTLDeleteFilterTransformandMergeTreeDataWriter::updateTTLall go throughthem). The silent row loss becomes a failed
INSERTor TTL merge; the table still attaches, soALTER TABLE ... REMOVE TTLor aMODIFY TTLto a sane expression repairs it. AGROUP BY ... SETassignment is stored as prebuilt
ExpressionActionsrather than as an AST, so it is screened throughthe built expression, in
buildExpression, which setting up theTTLAggregationAlgorithmthatexecutes it also goes through.
Test
05073_ttl_array_join_rejectedcoversCREATE,ALTER MODIFY TTL, a column TTL, theWHEREform, the
unnest/UNNESTalias with and withoutnormalize_function_names, theallow_suspicious_ttl_expressionsescape hatch, and that an ordinary TTL still expires exactly whatit should. Test
05099_ttl_array_join_stored_metadatacovers the stored form:clickhouse-localovera data directory creates the table with an ordinary TTL, its stored definition is then edited into the
form an older server would have written, and the next start loads it — the table attaches, the
INSERTand the TTL merge fail withBAD_TTL_EXPRESSIONinstead of dropping the row that is notexpired, and
ALTER TABLE ... REMOVE TTLrepairs the table.Workflow [PR]
Sync PR [sync-upstream/pr/117266]