Coverage Report

Created: 2026-10-15 19:32

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