Coverage Report

Created: 2026-07-03 22:26

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.00k
inline std::string compound_operator_to_string(TExprOpcode::type op) {
38
7.00k
    if (op == TExprOpcode::COMPOUND_AND) {
39
1.18k
        return "and";
40
5.81k
    } else if (op == TExprOpcode::COMPOUND_OR) {
41
5.22k
        return "or";
42
5.22k
    } else {
43
592
        return "not";
44
592
    }
45
7.00k
}
46
47
class VCompoundPred : public VectorizedFnCall {
48
    ENABLE_FACTORY_CREATOR(VCompoundPred);
49
50
public:
51
7.00k
    VCompoundPred(const TExprNode& node) : VectorizedFnCall(node) {
52
7.00k
        _op = node.opcode;
53
7.00k
        _fn.name.function_name = compound_operator_to_string(_op);
54
7.00k
        _expr_name = fmt::format("VCompoundPredicate[{}](arguments={},return={})",
55
7.00k
                                 _fn.name.function_name, get_child_names(), _data_type->get_name());
56
7.00k
    }
57
58
#ifdef BE_TEST
59
    VCompoundPred() = default;
60
#endif
61
62
15.7k
    const std::string& expr_name() const override { return _expr_name; }
63
794
    Status clone_node(VExprSPtr* cloned_expr) const override {
64
794
        DORIS_CHECK(cloned_expr != nullptr);
65
794
        *cloned_expr = VCompoundPred::create_shared(clone_texpr_node());
66
794
        return Status::OK();
67
794
    }
68
69
13.5k
    bool can_evaluate_zonemap_filter() const override {
70
13.5k
        switch (_op) {
71
2.08k
        case TExprOpcode::COMPOUND_AND:
72
2.59k
            return std::ranges::any_of(_children, [](const VExprSPtr& child) {
73
2.59k
                return child->can_evaluate_zonemap_filter();
74
2.59k
            });
75
8.91k
        case TExprOpcode::COMPOUND_OR:
76
14.5k
            return !_children.empty() && std::ranges::all_of(_children, [](const VExprSPtr& child) {
77
14.5k
                return child->can_evaluate_zonemap_filter();
78
14.5k
            });
79
2.55k
        case TExprOpcode::COMPOUND_NOT:
80
2.55k
            return false;
81
0
        default:
82
0
            return false;
83
13.5k
        }
84
13.5k
    }
85
86
2.54k
    ZoneMapFilterResult evaluate_zonemap_filter(const ZoneMapEvalContext& ctx) const override {
87
2.54k
        switch (_op) {
88
307
        case TExprOpcode::COMPOUND_AND: {
89
540
            for (const auto& child : _children) {
90
540
                if (!child->can_evaluate_zonemap_filter()) {
91
57
                    continue;
92
57
                }
93
483
                if (child->evaluate_zonemap_filter(ctx) == ZoneMapFilterResult::kNoMatch) {
94
125
                    return ZoneMapFilterResult::kNoMatch;
95
125
                }
96
483
            }
97
182
            return ZoneMapFilterResult::kMayMatch;
98
307
        }
99
2.23k
        case TExprOpcode::COMPOUND_OR: {
100
3.79k
            for (const auto& child : _children) {
101
3.79k
                DORIS_CHECK(child->can_evaluate_zonemap_filter());
102
3.79k
                if (child->evaluate_zonemap_filter(ctx) != ZoneMapFilterResult::kNoMatch) {
103
1.87k
                    return ZoneMapFilterResult::kMayMatch;
104
1.87k
                }
105
3.79k
            }
106
354
            return ZoneMapFilterResult::kNoMatch;
107
2.23k
        }
108
1
        case TExprOpcode::COMPOUND_NOT:
109
1
            return unsupported_zonemap_filter(ctx);
110
0
        default:
111
0
            return unsupported_zonemap_filter(ctx);
112
2.54k
        }
113
2.54k
    }
114
115
4.66k
    Status evaluate_inverted_index(VExprContext* context, uint32_t segment_num_rows) override {
116
4.66k
        segment_v2::InvertedIndexResultBitmap res;
117
4.66k
        bool all_pass = true;
118
119
4.66k
        switch (_op) {
120
2.97k
        case TExprOpcode::COMPOUND_OR: {
121
5.73k
            for (const auto& child : _children) {
122
5.73k
                if (Status st = child->evaluate_inverted_index(context, segment_num_rows);
123
5.73k
                    !st.ok()) {
124
177
                    LOG(ERROR) << "expr:" << child->expr_name()
125
177
                               << " evaluate_inverted_index error:" << st.to_string();
126
177
                    all_pass = false;
127
177
                    continue;
128
177
                }
129
5.55k
                auto inverted_index_context = context->get_index_context();
130
5.55k
                if (inverted_index_context->has_index_result_for_expr(child.get())) {
131
2.97k
                    const auto* index_result =
132
2.97k
                            inverted_index_context->get_index_result_for_expr(child.get());
133
2.97k
                    if (res.is_empty()) {
134
1.98k
                        res = *index_result;
135
1.98k
                    } else {
136
989
                        res |= *index_result;
137
989
                    }
138
2.97k
                    if (inverted_index_context->get_score_runtime() == nullptr) {
139
2.96k
                        if (res.get_data_bitmap()->cardinality() == segment_num_rows) {
140
331
                            break; // Early exit if result is full
141
331
                        }
142
2.96k
                    }
143
2.97k
                } else {
144
2.58k
                    all_pass = false;
145
2.58k
                }
146
5.55k
            }
147
2.97k
            break;
148
0
        }
149
5.22k
        case TExprOpcode::COMPOUND_AND: {
150
1.15k
            for (const auto& child : _children) {
151
1.15k
                if (Status st = child->evaluate_inverted_index(context, segment_num_rows);
152
1.15k
                    !st.ok()) {
153
26
                    LOG(ERROR) << "expr:" << child->expr_name()
154
26
                               << " evaluate_inverted_index error:" << st.to_string();
155
26
                    all_pass = false;
156
26
                    continue;
157
26
                }
158
1.12k
                if (context->get_index_context()->has_index_result_for_expr(child.get())) {
159
215
                    const auto* index_result =
160
215
                            context->get_index_context()->get_index_result_for_expr(child.get());
161
215
                    if (res.is_empty()) {
162
157
                        res = *index_result;
163
157
                    } else {
164
58
                        res &= *index_result;
165
58
                    }
166
167
215
                    if (res.get_data_bitmap()->isEmpty()) {
168
74
                        break; // Early exit if result is empty
169
74
                    }
170
911
                } else {
171
911
                    all_pass = false;
172
911
                }
173
1.12k
            }
174
609
            break;
175
0
        }
176
1.07k
        case TExprOpcode::COMPOUND_NOT: {
177
1.07k
            const auto& child = _children[0];
178
1.07k
            Status st = child->evaluate_inverted_index(context, segment_num_rows);
179
1.07k
            if (!st.ok()) {
180
38
                LOG(ERROR) << "expr:" << child->expr_name()
181
38
                           << " evaluate_inverted_index error:" << st.to_string();
182
38
                return st;
183
38
            }
184
185
1.04k
            if (context->get_index_context()->has_index_result_for_expr(child.get())) {
186
657
                const auto* index_result =
187
657
                        context->get_index_context()->get_index_result_for_expr(child.get());
188
657
                roaring::Roaring full_result;
189
657
                full_result.addRange(0, segment_num_rows);
190
657
                res = index_result->op_not(&full_result);
191
657
            } else {
192
384
                all_pass = false;
193
384
            }
194
1.04k
            break;
195
1.07k
        }
196
0
        default:
197
0
            return Status::NotSupported(
198
0
                    "Compound operator must be AND, OR, or NOT to execute with inverted index.");
199
4.66k
        }
200
201
4.64k
        if (all_pass && !res.is_empty()) {
202
1.97k
            context->get_index_context()->set_index_result_for_expr(this, res);
203
1.97k
        }
204
4.64k
        return Status::OK();
205
4.66k
    }
206
207
    Status execute_column_impl(VExprContext* context, const Block* block, const Selector* selector,
208
56.1k
                               size_t count, ColumnPtr& result_column) const override {
209
56.1k
        if (fast_execute(context, selector, count, result_column)) {
210
74
            return Status::OK();
211
74
        }
212
56.0k
        if (get_num_children() == 1 || _has_const_child()) {
213
918
            return VectorizedFnCall::execute_column_impl(context, block, selector, count,
214
918
                                                         result_column);
215
918
        }
216
217
55.1k
        ColumnPtr lhs_column;
218
55.1k
        RETURN_IF_ERROR(_children[0]->execute_column(context, block, selector, count, lhs_column));
219
55.1k
        lhs_column = lhs_column->convert_to_full_column_if_const();
220
55.1k
        size_t size = lhs_column->size();
221
222
55.1k
        bool lhs_is_nullable = lhs_column->is_nullable();
223
55.1k
        auto [lhs_data_column, lhs_null_map] =
224
55.1k
                _get_raw_data_and_null_map(lhs_column, lhs_is_nullable);
225
55.1k
        size_t filted = simd::count_zero_num((int8_t*)lhs_data_column, size);
226
55.1k
        bool lhs_all_true = (filted == 0);
227
55.1k
        bool lhs_all_false = (filted == size);
228
229
55.1k
        bool lhs_all_is_not_null = false;
230
55.1k
        if (lhs_is_nullable) {
231
41.7k
            filted = simd::count_zero_num((int8_t*)lhs_null_map, size);
232
41.7k
            lhs_all_is_not_null = (filted == size);
233
41.7k
        }
234
235
55.1k
        ColumnPtr rhs_column = nullptr;
236
55.1k
        const uint8_t* __restrict rhs_data_column = nullptr;
237
55.1k
        const uint8_t* __restrict rhs_null_map = nullptr;
238
55.1k
        bool rhs_is_nullable = false;
239
55.1k
        bool rhs_all_true = false;
240
55.1k
        bool rhs_all_false = false;
241
55.1k
        bool rhs_all_is_not_null = false;
242
55.1k
        bool result_is_nullable = _data_type->is_nullable();
243
244
55.1k
        auto get_rhs_colum = [&]() {
245
43.0k
            if (!rhs_column) {
246
43.0k
                RETURN_IF_ERROR(
247
43.0k
                        _children[1]->execute_column(context, block, selector, count, rhs_column));
248
43.0k
                rhs_column = rhs_column->convert_to_full_column_if_const();
249
43.0k
                rhs_is_nullable = rhs_column->is_nullable();
250
43.0k
                auto rhs_nullable_column = _get_raw_data_and_null_map(rhs_column, rhs_is_nullable);
251
43.0k
                rhs_data_column = rhs_nullable_column.first;
252
43.0k
                rhs_null_map = rhs_nullable_column.second;
253
43.0k
                size_t filted = simd::count_zero_num((int8_t*)rhs_data_column, size);
254
43.0k
                rhs_all_true = (filted == 0);
255
43.0k
                rhs_all_false = (filted == size);
256
43.0k
                if (rhs_is_nullable) {
257
35.0k
                    filted = simd::count_zero_num((int8_t*)rhs_null_map, size);
258
35.0k
                    rhs_all_is_not_null = (filted == size);
259
35.0k
                }
260
43.0k
            }
261
43.0k
            return Status::OK();
262
43.0k
        };
263
264
55.1k
        auto return_result_column_id = [&](ColumnPtr& arg_column) {
265
44.4k
            result_column = std::move(*arg_column).mutate();
266
44.4k
            if (result_is_nullable && !result_column->is_nullable()) {
267
3.20k
                result_column = make_nullable(result_column);
268
3.20k
            }
269
44.4k
        };
270
271
55.1k
        auto create_null_map_column = [&](ColumnPtr& null_map_column,
272
55.1k
                                          const uint8_t* __restrict null_map_data) {
273
20.3k
            if (null_map_data == nullptr) {
274
793
                null_map_column = ColumnUInt8::create(size, 0);
275
793
                null_map_data =
276
793
                        assert_cast<const ColumnUInt8*>(null_map_column.get())->get_data().data();
277
793
            }
278
20.3k
            return null_map_data;
279
20.3k
        };
280
281
55.1k
        auto vector_vector = [&]<bool is_and_op>() {
282
487
            MutableColumnPtr mutable_result_column;
283
487
            uint8_t* __restrict result_data_column = nullptr;
284
487
            const uint8_t* __restrict other_data_column = rhs_data_column;
285
487
            if (lhs_column->use_count() == 1) {
286
481
                mutable_result_column = IColumn::mutate(std::move(lhs_column));
287
481
                result_data_column =
288
481
                        assert_cast<ColumnUInt8*>(mutable_result_column.get())->get_data().data();
289
481
            } else if (rhs_column->use_count() == 1) {
290
1
                mutable_result_column = IColumn::mutate(std::move(rhs_column));
291
1
                result_data_column =
292
1
                        assert_cast<ColumnUInt8*>(mutable_result_column.get())->get_data().data();
293
1
                other_data_column = lhs_data_column;
294
5
            } else {
295
5
                mutable_result_column = lhs_column->clone_resized(size);
296
5
                result_data_column =
297
5
                        assert_cast<ColumnUInt8*>(mutable_result_column.get())->get_data().data();
298
5
            }
299
300
487
            do_not_null_pred<is_and_op>(result_data_column, other_data_column, size);
301
487
            result_column = std::move(mutable_result_column);
302
487
        };
_ZZNK5doris13VCompoundPred19execute_column_implEPNS_12VExprContextEPKNS_5BlockEPKNS_8PODArrayIjLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEEmRNS_3COWINS_7IColumnEE13immutable_ptrISE_EEENKUlTnbvE_clILb1EEEDav
Line
Count
Source
281
363
        auto vector_vector = [&]<bool is_and_op>() {
282
363
            MutableColumnPtr mutable_result_column;
283
363
            uint8_t* __restrict result_data_column = nullptr;
284
363
            const uint8_t* __restrict other_data_column = rhs_data_column;
285
363
            if (lhs_column->use_count() == 1) {
286
363
                mutable_result_column = IColumn::mutate(std::move(lhs_column));
287
363
                result_data_column =
288
363
                        assert_cast<ColumnUInt8*>(mutable_result_column.get())->get_data().data();
289
363
            } else if (rhs_column->use_count() == 1) {
290
0
                mutable_result_column = IColumn::mutate(std::move(rhs_column));
291
0
                result_data_column =
292
0
                        assert_cast<ColumnUInt8*>(mutable_result_column.get())->get_data().data();
293
0
                other_data_column = lhs_data_column;
294
0
            } else {
295
0
                mutable_result_column = lhs_column->clone_resized(size);
296
0
                result_data_column =
297
0
                        assert_cast<ColumnUInt8*>(mutable_result_column.get())->get_data().data();
298
0
            }
299
300
363
            do_not_null_pred<is_and_op>(result_data_column, other_data_column, size);
301
363
            result_column = std::move(mutable_result_column);
302
363
        };
_ZZNK5doris13VCompoundPred19execute_column_implEPNS_12VExprContextEPKNS_5BlockEPKNS_8PODArrayIjLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEEmRNS_3COWINS_7IColumnEE13immutable_ptrISE_EEENKUlTnbvE_clILb0EEEDav
Line
Count
Source
281
124
        auto vector_vector = [&]<bool is_and_op>() {
282
124
            MutableColumnPtr mutable_result_column;
283
124
            uint8_t* __restrict result_data_column = nullptr;
284
124
            const uint8_t* __restrict other_data_column = rhs_data_column;
285
124
            if (lhs_column->use_count() == 1) {
286
118
                mutable_result_column = IColumn::mutate(std::move(lhs_column));
287
118
                result_data_column =
288
118
                        assert_cast<ColumnUInt8*>(mutable_result_column.get())->get_data().data();
289
118
            } else if (rhs_column->use_count() == 1) {
290
1
                mutable_result_column = IColumn::mutate(std::move(rhs_column));
291
1
                result_data_column =
292
1
                        assert_cast<ColumnUInt8*>(mutable_result_column.get())->get_data().data();
293
1
                other_data_column = lhs_data_column;
294
5
            } else {
295
5
                mutable_result_column = lhs_column->clone_resized(size);
296
5
                result_data_column =
297
5
                        assert_cast<ColumnUInt8*>(mutable_result_column.get())->get_data().data();
298
5
            }
299
300
124
            do_not_null_pred<is_and_op>(result_data_column, other_data_column, size);
301
124
            result_column = std::move(mutable_result_column);
302
124
        };
303
55.1k
        auto vector_vector_null = [&]<bool is_and_op>() {
304
10.1k
            auto col_res = ColumnUInt8::create(size);
305
10.1k
            auto col_nulls = ColumnUInt8::create(size);
306
307
10.1k
            auto* __restrict res_datas = col_res->get_data().data();
308
10.1k
            auto* __restrict res_nulls = col_nulls->get_data().data();
309
10.1k
            ColumnPtr temp_null_map = nullptr;
310
            // maybe both children are nullable / or one of children is nullable
311
10.1k
            auto* __restrict lhs_null_map_tmp = create_null_map_column(temp_null_map, lhs_null_map);
312
10.1k
            auto* __restrict rhs_null_map_tmp = create_null_map_column(temp_null_map, rhs_null_map);
313
10.1k
            auto* __restrict lhs_data_column_tmp = lhs_data_column;
314
10.1k
            auto* __restrict rhs_data_column_tmp = rhs_data_column;
315
316
10.1k
            do_null_pred<is_and_op>(lhs_data_column_tmp, lhs_null_map_tmp, rhs_data_column_tmp,
317
10.1k
                                    rhs_null_map_tmp, res_datas, res_nulls, size);
318
319
10.1k
            result_column = ColumnNullable::create(std::move(col_res), std::move(col_nulls));
320
10.1k
        };
_ZZNK5doris13VCompoundPred19execute_column_implEPNS_12VExprContextEPKNS_5BlockEPKNS_8PODArrayIjLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEEmRNS_3COWINS_7IColumnEE13immutable_ptrISE_EEENKUlTnbvE0_clILb1EEEDav
Line
Count
Source
303
4.98k
        auto vector_vector_null = [&]<bool is_and_op>() {
304
4.98k
            auto col_res = ColumnUInt8::create(size);
305
4.98k
            auto col_nulls = ColumnUInt8::create(size);
306
307
4.98k
            auto* __restrict res_datas = col_res->get_data().data();
308
4.98k
            auto* __restrict res_nulls = col_nulls->get_data().data();
309
4.98k
            ColumnPtr temp_null_map = nullptr;
310
            // maybe both children are nullable / or one of children is nullable
311
4.98k
            auto* __restrict lhs_null_map_tmp = create_null_map_column(temp_null_map, lhs_null_map);
312
4.98k
            auto* __restrict rhs_null_map_tmp = create_null_map_column(temp_null_map, rhs_null_map);
313
4.98k
            auto* __restrict lhs_data_column_tmp = lhs_data_column;
314
4.98k
            auto* __restrict rhs_data_column_tmp = rhs_data_column;
315
316
4.98k
            do_null_pred<is_and_op>(lhs_data_column_tmp, lhs_null_map_tmp, rhs_data_column_tmp,
317
4.98k
                                    rhs_null_map_tmp, res_datas, res_nulls, size);
318
319
4.98k
            result_column = ColumnNullable::create(std::move(col_res), std::move(col_nulls));
320
4.98k
        };
_ZZNK5doris13VCompoundPred19execute_column_implEPNS_12VExprContextEPKNS_5BlockEPKNS_8PODArrayIjLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEEmRNS_3COWINS_7IColumnEE13immutable_ptrISE_EEENKUlTnbvE0_clILb0EEEDav
Line
Count
Source
303
5.18k
        auto vector_vector_null = [&]<bool is_and_op>() {
304
5.18k
            auto col_res = ColumnUInt8::create(size);
305
5.18k
            auto col_nulls = ColumnUInt8::create(size);
306
307
5.18k
            auto* __restrict res_datas = col_res->get_data().data();
308
5.18k
            auto* __restrict res_nulls = col_nulls->get_data().data();
309
5.18k
            ColumnPtr temp_null_map = nullptr;
310
            // maybe both children are nullable / or one of children is nullable
311
5.18k
            auto* __restrict lhs_null_map_tmp = create_null_map_column(temp_null_map, lhs_null_map);
312
5.18k
            auto* __restrict rhs_null_map_tmp = create_null_map_column(temp_null_map, rhs_null_map);
313
5.18k
            auto* __restrict lhs_data_column_tmp = lhs_data_column;
314
5.18k
            auto* __restrict rhs_data_column_tmp = rhs_data_column;
315
316
5.18k
            do_null_pred<is_and_op>(lhs_data_column_tmp, lhs_null_map_tmp, rhs_data_column_tmp,
317
5.18k
                                    rhs_null_map_tmp, res_datas, res_nulls, size);
318
319
5.18k
            result_column = ColumnNullable::create(std::move(col_res), std::move(col_nulls));
320
5.18k
        };
321
322
        // false and NULL ----> 0
323
        // true  and NULL ----> NULL
324
55.1k
        if (_op == TExprOpcode::COMPOUND_AND) {
325
            //1. not null column: all data is false
326
            //2. nullable column: null map all is not null
327
17.3k
            if ((lhs_all_false && !lhs_is_nullable) || (lhs_all_false && lhs_all_is_not_null)) {
328
                // false and any = false, return lhs
329
7.85k
                return_result_column_id(lhs_column);
330
9.48k
            } else {
331
9.48k
                RETURN_IF_ERROR(get_rhs_colum());
332
333
9.48k
                if ((lhs_all_true && !lhs_is_nullable) ||    //not null column
334
9.48k
                    (lhs_all_true && lhs_all_is_not_null)) { //nullable column
335
                                                             // true and any = any, return rhs
336
337
697
                    return_result_column_id(rhs_column);
338
8.79k
                } else if ((rhs_all_false && !rhs_is_nullable) ||
339
8.79k
                           (rhs_all_false && rhs_all_is_not_null)) {
340
                    // any and false = false, return rhs
341
216
                    return_result_column_id(rhs_column);
342
8.57k
                } else if ((rhs_all_true && !rhs_is_nullable) ||
343
8.57k
                           (rhs_all_true && rhs_all_is_not_null)) {
344
                    // any and true = any, return lhs
345
3.23k
                    return_result_column_id(lhs_column);
346
5.34k
                } else {
347
5.34k
                    if (!result_is_nullable) {
348
363
                        vector_vector.template operator()<true>();
349
4.97k
                    } else {
350
4.97k
                        vector_vector_null.template operator()<true>();
351
4.97k
                    }
352
5.34k
                }
353
9.48k
            }
354
37.7k
        } else if (_op == TExprOpcode::COMPOUND_OR) {
355
            // true  or NULL ----> 1
356
            // false or NULL ----> NULL
357
37.7k
            if ((lhs_all_true && !lhs_is_nullable) || (lhs_all_true && lhs_all_is_not_null)) {
358
                // true or any = true, return lhs
359
4.21k
                return_result_column_id(lhs_column);
360
33.5k
            } else {
361
33.5k
                RETURN_IF_ERROR(get_rhs_colum());
362
33.5k
                if ((lhs_all_false && !lhs_is_nullable) || (lhs_all_false && lhs_all_is_not_null)) {
363
                    // false or any = any, return rhs
364
24.8k
                    return_result_column_id(rhs_column);
365
24.8k
                } else if ((rhs_all_true && !rhs_is_nullable) ||
366
8.70k
                           (rhs_all_true && rhs_all_is_not_null)) {
367
                    // any or true = true, return rhs
368
735
                    return_result_column_id(rhs_column);
369
7.97k
                } else if ((rhs_all_false && !rhs_is_nullable) ||
370
7.97k
                           (rhs_all_false && rhs_all_is_not_null)) {
371
                    // any or false = any, return lhs
372
2.65k
                    return_result_column_id(lhs_column);
373
5.31k
                } else {
374
5.31k
                    if (!result_is_nullable) {
375
124
                        vector_vector.template operator()<false>();
376
5.19k
                    } else {
377
5.19k
                        vector_vector_null.template operator()<false>();
378
5.19k
                    }
379
5.31k
                }
380
33.5k
            }
381
37.7k
        } else {
382
23
            return Status::InternalError("Compound operator must be AND or OR.");
383
23
        }
384
385
55.1k
        DCHECK_EQ(result_column->size(), count);
386
55.1k
        return Status::OK();
387
55.1k
    }
388
389
1.00k
    double execute_cost() const override {
390
1.00k
        double cost = 0.3;
391
1.61k
        for (const auto& child : _children) {
392
1.61k
            cost += child->execute_cost();
393
1.61k
        }
394
1.00k
        return cost;
395
1.00k
    }
396
397
private:
398
197k
    static inline constexpr uint8_t apply_and_null(UInt8 a, UInt8 l_null, UInt8 b, UInt8 r_null) {
399
        // (<> && false) is false, (true && NULL) is NULL
400
197k
        return (l_null & r_null) | (r_null & (l_null ^ a)) | (l_null & (r_null ^ b));
401
197k
    }
402
549k
    static inline constexpr uint8_t apply_or_null(UInt8 a, UInt8 l_null, UInt8 b, UInt8 r_null) {
403
        // (<> || true) is true, (false || NULL) is NULL
404
549k
        return (l_null & r_null) | (r_null & (r_null ^ a)) | (l_null & (l_null ^ b));
405
549k
    }
406
407
    template <bool is_and>
408
    void static do_not_null_pred(uint8_t* __restrict lhs, const uint8_t* __restrict rhs,
409
487
                                 size_t size) {
410
#ifdef NDEBUG
411
#if defined(__clang__)
412
#pragma clang loop vectorize(enable)
413
#elif defined(__GNUC__) && (__GNUC__ >= 5)
414
#pragma GCC ivdep
415
#endif
416
#endif
417
71.1k
        for (size_t i = 0; i < size; ++i) {
418
70.6k
            if constexpr (is_and) {
419
66.3k
                lhs[i] &= rhs[i];
420
66.3k
            } else {
421
4.26k
                lhs[i] |= rhs[i];
422
4.26k
            }
423
70.6k
        }
424
487
    }
_ZN5doris13VCompoundPred16do_not_null_predILb1EEEvPhPKhm
Line
Count
Source
409
363
                                 size_t size) {
410
#ifdef NDEBUG
411
#if defined(__clang__)
412
#pragma clang loop vectorize(enable)
413
#elif defined(__GNUC__) && (__GNUC__ >= 5)
414
#pragma GCC ivdep
415
#endif
416
#endif
417
66.7k
        for (size_t i = 0; i < size; ++i) {
418
66.3k
            if constexpr (is_and) {
419
66.3k
                lhs[i] &= rhs[i];
420
            } else {
421
                lhs[i] |= rhs[i];
422
            }
423
66.3k
        }
424
363
    }
