Coverage Report

Created: 2026-07-30 18:53

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