Coverage Report

Created: 2026-10-30 16:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/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
#include <memory>
24
25
#include "common/logging.h"
26
#include "common/status.h"
27
#include "core/assert_cast.h"
28
#include "core/column/column.h"
29
#include "core/column/column_nullable.h"
30
#include "exprs/vectorized_fn_call.h"
31
#include "exprs/vexpr_context.h"
32
#include "exprs/vexpr_fwd.h"
33
#include "storage/index/inverted/query/scoring/collection_similarity.h"
34
#include "storage/index/zone_map/zonemap_eval_context.h"
35
#include "util/defer_op.h"
36
#include "util/simd/bits.h"
37
38
namespace doris {
39
#include "common/compile_check_begin.h"
40
41
63
inline std::string compound_operator_to_string(TExprOpcode::type op) {
42
63
    if (op == TExprOpcode::COMPOUND_AND) {
43
30
        return "and";
44
33
    } else if (op == TExprOpcode::COMPOUND_OR) {
45
28
        return "or";
46
28
    } else {
47
5
        return "not";
48
5
    }
49
63
}
50
51
3
inline bool inverted_index_status_allows_row_fallback(const Status& status) {
52
3
    DORIS_CHECK(!status.ok());
53
3
    return status.is<ErrorCode::INVERTED_INDEX_BYPASS>() ||
54
3
           status.is<ErrorCode::INVERTED_INDEX_EVALUATE_SKIPPED>() ||
55
3
           status.is<ErrorCode::INVERTED_INDEX_FILE_CORRUPTED>() ||
56
3
           status.is<ErrorCode::INVERTED_INDEX_FILE_NOT_FOUND>() ||
57
3
           status.is<ErrorCode::NOT_IMPLEMENTED_ERROR>();
58
3
}
59
60
class VCompoundPred : public VectorizedFnCall {
61
    ENABLE_FACTORY_CREATOR(VCompoundPred);
62
63
public:
64
63
    VCompoundPred(const TExprNode& node) : VectorizedFnCall(node) {
65
63
        _op = node.opcode;
66
63
        _fn.name.function_name = compound_operator_to_string(_op);
67
63
        _expr_name = fmt::format("VCompoundPredicate[{}](arguments={},return={})",
68
63
                                 _fn.name.function_name, get_child_names(), _data_type->get_name());
69
63
    }
70
71
#ifdef BE_TEST
72
2
    VCompoundPred() = default;
73
#endif
74
75
18
    const std::string& expr_name() const override { return _expr_name; }
76
0
    Status clone_node(VExprSPtr* cloned_expr) const override {
77
0
        DORIS_CHECK(cloned_expr != nullptr);
78
0
        *cloned_expr = VCompoundPred::create_shared(clone_texpr_node());
79
0
        return Status::OK();
80
0
    }
81
82
    bool can_execute_on_raw_fixed_values(const DataTypePtr& data_type,
83
24
                                         int column_id) const override {
84
24
        return !_children.empty() &&
85
24
               (_op == TExprOpcode::COMPOUND_AND || _op == TExprOpcode::COMPOUND_OR) &&
86
27
               std::ranges::all_of(_children, [&](const VExprSPtr& child) {
87
27
                   return child->can_execute_on_raw_fixed_values(data_type, column_id);
88
27
               });
89
24
    }
90
91
    Status execute_on_raw_fixed_values(const uint8_t* values, size_t num_values, size_t value_width,
92
                                       const DataTypePtr& data_type, int column_id,
93
2
                                       uint8_t* matches) const override {
94
2
        if (!can_execute_on_raw_fixed_values(data_type, column_id)) {
95
0
            return Status::NotSupported("Compound predicate cannot evaluate raw fixed values");
96
0
        }
97
2
        return _execute_raw_compound(
98
4
                num_values, matches, [&](const VExprSPtr& child, uint8_t* child_matches) {
99
4
                    return child->execute_on_raw_fixed_values(values, num_values, value_width,
100
4
                                                              data_type, column_id, child_matches);
101
4
                });
102
2
    }
103
104
    bool can_execute_on_raw_binary_values(const DataTypePtr& data_type,
105
40
                                          int column_id) const override {
106
40
        return !_children.empty() &&
107
40
               (_op == TExprOpcode::COMPOUND_AND || _op == TExprOpcode::COMPOUND_OR) &&
108
80
               std::ranges::all_of(_children, [&](const VExprSPtr& child) {
109
80
                   return child->can_execute_on_raw_binary_values(data_type, column_id);
110
80
               });
111
40
    }
112
113
    Status execute_on_raw_binary_values(const StringRef* values, size_t num_values,
114
                                        const DataTypePtr& data_type, int column_id,
115
8
                                        uint8_t* matches) const override {
116
8
        if (!can_execute_on_raw_binary_values(data_type, column_id)) {
117
0
            return Status::NotSupported("Compound predicate cannot evaluate raw binary values");
118
0
        }
119
8
        return _execute_raw_compound(
120
16
                num_values, matches, [&](const VExprSPtr& child, uint8_t* child_matches) {
121
16
                    return child->execute_on_raw_binary_values(values, num_values, data_type,
122
16
                                                               column_id, child_matches);
123
16
                });
124
8
    }
125
126
12
    bool raw_predicate_result_for_null() const override {
127
12
        if (_op != TExprOpcode::COMPOUND_AND && _op != TExprOpcode::COMPOUND_OR) {
128
            // A Boolean keep bit cannot distinguish FALSE from UNKNOWN, so negating a child's
129
            // collapsed result is not SQL-correct. NOT remains residual and rejects NULL here.
130
0
            return false;
131
0
        }
132
12
        if (_op == TExprOpcode::COMPOUND_AND) {
133
3
            return std::ranges::all_of(_children, [](const VExprSPtr& child) {
134
3
                return child->raw_predicate_result_for_null();
135
3
            });
136
3
        }
137
18
        return std::ranges::any_of(_children, [](const VExprSPtr& child) {
138
18
            return child->raw_predicate_result_for_null();
139
18
        });
140
12
    }
141
142
76
    bool can_evaluate_zonemap_filter() const override {
143
76
        switch (_op) {
144
42
        case TExprOpcode::COMPOUND_AND:
145
53
            return std::ranges::any_of(_children, [](const VExprSPtr& child) {
146
53
                return child->can_evaluate_zonemap_filter();
147
53
            });
148
34
        case TExprOpcode::COMPOUND_OR:
149
68
            return !_children.empty() && std::ranges::all_of(_children, [](const VExprSPtr& child) {
150
68
                return child->can_evaluate_zonemap_filter();
151
68
            });
152
0
        case TExprOpcode::COMPOUND_NOT:
153
0
            return false;
154
0
        default:
155
0
            return false;
156
76
        }
157
76
    }
158
159
10
    ZoneMapFilterResult evaluate_zonemap_filter(const ZoneMapEvalContext& ctx) const override {
160
10
        switch (_op) {
161
5
        case TExprOpcode::COMPOUND_AND: {
162
10
            for (const auto& child : _children) {
163
10
                if (!child->can_evaluate_zonemap_filter()) {
164
2
                    continue;
165
2
                }
166
8
                if (child->evaluate_zonemap_filter(ctx) == ZoneMapFilterResult::kNoMatch) {
167
2
                    return ZoneMapFilterResult::kNoMatch;
168
2
                }
169
8
            }
170
3
            return ZoneMapFilterResult::kMayMatch;
171
5
        }
172
4
        case TExprOpcode::COMPOUND_OR: {
173
6
            for (const auto& child : _children) {
174
6
                DORIS_CHECK(child->can_evaluate_zonemap_filter());
175
6
                if (child->evaluate_zonemap_filter(ctx) != ZoneMapFilterResult::kNoMatch) {
176
3
                    return ZoneMapFilterResult::kMayMatch;
177
3
                }
178
6
            }
179
1
            return ZoneMapFilterResult::kNoMatch;
180
4
        }
181
1
        case TExprOpcode::COMPOUND_NOT:
182
1
            return unsupported_zonemap_filter(ctx);
183
0
        default:
184
0
            return unsupported_zonemap_filter(ctx);
185
10
        }
186
10
    }
187
188
33
    bool can_evaluate_dictionary_filter() const override {
189
33
        switch (_op) {
190
30
        case TExprOpcode::COMPOUND_AND:
191
33
            return std::ranges::any_of(_children, [](const VExprSPtr& child) {
192
33
                return child->can_evaluate_dictionary_filter();
193
33
            });
194
3
        case TExprOpcode::COMPOUND_OR:
195
6
            return !_children.empty() && std::ranges::all_of(_children, [](const VExprSPtr& child) {
196
6
                return child->can_evaluate_dictionary_filter();
197
6
            });
198
0
        default:
199
0
            return false;
200
33
        }
201
33
    }
202
203
9
    bool is_safe_to_execute_on_selected_rows() const override {
204
        // Boolean composition introduces no data-dependent failure of its own. Reuse the generic
205
        // child walk so AND/OR remain eligible only when every nested expression is independently
206
        // safe; applying VectorizedFnCall's scalar-function allowlist to this structural node would
207
        // incorrectly disable selected-row execution for otherwise safe predicates.
208
9
        return VExpr::is_safe_to_execute_on_selected_rows();
209
9
    }
210
211
    ZoneMapFilterResult evaluate_dictionary_filter(
212
21
            const DictionaryEvalContext& ctx) const override {
213
21
        switch (_op) {
214
21
        case TExprOpcode::COMPOUND_AND:
215
29
            for (const auto& child : _children) {
216
29
                if (!child->can_evaluate_dictionary_filter()) {
217
8
                    continue;
218
8
                }
219
21
                if (child->evaluate_dictionary_filter(ctx) == ZoneMapFilterResult::kNoMatch) {
220
13
                    return ZoneMapFilterResult::kNoMatch;
221
13
                }
222
21
            }
223
8
            return ZoneMapFilterResult::kMayMatch;
224
0
        case TExprOpcode::COMPOUND_OR:
225
0
            for (const auto& child : _children) {
226
0
                DORIS_CHECK(child->can_evaluate_dictionary_filter());
227
0
                if (child->evaluate_dictionary_filter(ctx) != ZoneMapFilterResult::kNoMatch) {
228
0
                    return ZoneMapFilterResult::kMayMatch;
229
0
                }
230
0
            }
231
0
            return ZoneMapFilterResult::kNoMatch;
232
0
        default:
233
0
            return ZoneMapFilterResult::kUnsupported;
234
21
        }
235
21
    }
236
237
14
    bool can_evaluate_bloom_filter() const override {
238
14
        switch (_op) {
239
9
        case TExprOpcode::COMPOUND_AND:
240
15
            return std::ranges::any_of(_children, [](const VExprSPtr& child) {
241
15
                return child->can_evaluate_bloom_filter();
242
15
            });
243
5
        case TExprOpcode::COMPOUND_OR:
244
8
            return !_children.empty() && std::ranges::all_of(_children, [](const VExprSPtr& child) {
245
8
                return child->can_evaluate_bloom_filter();
246
8
            });
247
0
        default:
248
0
            return false;
249
14
        }
250
14
    }
251
252
0
    ZoneMapFilterResult evaluate_bloom_filter(const BloomFilterEvalContext& ctx) const override {
253
0
        switch (_op) {
254
0
        case TExprOpcode::COMPOUND_AND:
255
0
            for (const auto& child : _children) {
256
0
                if (!child->can_evaluate_bloom_filter()) {
257
0
                    continue;
258
0
                }
259
0
                if (child->evaluate_bloom_filter(ctx) == ZoneMapFilterResult::kNoMatch) {
260
0
                    return ZoneMapFilterResult::kNoMatch;
261
0
                }
262
0
            }
263
0
            return ZoneMapFilterResult::kMayMatch;
264
0
        case TExprOpcode::COMPOUND_OR:
265
0
            for (const auto& child : _children) {
266
0
                DORIS_CHECK(child->can_evaluate_bloom_filter());
267
0
                if (child->evaluate_bloom_filter(ctx) != ZoneMapFilterResult::kNoMatch) {
268
0
                    return ZoneMapFilterResult::kMayMatch;
269
0
                }
270
0
            }
271
0
            return ZoneMapFilterResult::kNoMatch;
272
0
        default:
273
0
            return ZoneMapFilterResult::kUnsupported;
274
0
        }
275
0
    }
276
277
    // NOLINTNEXTLINE(readability-function-size,readability-function-cognitive-complexity): Keep each operator's bitmap and score updates together.
278
13
    Status evaluate_inverted_index(VExprContext* context, uint32_t segment_num_rows) override {
279
13
        segment_v2::InvertedIndexResultBitmap res;
280
13
        bool all_pass = true;
281
13
        auto index_context = context->get_index_context();
282
13
        auto query_context = index_context->get_index_query_context();
283
13
        auto parent_similarity =
284
13
                query_context != nullptr ? query_context->collection_similarity : nullptr;
285
13
        CollectionSimilarityPtr branch_similarity;
286
13
        if (parent_similarity != nullptr) {
287
6
            branch_similarity = std::make_shared<CollectionSimilarity>();
288
6
            query_context->collection_similarity = branch_similarity;
289
6
        }
290
13
        Defer restore_similarity {[&]() {
291
13
            if (branch_similarity != nullptr) {
292
6
                query_context->collection_similarity = parent_similarity;
293
6
            }
294
13
        }};
295
296
13
        switch (_op) {
297
4
        case TExprOpcode::COMPOUND_OR: {
298
7
            for (const auto& child : _children) {
299
7
                if (Status st = child->evaluate_inverted_index(context, segment_num_rows);
300
7
                    !st.ok()) {
301
1
                    LOG(ERROR) << "expr:" << child->expr_name()
302
1
                               << " evaluate_inverted_index error:" << st.to_string();
303
1
                    if (!inverted_index_status_allows_row_fallback(st)) {
304
1
                        return st;
305
1
                    }
306
0
                    all_pass = false;
307
0
                    continue;
308
1
                }
309
6
                auto inverted_index_context = context->get_index_context();
310
6
                if (inverted_index_context->has_index_result_for_expr(child.get())) {
311
4
                    const auto* index_result =
312
4
                            inverted_index_context->get_index_result_for_expr(child.get());
313
4
                    if (res.is_empty()) {
314
2
                        res = *index_result;
315
2
                    } else {
316
2
                        res |= *index_result;
317
2
                    }
318
4
                    if (branch_similarity == nullptr) {
319
0
                        if (res.get_data_bitmap()->cardinality() == segment_num_rows) {
320
0
                            break; // Early exit if result is full
321
0
                        }
322
0
                    }
323
4
                } else {
324
2
                    all_pass = false;
325
2
                }
326
6
            }
327
3
            break;
328
4
        }
329
6
        case TExprOpcode::COMPOUND_AND: {
330
10
            for (const auto& child : _children) {
331
10
                if (Status st = child->evaluate_inverted_index(context, segment_num_rows);
332
10
                    !st.ok()) {
333
2
                    LOG(ERROR) << "expr:" << child->expr_name()
334
2
                               << " evaluate_inverted_index error:" << st.to_string();
335
2
                    if (!inverted_index_status_allows_row_fallback(st)) {
336
2
                        return st;
337
2
                    }
338
0
                    all_pass = false;
339
0
                    continue;
340
2
                }
341
8
                if (context->get_index_context()->has_index_result_for_expr(child.get())) {
342
7
                    const auto* index_result =
343
7
                            context->get_index_context()->get_index_result_for_expr(child.get());
344
7
                    if (res.is_empty()) {
345
4
                        res = *index_result;
346
4
                    } else {
347
3
                        res &= *index_result;
348
3
                    }
349
350
7
                    if (res.get_data_bitmap()->isEmpty()) {
351
1
                        break; // Early exit if result is empty
352
1
                    }
353
7
                } else {
354
1
                    all_pass = false;
355
1
                }
356
8
            }
357
4
            break;
358
6
        }
359
4
        case TExprOpcode::COMPOUND_NOT: {
360
3
            const auto& child = _children[0];
361
3
            Status st = child->evaluate_inverted_index(context, segment_num_rows);
362
3
            if (!st.ok()) {
363
0
                LOG(ERROR) << "expr:" << child->expr_name()
364
0
                           << " evaluate_inverted_index error:" << st.to_string();
365
0
                return st;
366
0
            }
367
368
3
            if (context->get_index_context()->has_index_result_for_expr(child.get())) {
369
2
                const auto* index_result =
370
2
                        context->get_index_context()->get_index_result_for_expr(child.get());
371
2
                roaring::Roaring full_result;
372
2
                full_result.addRange(0, segment_num_rows);
373
2
                res = index_result->op_not(&full_result);
374
2
            } else {
375
1
                all_pass = false;
376
1
            }
377
3
            break;
378
3
        }
379
0
        default:
380
0
            return Status::NotSupported(
381
0
                    "Compound operator must be AND, OR, or NOT to execute with inverted index.");
382
13
        }
383
384
10
        if (branch_similarity != nullptr && _op != TExprOpcode::COMPOUND_NOT) {
385
5
            auto scores = branch_similarity->release_scores();
386
5
            if (!scores.empty() && (!all_pass || res.is_empty())) {
387
1
                return Status::Error<ErrorCode::INVERTED_INDEX_NOT_SUPPORTED>(
388
1
                        "Cannot score a compound predicate with incomplete index evaluation");
389
1
            }
390
4
            if (all_pass && !res.is_empty()) {
391
4
                DORIS_CHECK(res.get_data_bitmap() != nullptr);
392
4
                parent_similarity->merge_scores(scores, *res.get_data_bitmap());
393
4
            }
394
4
        }
395
9
        if (all_pass && !res.is_empty()) {
396
7
            index_context->set_index_result_for_expr(this, res);
397
7
        }
398
9
        return Status::OK();
399
10
    }
400
401
    Status execute_column_impl(VExprContext* context, const Block* block, const Selector* selector,
402
4
                               size_t count, ColumnPtr& result_column) const override {
403
4
        if (fast_execute(context, selector, count, result_column)) {
404
0
            return Status::OK();
405
0
        }
406
4
        if (get_num_children() == 1 || _has_const_child()) {
407
0
            return VectorizedFnCall::execute_column_impl(context, block, selector, count,
408
0
                                                         result_column);
409
0
        }
410
411
4
        ColumnPtr lhs_column;
412
4
        RETURN_IF_ERROR(_children[0]->execute_column(context, block, selector, count, lhs_column));
413
4
        lhs_column = lhs_column->convert_to_full_column_if_const();
414
4
        size_t size = lhs_column->size();
415
416
4
        bool lhs_is_nullable = lhs_column->is_nullable();
417
4
        auto [lhs_data_column, lhs_null_map] =
418
4
                _get_raw_data_and_null_map(lhs_column, lhs_is_nullable);
419
4
        size_t filted = simd::count_zero_num((int8_t*)lhs_data_column, size);
420
4
        bool lhs_all_true = (filted == 0);
421
4
        bool lhs_all_false = (filted == size);
422
423
4
        bool lhs_all_is_not_null = false;
424
4
        if (lhs_is_nullable) {
425
4
            filted = simd::count_zero_num((int8_t*)lhs_null_map, size);
426
4
            lhs_all_is_not_null = (filted == size);
427
4
        }
428
429
4
        ColumnPtr rhs_column = nullptr;
430
4
        const uint8_t* __restrict rhs_data_column = nullptr;
431
4
        const uint8_t* __restrict rhs_null_map = nullptr;
432
4
        bool rhs_is_nullable = false;
433
4
        bool rhs_all_true = false;
434
4
        bool rhs_all_false = false;
435
4
        bool rhs_all_is_not_null = false;
436
4
        bool result_is_nullable = _data_type->is_nullable();
437
438
4
        auto get_rhs_colum = [&]() {
439
4
            if (!rhs_column) {
440
4
                RETURN_IF_ERROR(
441
4
                        _children[1]->execute_column(context, block, selector, count, rhs_column));
442
3
                rhs_column = rhs_column->convert_to_full_column_if_const();
443
3
                rhs_is_nullable = rhs_column->is_nullable();
444
3
                auto rhs_nullable_column = _get_raw_data_and_null_map(rhs_column, rhs_is_nullable);
445
3
                rhs_data_column = rhs_nullable_column.first;
446
3
                rhs_null_map = rhs_nullable_column.second;
447
3
                size_t filted = simd::count_zero_num((int8_t*)rhs_data_column, size);
448
3
                rhs_all_true = (filted == 0);
449
3
                rhs_all_false = (filted == size);
450
3
                if (rhs_is_nullable) {
451
3
                    filted = simd::count_zero_num((int8_t*)rhs_null_map, size);
452
3
                    rhs_all_is_not_null = (filted == size);
453
3
                }
454
3
            }
455
3
            return Status::OK();
456
4
        };
457
458
4
        auto return_result_column_id = [&](ColumnPtr& arg_column) {
459
2
            result_column = std::move(*arg_column).mutate();
460
2
            if (result_is_nullable && !result_column->is_nullable()) {
461
0
                result_column = make_nullable(result_column);
462
0
            }
463
2
        };
464
465
4
        auto create_null_map_column = [&](ColumnPtr& null_map_column,
466
4
                                          const uint8_t* __restrict null_map_data) {
467
2
            if (null_map_data == nullptr) {
468
0
                null_map_column = ColumnUInt8::create(size, 0);
469
0
                null_map_data =
470
0
                        assert_cast<const ColumnUInt8*>(null_map_column.get())->get_data().data();
471
0
            }
472
2
            return null_map_data;
473
2
        };
474
475
4
        auto vector_vector = [&]<bool is_and_op>() {
476
0
            MutableColumnPtr mutable_result_column;
477
0
            uint8_t* __restrict result_data_column = nullptr;
478
0
            const uint8_t* __restrict other_data_column = rhs_data_column;
479
0
            if (lhs_column->use_count() == 1) {
480
0
                mutable_result_column = IColumn::mutate(std::move(lhs_column));
481
0
                result_data_column =
482
0
                        assert_cast<ColumnUInt8*>(mutable_result_column.get())->get_data().data();
483
0
            } else if (rhs_column->use_count() == 1) {
484
0
                mutable_result_column = IColumn::mutate(std::move(rhs_column));
485
0
                result_data_column =
486
0
                        assert_cast<ColumnUInt8*>(mutable_result_column.get())->get_data().data();
487
0
                other_data_column = lhs_data_column;
488
0
            } else {
489
0
                mutable_result_column = lhs_column->clone_resized(size);
490
0
                result_data_column =
491
0
                        assert_cast<ColumnUInt8*>(mutable_result_column.get())->get_data().data();
492
0
            }
493
494
0
            do_not_null_pred<is_and_op>(result_data_column, other_data_column, size);
495
0
            result_column = std::move(mutable_result_column);
496
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
497
4
        auto vector_vector_null = [&]<bool is_and_op>() {
498
1
            auto col_res = ColumnUInt8::create(size);
499
1
            auto col_nulls = ColumnUInt8::create(size);
500
501
1
            auto* __restrict res_datas = col_res->get_data().data();
502
1
            auto* __restrict res_nulls = col_nulls->get_data().data();
503
1
            ColumnPtr temp_null_map = nullptr;
504
            // maybe both children are nullable / or one of children is nullable
505
1
            auto* __restrict lhs_null_map_tmp = create_null_map_column(temp_null_map, lhs_null_map);
506
1
            auto* __restrict rhs_null_map_tmp = create_null_map_column(temp_null_map, rhs_null_map);
507
1
            auto* __restrict lhs_data_column_tmp = lhs_data_column;
508
1
            auto* __restrict rhs_data_column_tmp = rhs_data_column;
509
510
1
            do_null_pred<is_and_op>(lhs_data_column_tmp, lhs_null_map_tmp, rhs_data_column_tmp,
511
1
                                    rhs_null_map_tmp, res_datas, res_nulls, size);
512
513
1
            result_column = ColumnNullable::create(std::move(col_res), std::move(col_nulls));
514
1
        };
Unexecuted instantiation: _ZZNK5doris13VCompoundPred19execute_column_implEPNS_12VExprContextEPKNS_5BlockEPKNS_8PODArrayIjLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEEmRNS_3COWINS_7IColumnEE13immutable_ptrISE_EEENKUlTnbvE0_clILb1EEEDav
_ZZNK5doris13VCompoundPred19execute_column_implEPNS_12VExprContextEPKNS_5BlockEPKNS_8PODArrayIjLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEEmRNS_3COWINS_7IColumnEE13immutable_ptrISE_EEENKUlTnbvE0_clILb0EEEDav
Line
Count
Source
497
1
        auto vector_vector_null = [&]<bool is_and_op>() {
498
1
            auto col_res = ColumnUInt8::create(size);
499
1
            auto col_nulls = ColumnUInt8::create(size);
500
501
1
            auto* __restrict res_datas = col_res->get_data().data();
502
1
            auto* __restrict res_nulls = col_nulls->get_data().data();
503
1
            ColumnPtr temp_null_map = nullptr;
504
            // maybe both children are nullable / or one of children is nullable
505
1
            auto* __restrict lhs_null_map_tmp = create_null_map_column(temp_null_map, lhs_null_map);
506
1
            auto* __restrict rhs_null_map_tmp = create_null_map_column(temp_null_map, rhs_null_map);
507
1
            auto* __restrict lhs_data_column_tmp = lhs_data_column;
508
1
            auto* __restrict rhs_data_column_tmp = rhs_data_column;
509
510
1
            do_null_pred<is_and_op>(lhs_data_column_tmp, lhs_null_map_tmp, rhs_data_column_tmp,
511
1
                                    rhs_null_map_tmp, res_datas, res_nulls, size);
512
513
1
            result_column = ColumnNullable::create(std::move(col_res), std::move(col_nulls));
514
1
        };
515
516
        // false and NULL ----> 0
517
        // true  and NULL ----> NULL
518
4
        if (_op == TExprOpcode::COMPOUND_AND) {
519
            //1. not null column: all data is false
520
            //2. nullable column: null map all is not null
521
1
            if ((lhs_all_false && !lhs_is_nullable) || (lhs_all_false && lhs_all_is_not_null)) {
522
                // false and any = false, return lhs
523
0
                return_result_column_id(lhs_column);
524
1
            } else {
525
1
                RETURN_IF_ERROR(get_rhs_colum());
526
527
0
                if ((lhs_all_true && !lhs_is_nullable) ||    //not null column
528
0
                    (lhs_all_true && lhs_all_is_not_null)) { //nullable column
529
                                                             // true and any = any, return rhs
530
531
0
                    return_result_column_id(rhs_column);
532
0
                } else if ((rhs_all_false && !rhs_is_nullable) ||
533
0
                           (rhs_all_false && rhs_all_is_not_null)) {
534
                    // any and false = false, return rhs
535
0
                    return_result_column_id(rhs_column);
536
0
                } else if ((rhs_all_true && !rhs_is_nullable) ||
537
0
                           (rhs_all_true && rhs_all_is_not_null)) {
538
                    // any and true = any, return lhs
539
0
                    return_result_column_id(lhs_column);
540
0
                } else {
541
0
                    if (!result_is_nullable) {
542
0
                        vector_vector.template operator()<true>();
543
0
                    } else {
544
0
                        vector_vector_null.template operator()<true>();
545
0
                    }
546
0
                }
547
0
            }
548
3
        } else if (_op == TExprOpcode::COMPOUND_OR) {
549
            // true  or NULL ----> 1
550
            // false or NULL ----> NULL
551
3
            if ((lhs_all_true && !lhs_is_nullable) || (lhs_all_true && lhs_all_is_not_null)) {
552
                // true or any = true, return lhs
553
0
                return_result_column_id(lhs_column);
554
3
            } else {
555
3
                RETURN_IF_ERROR(get_rhs_colum());
556
3
                if ((lhs_all_false && !lhs_is_nullable) || (lhs_all_false && lhs_all_is_not_null)) {
557
                    // false or any = any, return rhs
558
1
                    return_result_column_id(rhs_column);
559
2
                } else if ((rhs_all_true && !rhs_is_nullable) ||
560
2
                           (rhs_all_true && rhs_all_is_not_null)) {
561
                    // any or true = true, return rhs
562
0
                    return_result_column_id(rhs_column);
563
2
                } else if ((rhs_all_false && !rhs_is_nullable) ||
564
2
                           (rhs_all_false && rhs_all_is_not_null)) {
565
                    // any or false = any, return lhs
566
1
                    return_result_column_id(lhs_column);
567
1
                } else {
568
1
                    if (!result_is_nullable) {
569
0
                        vector_vector.template operator()<false>();
570
1
                    } else {
571
1
                        vector_vector_null.template operator()<false>();
572
1
                    }
573
1
                }
574
3
            }
575
3
        } else {
576
0
            return Status::InternalError("Compound operator must be AND or OR.");
577
0
        }
578
579
4
        DCHECK_EQ(result_column->size(), count);
580
3
        return Status::OK();
581
4
    }
582
583
0
    double execute_cost() const override {
584
0
        double cost = 0.3;
585
0
        for (const auto& child : _children) {
586
0
            cost += child->execute_cost();
587
0
        }
588
0
        return cost;
589
0
    }
590
591
private:
592
    template <typename ExecuteChild>
593
    Status _execute_raw_compound(size_t num_values, uint8_t* matches,
594
10
                                 ExecuteChild&& execute_child) const {
595
10
        if (_op == TExprOpcode::COMPOUND_AND) {
596
2
            for (const auto& child : _children) {
597
2
                RETURN_IF_ERROR(execute_child(child, matches));
598
2
            }
599
1
            return Status::OK();
600
1
        }
601
602
        // Each execution context owns its expression tree. Retaining masks on the OR node avoids
603
        // N+1 row-sized allocations for every decoder fragment, while nested OR nodes keep
604
        // independent buffers and therefore cannot overwrite their parent's in-flight state.
605
9
        _raw_combined_scratch.resize(num_values);
606
9
        std::ranges::fill(_raw_combined_scratch, 0);
607
18
        for (const auto& child : _children) {
608
            // resize_fill() initializes only newly appended bytes; explicitly reset a reused mask
609
            // so matches from an earlier page fragment cannot leak into this OR evaluation.
610
18
            _raw_child_scratch.resize(num_values);
611
18
            std::ranges::fill(_raw_child_scratch, 1);
612
18
            RETURN_IF_ERROR(execute_child(child, _raw_child_scratch.data()));
613
154
            for (size_t row = 0; row < num_values; ++row) {
614
136
                _raw_combined_scratch[row] |= _raw_child_scratch[row];
615
136
            }
616
18
        }
617
        // Raw kernels receive an existing selection mask, so composition must preserve rows that
618
        // an earlier conjunct already rejected instead of replacing the caller's mask.
619
77
        for (size_t row = 0; row < num_values; ++row) {
620
68
            matches[row] &= _raw_combined_scratch[row];
621
68
        }
622
9
        constexpr size_t MAX_RETAINED_RAW_MASK_BYTES = 1UL << 20;
623
9
        if (_raw_combined_scratch.capacity() > MAX_RETAINED_RAW_MASK_BYTES) {
624
0
            IColumn::Filter().swap(_raw_combined_scratch);
625
0
        }
626
9
        if (_raw_child_scratch.capacity() > MAX_RETAINED_RAW_MASK_BYTES) {
627
0
            IColumn::Filter().swap(_raw_child_scratch);
628
0
        }
629
9
        return Status::OK();
630
9
    }
_ZNK5doris13VCompoundPred21_execute_raw_compoundIZNKS0_27execute_on_raw_fixed_valuesEPKhmmRKSt10shared_ptrIKNS_9IDataTypeEEiPhEUlRKS4_INS_5VExprEESA_E_EENS_6StatusEmSA_OT_
Line
Count
Source
594
2
                                 ExecuteChild&& execute_child) const {
595
2
        if (_op == TExprOpcode::COMPOUND_AND) {
596
2
            for (const auto& child : _children) {
597
2
                RETURN_IF_ERROR(execute_child(child, matches));
598
2
            }
599
1
            return Status::OK();
600
1
        }
601
602
        // Each execution context owns its expression tree. Retaining masks on the OR node avoids
603
        // N+1 row-sized allocations for every decoder fragment, while nested OR nodes keep
604
        // independent buffers and therefore cannot overwrite their parent's in-flight state.
605
1
        _raw_combined_scratch.resize(num_values);
606
1
        std::ranges::fill(_raw_combined_scratch, 0);
607
2
        for (const auto& child : _children) {
608
            // resize_fill() initializes only newly appended bytes; explicitly reset a reused mask
609
            // so matches from an earlier page fragment cannot leak into this OR evaluation.
610
2
            _raw_child_scratch.resize(num_values);
611
2
            std::ranges::fill(_raw_child_scratch, 1);
612
2
            RETURN_IF_ERROR(execute_child(child, _raw_child_scratch.data()));
613
10
            for (size_t row = 0; row < num_values; ++row) {
614
8
                _raw_combined_scratch[row] |= _raw_child_scratch[row];
615
8
            }
616
2
        }
617
        // Raw kernels receive an existing selection mask, so composition must preserve rows that
618
        // an earlier conjunct already rejected instead of replacing the caller's mask.
619
5
        for (size_t row = 0; row < num_values; ++row) {
620
4
            matches[row] &= _raw_combined_scratch[row];
621
4
        }
622
1
        constexpr size_t MAX_RETAINED_RAW_MASK_BYTES = 1UL << 20;
623
1
        if (_raw_combined_scratch.capacity() > MAX_RETAINED_RAW_MASK_BYTES) {
624
0
            IColumn::Filter().swap(_raw_combined_scratch);
625
0
        }
626
1
        if (_raw_child_scratch.capacity() > MAX_RETAINED_RAW_MASK_BYTES) {
627
0
            IColumn::Filter().swap(_raw_child_scratch);
628
0
        }
629
1
        return Status::OK();
630
1
    }
_ZNK5doris13VCompoundPred21_execute_raw_compoundIZNKS0_28execute_on_raw_binary_valuesEPKNS_9StringRefEmRKSt10shared_ptrIKNS_9IDataTypeEEiPhEUlRKS5_INS_5VExprEESB_E_EENS_6StatusEmSB_OT_
Line
Count
Source
594
8
                                 ExecuteChild&& execute_child) const {
595
8
        if (_op == TExprOpcode::COMPOUND_AND) {
596
0
            for (const auto& child : _children) {
597
0
                RETURN_IF_ERROR(execute_child(child, matches));
598
0
            }
599
0
            return Status::OK();
600
0
        }
601
602
        // Each execution context owns its expression tree. Retaining masks on the OR node avoids
603
        // N+1 row-sized allocations for every decoder fragment, while nested OR nodes keep
604
        // independent buffers and therefore cannot overwrite their parent's in-flight state.
605
8
        _raw_combined_scratch.resize(num_values);
606
8
        std::ranges::fill(_raw_combined_scratch, 0);
607
16
        for (const auto& child : _children) {
608
            // resize_fill() initializes only newly appended bytes; explicitly reset a reused mask
609
            // so matches from an earlier page fragment cannot leak into this OR evaluation.
610
16
            _raw_child_scratch.resize(num_values);
611
16
            std::ranges::fill(_raw_child_scratch, 1);
612
16
            RETURN_IF_ERROR(execute_child(child, _raw_child_scratch.data()));
613
144
            for (size_t row = 0; row < num_values; ++row) {
614
128
                _raw_combined_scratch[row] |= _raw_child_scratch[row];
615
128
            }
616
16
        }
617
        // Raw kernels receive an existing selection mask, so composition must preserve rows that
618
        // an earlier conjunct already rejected instead of replacing the caller's mask.
619
72
        for (size_t row = 0; row < num_values; ++row) {
620
64
            matches[row] &= _raw_combined_scratch[row];
621
64
        }
622
8
        constexpr size_t MAX_RETAINED_RAW_MASK_BYTES = 1UL << 20;
623
8
        if (_raw_combined_scratch.capacity() > MAX_RETAINED_RAW_MASK_BYTES) {
624
0
            IColumn::Filter().swap(_raw_combined_scratch);
625
0
        }
626
8
        if (_raw_child_scratch.capacity() > MAX_RETAINED_RAW_MASK_BYTES) {
627
0
            IColumn::Filter().swap(_raw_child_scratch);
628
0
        }
629
8
        return Status::OK();
630
8
    }
631
632
    mutable IColumn::Filter _raw_combined_scratch;
633
    mutable IColumn::Filter _raw_child_scratch;
634
635
0
    static inline constexpr uint8_t apply_and_null(UInt8 a, UInt8 l_null, UInt8 b, UInt8 r_null) {
636
        // (<> && false) is false, (true && NULL) is NULL
637
0
        return (l_null & r_null) | (r_null & (l_null ^ a)) | (l_null & (r_null ^ b));
638
0
    }
639
5
    static inline constexpr uint8_t apply_or_null(UInt8 a, UInt8 l_null, UInt8 b, UInt8 r_null) {
640
        // (<> || true) is true, (false || NULL) is NULL
641
5
        return (l_null & r_null) | (r_null & (r_null ^ a)) | (l_null & (l_null ^ b));
642
5
    }
643
644
    template <bool is_and>
645
    void static do_not_null_pred(uint8_t* __restrict lhs, const uint8_t* __restrict rhs,
646
0
                                 size_t size) {
647
#ifdef NDEBUG
648
#if defined(__clang__)
649
#pragma clang loop vectorize(enable)
650
#elif defined(__GNUC__) && (__GNUC__ >= 5)
651
#pragma GCC ivdep
652
#endif
653
#endif
654
0
        for (size_t i = 0; i < size; ++i) {
655
0
            if constexpr (is_and) {
656
0
                lhs[i] &= rhs[i];
657
0
            } else {
658
                // Logical OR must produce a canonical Boolean instead of preserving input bits.
659
0
                lhs[i] = (lhs[i] | rhs[i]) != 0;
660
0
            }
661
0
        }
662
0
    }
Unexecuted instantiation: _ZN5doris13VCompoundPred16do_not_null_predILb1EEEvPhPKhm
Unexecuted instantiation: _ZN5doris13VCompoundPred16do_not_null_predILb0EEEvPhPKhm
663
664
    template <bool is_and>
665
    void static do_null_pred(const uint8_t* __restrict lhs_data, const uint8_t* __restrict lhs_null,
666
                             const uint8_t* __restrict rhs_data, const uint8_t* __restrict rhs_null,
667
                             uint8_t* __restrict res_data, uint8_t* __restrict res_null,
668
1
                             size_t size) {
669
#ifdef NDEBUG
670
#if defined(__clang__)
671
#pragma clang loop vectorize(enable)
672
#elif defined(__GNUC__) && (__GNUC__ >= 5)
673
#pragma GCC ivdep
674
#endif
675
#endif
676
6
        for (size_t i = 0; i < size; ++i) {
677
5
            if constexpr (is_and) {
678
0
                res_null[i] = apply_and_null(lhs_data[i], lhs_null[i], rhs_data[i], rhs_null[i]);
679
0
                res_data[i] = lhs_data[i] & rhs_data[i];
680
5
            } else {
681
5
                res_null[i] = apply_or_null(lhs_data[i], lhs_null[i], rhs_data[i], rhs_null[i]);
682
                // A NULL row may carry an arbitrary nested byte. If the result remains NULL the
683
                // byte is ignored; otherwise normalization prevents it from becoming visible.
684
5
                res_data[i] = (lhs_data[i] | rhs_data[i]) != 0;
685
5
            }
686
5
        }
687
1
    }
Unexecuted instantiation: _ZN5doris13VCompoundPred12do_null_predILb1EEEvPKhS3_S3_S3_PhS4_m
_ZN5doris13VCompoundPred12do_null_predILb0EEEvPKhS3_S3_S3_PhS4_m
Line
Count
Source
668
1
                             size_t size) {
669
#ifdef NDEBUG
670
#if defined(__clang__)
671
#pragma clang loop vectorize(enable)
672
#elif defined(__GNUC__) && (__GNUC__ >= 5)
673
#pragma GCC ivdep
674
#endif
675
#endif
676
6
        for (size_t i = 0; i < size; ++i) {
677
            if constexpr (is_and) {
678
                res_null[i] = apply_and_null(lhs_data[i], lhs_null[i], rhs_data[i], rhs_null[i]);
679
                res_data[i] = lhs_data[i] & rhs_data[i];
680
5
            } else {
681
5
                res_null[i] = apply_or_null(lhs_data[i], lhs_null[i], rhs_data[i], rhs_null[i]);
682
                // A NULL row may carry an arbitrary nested byte. If the result remains NULL the
683
                // byte is ignored; otherwise normalization prevents it from becoming visible.
684
5
                res_data[i] = (lhs_data[i] | rhs_data[i]) != 0;
685
5
            }
686
5
        }
687
1
    }
688
689
4
    bool _has_const_child() const {
690
4
        return std::ranges::any_of(_children,
691
8
                                   [](const VExprSPtr& arg) -> bool { return arg->is_constant(); });
692
4
    }
693
694
    std::pair<const uint8_t*, const uint8_t*> _get_raw_data_and_null_map(
695
7
            const ColumnPtr& column, bool has_nullable_column) const {
696
7
        if (has_nullable_column) {
697
7
            const auto* nullable_column = assert_cast<const ColumnNullable*>(column.get());
698
7
            auto* data_column =
699
7
                    assert_cast<const ColumnUInt8*>(nullable_column->get_nested_column_ptr().get())
700
7
                            ->get_data()
701
7
                            .data();
702
7
            auto* null_map = nullable_column->get_null_map_column_ptr()->get_data().data();
703
7
            return std::make_pair(data_column, null_map);
704
7
        } else {
705
0
            auto* data_column = assert_cast<const ColumnUInt8*>(column.get())->get_data().data();
706
0
            return std::make_pair(data_column, nullptr);
707
0
        }
708
7
    }
709
710
    TExprOpcode::type _op;
711
};
712
713
#include "common/compile_check_end.h"
714
} // namespace doris