Skip to content

Commit 973c382

Browse files
Backport #88605 to 25.3: Fix potential crash caused by concurrent mutation of underlying const PREWHERE columns
1 parent 14b26d9 commit 973c382

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

src/Storages/MergeTree/MergeTreeReadTask.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,12 @@ MergeTreeReadTask::BlockAndProgress MergeTreeReadTask::read()
198198
if (read_result.num_rows != 0)
199199
{
200200
for (const auto & column : read_result.columns)
201-
column->assumeMutableRef().shrinkToFit();
201+
{
202+
/// We may have columns that has other references, usually it is a constant column that has been created during analysis
203+
/// (that will not be const here anymore, i.e. after materialize()), and we do not need to shrink it anyway.
204+
if (column->use_count() == 1)
205+
column->assumeMutableRef().shrinkToFit();
206+
}
202207
block = sample_block.cloneWithColumns(read_result.columns);
203208
}
204209

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
1
2+
2
3+
3
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
DROP TABLE IF EXISTS const_node;
2+
CREATE TABLE const_node (`v` Nullable(UInt8)) ENGINE = MergeTree ORDER BY tuple();
3+
SYSTEM STOP MERGES const_node;
4+
INSERT INTO const_node VALUES (1);
5+
INSERT INTO const_node VALUES (2);
6+
INSERT INTO const_node VALUES (3);
7+
-- Here we have condition with a constant "materialize(255)", for which convertToFullColumnIfConst() will return underlying column w/o copying,
8+
-- and later shrinkToFit() will be called from multiple threads on this column, and leads to UB
9+
SELECT v FROM const_node PREWHERE and(materialize(255), *) ORDER BY v;

0 commit comments

Comments
 (0)