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
8
9namespace DB
10{
11
12/// Implements the function isNull which returns true if a value
13/// is null, false otherwise.
14class FunctionIsNull : public IFunction
15{
16public:
17 static constexpr auto name = "isNull";
18
19 static FunctionPtr create(const Context &)
20 {
21 return std::make_shared<FunctionIsNull>();
22 }
23
24 std::string getName() const override
25 {
26 return name;
27 }
28
29 size_t getNumberOfArguments() const override { return 1; }
30 bool useDefaultImplementationForNulls() const override { return false; }
31 bool useDefaultImplementationForConstants() const override { return true; }
32 ColumnNumbers getArgumentsThatDontImplyNullableReturnType(size_t /*number_of_arguments*/) const override { return {0}; }
33
34 DataTypePtr getReturnTypeImpl(const DataTypes &) const override
35 {
36 return std::make_shared<DataTypeUInt8>();
37 }
38
39 void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t) override
40 {
41 const ColumnWithTypeAndName & elem = block.getByPosition(arguments[0]);
42 if (auto * nullable = checkAndGetColumn<ColumnNullable>(*elem.column))
43 {
44 /// Merely return the embedded null map.
45 block.getByPosition(result).column = nullable->getNullMapColumnPtr();
46 }
47 else
48 {
49 /// Since no element is nullable, return a zero-constant column representing
50 /// a zero-filled null map.
51 block.getByPosition(result).column = DataTypeUInt8().createColumnConst(elem.column->size(), 0u);
52 }
53 }
54};
55
56
57void registerFunctionIsNull(FunctionFactory & factory)
58{
59 factory.registerFunction<FunctionIsNull>();
60}
61
62}
63