Coverage Report

Created: 2026-07-29 07:07

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
10.7k
inline std::string compound_operator_to_string(TExprOpcode::type op) {
38
10.7k
    if (op == TExprOpcode::COMPOUND_AND) {
39
2.16k
        return "and";
40
8.56k
    } else if (op == TExprOpcode::COMPOUND_OR) {
41
7.87k
        return "or";
42
7.87k
    } else {
43
695
        return "not";
44
695
    }
45
10.7k
}
46
47
class VCompoundPred : public VectorizedFnCall {
48
    ENABLE_FACTORY_CREATOR(VCompoundPred);
49
50
public:
51
10.7k
    VCompoundPred(const TExprNode& node) : VectorizedFnCall(node) {
52
10.7k
        _op = node.opcode;
53
10.7k
        _fn.name.function_name = compound_operator_to_string(_op);
54
10.7k
        _expr_name = fmt::format("VCompoundPredicate[{}](arguments={},return={})",
55
10.7k
                                 _fn.name.function_name, get_child_names(), _data_type->get_name());
56
10.7k
    }
57
58
#ifdef BE_TEST
59
    VCompoundPred() = default;
60
#endif
61
62
16.1k
    const std::string& expr_name() const override { return _expr_name; }
63
4.32k
    Status clone_node(VExprSPtr* cloned_expr) const override {
64
4.32k
        DORIS_CHECK(cloned_expr != nullptr);
65
4.32k
        *cloned_expr = VCompoundPred::create_shared(clone_texpr_node());
66
4.32k
        return Status::OK();
67
4.32k
    }
68
69
    bool can_execute_on_raw_fixed_values(const DataTypePtr& data_type,
70
44
                                         int column_id) const override {
71
44
        return !_children.empty() &&
72
44
               (_op == TExprOpcode::COMPOUND_AND || _op == TExprOpcode::COMPOUND_OR) &&
73
57
               std::ranges::all_of(_children, [&](const VExprSPtr& child) {
74
57
                   return child->can_execute_on_raw_fixed_values(data_type, column_id);
75
57
               });
76
44
    }
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
60
                                          int column_id) const override {
93
60
        return !_children.empty() &&
94
60
               (_op == TExprOpcode::COMPOUND_AND || _op == TExprOpcode::COMPOUND_OR) &&
95
110
               std::ranges::all_of(_children, [&](const VExprSPtr& child) {
96
110
                   return child->can_execute_on_raw_binary_values(data_type, column_id);
97
110
               });
98
60
    }
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
52
    bool raw_predicate_result_for_null() const override {
114
52
        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
52
        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
98
        return std::ranges::any_of(_children, [](const VExprSPtr& child) {
125
98
            return child->raw_predicate_result_for_null();
126
98
        });
127
52
    }
128
129
16.7k
    bool can_evaluate_zonemap_filter() const override {
130
16.7k
        switch (_op) {
131
3.40k
        case TExprOpcode::COMPOUND_AND:
132
4.01k
            return std::ranges::any_of(_children, [](const VExprSPtr& child) {
133
4.01k
                return child->can_evaluate_zonemap_filter();
134
4.01k
            });
135
10.3k
        case TExprOpcode::COMPOUND_OR:
136
17.4k
            return !_children.empty() && std::ranges::all_of(_children, [](const VExprSPtr& child) {
137
17.4k
                return child->can_evaluate_zonemap_filter();
138
17.4k
            });
139
3.02k
        case TExprOpcode::COMPOUND_NOT:
140
3.02k
            return false;
141
0
        default:
142
0
            return false;
143
16.7k
        }
144
16.7k
    }
