Coverage Report

Created: 2026-03-16 14:53

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
2
    static FunctionPtr create() { return std::make_shared<FunctionNullable>(); }
41
42
1
    String get_name() const override { return name; }
43
44
0
    size_t get_number_of_arguments() const override { return 1; }
45
46
0
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
47
0
        return make_nullable(arguments[0]);
48
0
    }
49
50
0
    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
0
                        uint32_t result, size_t input_rows_count) const override {
55
0
        ColumnPtr& col = block.get_by_position(arguments[0]).column;
56
0
        if (const auto* col_null = check_and_get_column<ColumnNullable>(col.get());
57
0
            col_null == nullptr) {
58
            // not null
59
0
            block.replace_by_position(
60
0
                    result, ColumnNullable::create(col, ColumnBool::create(input_rows_count, 0)));
61
0
        } else { // column is ColumnNullable
62
0
            block.replace_by_position(result, col->clone_resized(input_rows_count));
63
0
        }
64
0
        return Status::OK();
65
0
    }
66
};
67
68
class FunctionNonNullable : public IFunction {
69
public:
70
    static constexpr auto name = "non_nullable";
71
72
2
    static FunctionPtr create() { return std::make_shared<FunctionNonNullable>(); }
73
74
1
    String get_name() const override { return name; }
75
76
0
    size_t get_number_of_arguments() const override { return 1; }
77
78
0
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
79
0
        return remove_nullable(arguments[0]);
80
0
    }
81
82
0
    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
0
                        uint32_t result, size_t input_rows_count) const override {
87
0
        auto& data = block.get_by_position(arguments[0]);
88
0
        if (const auto* col_null = check_and_get_column<ColumnNullable>(data.column.get());
89
0
            col_null != nullptr) {
90
0
            if (col_null->has_null()) [[unlikely]] {
91
0
                return Status::InvalidArgument(
92
0
                        "There's NULL value in column {} which is illegal for non_nullable",
93
0
                        data.name);
94
0
            }
95
0
            const ColumnPtr& nest_col = col_null->get_nested_column_ptr();
96
0
            block.replace_by_position(result, nest_col->clone_resized(nest_col->size()));
97
0
        } else {
98
0
            block.replace_by_position(result, data.column->clone_resized(input_rows_count));
99
0
        }
100
0
        return Status::OK();
101
0
    }
102
};
103
104
1
void register_function_nullables(SimpleFunctionFactory& factory) {
105
1
    factory.register_function<FunctionNullable>();
106
1
    factory.register_function<FunctionNonNullable>();
107
1
}
108
109
} // namespace doris