Coverage Report

Created: 2025-10-28 14:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/vec/functions/if.cpp
Line
Count
Source
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/If.cpp
19
// and modified by Doris
20
21
#include "if.h"
22
23
#include <glog/logging.h>
24
#include <stddef.h>
25
26
#include <algorithm>
27
#include <boost/iterator/iterator_facade.hpp>
28
#include <memory>
29
#include <type_traits>
30
#include <utility>
31
32
#include "common/status.h"
33
#include "runtime/define_primitive_type.h"
34
#include "runtime/primitive_type.h"
35
#include "util/simd/bits.h"
36
#include "vec/aggregate_functions/aggregate_function.h"
37
#include "vec/columns/column.h"
38
#include "vec/columns/column_const.h"
39
#include "vec/columns/column_nullable.h"
40
#include "vec/columns/column_vector.h"
41
#include "vec/common/assert_cast.h"
42
#include "vec/common/typeid_cast.h"
43
#include "vec/core/block.h"
44
#include "vec/core/call_on_type_index.h"
45
#include "vec/core/column_numbers.h"
46
#include "vec/core/column_with_type_and_name.h"
47
#include "vec/core/types.h"
48
#include "vec/data_types/data_type.h"
49
#include "vec/data_types/data_type_date_or_datetime_v2.h"
50
#include "vec/data_types/data_type_decimal.h"
51
#include "vec/data_types/data_type_ipv4.h"
52
#include "vec/data_types/data_type_ipv6.h"
53
#include "vec/data_types/data_type_nullable.h"
54
#include "vec/data_types/data_type_number.h"
55
#include "vec/data_types/data_type_time.h"
56
#include "vec/functions/cast_type_to_either.h"
57
#include "vec/functions/function.h"
58
#include "vec/functions/function_helpers.h"
59
#include "vec/functions/simple_function_factory.h"
60
namespace doris {
61
class FunctionContext;
62
63
namespace vectorized {
64
namespace NumberTraits {
65
struct Error;
66
} // namespace NumberTraits
67
} // namespace vectorized
68
} // namespace doris
69
70
namespace doris::vectorized {
71
72
8
size_t count_true_with_notnull(const ColumnPtr& col) {
73
8
    if (col->only_null()) {
74
0
        return 0;
75
0
    }
76
77
8
    if (const auto* const_col = check_and_get_column_const<ColumnUInt8>(col.get())) {
78
0
        bool is_true = const_col->get_bool(0);
79
0
        return is_true ? col->size() : 0;
80
0
    }
81
82
8
    auto count = col->size();
83
8
    if (col->is_nullable()) {
84
3
        const auto* nullable = assert_cast<const ColumnNullable*>(col.get());
85
3
        const auto* __restrict null_data = nullable->get_null_map_data().data();
86
3
        const auto* __restrict bool_data =
87
3
                ((const ColumnUInt8&)(nullable->get_nested_column())).get_data().data();
88
89
3
        size_t null_count = count - simd::count_zero_num((const int8_t*)null_data, count);
90
91
3
        if (null_count == count) {
92
0
            return 0;
93
3
        } else if (null_count == 0) {
94
0
            size_t true_count = count - simd::count_zero_num((const int8_t*)bool_data, count);
95
0
            return true_count;
96
3
        } else {
97
            // In fact, the null_count maybe is different with true_count, but it's no impact
98
3
            return null_count;
99
3
        }
100
5
    } else {
101
5
        const auto* bool_col = assert_cast<const ColumnUInt8*>(col.get());
102
5
        const auto* __restrict bool_data = bool_col->get_data().data();
103
5
        return count - simd::count_zero_num((const int8_t*)bool_data, count);
104
5
    }
105
8
}
106
// todo(wb) support llvm codegen
107
class FunctionIf : public IFunction {
108
public:
109
    static constexpr auto name = "if";
110
111
10
    static FunctionPtr create() { return std::make_shared<FunctionIf>(); }
112
1
    String get_name() const override { return name; }
113
114
8
    size_t get_number_of_arguments() const override { return 3; }
115
16
    bool use_default_implementation_for_nulls() const override { return false; }
116
8
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
117
        // if return type is custom, one of nullable return type will be nullable
118
8
        bool nullable = arguments[1]->is_nullable() || arguments[2]->is_nullable();
119
8
        if (nullable) {
120
8
            return make_nullable(arguments[1]);
121
8
        } else {
122
0
            return arguments[1];
123
0
        }
124
8
    }
125
126
39
    static ColumnPtr materialize_column_if_const(const ColumnPtr& column) {
127
39
        return column->convert_to_full_column_if_const();
128
39
    }
129
130
0
    static ColumnPtr make_nullable_column_if_not(const ColumnPtr& column) {
131
0
        if (is_column_nullable(*column)) return column;
132
133
0
        return ColumnNullable::create(materialize_column_if_const(column),
134
0
                                      ColumnUInt8::create(column->size(), 0));
135
0
    }
136
137
10
    static ColumnPtr get_nested_column(const ColumnPtr& column) {
138
10
        if (auto* nullable = check_and_get_column<ColumnNullable>(*column))
139
10
            return nullable->get_nested_column_ptr();
140
0
        else if (const auto* column_const = check_and_get_column<ColumnConst>(*column))
141
0
            return ColumnConst::create(get_nested_column(column_const->get_data_column_ptr()),
142
0
                                       column->size());
143
144
0
        return column;
145
10
    }
146
147
    Status execute_generic(Block& block, const ColumnUInt8* cond_col,
148
                           const ColumnWithTypeAndName& then_col_type_name,
149
                           const ColumnWithTypeAndName& else_col_type_name, uint32_t result,
150
1
                           size_t input_row_count) const {
151
1
        MutableColumnPtr result_column = block.get_by_position(result).type->create_column();
152
1
        result_column->reserve(input_row_count);
153
154
1
        const IColumn& then_col = *then_col_type_name.column;
155
1
        const IColumn& else_col = *else_col_type_name.column;
156
1
        bool then_is_const = is_column_const(then_col);
157
1
        bool else_is_const = is_column_const(else_col);
158
159
1
        const auto& cond_array = cond_col->get_data();
160
161
1
        if (then_is_const && else_is_const) {
162
0
            const IColumn& then_nested_column =
163
0
                    assert_cast<const ColumnConst&>(then_col).get_data_column();
164
0
            const IColumn& else_nested_column =
165
0
                    assert_cast<const ColumnConst&>(else_col).get_data_column();
166
0
            for (size_t i = 0; i < input_row_count; i++) {
167
0
                if (cond_array[i])
168
0
                    result_column->insert_from(then_nested_column, 0);
169
0
                else
170
0
                    result_column->insert_from(else_nested_column, 0);
171
0
            }
172
1
        } else if (then_is_const) {
173
0
            const IColumn& then_nested_column =
174
0
                    assert_cast<const ColumnConst&>(then_col).get_data_column();
175
176
0
            for (size_t i = 0; i < input_row_count; i++) {
177
0
                if (cond_array[i])
178
0
                    result_column->insert_from(then_nested_column, 0);
179
0
                else
180
0
                    result_column->insert_from(else_col, i);
181
0
            }
182
1
        } else if (else_is_const) {
183
0
            const IColumn& else_nested_column =
184
0
                    assert_cast<const ColumnConst&>(else_col).get_data_column();
185
186
0
            for (size_t i = 0; i < input_row_count; i++) {
187
0
                if (cond_array[i])
188
0
                    result_column->insert_from(then_col, i);
189
0
                else
190
0
                    result_column->insert_from(else_nested_column, 0);
191
0
            }
192
1
        } else {
193
4
            for (size_t i = 0; i < input_row_count; i++) {
194
3
                result_column->insert_from(cond_array[i] ? then_col : else_col, i);
195
3
            }
196
1
        }
197
1
        block.replace_by_position(result, std::move(result_column));
198
1
        return Status::OK();
199
1
    }
200
201
    template <PrimitiveType PType>
202
    Status execute_basic_type(Block& block, const ColumnUInt8* cond_col,
203
                              const ColumnWithTypeAndName& then_col,
204
                              const ColumnWithTypeAndName& else_col, uint32_t result,
205
9
                              Status& status) const {
206
9
        if (then_col.type->get_primitive_type() != else_col.type->get_primitive_type()) {
207
0
            return Status::InternalError(
208
0
                    "then and else column type must be same for function {} , but got {} , {}",
209
0
                    get_name(), then_col.type->get_name(), else_col.type->get_name());
210
0
        }
211
212
9
        auto res_column =
213
9
                NumIfImpl<PType>::execute_if(cond_col->get_data(), then_col.column, else_col.column,
214
9
                                             block.get_by_position(result).type->get_scale());
215
9
        if (!res_column) {
216
0
            return Status::InternalError("unexpected args column {} , {} , of function {}",
217
0
                                         then_col.column->get_name(), else_col.column->get_name(),
218
0
                                         get_name());
219
0
        }
220
9
        block.replace_by_position(result, std::move(res_column));
221
9
        return Status::OK();
222
9
    }
_ZNK5doris10vectorized10FunctionIf18execute_basic_typeILNS_13PrimitiveTypeE2EEENS_6StatusERNS0_5BlockEPKNS0_12ColumnVectorILS3_2EEERKNS0_21ColumnWithTypeAndNameESD_jRS4_
Line
Count
Source
205
5
                              Status& status) const {
206
5
        if (then_col.type->get_primitive_type() != else_col.type->get_primitive_type()) {
207
0
            return Status::InternalError(
208
0
                    "then and else column type must be same for function {} , but got {} , {}",
209
0
                    get_name(), then_col.type->get_name(), else_col.type->get_name());
210
0
        }
211
212
5
        auto res_column =
213
5
                NumIfImpl<PType>::execute_if(cond_col->get_data(), then_col.column, else_col.column,
214
5
                                             block.get_by_position(result).type->get_scale());
215
5
        if (!res_column) {
216
0
            return Status::InternalError("unexpected args column {} , {} , of function {}",
217
0
                                         then_col.column->get_name(), else_col.column->get_name(),
218
0
                                         get_name());
219
0
        }
220
5
        block.replace_by_position(result, std::move(res_column));
221
5
        return Status::OK();
222
5
    }