145
146
2.98k
    ZoneMapFilterResult evaluate_zonemap_filter(const ZoneMapEvalContext& ctx) const override {
147
2.98k
        switch (_op) {
148
439
        case TExprOpcode::COMPOUND_AND: {
149
787
            for (const auto& child : _children) {
150
787
                if (!child->can_evaluate_zonemap_filter()) {
151
76
                    continue;
152
76
                }
153
711
                if (child->evaluate_zonemap_filter(ctx) == ZoneMapFilterResult::kNoMatch) {
154
146
                    return ZoneMapFilterResult::kNoMatch;
155
146
                }
156
711
            }
157
293
            return ZoneMapFilterResult::kMayMatch;
158
439
        }
159
2.54k
        case TExprOpcode::COMPOUND_OR: {
160
4.19k
            for (const auto& child : _children) {
161
4.19k
                DORIS_CHECK(child->can_evaluate_zonemap_filter());
162
4.19k
                if (child->evaluate_zonemap_filter(ctx) != ZoneMapFilterResult::kNoMatch) {
163
2.16k
                    return ZoneMapFilterResult::kMayMatch;
164
2.16k
                }
165
4.19k
            }
166
384
            return ZoneMapFilterResult::kNoMatch;
167
2.54k
        }
168
1
        case TExprOpcode::COMPOUND_NOT:
169
1
            return unsupported_zonemap_filter(ctx);
170
0
        default:
171
0
            return unsupported_zonemap_filter(ctx);
172
2.98k
        }
173
2.98k
    }
174
175
364
    bool can_evaluate_dictionary_filter() const override {
176
364
        switch (_op) {
177
134
        case TExprOpcode::COMPOUND_AND:
178
137
            return std::ranges::any_of(_children, [](const VExprSPtr& child) {
179
137
                return child->can_evaluate_dictionary_filter();
180
137
            });
181
230
        case TExprOpcode::COMPOUND_OR:
182
432
            return !_children.empty() && std::ranges::all_of(_children, [](const VExprSPtr& child) {
183
432
                return child->can_evaluate_dictionary_filter();
184
432
            });
185
0
        default:
186
0
            return false;
187
364
        }
188
364
    }
189
190
2.05k
    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
2.05k
        return VExpr::is_safe_to_execute_on_selected_rows();
