From 89e1d1f64a7ba63400867ae2d872183fed58e046 Mon Sep 17 00:00:00 2001 From: Anton Popov Date: Fri, 27 Aug 2021 16:55:55 +0300 Subject: [PATCH] fix order by for StorageMerge with optimize_read_in_order --- src/Storages/StorageMerge.cpp | 5 +++-- .../02014_storage_merge_order_by.reference | 5 +++++ .../02014_storage_merge_order_by.sql | 22 +++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 tests/queries/0_stateless/02014_storage_merge_order_by.reference create mode 100644 tests/queries/0_stateless/02014_storage_merge_order_by.sql diff --git a/src/Storages/StorageMerge.cpp b/src/Storages/StorageMerge.cpp index 4e50b78ea8e..d537ef05cdb 100644 --- a/src/Storages/StorageMerge.cpp +++ b/src/Storages/StorageMerge.cpp @@ -338,9 +338,10 @@ Pipe StorageMerge::read( auto pipe = Pipe::unitePipes(std::move(pipes)); - if (!pipe.empty()) + if (!pipe.empty() && !query_info.input_order_info) // It's possible to have many tables read from merge, resize(num_streams) might open too many files at the same time. - // Using narrowPipe instead. + // Using narrowPipe instead. But in case of reading in order of primary key, we cannot do it, + // because narrowPipe doesn't preserve order. narrowPipe(pipe, num_streams); return pipe; diff --git a/tests/queries/0_stateless/02014_storage_merge_order_by.reference b/tests/queries/0_stateless/02014_storage_merge_order_by.reference new file mode 100644 index 00000000000..0bb816b3987 --- /dev/null +++ b/tests/queries/0_stateless/02014_storage_merge_order_by.reference @@ -0,0 +1,5 @@ +20 +20 +20 +20 +20 diff --git a/tests/queries/0_stateless/02014_storage_merge_order_by.sql b/tests/queries/0_stateless/02014_storage_merge_order_by.sql new file mode 100644 index 00000000000..5b9789ae1d9 --- /dev/null +++ b/tests/queries/0_stateless/02014_storage_merge_order_by.sql @@ -0,0 +1,22 @@ +DROP TABLE IF EXISTS short; +DROP TABLE IF EXISTS long; +DROP TABLE IF EXISTS merged; + +CREATE TABLE short (e Int64, t DateTime ) ENGINE = MergeTree PARTITION BY e ORDER BY t; +CREATE TABLE long (e Int64, t DateTime ) ENGINE = MergeTree PARTITION BY (e, toStartOfMonth(t)) ORDER BY t; + +insert into short select number % 11, toDateTime('2021-01-01 00:00:00') + number from numbers(1000); +insert into long select number % 11, toDateTime('2021-01-01 00:00:00') + number from numbers(1000); + +CREATE TABLE merged as short ENGINE = Merge(currentDatabase(), 'short|long'); + +select sum(e) from (select * from merged order by t limit 10) SETTINGS optimize_read_in_order = 0; + +select sum(e) from (select * from merged order by t limit 10) SETTINGS max_threads = 1; +select sum(e) from (select * from merged order by t limit 10) SETTINGS max_threads = 3; +select sum(e) from (select * from merged order by t limit 10) SETTINGS max_threads = 10; +select sum(e) from (select * from merged order by t limit 10) SETTINGS max_threads = 50; + +DROP TABLE IF EXISTS short; +DROP TABLE IF EXISTS long; +DROP TABLE IF EXISTS merged;