Coverage Report

Created: 2026-08-13 19:39

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
7.47k
inline std::string compound_operator_to_string(TExprOpcode::type op) {
38
7.47k
    if (op == TExprOpcode::COMPOUND_AND) {
39
1.45k
        return "and";
40
6.02k
    } else if (op == TExprOpcode::COMPOUND_OR) {
41
5.44k
        return "or";
42
5.44k
    } else {
43
584
        return "not";
44
584
    }
45
7.47k
}
46
47
class VCompoundPred : public VectorizedFnCall {
48
    ENABLE_FACTORY_CREATOR(VCompoundPred);
49
50
public:
51
7.48k
    VCompoundPred(const TExprNode& node) : VectorizedFnCall(node) {
52
7.48k
        _op = node.opcode;
53
7.48k
        _fn.name.function_name = compound_operator_to_string(_op);
54
7.48k
        _expr_name = fmt::format("VCompoundPredicate[{}](arguments={},return={})",
55
7.48k
                                 _fn.name.function_name, get_child_names(), _data_type->get_name());
56
7.48k
    }
57
58
#ifdef BE_TEST
59
    VCompoundPred() = default;
60
#endif
61
62
13.4k
    const std::string& expr_name() const override { return _expr_name; }
63
1.92k
    Status clone_node(VExprSPtr* cloned_expr) const override {
64
1.92k
        DORIS_CHECK(cloned_expr != nullptr);
65
1.92k
        *cloned_expr = VCompoundPred::create_shared(clone_texpr_node());
66
1.92k
        return Status::OK();
67
1.92k
    }
68
69
    bool can_execute_on_raw_fixed_values(const DataTypePtr& data_type,
70
34
                                         int column_id) const override {
71
34
        return !_children.empty() &&
72
34
               (_op == TExprOpcode::COMPOUND_AND || _op == TExprOpcode::COMPOUND_OR) &&
73
42
               std::ranges::all_of(_children, [&](const VExprSPtr& child) {
74
42
                   return child->can_execute_on_raw_fixed_values(data_type, column_id);
75
42
               });
76
34
    }
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
50
                                          int column_id) const override {
93
50
        return !_children.empty() &&
94
50
               (_op == TExprOpcode::COMPOUND_AND || _op == TExprOpcode::COMPOUND_OR) &&
95
95
               std::ranges::all_of(_children, [&](const VExprSPtr& child) {
96
95
                   return child->can_execute_on_raw_binary_values(data_type, column_id);
97
95
               });
98
50
    }
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
32
    bool raw_predicate_result_for_null() const override {
114
32
        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
32
        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
58
        return std::ranges::any_of(_children, [](const VExprSPtr& child) {
125
58
            return child->raw_predicate_result_for_null();
126
58
        });
127
32
    }
128
129
17.2k
    bool can_evaluate_zonemap_filter() const override {
130
17.2k
        switch (_op) {
131
3.25k
        case TExprOpcode::COMPOUND_AND:
132
4.16k
            return std::ranges::any_of(_children, [](const VExprSPtr& child) {
133
4.16k
                return child->can_evaluate_zonemap_filter();
134
4.16k
            });
135
10.4k
        case TExprOpcode::COMPOUND_OR:
136
17.1k
            return !_children.empty() && std::ranges::all_of(_children, [](const VExprSPtr& child) {
137
17.1k
                return child->can_evaluate_zonemap_filter();
138
17.1k
            });
139
3.49k
        case TExprOpcode::COMPOUND_NOT:
140
3.49k
            return false;
141
0
        default:
142
0
            return false;
143
17.2k
        }
144
17.2k
    }
145
146
2.99k
    ZoneMapFilterResult evaluate_zonemap_filter(const ZoneMapEvalContext& ctx) const override {
147
2.99k
        switch (_op) {
148
469
        case TExprOpcode::COMPOUND_AND: {
149
859
            for (const auto& child : _children) {
150
859
                if (!child->can_evaluate_zonemap_filter()) {
151
153
                    continue;
152
153
                }
153
706
                if (child->evaluate_zonemap_filter(ctx) == ZoneMapFilterResult::kNoMatch) {
154
187
                    return ZoneMapFilterResult::kNoMatch;
155
187
                }
156
706
            }
157
282
            return ZoneMapFilterResult::kMayMatch;
158
469
        }
159
2.52k
        case TExprOpcode::COMPOUND_OR: {
160
4.20k
            for (const auto& child : _children) {
161
4.20k
                DORIS_CHECK(child->can_evaluate_zonemap_filter());
162
4.20k
                if (child->evaluate_zonemap_filter(ctx) != ZoneMapFilterResult::kNoMatch) {
163
2.14k
                    return ZoneMapFilterResult::kMayMatch;
164
2.14k
                }
165
4.20k
            }
166
383
            return ZoneMapFilterResult::kNoMatch;
167
2.52k
        }
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.99k
        }
173
2.99k
    }
