another way

This commit is contained in:
Alexander Kuzmenkov 2021-03-17 18:31:30 +03:00
parent e5bef75728
commit 1cd9f28bd4
2 changed files with 15 additions and 10 deletions

View File

@ -2038,7 +2038,13 @@ void InterpreterSelectQuery::executeWindow(QueryPlan & query_plan)
for (size_t i = 0; i < windows_sorted.size(); ++i)
{
const auto & w = *windows_sorted[i];
if (i == 0 || !sortIsPrefix(w, *windows_sorted[i - 1]))
// We don't need to sort again if the input from previous window already
// has suitable sorting. Also don't create sort steps when there are no
// columns to sort by, because the sort nodes are confused by this. It
// happens in case of `over ()`.
if (!w.full_sort_description.empty()
&& (i == 0 || !sortIsPrefix(w, *windows_sorted[i - 1])))
{
auto partial_sorting = std::make_unique<PartialSortingStep>(
query_plan.getCurrentDataStream(),

View File

@ -10,6 +10,8 @@ PartialSortingTransform::PartialSortingTransform(
: ISimpleTransform(header_, header_, false)
, description(description_), limit(limit_)
{
// Sorting by no columns doesn't make sense.
assert(!description.empty());
}
static ColumnRawPtrs extractColumns(const Block & block, const SortDescription & description)
@ -91,17 +93,14 @@ size_t getFilterMask(const ColumnRawPtrs & lhs, const ColumnRawPtrs & rhs, size_
void PartialSortingTransform::transform(Chunk & chunk)
{
if (chunk.getColumns().empty())
if (chunk.getNumRows())
{
// Sometimes we can have Chunks w/o columns, e.g. in case of
// `select count() over () from numbers(4) where number < 2`.
// We don't have to modify this Chunk, but we have to preserve the input
// number of rows. The following code uses Block for sorting, and Block
// is incapable of recording the number of rows when there is no columns.
// The simplest solution is to specifically check for Chunk with no
// columns and not modify it, which is what we do here.
return;
// The following code works with Blocks and will lose the number of
// rows when there are no columns. We shouldn't get such block, because
// we have to sort by at least one column.
assert(chunk.getNumColumns());
}
if (read_rows)
read_rows->add(chunk.getNumRows());