diff --git a/dbms/src/Storages/MergeTree/MergeTreeDataSelectExecutor.cpp b/dbms/src/Storages/MergeTree/MergeTreeDataSelectExecutor.cpp index 2d6e0da9161..4ca213e4d0f 100644 --- a/dbms/src/Storages/MergeTree/MergeTreeDataSelectExecutor.cpp +++ b/dbms/src/Storages/MergeTree/MergeTreeDataSelectExecutor.cpp @@ -167,20 +167,20 @@ BlockInputStreams MergeTreeDataSelectExecutor::read( UInt64 sampling_column_value_lower_limit; UInt64 sampling_column_value_upper_limit; - UInt64 upper_limit = static_cast(static_cast(relative_sample_size) * sampling_column_max); + UInt64 upper_limit = static_cast(relative_sample_size) * sampling_column_max; if (settings.parallel_replicas_count > 1) { - sampling_column_value_lower_limit = (settings.parallel_replica_offset * upper_limit) / settings.parallel_replicas_count; + sampling_column_value_lower_limit = (static_cast(settings.parallel_replica_offset) / settings.parallel_replicas_count) * upper_limit; if ((settings.parallel_replica_offset + 1) < settings.parallel_replicas_count) - sampling_column_value_upper_limit = ((settings.parallel_replica_offset + 1) * upper_limit) / settings.parallel_replicas_count; + sampling_column_value_upper_limit = (static_cast(settings.parallel_replica_offset + 1) / settings.parallel_replicas_count) * upper_limit; else - sampling_column_value_upper_limit = upper_limit + 1; + sampling_column_value_upper_limit = (upper_limit < sampling_column_max) ? (upper_limit + 1) : upper_limit; } else { sampling_column_value_lower_limit = 0; - sampling_column_value_upper_limit = upper_limit + 1; + sampling_column_value_upper_limit = (upper_limit < sampling_column_max) ? (upper_limit + 1) : upper_limit; } /// Добавим условие, чтобы отсечь еще что-нибудь при повторном просмотре индекса. diff --git a/dbms/tests/queries/0_stateless/00193_parallel_replicas.reference b/dbms/tests/queries/0_stateless/00193_parallel_replicas.reference index 2316721a68d..919fe6cdcae 100644 --- a/dbms/tests/queries/0_stateless/00193_parallel_replicas.reference +++ b/dbms/tests/queries/0_stateless/00193_parallel_replicas.reference @@ -1,3 +1,57 @@ -1000000 1 1 +1 2 A +3 4 B +5 6 C +7 8 D +9 10 E +11 12 F +13 14 G +15 16 H +17 18 I +19 20 J +21 22 K +23 24 L +25 26 M +27 28 N +29 30 O +31 32 P +33 34 Q +35 36 R +37 38 S +39 40 T +41 42 U +43 44 V +45 46 W +47 48 X +49 50 Y +51 52 Z +1 +1 +1 +1 2 A +3 4 B +5 6 C +7 8 D +9 10 E +11 12 F +13 14 G +15 16 H +17 18 I +19 20 J +21 22 K +23 24 L +25 26 M +27 28 N +29 30 O +31 32 P +33 34 Q +35 36 R +37 38 S +39 40 T +41 42 U +43 44 V +45 46 W +47 48 X +49 50 Y +51 52 Z diff --git a/dbms/tests/queries/0_stateless/00193_parallel_replicas.sql b/dbms/tests/queries/0_stateless/00193_parallel_replicas.sql index 6033d8ab372..5a3908b7d5d 100644 --- a/dbms/tests/queries/0_stateless/00193_parallel_replicas.sql +++ b/dbms/tests/queries/0_stateless/00193_parallel_replicas.sql @@ -1,13 +1,55 @@ DROP TABLE IF EXISTS test.parallel_replicas; CREATE TABLE test.parallel_replicas (d Date DEFAULT today(), x UInt32, u UInt64, s String) ENGINE = MergeTree(d, cityHash64(u, s), (x, d, cityHash64(u, s)), 8192); -INSERT INTO test.parallel_replicas (x, u, s) SELECT toUInt32(number / 1000) AS x, toUInt64(number % 1000) AS u, toString(rand64()) AS s FROM system.numbers LIMIT 1000000; +INSERT INTO test.parallel_replicas (x, u, s) VALUES (1, 2, 'A'),(3, 4, 'B'),(5, 6, 'C'),(7, 8, 'D'),(9,10,'E'); +INSERT INTO test.parallel_replicas (x, u, s) VALUES (11, 12, 'F'),(13, 14, 'G'),(15, 16, 'H'),(17, 18, 'I'),(19,20,'J'); +INSERT INTO test.parallel_replicas (x, u, s) VALUES (21, 22, 'K'),(23, 24, 'L'),(25, 26, 'M'),(27, 28, 'N'),(29,30,'O'); +INSERT INTO test.parallel_replicas (x, u, s) VALUES (31, 32, 'P'),(33, 34, 'Q'),(35, 36, 'R'),(37, 38, 'S'),(39,40,'T'); +INSERT INTO test.parallel_replicas (x, u, s) VALUES (41, 42, 'U'),(43, 44, 'V'),(45, 46, 'W'),(47, 48, 'X'),(49,50,'Y'); +INSERT INTO test.parallel_replicas (x, u, s) VALUES (51, 52, 'Z'); -SELECT count() FROM test.parallel_replicas; -SELECT count() > 0 FROM test.parallel_replicas SAMPLE 0.5; +/* + * Проверяем, что: + * - на каждой реплике таблица не пустая; + * - объединение данных всех реплик совпадает с содержанием таблицы test.parallel_replicas. + */ + +/* Две реплики */ + +DROP TABLE IF EXISTS test.parallel_replicas_backup; +CREATE TABLE test.parallel_replicas_backup(d Date DEFAULT today(), x UInt32, u UInt64, s String) ENGINE = Memory; SET parallel_replicas_count = 2; + +SET parallel_replica_offset = 0; +INSERT INTO test.parallel_replicas_backup(d, x, u, s) SELECT d, x, u, s FROM test.parallel_replicas; SELECT count() > 0 FROM test.parallel_replicas; SET parallel_replica_offset = 1; -SELECT count() FROM test.parallel_replicas; +INSERT INTO test.parallel_replicas_backup(d, x, u, s) SELECT d, x, u, s FROM test.parallel_replicas; +SELECT count() > 0 FROM test.parallel_replicas; + +SET parallel_replicas_count = 0; +SELECT x, u, s FROM test.parallel_replicas_backup ORDER BY x, u, s ASC; + +DROP TABLE IF EXISTS test.parallel_replicas_backup; +CREATE TABLE test.parallel_replicas_backup(d Date DEFAULT today(), x UInt32, u UInt64, s String) ENGINE = Memory; + +/* Три реплики */ + +SET parallel_replicas_count = 3; + +SET parallel_replica_offset = 0; +INSERT INTO test.parallel_replicas_backup(d, x, u, s) SELECT d, x, u, s FROM test.parallel_replicas; +SELECT count() > 0 FROM test.parallel_replicas; + +SET parallel_replica_offset = 1; +INSERT INTO test.parallel_replicas_backup(d, x, u, s) SELECT d, x, u, s FROM test.parallel_replicas; +SELECT count() > 0 FROM test.parallel_replicas; + +SET parallel_replica_offset = 2; +INSERT INTO test.parallel_replicas_backup(d, x, u, s) SELECT d, x, u, s FROM test.parallel_replicas; +SELECT count() > 0 FROM test.parallel_replicas; + +SET parallel_replicas_count = 0; +SELECT x, u, s FROM test.parallel_replicas_backup ORDER BY x, u, s ASC;