Unexecuted instantiation: _ZNK5doris10vectorized10FunctionIf18execute_basic_typeILNS_13PrimitiveTypeE3EEENS_6StatusERNS0_5BlockEPKNS0_12ColumnVectorILS3_2EEERKNS0_21ColumnWithTypeAndNameESD_jRS4_
Unexecuted instantiation: _ZNK5doris10vectorized10FunctionIf18execute_basic_typeILNS_13PrimitiveTypeE4EEENS_6StatusERNS0_5BlockEPKNS0_12ColumnVectorILS3_2EEERKNS0_21ColumnWithTypeAndNameESD_jRS4_
_ZNK5doris10vectorized10FunctionIf18execute_basic_typeILNS_13PrimitiveTypeE5EEENS_6StatusERNS0_5BlockEPKNS0_12ColumnVectorILS3_2EEERKNS0_21ColumnWithTypeAndNameESD_jRS4_
Line
Count
Source
205
2
                              Status& status) const {
206
2
        if (then_col.type->get_primitive_type() != else_col.type->get_primitive_type()) {
207
0
            return Status::InternalError(
208
0
                    "then and else column type must be same for function {} , but got {} , {}",
209
0
                    get_name(), then_col.type->get_name(), else_col.type->get_name());
210
0
        }
211
212
2
        auto res_column =
213
2
                NumIfImpl<PType>::execute_if(cond_col->get_data(), then_col.column, else_col.column,
214
2
                                             block.get_by_position(result).type->get_scale());
215
2
        if (!res_column) {
216
0
            return Status::InternalError("unexpected args column {} , {} , of function {}",
217
0
                                         then_col.column->get_name(), else_col.column->get_name(),
218
0
                                         get_name());
219
0
        }
220
2
        block.replace_by_position(result, std::move(res_column));
221
2
        return Status::OK();
222
2
    }