174
175
198
    bool can_evaluate_dictionary_filter() const override {
176
198
        switch (_op) {
177
82
        case TExprOpcode::COMPOUND_AND:
178
85
            return std::ranges::any_of(_children, [](const VExprSPtr& child) {
179
85
                return child->can_evaluate_dictionary_filter();
180
85
            });
181
116
        case TExprOpcode::COMPOUND_OR:
182
218
            return !_children.empty() && std::ranges::all_of(_children, [](const VExprSPtr& child) {
183
218
                return child->can_evaluate_dictionary_filter();
184
218
            });
185
0
        default:
186
0
            return false;
187
198
        }
188
198
    }
189
190
1.07k
    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
1.07k
        return VExpr::is_safe_to_execute_on_selected_rows();
196
1.07k
    }
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
156
    bool can_evaluate_bloom_filter() const override {
225
156
        switch (_op) {
226
51
        case TExprOpcode::COMPOUND_AND:
227
62
            return std::ranges::any_of(_children, [](const VExprSPtr& child) {
228
62
                return child->can_evaluate_bloom_filter();
229
62
            });
230
105
        case TExprOpcode::COMPOUND_OR:
231
168
            return !_children.empty() && std::ranges::all_of(_children, [](const VExprSPtr& child) {
232
168
                return child->can_evaluate_bloom_filter();
233
168
            });
234
0
        default:
235
0
            return false;
236
156
        }
237
156
    }
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.47k
    Status evaluate_inverted_index(VExprContext* context, uint32_t segment_num_rows) override {
265
5.47k
        segment_v2::InvertedIndexResultBitmap res;
266
5.47k
        bool all_pass = true;
267
268
5.47k
        switch (_op) {
269
3.26k
        case TExprOpcode::COMPOUND_OR: {
270
6.27k
            for (const auto& child : _children) {
271
6.27k
                if (Status st = child->evaluate_inverted_index(context, segment_num_rows);
272
6.27k
                    !st.ok()) {
273
176
                    LOG(ERROR) << "expr:" << child->expr_name()
274
176
                               << " evaluate_inverted_index error:" << st.to_string();
275
176
                    all_pass = false;
276
176
                    continue;
277
176
                }
278
6.09k
                auto inverted_index_context = context->get_index_context();
279
6.09k
                if (inverted_index_context->has_index_result_for_expr(child.get())) {
280
3.14k
                    const auto* index_result =
281
3.14k
                            inverted_index_context->get_index_result_for_expr(child.get());
282
3.14k
                    if (res.is_empty()) {
283
2.17k
                        res = *index_result;
284
2.17k
                    } else {
285
967
                        res |= *index_result;
286
967
                    }
287
3.15k
                    if (inverted_index_context->get_score_runtime() == nullptr) {
288
3.15k
                        if (res.get_data_bitmap()->cardinality() == segment_num_rows) {
289
397
                            break; // Early exit if result is full
290
397
                        }
291
3.15k
                    }
292
3.14k
                } else {
293
2.95k
                    all_pass = false;
294
2.95k
                }
295
6.09k
            }
296
3.26k
            break;
297
0
        }
298
5.69k
        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
27
                    LOG(ERROR) << "expr:" << child->expr_name()
303
27
                               << " evaluate_inverted_index error:" << st.to_string();
304
27
                    all_pass = false;
305
27
                    continue;
306
27
                }
307
1.50k
                if (context->get_index_context()->has_index_result_for_expr(child.get())) {
308
278
                    const auto* index_result =
309
278
                            context->get_index_context()->get_index_result_for_expr(child.get());
310
278
                    if (res.is_empty()) {
311
219
                        res = *index_result;
312
219
                    } else {
313
59
                        res &= *index_result;
314
59
                    }
315
316
278
                    if (res.get_data_bitmap()->isEmpty()) {
317
135
                        break; // Early exit if result is empty
318
135
                    }
319
1.22k
                } else {
320
1.22k
                    all_pass = false;
321
1.22k
                }
322
1.50k
            }
