Coverage Report

Created: 2026-06-18 02:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/vcompound_pred.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
18
#pragma once
19
#include <gen_cpp/Opcodes_types.h>
20
21
#include <algorithm>
22
#include <cstdint>
23
24
#include "common/logging.h"
25
#include "common/status.h"
26
#include "core/assert_cast.h"
27
#include "core/column/column.h"
28
#include "core/column/column_nullable.h"
29
#include "exprs/vectorized_fn_call.h"
30
#include "exprs/vexpr_context.h"
31
#include "exprs/vexpr_fwd.h"
32
#include "storage/index/zone_map/zonemap_eval_context.h"
33
#include "util/simd/bits.h"
34
35
namespace doris {
36
37
23
inline std::string compound_operator_to_string(TExprOpcode::type op) {
38
23
    if (op == TExprOpcode::COMPOUND_AND) {
39
10
        return "and";
40
13
    } else if (op == TExprOpcode::COMPOUND_OR) {
41
12
        return "or";
42
12
    } else {
43
1
        return "not";
44
1
    }
45
23
}
46
47
class VCompoundPred : public VectorizedFnCall {
48
    ENABLE_FACTORY_CREATOR(VCompoundPred);
49
50
public:
51
23
    VCompoundPred(const TExprNode& node) : VectorizedFnCall(node) {
52
23
        _op = node.opcode;
53
23
        _fn.name.function_name = compound_operator_to_string(_op);
54
23
        _expr_name = fmt::format("VCompoundPredicate[{}](arguments={},return={})",
55
23
                                 _fn.name.function_name, get_child_names(), _data_type->get_name());
56
23
    }
57
58
#ifdef BE_TEST
59
2
    VCompoundPred() = default;
60
#endif
61
62
18
    const std::string& expr_name() const override { return _expr_name; }
63
64
1
    bool can_evaluate_zonemap_filter() const override {
65
1
        switch (_op) {
66
0
        case TExprOpcode::COMPOUND_AND:
67
0
            return std::ranges::any_of(_children, [](const VExprSPtr& child) {
68
0
                return child->can_evaluate_zonemap_filter();
69
0
            });
70
1
        case TExprOpcode::COMPOUND_OR:
71
2
            return !_children.empty() && std::ranges::all_of(_children, [](const VExprSPtr& child) {
72
2
                return child->can_evaluate_zonemap_filter();
73
2
            });
74
0
        case TExprOpcode::COMPOUND_NOT:
75
0
            return false;
76
0
        default:
77
0
            return false;
78
1
        }
79
1
    }
80
81
5
    ZoneMapFilterResult evaluate_zonemap_filter(const ZoneMapEvalContext& ctx) const override {
82
5
        switch (_op) {
83
2
        case TExprOpcode::COMPOUND_AND: {
84
4
            for (const auto& child : _children) {
85
4
                if (!child->can_evaluate_zonemap_filter()) {
86
1
                    continue;
87
1
                }
88
3
                if (child->evaluate_zonemap_filter(ctx) == ZoneMapFilterResult::kNoMatch) {
89
2
                    return ZoneMapFilterResult::kNoMatch;
90
2
                }
91
3
            }
92
0
            return ZoneMapFilterResult::kMayMatch;
93
2
        }
94
2
        case TExprOpcode::COMPOUND_OR: {
95
4
            for (const auto& child : _children) {
96
4
                DORIS_CHECK(child->can_evaluate_zonemap_filter());
97
4
                if (child->evaluate_zonemap_filter(ctx) != ZoneMapFilterResult::kNoMatch) {
98
1
                    return ZoneMapFilterResult::kMayMatch;
99
1
                }
100
4
            }
101
1
            return ZoneMapFilterResult::kNoMatch;
102
2
        }
103
1
        case TExprOpcode::COMPOUND_NOT:
104
1
            return unsupported_zonemap_filter(ctx);
105
0
        default:
106
0
            return unsupported_zonemap_filter(ctx);
107
5
        }
108
5
    }
109
110
0
    Status evaluate_inverted_index(VExprContext* context, uint32_t segment_num_rows) override {
111
0
        segment_v2::InvertedIndexResultBitmap res;
112
0
        bool all_pass = true;
113
114
0
        switch (_op) {
115
0
        case TExprOpcode::COMPOUND_OR: {
116
0
            for (const auto& child : _children) {
117
0
                if (Status st = child->evaluate_inverted_index(context, segment_num_rows);
118
0
                    !st.ok()) {
119
0
                    LOG(ERROR) << "expr:" << child->expr_name()
120
0
                               << " evaluate_inverted_index error:" << st.to_string();
121
0
                    all_pass = false;
122
0
                    continue;
123
0
                }
124
0
                auto inverted_index_context = context->get_index_context();
125
0
                if (inverted_index_context->has_index_result_for_expr(child.get())) {
126
0
                    const auto* index_result =
127
0
                            inverted_index_context->get_index_result_for_expr(child.get());
128
0
                    if (res.is_empty()) {
129
0
                        res = *index_result;
130
0
                    } else {
131
0
                        res |= *index_result;
132
0
                    }
133
0
                    if (inverted_index_context->get_score_runtime() == nullptr) {
134
0
                        if (res.get_data_bitmap()->cardinality() == segment_num_rows) {
135
0
                            break; // Early exit if result is full
136
0
                        }
137
0
                    }
138
0
                } else {
139
0
                    all_pass = false;
140
0
                }
141
0
            }
142
0
            break;
143
0
        }
144
0
        case TExprOpcode::COMPOUND_AND: {
145
0
            for (const auto& child : _children) {
146
0
                if (Status st = child->evaluate_inverted_index(context, segment_num_rows);
147
0
                    !st.ok()) {
148
0
                    LOG(ERROR) << "expr:" << child->expr_name()
149
0
                               << " evaluate_inverted_index error:" << st.to_string();
150
0
                    all_pass = false;
151
0
                    continue;
152
0
                }
153
0
                if (context->get_index_context()->has_index_result_for_expr(child.get())) {
154
0
                    const auto* index_result =
155
0
                            context->get_index_context()->get_index_result_for_expr(child.get());
156
0
                    if (res.is_empty()) {
157
0
                        res = *index_result;
158
0
                    } else {
159
0
                        res &= *index_result;
160
0
                    }
161
162
0
                    if (res.get_data_bitmap()->isEmpty()) {
163
0
                        break; // Early exit if result is empty
164
0
                    }
165
0
                } else {
166
0
                    all_pass = false;
167
0
                }
168
0
            }
169
0
            break;
170
0
        }
171
0
        case TExprOpcode::COMPOUND_NOT: {
172
0
            const auto& child = _children[0];
173
0
            Status st = child->evaluate_inverted_index(context, segment_num_rows);
174
0
            if (!st.ok()) {
175
0
                LOG(ERROR) << "expr:" << child->expr_name()
176
0
                           << " evaluate_inverted_index error:" << st.to_string();
177
0
                return st;
178
0
            }
179
180
0
            if (context->get_index_context()->has_index_result_for_expr(child.get())) {
181
0
                const auto* index_result =
182
0
                        context->get_index_context()->get_index_result_for_expr(child.get());
183
0
                roaring::Roaring full_result;
184
0
                full_result.addRange(0, segment_num_rows);
185
0
                res = index_result->op_not(&full_result);
186
0
            } else {
187
0
                all_pass = false;
188
0
            }
189
0
            break;
190
0
        }
191
0
        default:
192
0
            return Status::NotSupported(
193
0
                    "Compound operator must be AND, OR, or NOT to execute with inverted index.");
194
0
        }
195
196
0
        if (all_pass && !res.is_empty()) {
197
0
            context->get_index_context()->set_index_result_for_expr(this, res);
198
0
        }
199
0
        return Status::OK();
200
0
    }
201
202
    Status execute_column_impl(VExprContext* context, const Block* block, const Selector* selector,
203
0
                               size_t count, ColumnPtr& result_column) const override {
204
0
        if (fast_execute(context, selector, count, result_column)) {
205
0
            return Status::OK();
206
0
        }
207
0
        if (get_num_children() == 1 || _has_const_child()) {
208
0
            return VectorizedFnCall::execute_column_impl(context, block, selector, count,
209
0
                                                         result_column);
210
0
        }
211
212
0
        ColumnPtr lhs_column;
213
0
        RETURN_IF_ERROR(_children[0]->execute_column(context, block, selector, count, lhs_column));
214
0
        lhs_column = lhs_column->convert_to_full_column_if_const();
215
0
        size_t size = lhs_column->size();
216
217
0
        bool lhs_is_nullable = lhs_column->is_nullable();
218
0
        auto [lhs_data_column, lhs_null_map] =
219
0
                _get_raw_data_and_null_map(lhs_column, lhs_is_nullable);
220
0
        size_t filted = simd::count_zero_num((int8_t*)lhs_data_column, size);
221
0
        bool lhs_all_true = (filted == 0);
222
0
        bool lhs_all_false = (filted == size);
223
224
0
        bool lhs_all_is_not_null = false;
225
0
        if (lhs_is_nullable) {
226
0
            filted = simd::count_zero_num((int8_t*)lhs_null_map, size);
227
0
            lhs_all_is_not_null = (filted == size);
228
0
        }
229
230
0
        ColumnPtr rhs_column = nullptr;
231
0
        const uint8_t* __restrict rhs_data_column = nullptr;
232
0
        const uint8_t* __restrict rhs_null_map = nullptr;
233
0
        bool rhs_is_nullable = false;
234
0
        bool rhs_all_true = false;
235
0
        bool rhs_all_false = false;
236
0
        bool rhs_all_is_not_null = false;
237
0
        bool result_is_nullable = _data_type->is_nullable();
238
239
0
        auto get_rhs_colum = [&]() {
240
0
            if (!rhs_column) {
241
0
                RETURN_IF_ERROR(
242
0
                        _children[1]->execute_column(context, block, selector, count, rhs_column));
243
0
                rhs_column = rhs_column->convert_to_full_column_if_const();
244
0
                rhs_is_nullable = rhs_column->is_nullable();
245
0
                auto rhs_nullable_column = _get_raw_data_and_null_map(rhs_column, rhs_is_nullable);
246
0
                rhs_data_column = rhs_nullable_column.first;
247
0
                rhs_null_map = rhs_nullable_column.second;
248
0
                size_t filted = simd::count_zero_num((int8_t*)rhs_data_column, size);
249
0
                rhs_all_true = (filted == 0);
250
0
                rhs_all_false = (filted == size);
251
0
                if (rhs_is_nullable) {
252
0
                    filted = simd::count_zero_num((int8_t*)rhs_null_map, size);
253
0
                    rhs_all_is_not_null = (filted == size);
254
0
                }
255
0
            }
256
0
            return Status::OK();
257
0
        };
258
259
0
        auto return_result_column_id = [&](ColumnPtr& arg_column) {
260
0
            result_column = std::move(*arg_column).mutate();
261
0
            if (result_is_nullable && !result_column->is_nullable()) {
262
0
                result_column = make_nullable(result_column);
263
0
            }
264
0
        };
265
266
0
        auto create_null_map_column = [&](ColumnPtr& null_map_column,
267
0
                                          const uint8_t* __restrict null_map_data) {
268
0
            if (null_map_data == nullptr) {
269
0
                null_map_column = ColumnUInt8::create(size, 0);
270
0
                null_map_data =
271
0
                        assert_cast<const ColumnUInt8*>(null_map_column.get())->get_data().data();
272
0
            }
273
0
            return null_map_data;
274
0
        };
275
276
0
        auto vector_vector = [&]<bool is_and_op>() {
277
0
            MutableColumnPtr mutable_result_column;
278
0
            uint8_t* __restrict result_data_column = nullptr;
279
0
            const uint8_t* __restrict other_data_column = rhs_data_column;
280
0
            if (lhs_column->use_count() == 1) {
281
0
                mutable_result_column = IColumn::mutate(std::move(lhs_column));
282
0
                result_data_column =
283
0
                        assert_cast<ColumnUInt8*>(mutable_result_column.get())->get_data().data();
284
0
            } else if (rhs_column->use_count() == 1) {
285
0
                mutable_result_column = IColumn::mutate(std::move(rhs_column));
286
0
                result_data_column =
287
0
                        assert_cast<ColumnUInt8*>(mutable_result_column.get())->get_data().data();
288
0
                other_data_column = lhs_data_column;
289
0
            } else {
290
0
                mutable_result_column = lhs_column->clone_resized(size);
291
0
                result_data_column =
292
0
                        assert_cast<ColumnUInt8*>(mutable_result_column.get())->get_data().data();
293
0
            }
294
295
0
            do_not_null_pred<is_and_op>(result_data_column, other_data_column, size);
296
0
            result_column = std::move(mutable_result_column);
297
0
        };
Unexecuted instantiation: _ZZNK5doris13VCompoundPred19execute_column_implEPNS_12VExprContextEPKNS_5BlockEPKNS_8PODArrayIjLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEEmRNS_3COWINS_7IColumnEE13immutable_ptrISE_EEENKUlTnbvE_clILb1EEEDav
Unexecuted instantiation: _ZZNK5doris13VCompoundPred19execute_column_implEPNS_12VExprContextEPKNS_5BlockEPKNS_8PODArrayIjLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEEmRNS_3COWINS_7IColumnEE13immutable_ptrISE_EEENKUlTnbvE_clILb0EEEDav
298
0
        auto vector_vector_null = [&]<bool is_and_op>() {
299
0
            auto col_res = ColumnUInt8::create(size);
300
0
            auto col_nulls = ColumnUInt8::create(size);
301
302
0
            auto* __restrict res_datas = col_res->get_data().data();
303
0
            auto* __restrict res_nulls = col_nulls->get_data().data();
304
0
            ColumnPtr temp_null_map = nullptr;
305
            // maybe both children are nullable / or one of children is nullable
306
0
            auto* __restrict lhs_null_map_tmp = create_null_map_column(temp_null_map, lhs_null_map);
307
0
            auto* __restrict rhs_null_map_tmp = create_null_map_column(temp_null_map, rhs_null_map);
308
0
            auto* __restrict lhs_data_column_tmp = lhs_data_column;
309
0
            auto* __restrict rhs_data_column_tmp = rhs_data_column;
310
311
0
            do_null_pred<is_and_op>(lhs_data_column_tmp, lhs_null_map_tmp, rhs_data_column_tmp,
312
0
                                    rhs_null_map_tmp, res_datas, res_nulls, size);
313
314
0
            result_column = ColumnNullable::create(std::move(col_res), std::move(col_nulls));
315
0
        };
Unexecuted instantiation: _ZZNK5doris13VCompoundPred19execute_column_implEPNS_12VExprContextEPKNS_5BlockEPKNS_8PODArrayIjLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEEmRNS_3COWINS_7IColumnEE13immutable_ptrISE_EEENKUlTnbvE0_clILb1EEEDav
Unexecuted instantiation: _ZZNK5doris13VCompoundPred19execute_column_implEPNS_12VExprContextEPKNS_5BlockEPKNS_8PODArrayIjLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEEmRNS_3COWINS_7IColumnEE13immutable_ptrISE_EEENKUlTnbvE0_clILb0EEEDav
316
317
        // false and NULL ----> 0
318
        // true  and NULL ----> NULL
319
0
        if (_op == TExprOpcode::COMPOUND_AND) {
320
            //1. not null column: all data is false
321
            //2. nullable column: null map all is not null
322
0
            if ((lhs_all_false && !lhs_is_nullable) || (lhs_all_false && lhs_all_is_not_null)) {
323
                // false and any = false, return lhs
324
0
                return_result_column_id(lhs_column);
325
0
            } else {
326
0
                RETURN_IF_ERROR(get_rhs_colum());
327
328
0
                if ((lhs_all_true && !lhs_is_nullable) ||    //not null column
329
0
                    (lhs_all_true && lhs_all_is_not_null)) { //nullable column
330
                                                             // true and any = any, return rhs
331
332
0
                    return_result_column_id(rhs_column);
333
0
                } else if ((rhs_all_false && !rhs_is_nullable) ||
334
0
                           (rhs_all_false && rhs_all_is_not_null)) {
335
                    // any and false = false, return rhs
336
0
                    return_result_column_id(rhs_column);
337
0
                } else if ((rhs_all_true && !rhs_is_nullable) ||
338
0
                           (rhs_all_true && rhs_all_is_not_null)) {
339
                    // any and true = any, return lhs
340
0
                    return_result_column_id(lhs_column);
341
0
                } else {
342
0
                    if (!result_is_nullable) {
343
0
                        vector_vector.template operator()<true>();
344
0
                    } else {
345
0
                        vector_vector_null.template operator()<true>();
346
0
                    }
347
0
                }
348
0
            }
349
0
        } else if (_op == TExprOpcode::COMPOUND_OR) {
350
            // true  or NULL ----> 1
351
            // false or NULL ----> NULL
352
0
            if ((lhs_all_true && !lhs_is_nullable) || (lhs_all_true && lhs_all_is_not_null)) {
353
                // true or any = true, return lhs
354
0
                return_result_column_id(lhs_column);
355
0
            } else {
356
0
                RETURN_IF_ERROR(get_rhs_colum());
357
0
                if ((lhs_all_false && !lhs_is_nullable) || (lhs_all_false && lhs_all_is_not_null)) {
358
                    // false or any = any, return rhs
359
0
                    return_result_column_id(rhs_column);
360
0
                } else if ((rhs_all_true && !rhs_is_nullable) ||
361
0
                           (rhs_all_true && rhs_all_is_not_null)) {
362
                    // any or true = true, return rhs
363
0
                    return_result_column_id(rhs_column);
364
0
                } else if ((rhs_all_false && !rhs_is_nullable) ||
365
0
                           (rhs_all_false && rhs_all_is_not_null)) {
366
                    // any or false = any, return lhs
367
0
                    return_result_column_id(lhs_column);
368
0
                } else {
369
0
                    if (!result_is_nullable) {
370
0
                        vector_vector.template operator()<false>();
371
0
                    } else {
372
0
                        vector_vector_null.template operator()<false>();
373
0
                    }
374
0
                }
375
0
            }
376
0
        } else {
377
0
            return Status::InternalError("Compound operator must be AND or OR.");
378
0
        }
379
380
0
        DCHECK_EQ(result_column->size(), count);
381
0
        return Status::OK();
382
0
    }
383
384
0
    double execute_cost() const override {
385
0
        double cost = 0.3;
386
0
        for (const auto& child : _children) {
387
0
            cost += child->execute_cost();
388
0
        }
389
0
        return cost;
390
0
    }
391
392
private:
393
0
    static inline constexpr uint8_t apply_and_null(UInt8 a, UInt8 l_null, UInt8 b, UInt8 r_null) {
394
        // (<> && false) is false, (true && NULL) is NULL
395
0
        return (l_null & r_null) | (r_null & (l_null ^ a)) | (l_null & (r_null ^ b));
396
0
    }
397
0
    static inline constexpr uint8_t apply_or_null(UInt8 a, UInt8 l_null, UInt8 b, UInt8 r_null) {
398
        // (<> || true) is true, (false || NULL) is NULL
399
0
        return (l_null & r_null) | (r_null & (r_null ^ a)) | (l_null & (l_null ^ b));
400
0
    }
401
402
    template <bool is_and>
403
    void static do_not_null_pred(uint8_t* __restrict lhs, const uint8_t* __restrict rhs,
404
0
                                 size_t size) {
405
#ifdef NDEBUG
406
#if defined(__clang__)
407
#pragma clang loop vectorize(enable)
408
#elif defined(__GNUC__) && (__GNUC__ >= 5)
409
#pragma GCC ivdep
410
#endif
411
#endif
412
0
        for (size_t i = 0; i < size; ++i) {
413
0
            if constexpr (is_and) {
414
0
                lhs[i] &= rhs[i];
415
0
            } else {
416
0
                lhs[i] |= rhs[i];
417
0
            }
418
0
        }
419
0
    }
Unexecuted instantiation: _ZN5doris13VCompoundPred16do_not_null_predILb1EEEvPhPKhm
Unexecuted instantiation: _ZN5doris13VCompoundPred16do_not_null_predILb0EEEvPhPKhm
420
421
    template <bool is_and>
422
    void static do_null_pred(const uint8_t* __restrict lhs_data, const uint8_t* __restrict lhs_null,
423
                             const uint8_t* __restrict rhs_data, const uint8_t* __restrict rhs_null,
424
                             uint8_t* __restrict res_data, uint8_t* __restrict res_null,
425
0
                             size_t size) {
426
#ifdef NDEBUG
427
#if defined(__clang__)
428
#pragma clang loop vectorize(enable)
429
#elif defined(__GNUC__) && (__GNUC__ >= 5)
430
#pragma GCC ivdep
431
#endif
432
#endif
433
0
        for (size_t i = 0; i < size; ++i) {
434
0
            if constexpr (is_and) {
435
0
                res_null[i] = apply_and_null(lhs_data[i], lhs_null[i], rhs_data[i], rhs_null[i]);
436
0
                res_data[i] = lhs_data[i] & rhs_data[i];
437
0
            } else {
438
0
                res_null[i] = apply_or_null(lhs_data[i], lhs_null[i], rhs_data[i], rhs_null[i]);
439
0
                res_data[i] = lhs_data[i] | rhs_data[i];
440
0
            }
441
0
        }
442
0
    }
Unexecuted instantiation: _ZN5doris13VCompoundPred12do_null_predILb1EEEvPKhS3_S3_S3_PhS4_m
Unexecuted instantiation: _ZN5doris13VCompoundPred12do_null_predILb0EEEvPKhS3_S3_S3_PhS4_m
443
444
0
    bool _has_const_child() const {
445
0
        return std::ranges::any_of(_children,
446
0
                                   [](const VExprSPtr& arg) -> bool { return arg->is_constant(); });
447
0
    }
448
449
    std::pair<const uint8_t*, const uint8_t*> _get_raw_data_and_null_map(
450
0
            const ColumnPtr& column, bool has_nullable_column) const {
451
0
        if (has_nullable_column) {
452
0
            const auto* nullable_column = assert_cast<const ColumnNullable*>(column.get());
453
0
            auto* data_column =
454
0
                    assert_cast<const ColumnUInt8*>(nullable_column->get_nested_column_ptr().get())
455
0
                            ->get_data()
456
0
                            .data();
457
0
            auto* null_map = nullable_column->get_null_map_column_ptr()->get_data().data();
458
0
            return std::make_pair(data_column, null_map);
459
0
        } else {
460
0
            auto* data_column = assert_cast<const ColumnUInt8*>(column.get())->get_data().data();
461
0
            return std::make_pair(data_column, nullptr);
462
0
        }
463
0
    }
464
465
    TExprOpcode::type _op;
466
};
467
468
} // namespace doris