2020-06-24 18:49:55 +00:00
|
|
|
#include <unordered_set>
|
|
|
|
|
2020-06-03 10:28:30 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
|
|
|
#include <Parsers/ASTLiteral.h>
|
|
|
|
#include <Parsers/ASTFunction.h>
|
2020-06-24 18:49:55 +00:00
|
|
|
#include <Parsers/ASTSubquery.h>
|
2020-06-03 10:28:30 +00:00
|
|
|
#include <Parsers/ASTTablesInSelectQuery.h>
|
2020-06-24 18:49:55 +00:00
|
|
|
#include <Interpreters/ArithmeticOperationsInAgrFuncOptimize.h>
|
2020-06-03 10:28:30 +00:00
|
|
|
|
2021-02-09 14:17:48 +00:00
|
|
|
#include <Poco/String.h>
|
|
|
|
|
2020-06-03 10:28:30 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2020-06-24 18:49:55 +00:00
|
|
|
extern const int BAD_TYPE_OF_FIELD;
|
2020-06-03 10:28:30 +00:00
|
|
|
}
|
2020-06-20 06:24:34 +00:00
|
|
|
|
2020-06-03 10:28:30 +00:00
|
|
|
namespace
|
|
|
|
{
|
2020-06-20 06:24:34 +00:00
|
|
|
|
2020-06-24 18:49:55 +00:00
|
|
|
const ASTFunction * getInternalFunction(const ASTFunction & func)
|
2020-06-03 10:28:30 +00:00
|
|
|
{
|
2020-12-04 02:15:44 +00:00
|
|
|
if (func.arguments && func.arguments->children.size() == 1)
|
2020-06-24 18:49:55 +00:00
|
|
|
return func.arguments->children[0]->as<ASTFunction>();
|
|
|
|
return nullptr;
|
2020-06-03 10:28:30 +00:00
|
|
|
}
|
|
|
|
|
2020-06-24 18:49:55 +00:00
|
|
|
ASTPtr exchangeExtractFirstArgument(const String & func_name, const ASTFunction & child_func)
|
2020-06-03 10:28:30 +00:00
|
|
|
{
|
2020-06-24 18:49:55 +00:00
|
|
|
ASTs new_child_args;
|
|
|
|
new_child_args.push_back(child_func.arguments->children[1]);
|
2020-06-03 10:28:30 +00:00
|
|
|
|
2020-06-24 18:49:55 +00:00
|
|
|
auto new_child = makeASTFunction(func_name, new_child_args);
|
2020-06-03 10:28:30 +00:00
|
|
|
|
2020-06-24 18:49:55 +00:00
|
|
|
ASTs new_args;
|
|
|
|
new_args.push_back(child_func.arguments->children[0]);
|
|
|
|
new_args.push_back(new_child);
|
2020-06-03 10:28:30 +00:00
|
|
|
|
2020-06-24 18:49:55 +00:00
|
|
|
return makeASTFunction(child_func.name, new_args);
|
2020-06-03 10:28:30 +00:00
|
|
|
}
|
|
|
|
|
2020-06-24 18:49:55 +00:00
|
|
|
ASTPtr exchangeExtractSecondArgument(const String & func_name, const ASTFunction & child_func)
|
2020-06-03 10:28:30 +00:00
|
|
|
{
|
2020-06-24 18:49:55 +00:00
|
|
|
ASTs new_child_args;
|
|
|
|
new_child_args.push_back(child_func.arguments->children[0]);
|
2020-06-03 10:28:30 +00:00
|
|
|
|
2020-06-24 18:49:55 +00:00
|
|
|
auto new_child = makeASTFunction(func_name, new_child_args);
|
2020-06-03 10:28:30 +00:00
|
|
|
|
2020-06-24 18:49:55 +00:00
|
|
|
ASTs new_args;
|
|
|
|
new_args.push_back(new_child);
|
|
|
|
new_args.push_back(child_func.arguments->children[1]);
|
2020-06-03 10:28:30 +00:00
|
|
|
|
2020-06-24 18:49:55 +00:00
|
|
|
return makeASTFunction(child_func.name, new_args);
|
2020-06-03 10:28:30 +00:00
|
|
|
}
|
|
|
|
|
2020-06-24 18:49:55 +00:00
|
|
|
Field zeroField(const Field & value)
|
2020-06-03 10:28:30 +00:00
|
|
|
{
|
2020-06-24 18:49:55 +00:00
|
|
|
switch (value.getType())
|
2020-06-18 19:25:28 +00:00
|
|
|
{
|
2020-06-24 18:49:55 +00:00
|
|
|
case Field::Types::UInt64: return UInt64(0);
|
|
|
|
case Field::Types::Int64: return Int64(0);
|
|
|
|
case Field::Types::Float64: return Float64(0);
|
|
|
|
case Field::Types::UInt128: return UInt128(0);
|
|
|
|
case Field::Types::Int128: return Int128(0);
|
2021-05-06 00:31:09 +00:00
|
|
|
case Field::Types::UInt256: return UInt256(0);
|
|
|
|
case Field::Types::Int256: return Int256(0);
|
2020-06-24 18:49:55 +00:00
|
|
|
default:
|
|
|
|
break;
|
2020-06-18 19:25:28 +00:00
|
|
|
}
|
2020-06-03 10:28:30 +00:00
|
|
|
|
2020-06-24 18:49:55 +00:00
|
|
|
throw Exception("Unexpected literal type in function", ErrorCodes::BAD_TYPE_OF_FIELD);
|
2020-06-03 10:28:30 +00:00
|
|
|
}
|
|
|
|
|
2020-06-24 18:49:55 +00:00
|
|
|
const String & changeNameIfNeeded(const String & func_name, const String & child_name, const ASTLiteral & literal)
|
2020-06-03 10:28:30 +00:00
|
|
|
{
|
2020-06-24 18:49:55 +00:00
|
|
|
static const std::unordered_map<String, std::unordered_set<String>> matches = {
|
|
|
|
{ "min", { "multiply", "divide" } },
|
|
|
|
{ "max", { "multiply", "divide" } }
|
|
|
|
};
|
2020-06-03 10:28:30 +00:00
|
|
|
|
2020-06-24 18:49:55 +00:00
|
|
|
static const std::unordered_map<String, String> swap_to = {
|
|
|
|
{ "min", "max" },
|
|
|
|
{ "max", "min" }
|
|
|
|
};
|
2020-06-18 19:25:28 +00:00
|
|
|
|
2020-06-24 18:49:55 +00:00
|
|
|
if (literal.value < zeroField(literal.value) && matches.count(func_name) && matches.find(func_name)->second.count(child_name))
|
|
|
|
return swap_to.find(func_name)->second;
|
2020-06-03 10:28:30 +00:00
|
|
|
|
2020-06-24 18:49:55 +00:00
|
|
|
return func_name;
|
2020-06-03 10:28:30 +00:00
|
|
|
}
|
|
|
|
|
2020-06-24 18:49:55 +00:00
|
|
|
ASTPtr tryExchangeFunctions(const ASTFunction & func)
|
2020-06-03 10:28:30 +00:00
|
|
|
{
|
2021-02-07 14:41:41 +00:00
|
|
|
static const std::unordered_map<String, std::unordered_set<String>> supported
|
|
|
|
= {{"sum", {"multiply", "divide"}},
|
|
|
|
{"min", {"multiply", "divide", "plus", "minus"}},
|
|
|
|
{"max", {"multiply", "divide", "plus", "minus"}},
|
|
|
|
{"avg", {"multiply", "divide", "plus", "minus"}}};
|
2020-06-03 10:28:30 +00:00
|
|
|
|
2021-02-09 14:17:48 +00:00
|
|
|
/// Aggregate functions[sum|min|max|avg] is case-insensitive, so we use lower cases name
|
|
|
|
auto lower_name = Poco::toLower(func.name);
|
|
|
|
|
2020-06-24 18:49:55 +00:00
|
|
|
const ASTFunction * child_func = getInternalFunction(func);
|
2021-02-09 14:17:48 +00:00
|
|
|
if (!child_func || !child_func->arguments || child_func->arguments->children.size() != 2 || !supported.count(lower_name)
|
|
|
|
|| !supported.find(lower_name)->second.count(child_func->name))
|
2020-06-24 18:49:55 +00:00
|
|
|
return {};
|
2020-06-03 10:28:30 +00:00
|
|
|
|
2021-09-08 12:10:49 +00:00
|
|
|
auto original_alias = func.tryGetAlias();
|
2020-06-24 18:49:55 +00:00
|
|
|
const auto & child_func_args = child_func->arguments->children;
|
|
|
|
const auto * first_literal = child_func_args[0]->as<ASTLiteral>();
|
|
|
|
const auto * second_literal = child_func_args[1]->as<ASTLiteral>();
|
2020-06-03 10:28:30 +00:00
|
|
|
|
2020-06-24 18:49:55 +00:00
|
|
|
ASTPtr optimized_ast;
|
2020-06-03 10:28:30 +00:00
|
|
|
|
2020-06-24 18:49:55 +00:00
|
|
|
if (first_literal && !second_literal)
|
2020-06-03 10:28:30 +00:00
|
|
|
{
|
2020-06-24 18:49:55 +00:00
|
|
|
/// It's possible to rewrite 'sum(1/n)' with 'sum(1) * div(1/n)' but we lose accuracy. Ignored.
|
|
|
|
if (child_func->name == "divide")
|
|
|
|
return {};
|
2020-06-03 10:28:30 +00:00
|
|
|
|
2021-02-09 14:17:48 +00:00
|
|
|
const String & new_name = changeNameIfNeeded(lower_name, child_func->name, *first_literal);
|
2020-06-24 18:49:55 +00:00
|
|
|
optimized_ast = exchangeExtractFirstArgument(new_name, *child_func);
|
2020-06-03 10:28:30 +00:00
|
|
|
}
|
2020-06-24 18:49:55 +00:00
|
|
|
else if (second_literal) /// second or both are consts
|
2020-06-03 10:28:30 +00:00
|
|
|
{
|
2021-02-09 14:17:48 +00:00
|
|
|
const String & new_name = changeNameIfNeeded(lower_name, child_func->name, *second_literal);
|
2020-06-24 18:49:55 +00:00
|
|
|
optimized_ast = exchangeExtractSecondArgument(new_name, *child_func);
|
2020-06-03 10:28:30 +00:00
|
|
|
}
|
|
|
|
|
2021-09-08 12:10:49 +00:00
|
|
|
if (optimized_ast)
|
|
|
|
{
|
|
|
|
optimized_ast->setAlias(original_alias);
|
|
|
|
return optimized_ast;
|
|
|
|
}
|
|
|
|
return {};
|
2020-06-03 10:28:30 +00:00
|
|
|
}
|
|
|
|
|
2020-06-20 06:24:34 +00:00
|
|
|
}
|
|
|
|
|
2020-06-03 10:28:30 +00:00
|
|
|
|
2020-06-24 18:49:55 +00:00
|
|
|
void ArithmeticOperationsInAgrFuncMatcher::visit(const ASTFunction & func, ASTPtr & ast, Data & data)
|
2020-06-03 10:28:30 +00:00
|
|
|
{
|
2020-06-24 18:49:55 +00:00
|
|
|
if (auto exchanged_funcs = tryExchangeFunctions(func))
|
|
|
|
{
|
|
|
|
ast = exchanged_funcs;
|
2020-06-03 10:28:30 +00:00
|
|
|
|
2020-06-24 18:49:55 +00:00
|
|
|
/// Main visitor is bottom-up. This is top-down part.
|
|
|
|
/// We've found an aggregate function an now move it down through others: sum(mul(mul)) -> mul(mul(sum)).
|
|
|
|
/// It's not dangerous cause main visitor already has visited this part of tree.
|
|
|
|
auto & expression_list = ast->children[0];
|
|
|
|
visit(expression_list->children[0], data);
|
|
|
|
}
|
2020-06-03 10:28:30 +00:00
|
|
|
}
|
|
|
|
|
2020-06-24 18:49:55 +00:00
|
|
|
void ArithmeticOperationsInAgrFuncMatcher::visit(ASTPtr & ast, Data & data)
|
2020-06-03 10:28:30 +00:00
|
|
|
{
|
2020-06-24 18:49:55 +00:00
|
|
|
if (const auto * function_node = ast->as<ASTFunction>())
|
|
|
|
visit(*function_node, ast, data);
|
|
|
|
}
|
2020-06-03 10:28:30 +00:00
|
|
|
|
2020-06-24 18:49:55 +00:00
|
|
|
bool ArithmeticOperationsInAgrFuncMatcher::needChildVisit(const ASTPtr & node, const ASTPtr &)
|
|
|
|
{
|
2020-07-23 15:15:22 +00:00
|
|
|
return !node->as<ASTSubquery>() &&
|
|
|
|
!node->as<ASTTableExpression>() &&
|
|
|
|
!node->as<ASTArrayJoin>();
|
2020-06-03 10:28:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|