323
828
            break;
324
0
        }
325
1.39k
        case TExprOpcode::COMPOUND_NOT: {
326
1.39k
            const auto& child = _children[0];
327
1.39k
            Status st = child->evaluate_inverted_index(context, segment_num_rows);
328
1.39k
            if (!st.ok()) {
329
38
                LOG(ERROR) << "expr:" << child->expr_name()
330
38
                           << " evaluate_inverted_index error:" << st.to_string();
331
38
                return st;
332
38
            }
333
334
1.35k
            if (context->get_index_context()->has_index_result_for_expr(child.get())) {
335
858
                const auto* index_result =
336
858
                        context->get_index_context()->get_index_result_for_expr(child.get());
337
858
                roaring::Roaring full_result;
338
858
                full_result.addRange(0, segment_num_rows);
339
858
                res = index_result->op_not(&full_result);
340
858
            } else {
341
501
                all_pass = false;
342
501
            }
343
1.35k
            break;
344
1.39k
        }
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.47k
        }
349
350
5.47k
        if (all_pass && !res.is_empty()) {
351
2.30k
            context->get_index_context()->set_index_result_for_expr(this, res);
352
2.30k
        }
353
5.47k
        return Status::OK();
354
5.47k
    }
355
356
    Status execute_column_impl(VExprContext* context, const Block* block, const Selector* selector,
357
25.8k
                               size_t count, ColumnPtr& result_column) const override {
358
25.8k
        if (fast_execute(context, selector, count, result_column)) {
359
139
            return Status::OK();
360
139
        }
361
25.7k
        if (get_num_children() == 1 || _has_const_child()) {
362
1.02k
            return VectorizedFnCall::execute_column_impl(context, block, selector, count,
363
1.02k
                                                         result_column);
364
1.02k
        }
365
366
24.7k
        ColumnPtr lhs_column;
367
24.7k
        RETURN_IF_ERROR(_children[0]->execute_column(context, block, selector, count, lhs_column));
368
24.7k
        lhs_column = lhs_column->convert_to_full_column_if_const();
369
24.7k
        size_t size = lhs_column->size();
370
371
24.7k
        bool lhs_is_nullable = lhs_column->is_nullable();
372
24.7k
        auto [lhs_data_column, lhs_null_map] =
373
24.7k
                _get_raw_data_and_null_map(lhs_column, lhs_is_nullable);
374
24.7k
        size_t filted = simd::count_zero_num((int8_t*)lhs_data_column, size);
375
24.7k
        bool lhs_all_true = (filted == 0);
376
24.7k
        bool lhs_all_false = (filted == size);
377
378
24.7k
        bool lhs_all_is_not_null = false;
379
24.7k
        if (lhs_is_nullable) {
380
13.4k
            filted = simd::count_zero_num((int8_t*)lhs_null_map, size);
381
13.4k
            lhs_all_is_not_null = (filted == size);
382
13.4k
        }
383
384
24.7k
        ColumnPtr rhs_column = nullptr;
385
24.7k
        const uint8_t* __restrict rhs_data_column = nullptr;
386
24.7k
        const uint8_t* __restrict rhs_null_map = nullptr;
387
24.7k
        bool rhs_is_nullable = false;
388
24.7k
        bool rhs_all_true = false;
389
24.7k
        bool rhs_all_false = false;
390
24.7k
        bool rhs_all_is_not_null = false;
391
24.7k
        bool result_is_nullable = _data_type->is_nullable();
392
393
24.7k
        auto get_rhs_colum = [&]() {
394
19.1k
            if (!rhs_column) {
395
19.1k
                RETURN_IF_ERROR(
396
19.1k
                        _children[1]->execute_column(context, block, selector, count, rhs_column));
397
19.1k
                rhs_column = rhs_column->convert_to_full_column_if_const();
398
19.1k
                rhs_is_nullable = rhs_column->is_nullable();
399
19.1k
                auto rhs_nullable_column = _get_raw_data_and_null_map(rhs_column, rhs_is_nullable);
400
19.1k
                rhs_data_column = rhs_nullable_column.first;
401
19.1k
                rhs_null_map = rhs_nullable_column.second;
402
19.1k
                size_t filted = simd::count_zero_num((int8_t*)rhs_data_column, size);
403
19.1k
                rhs_all_true = (filted == 0);
404
19.1k
                rhs_all_false = (filted == size);
405
19.1k
                if (rhs_is_nullable) {
406
14.1k
                    filted = simd::count_zero_num((int8_t*)rhs_null_map, size);
407
14.1k
                    rhs_all_is_not_null = (filted == size);
408
14.1k
                }
409
19.1k
            }
410
19.1k
            return Status::OK();
411
19.1k
        };
412
413
24.7k
        auto return_result_column_id = [&](ColumnPtr& arg_column) {
414
21.0k
            result_column = std::move(*arg_column).mutate();
415
21.0k
            if (result_is_nullable && !result_column->is_nullable()) {
416
2.27k
                result_column = make_nullable(result_column);
417
2.27k
            }
418
21.0k
        };
419
420
24.7k
        auto create_null_map_column = [&](ColumnPtr& null_map_column,
421
24.7k
                                          const uint8_t* __restrict null_map_data) {
422
6.14k
            if (null_map_data == nullptr) {
423
688
                null_map_column = ColumnUInt8::create(size, 0);
424
688
                null_map_data =
425
688
                        assert_cast<const ColumnUInt8*>(null_map_column.get())->get_data().data();
426
688
            }
427
6.14k
            return null_map_data;
428
6.14k
        };
429
430
24.7k
        auto vector_vector = [&]<bool is_and_op>() {
431
624
            MutableColumnPtr mutable_result_column;
432
624
            uint8_t* __restrict result_data_column = nullptr;
433
624
            const uint8_t* __restrict other_data_column = rhs_data_column;
434
624
            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
3
            } else {
444
3
                mutable_result_column = lhs_column->clone_resized(size);
445
3
                result_data_column =
446
3
                        assert_cast<ColumnUInt8*>(mutable_result_column.get())->get_data().data();
447
3
            }
448
449
624
            do_not_null_pred<is_and_op>(result_data_column, other_data_column, size);
450
624
            result_column = std::move(mutable_result_column);
451
624
        };