196
2.05k
    }
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
305
    bool can_evaluate_bloom_filter() const override {
225
305
        switch (_op) {
226
96
        case TExprOpcode::COMPOUND_AND:
227
112
            return std::ranges::any_of(_children, [](const VExprSPtr& child) {
228
112
                return child->can_evaluate_bloom_filter();
229
112
            });
230
209
        case TExprOpcode::COMPOUND_OR:
231
335
            return !_children.empty() && std::ranges::all_of(_children, [](const VExprSPtr& child) {
232
335
                return child->can_evaluate_bloom_filter();
233
335
            });
234
0
        default:
235
0
            return false;
236
305
        }
237
305
    }
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
5.11k
    Status evaluate_inverted_index(VExprContext* context, uint32_t segment_num_rows) override {
265
5.11k
        segment_v2::InvertedIndexResultBitmap res;
266
5.11k
        bool all_pass = true;
267
268
5.11k
        switch (_op) {
269
3.05k
        case TExprOpcode::COMPOUND_OR: {
270
5.90k
            for (const auto& child : _children) {
271
5.90k
                if (Status st = child->evaluate_inverted_index(context, segment_num_rows);
272
5.90k
                    !st.ok()) {
273
183
                    LOG(ERROR) << "expr:" << child->expr_name()
274
183
                               << " evaluate_inverted_index error:" << st.to_string();
275
183
                    all_pass = false;
276
183
                    continue;
277
183
                }
278
5.71k
                auto inverted_index_context = context->get_index_context();
279
5.71k
                if (inverted_index_context->has_index_result_for_expr(child.get())) {
280
3.00k
                    const auto* index_result =
281
3.00k
                            inverted_index_context->get_index_result_for_expr(child.get());
282
3.00k
                    if (res.is_empty()) {
283
2.02k
                        res = *index_result;
284
2.02k
                    } else {
285
979
                        res |= *index_result;
286
979
                    }
287
3.01k
                    if (inverted_index_context->get_score_runtime() == nullptr) {
288
3.01k
                        if (res.get_data_bitmap()->cardinality() == segment_num_rows) {
289
350
                            break; // Early exit if result is full
290
350
                        }
291
3.01k
                    }
292
3.00k
                } else {
293
2.71k
                    all_pass = false;
294
2.71k
                }
295
5.71k
            }
296
3.05k
            break;
297
0
        }
298
5.36k
        case TExprOpcode::COMPOUND_AND: {
299
1.52k
            for (const auto& child : _children) {
300
1.52k
                if (Status st = child->evaluate_inverted_index(context, segment_num_rows);
301
1.52k
                    !st.ok()) {
302
23
                    LOG(ERROR) << "expr:" << child->expr_name()
303
23
                               << " evaluate_inverted_index error:" << st.to_string();
304
23
                    all_pass = false;
305
23
                    continue;
306
23
                }
307
1.49k
                if (context->get_index_context()->has_index_result_for_expr(child.get())) {
308
233
                    const auto* index_result =
309
233
                            context->get_index_context()->get_index_result_for_expr(child.get());
310
233
                    if (res.is_empty()) {
311
173
                        res = *index_result;
312
173
                    } else {
313
60
                        res &= *index_result;
314
60
                    }
315
316
233
                    if (res.get_data_bitmap()->isEmpty()) {
317
89
                        break; // Early exit if result is empty
318
89
                    }
319
1.26k
                } else {
320
1.26k
                    all_pass = false;
321
1.26k
                }
322
1.49k
            }
323
804
            break;
324
0
        }
325
1.41k
        case TExprOpcode::COMPOUND_NOT: {
326
1.24k
            const auto& child = _children[0];
327
1.24k
            Status st = child->evaluate_inverted_index(context, segment_num_rows);
328
1.24k
            if (!st.ok()) {
329
35
                LOG(ERROR) << "expr:" << child->expr_name()
330
35
                           << " evaluate_inverted_index error:" << st.to_string();
331
35
                return st;
332
35
            }
333
334
1.21k
            if (context->get_index_context()->has_index_result_for_expr(child.get())) {
335
685
                const auto* index_result =
336
685
                        context->get_index_context()->get_index_result_for_expr(child.get());
337
685
                roaring::Roaring full_result;
338
685
                full_result.addRange(0, segment_num_rows);
339
685
                res = index_result->op_not(&full_result);
340
685
            } else {
341
527
                all_pass = false;
342
527
            }
343
1.21k
            break;
344
1.24k
        }
345
0
        default:
346
0
            return Status::NotSupported(
347
0
                    "Compound operator must be AND, OR, or NOT to execute with inverted index.");
348
5.11k
        }
349
350
5.08k
        if (all_pass && !res.is_empty()) {
351
2.03k
            context->get_index_context()->set_index_result_for_expr(this, res);
352
2.03k
        }
353
5.08k
        return Status::OK();
354
5.11k
    }
