Coverage Report

Created: 2026-07-28 17:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/vbloom_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 "exprs/vbloom_predicate.h"
19
20
#include <cstddef>
21
#include <utility>
22
23
#include "common/status.h"
24
#include "core/block/block.h"
25
#include "core/block/column_numbers.h"
26
#include "core/block/column_with_type_and_name.h"
27
#include "core/column/column.h"
28
#include "core/column/column_nullable.h"
29
#include "core/column/column_vector.h"
30
#include "core/data_type/data_type.h"
31
#include "core/data_type/data_type_nullable.h"
32
#include "core/types.h"
33
#include "exprs/bloom_filter_func.h"
34
#include "exprs/expr_zonemap_filter.h"
35
#include "exprs/vslot_ref.h"
36
#include "runtime/runtime_state.h"
37
38
namespace doris {
39
class RowDescriptor;
40
class TExprNode;
41
42
} // namespace doris
43
44
namespace doris {
45
46
class VExprContext;
47
48
3.43k
VBloomPredicate::VBloomPredicate(const TExprNode& node) : VExpr(node), _filter(nullptr) {}
49
50
Status VBloomPredicate::prepare(RuntimeState* state, const RowDescriptor& desc,
51
3.41k
                                VExprContext* context) {
52
3.41k
    RETURN_IF_ERROR_OR_PREPARED(VExpr::prepare(state, desc, context));
53
54
3.41k
    if (_children.size() != 1) {
55
0
        return Status::InternalError("Invalid argument for VBloomPredicate.");
56
0
    }
57
58
3.41k
    _prepare_finished = true;
59
3.41k
    return Status::OK();
60
3.41k
}
61
62
Status VBloomPredicate::open(RuntimeState* state, VExprContext* context,
63
4.04k
                             FunctionContext::FunctionStateScope scope) {
64
4.04k
    DCHECK(_prepare_finished);
65
4.04k
    RETURN_IF_ERROR(VExpr::open(state, context, scope));
66
4.04k
    _open_finished = true;
67
4.04k
    return Status::OK();
68
4.04k
}
69
70
4.07k
void VBloomPredicate::close(VExprContext* context, FunctionContext::FunctionStateScope scope) {
71
4.07k
    VExpr::close(context, scope);
72
4.07k
}
73
74
Status VBloomPredicate::_do_execute(VExprContext* context, const Block* block,
75
                                    const uint8_t* __restrict filter, const Selector* selector,
76
746
                                    size_t count, ColumnPtr& result_column) const {
77
746
    DCHECK(_open_finished || block == nullptr);
78
746
    DCHECK(!(filter != nullptr && selector != nullptr))
79
0
            << "filter and selector can not be both set";
80
746
    DCHECK_EQ(_children.size(), 1);
81
82
746
    ColumnPtr argument_column;
83
746
    RETURN_IF_ERROR(_children[0]->execute_column(context, block, selector, count, argument_column));
84
746
    argument_column = argument_column->convert_to_full_column_if_const();
85
86
746
    size_t sz = argument_column->size();
87
746
    auto res_data_column = ColumnUInt8::create(sz);
88
89
746
    res_data_column->resize(sz);
90
746
    auto* ptr = ((ColumnUInt8*)res_data_column.get())->get_data().data();
91
92
746
    _filter->find_fixed_len(argument_column, ptr, filter);
93
94
746
    result_column = std::move(res_data_column);
95
746
    DCHECK_EQ(result_column->size(), count);
96
746
    return Status::OK();
97
746
}
98
99
Status VBloomPredicate::execute_column_impl(VExprContext* context, const Block* block,
100
                                            const Selector* selector, size_t count,
101
166
                                            ColumnPtr& result_column) const {
102
166
    return _do_execute(context, block, nullptr, selector, count, result_column);
103
166
}
104
105
Status VBloomPredicate::execute_runtime_filter(VExprContext* context, const Block* block,
106
                                               const uint8_t* __restrict filter, size_t count,
107
                                               ColumnPtr& result_column,
108
580
                                               ColumnPtr* arg_column) const {
109
580
    return _do_execute(context, block, filter, nullptr, count, result_column);
110
580
}
111
112
namespace {
113
114
57
bool bloom_filter_type_matches(PrimitiveType filter_type, const DataTypePtr& data_type) {
115
57
    if (data_type == nullptr) {
116
0
        return false;
117
0
    }
118
57
    const auto value_type = remove_nullable(data_type)->get_primitive_type();
119
57
    return filter_type == value_type || (is_string_type(filter_type) && is_string_type(value_type));
120
57
}
121
122
} // namespace
123
124
bool VBloomPredicate::can_execute_on_raw_fixed_values(const DataTypePtr& data_type,
125
12
                                                      int column_id) const {
126
12
    if (_filter == nullptr || !_filter->supports_raw_fixed_values() || _children.size() != 1) {
127
1
        return false;
128
1
    }
129
11
    const auto slot = std::dynamic_pointer_cast<VSlotRef>(_children[0]);
130
11
    return slot != nullptr && slot->column_id() == column_id &&
131
11
           bloom_filter_type_matches(_filter->primitive_type(), slot->data_type()) &&
132
11
           bloom_filter_type_matches(_filter->primitive_type(), data_type);
133
12
}
134
135
Status VBloomPredicate::execute_on_raw_fixed_values(const uint8_t* values, size_t num_values,
136
                                                    size_t value_width,
137
                                                    const DataTypePtr& data_type, int column_id,
138
3
                                                    uint8_t* matches) const {
139
3
    if (!can_execute_on_raw_fixed_values(data_type, column_id)) {
140
0
        return Status::NotSupported("Bloom predicate cannot evaluate raw fixed-width values");
141
0
    }
142
    // Hash physical values inside BloomFilterFunc<T>; reconstructing an untyped hash here could
143
    // disagree with the build-side hash for dates, decimals, and other fixed-width wrappers.
144
3
    return _filter->find_batch_raw_fixed(values, num_values, value_width, matches);
145
3
}
146
147
ZoneMapFilterResult VBloomPredicate::evaluate_dictionary_filter(
148
7
        const DictionaryEvalContext& ctx) const {
149
7
    if (!can_evaluate_dictionary_filter()) {
150
0
        return ZoneMapFilterResult::kUnsupported;
151
0
    }
152
7
    const auto slot = std::dynamic_pointer_cast<VSlotRef>(_children[0]);
153
7
    DORIS_CHECK(slot != nullptr);
154
7
    const auto* dictionary = ctx.slot(slot->column_id());
155
7
    if (dictionary == nullptr ||
156
7
        !bloom_filter_type_matches(_filter->primitive_type(), dictionary->data_type)) {
157
0
        return ZoneMapFilterResult::kUnsupported;
158
0
    }
159
8
    for (const auto& value : dictionary->values) {
160
8
        if (_filter->test_field(value)) {
161
4
            return ZoneMapFilterResult::kMayMatch;
162
4
        }
163
8
    }
164
3
    return ZoneMapFilterResult::kNoMatch;
165
7
}
166
167
28
bool VBloomPredicate::can_evaluate_dictionary_filter() const {
168
28
    if (_filter == nullptr || _children.size() != 1) {
169
0
        return false;
170
0
    }
171
28
    const auto slot = std::dynamic_pointer_cast<VSlotRef>(_children[0]);
172
28
    return slot != nullptr &&
173
28
           bloom_filter_type_matches(_filter->primitive_type(), slot->data_type());
174
28
}
175
176
3.39k
const std::string& VBloomPredicate::expr_name() const {
177
3.39k
    return EXPR_NAME;
178
3.39k
}
179
180
3.43k
void VBloomPredicate::set_filter(std::shared_ptr<BloomFilterFuncBase> filter) {
181
3.43k
    _filter = filter;
182
3.43k
}
183
184
3.19k
uint64_t VBloomPredicate::get_digest(uint64_t seed) const {
185
3.19k
    seed = _children[0]->get_digest(seed);
186
3.19k
    if (seed) {
187
3.18k
        char* data;
188
3.18k
        int len;
189
3.18k
        _filter->get_data(&data, &len);
190
3.18k
        return HashUtil::hash64(data, len, seed);
191
3.18k
    }
192
13
    return 0;
193
3.19k
}
194
195
} // namespace doris