_ZZNK5doris13VCompoundPred19execute_column_implEPNS_12VExprContextEPKNS_5BlockEPKNS_8PODArrayIjLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEEmRNS_3COWINS_7IColumnEE13immutable_ptrISE_EEENKUlTnbvE_clILb1EEEDav
Line
Count
Source
430
483
        auto vector_vector = [&]<bool is_and_op>() {
431
483
            MutableColumnPtr mutable_result_column;
432
483
            uint8_t* __restrict result_data_column = nullptr;
433
483
            const uint8_t* __restrict other_data_column = rhs_data_column;
434
483
            if (lhs_column->use_count() == 1) {
435
483
                mutable_result_column = IColumn::mutate(std::move(lhs_column));
436
483
                result_data_column =
437
483
                        assert_cast<ColumnUInt8*>(mutable_result_column.get())->get_data().data();
438
483
            } 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
483
            do_not_null_pred<is_and_op>(result_data_column, other_data_column, size);
450
483
            result_column = std::move(mutable_result_column);
451
483
        };
_ZZNK5doris13VCompoundPred19execute_column_implEPNS_12VExprContextEPKNS_5BlockEPKNS_8PODArrayIjLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEEmRNS_3COWINS_7IColumnEE13immutable_ptrISE_EEENKUlTnbvE_clILb0EEEDav
Line
Count
Source
430
141
        auto vector_vector = [&]<bool is_and_op>() {
431
141
            MutableColumnPtr mutable_result_column;
432
141
            uint8_t* __restrict result_data_column = nullptr;
433
141
            const uint8_t* __restrict other_data_column = rhs_data_column;
434
141
            if (lhs_column->use_count() == 1) {
435
135
                mutable_result_column = IColumn::mutate(std::move(lhs_column));
436
135
                result_data_column =
437
135
                        assert_cast<ColumnUInt8*>(mutable_result_column.get())->get_data().data();
438
135
            } 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
3
            } else {
444
3
                mutable_result_column = lhs_column->clone_resized(size);
445
3
                result_data_column =
446
3
                        assert_cast<ColumnUInt8*>(mutable_result_column.get())->get_data().data();
447
3
            }
