ClickHouse/src/Interpreters/sortBlock.cpp

291 lines
8.8 KiB
C++
Raw Normal View History

#include <Interpreters/sortBlock.h>
2012-07-17 20:05:36 +00:00
2019-12-05 15:31:45 +00:00
#include <Columns/ColumnConst.h>
#include <Columns/ColumnNullable.h>
#include <Columns/ColumnTuple.h>
2019-12-06 22:22:12 +00:00
#include <Functions/FunctionHelpers.h>
2012-07-17 20:05:36 +00:00
namespace DB
{
namespace ErrorCodes
{
extern const int BAD_COLLATION;
}
/// Column with description for sort
struct ColumnWithSortDescription
{
const IColumn * column = nullptr;
2022-01-29 12:55:52 +00:00
SortColumnDescription description;
/// It means, that this column is ColumnConst
bool column_const = false;
};
2022-01-29 15:50:55 +00:00
using ColumnsWithSortDescriptions = std::vector<ColumnWithSortDescription>;
2022-01-29 15:50:55 +00:00
namespace
{
2018-10-04 10:24:51 +00:00
2022-01-29 15:50:55 +00:00
inline bool isCollationRequired(const SortColumnDescription & description)
{
2022-01-29 15:50:55 +00:00
return description.collator != nullptr;
}
2022-01-29 15:50:55 +00:00
template <bool check_collation>
struct PartialSortingLessImpl
2018-10-04 14:55:02 +00:00
{
const ColumnsWithSortDescriptions & columns;
2022-01-29 15:50:55 +00:00
explicit PartialSortingLessImpl(const ColumnsWithSortDescriptions & columns_) : columns(columns_) { }
2018-10-04 14:55:02 +00:00
2022-01-29 15:50:55 +00:00
inline bool operator()(size_t a, size_t b) const
2018-10-04 14:55:02 +00:00
{
2020-03-09 00:28:05 +00:00
for (const auto & elem : columns)
2018-10-04 14:55:02 +00:00
{
2019-12-06 22:22:12 +00:00
int res;
2022-01-29 15:50:55 +00:00
2020-03-09 00:28:05 +00:00
if (elem.column_const)
2022-01-29 15:50:55 +00:00
{
2019-12-06 22:22:12 +00:00
res = 0;
2022-01-29 15:50:55 +00:00
continue;
}
if constexpr (check_collation)
{
if (isCollationRequired(elem.description))
{
res = elem.column->compareAtWithCollation(a, b, *elem.column, elem.description.nulls_direction, *elem.description.collator);
}
else
{
res = elem.column->compareAt(a, b, *elem.column, elem.description.nulls_direction);
}
}
2019-12-06 22:22:12 +00:00
else
2022-01-29 15:50:55 +00:00
{
res = elem.column->compareAt(a, b, *elem.column, elem.description.nulls_direction);
}
res *= elem.description.direction;
2018-10-04 14:55:02 +00:00
if (res < 0)
return true;
else if (res > 0)
return false;
}
return false;
}
};
2022-01-29 15:50:55 +00:00
using PartialSortingLess = PartialSortingLessImpl<false>;
using PartialSortingLessWithCollation = PartialSortingLessImpl<true>;
2018-10-04 14:55:02 +00:00
2022-01-29 15:50:55 +00:00
}
2022-01-29 15:50:55 +00:00
void convertTupleColumnIntoSortDescriptions(
const ColumnTuple * tuple, const SortColumnDescription & description, ColumnsWithSortDescriptions & result)
{
for (const auto & column : tuple->getColumns())
2019-12-06 22:22:12 +00:00
{
2022-01-29 15:50:55 +00:00
if (const auto * subtuple = typeid_cast<const ColumnTuple *>(column.get()))
{
convertTupleColumnIntoSortDescriptions(subtuple, description, result);
}
else
{
result.emplace_back(ColumnWithSortDescription{column.get(), description, isColumnConst(*column)});
if (isCollationRequired(description) && !result.back().column->isCollationSupported())
result.back().description.collator = nullptr;
}
2019-12-06 22:22:12 +00:00
}
2022-01-29 15:50:55 +00:00
}
2022-01-29 15:50:55 +00:00
ColumnsWithSortDescriptions getColumnsWithSortDescription(const Block & block, const SortDescription & description)
{
size_t size = description.size();
ColumnsWithSortDescriptions result;
result.reserve(size);
for (size_t i = 0; i < size; ++i)
{
2022-01-29 15:50:55 +00:00
const auto & sort_column_description = description[i];
2019-12-06 22:22:12 +00:00
2022-01-29 15:50:55 +00:00
const IColumn * column = !sort_column_description.column_name.empty()
? block.getByName(sort_column_description.column_name).column.get()
: block.safeGetByPosition(sort_column_description.column_number).column.get();
if (isCollationRequired(sort_column_description))
{
if (!column->isCollationSupported())
throw Exception(
"Collations could be specified only for String, LowCardinality(String), Nullable(String) or for Array or Tuple, "
"containing them.",
ErrorCodes::BAD_COLLATION);
}
2022-01-29 15:50:55 +00:00
if (const auto * tuple = typeid_cast<const ColumnTuple *>(column))
convertTupleColumnIntoSortDescriptions(tuple, sort_column_description, result);
else
result.emplace_back(ColumnWithSortDescription{column, sort_column_description, isColumnConst(*column)});
}
2022-01-29 15:50:55 +00:00
return result;
}
2012-07-17 20:05:36 +00:00
2019-02-10 15:17:45 +00:00
void sortBlock(Block & block, const SortDescription & description, UInt64 limit)
2012-07-17 20:05:36 +00:00
{
if (!block)
return;
2022-01-29 15:50:55 +00:00
ColumnsWithSortDescriptions columns_with_sort_descriptions = getColumnsWithSortDescription(block, description);
2022-01-28 17:37:39 +00:00
bool all_const = true;
2022-01-29 15:50:55 +00:00
for (const auto & column : columns_with_sort_descriptions)
2022-01-28 17:37:39 +00:00
{
if (!column.column_const)
{
all_const = false;
break;
}
}
if (all_const)
return;
2022-01-29 15:50:55 +00:00
IColumn::Permutation permutation;
/// If only one column to sort by
2022-01-29 15:50:55 +00:00
if (columns_with_sort_descriptions.size() == 1)
{
2022-01-29 15:50:55 +00:00
auto & column_with_sort_description = columns_with_sort_descriptions[0];
bool reverse = column_with_sort_description.description.direction == -1;
int nan_direction_hint = column_with_sort_description.description.nulls_direction;
const auto & column = column_with_sort_description.column;
if (isCollationRequired(column_with_sort_description.description))
column->getPermutationWithCollation(
*column_with_sort_description.description.collator, reverse, limit, nan_direction_hint, permutation);
2022-01-28 20:49:58 +00:00
else
2022-01-29 15:50:55 +00:00
column->getPermutation(reverse, limit, nan_direction_hint, permutation);
}
else
{
2019-12-07 13:13:14 +00:00
size_t size = block.rows();
2022-01-29 15:50:55 +00:00
permutation.resize(size);
2019-12-07 13:13:14 +00:00
for (size_t i = 0; i < size; ++i)
2022-01-29 15:50:55 +00:00
permutation[i] = i;
2019-12-07 13:13:14 +00:00
if (limit >= size)
limit = 0;
2022-01-28 20:49:58 +00:00
EqualRanges ranges;
2022-01-29 15:50:55 +00:00
ranges.emplace_back(0, permutation.size());
for (const auto & column_with_sort_description : columns_with_sort_descriptions)
{
2022-01-28 20:49:58 +00:00
while (!ranges.empty() && limit && limit <= ranges.back().first)
ranges.pop_back();
2022-01-28 20:49:58 +00:00
if (ranges.empty())
break;
2022-01-29 15:50:55 +00:00
if (column_with_sort_description.column_const)
2022-01-28 20:49:58 +00:00
continue;
2022-01-29 15:50:55 +00:00
bool is_collation_required = isCollationRequired(column_with_sort_description.description);
bool reverse = column_with_sort_description.description.direction < 0;
int nan_direction_hint = column_with_sort_description.description.nulls_direction;
const auto & column = column_with_sort_description.column;
if (is_collation_required)
2020-05-12 00:58:58 +00:00
{
2022-01-29 15:50:55 +00:00
column->updatePermutationWithCollation(
*column_with_sort_description.description.collator, reverse, limit, nan_direction_hint, permutation, ranges);
2020-05-12 00:58:58 +00:00
}
2022-01-28 20:49:58 +00:00
else
2020-05-12 00:58:58 +00:00
{
2022-01-29 15:50:55 +00:00
column->updatePermutation(reverse, limit, nan_direction_hint, permutation, ranges);
2020-05-12 00:58:58 +00:00
}
}
}
2022-01-29 15:50:55 +00:00
size_t columns = block.columns();
for (size_t i = 0; i < columns; ++i)
2022-01-29 15:50:55 +00:00
{
auto & column_to_sort = block.getByPosition(i).column;
column_to_sort = column_to_sort->permute(permutation, limit);
}
2012-07-17 20:05:36 +00:00
}
void stableGetPermutation(const Block & block, const SortDescription & description, IColumn::Permutation & out_permutation)
2013-11-29 22:10:15 +00:00
{
if (!block)
return;
2013-11-29 22:10:15 +00:00
size_t size = block.rows();
out_permutation.resize(size);
for (size_t i = 0; i < size; ++i)
out_permutation[i] = i;
2013-11-29 22:10:15 +00:00
ColumnsWithSortDescriptions columns_with_sort_desc = getColumnsWithSortDescription(block, description);
2013-11-29 22:10:15 +00:00
std::stable_sort(out_permutation.begin(), out_permutation.end(), PartialSortingLess(columns_with_sort_desc));
}
bool isAlreadySorted(const Block & block, const SortDescription & description)
{
if (!block)
return true;
size_t rows = block.rows();
ColumnsWithSortDescriptions columns_with_sort_desc = getColumnsWithSortDescription(block, description);
PartialSortingLess less(columns_with_sort_desc);
2017-04-02 17:37:49 +00:00
/** If the rows are not too few, then let's make a quick attempt to verify that the block is not sorted.
* Constants - at random.
*/
static constexpr size_t num_rows_to_try = 10;
if (rows > num_rows_to_try * 5)
{
for (size_t i = 1; i < num_rows_to_try; ++i)
{
size_t prev_position = rows * (i - 1) / num_rows_to_try;
size_t curr_position = rows * i / num_rows_to_try;
if (less(curr_position, prev_position))
return false;
}
}
for (size_t i = 1; i < rows; ++i)
if (less(i, i - 1))
return false;
return true;
}
void stableSortBlock(Block & block, const SortDescription & description)
{
if (!block)
return;
2022-01-29 15:50:55 +00:00
IColumn::Permutation permutation;
stableGetPermutation(block, description, permutation);
2013-11-29 22:10:15 +00:00
size_t columns = block.columns();
for (size_t i = 0; i < columns; ++i)
2022-01-29 15:50:55 +00:00
{
auto & column_to_sort = block.safeGetByPosition(i).column;
column_to_sort = column_to_sort->permute(permutation, 0);
}
2013-11-29 22:10:15 +00:00
}
2012-07-17 20:05:36 +00:00
}