Skip to content

Commit

Permalink
Merge pull request #55565 from ClickHouse/backport/23.3/55062
Browse files Browse the repository at this point in the history
Backport #55062 to 23.3: Prevent attaching parts from tables with different projections or indices
  • Loading branch information
robot-clickhouse-ci-1 committed Oct 12, 2023
2 parents c652cd6 + 419c497 commit c8f4ba5
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/en/sql-reference/statements/alter/partition.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ For the query to run successfully, the following conditions must be met:

- Both tables must have the same structure.
- Both tables must have the same partition key, the same order by key and the same primary key.
- Both tables must have the same indices and projections.
- Both tables must have the same storage policy (a disk where the partition is stored should be available for both tables).

## REPLACE PARTITION
Expand All @@ -123,6 +124,7 @@ For the query to run successfully, the following conditions must be met:

- Both tables must have the same structure.
- Both tables must have the same partition key, the same order by key and the same primary key.
- Both tables must have the same indices and projections.
- Both tables must have the same storage policy (a disk where the partition is stored should be available for both tables).

## MOVE PARTITION TO TABLE
Expand All @@ -137,6 +139,7 @@ For the query to run successfully, the following conditions must be met:

- Both tables must have the same structure.
- Both tables must have the same partition key, the same order by key and the same primary key.
- Both tables must have the same indices and projections.
- Both tables must have the same storage policy (a disk where the partition is stored should be available for both tables).
- Both tables must be the same engine family (replicated or non-replicated).

Expand Down
22 changes: 22 additions & 0 deletions src/Storages/MergeTree/MergeTreeData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7019,6 +7019,28 @@ MergeTreeData & MergeTreeData::checkStructureAndGetMergeTreeData(IStorage & sour
if (query_to_string(my_snapshot->getPrimaryKeyAST()) != query_to_string(src_snapshot->getPrimaryKeyAST()))
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Tables have different primary key");

const auto check_definitions = [](const auto & my_descriptions, const auto & src_descriptions)
{
if (my_descriptions.size() != src_descriptions.size())
return false;

std::unordered_set<std::string> my_query_strings;
for (const auto & description : my_descriptions)
my_query_strings.insert(queryToString(description.definition_ast));

for (const auto & src_description : src_descriptions)
if (!my_query_strings.contains(queryToString(src_description.definition_ast)))
return false;

return true;
};

if (!check_definitions(my_snapshot->getSecondaryIndices(), src_snapshot->getSecondaryIndices()))
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Tables have different secondary indices");

if (!check_definitions(my_snapshot->getProjections(), src_snapshot->getProjections()))
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Tables have different projections");

return *src_data;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
1 1
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
-- test different index type
CREATE TABLE attach_partition_t1 (
a UInt32,
b String,
INDEX bf b TYPE tokenbf_v1(8192, 3, 0) GRANULARITY 1
)
ENGINE = MergeTree
ORDER BY a;

INSERT INTO attach_partition_t1 SELECT number, toString(number) FROM numbers(10);

CREATE TABLE attach_partition_t2 (
a UInt32,
b String,
INDEX bf b TYPE bloom_filter GRANULARITY 1
)
ENGINE = MergeTree
ORDER BY a;

ALTER TABLE attach_partition_t2 ATTACH PARTITION tuple() FROM attach_partition_t1; -- { serverError 36 }

-- test different projection name
CREATE TABLE attach_partition_t3 (
a UInt32,
b String,
PROJECTION proj
(
SELECT
b,
sum(a)
GROUP BY b
)
)
ENGINE = MergeTree
ORDER BY a;

INSERT INTO attach_partition_t3 SELECT number, toString(number) FROM numbers(10);

CREATE TABLE attach_partition_t4 (
a UInt32,
b String,
PROJECTION differently_named_proj
(
SELECT
b,
sum(a)
GROUP BY b
)
)
ENGINE = MergeTree
ORDER BY a;

ALTER TABLE attach_partition_t4 ATTACH PARTITION tuple() FROM attach_partition_t3; -- { serverError 36 }

-- check attach with same index and projection
CREATE TABLE attach_partition_t5 (
a UInt32,
b String,
PROJECTION proj
(
SELECT
b,
sum(a)
GROUP BY b
)
)
ENGINE = MergeTree
ORDER BY a;

INSERT INTO attach_partition_t5 SELECT number, toString(number) FROM numbers(10);


CREATE TABLE attach_partition_t6 (
a UInt32,
b String,
PROJECTION proj
(
SELECT
b,
sum(a)
GROUP BY b
)
)
ENGINE = MergeTree
ORDER BY a;

ALTER TABLE attach_partition_t6 ATTACH PARTITION tuple() FROM attach_partition_t5;

SELECT * FROM attach_partition_t6 WHERE b = '1';
SELECT b, sum(a) FROM attach_partition_t6 GROUP BY b ORDER BY b;

0 comments on commit c8f4ba5

Please sign in to comment.