Unexecuted instantiation: _ZNK5doris10vectorized10FunctionIf18execute_basic_typeILNS_13PrimitiveTypeE6EEENS_6StatusERNS0_5BlockEPKNS0_12ColumnVectorILS3_2EEERKNS0_21ColumnWithTypeAndNameESD_jRS4_
Unexecuted instantiation: _ZNK5doris10vectorized10FunctionIf18execute_basic_typeILNS_13PrimitiveTypeE7EEENS_6StatusERNS0_5BlockEPKNS0_12ColumnVectorILS3_2EEERKNS0_21ColumnWithTypeAndNameESD_jRS4_
Unexecuted instantiation: _ZNK5doris10vectorized10FunctionIf18execute_basic_typeILNS_13PrimitiveTypeE8EEENS_6StatusERNS0_5BlockEPKNS0_12ColumnVectorILS3_2EEERKNS0_21ColumnWithTypeAndNameESD_jRS4_
_ZNK5doris10vectorized10FunctionIf18execute_basic_typeILNS_13PrimitiveTypeE9EEENS_6StatusERNS0_5BlockEPKNS0_12ColumnVectorILS3_2EEERKNS0_21ColumnWithTypeAndNameESD_jRS4_
Line
Count
Source
205
1
                              Status& status) const {
206
1
        if (then_col.type->get_primitive_type() != else_col.type->get_primitive_type()) {
207
0
            return Status::InternalError(
208
0
                    "then and else column type must be same for function {} , but got {} , {}",
209
0
                    get_name(), then_col.type->get_name(), else_col.type->get_name());
210
0
        }
211
212
1
        auto res_column =
213
1
                NumIfImpl<PType>::execute_if(cond_col->get_data(), then_col.column, else_col.column,
214
1
                                             block.get_by_position(result).type->get_scale());
215
1
        if (!res_column) {
216
0
            return Status::InternalError("unexpected args column {} , {} , of function {}",
217
0
                                         then_col.column->get_name(), else_col.column->get_name(),
218
0
                                         get_name());
219
0
        }
220
1
        block.replace_by_position(result, std::move(res_column));
221
1
        return Status::OK();
222
1
    }
Unexecuted instantiation: _ZNK5doris10vectorized10FunctionIf18execute_basic_typeILNS_13PrimitiveTypeE28EEENS_6StatusERNS0_5BlockEPKNS0_12ColumnVectorILS3_2EEERKNS0_21ColumnWithTypeAndNameESD_jRS4_
Unexecuted instantiation: _ZNK5doris10vectorized10FunctionIf18execute_basic_typeILNS_13PrimitiveTypeE29EEENS_6StatusERNS0_5BlockEPKNS0_12ColumnVectorILS3_2EEERKNS0_21ColumnWithTypeAndNameESD_jRS4_
Unexecuted instantiation: _ZNK5doris10vectorized10FunctionIf18execute_basic_typeILNS_13PrimitiveTypeE20EEENS_6StatusERNS0_5BlockEPKNS0_12ColumnVectorILS3_2EEERKNS0_21ColumnWithTypeAndNameESD_jRS4_
Unexecuted instantiation: _ZNK5doris10vectorized10FunctionIf18execute_basic_typeILNS_13PrimitiveTypeE30EEENS_6StatusERNS0_5BlockEPKNS0_12ColumnVectorILS3_2EEERKNS0_21ColumnWithTypeAndNameESD_jRS4_
Unexecuted instantiation: _ZNK5doris10vectorized10FunctionIf18execute_basic_typeILNS_13PrimitiveTypeE35EEENS_6StatusERNS0_5BlockEPKNS0_12ColumnVectorILS3_2EEERKNS0_21ColumnWithTypeAndNameESD_jRS4_
Unexecuted instantiation: _ZNK5doris10vectorized10FunctionIf18execute_basic_typeILNS_13PrimitiveTypeE11EEENS_6StatusERNS0_5BlockEPKNS0_12ColumnVectorILS3_2EEERKNS0_21ColumnWithTypeAndNameESD_jRS4_
Unexecuted instantiation: _ZNK5doris10vectorized10FunctionIf18execute_basic_typeILNS_13PrimitiveTypeE25EEENS_6StatusERNS0_5BlockEPKNS0_12ColumnVectorILS3_2EEERKNS0_21ColumnWithTypeAndNameESD_jRS4_
Unexecuted instantiation: _ZNK5doris10vectorized10FunctionIf18execute_basic_typeILNS_13PrimitiveTypeE26EEENS_6StatusERNS0_5BlockEPKNS0_12ColumnVectorILS3_2EEERKNS0_21ColumnWithTypeAndNameESD_jRS4_
_ZNK5doris10vectorized10FunctionIf18execute_basic_typeILNS_13PrimitiveTypeE12EEENS_6StatusERNS0_5BlockEPKNS0_12ColumnVectorILS3_2EEERKNS0_21ColumnWithTypeAndNameESD_jRS4_
Line
Count
Source
205
1
                              Status& status) const {
206
1
        if (then_col.type->get_primitive_type() != else_col.type->get_primitive_type()) {
207
0
            return Status::InternalError(
208
0
                    "then and else column type must be same for function {} , but got {} , {}",
209
0
                    get_name(), then_col.type->get_name(), else_col.type->get_name());
210
0
        }
211
212
1
        auto res_column =
213
1
                NumIfImpl<PType>::execute_if(cond_col->get_data(), then_col.column, else_col.column,
214
1
                                             block.get_by_position(result).type->get_scale());
215
1
        if (!res_column) {
216
0
            return Status::InternalError("unexpected args column {} , {} , of function {}",
217
0
                                         then_col.column->get_name(), else_col.column->get_name(),
218
0
                                         get_name());
219
0
        }
220
1
        block.replace_by_position(result, std::move(res_column));
221
1
        return Status::OK();
222
1
    }