448
449
141
            do_not_null_pred<is_and_op>(result_data_column, other_data_column, size);
450
141
            result_column = std::move(mutable_result_column);
451
141
        };
452
24.7k
        auto vector_vector_null = [&]<bool is_and_op>() {
453
3.07k
            auto col_res = ColumnUInt8::create(size);
454
3.07k
            auto col_nulls = ColumnUInt8::create(size);
455
456
3.07k
            auto* __restrict res_datas = col_res->get_data().data();
457
3.07k
            auto* __restrict res_nulls = col_nulls->get_data().data();
458
3.07k
            ColumnPtr temp_null_map = nullptr;
459
            // maybe both children are nullable / or one of children is nullable
460
3.07k
            auto* __restrict lhs_null_map_tmp = create_null_map_column(temp_null_map, lhs_null_map);
461
3.07k
            auto* __restrict rhs_null_map_tmp = create_null_map_column(temp_null_map, rhs_null_map);
462
3.07k
            auto* __restrict lhs_data_column_tmp = lhs_data_column;
463
3.07k
            auto* __restrict rhs_data_column_tmp = rhs_data_column;
464
465
3.07k
            do_null_pred<is_and_op>(lhs_data_column_tmp, lhs_null_map_tmp, rhs_data_column_tmp,
466
3.07k
                                    rhs_null_map_tmp, res_datas, res_nulls, size);
467
468
3.07k
            result_column = ColumnNullable::create(std::move(col_res), std::move(col_nulls));
469
3.07k
        };
_ZZNK5doris13VCompoundPred19execute_column_implEPNS_12VExprContextEPKNS_5BlockEPKNS_8PODArrayIjLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEEmRNS_3COWINS_7IColumnEE13immutable_ptrISE_EEENKUlTnbvE0_clILb1EEEDav
Line
Count
Source
452
530
        auto vector_vector_null = [&]<bool is_and_op>() {
453
530
            auto col_res = ColumnUInt8::create(size);
454
530
            auto col_nulls = ColumnUInt8::create(size);
455
456
530
            auto* __restrict res_datas = col_res->get_data().data();
457
530
            auto* __restrict res_nulls = col_nulls->get_data().data();
458
530
            ColumnPtr temp_null_map = nullptr;
459
            // maybe both children are nullable / or one of children is nullable
460
530
            auto* __restrict lhs_null_map_tmp = create_null_map_column(temp_null_map, lhs_null_map);
461
530
            auto* __restrict rhs_null_map_tmp = create_null_map_column(temp_null_map, rhs_null_map);
462
530
            auto* __restrict lhs_data_column_tmp = lhs_data_column;
463
530
            auto* __restrict rhs_data_column_tmp = rhs_data_column;
464
465
530
            do_null_pred<is_and_op>(lhs_data_column_tmp, lhs_null_map_tmp, rhs_data_column_tmp,
466
530
                                    rhs_null_map_tmp, res_datas, res_nulls, size);
467
468
530
            result_column = ColumnNullable::create(std::move(col_res), std::move(col_nulls));
469
530
        };
_ZZNK5doris13VCompoundPred19execute_column_implEPNS_12VExprContextEPKNS_5BlockEPKNS_8PODArrayIjLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEEmRNS_3COWINS_7IColumnEE13immutable_ptrISE_EEENKUlTnbvE0_clILb0EEEDav
Line
Count
Source
452
2.54k
        auto vector_vector_null = [&]<bool is_and_op>() {
453
2.54k
            auto col_res = ColumnUInt8::create(size);
454
2.54k
            auto col_nulls = ColumnUInt8::create(size);
455
456
2.54k
            auto* __restrict res_datas = col_res->get_data().data();
457
2.54k
            auto* __restrict res_nulls = col_nulls->get_data().data();
458
2.54k
            ColumnPtr temp_null_map = nullptr;
459
            // maybe both children are nullable / or one of children is nullable
460
2.54k
            auto* __restrict lhs_null_map_tmp = create_null_map_column(temp_null_map, lhs_null_map);
461
2.54k
            auto* __restrict rhs_null_map_tmp = create_null_map_column(temp_null_map, rhs_null_map);
462
2.54k
            auto* __restrict lhs_data_column_tmp = lhs_data_column;
463
2.54k
            auto* __restrict rhs_data_column_tmp = rhs_data_column;
464
465
2.54k
            do_null_pred<is_and_op>(lhs_data_column_tmp, lhs_null_map_tmp, rhs_data_column_tmp,
466
2.54k
                                    rhs_null_map_tmp, res_datas, res_nulls, size);
467
468
2.54k
            result_column = ColumnNullable::create(std::move(col_res), std::move(col_nulls));
469
2.54k
        };
