ClickHouse

Форк
0
/
getSubcolumn.cpp 
67 строк · 2.2 Кб
1
#include <Functions/IFunction.h>
2
#include <Functions/FunctionFactory.h>
3
#include <Functions/FunctionHelpers.h>
4

5
namespace DB
6
{
7

8
namespace ErrorCodes
9
{
10
    extern const int ILLEGAL_COLUMN;
11
}
12

13
namespace
14
{
15

16
class FunctionGetSubcolumn : public IFunction
17
{
18
public:
19
    static constexpr auto name = "getSubcolumn";
20
    static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionGetSubcolumn>(); }
21

22
    String getName() const override { return name; }
23
    size_t getNumberOfArguments() const override { return 2; }
24
    bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo &) const override { return true; }
25
    bool useDefaultImplementationForConstants() const override { return true; }
26
    ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {1}; }
27

28
    DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
29
    {
30
        auto subcolumn_name = getSubcolumnName(arguments);
31
        return arguments[0].type->getSubcolumnType(subcolumn_name);
32
    }
33

34
    ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t /*input_rows_count*/) const override
35
    {
36
        auto subcolumn_name = getSubcolumnName(arguments);
37
        return arguments[0].type->getSubcolumn(subcolumn_name, arguments[0].column);
38
    }
39

40
private:
41
    static std::string_view getSubcolumnName(const ColumnsWithTypeAndName & arguments)
42
    {
43
        const auto * column = arguments[1].column.get();
44
        if (!isString(arguments[1].type) || !column || !checkAndGetColumnConstStringOrFixedString(column))
45
            throw Exception(ErrorCodes::ILLEGAL_COLUMN,
46
                "The second argument of function {} should be a constant string with the name of a subcolumn", name);
47

48
        return column->getDataAt(0).toView();
49
    }
50
};
51

52
}
53

54
REGISTER_FUNCTION(GetSubcolumn)
55
{
56
    factory.registerFunction<FunctionGetSubcolumn>(FunctionDocumentation{
57
        .description=R"(
58
Receives the expression or identifier and constant string with the name of subcolumn.
59

60
Returns requested subcolumn extracted from the expression.
61
)",
62
        .examples{{"getSubcolumn", "SELECT getSubcolumn(array_col, 'size0'), getSubcolumn(tuple_col, 'elem_name')", ""}},
63
        .categories{"OtherFunctions"}
64
    });
65
}
66

67
}
68

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

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

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

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