1#include <Functions/IFunctionImpl.h>
2#include <Functions/FunctionHelpers.h>
3#include <Functions/FunctionFactory.h>
4#include <DataTypes/DataTypesNumber.h>
5#include <Core/ColumnNumbers.h>
6#include <Columns/ColumnNullable.h>
7#include <Common/assert_cast.h>
8
9
10namespace DB
11{
12
13/// Implements the function isNotNull which returns true if a value
14/// is not null, false otherwise.
15class FunctionIsNotNull : public IFunction
16{
17public:
18 static constexpr auto name = "isNotNull";
19
20 static FunctionPtr create(const Context &)
21 {
22 return std::make_shared<FunctionIsNotNull>();
23 }
24
25 std::string getName() const override
26 {
27 return name;
28 }
29
30 size_t getNumberOfArguments() const override { return 1; }
31 bool useDefaultImplementationForNulls() const override { return false; }
32 bool useDefaultImplementationForConstants() const override { return true; }
33 ColumnNumbers getArgumentsThatDontImplyNullableReturnType(size_t /*number_of_arguments*/) const override { return {0}; }
34
35 DataTypePtr getReturnTypeImpl(const DataTypes &) const override
36 {
37 return std::make_shared<DataTypeUInt8>();
38 }
39
40 void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t input_rows_count) override
41 {
42 const ColumnWithTypeAndName & elem = block.getByPosition(arguments[0]);
43 if (auto * nullable = checkAndGetColumn<ColumnNullable>(*elem.column))
44 {
45 /// Return the negated null map.
46 auto res_column = ColumnUInt8::create(input_rows_count);
47 const auto & src_data = nullable->getNullMapData();
48 auto & res_data = assert_cast<ColumnUInt8 &>(*res_column).getData();
49
50 for (size_t i = 0; i < input_rows_count; ++i)
51 res_data[i] = !src_data[i];
52
53 block.getByPosition(result).column = std::move(res_column);
54 }
55 else
56 {
57 /// Since no element is nullable, return a constant one.
58 block.getByPosition(result).column = DataTypeUInt8().createColumnConst(elem.column->size(), 1u);
59 }
60 }
61};
62
63void registerFunctionIsNotNull(FunctionFactory & factory)
64{
65 factory.registerFunction<FunctionIsNotNull>();
66}
67
68}
69