/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 "runtime/runtime_state.h" |
31 | | #include "vec/aggregate_functions/aggregate_function.h" |
32 | | #include "vec/columns/column.h" |
33 | | #include "vec/columns/column_const.h" |
34 | | #include "vec/common/assert_cast.h" |
35 | | #include "vec/core/block.h" |
36 | | #include "vec/core/column_numbers.h" |
37 | | #include "vec/core/column_with_type_and_name.h" |
38 | | #include "vec/core/columns_with_type_and_name.h" |
39 | | #include "vec/core/types.h" |
40 | | #include "vec/data_types/data_type.h" |
41 | | #include "vec/data_types/data_type_nothing.h" |
42 | | #include "vec/data_types/data_type_nullable.h" |
43 | | #include "vec/data_types/data_type_number.h" |
44 | | #include "vec/functions/function.h" |
45 | | #include "vec/functions/simple_function_factory.h" |
46 | | |
47 | | namespace doris { |
48 | | class FunctionContext; |
49 | | } // namespace doris |
50 | | |
51 | | namespace doris::vectorized { |
52 | | class FunctionNullIf : public IFunction { |
53 | | public: |
54 | | static constexpr auto name = "nullif"; |
55 | | |
56 | 4 | static FunctionPtr create() { return std::make_shared<FunctionNullIf>(); } |
57 | | |
58 | 0 | String get_name() const override { return name; } |
59 | | |
60 | 3 | size_t get_number_of_arguments() const override { return 2; } |
61 | | |
62 | 6 | bool use_default_implementation_for_nulls() const override { return false; } |
63 | | |
64 | 3 | DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { |
65 | 3 | return make_nullable(arguments[0]); |
66 | 3 | } |
67 | | |
68 | 3 | static DataTypePtr get_return_type_for_equal(const ColumnsWithTypeAndName& arguments) { |
69 | 3 | ColumnsWithTypeAndName args_without_low_cardinality(arguments); |
70 | | |
71 | 6 | for (ColumnWithTypeAndName& arg : args_without_low_cardinality) { |
72 | 6 | bool is_const = arg.column && is_column_const(*arg.column); |
73 | 6 | if (is_const) { |
74 | 0 | arg.column = assert_cast<const ColumnConst&>(*arg.column).remove_low_cardinality(); |
75 | 0 | } |
76 | 6 | } |
77 | | |
78 | 3 | if (!arguments.empty()) { |
79 | 3 | if (have_null_column(arguments)) { |
80 | 3 | return make_nullable(std::make_shared<doris::vectorized::DataTypeUInt8>()); |
81 | 3 | } |
82 | 3 | } |
83 | | |
84 | 0 | return std::make_shared<doris::vectorized::DataTypeUInt8>(); |
85 | 3 | } |
86 | | |
87 | | // nullIf(col1, col2) == if(col1 = col2, NULL, col1) |
88 | | Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
89 | 3 | size_t result, size_t input_rows_count) const override { |
90 | 3 | const ColumnsWithTypeAndName eq_columns { |
91 | 3 | block.get_by_position(arguments[0]), |
92 | 3 | block.get_by_position(arguments[1]), |
93 | 3 | }; |
94 | 3 | auto result_type = get_return_type_for_equal(eq_columns); |
95 | 3 | Block eq_temporary_block({block.get_by_position(arguments[0]), |
96 | 3 | block.get_by_position(arguments[1]), |
97 | 3 | {nullptr, result_type, ""}}); |
98 | | |
99 | 3 | auto equals_func = SimpleFunctionFactory::instance().get_function( |
100 | 3 | "eq", eq_columns, result_type, |
101 | 3 | {.enable_decimal256 = context->state()->enable_decimal256()}); |
102 | 3 | DCHECK(equals_func); |
103 | 3 | RETURN_IF_ERROR( |
104 | 3 | equals_func->execute(context, eq_temporary_block, {0, 1}, 2, input_rows_count)); |
105 | | |
106 | 3 | const ColumnWithTypeAndName new_result_column { |
107 | 3 | block.get_by_position(result), |
108 | 3 | }; |
109 | | |
110 | 3 | const ColumnWithTypeAndName if_column { |
111 | 3 | eq_temporary_block.get_by_position(2), |
112 | 3 | }; |
113 | 3 | const ColumnsWithTypeAndName if_columns { |
114 | 3 | if_column, |
115 | 3 | {block.get_by_position(result).type->create_column_const_with_default_value( |
116 | 3 | input_rows_count), |
117 | 3 | block.get_by_position(result).type, "NULL"}, |
118 | 3 | block.get_by_position(arguments[0]), |
119 | 3 | }; |
120 | | |
121 | 3 | Block temporary_block( |
122 | 3 | {if_column, |
123 | 3 | {block.get_by_position(result).type->create_column_const_with_default_value( |
124 | 3 | input_rows_count), |
125 | 3 | block.get_by_position(result).type, "NULL"}, |
126 | 3 | block.get_by_position(arguments[0]), |
127 | 3 | new_result_column}); |
128 | | |
129 | 3 | auto func_if = SimpleFunctionFactory::instance().get_function( |
130 | 3 | "if", if_columns, new_result_column.type, |
131 | 3 | {.enable_decimal256 = context->state()->enable_decimal256()}); |
132 | 3 | DCHECK(func_if); |
133 | 3 | RETURN_IF_ERROR(func_if->execute(context, temporary_block, {0, 1, 2}, 3, input_rows_count)); |
134 | 3 | block.get_by_position(result).column = temporary_block.get_by_position(3).column; |
135 | | |
136 | 3 | return Status::OK(); |
137 | 3 | } |
138 | | }; |
139 | | |
140 | 1 | void register_function_nullif(SimpleFunctionFactory& factory) { |
141 | 1 | factory.register_function<FunctionNullIf>(); |
142 | 1 | } |
143 | | } // namespace doris::vectorized |