355
356
    Status execute_column_impl(VExprContext* context, const Block* block, const Selector* selector,
357
46.6k
                               size_t count, ColumnPtr& result_column) const override {
358
46.6k
        if (fast_execute(context, selector, count, result_column)) {
359
90
            return Status::OK();
360
90
        }
361
46.5k
        if (get_num_children() == 1 || _has_const_child()) {
362
1.29k
            return VectorizedFnCall::execute_column_impl(context, block, selector, count,
363
1.29k
                                                         result_column);
364
1.29k
        }
365
366
45.3k
        ColumnPtr lhs_column;
367
45.3k
        RETURN_IF_ERROR(_children[0]->execute_column(context, block, selector, count, lhs_column));
368
45.3k
        lhs_column = lhs_column->convert_to_full_column_if_const();
369
45.3k
        size_t size = lhs_column->size();
370
371
45.3k
        bool lhs_is_nullable = lhs_column->is_nullable();
372
45.3k
        auto [lhs_data_column, lhs_null_map] =
373
45.3k
                _get_raw_data_and_null_map(lhs_column, lhs_is_nullable);
374
45.3k
        size_t filted = simd::count_zero_num((int8_t*)lhs_data_column, size);
375
45.3k
        bool lhs_all_true = (filted == 0);
376
45.3k
        bool lhs_all_false = (filted == size);
377
378
45.3k
        bool lhs_all_is_not_null = false;
379
45.3k
        if (lhs_is_nullable) {
380
31.7k
            filted = simd::count_zero_num((int8_t*)lhs_null_map, size);
381
31.7k
            lhs_all_is_not_null = (filted == size);
382
31.7k
        }
383
384
45.3k
        ColumnPtr rhs_column = nullptr;
385
45.3k
        const uint8_t* __restrict rhs_data_column = nullptr;
386
45.3k
        const uint8_t* __restrict rhs_null_map = nullptr;
387
45.3k
        bool rhs_is_nullable = false;
388
45.3k
        bool rhs_all_true = false;
389
45.3k
        bool rhs_all_false = false;
390
45.3k
        bool rhs_all_is_not_null = false;
391
45.3k
        bool result_is_nullable = _data_type->is_nullable();
392
393
45.3k
        auto get_rhs_colum = [&]() {
394
31.8k
            if (!rhs_column) {
395
31.8k
                RETURN_IF_ERROR(
396
31.8k
                        _children[1]->execute_column(context, block, selector, count, rhs_column));
397
31.8k
                rhs_column = rhs_column->convert_to_full_column_if_const();
398
31.8k
                rhs_is_nullable = rhs_column->is_nullable();
399
31.8k
                auto rhs_nullable_column = _get_raw_data_and_null_map(rhs_column, rhs_is_nullable);
400
31.8k
                rhs_data_column = rhs_nullable_column.first;
401
31.8k
                rhs_null_map = rhs_nullable_column.second;
402
31.8k
                size_t filted = simd::count_zero_num((int8_t*)rhs_data_column, size);
403
31.8k
                rhs_all_true = (filted == 0);
404
31.8k
                rhs_all_false = (filted == size);
405
31.8k
                if (rhs_is_nullable) {
406
25.1k
                    filted = simd::count_zero_num((int8_t*)rhs_null_map, size);
407
25.1k
                    rhs_all_is_not_null = (filted == size);
408
25.1k
                }
409
31.8k
            }
410
31.8k
            return Status::OK();
411
31.8k
        };
412
413
45.3k
        auto return_result_column_id = [&](ColumnPtr& arg_column) {
414
38.4k
            result_column = std::move(*arg_column).mutate();
415
38.4k
            if (result_is_nullable && !result_column->is_nullable()) {
416
2.51k
                result_column = make_nullable(result_column);
417
2.51k
            }
418
38.4k
        };
419
420
45.3k
        auto create_null_map_column = [&](ColumnPtr& null_map_column,
421
45.3k
                                          const uint8_t* __restrict null_map_data) {
422
12.4k
            if (null_map_data == nullptr) {
423
779
                null_map_column = ColumnUInt8::create(size, 0);
424
779
                null_map_data =
425
779
                        assert_cast<const ColumnUInt8*>(null_map_column.get())->get_data().data();
426
779
            }
427
12.4k
            return null_map_data;
428
12.4k
        };
429
430
45.3k
        auto vector_vector = [&]<bool is_and_op>() {
431
626
            MutableColumnPtr mutable_result_column;
432
626
            uint8_t* __restrict result_data_column = nullptr;
433
626
            const uint8_t* __restrict other_data_column = rhs_data_column;
434
626
            if (lhs_column->use_count() == 1) {
435
618
                mutable_result_column = IColumn::mutate(std::move(lhs_column));
436
618
                result_data_column =
437
618
                        assert_cast<ColumnUInt8*>(mutable_result_column.get())->get_data().data();
438
618
            } else if (rhs_column->use_count() == 1) {
439
3
                mutable_result_column = IColumn::mutate(std::move(rhs_column));
440
3
                result_data_column =
441
3
                        assert_cast<ColumnUInt8*>(mutable_result_column.get())->get_data().data();
442
3
                other_data_column = lhs_data_column;
443
5
            } else {
444
5
                mutable_result_column = lhs_column->clone_resized(size);
445
5
                result_data_column =
446
5
                        assert_cast<ColumnUInt8*>(mutable_result_column.get())->get_data().data();
447
5
            }
448
449
626
            do_not_null_pred<is_and_op>(result_data_column, other_data_column, size);
450
626
            result_column = std::move(mutable_result_column);
451
626
        };
_ZZNK5doris13VCompoundPred19execute_column_implEPNS_12VExprContextEPKNS_5BlockEPKNS_8PODArrayIjLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEEmRNS_3COWINS_7IColumnEE13immutable_ptrISE_EEENKUlTnbvE_clILb1EEEDav
Line
Count
Source
430
490
        auto vector_vector = [&]<bool is_and_op>() {
431
490
            MutableColumnPtr mutable_result_column;
432
490
            uint8_t* __restrict result_data_column = nullptr;
433
490
            const uint8_t* __restrict other_data_column = rhs_data_column;
434
490
            if (lhs_column->use_count() == 1) {
435
490
                mutable_result_column = IColumn::mutate(std::move(lhs_column));
436
490
                result_data_column =
437
490
                        assert_cast<ColumnUInt8*>(mutable_result_column.get())->get_data().data();
438
490
            } 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
490
            do_not_null_pred<is_and_op>(result_data_column, other_data_column, size);
450
490
            result_column = std::move(mutable_result_column);
451
490
        };
_ZZNK5doris13VCompoundPred19execute_column_implEPNS_12VExprContextEPKNS_5BlockEPKNS_8PODArrayIjLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEEmRNS_3COWINS_7IColumnEE13immutable_ptrISE_EEENKUlTnbvE_clILb0EEEDav
Line
Count
Source
430
136
        auto vector_vector = [&]<bool is_and_op>() {
431
136
            MutableColumnPtr mutable_result_column;
432
136
            uint8_t* __restrict result_data_column = nullptr;
433
136
            const uint8_t* __restrict other_data_column = rhs_data_column;
434
136
            if (lhs_column->use_count() == 1) {
435
128
                mutable_result_column = IColumn::mutate(std::move(lhs_column));
436
128
                result_data_column =
437
128
                        assert_cast<ColumnUInt8*>(mutable_result_column.get())->get_data().data();
438
128
            } else if (rhs_column->use_count() == 1) {
439
3
                mutable_result_column = IColumn::mutate(std::move(rhs_column));
440
3
                result_data_column =
441
3
                        assert_cast<ColumnUInt8*>(mutable_result_column.get())->get_data().data();
442
3
                other_data_column = lhs_data_column;
443
5
            } else {
444
5
                mutable_result_column = lhs_column->clone_resized(size);
445
5
                result_data_column =
446
5
                        assert_cast<ColumnUInt8*>(mutable_result_column.get())->get_data().data();
447
5
            }
448
449
136
            do_not_null_pred<is_and_op>(result_data_column, other_data_column, size);
450
136
            result_column = std::move(mutable_result_column);
451
136
        };
452
45.3k
        auto vector_vector_null = [&]<bool is_and_op>() {
453
6.20k
            auto col_res = ColumnUInt8::create(size);
454
6.20k
            auto col_nulls = ColumnUInt8::create(size);
455
456
6.20k
            auto* __restrict res_datas = col_res->get_data().data();
457
6.20k
            auto* __restrict res_nulls = col_nulls->get_data().data();
458
6.20k
            ColumnPtr temp_null_map = nullptr;
459
            // maybe both children are nullable / or one of children is nullable
460
6.20k
            auto* __restrict lhs_null_map_tmp = create_null_map_column(temp_null_map, lhs_null_map);
461
6.20k
            auto* __restrict rhs_null_map_tmp = create_null_map_column(temp_null_map, rhs_null_map);
462
6.20k
            auto* __restrict lhs_data_column_tmp = lhs_data_column;
463
6.20k
            auto* __restrict rhs_data_column_tmp = rhs_data_column;
464
465
6.20k
            do_null_pred<is_and_op>(lhs_data_column_tmp, lhs_null_map_tmp, rhs_data_column_tmp,
466
6.20k
                                    rhs_null_map_tmp, res_datas, res_nulls, size);
467
468
6.20k
            result_column = ColumnNullable::create(std::move(col_res), std::move(col_nulls));
469
6.20k
        };
_ZZNK5doris13VCompoundPred19execute_column_implEPNS_12VExprContextEPKNS_5BlockEPKNS_8PODArrayIjLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEEmRNS_3COWINS_7IColumnEE13immutable_ptrISE_EEENKUlTnbvE0_clILb1EEEDav
Line
Count
Source
452
2.35k
        auto vector_vector_null = [&]<bool is_and_op>() {
453
2.35k
            auto col_res = ColumnUInt8::create(size);
454
2.35k
            auto col_nulls = ColumnUInt8::create(size);
455
456
2.35k
            auto* __restrict res_datas = col_res->get_data().data();
457
2.35k
            auto* __restrict res_nulls = col_nulls->get_data().data();
458
2.35k
            ColumnPtr temp_null_map = nullptr;
459
            // maybe both children are nullable / or one of children is nullable
460
2.35k
            auto* __restrict lhs_null_map_tmp = create_null_map_column(temp_null_map, lhs_null_map);
461
2.35k
            auto* __restrict rhs_null_map_tmp = create_null_map_column(temp_null_map, rhs_null_map);
462
2.35k
            auto* __restrict lhs_data_column_tmp = lhs_data_column;
463
2.35k
            auto* __restrict rhs_data_column_tmp = rhs_data_column;
464
465
2.35k
            do_null_pred<is_and_op>(lhs_data_column_tmp, lhs_null_map_tmp, rhs_data_column_tmp,
466
2.35k
                                    rhs_null_map_tmp, res_datas, res_nulls, size);
467
468
2.35k
            result_column = ColumnNullable::create(std::move(col_res), std::move(col_nulls));
469
2.35k
        };
_ZZNK5doris13VCompoundPred19execute_column_implEPNS_12VExprContextEPKNS_5BlockEPKNS_8PODArrayIjLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEEmRNS_3COWINS_7IColumnEE13immutable_ptrISE_EEENKUlTnbvE0_clILb0EEEDav
Line
Count
Source
452
3.84k
        auto vector_vector_null = [&]<bool is_and_op>() {
453
3.84k
            auto col_res = ColumnUInt8::create(size);
454
3.84k
            auto col_nulls = ColumnUInt8::create(size);
455
456
3.84k
            auto* __restrict res_datas = col_res->get_data().data();
457
3.84k
            auto* __restrict res_nulls = col_nulls->get_data().data();
458
3.84k
            ColumnPtr temp_null_map = nullptr;
459
            // maybe both children are nullable / or one of children is nullable
460
3.84k
            auto* __restrict lhs_null_map_tmp = create_null_map_column(temp_null_map, lhs_null_map);
461
3.84k
            auto* __restrict rhs_null_map_tmp = create_null_map_column(temp_null_map, rhs_null_map);
462
3.84k
            auto* __restrict lhs_data_column_tmp = lhs_data_column;
463
3.84k
            auto* __restrict rhs_data_column_tmp = rhs_data_column;
464
465
3.84k
            do_null_pred<is_and_op>(lhs_data_column_tmp, lhs_null_map_tmp, rhs_data_column_tmp,
466
3.84k
                                    rhs_null_map_tmp, res_datas, res_nulls, size);
467
468
3.84k
            result_column = ColumnNullable::create(std::move(col_res), std::move(col_nulls));
469
3.84k
        };
470
471
        // false and NULL ----> 0
472
        // true  and NULL ----> NULL
473
45.3k
        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
15.2k
            if ((lhs_all_false && !lhs_is_nullable) || (lhs_all_false && lhs_all_is_not_null)) {
477
                // false and any = false, return lhs
478
9.74k
                return_result_column_id(lhs_column);
479
9.74k
            } else {
480
5.49k
                RETURN_IF_ERROR(get_rhs_colum());
481
482
5.48k
                if ((lhs_all_true && !lhs_is_nullable) ||    //not null column
483
5.48k
                    (lhs_all_true && lhs_all_is_not_null)) { //nullable column
484
                                                             // true and any = any, return rhs
485
486
750
                    return_result_column_id(rhs_column);
487
4.73k
                } else if ((rhs_all_false && !rhs_is_nullable) ||
488
4.73k
                           (rhs_all_false && rhs_all_is_not_null)) {
489
                    // any and false = false, return rhs
490
450
                    return_result_column_id(rhs_column);
491
4.28k
                } else if ((rhs_all_true && !rhs_is_nullable) ||
492
4.28k
                           (rhs_all_true && rhs_all_is_not_null)) {
493
                    // any and true = any, return lhs
494
1.44k
                    return_result_column_id(lhs_column);
495
2.84k
                } else {
496
2.84k
                    if (!result_is_nullable) {
497
489
                        vector_vector.template operator()<true>();
498
2.35k
                    } else {
499
2.35k
                        vector_vector_null.template operator()<true>();
500
2.35k
                    }
501
2.84k
                }
502
5.48k
            }
503
30.0k
        } else if (_op == TExprOpcode::COMPOUND_OR) {
504
            // true  or NULL ----> 1
505
            // false or NULL ----> NULL
506
30.0k
            if ((lhs_all_true && !lhs_is_nullable) || (lhs_all_true && lhs_all_is_not_null)) {
507
                // true or any = true, return lhs
508
3.74k
                return_result_column_id(lhs_column);
509
26.3k
            } else {
510
26.3k
                RETURN_IF_ERROR(get_rhs_colum());
511
26.3k
                if ((lhs_all_false && !lhs_is_nullable) || (lhs_all_false && lhs_all_is_not_null)) {
512
                    // false or any = any, return rhs
513
19.9k
                    return_result_column_id(rhs_column);
514
19.9k
                } else if ((rhs_all_true && !rhs_is_nullable) ||
515
6.35k
                           (rhs_all_true && rhs_all_is_not_null)) {
516
                    // any or true = true, return rhs
517
801
                    return_result_column_id(rhs_column);
518
5.55k
                } else if ((rhs_all_false && !rhs_is_nullable) ||
519
5.55k
                           (rhs_all_false && rhs_all_is_not_null)) {
520
                    // any or false = any, return lhs
521
1.57k
                    return_result_column_id(lhs_column);
522
3.97k
                } else {
523
3.97k
                    if (!result_is_nullable) {
524
136
                        vector_vector.template operator()<false>();
525
3.84k
                    } else {
526
3.84k
                        vector_vector_null.template operator()<false>();
527
3.84k
                    }
528
3.97k
                }
529
26.3k
            }
530
30.0k
        } else {
531
18
            return Status::InternalError("Compound operator must be AND or OR.");
532
18
        }
