Coverage Report

Created: 2026-07-30 16:45

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
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
#include "common/compile_check_begin.h"
37
38
33
inline std::string compound_operator_to_string(TExprOpcode::type op) {
39
33
    if (op == TExprOpcode::COMPOUND_AND) {
40
17
        return "and";
41
17
    } else if (op == TExprOpcode::COMPOUND_OR) {
42
14
        return "or";
43
14
    } else {
44
2
        return "not";
45
2
    }
46
33
}
47
48
class VCompoundPred : public VectorizedFnCall {
49
    ENABLE_FACTORY_CREATOR(VCompoundPred);
50
51
public:
52
33
    VCompoundPred(const TExprNode& node) : VectorizedFnCall(node) {
53
33
        _op = node.opcode;
54
33
        _fn.name.function_name = compound_operator_to_string(_op);
55
33
        _expr_name = fmt::format("VCompoundPredicate[{}](arguments={},return={})",
56
33
                                 _fn.name.function_name, get_child_names(), _data_type->get_name());
57
33
    }
58
59
#ifdef BE_TEST
60
2
    VCompoundPred() = default;
61
#endif
62
63
18
    const std::string& expr_name() const override { return _expr_name; }
64
0
    Status clone_node(VExprSPtr* cloned_expr) const override {
65
0
        DORIS_CHECK(cloned_expr != nullptr);
66
0
        *cloned_expr = VCompoundPred::create_shared(clone_texpr_node());
67
0
        return Status::OK();
68
0
    }
69
70
    bool can_execute_on_raw_fixed_values(const DataTypePtr& data_type,
71
24
                                         int column_id) const override {
72
24
        return !_children.empty() &&
73
24
               (_op == TExprOpcode::COMPOUND_AND || _op == TExprOpcode::COMPOUND_OR) &&
74
27
               std::ranges::all_of(_children, [&](const VExprSPtr& child) {
75
27
                   return child->can_execute_on_raw_fixed_values(data_type, column_id);
76
27
               });
77
24
    }
78
79
    Status execute_on_raw_fixed_values(const uint8_t* values, size_t num_values, size_t value_width,
80
                                       const DataTypePtr& data_type, int column_id,
81
2
                                       uint8_t* matches) const override {
82
2
        if (!can_execute_on_raw_fixed_values(data_type, column_id)) {
83
0
            return Status::NotSupported("Compound predicate cannot evaluate raw fixed values");
84
0
        }
85
2
        return _execute_raw_compound(
86
4
                num_values, matches, [&](const VExprSPtr& child, uint8_t* child_matches) {
87
4
                    return child->execute_on_raw_fixed_values(values, num_values, value_width,
88
4
                                                              data_type, column_id, child_matches);
89
4
                });
90
2
    }
91
92
    bool can_execute_on_raw_binary_values(const DataTypePtr& data_type,
93
40
                                          int column_id) const override {
94
40
        return !_children.empty() &&
95
40
               (_op == TExprOpcode::COMPOUND_AND || _op == TExprOpcode::COMPOUND_OR) &&
96
80
               std::ranges::all_of(_children, [&](const VExprSPtr& child) {
97
80
                   return child->can_execute_on_raw_binary_values(data_type, column_id);
98
80
               });
99
40
    }
100
101
    Status execute_on_raw_binary_values(const StringRef* values, size_t num_values,
102
                                        const DataTypePtr& data_type, int column_id,
103
8
                                        uint8_t* matches) const override {
104
8
        if (!can_execute_on_raw_binary_values(data_type, column_id)) {
105
0
            return Status::NotSupported("Compound predicate cannot evaluate raw binary values");
106
0
        }
107
8
        return _execute_raw_compound(
108
16
                num_values, matches, [&](const VExprSPtr& child, uint8_t* child_matches) {
109
16
                    return child->execute_on_raw_binary_values(values, num_values, data_type,
110
16
                                                               column_id, child_matches);
111
16
                });
112
8
    }
113
114
12
    bool raw_predicate_result_for_null() const override {
115
12
        if (_op != TExprOpcode::COMPOUND_AND && _op != TExprOpcode::COMPOUND_OR) {
116
            // A Boolean keep bit cannot distinguish FALSE from UNKNOWN, so negating a child's
117
            // collapsed result is not SQL-correct. NOT remains residual and rejects NULL here.
118
0
            return false;
119
0
        }
120
12
        if (_op == TExprOpcode::COMPOUND_AND) {
121
3
            return std::ranges::all_of(_children, [](const VExprSPtr& child) {
122
3
                return child->raw_predicate_result_for_null();
123
3
            });
124
3
        }
125
18
        return std::ranges::any_of(_children, [](const VExprSPtr& child) {
126
18
            return child->raw_predicate_result_for_null();
127
18
        });
128
12
    }
129
130
26
    bool can_evaluate_zonemap_filter() const override {
131
26
        switch (_op) {
132
20
        case TExprOpcode::COMPOUND_AND:
133
40
            return std::ranges::any_of(_children, [](const VExprSPtr& child) {
134
40
                return child->can_evaluate_zonemap_filter();
135
40
            });
136
6
        case TExprOpcode::COMPOUND_OR:
137
12
            return !_children.empty() && std::ranges::all_of(_children, [](const VExprSPtr& child) {
138
12
                return child->can_evaluate_zonemap_filter();
139
12
            });
140
0
        case TExprOpcode::COMPOUND_NOT:
141
0
            return false;
142
0
        default:
143
0
            return false;
144
26
        }
145
26
    }
146
147
6
    ZoneMapFilterResult evaluate_zonemap_filter(const ZoneMapEvalContext& ctx) const override {
148
6
        switch (_op) {
149
2
        case TExprOpcode::COMPOUND_AND: {
150
4
            for (const auto& child : _children) {
151
4
                if (!child->can_evaluate_zonemap_filter()) {
152
1
                    continue;
153
1
                }
154
3
                if (child->evaluate_zonemap_filter(ctx) == ZoneMapFilterResult::kNoMatch) {
155
2
                    return ZoneMapFilterResult::kNoMatch;
156
2
                }
157
3
            }
158
0
            return ZoneMapFilterResult::kMayMatch;
159
2
        }
160
3
        case TExprOpcode::COMPOUND_OR: {
161
5
            for (const auto& child : _children) {
162
5
                DORIS_CHECK(child->can_evaluate_zonemap_filter());
163
5
                if (child->evaluate_zonemap_filter(ctx) != ZoneMapFilterResult::kNoMatch) {
164
2
                    return ZoneMapFilterResult::kMayMatch;
165
2
                }
166
5
            }
167
1
            return ZoneMapFilterResult::kNoMatch;
168
3
        }
169
1
        case TExprOpcode::COMPOUND_NOT:
170
1
            return unsupported_zonemap_filter(ctx);
171
0
        default:
172
0
            return unsupported_zonemap_filter(ctx);
173
6
        }
174
6
    }
175
176
32
    bool can_evaluate_dictionary_filter() const override {
177
32
        switch (_op) {
178
30
        case TExprOpcode::COMPOUND_AND:
179
33
            return std::ranges::any_of(_children, [](const VExprSPtr& child) {
180
33
                return child->can_evaluate_dictionary_filter();
181
33
            });
182
2
        case TExprOpcode::COMPOUND_OR:
183
4
            return !_children.empty() && std::ranges::all_of(_children, [](const VExprSPtr& child) {
184
4
                return child->can_evaluate_dictionary_filter();
185
4
            });
186
0
        default:
187
0
            return false;
188
32
        }
189
32
    }
190
191
8
    bool is_safe_to_execute_on_selected_rows() const override {
192
        // Boolean composition introduces no data-dependent failure of its own. Reuse the generic
193
        // child walk so AND/OR remain eligible only when every nested expression is independently
194
        // safe; applying VectorizedFnCall's scalar-function allowlist to this structural node would
195
        // incorrectly disable selected-row execution for otherwise safe predicates.
196
8
        return VExpr::is_safe_to_execute_on_selected_rows();
197
8
    }
198
199
    ZoneMapFilterResult evaluate_dictionary_filter(
200
21
            const DictionaryEvalContext& ctx) const override {
201
21
        switch (_op) {
202
21
        case TExprOpcode::COMPOUND_AND:
203
29
            for (const auto& child : _children) {
204
29
                if (!child->can_evaluate_dictionary_filter()) {
205
8
                    continue;
206
8
                }
207
21
                if (child->evaluate_dictionary_filter(ctx) == ZoneMapFilterResult::kNoMatch) {
208
13
                    return ZoneMapFilterResult::kNoMatch;
209
13
                }
210
21
            }
211
8
            return ZoneMapFilterResult::kMayMatch;
212
0
        case TExprOpcode::COMPOUND_OR:
213
0
            for (const auto& child : _children) {
214
0
                DORIS_CHECK(child->can_evaluate_dictionary_filter());
215
0
                if (child->evaluate_dictionary_filter(ctx) != ZoneMapFilterResult::kNoMatch) {
216
0
                    return ZoneMapFilterResult::kMayMatch;
217
0
                }
218
0
            }
219
0
            return ZoneMapFilterResult::kNoMatch;
220
0
        default:
221
0
            return ZoneMapFilterResult::kUnsupported;
222
21
        }
223
21
    }
224
225
7
    bool can_evaluate_bloom_filter() const override {
226
7
        switch (_op) {
227
6
        case TExprOpcode::COMPOUND_AND:
228
12
            return std::ranges::any_of(_children, [](const VExprSPtr& child) {
229
12
                return child->can_evaluate_bloom_filter();
230
12
            });
231
1
        case TExprOpcode::COMPOUND_OR:
232
1
            return !_children.empty() && std::ranges::all_of(_children, [](const VExprSPtr& child) {
233
1
                return child->can_evaluate_bloom_filter();
234
1
            });
235
0
        default:
236
0
            return false;
237
7
        }
238
7
    }
239
240
0
    ZoneMapFilterResult evaluate_bloom_filter(const BloomFilterEvalContext& ctx) const override {
241
0
        switch (_op) {
242
0
        case TExprOpcode::COMPOUND_AND:
243
0
            for (const auto& child : _children) {
244
0
                if (!child->can_evaluate_bloom_filter()) {
245
0
                    continue;
246
0
                }
247
0
                if (child->evaluate_bloom_filter(ctx) == ZoneMapFilterResult::kNoMatch) {
248
0
                    return ZoneMapFilterResult::kNoMatch;
249
0
                }
250
0
            }
251
0
            return ZoneMapFilterResult::kMayMatch;
252
0
        case TExprOpcode::COMPOUND_OR:
253
0
            for (const auto& child : _children) {
254
0
                DORIS_CHECK(child->can_evaluate_bloom_filter());
255
0
                if (child->evaluate_bloom_filter(ctx) != ZoneMapFilterResult::kNoMatch) {
256
0
                    return ZoneMapFilterResult::kMayMatch;
257
0
                }
258
0
            }
259
0
            return ZoneMapFilterResult::kNoMatch;
260
0
        default:
261
0
            return ZoneMapFilterResult::kUnsupported;
262
0
        }
263
0
    }
264
265
0
    Status evaluate_inverted_index(VExprContext* context, uint32_t segment_num_rows) override {
266
0
        segment_v2::InvertedIndexResultBitmap res;
267
0
        bool all_pass = true;
268
269
0
        switch (_op) {
270
0
        case TExprOpcode::COMPOUND_OR: {
271
0
            for (const auto& child : _children) {
272
0
                if (Status st = child->evaluate_inverted_index(context, segment_num_rows);
273
0
                    !st.ok()) {
274
0
                    LOG(ERROR) << "expr:" << child->expr_name()
275
0
                               << " evaluate_inverted_index error:" << st.to_string();
276
0
                    all_pass = false;
277
0
                    continue;
278
0
                }
279
0
                auto inverted_index_context = context->get_index_context();
280
0
                if (inverted_index_context->has_index_result_for_expr(child.get())) {
281
0
                    const auto* index_result =
282
0
                            inverted_index_context->get_index_result_for_expr(child.get());
283
0
                    if (res.is_empty()) {
284
0
                        res = *index_result;
285
0
                    } else {
286
0
                        res |= *index_result;
287
0
                    }
288
0
                    if (inverted_index_context->get_score_runtime() == nullptr) {
289
0
                        if (res.get_data_bitmap()->cardinality() == segment_num_rows) {
290
0
                            break; // Early exit if result is full
291
0
                        }
292
0
                    }
293
0
                } else {
294
0
                    all_pass = false;
295
0
                }
296
0
            }
297
0
            break;
298
0
        }
299
0
        case TExprOpcode::COMPOUND_AND: {
300
0
            for (const auto& child : _children) {
301
0
                if (Status st = child->evaluate_inverted_index(context, segment_num_rows);
302
0
                    !st.ok()) {
303
0
                    LOG(ERROR) << "expr:" << child->expr_name()
304
0
                               << " evaluate_inverted_index error:" << st.to_string();
305
0
                    all_pass = false;
306
0
                    continue;
307
0
                }
308
0
                if (context->get_index_context()->has_index_result_for_expr(child.get())) {
309
0
                    const auto* index_result =
310
0
                            context->get_index_context()->get_index_result_for_expr(child.get());
311
0
                    if (res.is_empty()) {
312
0
                        res = *index_result;
313
0
                    } else {
314
0
                        res &= *index_result;
315
0
                    }
316
317
0
                    if (res.get_data_bitmap()->isEmpty()) {
318
0
                        break; // Early exit if result is empty
319
0
                    }
320
0
                } else {
321
0
                    all_pass = false;
322
0
                }
323
0
            }
324
0
            break;
325
0
        }
326
0
        case TExprOpcode::COMPOUND_NOT: {
327
0
            const auto& child = _children[0];
328
0
            Status st = child->evaluate_inverted_index(context, segment_num_rows);
329
0
            if (!st.ok()) {
330
0
                LOG(ERROR) << "expr:" << child->expr_name()
331
0
                           << " evaluate_inverted_index error:" << st.to_string();
332
0
                return st;
333
0
            }
334
335
0
            if (context->get_index_context()->has_index_result_for_expr(child.get())) {
336
0
                const auto* index_result =
337
0
                        context->get_index_context()->get_index_result_for_expr(child.get());
338
0
                roaring::Roaring full_result;
339
0
                full_result.addRange(0, segment_num_rows);
340
0
                res = index_result->op_not(&full_result);
341
0
            } else {
342
0
                all_pass = false;
343
0
            }
344
0
            break;
345
0
        }
346
0
        default:
347
0
            return Status::NotSupported(
348
0
                    "Compound operator must be AND, OR, or NOT to execute with inverted index.");
349
0
        }
350
351
0
        if (all_pass && !res.is_empty()) {
352
0
            context->get_index_context()->set_index_result_for_expr(this, res);
353
0
        }
354
0
        return Status::OK();
355
0
    }
356
357
    Status execute_column(VExprContext* context, const Block* block, Selector* selector,
358
1
                          size_t count, ColumnPtr& result_column) const override {
359
1
        if (fast_execute(context, selector, count, result_column)) {
360
0
            return Status::OK();
361
0
        }
362
1
        if (get_num_children() == 1 || _has_const_child()) {
363
0
            return VectorizedFnCall::execute_column(context, block, selector, count, 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: _ZZNK5doris13VCompoundPred14execute_columnEPNS_12VExprContextEPKNS_5BlockEPNS_8PODArrayIjLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEEmRNS_3COWINS_7IColumnEE13immutable_ptrISD_EEENKUlTnbvE_clILb1EEEDav
Unexecuted instantiation: _ZZNK5doris13VCompoundPred14execute_columnEPNS_12VExprContextEPKNS_5BlockEPNS_8PODArrayIjLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEEmRNS_3COWINS_7IColumnEE13immutable_ptrISD_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: _ZZNK5doris13VCompoundPred14execute_columnEPNS_12VExprContextEPKNS_5BlockEPNS_8PODArrayIjLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEEmRNS_3COWINS_7IColumnEE13immutable_ptrISD_EEENKUlTnbvE0_clILb1EEEDav
Unexecuted instantiation: _ZZNK5doris13VCompoundPred14execute_columnEPNS_12VExprContextEPKNS_5BlockEPNS_8PODArrayIjLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEEmRNS_3COWINS_7IColumnEE13immutable_ptrISD_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
#include "common/compile_check_end.h"
666
} // namespace doris