Coverage Report

Created: 2025-06-20 14:56

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