Unexecuted instantiation: _ZNK5doris10vectorized10FunctionIf18execute_basic_typeILNS_13PrimitiveTypeE27EEENS_6StatusERNS0_5BlockEPKNS0_12ColumnVectorILS3_2EEERKNS0_21ColumnWithTypeAndNameESD_jRS4_
Unexecuted instantiation: _ZNK5doris10vectorized10FunctionIf18execute_basic_typeILNS_13PrimitiveTypeE36EEENS_6StatusERNS0_5BlockEPKNS0_12ColumnVectorILS3_2EEERKNS0_21ColumnWithTypeAndNameESD_jRS4_
Unexecuted instantiation: _ZNK5doris10vectorized10FunctionIf18execute_basic_typeILNS_13PrimitiveTypeE37EEENS_6StatusERNS0_5BlockEPKNS0_12ColumnVectorILS3_2EEERKNS0_21ColumnWithTypeAndNameESD_jRS4_
223
224
    Status execute_for_null_then_else(FunctionContext* context, Block& block,
225
                                      const ColumnWithTypeAndName& arg_cond,
226
                                      const ColumnWithTypeAndName& arg_then,
227
                                      const ColumnWithTypeAndName& arg_else, uint32_t result,
228
18
                                      size_t input_rows_count, bool& handled) const {
229
18
        bool then_is_null = arg_then.column->only_null();
230
18
        bool else_is_null = arg_else.column->only_null();
231
232
18
        handled = false;
233
18
        if (!then_is_null && !else_is_null) {
234
15
            return Status::OK();
235
15
        }
236
237
3
        if (then_is_null && else_is_null) {
238
0
            block.get_by_position(result).column =
239
0
                    block.get_by_position(result).type->create_column_const_with_default_value(
240
0
                            input_rows_count);
241
0
            handled = true;
242
0
            return Status::OK();
243
0
        }
244
245
3
        const auto* cond_col = typeid_cast<const ColumnUInt8*>(arg_cond.column.get());
246
3
        const ColumnConst* cond_const_col =
247
3
                check_and_get_column_const<ColumnUInt8>(arg_cond.column.get());
248
249
        /// If then is NULL, we create Nullable column with null mask OR-ed with condition.
250
3
        if (then_is_null) {
251
3
            if (cond_col) {
252
3
                if (is_column_nullable(*arg_else.column)) { // if(cond, null, nullable)
253
3
                    auto arg_else_column = arg_else.column;
254
3
                    auto result_column = (*std::move(arg_else_column)).mutate();
255
3
                    assert_cast<ColumnNullable&>(*result_column)
256
3
                            .apply_null_map(assert_cast<const ColumnUInt8&>(*arg_cond.column));
257
3
                    block.replace_by_position(result, std::move(result_column));
258
3
                } else { // if(cond, null, not_nullable)
259
0
                    block.replace_by_position(
260
0
                            result,
261
0
                            ColumnNullable::create(materialize_column_if_const(arg_else.column),
262
0
                                                   arg_cond.column));
263
0
                }
264
3
            } else if (cond_const_col) {
265
0
                if (cond_const_col->get_value<UInt8>()) { // if(true, null, else)
266
0
                    block.get_by_position(result).column =
267
0
                            block.get_by_position(result).type->create_column()->clone_resized(
268
0
                                    input_rows_count);
269
0
                } else { // if(false, null, else)
270
0
                    block.get_by_position(result).column =
271
0
                            make_nullable_column_if_not(arg_else.column);
272
0
                }
273
0
            } else {
274
0
                return Status::InternalError(
275
0
                        "Illegal column {} of first argument of function {}. Must be ColumnUInt8 "
276
0
                        "or ColumnConstUInt8.",
277
0
                        arg_cond.column->get_name(), get_name());
278
0
            }
279
3
        } else { /// If else is NULL, we create Nullable column with null mask OR-ed with negated condition.
280
0
            if (cond_col) {
281
0
                size_t size = input_rows_count;
282
283
0
                if (is_column_nullable(*arg_then.column)) { // if(cond, nullable, NULL)
284
0
                    auto arg_then_column = arg_then.column;
285
0
                    auto result_column = (*std::move(arg_then_column)).mutate();
286
0
                    assert_cast<ColumnNullable&>(*result_column)
287
0
                            .apply_negated_null_map(
288
0
                                    assert_cast<const ColumnUInt8&>(*arg_cond.column));
289
0
                    block.replace_by_position(result, std::move(result_column));
290
0
                } else { // if(cond, not_nullable, NULL)
291
0
                    const auto& null_map_data = cond_col->get_data();
292
0
                    auto negated_null_map = ColumnUInt8::create();
293
0
                    auto& negated_null_map_data = negated_null_map->get_data();
294
0
                    negated_null_map_data.resize(size);
295
296
0
                    for (size_t i = 0; i < size; ++i) {
297
0
                        negated_null_map_data[i] = !null_map_data[i];
298
0
                    }
299
300
0
                    block.replace_by_position(
301
0
                            result,
302
0
                            ColumnNullable::create(materialize_column_if_const(arg_then.column),
303
0
                                                   std::move(negated_null_map)));
304
0
                }
305
0
            } else if (cond_const_col) {
306
0
                if (cond_const_col->get_value<UInt8>()) { // if(true, then, NULL)
307
0
                    block.get_by_position(result).column =
308
0
                            make_nullable_column_if_not(arg_then.column);
309
0
                } else { // if(false, then, NULL)
310
0
                    block.get_by_position(result).column =
311
0
                            block.get_by_position(result).type->create_column()->clone_resized(
312
0
                                    input_rows_count);
313
0
                }
314
0
            } else {
315
0
                return Status::InternalError(
316
0
                        "Illegal column {} of first argument of function {}. Must be ColumnUInt8 "
317
0
                        "or ColumnConstUInt8.",
318
0
                        arg_cond.column->get_name(), get_name());
319
0
            }
320
0
        }
321
3
        handled = true;
322
3
        return Status::OK();
323
3
    }
