aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Storages/checkAndGetLiteralArgument.cpp
blob: 5baf47fe91a986457723532c3234542d00936e1a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <Storages/checkAndGetLiteralArgument.h>
#include <Core/Field.h>

namespace DB
{

namespace ErrorCodes
{
    extern const int BAD_ARGUMENTS;
}

template <typename T>
T checkAndGetLiteralArgument(const ASTPtr & arg, const String & arg_name)
{
    if (arg && arg->as<ASTLiteral>())
        return checkAndGetLiteralArgument<T>(*arg->as<ASTLiteral>(), arg_name);

    throw Exception(
        ErrorCodes::BAD_ARGUMENTS,
        "Argument '{}' must be a literal, get {} (value: {})",
        arg_name,
        arg ? arg->getID() : "NULL",
        arg ? arg->formatForErrorMessage() : "NULL");
}

template <typename T>
T checkAndGetLiteralArgument(const ASTLiteral & arg, const String & arg_name)
{
    auto requested_type = Field::TypeToEnum<NearestFieldType<std::decay_t<T>>>::value;
    auto provided_type = arg.value.getType();
    if (requested_type != provided_type)
        throw Exception(
            ErrorCodes::BAD_ARGUMENTS,
            "Argument '{}' must be a literal with type {}, got {}",
            arg_name,
            fieldTypeToString(requested_type),
            fieldTypeToString(provided_type));

    return arg.value.safeGet<T>();
}

template String checkAndGetLiteralArgument(const ASTPtr &, const String &);
template UInt64 checkAndGetLiteralArgument(const ASTPtr &, const String &);
template UInt8 checkAndGetLiteralArgument(const ASTPtr &, const String &);
template bool checkAndGetLiteralArgument(const ASTPtr &, const String &);
template String checkAndGetLiteralArgument(const ASTLiteral &, const String &);
template UInt64 checkAndGetLiteralArgument(const ASTLiteral &, const String &);
}