470
471
        // false and NULL ----> 0
472
        // true  and NULL ----> NULL
473
24.7k
        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
4.41k
            if ((lhs_all_false && !lhs_is_nullable) || (lhs_all_false && lhs_all_is_not_null)) {
477
                // false and any = false, return lhs
478
2.21k
                return_result_column_id(lhs_column);
479
2.21k
            } else {
480
2.19k
                RETURN_IF_ERROR(get_rhs_colum());
481
482
2.19k
                if ((lhs_all_true && !lhs_is_nullable) ||    //not null column
483
2.19k
                    (lhs_all_true && lhs_all_is_not_null)) { //nullable column
484
                                                             // true and any = any, return rhs
485
486
721
                    return_result_column_id(rhs_column);
487
1.47k
                } else if ((rhs_all_false && !rhs_is_nullable) ||
488
1.47k
                           (rhs_all_false && rhs_all_is_not_null)) {
489
                    // any and false = false, return rhs
490
214
                    return_result_column_id(rhs_column);
491
1.26k
                } else if ((rhs_all_true && !rhs_is_nullable) ||
492
1.26k
                           (rhs_all_true && rhs_all_is_not_null)) {
493
                    // any and true = any, return lhs
494
253
                    return_result_column_id(lhs_column);
495
1.01k
                } else {
496
1.01k
                    if (!result_is_nullable) {
497
483
                        vector_vector.template operator()<true>();
498
527
                    } else {
499
527
                        vector_vector_null.template operator()<true>();
500
527
                    }
501
1.01k
                }
502
2.19k
            }
503
20.3k
        } else if (_op == TExprOpcode::COMPOUND_OR) {
504
            // true  or NULL ----> 1
505
            // false or NULL ----> NULL
506
20.3k
            if ((lhs_all_true && !lhs_is_nullable) || (lhs_all_true && lhs_all_is_not_null)) {
507
                // true or any = true, return lhs
508
3.36k
                return_result_column_id(lhs_column);
509
16.9k
            } else {
510
16.9k
                RETURN_IF_ERROR(get_rhs_colum());
511
16.9k
                if ((lhs_all_false && !lhs_is_nullable) || (lhs_all_false && lhs_all_is_not_null)) {
512
                    // false or any = any, return rhs
513
12.9k
                    return_result_column_id(rhs_column);
514
12.9k
                } else if ((rhs_all_true && !rhs_is_nullable) ||
515
4.03k
                           (rhs_all_true && rhs_all_is_not_null)) {
516
                    // any or true = true, return rhs
517
730
                    return_result_column_id(rhs_column);
518
3.30k
                } else if ((rhs_all_false && !rhs_is_nullable) ||
519
3.30k
                           (rhs_all_false && rhs_all_is_not_null)) {
520
                    // any or false = any, return lhs
521
625
                    return_result_column_id(lhs_column);
522
2.68k
                } else {
523
2.68k
                    if (!result_is_nullable) {
524
141
                        vector_vector.template operator()<false>();
525
2.54k
                    } else {
526
2.54k
                        vector_vector_null.template operator()<false>();
527
2.54k
                    }
528
2.68k
                }
529
16.9k
            }
530
18.4E
        } else {
531
18.4E
            return Status::InternalError("Compound operator must be AND or OR.");
532
18.4E
        }
533
534
24.7k
        DCHECK_EQ(result_column->size(), count);
535
24.7k
        return Status::OK();
536
24.7k
    }
537
538
999
    double execute_cost() const override {
539
999
        double cost = 0.3;
540
1.60k
        for (const auto& child : _children) {
541
1.60k
            cost += child->execute_cost();
542
1.60k
        }
543
999
        return cost;
544
999
    }
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
78.3k
    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
78.3k
        return (l_null & r_null) | (r_null & (l_null ^ a)) | (l_null & (r_null ^ b));
593
78.3k
    }
594
274k
    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
274k
        return (l_null & r_null) | (r_null & (r_null ^ a)) | (l_null & (l_null ^ b));
