2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/sortBlock.h>
|
2012-07-17 20:05:36 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Columns/ColumnString.h>
|
2017-07-13 20:58:19 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2017-03-12 13:01:19 +00:00
|
|
|
|
2012-07-17 20:05:36 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2016-01-11 21:46:36 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int BAD_COLLATION;
|
2016-01-11 21:46:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
using ColumnsWithSortDescriptions = std::vector<std::pair<const IColumn *, SortColumnDescription>>;
|
2013-05-28 16:56:05 +00:00
|
|
|
|
2016-11-20 04:47:51 +00:00
|
|
|
static ColumnsWithSortDescriptions getColumnsWithSortDescription(const Block & block, const SortDescription & description)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t size = description.size();
|
|
|
|
ColumnsWithSortDescriptions res;
|
|
|
|
res.reserve(size);
|
2016-11-20 04:47:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
|
|
|
const IColumn * column = !description[i].column_name.empty()
|
|
|
|
? block.getByName(description[i].column_name).column.get()
|
|
|
|
: block.safeGetByPosition(description[i].column_number).column.get();
|
2016-11-20 04:47:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
res.emplace_back(column, description[i]);
|
|
|
|
}
|
2016-11-20 04:47:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return res;
|
2016-11-20 04:47:51 +00:00
|
|
|
}
|
|
|
|
|
2013-05-28 16:56:05 +00:00
|
|
|
|
|
|
|
static inline bool needCollation(const IColumn * column, const SortColumnDescription & description)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!description.collator)
|
|
|
|
return false;
|
2015-01-07 15:30:11 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!typeid_cast<const ColumnString *>(column)) /// TODO Nullable(String)
|
|
|
|
throw Exception("Collations could be specified only for String columns.", ErrorCodes::BAD_COLLATION);
|
2015-01-07 15:30:11 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
2013-05-28 16:56:05 +00:00
|
|
|
}
|
|
|
|
|
2012-07-17 20:05:36 +00:00
|
|
|
|
|
|
|
struct PartialSortingLess
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
const ColumnsWithSortDescriptions & columns;
|
|
|
|
|
2017-09-07 21:04:48 +00:00
|
|
|
explicit PartialSortingLess(const ColumnsWithSortDescriptions & columns_) : columns(columns_) {}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
bool operator() (size_t a, size_t b) const
|
|
|
|
{
|
|
|
|
for (ColumnsWithSortDescriptions::const_iterator it = columns.begin(); it != columns.end(); ++it)
|
|
|
|
{
|
|
|
|
int res = it->second.direction * it->first->compareAt(a, b, *it->first, it->second.nulls_direction);
|
|
|
|
if (res < 0)
|
|
|
|
return true;
|
|
|
|
else if (res > 0)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2013-05-28 16:56:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct PartialSortingLessWithCollation
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
const ColumnsWithSortDescriptions & columns;
|
|
|
|
|
2017-09-07 21:04:48 +00:00
|
|
|
explicit PartialSortingLessWithCollation(const ColumnsWithSortDescriptions & columns_) : columns(columns_) {}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
bool operator() (size_t a, size_t b) const
|
|
|
|
{
|
|
|
|
for (ColumnsWithSortDescriptions::const_iterator it = columns.begin(); it != columns.end(); ++it)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
if (needCollation(it->first, it->second))
|
|
|
|
{
|
|
|
|
const ColumnString & column_string = typeid_cast<const ColumnString &>(*it->first);
|
|
|
|
res = column_string.compareAtWithCollation(a, b, *it->first, *it->second.collator);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
res = it->first->compareAt(a, b, *it->first, it->second.nulls_direction);
|
|
|
|
|
|
|
|
res *= it->second.direction;
|
|
|
|
if (res < 0)
|
|
|
|
return true;
|
|
|
|
else if (res > 0)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2012-07-17 20:05:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-09-16 05:44:47 +00:00
|
|
|
void sortBlock(Block & block, const SortDescription & description, size_t limit)
|
2012-07-17 20:05:36 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!block)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/// If only one column to sort by
|
|
|
|
if (description.size() == 1)
|
|
|
|
{
|
|
|
|
bool reverse = description[0].direction == -1;
|
|
|
|
|
|
|
|
const IColumn * column = !description[0].column_name.empty()
|
|
|
|
? block.getByName(description[0].column_name).column.get()
|
|
|
|
: block.safeGetByPosition(description[0].column_number).column.get();
|
|
|
|
|
|
|
|
IColumn::Permutation perm;
|
|
|
|
if (needCollation(column, description[0]))
|
|
|
|
{
|
|
|
|
const ColumnString & column_string = typeid_cast<const ColumnString &>(*column);
|
|
|
|
column_string.getPermutationWithCollation(*description[0].collator, reverse, limit, perm);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
column->getPermutation(reverse, limit, description[0].nulls_direction, perm);
|
|
|
|
|
|
|
|
size_t columns = block.columns();
|
|
|
|
for (size_t i = 0; i < columns; ++i)
|
2018-06-30 21:35:01 +00:00
|
|
|
block.getByPosition(i).column = block.getByPosition(i).column->permute(perm, limit);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t size = block.rows();
|
|
|
|
IColumn::Permutation perm(size);
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
perm[i] = i;
|
|
|
|
|
|
|
|
if (limit >= size)
|
|
|
|
limit = 0;
|
|
|
|
|
|
|
|
bool need_collation = false;
|
|
|
|
ColumnsWithSortDescriptions columns_with_sort_desc = getColumnsWithSortDescription(block, description);
|
|
|
|
|
|
|
|
for (size_t i = 0, size = description.size(); i < size; ++i)
|
|
|
|
{
|
|
|
|
if (needCollation(columns_with_sort_desc[i].first, description[i]))
|
|
|
|
{
|
|
|
|
need_collation = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (need_collation)
|
|
|
|
{
|
|
|
|
PartialSortingLessWithCollation less_with_collation(columns_with_sort_desc);
|
|
|
|
|
|
|
|
if (limit)
|
|
|
|
std::partial_sort(perm.begin(), perm.begin() + limit, perm.end(), less_with_collation);
|
|
|
|
else
|
|
|
|
std::sort(perm.begin(), perm.end(), less_with_collation);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PartialSortingLess less(columns_with_sort_desc);
|
|
|
|
|
|
|
|
if (limit)
|
|
|
|
std::partial_sort(perm.begin(), perm.begin() + limit, perm.end(), less);
|
|
|
|
else
|
|
|
|
std::sort(perm.begin(), perm.end(), less);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t columns = block.columns();
|
|
|
|
for (size_t i = 0; i < columns; ++i)
|
2018-06-30 21:35:01 +00:00
|
|
|
block.getByPosition(i).column = block.getByPosition(i).column->permute(perm, limit);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2012-07-17 20:05:36 +00:00
|
|
|
}
|
|
|
|
|
2013-11-29 22:10:15 +00:00
|
|
|
|
2015-08-14 02:45:40 +00:00
|
|
|
void stableGetPermutation(const Block & block, const SortDescription & description, IColumn::Permutation & out_permutation)
|
2013-11-29 22:10:15 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!block)
|
|
|
|
return;
|
2013-11-29 22:10:15 +00:00
|
|
|
|
2017-04-01 07:20:54 +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
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
ColumnsWithSortDescriptions columns_with_sort_desc = getColumnsWithSortDescription(block, description);
|
2013-11-29 22:10:15 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::stable_sort(out_permutation.begin(), out_permutation.end(), PartialSortingLess(columns_with_sort_desc));
|
2015-08-14 02:45:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-21 15:47:32 +00:00
|
|
|
bool isAlreadySorted(const Block & block, const SortDescription & description)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!block)
|
|
|
|
return true;
|
2015-11-21 15:47:32 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t rows = block.rows();
|
2015-11-21 15:47:32 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
ColumnsWithSortDescriptions columns_with_sort_desc = getColumnsWithSortDescription(block, description);
|
2015-11-21 15:47:32 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
PartialSortingLess less(columns_with_sort_desc);
|
2015-11-21 15:47:32 +00:00
|
|
|
|
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.
|
|
|
|
*/
|
2017-04-01 07:20:54 +00:00
|
|
|
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;
|
2015-11-21 15:47:32 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (less(curr_position, prev_position))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2015-11-21 15:47:32 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
for (size_t i = 1; i < rows; ++i)
|
|
|
|
if (less(i, i - 1))
|
|
|
|
return false;
|
2015-11-21 15:47:32 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
2015-11-21 15:47:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-14 02:45:40 +00:00
|
|
|
void stableSortBlock(Block & block, const SortDescription & description)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!block)
|
|
|
|
return;
|
2015-08-14 02:45:40 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
IColumn::Permutation perm;
|
|
|
|
stableGetPermutation(block, description, perm);
|
2013-11-29 22:10:15 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t columns = block.columns();
|
|
|
|
for (size_t i = 0; i < columns; ++i)
|
|
|
|
block.safeGetByPosition(i).column = block.safeGetByPosition(i).column->permute(perm, 0);
|
2013-11-29 22:10:15 +00:00
|
|
|
}
|
|
|
|
|
2012-07-17 20:05:36 +00:00
|
|
|
}
|