ClickHouse/src/Interpreters/sortBlock.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

273 lines
8.3 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-02-19 11:11:17 +00:00
ALWAYS_INLINE int compare(size_t lhs, size_t rhs) const
2018-10-04 14:55:02 +00:00
{
2022-02-19 11:11:17 +00:00
int res = 0;
2020-03-09 00:28:05 +00:00
for (const auto & elem : columns)
2018-10-04 14:55:02 +00:00
{
2020-03-09 00:28:05 +00:00
if (elem.column_const)
2022-01-29 15:50:55 +00:00
{
continue;
}
if constexpr (check_collation)
{
if (isCollationRequired(elem.description))
{
2022-02-19 11:11:17 +00:00
res = elem.column->compareAtWithCollation(lhs, rhs, *elem.column, elem.description.nulls_direction, *elem.description.collator);
2022-01-29 15:50:55 +00:00
}
else
{
2022-02-19 11:11:17 +00:00
res = elem.column->compareAt(lhs, rhs, *elem.column, elem.description.nulls_direction);
2022-01-29 15:50:55 +00:00
}
}
2019-12-06 22:22:12 +00:00
else
2022-01-29 15:50:55 +00:00
{
2022-02-19 11:11:17 +00:00
res = elem.column->compareAt(lhs, rhs, *elem.column, elem.description.nulls_direction);
2022-01-29 15:50:55 +00:00
}
res *= elem.description.direction;
2022-02-19 11:11:17 +00:00
if (res != 0)
break;
2018-10-04 14:55:02 +00:00
}
2022-02-19 11:11:17 +00:00
return res;
}
ALWAYS_INLINE bool operator()(size_t lhs, size_t rhs) const
{
int res = compare(lhs, rhs);
return res < 0;
2018-10-04 14:55:02 +00:00
}
};
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
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)
2012-07-17 20:05:36 +00:00
{
2022-01-29 15:50:55 +00:00
const auto & sort_column_description = description[i];
2019-12-06 22:22:12 +00:00
const IColumn * column = block.getByName(sort_column_description.column_name).column.get();
2022-01-29 15:50:55 +00:00
if (isCollationRequired(sort_column_description))
{
if (!column->isCollationSupported())
throw Exception(ErrorCodes::BAD_COLLATION, "Collations could be specified only for String, LowCardinality(String), "
"Nullable(String) or for Array or Tuple, containing them.");
2012-07-17 20:05:36 +00:00
}
2022-01-29 15:50:55 +00:00
result.emplace_back(ColumnWithSortDescription{column, sort_column_description, isColumnConst(*column)});
2012-07-17 20:05:36 +00:00
}
2022-01-29 15:50:55 +00:00
return result;
}
2012-07-17 20:05:36 +00:00
void getBlockSortPermutationImpl(const Block & block, const SortDescription & description, IColumn::PermutationSortStability stability, UInt64 limit, IColumn::Permutation & permutation)
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 (unlikely(all_const))
return;
2022-01-29 15:50:55 +00:00
/// If only one column to sort by
2022-01-29 15:50:55 +00:00
if (columns_with_sort_descriptions.size() == 1)
2012-07-17 20:05:36 +00:00
{
2022-01-29 15:50:55 +00:00
auto & column_with_sort_description = columns_with_sort_descriptions[0];
IColumn::PermutationSortDirection direction = column_with_sort_description.description.direction == -1 ? IColumn::PermutationSortDirection::Descending : IColumn::PermutationSortDirection::Ascending;
2022-01-29 15:50:55 +00:00
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, direction, stability, limit, nan_direction_hint, permutation);
2022-01-28 20:49:58 +00:00
else
column->getPermutation(direction, stability, limit, nan_direction_hint, permutation);
2012-07-17 20:05:36 +00:00
}
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)
2013-05-28 16:56:05 +00:00
{
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);
IColumn::PermutationSortDirection direction = column_with_sort_description.description.direction == -1 ? IColumn::PermutationSortDirection::Descending : IColumn::PermutationSortDirection::Ascending;
2022-01-29 15:50:55 +00:00
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, direction, stability, 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
{
column->updatePermutation(direction, stability, limit, nan_direction_hint, permutation, ranges);
2020-05-12 00:58:58 +00:00
}
2013-05-28 16:56:05 +00:00
}
}
}
}
void sortBlock(Block & block, const SortDescription & description, UInt64 limit)
{
IColumn::Permutation permutation;
getBlockSortPermutationImpl(block, description, IColumn::PermutationSortStability::Unstable, limit, permutation);
if (permutation.empty())
return;
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 stableSortBlock(Block & block, const SortDescription & description)
2013-11-29 22:10:15 +00:00
{
if (!block)
return;
2022-02-19 11:11:17 +00:00
IColumn::Permutation permutation;
getBlockSortPermutationImpl(block, description, IColumn::PermutationSortStability::Stable, 0, permutation);
2022-02-19 11:11:17 +00:00
if (permutation.empty())
return;
2022-02-19 11:11:17 +00:00
size_t columns = block.columns();
for (size_t i = 0; i < columns; ++i)
{
auto & column_to_sort = block.getByPosition(i).column;
column_to_sort = column_to_sort->permute(permutation, 0);
}
2022-02-19 11:11:17 +00:00
}
void stableGetPermutation(const Block & block, const SortDescription & description, IColumn::Permutation & out_permutation)
{
if (!block)
return;
2013-11-29 22:10:15 +00:00
getBlockSortPermutationImpl(block, description, IColumn::PermutationSortStability::Stable, 0, out_permutation);
}
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;
}
2012-07-17 20:05:36 +00:00
}