diff --git a/src/Databases/DatabaseOrdinary.cpp b/src/Databases/DatabaseOrdinary.cpp index 0067bab19bd..2c321bb1026 100644 --- a/src/Databases/DatabaseOrdinary.cpp +++ b/src/Databases/DatabaseOrdinary.cpp @@ -273,7 +273,7 @@ void DatabaseOrdinary::alterTable(const Context & context, const StorageID & tab if (metadata.primary_key.definition_ast) storage_ast.set(storage_ast.primary_key, metadata.primary_key.definition_ast); - if (metadata.sampling_key.definition_ast && storage_ast.sample_by) + if (metadata.sampling_key.definition_ast) storage_ast.set(storage_ast.sample_by, metadata.sampling_key.definition_ast); if (metadata.table_ttl.definition_ast) diff --git a/src/Storages/MergeTree/MergeTreeData.cpp b/src/Storages/MergeTree/MergeTreeData.cpp index 17fbd9aa2a1..0b9fad7904b 100644 --- a/src/Storages/MergeTree/MergeTreeData.cpp +++ b/src/Storages/MergeTree/MergeTreeData.cpp @@ -1423,6 +1423,11 @@ void MergeTreeData::checkAlterIsPossible(const AlterCommands & commands, const S } if (command.type == AlterCommand::MODIFY_SAMPLE_BY) { + if (!is_custom_partitioned) + throw Exception( + "ALTER MODIFY SAMPLE BY is not supported for default-partitioned tables created with the old syntax", + ErrorCodes::BAD_ARGUMENTS); + checkSampleExpression(new_metadata, getSettings()->compatibility_allow_sampling_expression_not_in_primary_key); } if (command.type == AlterCommand::ADD_INDEX && !is_custom_partitioned) diff --git a/tests/queries/0_stateless/01430_modify_sample_by.reference b/tests/queries/0_stateless/01430_modify_sample_by.reference deleted file mode 100644 index 00992519f2d..00000000000 --- a/tests/queries/0_stateless/01430_modify_sample_by.reference +++ /dev/null @@ -1,2 +0,0 @@ -25 0 24 300 25 -25 0 24 300 25 diff --git a/tests/queries/0_stateless/01430_modify_sample_by.sql b/tests/queries/0_stateless/01430_modify_sample_by.sql deleted file mode 100644 index 9c6634ec2d9..00000000000 --- a/tests/queries/0_stateless/01430_modify_sample_by.sql +++ /dev/null @@ -1,22 +0,0 @@ -DROP TABLE IF EXISTS modify_sample; - -SET min_insert_block_size_rows = 0, min_insert_block_size_bytes = 0; -SET max_block_size = 10; - -CREATE TABLE modify_sample (d Date DEFAULT '2000-01-01', x UInt8) ENGINE = MergeTree PARTITION BY d ORDER BY x; -INSERT INTO modify_sample (x) SELECT toUInt8(number) AS x FROM system.numbers LIMIT 256; - -SELECT count(), min(x), max(x), sum(x), uniqExact(x) FROM modify_sample SAMPLE 0.1; -- { serverError 141 } - -ALTER TABLE modify_sample MODIFY SAMPLE BY x; -SELECT count(), min(x), max(x), sum(x), uniqExact(x) FROM modify_sample SAMPLE 0.1; - -CREATE TABLE modify_sample_replicated (d Date DEFAULT '2000-01-01', x UInt8) ENGINE = ReplicatedMergeTree('/clickhouse/tables/test_01430', 'modify_sample') PARTITION BY d ORDER BY x; -INSERT INTO modify_sample_replicated (x) SELECT toUInt8(number) AS x FROM system.numbers LIMIT 256; - -SELECT count(), min(x), max(x), sum(x), uniqExact(x) FROM modify_sample_replicated SAMPLE 0.1; -- { serverError 141 } - -ALTER TABLE modify_sample_replicated MODIFY SAMPLE BY x; -SELECT count(), min(x), max(x), sum(x), uniqExact(x) FROM modify_sample_replicated SAMPLE 0.1; - -DROP TABLE modify_sample; diff --git a/tests/queries/0_stateless/01430_modify_sample_by_zookeeper.reference b/tests/queries/0_stateless/01430_modify_sample_by_zookeeper.reference new file mode 100644 index 00000000000..9462a53a19d --- /dev/null +++ b/tests/queries/0_stateless/01430_modify_sample_by_zookeeper.reference @@ -0,0 +1,5 @@ +25 0 24 300 25 +25 0 24 300 25 +25 0 24 300 25 +256 0 255 32640 256 +256 0 255 32640 256 diff --git a/tests/queries/0_stateless/01430_modify_sample_by_zookeeper.sql b/tests/queries/0_stateless/01430_modify_sample_by_zookeeper.sql new file mode 100644 index 00000000000..dc7f5017bfd --- /dev/null +++ b/tests/queries/0_stateless/01430_modify_sample_by_zookeeper.sql @@ -0,0 +1,46 @@ +DROP TABLE IF EXISTS modify_sample; + +SET min_insert_block_size_rows = 0, min_insert_block_size_bytes = 0; +SET max_block_size = 10; + +CREATE TABLE modify_sample (d Date DEFAULT '2000-01-01', x UInt8) ENGINE = MergeTree PARTITION BY d ORDER BY x; +INSERT INTO modify_sample (x) SELECT toUInt8(number) AS x FROM system.numbers LIMIT 256; + +SELECT count(), min(x), max(x), sum(x), uniqExact(x) FROM modify_sample SAMPLE 0.1; -- { serverError 141 } + +ALTER TABLE modify_sample MODIFY SAMPLE BY x; +SELECT count(), min(x), max(x), sum(x), uniqExact(x) FROM modify_sample SAMPLE 0.1; + +CREATE TABLE modify_sample_replicated (d Date DEFAULT '2000-01-01', x UInt8, y UInt64) ENGINE = ReplicatedMergeTree('/clickhouse/tables/test_01430', 'modify_sample') PARTITION BY d ORDER BY (x, y); + +INSERT INTO modify_sample_replicated (x, y) SELECT toUInt8(number) AS x, toUInt64(number) as y FROM system.numbers LIMIT 256; + +SELECT count(), min(x), max(x), sum(x), uniqExact(x) FROM modify_sample_replicated SAMPLE 0.1; -- { serverError 141 } + +ALTER TABLE modify_sample_replicated MODIFY SAMPLE BY x; +SELECT count(), min(x), max(x), sum(x), uniqExact(x) FROM modify_sample_replicated SAMPLE 0.1; + +DETACH TABLE modify_sample_replicated; +ATTACH TABLE modify_sample_replicated; + +SELECT count(), min(x), max(x), sum(x), uniqExact(x) FROM modify_sample_replicated SAMPLE 0.1; + +ALTER TABLE modify_sample_replicated MODIFY SAMPLE BY d; -- { serverError 36 } +ALTER TABLE modify_sample_replicated MODIFY SAMPLE BY y; + +SELECT count(), min(y), max(y), sum(y), uniqExact(y) FROM modify_sample_replicated SAMPLE 0.1; + +DETACH TABLE modify_sample_replicated; +ATTACH TABLE modify_sample_replicated; + +SELECT count(), min(y), max(y), sum(y), uniqExact(y) FROM modify_sample_replicated SAMPLE 0.1; + +CREATE TABLE modify_sample_old (d Date DEFAULT '2000-01-01', x UInt8, y UInt64) ENGINE = MergeTree(d, (x, y), 8192); + +ALTER TABLE modify_sample_old MODIFY SAMPLE BY x; -- { serverError 36 } + +DROP TABLE modify_sample; + +DROP TABLE modify_sample_replicated; + +DROP TABLE modify_sample_old;