ClickHouse
71 строка · 2.0 Кб
1#include <Functions/IFunction.h>
2#include <Functions/FunctionFactory.h>
3#include <DataTypes/DataTypeNullable.h>
4#include <Core/ColumnNumbers.h>
5#include <Columns/ColumnNullable.h>
6
7
8namespace DB
9{
10
11namespace ErrorCodes
12{
13extern const int ILLEGAL_COLUMN;
14}
15
16namespace
17{
18
19/// Implements the function assumeNotNull which takes 1 argument and works as follows:
20/// - if the argument is a nullable column, return its embedded column;
21/// - otherwise return the original argument.
22/// NOTE: assumeNotNull may not be called with the NULL value.
23class FunctionAssumeNotNull : public IFunction
24{
25public:
26static constexpr auto name = "assumeNotNull";
27
28static FunctionPtr create(ContextPtr)
29{
30return std::make_shared<FunctionAssumeNotNull>();
31}
32
33std::string getName() const override
34{
35return name;
36}
37
38bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
39
40size_t getNumberOfArguments() const override { return 1; }
41bool useDefaultImplementationForNulls() const override { return false; }
42bool useDefaultImplementationForConstants() const override { return true; }
43ColumnNumbers getArgumentsThatDontImplyNullableReturnType(size_t /*number_of_arguments*/) const override { return {0}; }
44
45DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
46{
47return removeNullable(arguments[0]);
48}
49
50ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t) const override
51{
52const ColumnPtr & col = arguments[0].column;
53
54if (arguments[0].type->onlyNull() && !col->empty())
55throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Cannot create non-empty column with type Nothing");
56
57if (const auto * nullable_col = checkAndGetColumn<ColumnNullable>(*col))
58return nullable_col->getNestedColumnPtr();
59else
60return col;
61}
62};
63
64}
65
66REGISTER_FUNCTION(AssumeNotNull)
67{
68factory.registerFunction<FunctionAssumeNotNull>();
69}
70
71}
72