2015-02-18 09:43:36 +00:00
|
|
|
|
#include <DB/Interpreters/LogicalExpressionsOptimizer.h>
|
|
|
|
|
|
2015-02-18 14:55:11 +00:00
|
|
|
|
#include <DB/Parsers/ASTFunction.h>
|
2015-02-18 09:43:36 +00:00
|
|
|
|
#include <DB/Parsers/ASTSelectQuery.h>
|
|
|
|
|
#include <DB/Parsers/ASTLiteral.h>
|
|
|
|
|
#include <DB/Parsers/queryToString.h>
|
|
|
|
|
|
2015-02-18 13:16:59 +00:00
|
|
|
|
#include <DB/Core/ErrorCodes.h>
|
|
|
|
|
|
2015-02-18 09:43:36 +00:00
|
|
|
|
#include <deque>
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
2015-02-18 14:55:11 +00:00
|
|
|
|
OrWithExpression::OrWithExpression(ASTFunction * or_function_, const std::string & expression_)
|
2015-02-18 09:43:36 +00:00
|
|
|
|
: or_function(or_function_), expression(expression_)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-18 14:55:11 +00:00
|
|
|
|
bool operator<(const OrWithExpression & lhs, const OrWithExpression & rhs)
|
2015-02-18 09:43:36 +00:00
|
|
|
|
{
|
2015-02-18 11:57:44 +00:00
|
|
|
|
std::ptrdiff_t res1 = lhs.or_function - rhs.or_function;
|
|
|
|
|
if (res1 < 0)
|
2015-02-18 09:43:36 +00:00
|
|
|
|
return true;
|
2015-02-18 11:57:44 +00:00
|
|
|
|
if (res1 > 0)
|
2015-02-18 09:43:36 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
2015-02-18 11:57:44 +00:00
|
|
|
|
int res2 = lhs.expression.compare(rhs.expression);
|
|
|
|
|
if (res2 < 0)
|
2015-02-18 09:43:36 +00:00
|
|
|
|
return true;
|
2015-02-18 11:57:44 +00:00
|
|
|
|
if (res2 > 0)
|
2015-02-18 09:43:36 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-18 15:11:47 +00:00
|
|
|
|
LogicalExpressionsOptimizer::LogicalExpressionsOptimizer(ASTSelectQuery * select_query_)
|
|
|
|
|
: select_query(select_query_)
|
2015-02-18 09:43:36 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LogicalExpressionsOptimizer::optimizeDisjunctiveEqualityChains()
|
|
|
|
|
{
|
|
|
|
|
if (select_query == nullptr)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
collectDisjunctiveEqualityChains();
|
|
|
|
|
|
2015-02-18 11:57:44 +00:00
|
|
|
|
for (const auto & chain : disjunctive_equalities_map)
|
2015-02-18 09:43:36 +00:00
|
|
|
|
{
|
2015-02-18 13:53:19 +00:00
|
|
|
|
if (!mayOptimizeDisjunctiveEqualityChain(chain))
|
2015-02-18 09:43:36 +00:00
|
|
|
|
continue;
|
2015-02-18 14:55:11 +00:00
|
|
|
|
replaceOrByIn(chain);
|
2015-02-18 09:43:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fixBrokenOrExpressions();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LogicalExpressionsOptimizer::collectDisjunctiveEqualityChains()
|
|
|
|
|
{
|
2015-02-18 15:11:47 +00:00
|
|
|
|
using Edge = std::pair<IAST *, IAST *>;
|
2015-02-18 09:43:36 +00:00
|
|
|
|
std::deque<Edge> to_visit;
|
2015-02-18 12:11:16 +00:00
|
|
|
|
|
2015-02-18 15:11:47 +00:00
|
|
|
|
to_visit.push_back(Edge(nullptr, select_query));
|
2015-02-18 09:43:36 +00:00
|
|
|
|
while (!to_visit.empty())
|
|
|
|
|
{
|
|
|
|
|
auto edge = to_visit.front();
|
2015-02-18 15:11:47 +00:00
|
|
|
|
auto from_node = edge.first;
|
|
|
|
|
auto to_node = edge.second;
|
2015-02-18 09:43:36 +00:00
|
|
|
|
|
|
|
|
|
to_visit.pop_front();
|
|
|
|
|
|
2015-02-18 13:53:19 +00:00
|
|
|
|
bool found_chain = false;
|
2015-02-18 09:43:36 +00:00
|
|
|
|
|
2015-02-18 15:11:47 +00:00
|
|
|
|
auto function = typeid_cast<ASTFunction *>(to_node);
|
2015-02-18 09:43:36 +00:00
|
|
|
|
if ((function != nullptr) && (function->name == "or") && (function->children.size() == 1))
|
|
|
|
|
{
|
2015-02-18 12:11:16 +00:00
|
|
|
|
auto expression_list = typeid_cast<ASTExpressionList *>(&*(function->children[0]));
|
2015-02-18 09:43:36 +00:00
|
|
|
|
if (expression_list != nullptr)
|
|
|
|
|
{
|
|
|
|
|
/// Цепочка элементов выражения OR.
|
|
|
|
|
for (auto child : expression_list->children)
|
|
|
|
|
{
|
2015-02-18 12:11:16 +00:00
|
|
|
|
auto equals = typeid_cast<ASTFunction *>(&*child);
|
2015-02-18 09:43:36 +00:00
|
|
|
|
if ((equals != nullptr) && (equals->name == "equals") && (equals->children.size() == 1))
|
|
|
|
|
{
|
2015-02-18 12:11:16 +00:00
|
|
|
|
auto equals_expression_list = typeid_cast<ASTExpressionList *>(&*(equals->children[0]));
|
2015-02-18 09:43:36 +00:00
|
|
|
|
if ((equals_expression_list != nullptr) && (equals_expression_list->children.size() == 2))
|
|
|
|
|
{
|
2015-02-18 13:16:59 +00:00
|
|
|
|
/// Равенство expr = xN.
|
2015-02-18 09:43:36 +00:00
|
|
|
|
auto expr_lhs = queryToString(equals_expression_list->children[0]);
|
2015-02-18 12:11:16 +00:00
|
|
|
|
auto literal = typeid_cast<ASTLiteral *>(&*(equals_expression_list->children[1]));
|
2015-02-18 09:43:36 +00:00
|
|
|
|
if (literal != nullptr)
|
|
|
|
|
{
|
2015-02-18 14:55:11 +00:00
|
|
|
|
OrWithExpression or_with_expression(function, expr_lhs);
|
|
|
|
|
disjunctive_equalities_map[or_with_expression].push_back(equals);
|
2015-02-18 13:53:19 +00:00
|
|
|
|
found_chain = true;
|
2015-02-18 09:43:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-02-18 13:16:59 +00:00
|
|
|
|
|
2015-02-18 15:11:47 +00:00
|
|
|
|
if ((from_node != nullptr) && found_chain)
|
|
|
|
|
or_parent_map[function].push_back(from_node);
|
2015-02-18 09:43:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-18 13:53:19 +00:00
|
|
|
|
if (!found_chain)
|
2015-02-18 09:43:36 +00:00
|
|
|
|
for (auto & child : to_node->children)
|
|
|
|
|
if (typeid_cast<ASTSelectQuery *>(&*child) == nullptr)
|
2015-02-18 15:11:47 +00:00
|
|
|
|
to_visit.push_back(Edge(to_node, child.get()));
|
2015-02-18 09:43:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-18 12:11:16 +00:00
|
|
|
|
for (auto & chain : disjunctive_equalities_map)
|
2015-02-18 09:43:36 +00:00
|
|
|
|
{
|
2015-02-18 12:11:16 +00:00
|
|
|
|
auto & equalities = chain.second;
|
2015-02-18 09:43:36 +00:00
|
|
|
|
std::sort(equalities.begin(), equalities.end());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-18 13:53:19 +00:00
|
|
|
|
bool LogicalExpressionsOptimizer::mayOptimizeDisjunctiveEqualityChain(const DisjunctiveEqualityChain & chain) const
|
2015-02-18 09:43:36 +00:00
|
|
|
|
{
|
|
|
|
|
const UInt64 mutation_threshold = 3;
|
2015-02-18 13:53:19 +00:00
|
|
|
|
const auto & equalities = chain.second;
|
2015-02-18 09:43:36 +00:00
|
|
|
|
|
2015-02-18 13:53:19 +00:00
|
|
|
|
/// Исключаем слишком короткие цепочки.
|
2015-02-18 09:43:36 +00:00
|
|
|
|
if (equalities.size() < mutation_threshold)
|
|
|
|
|
return false;
|
|
|
|
|
|
2015-02-18 13:53:19 +00:00
|
|
|
|
/// Проверяем, что правые части всех равенств имеют один и тот же тип.
|
2015-02-18 09:43:36 +00:00
|
|
|
|
auto first_expr = static_cast<ASTExpressionList *>(&*(equalities[0]->children[0]));
|
|
|
|
|
auto first_literal = static_cast<ASTLiteral *>(&*(first_expr->children[1]));
|
|
|
|
|
for (size_t i = 1; i < equalities.size(); ++i)
|
|
|
|
|
{
|
|
|
|
|
auto expr = static_cast<ASTExpressionList *>(&*(equalities[i]->children[0]));
|
|
|
|
|
auto literal = static_cast<ASTLiteral *>(&*(expr->children[1]));
|
|
|
|
|
|
|
|
|
|
if (literal->type != first_literal->type)
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-18 14:55:11 +00:00
|
|
|
|
namespace
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
ASTs & getOrExpressionOperands(const OrWithExpression & or_with_expression)
|
2015-02-18 09:43:36 +00:00
|
|
|
|
{
|
2015-02-18 14:55:11 +00:00
|
|
|
|
auto or_function = or_with_expression.or_function;
|
|
|
|
|
auto expression_list = static_cast<ASTExpressionList *>(&*(or_function->children[0]));
|
|
|
|
|
return expression_list->children;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LogicalExpressionsOptimizer::replaceOrByIn(const DisjunctiveEqualityChain & chain)
|
|
|
|
|
{
|
|
|
|
|
using ASTFunctionPtr = Poco::SharedPtr<ASTFunction>;
|
|
|
|
|
|
|
|
|
|
const auto & or_with_expression = chain.first;
|
|
|
|
|
const auto & equalities = chain.second;
|
|
|
|
|
|
|
|
|
|
/// Создать новое выражение IN на основе информации из OR-цепочки.
|
|
|
|
|
|
2015-02-18 09:43:36 +00:00
|
|
|
|
ASTPtr value_list = new ASTExpressionList;
|
|
|
|
|
for (auto function : equalities)
|
|
|
|
|
{
|
|
|
|
|
auto expr = static_cast<ASTExpressionList *>(&*(function->children[0]));
|
|
|
|
|
auto literal = static_cast<ASTLiteral *>(&*(expr->children[1]));
|
|
|
|
|
value_list->children.push_back(literal->clone());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto function = equalities[0];
|
|
|
|
|
auto expr = static_cast<ASTExpressionList *>(&*(function->children[0]));
|
|
|
|
|
auto equals_expr_lhs = static_cast<ASTLiteral *>(&*(expr->children[0]));
|
|
|
|
|
|
|
|
|
|
ASTFunctionPtr tuple_function = new ASTFunction;
|
|
|
|
|
tuple_function->name = "tuple";
|
|
|
|
|
tuple_function->arguments = value_list;
|
|
|
|
|
tuple_function->children.push_back(tuple_function->arguments);
|
|
|
|
|
|
|
|
|
|
ASTPtr in_expr = new ASTExpressionList;
|
|
|
|
|
in_expr->children.push_back(equals_expr_lhs->clone());
|
|
|
|
|
in_expr->children.push_back(tuple_function);
|
|
|
|
|
|
|
|
|
|
ASTFunctionPtr in_function = new ASTFunction;
|
|
|
|
|
in_function->name = "in";
|
|
|
|
|
in_function->arguments = in_expr;
|
|
|
|
|
in_function->children.push_back(in_function->arguments);
|
|
|
|
|
|
2015-02-18 14:55:11 +00:00
|
|
|
|
/// Заменить OR-цепочку на новое выражение IN.
|
2015-02-18 13:16:59 +00:00
|
|
|
|
|
2015-02-18 14:55:11 +00:00
|
|
|
|
auto & operands = getOrExpressionOperands(or_with_expression);
|
|
|
|
|
operands.push_back(in_function);
|
2015-02-18 11:57:44 +00:00
|
|
|
|
|
2015-02-18 13:16:59 +00:00
|
|
|
|
auto it = std::remove_if(operands.begin(), operands.end(), [&](const ASTPtr & node)
|
2015-02-18 12:11:16 +00:00
|
|
|
|
{
|
|
|
|
|
return std::binary_search(equalities.begin(), equalities.end(), node.get());
|
|
|
|
|
});
|
2015-02-18 13:16:59 +00:00
|
|
|
|
operands.erase(it, operands.end());
|
2015-02-18 11:57:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-18 09:43:36 +00:00
|
|
|
|
void LogicalExpressionsOptimizer::fixBrokenOrExpressions()
|
|
|
|
|
{
|
2015-02-18 12:11:16 +00:00
|
|
|
|
for (const auto & chain : disjunctive_equalities_map)
|
2015-02-18 09:43:36 +00:00
|
|
|
|
{
|
2015-02-18 14:55:11 +00:00
|
|
|
|
const auto & or_with_expression = chain.first;
|
|
|
|
|
auto or_function = or_with_expression.or_function;
|
|
|
|
|
auto & operands = getOrExpressionOperands(or_with_expression);
|
2015-02-18 09:43:36 +00:00
|
|
|
|
|
2015-02-18 13:16:59 +00:00
|
|
|
|
if (operands.size() == 1)
|
2015-02-18 09:43:36 +00:00
|
|
|
|
{
|
2015-02-18 14:55:11 +00:00
|
|
|
|
auto it = or_parent_map.find(or_function);
|
|
|
|
|
if (it == or_parent_map.end())
|
2015-02-18 13:16:59 +00:00
|
|
|
|
throw Exception("Parent node information is corrupted", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
auto & parents = it->second;
|
|
|
|
|
|
2015-02-18 09:43:36 +00:00
|
|
|
|
for (auto & parent : parents)
|
|
|
|
|
{
|
2015-02-18 13:16:59 +00:00
|
|
|
|
parent->children.push_back(operands[0]);
|
2015-02-18 09:43:36 +00:00
|
|
|
|
auto it = std::remove(parent->children.begin(), parent->children.end(), or_function);
|
|
|
|
|
parent->children.erase(it, parent->children.end());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Если узел OR был корнем выражения WHERE, PREWHERE или HAVING, то следует обновить этот корень.
|
2015-02-18 13:16:59 +00:00
|
|
|
|
/// Из-за того, что имеем дело с направленным ациклическим графом, надо проверить все случаи.
|
2015-02-18 09:43:36 +00:00
|
|
|
|
if (or_function == select_query->where_expression.get())
|
2015-02-18 13:16:59 +00:00
|
|
|
|
select_query->where_expression = operands[0];
|
|
|
|
|
if (or_function == select_query->prewhere_expression.get())
|
|
|
|
|
select_query->prewhere_expression = operands[0];
|
|
|
|
|
if (or_function == select_query->having_expression.get())
|
|
|
|
|
select_query->having_expression = operands[0];
|
2015-02-18 09:43:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|