Coverage Report

Created: 2026-10-16 08:30

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