Coverage Report

Created: 2025-12-04 16:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/olap/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 "olap/null_predicate.h"
19
20
#include <string.h>
21
22
#include <roaring/roaring.hh>
23
24
#include "olap/rowset/segment_v2/inverted_index_cache.h"
25
#include "olap/rowset/segment_v2/inverted_index_reader.h"
26
#include "vec/columns/column.h"
27
#include "vec/columns/column_nullable.h"
28
#include "vec/runtime/vdatetime_value.h"
29
30
using namespace doris::vectorized;
31
32
namespace doris {
33
34
NullPredicate::NullPredicate(uint32_t column_id, bool is_null, bool opposite)
35
38
        : ColumnPredicate(column_id), _is_null(opposite != is_null) {}
36
37
24
PredicateType NullPredicate::type() const {
38
24
    return _is_null ? PredicateType::IS_NULL : PredicateType::IS_NOT_NULL;
39
24
}
40
41
Status NullPredicate::evaluate(const vectorized::IndexFieldNameAndTypePair& name_with_type,
42
                               IndexIterator* iterator, uint32_t num_rows,
43
0
                               roaring::Roaring* bitmap) const {
44
0
    if (iterator->has_null()) {
45
0
        InvertedIndexQueryCacheHandle null_bitmap_cache_handle;
46
0
        RETURN_IF_ERROR(iterator->read_null_bitmap(&null_bitmap_cache_handle));
47
0
        std::shared_ptr<roaring::Roaring> null_bitmap = null_bitmap_cache_handle.get_bitmap();
48
49
0
        if (null_bitmap) {
50
0
            if (_is_null) {
51
0
                *bitmap &= *null_bitmap; // Keep only nulls in bitmap if _is_null is true
52
0
            } else {
53
0
                *bitmap -= *null_bitmap; // Remove nulls from bitmap if _is_null is false
54
0
            }
55
0
        } else if (_is_null) {
56
0
            *bitmap = roaring::Roaring(); // Reset bitmap to an empty bitmap
57
0
        }
58
0
    } else if (_is_null) {
59
0
        *bitmap = roaring::Roaring(); // Reset bitmap to an empty bitmap
60
0
    }
61
62
0
    return Status::OK();
63
0
}
64
65
uint16_t NullPredicate::_evaluate_inner(const vectorized::IColumn& column, uint16_t* sel,
66
12
                                        uint16_t size) const {
67
12
    uint16_t new_size = 0;
68
12
    if (auto* nullable = check_and_get_column<ColumnNullable>(column)) {
69
12
        if (!nullable->has_null()) {
70
12
            return _is_null ? 0 : size;
71
12
        }
72
0
        auto& pred_col = nullable->get_null_map_data();
73
0
        constexpr bool is_nullable = true;
74
0
#define EVALUATE_WITH_NULL_IMPL(IDX) pred_col[IDX] == _is_null
75
0
#define EVALUATE_WITHOUT_NULL_IMPL(IDX) true
76
0
        EVALUATE_BY_SELECTOR(EVALUATE_WITH_NULL_IMPL, EVALUATE_WITHOUT_NULL_IMPL)
77
0
#undef EVALUATE_WITH_NULL_IMPL
78
0
#undef EVALUATE_WITHOUT_NULL_IMPL
79
0
        return new_size;
80
12
    } else {
81
0
        if (_is_null) return 0;
82
0
    }
83
0
    return size;
84
12
}
85
86
void NullPredicate::evaluate_or(const IColumn& column, const uint16_t* sel, uint16_t size,
87
0
                                bool* flags) const {
88
0
    if (auto* nullable = check_and_get_column<ColumnNullable>(column)) {
89
0
        if (!nullable->has_null()) {
90
0
            if (!_is_null) {
91
0
                memset(flags, true, size);
92
0
            }
93
0
        } 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
0
    } else {
102
0
        if (!_is_null) memset(flags, true, size);
103
0
    }
104
0
}
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
void NullPredicate::evaluate_vec(const vectorized::IColumn& column, uint16_t size,
127
0
                                 bool* flags) const {
128
0
    if (auto* nullable = check_and_get_column<ColumnNullable>(column)) {
129
0
        if (!nullable->has_null()) {
130
0
            memset(flags, !_is_null, size);
131
0
        }
132
0
        auto& null_map = nullable->get_null_map_data();
133
0
        for (uint16_t i = 0; i < size; ++i) {
134
0
            flags[i] = (null_map[i] == _is_null);
135
0
        }
136
0
    } else {
137
0
        if (_is_null) memset(flags, false, size);
138
0
    }
139
0
}
140
141
} //namespace doris