533
534
45.3k
        DCHECK_EQ(result_column->size(), count);
535
45.2k
        return Status::OK();
536
45.3k
    }
537
538
1.06k
    double execute_cost() const override {
539
1.06k
        double cost = 0.3;
540
1.72k
        for (const auto& child : _children) {
541
1.72k
            cost += child->execute_cost();
542
1.72k
        }
543
1.06k
        return cost;
544
1.06k
    }
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
160k
    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
160k
        return (l_null & r_null) | (r_null & (l_null ^ a)) | (l_null & (r_null ^ b));
593
160k
    }
594
502k
    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
502k
        return (l_null & r_null) | (r_null & (r_null ^ a)) | (l_null & (l_null ^ b));
597
502k
    }
598
599
    template <bool is_and>
600
    void static do_not_null_pred(uint8_t* __restrict lhs, const uint8_t* __restrict rhs,
601
626
                                 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
69.9k
        for (size_t i = 0; i < size; ++i) {
610
69.2k
            if constexpr (is_and) {
611
65.5k
                lhs[i] &= rhs[i];
612
65.5k
            } else {
613
3.69k
                lhs[i] |= rhs[i];
614
3.69k
            }
615
69.2k
        }
616
626
    }
_ZN5doris13VCompoundPred16do_not_null_predILb1EEEvPhPKhm
Line
Count
Source
601
490
                                 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
