| 1 | #include <Functions/IFunctionImpl.h> |
| 2 | #include <Functions/FunctionHelpers.h> |
| 3 | #include <Functions/FunctionFactory.h> |
| 4 | #include <DataTypes/DataTypesNumber.h> |
| 5 | #include <DataTypes/DataTypeNullable.h> |
| 6 | #include <DataTypes/getLeastSupertype.h> |
| 7 | #include <Core/ColumnNumbers.h> |
| 8 | #include <Columns/ColumnNullable.h> |
| 9 | |
| 10 | |
| 11 | namespace DB |
| 12 | { |
| 13 | |
| 14 | /// Implements the function ifNull which takes 2 arguments and returns |
| 15 | /// the value of the 1st argument if it is not null. Otherwise it returns |
| 16 | /// the value of the 2nd argument. |
| 17 | class FunctionIfNull : public IFunction |
| 18 | { |
| 19 | public: |
| 20 | static constexpr auto name = "ifNull" ; |
| 21 | |
| 22 | FunctionIfNull(const Context & context_) : context(context_) {} |
| 23 | |
| 24 | static FunctionPtr create(const Context & context) |
| 25 | { |
| 26 | return std::make_shared<FunctionIfNull>(context); |
| 27 | } |
| 28 | |
| 29 | std::string getName() const override |
| 30 | { |
| 31 | return name; |
| 32 | } |
| 33 | |
| 34 | size_t getNumberOfArguments() const override { return 2; } |
| 35 | bool useDefaultImplementationForNulls() const override { return false; } |
| 36 | bool useDefaultImplementationForConstants() const override { return true; } |
| 37 | ColumnNumbers getArgumentsThatDontImplyNullableReturnType(size_t /*number_of_arguments*/) const override { return {0}; } |
| 38 | |
| 39 | DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override |
| 40 | { |
| 41 | if (arguments[0]->onlyNull()) |
| 42 | return arguments[1]; |
| 43 | |
| 44 | if (!arguments[0]->isNullable()) |
| 45 | return arguments[0]; |
| 46 | |
| 47 | return getLeastSupertype({removeNullable(arguments[0]), arguments[1]}); |
| 48 | } |
| 49 | |
| 50 | void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t input_rows_count) override |
| 51 | { |
| 52 | /// Always null. |
| 53 | if (block.getByPosition(arguments[0]).type->onlyNull()) |
| 54 | { |
| 55 | block.getByPosition(result).column = block.getByPosition(arguments[1]).column; |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | /// Could not contain nulls, so nullIf makes no sense. |
| 60 | if (!block.getByPosition(arguments[0]).type->isNullable()) |
| 61 | { |
| 62 | block.getByPosition(result).column = block.getByPosition(arguments[0]).column; |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | /// ifNull(col1, col2) == if(isNotNull(col1), assumeNotNull(col1), col2) |
| 67 | |
| 68 | Block temp_block = block; |
| 69 | |
| 70 | size_t is_not_null_pos = temp_block.columns(); |
| 71 | temp_block.insert({nullptr, std::make_shared<DataTypeUInt8>(), "" }); |
| 72 | size_t assume_not_null_pos = temp_block.columns(); |
| 73 | temp_block.insert({nullptr, removeNullable(block.getByPosition(arguments[0]).type), "" }); |
| 74 | |
| 75 | auto is_not_null = FunctionFactory::instance().get("isNotNull" , context)->build( |
| 76 | {temp_block.getByPosition(arguments[0])}); |
| 77 | |
| 78 | auto assume_not_null = FunctionFactory::instance().get("assumeNotNull" , context)->build( |
| 79 | {temp_block.getByPosition(arguments[0])}); |
| 80 | |
| 81 | auto func_if = FunctionFactory::instance().get("if" , context)->build( |
| 82 | {temp_block.getByPosition(is_not_null_pos), temp_block.getByPosition(assume_not_null_pos), temp_block.getByPosition(arguments[1])}); |
| 83 | |
| 84 | is_not_null->execute(temp_block, {arguments[0]}, is_not_null_pos, input_rows_count); |
| 85 | assume_not_null->execute(temp_block, {arguments[0]}, assume_not_null_pos, input_rows_count); |
| 86 | func_if->execute(temp_block, {is_not_null_pos, assume_not_null_pos, arguments[1]}, result, input_rows_count); |
| 87 | |
| 88 | block.getByPosition(result).column = std::move(temp_block.getByPosition(result).column); |
| 89 | } |
| 90 | |
| 91 | private: |
| 92 | const Context & context; |
| 93 | }; |
| 94 | |
| 95 | |
| 96 | void registerFunctionIfNull(FunctionFactory & factory) |
| 97 | { |
| 98 | factory.registerFunction<FunctionIfNull>(FunctionFactory::CaseInsensitive); |
| 99 | } |
| 100 | |
| 101 | } |
| 102 | |