2021-09-15 12:15:42 +00:00
|
|
|
#include <Interpreters/GatherFunctionQuantileVisitor.h>
|
2022-07-21 15:08:42 +00:00
|
|
|
|
2021-11-26 17:21:54 +00:00
|
|
|
#include <Parsers/ASTFunction.h>
|
2022-07-21 15:08:42 +00:00
|
|
|
#include <Common/Exception.h>
|
2021-09-15 12:15:42 +00:00
|
|
|
|
2023-11-11 05:59:20 +00:00
|
|
|
|
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
|
2023-11-11 05:59:20 +00:00
|
|
|
static const std::unordered_map<String, String> quantile_fuse_name_mapping =
|
|
|
|
{
|
|
|
|
{"quantile", "quantiles"},
|
|
|
|
{"quantileBFloat16", "quantilesBFloat16"},
|
|
|
|
{"quantileBFloat16Weighted", "quantilesBFloat16Weighted"},
|
|
|
|
{"quantileDeterministic", "quantilesDeterministic"},
|
|
|
|
{"quantileExact", "quantilesExact"},
|
|
|
|
{"quantileExactExclusive", "quantilesExactExclusive"},
|
|
|
|
{"quantileExactHigh", "quantilesExactHigh"},
|
|
|
|
{"quantileExactInclusive", "quantilesExactInclusive"},
|
|
|
|
{"quantileExactLow", "quantilesExactLow"},
|
|
|
|
{"quantileExactWeighted", "quantilesExactWeighted"},
|
|
|
|
{"quantileInterpolatedWeighted", "quantilesInterpolatedWeighted"},
|
|
|
|
{"quantileTDigest", "quantilesTDigest"},
|
|
|
|
{"quantileTDigestWeighted", "quantilesTDigestWeighted"},
|
|
|
|
{"quantileTiming", "quantilesTiming"},
|
|
|
|
{"quantileTimingWeighted", "quantilesTimingWeighted"},
|
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
|
|
|
|
2023-11-11 05:59:20 +00:00
|
|
|
bool need_two_args = func->name == "quantileDeterministic" || func->name == "quantileExactWeighted"
|
|
|
|
|| func->name == "quantileInterpolatedWeighted" || func->name == "quantileTimingWeighted"
|
|
|
|
|| func->name == "quantileTDigestWeighted" || func->name == "quantileBFloat16Weighted";
|
2022-12-13 17:34:26 +00:00
|
|
|
|
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
|
|
|
}
|