Merge pull request #10826 from azat/block-sort-fix

Fix columns order after Block::sortColumns()
This commit is contained in:
alexey-milovidov 2020-05-12 06:39:18 +03:00 committed by GitHub
commit 75607db571
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 73 additions and 2 deletions

View File

@ -403,8 +403,20 @@ Block Block::sortColumns() const
{
Block sorted_block;
for (const auto & name : index_by_name)
sorted_block.insert(data[name.second]);
/// std::unordered_map (index_by_name) cannot be used to guarantee the sort order
std::vector<decltype(index_by_name.begin())> sorted_index_by_name(index_by_name.size());
{
size_t i = 0;
for (auto it = index_by_name.begin(); it != index_by_name.end(); ++it)
sorted_index_by_name[i++] = it;
}
std::sort(sorted_index_by_name.begin(), sorted_index_by_name.end(), [](const auto & lhs, const auto & rhs)
{
return lhs->first < rhs->first;
});
for (const auto & it : sorted_index_by_name)
sorted_block.insert(data[it->second]);
return sorted_block;
}

View File

@ -0,0 +1,59 @@
-- Check for Block::sortColumns(), can be done using Buffer.
drop table if exists out_01277;
drop table if exists in_01277;
drop table if exists buffer_01277;
drop table if exists mv_01277_1;
drop table if exists mv_01277_2;
create table out_01277
(
k1 Int,
k2 Int,
a1 Int,
a2 Int,
b1 Int,
b2 Int,
c Int
) Engine=Null();
create table buffer_01277 as out_01277 Engine=Buffer(currentDatabase(), out_01277, 1,
86400, 86400,
1e5, 1e6,
10e6, 100e6);
create table in_01277 as out_01277 Engine=Null();
-- differs in order of fields in SELECT clause
create materialized view mv_01277_1 to buffer_01277 as select k1, k2, a1, a2, b1, b2, c from in_01277;
create materialized view mv_01277_2 to buffer_01277 as select a1, a2, k1, k2, b1, b2, c from in_01277;
-- column order is ignored, just for humans
insert into mv_01277_1 select
number k1,
number k2,
number a1,
number a2,
number b1,
number b2,
number c
from numbers(1);
-- with wrong order in Block::sortColumns() triggers:
--
-- Code: 171. DB::Exception: Received from localhost:9000. DB::Exception: Block structure mismatch in Buffer stream: different names of columns:
-- c Int32 Int32(size = 1), b2 Int32 Int32(size = 1), a2 Int32 Int32(size = 1), a1 Int32 Int32(size = 1), k2 Int32 Int32(size = 1), b1 Int32 Int32(size = 1), k1 Int32 Int32(size = 1)
-- c Int32 Int32(size = 1), b2 Int32 Int32(size = 1), k2 Int32 Int32(size = 1), a1 Int32 Int32(size = 1), b1 Int32 Int32(size = 1), k1 Int32 Int32(size = 1), a2 Int32 Int32(size = 1).
insert into mv_01277_2 select
number a1,
number a2,
number k1,
number k2,
number b1,
number b2,
number c
from numbers(1);
drop table mv_01277_1;
drop table mv_01277_2;
drop table buffer_01277;
drop table out_01277;
drop table in_01277;