Coverage Report

Created: 2026-03-12 16:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/function/is_not_null.h
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/IsNotNull.cpp
19
// and modified by Doris
20
21
#include <stddef.h>
22
23
#include <algorithm>
24
#include <boost/iterator/iterator_facade.hpp>
25
#include <memory>
26
#include <string>
27
#include <utility>
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/column/column.h"
35
#include "core/column/column_nullable.h"
36
#include "core/column/column_vector.h"
37
#include "core/data_type/data_type_number.h"
38
#include "core/field.h"
39
#include "exprs/aggregate/aggregate_function.h"
40
#include "exprs/function/function.h"
41
42
namespace doris {
43
class FunctionContext;
44
} // namespace doris
45
46
namespace doris {
47
48
/// Implements the function isNotNull which returns true if a value
49
/// is not null, false otherwise.
50
class FunctionIsNotNull : public IFunction {
51
public:
52
    static constexpr auto name = "is_not_null_pred";
53
54
1.11k
    static FunctionPtr create() { return std::make_shared<FunctionIsNotNull>(); }
55
56
8
    std::string get_name() const override { return name; }
57
58
1.10k
    size_t get_number_of_arguments() const override { return 1; }
59
7.64k
    bool use_default_implementation_for_nulls() const override { return false; }
60
1.10k
    DataTypePtr get_return_type_impl(const DataTypes&) const override {
61
1.10k
        return std::make_shared<DataTypeUInt8>();
62
1.10k
    }
63
64
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
65
6.53k
                        uint32_t result, size_t input_rows_count) const override {
66
6.53k
        const ColumnWithTypeAndName& elem = block.get_by_position(arguments[0]);
67
6.53k
        if (auto* nullable = check_and_get_column<ColumnNullable>(*elem.column)) {
68
            /// Return the negated null map.
69
6.37k
            auto res_column = ColumnUInt8::create(input_rows_count);
70
6.37k
            const auto* __restrict src_data = nullable->get_null_map_data().data();
71
6.37k
            auto* __restrict res_data = assert_cast<ColumnUInt8&>(*res_column).get_data().data();
72
73
20.3M
            for (size_t i = 0; i < input_rows_count; ++i) {
74
20.3M
                res_data[i] = !src_data[i];
75
20.3M
            }
76
77
6.37k
            block.replace_by_position(result, std::move(res_column));
78
6.37k
        } else {
79
            /// Since no element is nullable, return a constant one.
80
160
            block.get_by_position(result).column = DataTypeUInt8().create_column_const(
81
160
                    elem.column->size(), Field::create_field<TYPE_BOOLEAN>(1));
82
160
        }
83
6.53k
        return Status::OK();
84
6.53k
    }
85
86
    Status evaluate_inverted_index(
87
            const ColumnsWithTypeAndName& arguments,
88
            const std::vector<IndexFieldNameAndTypePair>& data_type_with_names,
89
            std::vector<segment_v2::IndexIterator*> iterators, uint32_t num_rows,
90
            const InvertedIndexAnalyzerCtx* /*analyzer_ctx*/,
91
12
            segment_v2::InvertedIndexResultBitmap& bitmap_result) const override {
92
12
        if (iterators.empty() || iterators[0] == nullptr) {
93
2
            return Status::OK();
94
2
        }
95
10
        auto* index_iter = iterators[0];
96
10
        if (index_iter->has_null()) {
97
10
            segment_v2::InvertedIndexQueryCacheHandle null_bitmap_cache_handle;
98
10
            RETURN_IF_ERROR(index_iter->read_null_bitmap(&null_bitmap_cache_handle));
99
10
            std::shared_ptr<roaring::Roaring> null_bitmap = null_bitmap_cache_handle.get_bitmap();
100
            // only inverted index has null bitmap, so we can calculate
101
10
            if (null_bitmap) {
102
10
                std::shared_ptr<roaring::Roaring> data_bitmap =
103
10
                        std::make_shared<roaring::Roaring>();
104
10
                data_bitmap->addRange(0, num_rows);
105
106
                // null_bitmap is null bitmap
107
10
                bitmap_result = segment_v2::InvertedIndexResultBitmap(data_bitmap, null_bitmap);
108
109
                // need to mask out null bitmap
110
10
                bitmap_result.mask_out_null();
111
10
            }
112
10
        }
113
10
        return Status::OK();
114
10
    }
115
};
116
117
} // namespace doris