1#pragma once
2
3#include <Core/Block.h>
4#include <Core/SortDescription.h>
5
6
7namespace DB
8{
9
10/// Sort one block by `description`. If limit != 0, then the partial sort of the first `limit` rows is produced.
11void sortBlock(Block & block, const SortDescription & description, UInt64 limit = 0);
12
13
14/** Used only in StorageMergeTree to sort the data with INSERT.
15 * Sorting is stable. This is important for keeping the order of rows in the CollapsingMergeTree engine
16 * - because based on the order of rows it is determined whether to delete or leave groups of rows when collapsing.
17 * Collations are not supported. Partial sorting is not supported.
18 */
19void stableSortBlock(Block & block, const SortDescription & description);
20
21/** Same as stableSortBlock, but do not sort the block, but only calculate the permutation of the values,
22 * so that you can rearrange the column values yourself.
23 */
24void stableGetPermutation(const Block & block, const SortDescription & description, IColumn::Permutation & out_permutation);
25
26
27/** Quickly check whether the block is already sorted. If the block is not sorted - returns false as fast as possible.
28 * Collations are not supported.
29 */
30bool isAlreadySorted(const Block & block, const SortDescription & description);
31
32/// Column with description for sort
33struct ColumnWithSortDescription
34{
35 const IColumn * column;
36 SortColumnDescription description;
37
38 /// It means, that this column is ColumnConst
39 bool column_const = false;
40};
41
42using ColumnsWithSortDescriptions = std::vector<ColumnWithSortDescription>;
43
44ColumnsWithSortDescriptions getColumnsWithSortDescription(const Block & block, const SortDescription & description);
45
46}
47