2020-07-10 10:42:41 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
|
|
|
#include <Parsers/ASTSubquery.h>
|
|
|
|
#include <Parsers/ASTFunction.h>
|
|
|
|
#include <Parsers/ASTTablesInSelectQuery.h>
|
|
|
|
#include <Interpreters/RemoveInjectiveFunctionsVisitor.h>
|
|
|
|
#include <AggregateFunctions/AggregateFunctionFactory.h>
|
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
static bool isUniq(const ASTFunction & func)
|
|
|
|
{
|
|
|
|
return func.name == "uniq" || func.name == "uniqExact" || func.name == "uniqHLL12"
|
2021-05-05 08:42:57 +00:00
|
|
|
|| func.name == "uniqCombined" || func.name == "uniqCombined64"
|
2021-05-11 14:36:26 +00:00
|
|
|
|| func.name == "uniqTheta";
|
2020-07-10 10:42:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Remove injective functions of one argument: replace with a child
|
2021-05-28 16:44:59 +00:00
|
|
|
static bool removeInjectiveFunction(ASTPtr & ast, ContextConstPtr context, const FunctionFactory & function_factory)
|
2020-07-10 10:42:41 +00:00
|
|
|
{
|
|
|
|
const ASTFunction * func = ast->as<ASTFunction>();
|
|
|
|
if (!func)
|
|
|
|
return false;
|
|
|
|
|
2020-12-04 02:15:44 +00:00
|
|
|
if (!func->arguments || func->arguments->children.size() != 1)
|
2020-07-10 10:42:41 +00:00
|
|
|
return false;
|
|
|
|
|
2020-10-09 07:41:28 +00:00
|
|
|
if (!function_factory.get(func->name, context)->isInjective({}))
|
2020-07-10 10:42:41 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
ast = func->arguments->children[0];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoveInjectiveFunctionsMatcher::visit(ASTPtr & ast, const Data & data)
|
|
|
|
{
|
|
|
|
if (auto * func = ast->as<ASTFunction>())
|
|
|
|
visit(*func, ast, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoveInjectiveFunctionsMatcher::visit(ASTFunction & func, ASTPtr &, const Data & data)
|
|
|
|
{
|
|
|
|
if (isUniq(func))
|
|
|
|
{
|
|
|
|
const FunctionFactory & function_factory = FunctionFactory::instance();
|
|
|
|
|
|
|
|
for (auto & arg : func.arguments->children)
|
|
|
|
{
|
2021-04-10 23:33:54 +00:00
|
|
|
while (removeInjectiveFunction(arg, data.getContext(), function_factory))
|
2020-07-10 10:42:41 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RemoveInjectiveFunctionsMatcher::needChildVisit(const ASTPtr & node, const ASTPtr &)
|
|
|
|
{
|
|
|
|
if (node->as<ASTSubquery>() ||
|
|
|
|
node->as<ASTTableExpression>())
|
|
|
|
return false; // NOLINT
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|