324
325
    Status execute_for_nullable_then_else(FunctionContext* context, Block& block,
326
                                          const ColumnWithTypeAndName& arg_cond,
327
                                          const ColumnWithTypeAndName& arg_then,
328
                                          const ColumnWithTypeAndName& arg_else, uint32_t result,
329
15
                                          size_t input_rows_count, bool& handled) const {
330
15
        auto then_type_is_nullable = arg_then.type->is_nullable();
331
15
        auto else_type_is_nullable = arg_else.type->is_nullable();
332
15
        handled = false;
333
15
        if (!then_type_is_nullable && !else_type_is_nullable) {
334
10
            return Status::OK();
335
10
        }
336
337
5
        auto* then_is_nullable = check_and_get_column<ColumnNullable>(*arg_then.column);
338
5
        auto* else_is_nullable = check_and_get_column<ColumnNullable>(*arg_else.column);
339
5
        bool then_column_is_const_nullable = false;
340
5
        bool else_column_is_const_nullable = false;
341
5
        if (then_type_is_nullable && then_is_nullable == nullptr) {
342
            //this case is a const(nullable column)
343
0
            auto& const_column = assert_cast<const ColumnConst&>(*arg_then.column);
344
0
            then_is_nullable =
345
0
                    assert_cast<const ColumnNullable*>(const_column.get_data_column_ptr().get());
346
0
            then_column_is_const_nullable = true;
347
0
        }
348
349
5
        if (else_type_is_nullable && else_is_nullable == nullptr) {
350
            //this case is a const(nullable column)
351
0
            auto& const_column = assert_cast<const ColumnConst&>(*arg_else.column);
352
0
            else_is_nullable =
353
0
                    assert_cast<const ColumnNullable*>(const_column.get_data_column_ptr().get());
354
0
            else_column_is_const_nullable = true;
355
0
        }
356
357
        /** Calculate null mask of result and nested column separately.
358
          */
359
5
        ColumnPtr result_null_mask;
360
5
        {
361
            // get null map from column:
362
            // a. get_null_map_column_ptr() : it's a real nullable column, so could get it from nullable column
363
            // b. create a const_nullmap_column: it's a not nullable column or a const nullable column, contain a const value
364
5
            Block temporary_block;
365
5
            temporary_block.insert(arg_cond);
366
5
            auto then_nested_null_map =
367
5
                    (then_type_is_nullable && !then_column_is_const_nullable)
368
5
                            ? then_is_nullable->get_null_map_column_ptr()
369
5
                            : DataTypeUInt8().create_column_const_with_default_value(
370
0
                                      input_rows_count);
371
5
            temporary_block.insert({then_nested_null_map, std::make_shared<DataTypeUInt8>(),
372
5
                                    "then_column_null_map"});
373
374
5
            auto else_nested_null_map =
375
5
                    (else_type_is_nullable && !else_column_is_const_nullable)
376
5
                            ? else_is_nullable->get_null_map_column_ptr()
377
5
                            : DataTypeUInt8().create_column_const_with_default_value(
378
0
                                      input_rows_count);
379
5
            temporary_block.insert({else_nested_null_map, std::make_shared<DataTypeUInt8>(),
380
5
                                    "else_column_null_map"});
381
5
            temporary_block.insert(
382
5
                    {nullptr, std::make_shared<DataTypeUInt8>(), "result_column_null_map"});
383
384
5
            RETURN_IF_ERROR(_execute_impl_internal(context, temporary_block, {0, 1, 2}, 3,
385
5
                                                   temporary_block.rows()));
386
387
5
            result_null_mask = temporary_block.get_by_position(3).column;
388
5
        }
389
390
0
        ColumnPtr result_nested_column;
391
392
5
        {
393
5
            Block temporary_block(
394
5
                    {arg_cond,
395
5
                     {get_nested_column(arg_then.column), remove_nullable(arg_then.type), ""},
396
5
                     {get_nested_column(arg_else.column), remove_nullable(arg_else.type), ""},
397
5
                     {nullptr, remove_nullable(block.get_by_position(result).type), ""}});
398
399
5
            RETURN_IF_ERROR(_execute_impl_internal(context, temporary_block, {0, 1, 2}, 3,
400
5
                                                   temporary_block.rows()));
401
402
5
            result_nested_column = temporary_block.get_by_position(3).column;
403
5
        }
404
405
0
        auto column = ColumnNullable::create(materialize_column_if_const(result_nested_column),
406
5
                                             materialize_column_if_const(result_null_mask));
407
5
        block.replace_by_position(result, std::move(column));
408
5
        handled = true;
409
5
        return Status::OK();
410
5
    }
