ClickHouse/src/Interpreters/OptimizeIfChains.cpp

93 lines
2.8 KiB
C++
Raw Normal View History

2019-12-23 15:08:18 +00:00
#include <Common/typeid_cast.h>
#include <Parsers/ASTLiteral.h>
#include <Parsers/ASTFunction.h>
#include <Parsers/ASTExpressionList.h>
#include <Interpreters/OptimizeIfChains.h>
#include <IO/WriteHelpers.h>
namespace DB
{
2019-12-27 18:52:18 +00:00
namespace ErrorCodes
{
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
2019-12-30 22:24:19 +00:00
extern const int UNEXPECTED_AST_STRUCTURE;
2019-12-27 18:52:18 +00:00
}
void OptimizeIfChainsVisitor::visit(ASTPtr & current_ast)
{
if (!current_ast)
return;
2019-12-30 22:24:19 +00:00
2019-12-27 18:52:18 +00:00
for (ASTPtr & child : current_ast->children)
{
2019-12-30 22:24:19 +00:00
/// Fallthrough cases
const auto * function_node = child->as<ASTFunction>();
if (!function_node || function_node->name != "if" || !function_node->arguments)
{
visit(child);
continue;
}
2019-12-23 15:08:18 +00:00
2019-12-30 22:24:19 +00:00
const auto * function_args = function_node->arguments->as<ASTExpressionList>();
if (!function_args || function_args->children.size() != 3 || !function_args->children[2])
2019-12-23 15:08:18 +00:00
{
2019-12-27 18:52:18 +00:00
visit(child);
continue;
2019-12-23 15:08:18 +00:00
}
2019-12-27 18:52:18 +00:00
2019-12-30 22:24:19 +00:00
const auto * else_arg = function_args->children[2]->as<ASTFunction>();
if (!else_arg || else_arg->name != "if")
{
visit(child);
continue;
}
/// The case of:
/// if(cond, a, if(...))
auto chain = ifChain(child);
std::reverse(chain.begin(), chain.end());
2019-12-27 18:52:18 +00:00
child->as<ASTFunction>()->name = "multiIf";
child->as<ASTFunction>()->arguments->children = std::move(chain);
2019-12-23 15:08:18 +00:00
}
2019-12-27 18:52:18 +00:00
}
2019-12-23 15:08:18 +00:00
2019-12-30 22:24:19 +00:00
ASTs OptimizeIfChainsVisitor::ifChain(const ASTPtr & child)
2019-12-27 18:52:18 +00:00
{
2019-12-30 22:24:19 +00:00
const auto * function_node = child->as<ASTFunction>();
if (!function_node || !function_node->arguments)
throw Exception("Unexpected AST for function 'if'", ErrorCodes::UNEXPECTED_AST_STRUCTURE);
2019-12-23 15:08:18 +00:00
2019-12-30 22:24:19 +00:00
const auto * function_args = function_node->arguments->as<ASTExpressionList>();
2019-12-27 18:52:18 +00:00
2019-12-30 22:24:19 +00:00
if (!function_args || function_args->children.size() != 3)
throw Exception("Wrong number of arguments for function 'if' (" + toString(function_args->children.size()) + " instead of 3)",
2019-12-27 18:52:18 +00:00
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
2019-12-30 22:24:19 +00:00
const auto * else_arg = function_args->children[2]->as<ASTFunction>();
2020-01-11 09:50:41 +00:00
/// Recursively collect arguments from the innermost if ("head-recursion").
2019-12-30 22:24:19 +00:00
/// Arguments will be returned in reverse order.
if (else_arg && else_arg->name == "if")
2019-12-27 18:52:18 +00:00
{
2019-12-30 22:24:19 +00:00
auto cur = ifChain(function_node->arguments->children[2]);
2019-12-27 18:52:18 +00:00
cur.push_back(function_node->arguments->children[1]);
cur.push_back(function_node->arguments->children[0]);
return cur;
}
else
{
ASTs end;
2019-12-30 22:24:19 +00:00
end.reserve(3);
2019-12-27 18:52:18 +00:00
end.push_back(function_node->arguments->children[2]);
end.push_back(function_node->arguments->children[1]);
end.push_back(function_node->arguments->children[0]);
return end;
2019-12-23 15:08:18 +00:00
}
2019-12-27 18:52:18 +00:00
}
}