Skip to content

Commit 9e1bf73

Browse files
Backport ClickHouse#84158 to 25.6: Fix crash during lighweight mutation with projections
1 parent 6d0e416 commit 9e1bf73

File tree

4 files changed

+48
-12
lines changed

4 files changed

+48
-12
lines changed

src/Storages/MergeTree/MutateTask.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,6 +1277,10 @@ bool PartMergerWriter::mutateOriginalPartAndPrepareProjections()
12771277
ProfileEventTimeIncrement<Microseconds> projection_watch(ProfileEvents::MutateTaskProjectionsCalculationMicroseconds);
12781278
Block block_to_squash = ctx->projections_to_build[i]->calculate(cur_block, ctx->context);
12791279

1280+
/// Everything is deleted by lighweight delete
1281+
if (block_to_squash.rows() == 0)
1282+
continue;
1283+
12801284
projection_squashes[i].setHeader(block_to_squash.cloneEmpty());
12811285
squashed_chunk = Squashing::squash(projection_squashes[i].add({block_to_squash.getColumns(), block_to_squash.rows()}));
12821286
}

src/Storages/ProjectionsDescription.cpp

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ class ProjectionDataSink : public ISink
167167

168168
String getName() const override { return "ProjectionDataSink"; }
169169

170+
/// Accumulated data can be empty if DELETE query deleted all the rows from block
171+
bool isAccumulatedSomething() const { return accumulated_chunk && accumulated_chunk.getNumRows() > 0; }
172+
170173
Columns detachAccumulatedColumns() { return accumulated_chunk.detachColumns(); }
171174

172175
protected:
@@ -499,19 +502,22 @@ Block ProjectionDescription::calculate(const Block & block, ContextPtr context,
499502
pipeline.complete(sink);
500503
CompletedPipelineExecutor executor(pipeline);
501504
executor.execute();
502-
auto projection_block = sink->getPort().getHeader().cloneWithColumns(sink->detachAccumulatedColumns());
503-
chassert(projection_block.rows() > 0);
504-
505-
/// Rename parent _part_offset to _parent_part_offset column
506-
if (with_parent_part_offset)
505+
Block projection_block;
506+
if (sink->isAccumulatedSomething())
507507
{
508-
chassert(projection_block.has("_part_offset"));
509-
chassert(!projection_block.has("_parent_part_offset"));
510-
511-
auto new_column = projection_block.getByName("_part_offset");
512-
new_column.name = "_parent_part_offset";
513-
projection_block.erase("_part_offset");
514-
projection_block.insert(std::move(new_column));
508+
projection_block = sink->getPort().getHeader().cloneWithColumns(sink->detachAccumulatedColumns());
509+
510+
/// Rename parent _part_offset to _parent_part_offset column
511+
if (with_parent_part_offset)
512+
{
513+
chassert(projection_block.has("_part_offset"));
514+
chassert(!projection_block.has("_parent_part_offset"));
515+
516+
auto new_column = projection_block.getByName("_part_offset");
517+
new_column.name = "_parent_part_offset";
518+
projection_block.erase("_part_offset");
519+
projection_block.insert(std::move(new_column));
520+
}
515521
}
516522

517523
return projection_block;

tests/queries/0_stateless/03571_lwd_and_projections.reference

Whitespace-only changes.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
DROP TABLE IF EXISTS weird_projections;
2+
3+
CREATE TABLE weird_projections(
4+
`account_id` UInt64,
5+
`user_id` String,
6+
PROJECTION events_by_day_proj
7+
(
8+
SELECT
9+
account_id,
10+
countDistinct(user_id) AS total_users
11+
GROUP BY
12+
account_id
13+
)
14+
)
15+
ENGINE = ReplicatedMergeTree('/clickhouse/{database}/tables/test', '1')
16+
ORDER BY (account_id)
17+
SETTINGS index_granularity = 8192, lightweight_mutation_projection_mode = 'rebuild';
18+
19+
INSERT INTO weird_projections SELECT 134 as account_id, toString(account_id) as user_id FROM numbers(10000);
20+
INSERT INTO weird_projections SELECT 132 as account_id, toString(account_id) as user_id FROM numbers(10000);
21+
22+
OPTIMIZE TABLE weird_projections FINAL;
23+
24+
DELETE FROM weird_projections WHERE account_id = 134;
25+
26+
DROP TABLE IF EXISTS weird_projections;

0 commit comments

Comments
 (0)