Coverage Report

Created: 2026-03-14 13:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/function/function_nullables.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
18
#include "common/status.h"
19
#include "core/block/block.h"
20
#include "core/block/column_numbers.h"
21
#include "core/block/column_with_type_and_name.h"
22
#include "core/column/column.h"
23
#include "core/column/column_nullable.h"
24
#include "core/data_type/data_type.h"
25
#include "core/data_type/data_type_nullable.h"
26
#include "core/types.h"
27
#include "exprs/function/function.h"
28
#include "exprs/function/simple_function_factory.h"
29
30
namespace doris {
31
class FunctionContext;
32
} // namespace doris
33
34
namespace doris {
35
36
class FunctionNullable : public IFunction {
37
public:
38
    static constexpr auto name = "nullable";
39
40
5.03k
    static FunctionPtr create() { return std::make_shared<FunctionNullable>(); }
41
42
1
    String get_name() const override { return name; }
43
44
5.02k
    size_t get_number_of_arguments() const override { return 1; }
45
46
5.02k
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
47
5.02k
        return make_nullable(arguments[0]);
48
5.02k
    }
49
50
10.3k
    bool use_default_implementation_for_nulls() const override { return false; }
51
52
    // trans nullable column to non-nullable column. If argument is already non-nullable, raise error.
53
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
54
5.31k
                        uint32_t result, size_t input_rows_count) const override {
55
5.31k
        ColumnPtr& col = block.get_by_position(arguments[0]).column;
56
5.31k
        if (const auto* col_null = check_and_get_column<ColumnNullable>(col.get());
57
5.31k
            col_null == nullptr) {
58
            // not null
59
1.27k
            block.replace_by_position(
60
1.27k
                    result, ColumnNullable::create(col, ColumnBool::create(input_rows_count, 0)));
61
4.03k
        } else { // column is ColumnNullable
62
4.03k
            block.replace_by_position(result, col->clone_resized(input_rows_count));
63
4.03k
        }
64
5.31k
        return Status::OK();
65
5.31k
    }
66
};
67
68
class FunctionNonNullable : public IFunction {
69
public:
70
    static constexpr auto name = "non_nullable";
71
72
101
    static FunctionPtr create() { return std::make_shared<FunctionNonNullable>(); }
73
74
1
    String get_name() const override { return name; }
75
76
92
    size_t get_number_of_arguments() const override { return 1; }
77
78
92
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
79
92
        return remove_nullable(arguments[0]);
80
92
    }
81
82
276
    bool use_default_implementation_for_nulls() const override { return false; }
83
84
    // trans nullable column to non-nullable column. If argument is already non-nullable, raise error.
85
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
86
184
                        uint32_t result, size_t input_rows_count) const override {
87
184
        auto& data = block.get_by_position(arguments[0]);
88
184
        if (const auto* col_null = check_and_get_column<ColumnNullable>(data.column.get());
89
184
            col_null != nullptr) {
90
182
            if (col_null->has_null()) [[unlikely]] {
91
2
                return Status::InvalidArgument(
92
2
                        "There's NULL value in column {} which is illegal for non_nullable",
93
2
                        data.name);
94
2
            }
95
180
            const ColumnPtr& nest_col = col_null->get_nested_column_ptr();
96
180
            block.replace_by_position(result, nest_col->clone_resized(nest_col->size()));
97
180
        } else {
98
2
            block.replace_by_position(result, data.column->clone_resized(input_rows_count));
99
2
        }
100
182
        return Status::OK();
101
184
    }
102
};
103
104
8
void register_function_nullables(SimpleFunctionFactory& factory) {
105
8
    factory.register_function<FunctionNullable>();
106
8
    factory.register_function<FunctionNonNullable>();
107
8
}
108
109
} // namespace doris