1#include <Functions/IFunctionImpl.h>
2#include <Functions/FunctionHelpers.h>
3#include <Functions/FunctionFactory.h>
4#include <DataTypes/DataTypeNullable.h>
5#include <Core/ColumnNumbers.h>
6#include <Columns/ColumnNullable.h>
7
8
9namespace DB
10{
11
12/// Implements the function assumeNotNull which takes 1 argument and works as follows:
13/// - if the argument is a nullable column, return its embedded column;
14/// - otherwise return the original argument.
15/// NOTE: assumeNotNull may not be called with the NULL value.
16class FunctionAssumeNotNull : public IFunction
17{
18public:
19 static constexpr auto name = "assumeNotNull";
20
21 static FunctionPtr create(const Context &)
22 {
23 return std::make_shared<FunctionAssumeNotNull>();
24 }
25
26 std::string getName() const override
27 {
28 return name;
29 }
30
31 size_t getNumberOfArguments() const override { return 1; }
32 bool useDefaultImplementationForNulls() const override { return false; }
33 bool useDefaultImplementationForConstants() const override { return true; }
34 ColumnNumbers getArgumentsThatDontImplyNullableReturnType(size_t /*number_of_arguments*/) const override { return {0}; }
35
36 DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
37 {
38 return removeNullable(arguments[0]);
39 }
40
41 void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t) override
42 {
43 const ColumnPtr & col = block.getByPosition(arguments[0]).column;
44 ColumnPtr & res_col = block.getByPosition(result).column;
45
46 if (auto * nullable_col = checkAndGetColumn<ColumnNullable>(*col))
47 res_col = nullable_col->getNestedColumnPtr();
48 else
49 res_col = col;
50 }
51};
52
53
54void registerFunctionAssumeNotNull(FunctionFactory & factory)
55{
56 factory.registerFunction<FunctionAssumeNotNull>();
57}
58
59}
60