_ZN5doris13VCompoundPred16do_not_null_predILb0EEEvPhPKhm
Line
Count
Source
409
124
                                 size_t size) {
410
#ifdef NDEBUG
411
#if defined(__clang__)
412
#pragma clang loop vectorize(enable)
413
#elif defined(__GNUC__) && (__GNUC__ >= 5)
414
#pragma GCC ivdep
415
#endif
416
#endif
417
4.39k
        for (size_t i = 0; i < size; ++i) {
418
            if constexpr (is_and) {
419
                lhs[i] &= rhs[i];
420
4.26k
            } else {
421
4.26k
                lhs[i] |= rhs[i];
422
4.26k
            }
423
4.26k
        }
424
124
    }
425
426
    template <bool is_and>
427
    void static do_null_pred(const uint8_t* __restrict lhs_data, const uint8_t* __restrict lhs_null,
428
                             const uint8_t* __restrict rhs_data, const uint8_t* __restrict rhs_null,
429
                             uint8_t* __restrict res_data, uint8_t* __restrict res_null,
430
10.1k
                             size_t size) {
431
#ifdef NDEBUG
432
#if defined(__clang__)
433
#pragma clang loop vectorize(enable)
434
#elif defined(__GNUC__) && (__GNUC__ >= 5)
435
#pragma GCC ivdep
436
#endif
437
#endif
438
756k
        for (size_t i = 0; i < size; ++i) {
439
746k
            if constexpr (is_and) {
440
197k
                res_null[i] = apply_and_null(lhs_data[i], lhs_null[i], rhs_data[i], rhs_null[i]);
441
197k
                res_data[i] = lhs_data[i] & rhs_data[i];
442
549k
            } else {
443
549k
                res_null[i] = apply_or_null(lhs_data[i], lhs_null[i], rhs_data[i], rhs_null[i]);
444
549k
                res_data[i] = lhs_data[i] | rhs_data[i];
445
549k
            }
446
746k
        }
447
10.1k
    }
