diff --git a/docs/en/sql-reference/statements/alter/partition.md b/docs/en/sql-reference/statements/alter/partition.md index 52e99d93109..ce0bc1ea528 100644 --- a/docs/en/sql-reference/statements/alter/partition.md +++ b/docs/en/sql-reference/statements/alter/partition.md @@ -103,7 +103,11 @@ ALTER TABLE table2 [ON CLUSTER cluster] ATTACH PARTITION partition_expr FROM tab ``` This query copies the data partition from `table1` to `table2`. -Note that data will be deleted neither from `table1` nor from `table2`. + +Note that: + +- Data will be deleted neither from `table1` nor from `table2`. +- `table1` may be a temporary table. For the query to run successfully, the following conditions must be met: @@ -117,7 +121,12 @@ For the query to run successfully, the following conditions must be met: ALTER TABLE table2 [ON CLUSTER cluster] REPLACE PARTITION partition_expr FROM table1 ``` -This query copies the data partition from the `table1` to `table2` and replaces existing partition in the `table2`. Note that data won’t be deleted from `table1`. +This query copies the data partition from the `table1` to `table2` and replaces existing partition in the `table2`. + +Note that: + +- Data won’t be deleted from `table1`. +- `table1` may be a temporary table. For the query to run successfully, the following conditions must be met: diff --git a/docs/en/sql-reference/table-functions/dictionary.md b/docs/en/sql-reference/table-functions/dictionary.md index c4bdde4dce2..73d5039a64b 100644 --- a/docs/en/sql-reference/table-functions/dictionary.md +++ b/docs/en/sql-reference/table-functions/dictionary.md @@ -1,7 +1,7 @@ --- slug: /en/sql-reference/table-functions/dictionary sidebar_position: 54 -sidebar_label: dictionary function +sidebar_label: dictionary title: dictionary --- diff --git a/docs/ru/sql-reference/statements/alter/partition.md b/docs/ru/sql-reference/statements/alter/partition.md index 95d02c062bd..90688c9ece2 100644 --- a/docs/ru/sql-reference/statements/alter/partition.md +++ b/docs/ru/sql-reference/statements/alter/partition.md @@ -102,7 +102,11 @@ ALTER TABLE table2 [ON CLUSTER cluster] ATTACH PARTITION partition_expr FROM tab ``` Копирует партицию из таблицы `table1` в таблицу `table2`. -Обратите внимание, что данные не удаляются ни из `table1`, ни из `table2`. + +Обратите внимание, что: + +- Данные не удаляются ни из `table1`, ни из `table2`. +- `table1` может быть временной таблицей. Следует иметь в виду: @@ -118,7 +122,12 @@ ALTER TABLE table2 [ON CLUSTER cluster] ATTACH PARTITION partition_expr FROM tab ALTER TABLE table2 [ON CLUSTER cluster] REPLACE PARTITION partition_expr FROM table1 ``` -Копирует партицию из таблицы `table1` в таблицу `table2` с заменой существующих данных в `table2`. Данные из `table1` не удаляются. +Копирует партицию из таблицы `table1` в таблицу `table2` с заменой существующих данных в `table2`. + +Обратите внимание, что: + +- Данные из `table1` не удаляются. +- `table1` может быть временной таблицей. Следует иметь в виду: diff --git a/src/Storages/MergeTree/MergeTreeData.cpp b/src/Storages/MergeTree/MergeTreeData.cpp index a2a3348eb42..d200d8bb19e 100644 --- a/src/Storages/MergeTree/MergeTreeData.cpp +++ b/src/Storages/MergeTree/MergeTreeData.cpp @@ -4985,8 +4985,8 @@ Pipe MergeTreeData::alterPartition( if (command.replace) checkPartitionCanBeDropped(command.partition, query_context); - String from_database = query_context->resolveDatabase(command.from_database); - auto from_storage = DatabaseCatalog::instance().getTable({from_database, command.from_table}, query_context); + auto resolved = query_context->resolveStorageID({command.from_database, command.from_table}); + auto from_storage = DatabaseCatalog::instance().getTable(resolved, query_context); auto * from_storage_merge_tree = dynamic_cast(from_storage.get()); if (!from_storage_merge_tree) diff --git a/tests/queries/0_stateless/02731_replace_partition_from_temporary_table.reference b/tests/queries/0_stateless/02731_replace_partition_from_temporary_table.reference new file mode 100644 index 00000000000..ced682dd94f --- /dev/null +++ b/tests/queries/0_stateless/02731_replace_partition_from_temporary_table.reference @@ -0,0 +1,9 @@ +Initial +6 12 +6 12 +REPLACE simple +6 10 +6 10 +ATTACH FROM +6 10 +6 10 diff --git a/tests/queries/0_stateless/02731_replace_partition_from_temporary_table.sql b/tests/queries/0_stateless/02731_replace_partition_from_temporary_table.sql new file mode 100644 index 00000000000..b2f31230dfb --- /dev/null +++ b/tests/queries/0_stateless/02731_replace_partition_from_temporary_table.sql @@ -0,0 +1,48 @@ +-- Tags: no-replicated-database + +DROP TEMPORARY TABLE IF EXISTS src; +DROP TABLE IF EXISTS dst; +DROP TABLE IF EXISTS rdst; + +CREATE TEMPORARY TABLE src (p UInt64, k String, d UInt64) ENGINE = MergeTree PARTITION BY p ORDER BY k; +CREATE TABLE dst (p UInt64, k String, d UInt64) ENGINE = MergeTree PARTITION BY p ORDER BY k; +CREATE TABLE rdst (p UInt64, k String, d UInt64) ENGINE = ReplicatedMergeTree('/clickhouse/tables/{database}/test_alter_attach_00626_rdst', 'r1') PARTITION BY p ORDER BY k; + +SELECT 'Initial'; +INSERT INTO src VALUES (0, '0', 1); +INSERT INTO src VALUES (1, '0', 1); +INSERT INTO src VALUES (1, '1', 1); +INSERT INTO src VALUES (2, '0', 1); +INSERT INTO src VALUES (3, '0', 1); +INSERT INTO src VALUES (3, '1', 1); + +INSERT INTO dst VALUES (0, '1', 2); +INSERT INTO dst VALUES (1, '1', 2), (1, '2', 2); +INSERT INTO dst VALUES (2, '1', 2); +INSERT INTO dst VALUES (3, '1', 2), (3, '2', 2); + +INSERT INTO rdst VALUES (0, '1', 2); +INSERT INTO rdst VALUES (1, '1', 2), (1, '2', 2); +INSERT INTO rdst VALUES (2, '1', 2); +INSERT INTO rdst VALUES (3, '1', 2), (3, '2', 2); + +SELECT count(), sum(d) FROM dst; +SELECT count(), sum(d) FROM rdst; + +SELECT 'REPLACE simple'; +ALTER TABLE dst REPLACE PARTITION 1 FROM src; +SELECT count(), sum(d) FROM dst; +ALTER TABLE rdst REPLACE PARTITION 3 FROM src; +SELECT count(), sum(d) FROM rdst; + +SELECT 'ATTACH FROM'; +ALTER TABLE dst DROP PARTITION 1; +ALTER TABLE dst ATTACH PARTITION 1 FROM src; +SELECT count(), sum(d) FROM dst; +ALTER TABLE rdst DROP PARTITION 3; +ALTER TABLE rdst ATTACH PARTITION 1 FROM src; +SELECT count(), sum(d) FROM rdst; + +DROP TEMPORARY TABLE IF EXISTS src; +DROP TABLE IF EXISTS dst; +DROP TABLE IF EXISTS rdst;