2022-07-14 11:20:16 +00:00
|
|
|
#include <Analyzer/FunctionNode.h>
|
|
|
|
|
|
|
|
#include <Common/SipHash.h>
|
2022-08-31 15:21:17 +00:00
|
|
|
#include <Common/FieldVisitorToString.h>
|
2022-12-08 00:34:01 +00:00
|
|
|
#include <Analyzer/ConstantNode.h>
|
2022-07-14 11:20:16 +00:00
|
|
|
|
|
|
|
#include <IO/WriteBufferFromString.h>
|
|
|
|
#include <IO/Operators.h>
|
|
|
|
|
2022-09-12 14:14:40 +00:00
|
|
|
#include <Parsers/ASTFunction.h>
|
|
|
|
|
2022-07-14 11:20:16 +00:00
|
|
|
#include <Functions/IFunction.h>
|
2022-09-12 14:14:40 +00:00
|
|
|
|
2022-07-14 11:20:16 +00:00
|
|
|
#include <AggregateFunctions/IAggregateFunction.h>
|
|
|
|
|
2022-09-12 14:14:40 +00:00
|
|
|
#include <Analyzer/IdentifierNode.h>
|
2022-07-14 11:20:16 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2022-12-05 14:41:37 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
}
|
|
|
|
|
2022-08-26 13:06:14 +00:00
|
|
|
FunctionNode::FunctionNode(String function_name_)
|
2022-10-07 10:44:28 +00:00
|
|
|
: IQueryTreeNode(children_size)
|
|
|
|
, function_name(function_name_)
|
2022-08-26 13:06:14 +00:00
|
|
|
{
|
|
|
|
children[parameters_child_index] = std::make_shared<ListNode>();
|
|
|
|
children[arguments_child_index] = std::make_shared<ListNode>();
|
|
|
|
}
|
|
|
|
|
2022-11-28 15:02:59 +00:00
|
|
|
ColumnsWithTypeAndName FunctionNode::getArgumentTypes() const
|
2022-07-14 11:20:16 +00:00
|
|
|
{
|
2022-11-28 15:02:59 +00:00
|
|
|
ColumnsWithTypeAndName argument_types;
|
|
|
|
for (const auto & arg : getArguments().getNodes())
|
|
|
|
{
|
|
|
|
ColumnWithTypeAndName argument;
|
|
|
|
argument.type = arg->getResultType();
|
2022-12-08 00:34:01 +00:00
|
|
|
if (auto * constant = arg->as<ConstantNode>())
|
|
|
|
argument.column = argument.type->createColumnConst(1, constant->getValue());
|
2022-11-28 15:02:59 +00:00
|
|
|
argument_types.push_back(argument);
|
|
|
|
}
|
|
|
|
return argument_types;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FunctionNode::resolveAsFunction(FunctionBasePtr function_value)
|
|
|
|
{
|
|
|
|
function_name = function_value->getName();
|
2022-07-14 11:20:16 +00:00
|
|
|
function = std::move(function_value);
|
2022-12-01 18:51:57 +00:00
|
|
|
kind = FunctionKind::ORDINARY;
|
2022-07-14 11:20:16 +00:00
|
|
|
}
|
|
|
|
|
2022-11-28 15:02:59 +00:00
|
|
|
void FunctionNode::resolveAsAggregateFunction(AggregateFunctionPtr aggregate_function_value)
|
2022-07-14 11:20:16 +00:00
|
|
|
{
|
2022-11-28 15:02:59 +00:00
|
|
|
function_name = aggregate_function_value->getName();
|
|
|
|
function = std::move(aggregate_function_value);
|
2022-12-01 18:51:57 +00:00
|
|
|
kind = FunctionKind::AGGREGATE;
|
2022-07-14 11:20:16 +00:00
|
|
|
}
|
|
|
|
|
2022-11-28 15:02:59 +00:00
|
|
|
void FunctionNode::resolveAsWindowFunction(AggregateFunctionPtr window_function_value)
|
2022-09-12 14:14:40 +00:00
|
|
|
{
|
2022-12-01 18:51:57 +00:00
|
|
|
if (!hasWindow())
|
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR,
|
|
|
|
"Trying to resolve FunctionNode without window definition as a window function {}", window_function_value->getName());
|
2022-11-28 15:02:59 +00:00
|
|
|
resolveAsAggregateFunction(window_function_value);
|
2022-12-01 18:51:57 +00:00
|
|
|
kind = FunctionKind::WINDOW;
|
2022-09-12 14:14:40 +00:00
|
|
|
}
|
|
|
|
|
2022-07-19 10:54:45 +00:00
|
|
|
void FunctionNode::dumpTreeImpl(WriteBuffer & buffer, FormatState & format_state, size_t indent) const
|
2022-07-14 11:20:16 +00:00
|
|
|
{
|
2022-07-19 10:54:45 +00:00
|
|
|
buffer << std::string(indent, ' ') << "FUNCTION id: " << format_state.getNodeId(this);
|
|
|
|
|
|
|
|
if (hasAlias())
|
|
|
|
buffer << ", alias: " << getAlias();
|
|
|
|
|
|
|
|
buffer << ", function_name: " << function_name;
|
2022-09-12 14:14:40 +00:00
|
|
|
|
|
|
|
std::string function_type = "ordinary";
|
|
|
|
if (isAggregateFunction())
|
|
|
|
function_type = "aggregate";
|
|
|
|
else if (isWindowFunction())
|
|
|
|
function_type = "window";
|
|
|
|
|
|
|
|
buffer << ", function_type: " << function_type;
|
2022-07-19 10:54:45 +00:00
|
|
|
|
2022-11-28 15:02:59 +00:00
|
|
|
if (function)
|
|
|
|
buffer << ", result_type: " + function->getResultType()->getName();
|
2022-07-14 11:20:16 +00:00
|
|
|
|
|
|
|
const auto & parameters = getParameters();
|
|
|
|
if (!parameters.getNodes().empty())
|
|
|
|
{
|
|
|
|
buffer << '\n' << std::string(indent + 2, ' ') << "PARAMETERS\n";
|
2022-07-19 10:54:45 +00:00
|
|
|
parameters.dumpTreeImpl(buffer, format_state, indent + 4);
|
2022-07-14 11:20:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const auto & arguments = getArguments();
|
|
|
|
if (!arguments.getNodes().empty())
|
|
|
|
{
|
|
|
|
buffer << '\n' << std::string(indent + 2, ' ') << "ARGUMENTS\n";
|
2022-07-19 10:54:45 +00:00
|
|
|
arguments.dumpTreeImpl(buffer, format_state, indent + 4);
|
2022-07-14 11:20:16 +00:00
|
|
|
}
|
2022-09-12 14:14:40 +00:00
|
|
|
|
|
|
|
if (hasWindow())
|
|
|
|
{
|
|
|
|
buffer << '\n' << std::string(indent + 2, ' ') << "WINDOW\n";
|
|
|
|
getWindowNode()->dumpTreeImpl(buffer, format_state, indent + 4);
|
|
|
|
}
|
2022-07-14 11:20:16 +00:00
|
|
|
}
|
|
|
|
|
2022-07-15 13:32:53 +00:00
|
|
|
bool FunctionNode::isEqualImpl(const IQueryTreeNode & rhs) const
|
|
|
|
{
|
|
|
|
const auto & rhs_typed = assert_cast<const FunctionNode &>(rhs);
|
|
|
|
if (function_name != rhs_typed.function_name ||
|
|
|
|
isAggregateFunction() != rhs_typed.isAggregateFunction() ||
|
2022-10-03 12:22:38 +00:00
|
|
|
isOrdinaryFunction() != rhs_typed.isOrdinaryFunction() ||
|
|
|
|
isWindowFunction() != rhs_typed.isWindowFunction())
|
2022-07-15 13:32:53 +00:00
|
|
|
return false;
|
2022-11-28 15:02:59 +00:00
|
|
|
auto lhs_result_type = getResultType();
|
|
|
|
auto rhs_result_type = rhs.getResultType();
|
2022-07-15 13:32:53 +00:00
|
|
|
|
2022-11-28 15:02:59 +00:00
|
|
|
if (lhs_result_type && rhs_result_type && !lhs_result_type->equals(*rhs_result_type))
|
2022-08-31 15:21:17 +00:00
|
|
|
return false;
|
2022-11-28 15:02:59 +00:00
|
|
|
else if (lhs_result_type && !rhs_result_type)
|
2022-07-15 13:32:53 +00:00
|
|
|
return false;
|
2022-11-28 15:02:59 +00:00
|
|
|
else if (!lhs_result_type && rhs_result_type)
|
2022-07-15 13:32:53 +00:00
|
|
|
return false;
|
|
|
|
|
2022-08-31 15:21:17 +00:00
|
|
|
return true;
|
2022-07-15 13:32:53 +00:00
|
|
|
}
|
|
|
|
|
2022-07-14 11:20:16 +00:00
|
|
|
void FunctionNode::updateTreeHashImpl(HashState & hash_state) const
|
|
|
|
{
|
|
|
|
hash_state.update(function_name.size());
|
|
|
|
hash_state.update(function_name);
|
2022-10-03 12:22:38 +00:00
|
|
|
hash_state.update(isOrdinaryFunction());
|
2022-07-14 11:20:16 +00:00
|
|
|
hash_state.update(isAggregateFunction());
|
2022-09-12 14:14:40 +00:00
|
|
|
hash_state.update(isWindowFunction());
|
2022-07-14 11:20:16 +00:00
|
|
|
|
2022-11-28 15:02:59 +00:00
|
|
|
if (auto result_type = getResultType())
|
2022-07-14 11:20:16 +00:00
|
|
|
{
|
|
|
|
auto result_type_name = result_type->getName();
|
|
|
|
hash_state.update(result_type_name.size());
|
|
|
|
hash_state.update(result_type_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-10 10:25:58 +00:00
|
|
|
QueryTreeNodePtr FunctionNode::cloneImpl() const
|
|
|
|
{
|
|
|
|
auto result_function = std::make_shared<FunctionNode>(function_name);
|
|
|
|
|
|
|
|
/** This is valid for clone method to reuse same function pointers
|
|
|
|
* because ordinary functions or aggregate functions must be stateless.
|
|
|
|
*/
|
|
|
|
result_function->function = function;
|
2022-12-02 18:41:34 +00:00
|
|
|
result_function->kind = kind;
|
2022-10-10 10:25:58 +00:00
|
|
|
|
|
|
|
return result_function;
|
|
|
|
}
|
|
|
|
|
2022-07-14 11:20:16 +00:00
|
|
|
ASTPtr FunctionNode::toASTImpl() const
|
|
|
|
{
|
|
|
|
auto function_ast = std::make_shared<ASTFunction>();
|
|
|
|
|
|
|
|
function_ast->name = function_name;
|
2022-11-11 15:26:04 +00:00
|
|
|
|
|
|
|
if (isWindowFunction())
|
|
|
|
{
|
|
|
|
function_ast->is_window_function = true;
|
|
|
|
function_ast->kind = ASTFunction::Kind::WINDOW_FUNCTION;
|
|
|
|
}
|
2022-09-12 14:14:40 +00:00
|
|
|
|
2022-07-14 11:20:16 +00:00
|
|
|
const auto & parameters = getParameters();
|
|
|
|
if (!parameters.getNodes().empty())
|
|
|
|
{
|
|
|
|
function_ast->children.push_back(parameters.toAST());
|
|
|
|
function_ast->parameters = function_ast->children.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto & arguments = getArguments();
|
2022-08-26 16:23:26 +00:00
|
|
|
function_ast->children.push_back(arguments.toAST());
|
|
|
|
function_ast->arguments = function_ast->children.back();
|
2022-07-14 11:20:16 +00:00
|
|
|
|
2022-10-19 10:25:27 +00:00
|
|
|
auto window_node = getWindowNode();
|
|
|
|
if (window_node)
|
|
|
|
{
|
|
|
|
if (auto * identifier_node = window_node->as<IdentifierNode>())
|
|
|
|
function_ast->window_name = identifier_node->getIdentifier().getFullName();
|
|
|
|
else
|
|
|
|
function_ast->window_definition = window_node->toAST();
|
|
|
|
}
|
|
|
|
|
2022-07-14 11:20:16 +00:00
|
|
|
return function_ast;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|