From 1c1646621407df7ab4938f054a383dd7c1fe0313 Mon Sep 17 00:00:00 2001 From: Alexey Milovidov Date: Tue, 14 Oct 2014 22:39:38 +0400 Subject: [PATCH] dbms: fixed error when sorting result of ARRAY JOIN by empty arrays [#METR-13204]. --- .../MergeSortingBlockInputStream.cpp | 17 ++++++++++++----- .../MergingSortedBlockInputStream.cpp | 4 ++-- .../00073_merge_sorting_empty_array_joined.sql | 2 ++ 3 files changed, 16 insertions(+), 7 deletions(-) create mode 100755 dbms/tests/queries/0_stateless/00073_merge_sorting_empty_array_joined.sql diff --git a/dbms/src/DataStreams/MergeSortingBlockInputStream.cpp b/dbms/src/DataStreams/MergeSortingBlockInputStream.cpp index 6c25b33f83d..f9ccd3f066a 100644 --- a/dbms/src/DataStreams/MergeSortingBlockInputStream.cpp +++ b/dbms/src/DataStreams/MergeSortingBlockInputStream.cpp @@ -47,16 +47,23 @@ Block MergeSortingBlockInputStream::merge(Blocks & blocks) bool has_collation = false; - size_t i = 0; - for (Blocks::const_iterator it = blocks.begin(); it != blocks.end(); ++it, ++i) + size_t nonempty_blocks = 0; + for (Blocks::const_iterator it = blocks.begin(); it != blocks.end(); ++it) { - if (!*it) + if (it->rowsInFirstColumn() == 0) continue; - cursors[i] = SortCursorImpl(*it, description); - has_collation |= cursors[i].has_collation; + cursors[nonempty_blocks] = SortCursorImpl(*it, description); + has_collation |= cursors[nonempty_blocks].has_collation; + + ++nonempty_blocks; } + if (nonempty_blocks == 0) + return Block(); + + cursors.resize(nonempty_blocks); + Block merged; if (has_collation) diff --git a/dbms/src/DataStreams/MergingSortedBlockInputStream.cpp b/dbms/src/DataStreams/MergingSortedBlockInputStream.cpp index 064bbf1bb78..b3de734e3dc 100644 --- a/dbms/src/DataStreams/MergingSortedBlockInputStream.cpp +++ b/dbms/src/DataStreams/MergingSortedBlockInputStream.cpp @@ -22,7 +22,7 @@ void MergingSortedBlockInputStream::init(Block & merged_block, ColumnPlainPtrs & *it = children[i]->read(); - if (!*it) + if (it->rowsInFirstColumn() == 0) continue; if (!num_columns) @@ -31,7 +31,7 @@ void MergingSortedBlockInputStream::init(Block & merged_block, ColumnPlainPtrs & cursors[i] = SortCursorImpl(*it, description, i); has_collation |= cursors[i].has_collation; } - + if (has_collation) initQueue(queue_with_collation); else diff --git a/dbms/tests/queries/0_stateless/00073_merge_sorting_empty_array_joined.sql b/dbms/tests/queries/0_stateless/00073_merge_sorting_empty_array_joined.sql new file mode 100755 index 00000000000..50fa8b82e0c --- /dev/null +++ b/dbms/tests/queries/0_stateless/00073_merge_sorting_empty_array_joined.sql @@ -0,0 +1,2 @@ +SET max_block_size = 1; +SELECT number, arr FROM (SELECT number, arrayFilter(x -> x = 0, [1]) AS arr FROM system.numbers LIMIT 10) ARRAY JOIN arr ORDER BY number;