Skip to content

Commit b939a1e

Browse files
Backport #93233 to 25.12: Remove unused columns when rebuilding projection on merge.
1 parent 156a87e commit b939a1e

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

src/Storages/MergeTree/MergeTask.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,11 @@ void MergeTask::ExecuteAndFinalizeHorizontalPart::calculateProjections(const Blo
969969
for (size_t i = 0, size = global_ctx->projections_to_rebuild.size(); i < size; ++i)
970970
{
971971
const auto & projection = *global_ctx->projections_to_rebuild[i];
972-
Block block_to_squash = projection.calculate(block, global_ctx->context);
972+
Block block_with_required_columns;
973+
for (const auto & name : projection.getRequiredColumns())
974+
if (name != "_part_offset")
975+
block_with_required_columns.insert(block.getByName(name));
976+
Block block_to_squash = projection.calculate(block_with_required_columns, global_ctx->context);
973977
/// Avoid replacing the projection squash header if nothing was generated (it used to return an empty block)
974978
if (block_to_squash.rows() == 0)
975979
return;

tests/integration/test_projection_rebuild_with_required_columns/__init__.py

Whitespace-only changes.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<clickhouse>
2+
<profiles>
3+
<default>
4+
<min_insert_block_size_bytes>1048576</min_insert_block_size_bytes>
5+
<min_insert_block_size_rows>10000</min_insert_block_size_rows>
6+
</default>
7+
</profiles>
8+
</clickhouse>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import pytest
2+
3+
from helpers.client import QueryRuntimeException
4+
from helpers.cluster import ClickHouseCluster
5+
6+
cluster = ClickHouseCluster(__file__)
7+
node1 = cluster.add_instance("node1", user_configs=["configs/insert_limits.xml"])
8+
9+
10+
@pytest.fixture(scope="module")
11+
def started_cluster():
12+
try:
13+
cluster.start()
14+
15+
yield cluster
16+
17+
finally:
18+
cluster.shutdown()
19+
20+
21+
def test_projection_rebuild_uses_only_required_columns(started_cluster):
22+
node1.query("create table tab (x UInt64, y UInt64, data String codec(NONE), v UInt8, projection p (select _part_offset order by y)) engine = ReplacingMergeTree(v) order by x settings allow_part_offset_column_in_projections=1, deduplicate_merge_projection_mode='rebuild';")
23+
node1.query("insert into tab select number, number, rightPad('', 100, 'a'), 0 from numbers(30000);")
24+
node1.query("optimize table tab final settings mutations_sync=2, alter_sync=2;")
25+
node1.query("system flush logs;")
26+
27+
uuid = node1.query("select uuid from system.tables where table = 'tab';").strip()
28+
cnt = node1.query("select count() from system.text_log where query_id = '{}::all_1_1_2' and message like '%Reading%from part p_%from the beginning of the part%'".format(uuid))
29+
assert (cnt == '2\n')
30+

0 commit comments

Comments
 (0)