_ZN5doris13VCompoundPred12do_null_predILb1EEEvPKhS3_S3_S3_PhS4_m
Line
Count
Source
430
4.99k
                             size_t size) {
431
#ifdef NDEBUG
432
#if defined(__clang__)
433
#pragma clang loop vectorize(enable)
434
#elif defined(__GNUC__) && (__GNUC__ >= 5)
435
#pragma GCC ivdep
436
#endif
437
#endif
438
202k
        for (size_t i = 0; i < size; ++i) {
439
197k
            if constexpr (is_and) {
440
197k
                res_null[i] = apply_and_null(lhs_data[i], lhs_null[i], rhs_data[i], rhs_null[i]);
441
197k
                res_data[i] = lhs_data[i] & rhs_data[i];
442
            } else {
443
                res_null[i] = apply_or_null(lhs_data[i], lhs_null[i], rhs_data[i], rhs_null[i]);
444
                res_data[i] = lhs_data[i] | rhs_data[i];
445
            }
446
197k
        }
447
4.99k
    }
_ZN5doris13VCompoundPred12do_null_predILb0EEEvPKhS3_S3_S3_PhS4_m
Line
Count
Source
430
5.19k
                             size_t size) {
431
#ifdef NDEBUG
432
#if defined(__clang__)
433
#pragma clang loop vectorize(enable)
434
#elif defined(__GNUC__) && (__GNUC__ >= 5)
435
#pragma GCC ivdep
436
#endif
437
#endif
438
554k
        for (size_t i = 0; i < size; ++i) {
439
            if constexpr (is_and) {
440
                res_null[i] = apply_and_null(lhs_data[i], lhs_null[i], rhs_data[i], rhs_null[i]);
441
                res_data[i] = lhs_data[i] & rhs_data[i];
442
549k
            } else {
443
549k
                res_null[i] = apply_or_null(lhs_data[i], lhs_null[i], rhs_data[i], rhs_null[i]);
444
549k
                res_data[i] = lhs_data[i] | rhs_data[i];
445
549k
            }
446
549k
        }
447
5.19k
    }
