Repository navigation
Fix rows never deleted when two TTL DELETE WHERE rules share a time expression - #113384
Conversation
…xpression Per-part rows-where TTL infos are stored in a map keyed by the TTL time expression's result_column (MergeTreeDataPartTTLInfo.h:36,49). That key comes from the time expression alone (TTLDescription.cpp:1285) while the WHERE clause has a separate where_result_column (:1310), so two or more DELETE WHERE rules sharing a time expression share one map slot. TTLTransform::finalize resets data_part->ttl_infos and then calls finalize on each algorithm in declaration order. TTLDeleteAlgorithm::finalize wrote the shared slot with an unconditional assignment, so the last rule won: if that rule's WHERE matched no surviving row its new_ttl_info is empty and the earlier rule's valid min/max was discarded. The loss is durable and silent. part_min_ttl is not stored in ttl.txt and is recomputed from the map on load, updatePartMinMaxTTL ignores a zero min, and ITTLMergeSelector::findCenters skips any part whose TTL is 0, so no further TTL merge is ever selected for that part and the expired rows are retained indefinitely with no error and no log line. Merge into the shared slot with MergeTreeDataPartTTLInfo::update instead of overwriting it. That takes the smaller nonzero min, the larger max and ANDs ttl_finished, so the slot means "the earliest and latest still-live matching row across all rules on this time expression", which is what the merge selector needs. It is the same aggregation MergeTreeDataPartTTLInfos::update already applies to this map when parts combine. Because finalize starts from a default-constructed slot, a table with one rule per time expression gets a value identical to the old assignment. The per-time-expression key collision is deliberately left alone: the key is serialized verbatim into ttl.txt and the map is hashed for checksums (MergeTreeDataPartTTLInfo.h:35), so re-keying per rule would be an on-disk format change requiring upgrade and downgrade paths. Merging restores correct scheduling, which is the user-visible defect. The behaviour regressed in 25.9 with 67a3166 ("fix infinite recalculation of TTL with WHERE"), which added an if (where_filter_passed) guard around new_ttl_info.update(cur_ttl). Before that, rows failing the WHERE still updated the shared slot, so it always held a WHERE-ignoring but nonzero range and scheduling survived by accident. That commit is correct for its own purpose and its test 03622_ttl_infos_where stays green, so this is not a revert. Verified against a pristine-master build that the new test fails without the fix and passes with it, that ttl.txt keeps the same layout (only the value differs), that the result no longer depends on the order the rules are declared in, and that the infinite TTL recalculation 67a3166 fixed is not reintroduced. Closes: ClickHouse#113116
Two arms of the test did not pin what the change claims. The reordered arm declared the live rule last, so the pre-fix unconditional assignment also wrote that rule's valid range: the arm produced identical output with and without the fix. It now declares the live rule in the middle, so an empty info is finalized after it and the old assignment clobbers again. No arm distinguished merging from a weaker alternative that assigns only when the incoming info is initialized, because every arm had exactly one rule contributing a nonzero range. The new t_two_live arm gives two rules a live row each at different timestamps, so the slot's min must come from one rule and its max from the other, which only a merge produces. Measured against two mutants, both 5/5 deterministic. Restoring the assignment reddens merged, reloaded, materialized, reordered and two live. The assign-if-initialized variant reddens two live, and also control, because it never creates the uninitialized second slot. Test-only change; TTLDeleteAlgorithm is untouched.
Internal second-model review (2 rounds, clean)Two independent review rounds against a second model, plus my own cold review of the resulting Round 1 (sha
|
| arm | fix | restore assignment | assign-if-initialized |
|---|---|---|---|
merged / reloaded / materialized |
2099 / 2099 | 1970 / 1970 | 2099 / 2099 |
reordered (repaired) |
2099 / 2099 | 1970 / 1970 | 2099 / 2099 |
two live (new) |
2099 / 2100 | 2100 / 2100 | 2100 / 2100 |
control (distinct expressions) |
2 slots | 2 slots | 1 slot |
Before the repair, reordered reddened neither mutant. two live is the only arm that reddens
both. The control arm also turned out to redden the second mutant, because that variant never
creates the legitimately-empty second slot.
Round 2 (sha 6924e21a) - 0 findings
Second-model re-review of the full change: 0 findings. My own cold review: 0 blockers,
0 majors, 0 nits.
💡 During independent carrier enumeration I raised, then refuted, one candidate gap: MergeTask
has a second TTL-delete path (TTLDeleteFilterStep, the vertical-merge optimization) which
pre-filters rows and writes no TTL info. It is not a missed carrier - MergeTask.cpp:3320-3326
adds the TTLStep in both merge algorithms, so the fixed finalize runs either way. CI
randomizes vertical_merge_algorithm_min_rows_to_activate / min_columns_to_activate and
min_bytes_for_wide_part, so the 50/50 run crossed both algorithms.
💡 One nit declined: the validation sentence in this description names restart, MATERIALIZE TTL
and reversed rule order but not the new two-live-rules arm. The sentence is accurate as written,
and the contract that arm pins is already stated in the changelog entry and above.
Also verified
Serialization is unchanged in both directions - write() emits finished() as
value_or(false), so the std::optional merge inside update is not observable in ttl.txt,
and the map key is untouched, so no format versioning applies. 03622_ttl_infos_where, the test
added by the commit that changed this behaviour in 25.9, still passes, including its
CANNOT_ASSIGN_OPTIMIZE assertion - so this is not a revert of it.
Test is parallel-safe (no no-parallel, scoped to currentDatabase()), does not touch part
directories, and is a single file.
Pre-PR validation gate (a-i)
Regression runs: the |
|
cc @Michicosun, could you review this? Two TTL DELETE WHERE rules sharing a time expression share one rows_where_ttl slot, and finalize assigned into it rather than merging, so an empty info from the last rule erased the earlier rule's range and the part was never selected for a TTL merge again. |
|
Workflow [PR], commit [6924e21] Summary: ❌
AI ReviewSummaryThis PR fixes the Final Verdict✅ No blocking issues found. LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 3/3 (100.00%) · Uncovered code |
Build profile diff (arm_release)Comparing Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Object file sizes
|
| Object file | Master | PR | Δ |
|---|---|---|---|
src/CMakeFiles/dbms.dir/Processors/Formats/Impl/Parquet/GeoFilter.cpp.o |
new | 860.64 KiB | +860.64 KiB |
src/CMakeFiles/dbms.dir/Storages/MergeTree/SharedPartColumns.cpp.o |
new | 294.32 KiB | +294.32 KiB |
src/CMakeFiles/dbms.dir/Processors/Formats/Impl/Parquet/Reader.cpp.o |
558.56 KiB | 634.53 KiB | +75.97 KiB (+13.60%) |
src/CMakeFiles/dbms.dir/Storages/MergeTree/IMergeTreeDataPart.cpp.o |
1.38 MiB | 1.33 MiB | -46.27 KiB (-3.28%) |
src/CMakeFiles/dbms.dir/Interpreters/InterserverIOHandler.cpp.o |
new | 29.52 KiB | +29.52 KiB |
src/CMakeFiles/dbms.dir/Storages/MergeTree/ColumnsSubstreams.cpp.o |
132.58 KiB | 154.18 KiB | +21.60 KiB (+16.29%) |
737 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.
Compile time of recompiled translation units
7 translation units recompiled, 8 s compile time in total, 7 of them have a recent master baseline.
CI finish ledger - 6924e21Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task whose fixing-PR link will be posted here when it opens. Only
Session id: cron:our-pr-ci-monitor:20260805-033000 |
458264b
|
Hi — this looks like a backport candidate for |
|
Thanks - I checked this myself rather than relying on the runs above, and the branch list is right, with one correction to how it is best requested. I ran this PR's own merged regression test against the official per-release binaries and scored only the fix-dependent lines (
So your four branches are correct. Worth spelling out why 25.8 is excluded, because a whole-file diff would wrongly flag it: 25.8 does differ from the master reference, but only on the One correction on the request itself. A
So a single I also checked the port is mechanical rather than assuming it. Simulating the automation's cherry-pick against each of the four branches gives rc=0 with no conflicts and exactly 3 files ( @PedroTadim, since you merged this one - could you apply |
Closes: #113116
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed rows never being deleted when a
MergeTreetable has two or moreTTL ... DELETE WHERErules whose time expression is identical and which differ only in theWHEREcondition. After a TTL delete merge the part'srows_where_ttl_infowas reset to zero, so no further TTL merge was ever scheduled for it and the expired rows were retained indefinitely with no error or log message. Closes #113116.Description
Per-part rows-where TTL infos live in a map keyed by the TTL time expression's
result_column, not by the rule, so twoDELETE WHERErules sharing a time expression share one slot.TTLDeleteAlgorithm::finalizewrote that slot with an unconditional assignment, so the last rule finalized won. If that rule'sWHEREmatched no surviving row its info is empty and the earlier rule's valid min/max was discarded.A zero min is durable:
part_min_ttlis not stored inttl.txtand is recomputed from the map on load,updatePartMinMaxTTLignores a zero min, andITTLMergeSelector::findCentersskips any part whose TTL is 0, so the part is never selected for a TTL merge again.The change makes
finalizemerge into the shared slot withMergeTreeDataPartTTLInfo::updateinstead of overwriting it, so the slot holds the earliest and latest still-live matching row across all rules on that time expression. This is the same aggregationMergeTreeDataPartTTLInfos::updatealready applies to this map when parts combine. With one rule per time expression the merged value equals the old assignment, so nothing else changes.The key collision itself is left alone: the key is serialized verbatim into
ttl.txtand hashed for checksums, so re-keying per rule would be an on-disk format change needing upgrade and downgrade paths. Merging already restores correct scheduling, which is the user-visible defect.The behaviour regressed in 25.9 with 67a3166, which correctly stopped rows failing the
WHEREfrom updating the TTL info; before that the shared slot held aWHERE-ignoring but nonzero range, so scheduling survived by accident. That commit's test03622_ttl_infos_wherestill passes, so this is not a revert.Validated with a new stateless test that fails on unpatched master and passes with the fix, 50/50 under CI randomization, and covering restart,
MATERIALIZE TTLand reversed rule order.