Repository navigation
Use COW-safe IColumn::mutate for in-place column mutation on read/build paths - #107457
Conversation
…uild paths Several places obtained a mutable column via `assumeMutable`/`assumeMutableRef` (which bypass copy-on-write with a bare `const_cast`) and then mutated or reallocated it in place. When such a column happens to be shared (`use_count > 1`), this corrupts or frees data that may still be referenced elsewhere. Replace these with `IColumn::mutate(std::move(...))`, which clones only when the column is shared and is a no-op (same cost) when it is uniquely owned, and write the mutated result back into its holder. The common, uniquely-owned path is unchanged; the shared path becomes safe. Converted sites: MergeTree readers (`MergeTreeReaderTextIndex`, `MergeTreeReaderCompact`, `MergeTreeReaderIndex`), the `MergeTreeIndexSet` granule builder, the virtual-column and minmax block builders (`VirtualColumnUtils`, `MergeTreeData::getBlockWithVirtualsForFilter`), `SerializationObjectCombinedPath` deserialization, the hash-join rerange builder (`HashJoin::tryRerangeRightTableDataImpl`), `StorageExecutable` result accumulation, and the Prometheus remote-write id-column accumulator. Intentionally left as-is: the copy-on-write `create(const ColumnPtr &)` factories, the patch-parts in-place write-back (`applyPatches`), the substreams-cache offsets grow-in-place (`SerializationSparse`), and the in-place column-algorithm subcolumn idioms - these are deliberately shared and converting them would force a clone (a regression) or change semantics. `ProtobufSerializer` is left for a follow-up that makes it operate on mutable columns directly. Related: #107451 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Workflow [PR], commit [b48f721] Summary: ✅
AI ReviewSummaryThis PR replaces several in-place Missing context / blind spots
Final VerdictStatus: ✅ Approve |
The previous commit copied the reusable scratch column out of `block_for_reading` into a local `ColumnPtr` before deserializing into it. `IColumn::mutate(std::move(column))` for the `popBack` then saw the extra owner still held by `block_for_reading`, cloned the filled column, and the `popBack` reset landed only on the throwaway clone. The scratch column kept the just-read rows, so the next granule started with stale rows and `insertRangeFrom` copied the previous granule's values again, corrupting the `set` skip index granules (and producing `Array elements column is longer than the last offset` for array set indexes). Bind `column` to the holder by reference instead of copying. With a single owner, `IColumn::mutate` is a genuine no-op (no clone, no regression) and the write-back resets the reusable scratch column for the next granule, exactly preserving the previous in-place behaviour. Fixes the Fast test failures: 00965_set_index_string_functions, 00997_set_index_array, 01576_alias_column_rewrite, 02346_exclude_materialize_skip_indexes_on_insert/merge, 03033_set_index_in, 03356_array_join_subcolumns_indexes, 03374_indexes_with_literals, 03374_indexes_with_trivial_cast, 03734_has_function_key_condition_skip_index, 04027_set_index_or_non_indexed_column, 04143/04144_skip_index_after_alter_modify_column, 04201_set_index_in_subquery_disabled_fallback, 04290_set_index_is_true_false_unknown. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
`tryRerangeRightTableDataImpl` called `IColumn::mutate(std::move(...))` inside the per-row, per-column append loop. Even in the common uniquely-owned case, `mutate` is not a free alias for `assumeMutableRef`: it performs the COW refcount transition and recursively deep-mutates subcolumns on every call. Per single `insertFrom` cell that contradicts the sweep's "same cost" contract and can dominate the reranging hot path for large right tables. Detach all destination columns once into `MutableColumns` before the row loop (COW-safe: clones only if shared), append every row through those mutable handles, then move them back. Correctness is unchanged: the source columns (`it->columns_info`) are a distinct accumulator from the destination, so there is no aliasing across the hoist. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
@groeneai, fix |
|
@alexey-milovidov on it — investigating |
…lumn filtering `getFilterByPathAndFileIndexes` calls `addPathAndFileToVirtualColumns` once per listed file/object, and the COW-safe rewrite made that helper detach `_path`, `_file`, the hive columns, and `_idx` via `IColumn::mutate` for every single appended row. `IColumn::mutate` recursively re-wraps subcolumns through `forEachMutableSubcolumn` even when the top-level column is uniquely owned, so `LowCardinality` virtuals such as `_path`/`_file` paid repeated copy-on-write plumbing on large object-storage listings before pruning. Detach the block columns once via `Block::mutateColumns` around the `paths` loop, append every row through the resulting mutable holders, and reattach them once with `Block::setColumns` before `filterBlockWithExpression`. The per-row inserts are unchanged; only the per-row `mutate` is removed. Addresses review feedback on #107457 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
@alexey-milovidov done: #107812. Root cause was randomized |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 92/105 (87.62%) | Lost baseline coverage (was covered on master, now uncovered in this PR): 6 line(s) · Uncovered code |
Proactive copy-on-write (COW) safety sweep, following the same fix as #107451.
Several places obtained a mutable column via
assumeMutable/assumeMutableRef(which bypass copy-on-write with a bareconst_cast) and then mutated or reallocated it in place. When such a column happens to be shared (use_count > 1), this corrupts or frees data that may still be referenced elsewhere — the class of bug fixed in #107451.These sites are replaced with
IColumn::mutate(std::move(...)), which clones only when the column is shared and is a no-op (same cost) when it is uniquely owned, writing the mutated result back into its holder. The common uniquely-owned path is unchanged; the shared path becomes safe.Converted sites:
MergeTreeReaderTextIndex,MergeTreeReaderCompact,MergeTreeReaderIndexMergeTreeIndexSetgranule builderVirtualColumnUtils,MergeTreeData::getBlockWithVirtualsForFilterSerializationObjectCombinedPathdeserializationHashJoin::tryRerangeRightTableDataImplStorageExecutableresult accumulationIntentionally left as-is (converting would force a clone — a regression — or change semantics, because the column is deliberately shared and reallocation does not happen):
create(const ColumnPtr &)factoriesapplyPatches)SerializationSparse)ColumnArray/ColumnObject/ColumnVariant/…)ProtobufSerializeris left for a follow-up that makes it operate on mutable columns directly (it keeps a persistently sharedColumnPtralias to the row format's columns, so it needs a small refactor rather than a per-callmutate).Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Not required (internal copy-on-write safety hardening, no user-facing behavior change).
Documentation entry for user-facing changes
Version info
26.6.1.1002