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 <Core/ColumnNumbers.h> |
7 | #include <Columns/ColumnNullable.h> |
8 | |
9 | |
10 | namespace DB |
11 | { |
12 | |
13 | /// Implements the function nullIf which takes 2 arguments and returns |
14 | /// NULL if both arguments have the same value. Otherwise it returns the |
15 | /// value of the first argument. |
16 | class FunctionNullIf : public IFunction |
17 | { |
18 | private: |
19 | const Context & context; |
20 | public: |
21 | static constexpr auto name = "nullIf" ; |
22 | |
23 | static FunctionPtr create(const Context & context) |
24 | { |
25 | return std::make_shared<FunctionNullIf>(context); |
26 | } |
27 | |
28 | FunctionNullIf(const Context & context_) : context(context_) {} |
29 | |
30 | std::string getName() const override |
31 | { |
32 | return name; |
33 | } |
34 | |
35 | size_t getNumberOfArguments() const override { return 2; } |
36 | bool useDefaultImplementationForNulls() const override { return false; } |
37 | bool useDefaultImplementationForConstants() const override { return true; } |
38 | |
39 | DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override |
40 | { |
41 | return makeNullable(arguments[0]); |
42 | } |
43 | |
44 | void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t input_rows_count) override |
45 | { |
46 | /// nullIf(col1, col2) == if(col1 = col2, NULL, col1) |
47 | |
48 | Block temp_block = block; |
49 | |
50 | auto equals_func = FunctionFactory::instance().get("equals" , context)->build( |
51 | {temp_block.getByPosition(arguments[0]), temp_block.getByPosition(arguments[1])}); |
52 | |
53 | size_t equals_res_pos = temp_block.columns(); |
54 | temp_block.insert({nullptr, equals_func->getReturnType(), "" }); |
55 | |
56 | equals_func->execute(temp_block, {arguments[0], arguments[1]}, equals_res_pos, input_rows_count); |
57 | |
58 | /// Argument corresponding to the NULL value. |
59 | size_t null_pos = temp_block.columns(); |
60 | |
61 | /// Append a NULL column. |
62 | ColumnWithTypeAndName null_elem; |
63 | null_elem.type = block.getByPosition(result).type; |
64 | null_elem.column = null_elem.type->createColumnConstWithDefaultValue(input_rows_count); |
65 | null_elem.name = "NULL" ; |
66 | |
67 | temp_block.insert(null_elem); |
68 | |
69 | auto func_if = FunctionFactory::instance().get("if" , context)->build( |
70 | {temp_block.getByPosition(equals_res_pos), temp_block.getByPosition(null_pos), temp_block.getByPosition(arguments[0])}); |
71 | func_if->execute(temp_block, {equals_res_pos, null_pos, arguments[0]}, result, input_rows_count); |
72 | |
73 | block.getByPosition(result).column = makeNullable(std::move(temp_block.getByPosition(result).column)); |
74 | } |
75 | }; |
76 | |
77 | |
78 | void registerFunctionNullIf(FunctionFactory & factory) |
79 | { |
80 | factory.registerFunction<FunctionNullIf>(FunctionFactory::CaseInsensitive); |
81 | } |
82 | |
83 | } |
84 | |
85 | |