Add comments.

This commit is contained in:
Nikolai Kochetov 2021-03-17 19:08:46 +03:00
parent 738cb1af62
commit 3eba817a68
3 changed files with 26 additions and 7 deletions

View File

@ -1360,7 +1360,6 @@ ColumnsWithTypeAndName prepareFunctionArguments(const std::vector<ActionsDAG::No
///
/// Result actions add single column with conjunction result (it is always last in index).
/// No other columns are added or removed.
ActionsDAGPtr ActionsDAG::cloneActionsForConjunction(std::vector<Node *> conjunction, const ColumnsWithTypeAndName & all_inputs)
{
if (conjunction.empty())
@ -1375,7 +1374,7 @@ ActionsDAGPtr ActionsDAG::cloneActionsForConjunction(std::vector<Node *> conjunc
std::make_shared<FunctionAnd>()));
std::unordered_map<const ActionsDAG::Node *, ActionsDAG::Node *> nodes_mapping;
std::unordered_map<std::string, std::list<Node *>> added_inputs;
std::unordered_map<std::string, std::list<Node *>> required_inputs;
struct Frame
{
@ -1418,7 +1417,7 @@ ActionsDAGPtr ActionsDAG::cloneActionsForConjunction(std::vector<Node *> conjunc
child = nodes_mapping[child];
if (node.type == ActionType::INPUT)
added_inputs[node.result_name].push_back(&node);
required_inputs[node.result_name].push_back(&node);
stack.pop();
}
@ -1429,7 +1428,7 @@ ActionsDAGPtr ActionsDAG::cloneActionsForConjunction(std::vector<Node *> conjunc
for (const auto & col : all_inputs)
{
Node * input;
auto & list = added_inputs[col.name];
auto & list = required_inputs[col.name];
if (list.empty())
input = &const_cast<Node &>(actions->addInput(col));
else
@ -1458,7 +1457,11 @@ ActionsDAGPtr ActionsDAG::cloneActionsForConjunction(std::vector<Node *> conjunc
return actions;
}
ActionsDAGPtr ActionsDAG::splitActionsForFilter(const std::string & filter_name, bool can_remove_filter, const Names & available_inputs, const ColumnsWithTypeAndName & all_inputs)
ActionsDAGPtr ActionsDAG::cloneActionsForFilterPushDown(
const std::string & filter_name,
bool can_remove_filter,
const Names & available_inputs,
const ColumnsWithTypeAndName & all_inputs)
{
Node * predicate;

View File

@ -292,7 +292,23 @@ public:
/// Otherwise, return actions which inputs are from available_inputs.
/// Returned actions add single column which may be used for filter.
/// Also, replace some nodes of current inputs to constant 1 in case they are filtered.
ActionsDAGPtr splitActionsForFilter(const std::string & filter_name, bool can_remove_filter, const Names & available_inputs, const ColumnsWithTypeAndName & all_inputs);
///
/// @param all_inputs should containt inputs from previous step, which will be used for result actions.
/// It is expected that all_inputs contain columns from available_inputs.
/// This parameter is needed to enforce result actions save columns order in block.
/// Otherwise for some queries, e.g. with GROUP BY, columns colum be swapped.
/// Example: SELECT sum(x), y, z FROM tab WHERE z > 0 and sum(x) > 0
/// Pushed condition: z > 0
/// GROUP BY step will transform columns `x, y, z` -> `sum(x), y, z`
/// If we just add filter step with actions `z -> z > 0` before GROUP BY,
/// columns will be transformed like `x, y, z` -> `z, z > 0, x, y` -(remove filter)-> `z, z, y`.
/// To avoid it, add inputs from `all_inputs` list,
/// so actions `x, y, z -> x, y, z, z > 0` -(remove filter)-> `x, y, z` will not change columns order.
ActionsDAGPtr cloneActionsForFilterPushDown(
const std::string & filter_name,
bool can_remove_filter,
const Names & available_inputs,
const ColumnsWithTypeAndName & all_inputs);
private:
Node & addNode(Node node, bool can_replace = false, bool add_to_index = true);

View File

@ -44,7 +44,7 @@ static size_t tryAddNewFilterStep(
// std::cerr << "Filter: \n" << expression->dumpDAG() << std::endl;
const auto & all_inputs = child->getInputStreams().front().header.getColumnsWithTypeAndName();
auto split_filter = expression->splitActionsForFilter(filter_column_name, removes_filter, allowed_inputs, all_inputs);
auto split_filter = expression->cloneActionsForFilterPushDown(filter_column_name, removes_filter, allowed_inputs, all_inputs);
if (!split_filter)
return 0;