Coverage Report

Created: 2026-06-13 01:53

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/expr_zonemap_filter.h"
41
#include "exprs/function/function.h"
42
#include "exprs/vslot_ref.h"
43
44
namespace doris {
45
class FunctionContext;
46
} // namespace doris
47
48
namespace doris {
49
50
/// Implements the function isNotNull which returns true if a value
51
/// is not null, false otherwise.
52
class FunctionIsNotNull : public IFunction {
53
public:
54
    static constexpr auto name = "is_not_null_pred";
55
56
292
    static FunctionPtr create() { return std::make_shared<FunctionIsNotNull>(); }
57
58
1
    std::string get_name() const override { return name; }
59
60
284
    size_t get_number_of_arguments() const override { return 1; }
61
1.18k
    bool use_default_implementation_for_nulls() const override { return false; }
62
284
    DataTypePtr get_return_type_impl(const DataTypes&) const override {
63
284
        return std::make_shared<DataTypeUInt8>();
64
284
    }
65
66
    ZoneMapFilterResult evaluate_zonemap_filter(const ZoneMapEvalContext& ctx,
67
0
                                                const VExprSPtrs& arguments) const override {
68
0
        return expr_zonemap::eval_null_zonemap(ctx, arguments, false);
69
0
    }
70
71
0
    bool can_evaluate_zonemap_filter(const VExprSPtrs& arguments) const override {
72
0
        return std::dynamic_pointer_cast<VSlotRef>(arguments[0]) != nullptr;
73
0
    }
74
75
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
76
904
                        uint32_t result, size_t input_rows_count) const override {
77
904
        const ColumnWithTypeAndName& elem = block.get_by_position(arguments[0]);
78
904
        if (auto* nullable = check_and_get_column<ColumnNullable>(*elem.column)) {
79
            /// Return the negated null map.
80
904
            auto res_column = ColumnUInt8::create(input_rows_count);
81
904
            const auto* __restrict src_data = nullable->get_null_map_data().data();
82
904
            auto* __restrict res_data = res_column->get_data().data();
83
84
4.58k
            for (size_t i = 0; i < input_rows_count; ++i) {
85
3.67k
                res_data[i] = !src_data[i];
86
3.67k
            }
87
88
904
            block.replace_by_position(result, std::move(res_column));
89
904
        } else {
90
            /// Since no element is nullable, return a constant one.
91
0
            block.get_by_position(result).column = DataTypeUInt8().create_column_const(
92
0
                    elem.column->size(), Field::create_field<TYPE_BOOLEAN>(1));
93
0
        }
94
904
        return Status::OK();
95
904
    }
96
97
    Status evaluate_inverted_index(
98
            const ColumnsWithTypeAndName& arguments,
99
            const std::vector<IndexFieldNameAndTypePair>& data_type_with_names,
100
            std::vector<segment_v2::IndexIterator*> iterators, uint32_t num_rows,
101
            const InvertedIndexAnalyzerCtx* /*analyzer_ctx*/,
102
2
            segment_v2::InvertedIndexResultBitmap& bitmap_result) const override {
103
2
        if (iterators.empty() || iterators[0] == nullptr) {
104
2
            return Status::OK();
105
2
        }
106
0
        auto* index_iter = iterators[0];
107
0
        if (index_iter->has_null()) {
108
0
            segment_v2::InvertedIndexQueryCacheHandle null_bitmap_cache_handle;
109
0
            RETURN_IF_ERROR(index_iter->read_null_bitmap(&null_bitmap_cache_handle));
110
0
            std::shared_ptr<roaring::Roaring> null_bitmap = null_bitmap_cache_handle.get_bitmap();
111
            // only inverted index has null bitmap, so we can calculate
112
0
            if (null_bitmap) {
113
0
                std::shared_ptr<roaring::Roaring> data_bitmap =
114
0
                        std::make_shared<roaring::Roaring>();
115
0
                data_bitmap->addRange(0, num_rows);
116
117
                // null_bitmap is null bitmap
118
0
                bitmap_result = segment_v2::InvertedIndexResultBitmap(data_bitmap, null_bitmap);
119
120
                // need to mask out null bitmap
121
0
                bitmap_result.mask_out_null();
122
0
            }
123
0
        }
124
0
        return Status::OK();
125
0
    }
126
};
127
128
} // namespace doris