/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 | 3 | if (!arguments.empty()) { |
71 | 3 | if (have_null_column(arguments)) { |
72 | 3 | return make_nullable(std::make_shared<doris::vectorized::DataTypeUInt8>()); |
73 | 3 | } |
74 | 3 | } |
75 | | |
76 | 0 | return std::make_shared<doris::vectorized::DataTypeUInt8>(); |
77 | 3 | } |
78 | | |
79 | | // nullIf(col1, col2) == if(col1 = col2, NULL, col1) |
80 | | Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
81 | 3 | uint32_t result, size_t input_rows_count) const override { |
82 | 3 | const ColumnsWithTypeAndName eq_columns { |
83 | 3 | block.get_by_position(arguments[0]), |
84 | 3 | block.get_by_position(arguments[1]), |
85 | 3 | }; |
86 | 3 | auto result_type = get_return_type_for_equal(eq_columns); |
87 | 3 | Block eq_temporary_block({block.get_by_position(arguments[0]), |
88 | 3 | block.get_by_position(arguments[1]), |
89 | 3 | {nullptr, result_type, ""}}); |
90 | | |
91 | 3 | auto equals_func = SimpleFunctionFactory::instance().get_function( |
92 | 3 | "eq", eq_columns, result_type, |
93 | 3 | {.enable_decimal256 = context->state()->enable_decimal256()}); |
94 | 3 | DCHECK(equals_func); |
95 | 3 | RETURN_IF_ERROR( |
96 | 3 | equals_func->execute(context, eq_temporary_block, {0, 1}, 2, input_rows_count)); |
97 | | |
98 | 3 | const ColumnWithTypeAndName new_result_column { |
99 | 3 | block.get_by_position(result), |
100 | 3 | }; |
101 | | |
102 | 3 | const ColumnWithTypeAndName if_column { |
103 | 3 | eq_temporary_block.get_by_position(2), |
104 | 3 | }; |
105 | 3 | const ColumnsWithTypeAndName if_columns { |
106 | 3 | if_column, |
107 | 3 | {block.get_by_position(result).type->create_column_const_with_default_value( |
108 | 3 | input_rows_count), |
109 | 3 | block.get_by_position(result).type, "NULL"}, |
110 | 3 | block.get_by_position(arguments[0]), |
111 | 3 | }; |
112 | | |
113 | 3 | Block temporary_block( |
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 | new_result_column}); |
120 | | |
121 | 3 | auto func_if = SimpleFunctionFactory::instance().get_function( |
122 | 3 | "if", if_columns, new_result_column.type, |
123 | 3 | {.enable_decimal256 = context->state()->enable_decimal256()}); |
124 | 3 | DCHECK(func_if); |
125 | 3 | RETURN_IF_ERROR(func_if->execute(context, temporary_block, {0, 1, 2}, 3, input_rows_count)); |
126 | 3 | block.get_by_position(result).column = temporary_block.get_by_position(3).column; |
127 | | |
128 | 3 | return Status::OK(); |
129 | 3 | } |
130 | | }; |
131 | | |
132 | 1 | void register_function_nullif(SimpleFunctionFactory& factory) { |
133 | 1 | factory.register_function<FunctionNullIf>(); |
134 | 1 | } |
135 | | } // namespace doris::vectorized |