Skip to content

Commit

Permalink
Merge pull request #55472 from ClickHouse/cherrypick/23.8/c911c8daf4b…
Browse files Browse the repository at this point in the history
…d626315d6b67f35ed7ea0f6c476ce

Cherry pick #55062 to 23.8: Prevent attaching parts from tables with different projections or indices
  • Loading branch information
robot-ch-test-poll3 committed Oct 10, 2023
2 parents 1c9f0f1 + c911c8d commit 4b96504
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 @@ -113,6 +113,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.

## REPLACE PARTITION
Expand All @@ -132,6 +133,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.

## MOVE PARTITION TO TABLE
Expand All @@ -146,6 +148,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.
- 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 @@ -7387,6 +7387,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 4b96504

Please sign in to comment.