66.0k
        for (size_t i = 0; i < size; ++i) {
610
65.5k
            if constexpr (is_and) {
611
65.5k
                lhs[i] &= rhs[i];
612
            } else {
613
                lhs[i] |= rhs[i];
614
            }
615
65.5k
        }
616
490
    }
_ZN5doris13VCompoundPred16do_not_null_predILb0EEEvPhPKhm
Line
Count
Source
601
136
                                 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
3.83k
        for (size_t i = 0; i < size; ++i) {
610
            if constexpr (is_and) {
611
                lhs[i] &= rhs[i];
612
3.69k
            } else {
613
3.69k
                lhs[i] |= rhs[i];
614
3.69k
            }
615
3.69k
        }
616
136
    }
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
6.20k
                             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
668k
        for (size_t i = 0; i < size; ++i) {
631
662k
            if constexpr (is_and) {
632
160k
                res_null[i] = apply_and_null(lhs_data[i], lhs_null[i], rhs_data[i], rhs_null[i]);
633
160k
                res_data[i] = lhs_data[i] & rhs_data[i];
634
502k
            } else {
635
502k
                res_null[i] = apply_or_null(lhs_data[i], lhs_null[i], rhs_data[i], rhs_null[i]);
636
502k
                res_data[i] = lhs_data[i] | rhs_data[i];
637
502k
            }
638
662k
        }
