ClickHouse
88 строк · 2.4 Кб
1#include <Functions/IFunction.h>2#include <Functions/FunctionFactory.h>3#include <Interpreters/Context.h>4#include <DataTypes/DataTypeArray.h>5#include <DataTypes/DataTypeString.h>6
7
8namespace DB9{
10
11namespace ErrorCodes12{
13extern const int ILLEGAL_TYPE_OF_ARGUMENT;14}
15
16namespace
17{
18
19class FunctionCurrentSchemas : public IFunction20{
21const String db_name;22
23public:24static constexpr auto name = "currentSchemas";25static FunctionPtr create(ContextPtr context)26{27return std::make_shared<FunctionCurrentSchemas>(context->getCurrentDatabase());28}29
30explicit FunctionCurrentSchemas(const String & db_name_) :31db_name{db_name_}32{33}34
35String getName() const override36{37return name;38}39
40size_t getNumberOfArguments() const override41{42return 1;43}44
45DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override46{47// For compatibility, function implements the same signature as Postgres'48const bool argument_is_valid = arguments.size() == 1 && isBool(arguments.front());49if (!argument_is_valid)50throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Argument for function {} must be bool", getName());51
52return std::make_shared<DataTypeArray>(std::make_shared<DataTypeString>());53}54
55bool isDeterministic() const override { return false; }56
57bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }58
59ColumnPtr executeImpl(const ColumnsWithTypeAndName &, const DataTypePtr &, size_t input_rows_count) const override60{61return DataTypeArray(std::make_shared<DataTypeString>())62.createColumnConst(input_rows_count, Array { db_name });63}64};65
66}
67
68REGISTER_FUNCTION(CurrentSchema)69{
70factory.registerFunction<FunctionCurrentSchemas>(FunctionDocumentation71{72.description=R"(73Returns a single-element array with the name of the current database
74
75Requires a boolean parameter, but it is ignored actually. It is required just for compatibility with the implementation of this function in other DB engines.
76
77[example:common]
78)",79.examples{80{"common", "SELECT current_schemas(true);", "['default']"}81}82},83FunctionFactory::CaseInsensitive);84factory.registerAlias("current_schemas", FunctionCurrentSchemas::name, FunctionFactory::CaseInsensitive);85
86}
87
88}
89