597
274k
    }
598
599
    template <bool is_and>
600
    void static do_not_null_pred(uint8_t* __restrict lhs, const uint8_t* __restrict rhs,
601
624
                                 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
42.3k
        for (size_t i = 0; i < size; ++i) {
610
41.6k
            if constexpr (is_and) {
611
37.8k
                lhs[i] &= rhs[i];
612
37.8k
            } else {
613
3.83k
                lhs[i] |= rhs[i];
614
3.83k
            }
615
41.6k
        }
616
624
    }
_ZN5doris13VCompoundPred16do_not_null_predILb1EEEvPhPKhm
Line
Count
Source
601
483
                                 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
38.3k
        for (size_t i = 0; i < size; ++i) {
610
37.8k
            if constexpr (is_and) {
611
37.8k
                lhs[i] &= rhs[i];
612
            } else {
613
                lhs[i] |= rhs[i];
614
            }
615
37.8k
        }
616
483
    }
_ZN5doris13VCompoundPred16do_not_null_predILb0EEEvPhPKhm
Line
Count
Source
601
141
                                 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.97k
        for (size_t i = 0; i < size; ++i) {
610
            if constexpr (is_and) {
611
                lhs[i] &= rhs[i];
612
3.83k
            } else {
613
3.83k
                lhs[i] |= rhs[i];
614
3.83k
            }
615
3.83k
        }
616
141
    }
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
3.07k
                             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
356k
        for (size_t i = 0; i < size; ++i) {
631
352k
            if constexpr (is_and) {
632
78.3k
                res_null[i] = apply_and_null(lhs_data[i], lhs_null[i], rhs_data[i], rhs_null[i]);
633
78.3k
                res_data[i] = lhs_data[i] & rhs_data[i];
634
274k
            } else {
635
274k
                res_null[i] = apply_or_null(lhs_data[i], lhs_null[i], rhs_data[i], rhs_null[i]);
636
274k
                res_data[i] = lhs_data[i] | rhs_data[i];
637
274k
            }
638
352k
        }
639
3.07k
    }
_ZN5doris13VCompoundPred12do_null_predILb1EEEvPKhS3_S3_S3_PhS4_m
Line
Count
Source
622
530
                             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
78.8k
        for (size_t i = 0; i < size; ++i) {
631
78.3k
            if constexpr (is_and) {
632
78.3k
                res_null[i] = apply_and_null(lhs_data[i], lhs_null[i], rhs_data[i], rhs_null[i]);
633
78.3k
                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
78.3k
        }
639
530
    }
_ZN5doris13VCompoundPred12do_null_predILb0EEEvPKhS3_S3_S3_PhS4_m
Line
Count
Source
622
2.54k
                             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
277k
        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
274k
            } else {
635
274k
                res_null[i] = apply_or_null(lhs_data[i], lhs_null[i], rhs_data[i], rhs_null[i]);
636
274k
                res_data[i] = lhs_data[i] | rhs_data[i];
637
274k
            }
638
274k
        }
639
2.54k
    }
640
641
24.7k
    bool _has_const_child() const {
642
24.7k
        return std::ranges::any_of(_children,
643
49.5k
                                   [](const VExprSPtr& arg) -> bool { return arg->is_constant(); });
644
24.7k
    }
645
646
    std::pair<const uint8_t*, const uint8_t*> _get_raw_data_and_null_map(
647
43.8k
            const ColumnPtr& column, bool has_nullable_column) const {
648
43.8k
        if (has_nullable_column) {
649
27.5k
            const auto* nullable_column = assert_cast<const ColumnNullable*>(column.get());
650
27.5k
            auto* data_column =
651
27.5k
                    assert_cast<const ColumnUInt8*>(nullable_column->get_nested_column_ptr().get())
652
27.5k
                            ->get_data()
653
27.5k
                            .data();
654
27.5k
            auto* null_map = nullable_column->get_null_map_column_ptr()->get_data().data();
655
27.5k
            return std::make_pair(data_column, null_map);
656
27.5k
        } else {
657
16.2k
            auto* data_column = assert_cast<const ColumnUInt8*>(column.get())->get_data().data();
658
16.2k
            return std::make_pair(data_column, nullptr);
659
16.2k
        }
660
43.8k
    }
661
662
    TExprOpcode::type _op;
663
};
664
665
} // namespace doris