Fix: handle all const columns case correctly

This commit is contained in:
Igor Nikonov 2022-07-28 21:22:06 +00:00
parent f414af2348
commit 4af435bdda
4 changed files with 46 additions and 2 deletions

View File

@ -8,6 +8,20 @@ namespace ErrorCodes
extern const int SET_SIZE_LIMIT_EXCEEDED;
}
static void handleAllColumnsConst(Chunk & chunk)
{
const size_t rows = chunk.getNumRows();
IColumn::Filter filter(rows);
Chunk res_chunk;
std::fill(filter.begin(), filter.end(), 0);
filter[0] = 1;
for (const auto & column : chunk.getColumns())
res_chunk.addColumn(column->filter(filter, -1));
chunk = std::move(res_chunk);
}
DistinctSortedTransform::DistinctSortedTransform(
Block header_, SortDescription sort_description, const SizeLimits & set_size_limits_, UInt64 limit_hint_, const Names & columns)
: ISimpleTransform(header_, header_, true)
@ -23,9 +37,12 @@ DistinctSortedTransform::DistinctSortedTransform(
for (size_t i = 0; i < num_columns; ++i)
{
auto pos = column_names.empty() ? i : header.getPositionByName(column_names[i]);
const auto & col = header.getByPosition(pos).column;
if (col && !isColumnConst(*col))
const auto & column = header.getByPosition(pos).column;
if (column && !isColumnConst(*column))
{
column_positions.emplace_back(pos);
all_columns_const = false;
}
}
column_ptrs.reserve(column_positions.size());
@ -52,6 +69,14 @@ void DistinctSortedTransform::transform(Chunk & chunk)
if (unlikely(!chunk.hasRows()))
return;
/// special case - all column constant
if (unlikely(all_columns_const))
{
handleAllColumnsConst(chunk);
stopReading();
return;
}
/// get DISTINCT columns from chunk
column_ptrs.clear();
for (const auto pos : column_positions)

View File

@ -66,6 +66,7 @@ private:
/// Restrictions on the maximum size of the output data.
SizeLimits set_size_limits;
bool all_columns_const = true;
};
}

View File

@ -77,4 +77,14 @@
2 2
1 1
0 0
-- distinct with constants columns
-- { echoOn }
select distinct 1 as a, 2 as b from distinct_in_order;
1 2
select distinct 1 as a, 2 as b from distinct_in_order order by a;
1 2
select distinct 1 as a, 2 as b from distinct_in_order order by a, b;
1 2
select distinct x, y from (select 1 as x, 2 as y from distinct_in_order order by x) order by x;
1 2
-- check that distinct in order has the same result as ordinary distinct

View File

@ -43,6 +43,14 @@ select distinct b,c from distinct_in_order order by c;
select '-- distinct with non-key prefix and non-sorted column, order by non-sorted desc';
select distinct b,c from distinct_in_order order by c desc;
select '-- distinct with constants columns';
-- { echoOn }
select distinct 1 as a, 2 as b from distinct_in_order;
select distinct 1 as a, 2 as b from distinct_in_order order by a;
select distinct 1 as a, 2 as b from distinct_in_order order by a, b;
select distinct x, y from (select 1 as x, 2 as y from distinct_in_order order by x) order by x;
-- { echoOff }
drop table if exists distinct_in_order sync;
select '-- check that distinct in order has the same result as ordinary distinct';