ClickHouse/src/Interpreters/OptimizeIfChains.h

40 lines
927 B
C++
Raw Normal View History

2019-12-23 15:08:18 +00:00
#pragma once
2019-12-30 22:24:19 +00:00
#include <Parsers/IAST.h>
#include <Parsers/ASTFunction.h>
#include <Interpreters/InDepthNodeVisitor.h>
2019-12-23 15:08:18 +00:00
namespace DB
{
2019-12-27 18:52:18 +00:00
/// It converts if-chain to multiIf.
class OptimizeIfChainsVisitor
{
public:
2019-12-28 22:08:18 +00:00
OptimizeIfChainsVisitor() = default;
2019-12-27 18:52:18 +00:00
void visit(ASTPtr & ast);
2019-12-23 15:08:18 +00:00
2019-12-30 22:24:19 +00:00
private:
ASTs ifChain(const ASTPtr & child);
2019-12-27 18:52:18 +00:00
};
2019-12-23 15:08:18 +00:00
/// Replaces multiIf with one condition to if,
/// because it's more efficient.
class OptimizeMultiIfToIfData
{
public:
using TypeToVisit = ASTFunction;
void visit(ASTFunction & function, ASTPtr &)
{
/// 3 args: condition, then branch, else branch.
if (function.name == "multiIf" && (function.arguments && function.arguments->children.size() == 3))
function.name = "if";
}
};
using OptimizeMultiIfToIfMatcher = OneTypeMatcher<OptimizeMultiIfToIfData>;
using OptimizeMultiIfToIfVisitor = InDepthNodeVisitor<OptimizeMultiIfToIfMatcher, true>;
2019-12-27 18:52:18 +00:00
}