Coverage Report

Created: 2025-12-30 18:31

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