639
6.20k
    }
_ZN5doris13VCompoundPred12do_null_predILb1EEEvPKhS3_S3_S3_PhS4_m
Line
Count
Source
622
2.35k
                             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
162k
        for (size_t i = 0; i < size; ++i) {
631
160k
            if constexpr (is_and) {
632
160k
                res_null[i] = apply_and_null(lhs_data[i], lhs_null[i], rhs_data[i], rhs_null[i]);
633
160k
                res_data[i] = lhs_data[i] & rhs_data[i];
634
            } else {
635
                res_null[i] = apply_or_null(lhs_data[i], lhs_null[i], rhs_data[i], rhs_null[i]);
636
                res_data[i] = lhs_data[i] | rhs_data[i];
637
            }
638
160k
        }
639
2.35k
    }
_ZN5doris13VCompoundPred12do_null_predILb0EEEvPKhS3_S3_S3_PhS4_m
Line
Count
Source
622
3.84k
                             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
505k
        for (size_t i = 0; i < size; ++i) {
631
            if constexpr (is_and) {
632
                res_null[i] = apply_and_null(lhs_data[i], lhs_null[i], rhs_data[i], rhs_null[i]);
633
                res_data[i] = lhs_data[i] & rhs_data[i];
634
502k
            } else {
635
502k
                res_null[i] = apply_or_null(lhs_data[i], lhs_null[i], rhs_data[i], rhs_null[i]);
636
502k
                res_data[i] = lhs_data[i] | rhs_data[i];
637
502k
            }
638
502k
        }