411
412
    Status execute_for_null_condition(FunctionContext* context, Block& block,
413
                                      const ColumnNumbers& arguments,
414
                                      const ColumnWithTypeAndName& arg_cond,
415
                                      const ColumnWithTypeAndName& arg_then,
416
                                      const ColumnWithTypeAndName& arg_else, uint32_t result,
417
21
                                      bool& handled) const {
418
21
        bool cond_is_null = arg_cond.column->only_null();
419
21
        handled = false;
420
421
21
        if (cond_is_null) {
422
0
            block.replace_by_position(result,
423
0
                                      arg_else.column->clone_resized(arg_cond.column->size()));
424
0
            handled = true;
425
0
            return Status::OK();
426
0
        }
427
428
21
        if (const auto* nullable = check_and_get_column<ColumnNullable>(*arg_cond.column)) {
429
3
            DCHECK(remove_nullable(arg_cond.type)->get_primitive_type() ==
430
3
                   PrimitiveType::TYPE_BOOLEAN);
431
432
            // update nested column by null map
433
3
            const auto* __restrict null_map = nullable->get_null_map_data().data();
434
3
            auto* __restrict nested_bool_data =
435
3
                    ((ColumnUInt8&)(nullable->get_nested_column())).get_data().data();
436
3
            auto rows = nullable->size();
437
12
            for (size_t i = 0; i < rows; i++) {
438
9
                nested_bool_data[i] &= !null_map[i];
439
9
            }
440
3
            auto column_size = block.columns();
441
3
            block.insert({nullable->get_nested_column_ptr(), remove_nullable(arg_cond.type),
442
3
                          arg_cond.name});
443
444
3
            handled = true;
445
3
            return _execute_impl_internal(context, block, {column_size, arguments[1], arguments[2]},
446
3
                                          result, rows);
447
3
        }
448
18
        return Status::OK();
449
21
    }
450
451
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
452
8
                        uint32_t result, size_t input_rows_count) const override {
453
8
        const ColumnWithTypeAndName& arg_then = block.get_by_position(arguments[1]);
454
8
        const ColumnWithTypeAndName& arg_else = block.get_by_position(arguments[2]);
455
456
        /// A case for identical then and else (pointers are the same).
457
8
        if (arg_then.column.get() == arg_else.column.get()) {
458
            /// Just point result to them.
459
0
            block.replace_by_position(result, arg_then.column);
460
0
            return Status::OK();
461
0
        }
462
463
8
        ColumnWithTypeAndName& cond_column = block.get_by_position(arguments[0]);
464
8
        cond_column.column = materialize_column_if_const(cond_column.column);
465
8
        const ColumnWithTypeAndName& arg_cond = block.get_by_position(arguments[0]);
466
467
8
        auto true_count = count_true_with_notnull(arg_cond.column);
468
8
        auto item_count = arg_cond.column->size();
469
8
        if (true_count == item_count || true_count == 0) {
470
0
            bool result_nullable = block.get_by_position(result).type->is_nullable();
471
0
            if (true_count == item_count) {
472
0
                block.replace_by_position(
473
0
                        result,
474
0
                        result_nullable
475
0
                                ? make_nullable(arg_then.column->clone_resized(input_rows_count))
476
0
                                : arg_then.column->clone_resized(input_rows_count));
477
0
            } else {
478
0
                block.replace_by_position(
479
0
                        result,
480
0
                        result_nullable
481
0
                                ? make_nullable(arg_else.column->clone_resized(input_rows_count))
482
0
                                : arg_else.column->clone_resized(input_rows_count));
483
0
            }
484
0
            return Status::OK();
485
0
        }
486
487
8
        return _execute_impl_internal(context, block, arguments, result, input_rows_count);
488
8
    }
