未验证 提交 420f2958 编写于 作者: A alesapin 提交者: GitHub

Merge pull request #19991 from ClickHouse/constant-propagation-fix

Do not use inputs which values are known constants in ActionsDAG.
......@@ -39,7 +39,17 @@ ActionsDAG::ActionsDAG(const ColumnsWithTypeAndName & inputs_)
for (const auto & input : inputs_)
{
if (input.column && isColumnConst(*input.column))
{
addInput(input, true);
/// Here we also add column.
/// It will allow to remove input which is actually constant (after projection).
/// Also, some transforms from query pipeline may randomly materialize constants,
/// without any respect to header structure. So, it is a way to drop materialized column and use
/// constant value from header.
/// We cannot remove such input right now cause inputs positions are important in some cases.
addColumn(input, true);
}
else
addInput(input.name, input.type, true);
}
......@@ -116,7 +126,6 @@ ActionsDAG::Node & ActionsDAG::addAlias(Node & child, std::string alias, bool ca
node.result_type = child.result_type;
node.result_name = std::move(alias);
node.column = child.column;
node.allow_constant_folding = child.allow_constant_folding;
node.children.emplace_back(&child);
return addNode(std::move(node), can_replace);
......@@ -184,7 +193,6 @@ ActionsDAG::Node & ActionsDAG::addFunction(
for (size_t i = 0; i < num_arguments; ++i)
{
auto & child = *node.children[i];
node.allow_constant_folding = node.allow_constant_folding && child.allow_constant_folding;
ColumnWithTypeAndName argument;
argument.column = child.column;
......@@ -349,10 +357,15 @@ void ActionsDAG::removeUnusedActions()
stack.push(node);
}
/// We cannot remove arrayJoin because it changes the number of rows.
for (auto & node : nodes)
{
if (node.type == ActionType::ARRAY_JOIN && visited_nodes.count(&node) == 0)
/// We cannot remove function with side effects even if it returns constant (e.g. ignore(...)).
bool prevent_constant_folding = node.column && isColumnConst(*node.column) && !node.allow_constant_folding;
/// We cannot remove arrayJoin because it changes the number of rows.
bool is_array_join = node.type == ActionType::ARRAY_JOIN;
bool must_keep_node = is_array_join || prevent_constant_folding;
if (must_keep_node && visited_nodes.count(&node) == 0)
{
visited_nodes.insert(&node);
stack.push(&node);
......@@ -410,7 +423,6 @@ void ActionsDAG::addAliases(const NamesWithAliases & aliases, std::vector<Node *
node.result_type = child->result_type;
node.result_name = std::move(item.second);
node.column = child->column;
node.allow_constant_folding = child->allow_constant_folding;
node.children.emplace_back(child);
auto & alias = addNode(std::move(node), true);
......
......@@ -867,7 +867,7 @@ void MergeTreeRangeReader::executePrewhereActionsAndFilterColumns(ReadResult & r
if (result.totalRowsPerGranule() == 0)
result.setFilterConstFalse();
/// If we need to filter in PREWHERE
else if (prewhere->need_filter || result.need_filter)
else if (prewhere->need_filter || result.need_filter || prewhere->remove_prewhere_column)
{
/// If there is a filter and without optimized
if (result.getFilter() && last_reader_in_chain)
......
......@@ -3,3 +3,4 @@
│ b │ 2018-01-01 │ B │ 2018-01-01 │ 0.10 │
│ c │ 2018-01-01 │ C │ 2018-01-01 │ 0.10 │
└───┴────────────┴───┴────────────┴───────────┘
\N \N \N \N 0 0
......@@ -11,5 +11,7 @@ set join_algorithm = 'partial_merge';
SELECT * FROM table1 AS t1 ALL LEFT JOIN (SELECT *, '0.10', c, d AS b FROM table2) AS t2 USING (a, b) ORDER BY d ASC FORMAT PrettyCompact settings max_rows_in_join = 1;
SELECT pow('0.0000000257', NULL), pow(pow(NULL, NULL), NULL) - NULL, (val + NULL) = (rval * 0), * FROM (SELECT (val + 256) = (NULL * NULL), toLowCardinality(toNullable(dummy)) AS val FROM system.one) AS s1 ANY LEFT JOIN (SELECT toLowCardinality(dummy) AS rval FROM system.one) AS s2 ON (val + 0) = (rval * 255) settings max_rows_in_join = 1;
DROP TABLE IF EXISTS table1;
DROP TABLE IF EXISTS table2;
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册