This commit is contained in:
Nickita Taranov 2022-03-25 17:20:29 +01:00
parent 4c51329ad6
commit b095838444
8 changed files with 19 additions and 21 deletions

View File

@ -1527,15 +1527,12 @@ ActionsDAG::SplitResult ActionsDAG::splitActionsBeforeArrayJoin(const NameSet &
return res;
}
ActionsDAG::SplitResult ActionsDAG::splitActionsBySortingDescription(const SortDescription & sort_description) const
ActionsDAG::SplitResult ActionsDAG::splitActionsBySortingDescription(const NameSet & sort_columns) const
{
std::unordered_set<const Node *> split_nodes;
for (const auto & sort_column : sort_description)
{
const auto * node = tryFindInIndex(sort_column.column_name);
if (node)
for (const auto & sort_column : sort_columns)
if (const auto * node = tryFindInIndex(sort_column))
split_nodes.insert(node);
}
auto res = split(split_nodes);
res.second->project_input = project_input;
return res;

View File

@ -7,8 +7,6 @@
#include "config_core.h"
#include <Core/SortDescription.h>
namespace DB
{
@ -276,8 +274,9 @@ public:
/// Index of initial actions must contain column_name.
SplitResult splitActionsForFilter(const std::string & column_name) const;
///
SplitResult splitActionsBySortingDescription(const SortDescription & sort_description) const;
/// Splits actions into two parts. The first part contains all the calculations required to calculate sort_columns.
/// The second contains the rest.
SplitResult splitActionsBySortingDescription(const NameSet & sort_columns) const;
/// Create actions which may calculate part of filter using only available_inputs.
/// If nothing may be calculated, returns nullptr.

View File

@ -44,7 +44,8 @@ size_t tryMergeExpressions(QueryPlan::Node * parent_node, QueryPlan::Nodes &);
/// May split FilterStep and push down only part of it.
size_t tryPushDownFilter(QueryPlan::Node * parent_node, QueryPlan::Nodes & nodes);
///
/// Move ExpressionStep up if possible.
/// May split ExpressionStep and lift up only part of it.
size_t tryExecuteFunctionsAfterSorting(QueryPlan::Node * parent_node, QueryPlan::Nodes & nodes);
inline const auto & getOptimizations()

View File

@ -42,11 +42,14 @@ size_t tryExecuteFunctionsAfterSorting(QueryPlan::Node * parent_node, QueryPlan:
if (!sorting_step || !expression_step)
return 0;
const auto & sort_columns = sorting_step->getSortDescription();
NameSet sort_columns;
for (const auto & col : sorting_step->getSortDescription())
sort_columns.insert(col.column_name);
const auto & expression = expression_step->getExpression();
for (auto sc : sort_columns)
LOG_TRACE(&Poco::Logger::get("Optimizer"), "sort_columns: {}", sc.column_name);
LOG_TRACE(&Poco::Logger::get("Optimizer"), "sort_columns: {}", fmt::join(sort_columns, ", "));
auto split_actions = expression->splitActionsBySortingDescription(sort_columns);
LOG_TRACE(&Poco::Logger::get("Optimizer"), "source: {}", expression->dumpDAG());

View File

@ -89,17 +89,15 @@ SortingStep::SortingStep(
output_stream->sort_mode = DataStream::SortMode::Stream;
}
void SortingStep::updateInputStream(const DataStream & input_stream)
void SortingStep::updateInputStream(DataStream input_stream)
{
input_streams.clear();
input_streams.emplace_back(input_stream);
input_streams.push_back(std::move(input_stream));
}
void SortingStep::updateOutputStream(Block result_header)
{
if (input_streams.size() != 1)
throw std::runtime_error{"wasted"};
output_stream = createOutputStream(input_streams.at(0), result_header, getDataStreamTraits());
output_stream = createOutputStream(input_streams.front(), std::move(result_header), getDataStreamTraits());
}
void SortingStep::updateLimit(size_t limit_)

View File

@ -49,7 +49,7 @@ public:
/// Add limit or change it to lower value.
void updateLimit(size_t limit_);
void updateInputStream(const DataStream & input_stream);
void updateInputStream(DataStream input_stream);
void updateOutputStream(Block result_header);
SortDescription getSortDescription() const { return result_description; }

View File

@ -148,7 +148,7 @@ Expression
Limit
Expression
Sorting
> Expression should be divided into two subnodes and only one of them could be moved after Sorting
> Expression should be divided into two subexpressions and only one of them should be moved after Sorting
Expression
Limit
Expression

View File

@ -202,7 +202,7 @@ echo "> the whole Expression node could be moved after Sorting"
$CLICKHOUSE_CLIENT -q "
explain select sipHash64(number) from numbers(100) order by number limit 5" |
sed 's/ //g' | grep -o "^ *\(Expression\|Limit\|Sorting\)"
echo "> Expression should be divided into two subnodes and only one of them could be moved after Sorting"
echo "> Expression should be divided into two subexpressions and only one of them should be moved after Sorting"
$CLICKHOUSE_CLIENT -q "
explain select sipHash64(number) from numbers(100) order by number + 1 limit 5" |
sed 's/ //g' | grep -o "^ *\(Expression\|Limit\|Sorting\)"