Coverage Report

Created: 2026-03-12 17:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/predicate/null_predicate.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 "storage/predicate/null_predicate.h"
19
20
#include <string.h>
21
22
#include <roaring/roaring.hh>
23
24
#include "core/column/column.h"
25
#include "core/column/column_nullable.h"
26
#include "core/value/vdatetime_value.h"
27
#include "storage/index/inverted/inverted_index_cache.h"
28
#include "storage/index/inverted/inverted_index_reader.h"
29
30
using namespace doris;
31
32
namespace doris {
33
34
NullPredicate::NullPredicate(uint32_t column_id, std::string col_name, bool is_null,
35
                             PrimitiveType type, bool opposite)
36
4.97k
        : ColumnPredicate(column_id, col_name, type), _is_null(opposite != is_null) {}
37
38
8.54k
PredicateType NullPredicate::type() const {
39
8.54k
    return _is_null ? PredicateType::IS_NULL : PredicateType::IS_NOT_NULL;
40
8.54k
}
41
42
Status NullPredicate::evaluate(const IndexFieldNameAndTypePair& name_with_type,
43
                               IndexIterator* iterator, uint32_t num_rows,
44
128
                               roaring::Roaring* bitmap) const {
45
131
    if (iterator->has_null()) {
46
131
        InvertedIndexQueryCacheHandle null_bitmap_cache_handle;
47
131
        RETURN_IF_ERROR(iterator->read_null_bitmap(&null_bitmap_cache_handle));
48
131
        std::shared_ptr<roaring::Roaring> null_bitmap = null_bitmap_cache_handle.get_bitmap();
49
50
131
        if (null_bitmap) {
51
129
            if (_is_null) {
52
51
                *bitmap &= *null_bitmap; // Keep only nulls in bitmap if _is_null is true
53
78
            } else {
54
78
                *bitmap -= *null_bitmap; // Remove nulls from bitmap if _is_null is false
55
78
            }
56
129
        } else if (_is_null) {
57
0
            *bitmap = roaring::Roaring(); // Reset bitmap to an empty bitmap
58
0
        }
59
18.4E
    } else if (_is_null) {
60
0
        *bitmap = roaring::Roaring(); // Reset bitmap to an empty bitmap
61
0
    }
62
63
128
    return Status::OK();
64
128
}
65
66
2.37k
uint16_t NullPredicate::_evaluate_inner(const IColumn& column, uint16_t* sel, uint16_t size) const {
67
2.37k
    uint16_t new_size = 0;
68
2.37k
    if (auto* nullable = check_and_get_column<ColumnNullable>(column)) {
69
2.15k
        if (!nullable->has_null()) {
70
931
            return _is_null ? 0 : size;
71
931
        }
72
1.22k
        auto& pred_col = nullable->get_null_map_data();
73
1.22k
        constexpr bool is_nullable = true;
74
272k
#define EVALUATE_WITH_NULL_IMPL(IDX) pred_col[IDX] == _is_null
75
1.22k
#define EVALUATE_WITHOUT_NULL_IMPL(IDX) true
76
272k
        EVALUATE_BY_SELECTOR(EVALUATE_WITH_NULL_IMPL, EVALUATE_WITHOUT_NULL_IMPL)
77
1.22k
#undef EVALUATE_WITH_NULL_IMPL
78
1.22k
#undef EVALUATE_WITHOUT_NULL_IMPL
79
1.22k
        return new_size;
80
2.15k
    } else {
81
218
        if (_is_null) return 0;
82
218
    }
83
76
    return size;
84
2.37k
}
85
86
void NullPredicate::evaluate_or(const IColumn& column, const uint16_t* sel, uint16_t size,
87
21
                                bool* flags) const {
88
21
    if (auto* nullable = check_and_get_column<ColumnNullable>(column)) {
89
21
        if (!nullable->has_null()) {
90
21
            if (!_is_null) {
91
11
                memset(flags, true, size);
92
11
            }
93
21
        } else {
94
0
            auto& null_map = nullable->get_null_map_data();
95
0
            for (uint16_t i = 0; i < size; ++i) {
96
0
                if (flags[i]) continue;
97
0
                uint16_t idx = sel[i];
98
0
                flags[i] |= (null_map[idx] == _is_null);
99
0
            }
100
0
        }
101
21
    } else {
102
0
        if (!_is_null) memset(flags, true, size);
103
0
    }
104
21
}
105
106
void NullPredicate::evaluate_and(const IColumn& column, const uint16_t* sel, uint16_t size,
107
0
                                 bool* flags) const {
108
0
    if (auto* nullable = check_and_get_column<ColumnNullable>(column)) {
109
0
        if (!nullable->has_null()) {
110
0
            if (_is_null) {
111
0
                memset(flags, false, size);
112
0
            }
113
0
        } else {
114
0
            auto& null_map = nullable->get_null_map_data();
115
0
            for (uint16_t i = 0; i < size; ++i) {
116
0
                if (flags[i]) continue;
117
0
                uint16_t idx = sel[i];
118
0
                flags[i] &= (null_map[idx] == _is_null);
119
0
            }
120
0
        }
121
0
    } else {
122
0
        if (_is_null) memset(flags, false, size);
123
0
    }
124
0
}
125
126
0
void NullPredicate::evaluate_vec(const IColumn& column, uint16_t size, bool* flags) const {
127
0
    if (auto* nullable = check_and_get_column<ColumnNullable>(column)) {
128
0
        if (!nullable->has_null()) {
129
0
            memset(flags, !_is_null, size);
130
0
        }
131
0
        auto& null_map = nullable->get_null_map_data();
132
0
        for (uint16_t i = 0; i < size; ++i) {
133
0
            flags[i] = (null_map[i] == _is_null);
134
0
        }
135
0
    } else {
136
0
        if (_is_null) memset(flags, false, size);
137
0
    }
138
0
}
139
140
} //namespace doris