2011-09-04 01:42:14 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <vector>
|
2017-03-12 12:56:59 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <cstddef>
|
2017-03-16 15:04:05 +00:00
|
|
|
#include <string>
|
2019-04-23 01:48:51 +00:00
|
|
|
#include <Core/Field.h>
|
2020-07-16 22:01:08 +00:00
|
|
|
#include <Core/SettingsEnums.h>
|
2021-10-31 16:22:20 +00:00
|
|
|
#include <Common/IntervalKind.h>
|
2022-03-27 17:33:22 +00:00
|
|
|
#include <DataTypes/IDataType.h>
|
2022-07-12 13:14:10 +00:00
|
|
|
#include <Columns/Collator.h>
|
2011-09-04 01:42:14 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2021-04-16 16:36:59 +00:00
|
|
|
namespace JSONBuilder
|
|
|
|
{
|
|
|
|
class JSONMap;
|
|
|
|
class IItem;
|
|
|
|
using ItemPtr = std::unique_ptr<IItem>;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Block;
|
|
|
|
|
2019-04-21 23:04:23 +00:00
|
|
|
struct FillColumnDescription
|
|
|
|
{
|
2019-08-19 20:22:45 +00:00
|
|
|
/// All missed values in range [FROM, TO) will be filled
|
|
|
|
/// Range [FROM, TO) respects sorting direction
|
2019-04-21 23:04:23 +00:00
|
|
|
Field fill_from; /// Fill value >= FILL_FROM
|
2022-06-06 02:39:21 +00:00
|
|
|
DataTypePtr fill_from_type;
|
2019-08-19 20:22:45 +00:00
|
|
|
Field fill_to; /// Fill value + STEP < FILL_TO
|
2022-06-06 02:39:21 +00:00
|
|
|
DataTypePtr fill_to_type;
|
2021-10-31 16:22:20 +00:00
|
|
|
Field fill_step; /// Default = +1 or -1 according to direction
|
|
|
|
std::optional<IntervalKind> step_kind;
|
|
|
|
|
|
|
|
using StepFunction = std::function<void(Field &)>;
|
|
|
|
StepFunction step_func;
|
2019-04-21 23:04:23 +00:00
|
|
|
};
|
|
|
|
|
2017-04-30 13:50:16 +00:00
|
|
|
/// Description of the sorting rule by one column.
|
2011-09-04 01:42:14 +00:00
|
|
|
struct SortColumnDescription
|
|
|
|
{
|
2017-09-08 02:29:47 +00:00
|
|
|
std::string column_name; /// The name of the column.
|
|
|
|
int direction; /// 1 - ascending, -1 - descending.
|
|
|
|
int nulls_direction; /// 1 - NULLs and NaNs are greater, -1 - less.
|
|
|
|
/// To achieve NULLS LAST, set it equal to direction, to achieve NULLS FIRST, set it opposite.
|
|
|
|
std::shared_ptr<Collator> collator; /// Collator for locale-specific comparison of strings
|
2019-04-21 23:04:23 +00:00
|
|
|
bool with_fill;
|
|
|
|
FillColumnDescription fill_description;
|
2019-04-21 03:36:59 +00:00
|
|
|
|
2022-09-18 22:21:13 +00:00
|
|
|
SortColumnDescription() = default;
|
|
|
|
|
2021-07-21 17:03:33 +00:00
|
|
|
explicit SortColumnDescription(
|
2022-11-23 18:38:12 +00:00
|
|
|
std::string column_name_,
|
2022-04-05 19:02:34 +00:00
|
|
|
int direction_ = 1,
|
|
|
|
int nulls_direction_ = 1,
|
|
|
|
const std::shared_ptr<Collator> & collator_ = nullptr,
|
|
|
|
bool with_fill_ = false,
|
|
|
|
const FillColumnDescription & fill_description_ = {})
|
2022-11-23 18:38:12 +00:00
|
|
|
: column_name(std::move(column_name_))
|
2022-04-05 19:02:34 +00:00
|
|
|
, direction(direction_)
|
|
|
|
, nulls_direction(nulls_direction_)
|
|
|
|
, collator(collator_)
|
|
|
|
, with_fill(with_fill_)
|
|
|
|
, fill_description(fill_description_)
|
|
|
|
{
|
|
|
|
}
|
2018-10-19 12:02:31 +00:00
|
|
|
|
2022-07-12 14:27:57 +00:00
|
|
|
static bool compareCollators(const std::shared_ptr<Collator> & a, const std::shared_ptr<Collator> & b)
|
|
|
|
{
|
|
|
|
if (unlikely(a && b))
|
2022-08-04 19:30:25 +00:00
|
|
|
return *a == *b;
|
2022-07-12 14:27:57 +00:00
|
|
|
|
|
|
|
return a == b;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const SortColumnDescription & other) const
|
2018-10-19 12:02:31 +00:00
|
|
|
{
|
2022-07-12 13:14:10 +00:00
|
|
|
return column_name == other.column_name && direction == other.direction && nulls_direction == other.nulls_direction
|
2022-07-12 14:27:57 +00:00
|
|
|
&& compareCollators(collator, other.collator);
|
2018-10-19 12:02:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator != (const SortColumnDescription & other) const
|
|
|
|
{
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
2020-05-05 14:35:23 +00:00
|
|
|
|
2022-04-04 12:17:15 +00:00
|
|
|
std::string dump() const { return fmt::format("{}:dir {}nulls {}", column_name, direction, nulls_direction); }
|
|
|
|
|
|
|
|
void explain(JSONBuilder::JSONMap & map) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SortColumnDescriptionWithColumnIndex
|
|
|
|
{
|
|
|
|
SortColumnDescription base;
|
|
|
|
size_t column_number;
|
|
|
|
|
|
|
|
SortColumnDescriptionWithColumnIndex(SortColumnDescription description_, size_t column_number_)
|
|
|
|
: base(std::move(description_)), column_number(column_number_)
|
2020-05-07 15:37:19 +00:00
|
|
|
{
|
2020-05-05 14:35:23 +00:00
|
|
|
}
|
2022-07-01 19:43:28 +00:00
|
|
|
|
|
|
|
bool operator==(const SortColumnDescriptionWithColumnIndex & other) const
|
|
|
|
{
|
|
|
|
return base == other.base && column_number == other.column_number;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const SortColumnDescriptionWithColumnIndex & other) const { return !(*this == other); }
|
2011-09-04 01:42:14 +00:00
|
|
|
};
|
|
|
|
|
2022-02-09 16:32:52 +00:00
|
|
|
class CompiledSortDescriptionFunctionHolder;
|
|
|
|
|
2017-04-30 13:50:16 +00:00
|
|
|
/// Description of the sorting rule for several columns.
|
2022-04-04 12:17:15 +00:00
|
|
|
using SortDescriptionWithPositions = std::vector<SortColumnDescriptionWithColumnIndex>;
|
2011-09-04 01:42:14 +00:00
|
|
|
|
2022-02-09 16:32:52 +00:00
|
|
|
class SortDescription : public std::vector<SortColumnDescription>
|
|
|
|
{
|
|
|
|
public:
|
2022-05-11 11:02:49 +00:00
|
|
|
/// Can be safely casted into JITSortDescriptionFunc
|
2022-02-09 16:32:52 +00:00
|
|
|
void * compiled_sort_description = nullptr;
|
|
|
|
std::shared_ptr<CompiledSortDescriptionFunctionHolder> compiled_sort_description_holder;
|
2022-05-11 11:02:49 +00:00
|
|
|
size_t min_count_to_compile_sort_description = 3;
|
|
|
|
bool compile_sort_description = false;
|
2022-07-01 19:43:28 +00:00
|
|
|
|
2022-07-08 22:48:55 +00:00
|
|
|
bool hasPrefix(const SortDescription & prefix) const;
|
2022-02-09 16:32:52 +00:00
|
|
|
};
|
|
|
|
|
2022-11-27 23:41:31 +00:00
|
|
|
/// Returns a copy of lhs containing only the prefix of columns matching rhs's columns.
|
|
|
|
SortDescription commonPrefix(const SortDescription & lhs, const SortDescription & rhs);
|
|
|
|
|
2022-05-11 11:02:49 +00:00
|
|
|
/** Compile sort description for header_types.
|
2022-05-11 13:35:37 +00:00
|
|
|
* Description is compiled only if compilation attempts to compile identical description is more than min_count_to_compile_sort_description.
|
2022-05-11 11:02:49 +00:00
|
|
|
*/
|
2022-06-16 14:40:02 +00:00
|
|
|
void compileSortDescriptionIfNeeded(SortDescription & description, const DataTypes & sort_description_types, bool increase_compile_attempts);
|
2022-02-09 16:32:52 +00:00
|
|
|
|
2020-07-05 16:26:57 +00:00
|
|
|
/// Outputs user-readable description into `out`.
|
2022-04-04 12:17:15 +00:00
|
|
|
void dumpSortDescription(const SortDescription & description, WriteBuffer & out);
|
2020-06-24 12:09:01 +00:00
|
|
|
|
2020-12-10 19:06:52 +00:00
|
|
|
std::string dumpSortDescription(const SortDescription & description);
|
|
|
|
|
2022-04-04 12:17:15 +00:00
|
|
|
JSONBuilder::ItemPtr explainSortDescription(const SortDescription & description);
|
2011-09-04 01:42:14 +00:00
|
|
|
}
|