448
449
55.1k
    bool _has_const_child() const {
450
55.1k
        return std::ranges::any_of(_children,
451
110k
                                   [](const VExprSPtr& arg) -> bool { return arg->is_constant(); });
452
55.1k
    }
453
454
    std::pair<const uint8_t*, const uint8_t*> _get_raw_data_and_null_map(
455
98.1k
            const ColumnPtr& column, bool has_nullable_column) const {
456
98.1k
        if (has_nullable_column) {
457
76.8k
            const auto* nullable_column = assert_cast<const ColumnNullable*>(column.get());
458
76.8k
            auto* data_column =
459
76.8k
                    assert_cast<const ColumnUInt8*>(nullable_column->get_nested_column_ptr().get())
460
76.8k
                            ->get_data()
461
76.8k
                            .data();
462
76.8k
            auto* null_map = nullable_column->get_null_map_column_ptr()->get_data().data();
463
76.8k
            return std::make_pair(data_column, null_map);
464
76.8k
        } else {
465
21.3k
            auto* data_column = assert_cast<const ColumnUInt8*>(column.get())->get_data().data();
466
21.3k
            return std::make_pair(data_column, nullptr);
467
21.3k
        }
468
98.1k
    }
469
470
    TExprOpcode::type _op;
471
};
472
473
} // namespace doris