639
3.84k
    }
640
641
45.3k
    bool _has_const_child() const {
642
45.3k
        return std::ranges::any_of(_children,
643
90.6k
                                   [](const VExprSPtr& arg) -> bool { return arg->is_constant(); });
644
45.3k
    }
645
646
    std::pair<const uint8_t*, const uint8_t*> _get_raw_data_and_null_map(
647
77.0k
            const ColumnPtr& column, bool has_nullable_column) const {
648
77.0k
        if (has_nullable_column) {
649
56.9k
            const auto* nullable_column = assert_cast<const ColumnNullable*>(column.get());
650
56.9k
            auto* data_column =
651
56.9k
                    assert_cast<const ColumnUInt8*>(nullable_column->get_nested_column_ptr().get())
652
56.9k
                            ->get_data()
653
56.9k
                            .data();
654
56.9k
            auto* null_map = nullable_column->get_null_map_column_ptr()->get_data().data();
655
56.9k
            return std::make_pair(data_column, null_map);
656
56.9k
        } else {
657
20.1k
            auto* data_column = assert_cast<const ColumnUInt8*>(column.get())->get_data().data();
658
20.1k
            return std::make_pair(data_column, nullptr);
659
20.1k
        }
660
77.0k
    }
661
662
    TExprOpcode::type _op;
663
};
664
665
} // namespace doris