ClickHouse/dbms/include/DB/Parsers/ASTFunction.h

97 lines
2.4 KiB
C
Raw Normal View History

2011-09-19 01:42:16 +00:00
#pragma once
2010-06-24 19:12:10 +00:00
2014-07-03 22:39:13 +00:00
#include <DB/Parsers/ASTWithAlias.h>
#include <DB/Parsers/ASTExpressionList.h>
2011-08-09 19:19:00 +00:00
#include <DB/Functions/IFunction.h>
2011-09-19 01:42:16 +00:00
#include <DB/AggregateFunctions/IAggregateFunction.h>
2013-05-04 15:44:58 +00:00
#include <DB/IO/WriteBufferFromString.h>
2010-06-24 19:12:10 +00:00
namespace DB
{
/** Применение функции или оператора
*/
2014-07-03 22:39:13 +00:00
class ASTFunction : public ASTWithAlias
2010-06-24 19:12:10 +00:00
{
public:
enum FunctionKind
{
UNKNOWN,
TABLE_FUNCTION,
FUNCTION,
AGGREGATE_FUNCTION,
LAMBDA_EXPRESSION,
ARRAY_JOIN,
};
2014-07-03 22:39:13 +00:00
2010-06-24 19:12:10 +00:00
/// имя функции
String name;
2011-08-13 21:05:18 +00:00
/// аргументы
2010-06-24 19:12:10 +00:00
ASTPtr arguments;
/// параметры - для параметрических агрегатных функций. Пример: quantile(0.9)(x) - то, что в первых скобках - параметры.
ASTPtr parameters;
2014-07-03 22:39:13 +00:00
2014-12-17 15:26:24 +00:00
FunctionKind kind{UNKNOWN};
2010-06-24 19:12:10 +00:00
2014-12-17 15:26:24 +00:00
ASTFunction() = default;
ASTFunction(const StringRange range_) : ASTWithAlias(range_) {}
2011-08-09 19:19:00 +00:00
/** Получить текст, который идентифицирует этот элемент. */
2014-12-17 15:26:24 +00:00
String getID() const override { return "Function_" + name; }
2011-12-12 06:15:34 +00:00
void updateHashWith(SipHash & hash) const override
{
hash.update("Function", strlen("Function") + 1);
hash.update(name.data(), name.size() + 1);
}
2014-12-17 15:26:24 +00:00
ASTPtr clone() const override
2011-12-12 06:15:34 +00:00
{
ASTFunction * res = new ASTFunction(*this);
2014-12-17 15:26:24 +00:00
ASTPtr ptr{res};
2011-12-12 06:15:34 +00:00
res->children.clear();
if (arguments) { res->arguments = arguments->clone(); res->children.push_back(res->arguments); }
if (parameters) { res->parameters = parameters->clone(); res->children.push_back(res->parameters); }
2011-12-12 06:15:34 +00:00
2014-12-17 15:26:24 +00:00
return ptr;
2011-12-12 06:15:34 +00:00
}
2010-06-24 19:12:10 +00:00
};
template <typename... Args>
ASTPtr makeASTFunction(const String & name, Args &&... args)
{
const auto function = new ASTFunction{};
ASTPtr result{function};
function->name = name;
function->arguments = new ASTExpressionList{};
function->children.push_back(function->arguments);
function->arguments->children = { std::forward<Args>(args)... };
return result;
}
template <typename... Args>
ASTPtr makeASTFunction(const String & name, const StringRange & function_range,
const StringRange & arguments_range, Args &&... args)
{
const auto function = new ASTFunction{function_range};
ASTPtr result{function};
function->name = name;
function->arguments = new ASTExpressionList{arguments_range};
function->children.push_back(function->arguments);
function->arguments->children = { std::forward<Args>(args)... };
return result;
}
2010-06-24 19:12:10 +00:00
}