ClickHouse

Форк
0
/
currentSchemas.cpp 
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

8
namespace DB
9
{
10

11
namespace ErrorCodes
12
{
13
    extern const int ILLEGAL_TYPE_OF_ARGUMENT;
14
}
15

16
namespace
17
{
18

19
class FunctionCurrentSchemas : public IFunction
20
{
21
    const String db_name;
22

23
public:
24
    static constexpr auto name = "currentSchemas";
25
    static FunctionPtr create(ContextPtr context)
26
    {
27
        return std::make_shared<FunctionCurrentSchemas>(context->getCurrentDatabase());
28
    }
29

30
    explicit FunctionCurrentSchemas(const String & db_name_) :
31
        db_name{db_name_}
32
    {
33
    }
34

35
    String getName() const override
36
    {
37
        return name;
38
    }
39

40
    size_t getNumberOfArguments() const override
41
    {
42
        return 1;
43
    }
44

45
    DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
46
    {
47
        // For compatibility, function implements the same signature as Postgres'
48
        const bool argument_is_valid = arguments.size() == 1 && isBool(arguments.front());
49
        if (!argument_is_valid)
50
            throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Argument for function {} must be bool", getName());
51

52
        return std::make_shared<DataTypeArray>(std::make_shared<DataTypeString>());
53
    }
54

55
    bool isDeterministic() const override { return false; }
56

57
    bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
58

59
    ColumnPtr executeImpl(const ColumnsWithTypeAndName &, const DataTypePtr &, size_t input_rows_count) const override
60
    {
61
        return DataTypeArray(std::make_shared<DataTypeString>())
62
                               .createColumnConst(input_rows_count, Array { db_name });
63
    }
64
};
65

66
}
67

68
REGISTER_FUNCTION(CurrentSchema)
69
{
70
    factory.registerFunction<FunctionCurrentSchemas>(FunctionDocumentation
71
         {
72
             .description=R"(
73
Returns a single-element array with the name of the current database
74

75
Requires 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
        },
83
        FunctionFactory::CaseInsensitive);
84
    factory.registerAlias("current_schemas", FunctionCurrentSchemas::name, FunctionFactory::CaseInsensitive);
85

86
}
87

88
}
89

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.