/root/doris/be/src/vec/functions/nullif.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // This file is copied from |
18 | | // https://github.com/ClickHouse/ClickHouse/blob/master/src/Functions/Nullif.cpp |
19 | | // and modified by Doris |
20 | | |
21 | | #include <glog/logging.h> |
22 | | #include <stddef.h> |
23 | | |
24 | | #include <algorithm> |
25 | | #include <boost/iterator/iterator_facade.hpp> |
26 | | #include <memory> |
27 | | #include <vector> |
28 | | |
29 | | #include "common/status.h" |
30 | | #include "vec/aggregate_functions/aggregate_function.h" |
31 | | #include "vec/columns/column.h" |
32 | | #include "vec/columns/column_const.h" |
33 | | #include "vec/common/assert_cast.h" |
34 | | #include "vec/core/block.h" |
35 | | #include "vec/core/column_numbers.h" |
36 | | #include "vec/core/column_with_type_and_name.h" |
37 | | #include "vec/core/columns_with_type_and_name.h" |
38 | | #include "vec/core/types.h" |
39 | | #include "vec/data_types/data_type.h" |
40 | | #include "vec/data_types/data_type_nothing.h" |
41 | | #include "vec/data_types/data_type_nullable.h" |
42 | | #include "vec/data_types/data_type_number.h" |
43 | | #include "vec/functions/function.h" |
44 | | #include "vec/functions/simple_function_factory.h" |
45 | | |
46 | | namespace doris { |
47 | | class FunctionContext; |
48 | | } // namespace doris |
49 | | |
50 | | namespace doris::vectorized { |
51 | | class FunctionNullIf : public IFunction { |
52 | | public: |
53 | | struct NullPresence { |
54 | | bool has_nullable = false; |
55 | | bool has_null_constant = false; |
56 | | }; |
57 | | |
58 | | static constexpr auto name = "nullif"; |
59 | | |
60 | 4 | static FunctionPtr create() { return std::make_shared<FunctionNullIf>(); } |
61 | | |
62 | 0 | String get_name() const override { return name; } |
63 | | |
64 | 3 | size_t get_number_of_arguments() const override { return 2; } |
65 | | |
66 | 6 | bool use_default_implementation_for_nulls() const override { return false; } |
67 | | |
68 | 3 | DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { |
69 | 3 | return make_nullable(arguments[0]); |
70 | 3 | } |
71 | | |
72 | 3 | NullPresence get_null_resense(const ColumnsWithTypeAndName& args) const { |
73 | 3 | NullPresence res; |
74 | | |
75 | 6 | for (const auto& elem : args) { |
76 | 6 | if (!res.has_nullable) res.has_nullable = elem.type->is_nullable(); |
77 | 6 | if (!res.has_null_constant) res.has_null_constant = elem.type->only_null(); |
78 | 6 | } |
79 | | |
80 | 3 | return res; |
81 | 3 | } |
82 | | |
83 | 3 | DataTypePtr get_return_type_for_equal(const ColumnsWithTypeAndName& arguments) const { |
84 | 3 | ColumnsWithTypeAndName args_without_low_cardinality(arguments); |
85 | | |
86 | 6 | for (ColumnWithTypeAndName& arg : args_without_low_cardinality) { |
87 | 6 | bool is_const = arg.column && is_column_const(*arg.column); |
88 | 6 | if (is_const) |
89 | 0 | arg.column = assert_cast<const ColumnConst&>(*arg.column).remove_low_cardinality(); |
90 | 6 | } |
91 | | |
92 | 3 | if (!arguments.empty()) { |
93 | 3 | NullPresence null_presence = get_null_resense(arguments); |
94 | | |
95 | 3 | if (null_presence.has_null_constant) { |
96 | 0 | return make_nullable(std::make_shared<DataTypeNothing>()); |
97 | 0 | } |
98 | 3 | if (null_presence.has_nullable) { |
99 | 3 | return make_nullable(std::make_shared<doris::vectorized::DataTypeUInt8>()); |
100 | 3 | } |
101 | 3 | } |
102 | | |
103 | 0 | return std::make_shared<doris::vectorized::DataTypeUInt8>(); |
104 | 3 | } |
105 | | |
106 | | // nullIf(col1, col2) == if(col1 = col2, NULL, col1) |
107 | | Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
108 | 3 | size_t result, size_t input_rows_count) override { |
109 | 3 | const ColumnsWithTypeAndName eq_columns { |
110 | 3 | block.get_by_position(arguments[0]), |
111 | 3 | block.get_by_position(arguments[1]), |
112 | 3 | }; |
113 | 3 | auto result_type = get_return_type_for_equal(eq_columns); |
114 | 3 | Block eq_temporary_block({block.get_by_position(arguments[0]), |
115 | 3 | block.get_by_position(arguments[1]), |
116 | 3 | {nullptr, result_type, ""}}); |
117 | | |
118 | 3 | auto equals_func = |
119 | 3 | SimpleFunctionFactory::instance().get_function("eq", eq_columns, result_type); |
120 | 3 | DCHECK(equals_func); |
121 | 3 | RETURN_IF_ERROR( |
122 | 3 | equals_func->execute(context, eq_temporary_block, {0, 1}, 2, input_rows_count)); |
123 | | |
124 | 3 | const ColumnWithTypeAndName new_result_column { |
125 | 3 | block.get_by_position(result), |
126 | 3 | }; |
127 | | |
128 | 3 | const ColumnWithTypeAndName if_column { |
129 | 3 | eq_temporary_block.get_by_position(2), |
130 | 3 | }; |
131 | 3 | const ColumnsWithTypeAndName if_columns { |
132 | 3 | if_column, |
133 | 3 | {block.get_by_position(result).type->create_column_const_with_default_value( |
134 | 3 | input_rows_count), |
135 | 3 | block.get_by_position(result).type, "NULL"}, |
136 | 3 | block.get_by_position(arguments[0]), |
137 | 3 | }; |
138 | | |
139 | 3 | Block temporary_block( |
140 | 3 | {if_column, |
141 | 3 | {block.get_by_position(result).type->create_column_const_with_default_value( |
142 | 3 | input_rows_count), |
143 | 3 | block.get_by_position(result).type, "NULL"}, |
144 | 3 | block.get_by_position(arguments[0]), |
145 | 3 | new_result_column}); |
146 | | |
147 | 3 | auto func_if = SimpleFunctionFactory::instance().get_function("if", if_columns, |
148 | 3 | new_result_column.type); |
149 | 3 | DCHECK(func_if); |
150 | 3 | RETURN_IF_ERROR(func_if->execute(context, temporary_block, {0, 1, 2}, 3, input_rows_count)); |
151 | 3 | block.get_by_position(result).column = temporary_block.get_by_position(3).column; |
152 | | |
153 | 3 | return Status::OK(); |
154 | 3 | } |
155 | | }; |
156 | | |
157 | 1 | void register_function_nullif(SimpleFunctionFactory& factory) { |
158 | 1 | factory.register_function<FunctionNullIf>(); |
159 | 1 | } |
160 | | } // namespace doris::vectorized |