489
490
    Status _execute_impl_internal(FunctionContext* context, Block& block,
491
                                  const ColumnNumbers& arguments, uint32_t result,
492
21
                                  size_t input_rows_count) const {
493
21
        const ColumnWithTypeAndName& arg_then = block.get_by_position(arguments[1]);
494
21
        const ColumnWithTypeAndName& arg_else = block.get_by_position(arguments[2]);
495
21
        ColumnWithTypeAndName& cond_column = block.get_by_position(arguments[0]);
496
21
        cond_column.column = materialize_column_if_const(cond_column.column);
497
21
        const ColumnWithTypeAndName& arg_cond = block.get_by_position(arguments[0]);
498
499
21
        Status ret = Status::OK();
500
21
        bool handled = false;
501
21
        RETURN_IF_ERROR(execute_for_null_condition(context, block, arguments, arg_cond, arg_then,
502
21
                                                   arg_else, result, handled));
503
504
21
        if (!handled) {
505
18
            RETURN_IF_ERROR(execute_for_null_then_else(context, block, arg_cond, arg_then, arg_else,
506
18
                                                       result, input_rows_count, handled));
507
18
        }
508
509
21
        if (!handled) {
510
15
            RETURN_IF_ERROR(execute_for_nullable_then_else(context, block, arg_cond, arg_then,
511
15
                                                           arg_else, result, input_rows_count,
512
15
                                                           handled));
513
15
        }
514
515
21
        if (handled) {
516
11
            return Status::OK();
517
11
        }
518
519
10
        const auto* cond_col = assert_cast<const ColumnUInt8*>(arg_cond.column.get());
520
10
        const ColumnConst* cond_const_col =
521
10
                check_and_get_column_const<ColumnUInt8>(arg_cond.column.get());
522
523
10
        if (cond_const_col) {
524
0
            block.get_by_position(result).column =
525
0
                    cond_const_col->get_value<UInt8>() ? arg_then.column : arg_else.column;
526
0
            return Status::OK();
527
0
        }
528
529
10
        Status vec_exec;
530
531
10
        auto call = [&](const auto& type) -> bool {
532
9
            using DataType = std::decay_t<decltype(type)>;
533
9
            vec_exec = execute_basic_type<DataType::PType>(block, cond_col, arg_then, arg_else,
534
9
                                                           result, vec_exec);
535
9
            return true;
536
9
        };
_ZZNK5doris10vectorized10FunctionIf22_execute_impl_internalEPNS_15FunctionContextERNS0_5BlockERKSt6vectorIjSaIjEEjmENKUlRKT_E_clINS0_16DispatchDataTypeILNS_13PrimitiveTypeE2EEEEEbSD_
Line
Count
Source
531
5
        auto call = [&](const auto& type) -> bool {
532
5
            using DataType = std::decay_t<decltype(type)>;
533
5
            vec_exec = execute_basic_type<DataType::PType>(block, cond_col, arg_then, arg_else,
534
5
                                                           result, vec_exec);
535
5
            return true;
536
5
        };
Unexecuted instantiation: _ZZNK5doris10vectorized10FunctionIf22_execute_impl_internalEPNS_15FunctionContextERNS0_5BlockERKSt6vectorIjSaIjEEjmENKUlRKT_E_clINS0_16DispatchDataTypeILNS_13PrimitiveTypeE3EEEEEbSD_
Unexecuted instantiation: _ZZNK5doris10vectorized10FunctionIf22_execute_impl_internalEPNS_15FunctionContextERNS0_5BlockERKSt6vectorIjSaIjEEjmENKUlRKT_E_clINS0_16DispatchDataTypeILNS_13PrimitiveTypeE4EEEEEbSD_
_ZZNK5doris10vectorized10FunctionIf22_execute_impl_internalEPNS_15FunctionContextERNS0_5BlockERKSt6vectorIjSaIjEEjmENKUlRKT_E_clINS0_16DispatchDataTypeILNS_13PrimitiveTypeE5EEEEEbSD_
Line
Count
Source
531
2
        auto call = [&](const auto& type) -> bool {
532
2
            using DataType = std::decay_t<decltype(type)>;
533
2
            vec_exec = execute_basic_type<DataType::PType>(block, cond_col, arg_then, arg_else,
534
2
                                                           result, vec_exec);
535
2
            return true;
536
2
        };
Unexecuted instantiation: _ZZNK5doris10vectorized10FunctionIf22_execute_impl_internalEPNS_15FunctionContextERNS0_5BlockERKSt6vectorIjSaIjEEjmENKUlRKT_E_clINS0_16DispatchDataTypeILNS_13PrimitiveTypeE6EEEEEbSD_
Unexecuted instantiation: _ZZNK5doris10vectorized10FunctionIf22_execute_impl_internalEPNS_15FunctionContextERNS0_5BlockERKSt6vectorIjSaIjEEjmENKUlRKT_E_clINS0_16DispatchDataTypeILNS_13PrimitiveTypeE7EEEEEbSD_
Unexecuted instantiation: _ZZNK5doris10vectorized10FunctionIf22_execute_impl_internalEPNS_15FunctionContextERNS0_5BlockERKSt6vectorIjSaIjEEjmENKUlRKT_E_clINS0_16DispatchDataTypeILNS_13PrimitiveTypeE8EEEEEbSD_
_ZZNK5doris10vectorized10FunctionIf22_execute_impl_internalEPNS_15FunctionContextERNS0_5BlockERKSt6vectorIjSaIjEEjmENKUlRKT_E_clINS0_16DispatchDataTypeILNS_13PrimitiveTypeE9EEEEEbSD_
Line
Count
Source
531
1
        auto call = [&](const auto& type) -> bool {
532
1
            using DataType = std::decay_t<decltype(type)>;
533
1
            vec_exec = execute_basic_type<DataType::PType>(block, cond_col, arg_then, arg_else,
534
1
                                                           result, vec_exec);
535
1
            return true;
536
1
        };
Unexecuted instantiation: _ZZNK5doris10vectorized10FunctionIf22_execute_impl_internalEPNS_15FunctionContextERNS0_5BlockERKSt6vectorIjSaIjEEjmENKUlRKT_E_clINS0_16DispatchDataTypeILNS_13PrimitiveTypeE28EEEEEbSD_
Unexecuted instantiation: _ZZNK5doris10vectorized10FunctionIf22_execute_impl_internalEPNS_15FunctionContextERNS0_5BlockERKSt6vectorIjSaIjEEjmENKUlRKT_E_clINS0_16DispatchDataTypeILNS_13PrimitiveTypeE29EEEEEbSD_
Unexecuted instantiation: _ZZNK5doris10vectorized10FunctionIf22_execute_impl_internalEPNS_15FunctionContextERNS0_5BlockERKSt6vectorIjSaIjEEjmENKUlRKT_E_clINS0_16DispatchDataTypeILNS_13PrimitiveTypeE20EEEEEbSD_
Unexecuted instantiation: _ZZNK5doris10vectorized10FunctionIf22_execute_impl_internalEPNS_15FunctionContextERNS0_5BlockERKSt6vectorIjSaIjEEjmENKUlRKT_E_clINS0_16DispatchDataTypeILNS_13PrimitiveTypeE30EEEEEbSD_
Unexecuted instantiation: _ZZNK5doris10vectorized10FunctionIf22_execute_impl_internalEPNS_15FunctionContextERNS0_5BlockERKSt6vectorIjSaIjEEjmENKUlRKT_E_clINS0_16DispatchDataTypeILNS_13PrimitiveTypeE35EEEEEbSD_
Unexecuted instantiation: _ZZNK5doris10vectorized10FunctionIf22_execute_impl_internalEPNS_15FunctionContextERNS0_5BlockERKSt6vectorIjSaIjEEjmENKUlRKT_E_clINS0_16DispatchDataTypeILNS_13PrimitiveTypeE11EEEEEbSD_
Unexecuted instantiation: _ZZNK5doris10vectorized10FunctionIf22_execute_impl_internalEPNS_15FunctionContextERNS0_5BlockERKSt6vectorIjSaIjEEjmENKUlRKT_E_clINS0_16DispatchDataTypeILNS_13PrimitiveTypeE25EEEEEbSD_
Unexecuted instantiation: _ZZNK5doris10vectorized10FunctionIf22_execute_impl_internalEPNS_15FunctionContextERNS0_5BlockERKSt6vectorIjSaIjEEjmENKUlRKT_E_clINS0_16DispatchDataTypeILNS_13PrimitiveTypeE26EEEEEbSD_
_ZZNK5doris10vectorized10FunctionIf22_execute_impl_internalEPNS_15FunctionContextERNS0_5BlockERKSt6vectorIjSaIjEEjmENKUlRKT_E_clINS0_16DispatchDataTypeILNS_13PrimitiveTypeE12EEEEEbSD_
Line
Count
Source
531
1
        auto call = [&](const auto& type) -> bool {
532
1
            using DataType = std::decay_t<decltype(type)>;
533
1
            vec_exec = execute_basic_type<DataType::PType>(block, cond_col, arg_then, arg_else,
534
1
                                                           result, vec_exec);
535
1
            return true;
536
1
        };
Unexecuted instantiation: _ZZNK5doris10vectorized10FunctionIf22_execute_impl_internalEPNS_15FunctionContextERNS0_5BlockERKSt6vectorIjSaIjEEjmENKUlRKT_E_clINS0_16DispatchDataTypeILNS_13PrimitiveTypeE27EEEEEbSD_
Unexecuted instantiation: _ZZNK5doris10vectorized10FunctionIf22_execute_impl_internalEPNS_15FunctionContextERNS0_5BlockERKSt6vectorIjSaIjEEjmENKUlRKT_E_clINS0_16DispatchDataTypeILNS_13PrimitiveTypeE36EEEEEbSD_
Unexecuted instantiation: _ZZNK5doris10vectorized10FunctionIf22_execute_impl_internalEPNS_15FunctionContextERNS0_5BlockERKSt6vectorIjSaIjEEjmENKUlRKT_E_clINS0_16DispatchDataTypeILNS_13PrimitiveTypeE37EEEEEbSD_
537
538
10
        auto can_use_vec_exec = dispatch_switch_scalar(arg_then.type->get_primitive_type(), call);
539
10
        if (can_use_vec_exec) {
540
9
            return vec_exec;
541
9
        } else {
542
1
            return execute_generic(block, cond_col, arg_then, arg_else, result, input_rows_count);
543
1
        }
544
10
    }
545
};
546
547
1
void register_function_if(SimpleFunctionFactory& factory) {
548
1
    factory.register_function<FunctionIf>();
549
1
}
550
551
} // namespace doris::vectorized