2021-09-15 12:15:42 +00:00
|
|
|
#include <Interpreters/GatherFunctionQuantileVisitor.h>
|
2022-07-21 15:08:42 +00:00
|
|
|
|
|
|
|
#include <AggregateFunctions/AggregateFunctionQuantile.h>
|
2021-11-26 17:21:54 +00:00
|
|
|
#include <Parsers/ASTFunction.h>
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/types.h>
|
2022-07-21 15:08:42 +00:00
|
|
|
#include <Common/Exception.h>
|
2021-09-15 12:15:42 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2021-09-16 12:11:14 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
}
|
|
|
|
|
2021-09-15 12:15:42 +00:00
|
|
|
/// Mapping from quantile functions for single value to plural
|
|
|
|
static const std::unordered_map<String, String> quantile_fuse_name_mapping = {
|
|
|
|
{NameQuantile::name, NameQuantiles::name},
|
2021-09-22 12:46:47 +00:00
|
|
|
{NameQuantileBFloat16::name, NameQuantilesBFloat16::name},
|
|
|
|
{NameQuantileBFloat16Weighted::name, NameQuantilesBFloat16Weighted::name},
|
2021-09-15 12:15:42 +00:00
|
|
|
{NameQuantileDeterministic::name, NameQuantilesDeterministic::name},
|
|
|
|
{NameQuantileExact::name, NameQuantilesExact::name},
|
|
|
|
{NameQuantileExactExclusive::name, NameQuantilesExactExclusive::name},
|
2021-09-22 12:46:47 +00:00
|
|
|
{NameQuantileExactHigh::name, NameQuantilesExactHigh::name},
|
2021-09-15 12:15:42 +00:00
|
|
|
{NameQuantileExactInclusive::name, NameQuantilesExactInclusive::name},
|
2021-09-22 12:46:47 +00:00
|
|
|
{NameQuantileExactLow::name, NameQuantilesExactLow::name},
|
2021-09-15 12:15:42 +00:00
|
|
|
{NameQuantileExactWeighted::name, NameQuantilesExactWeighted::name},
|
2022-07-09 08:32:39 +00:00
|
|
|
{NameQuantileInterpolatedWeighted::name, NameQuantilesInterpolatedWeighted::name},
|
2021-09-15 12:15:42 +00:00
|
|
|
{NameQuantileTDigest::name, NameQuantilesTDigest::name},
|
|
|
|
{NameQuantileTDigestWeighted::name, NameQuantilesTDigestWeighted::name},
|
2021-09-22 12:46:47 +00:00
|
|
|
{NameQuantileTiming::name, NameQuantilesTiming::name},
|
|
|
|
{NameQuantileTimingWeighted::name, NameQuantilesTimingWeighted::name},
|
2021-09-15 12:15:42 +00:00
|
|
|
};
|
|
|
|
|
2022-07-21 15:08:42 +00:00
|
|
|
String GatherFunctionQuantileData::toFusedNameOrSelf(const String & func_name)
|
|
|
|
{
|
|
|
|
if (auto it = quantile_fuse_name_mapping.find(func_name); it != quantile_fuse_name_mapping.end())
|
|
|
|
return it->second;
|
|
|
|
return func_name;
|
|
|
|
}
|
|
|
|
|
2021-09-15 12:15:42 +00:00
|
|
|
String GatherFunctionQuantileData::getFusedName(const String & func_name)
|
|
|
|
{
|
|
|
|
if (auto it = quantile_fuse_name_mapping.find(func_name); it != quantile_fuse_name_mapping.end())
|
|
|
|
return it->second;
|
|
|
|
throw DB::Exception(ErrorCodes::LOGICAL_ERROR, "Function '{}' is not quantile-family or cannot be fused", func_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GatherFunctionQuantileData::visit(ASTFunction & function, ASTPtr & ast)
|
|
|
|
{
|
|
|
|
if (!quantile_fuse_name_mapping.contains(function.name))
|
|
|
|
return;
|
|
|
|
|
|
|
|
fuse_quantile[function.name].addFuncNode(ast);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GatherFunctionQuantileData::FuseQuantileAggregatesData::addFuncNode(ASTPtr & ast)
|
|
|
|
{
|
|
|
|
const auto * func = ast->as<ASTFunction>();
|
2021-10-11 02:39:55 +00:00
|
|
|
if (!func || func->parameters == nullptr)
|
2021-09-15 12:15:42 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
const auto & arguments = func->arguments->children;
|
|
|
|
|
2022-12-13 17:34:26 +00:00
|
|
|
|
2022-07-21 15:08:42 +00:00
|
|
|
bool need_two_args = func->name == NameQuantileDeterministic::name || func->name == NameQuantileExactWeighted::name
|
2022-12-13 17:34:26 +00:00
|
|
|
|| func->name == NameQuantileInterpolatedWeighted::name || func->name == NameQuantileTimingWeighted::name
|
|
|
|
|| func->name == NameQuantileTDigestWeighted::name || func->name == NameQuantileBFloat16Weighted::name;
|
|
|
|
|
2021-09-15 12:15:42 +00:00
|
|
|
if (arguments.size() != (need_two_args ? 2 : 1))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (arguments[0]->getColumnName().find(',') != std::string::npos)
|
|
|
|
return;
|
|
|
|
String arg_name = arguments[0]->getColumnName();
|
|
|
|
if (need_two_args)
|
|
|
|
{
|
|
|
|
if (arguments[1]->getColumnName().find(',') != std::string::npos)
|
|
|
|
return;
|
|
|
|
arg_name += "," + arguments[1]->getColumnName();
|
|
|
|
}
|
|
|
|
|
|
|
|
arg_map_function[arg_name].push_back(&ast);
|
|
|
|
}
|
|
|
|
|
2021-09-17 11:30:45 +00:00
|
|
|
bool GatherFunctionQuantileData::needChild(const ASTPtr & node, const ASTPtr &)
|
|
|
|
{
|
|
|
|
/// Skip children of quantile* functions to escape cycles in further processing
|
|
|
|
if (const auto * func = node ? node->as<ASTFunction>() : nullptr)
|
|
|
|
return !quantile_fuse_name_mapping.contains(func->name);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-09-15 12:15:42 +00:00
|
|
|
}
|