Coverage Report

Created: 2026-06-27 16:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/vexpr.cpp
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
#include "exprs/vexpr.h"
19
20
#include <fmt/format.h>
21
#include <gen_cpp/Exprs_types.h>
22
#include <gen_cpp/FrontendService_types.h>
23
#include <thrift/protocol/TDebugProtocol.h>
24
25
#include <algorithm>
26
#include <boost/algorithm/string/split.hpp>
27
#include <boost/iterator/iterator_facade.hpp>
28
#include <cstdint>
29
#include <memory>
30
#include <stack>
31
#include <string_view>
32
#include <utility>
33
34
#include "common/config.h"
35
#include "common/exception.h"
36
#include "common/status.h"
37
#include "core/column/column_nothing.h"
38
#include "core/column/column_vector.h"
39
#include "core/data_type/data_type_array.h"
40
#include "core/data_type/data_type_decimal.h"
41
#include "core/data_type/data_type_factory.hpp"
42
#include "core/data_type/data_type_nullable.h"
43
#include "core/data_type/data_type_number.h"
44
#include "core/data_type/define_primitive_type.h"
45
#include "core/field.h"
46
#include "core/value/timestamptz_value.h"
47
#include "exec/common/util.hpp"
48
#include "exec/pipeline/pipeline_task.h"
49
#include "exprs/short_circuit_evaluation_expr.h"
50
#include "exprs/varray_literal.h"
51
#include "exprs/vcase_expr.h"
52
#include "exprs/vcast_expr.h"
53
#include "exprs/vcolumn_ref.h"
54
#include "exprs/vcompound_pred.h"
55
#include "exprs/vcondition_expr.h"
56
#include "exprs/vectorized_fn_call.h"
57
#include "exprs/vexpr_context.h"
58
#include "exprs/vexpr_fwd.h"
59
#include "exprs/vin_predicate.h"
60
#include "exprs/vinfo_func.h"
61
#include "exprs/virtual_slot_ref.h"
62
#include "exprs/vlambda_function_call_expr.h"
63
#include "exprs/vlambda_function_expr.h"
64
#include "exprs/vliteral.h"
65
#include "exprs/vmap_literal.h"
66
#include "exprs/vmatch_predicate.h"
67
#include "exprs/vsearch.h"
68
#include "exprs/vslot_ref.h"
69
#include "exprs/vstruct_literal.h"
70
#include "storage/index/ann/ann_search_params.h"
71
#include "storage/index/ann/ann_topn_runtime.h"
72
#include "storage/index/inverted/inverted_index_parser.h"
73
#include "storage/segment/column_reader.h"
74
75
namespace doris {
76
77
class RowDescriptor;
78
class RuntimeState;
79
80
// NOLINTBEGIN(readability-function-cognitive-complexity)
81
// NOLINTBEGIN(readability-function-size)
82
TExprNode create_texpr_node_from(const void* data, const PrimitiveType& type, int precision,
83
69
                                 int scale) {
84
69
    TExprNode node;
85
86
69
    switch (type) {
87
2
    case TYPE_BOOLEAN: {
88
2
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_BOOLEAN>(data, &node));
89
2
        break;
90
2
    }
91
2
    case TYPE_TINYINT: {
92
2
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_TINYINT>(data, &node));
93
2
        break;
94
2
    }
95
2
    case TYPE_SMALLINT: {
96
2
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_SMALLINT>(data, &node));
97
2
        break;
98
2
    }
99
7
    case TYPE_INT: {
100
7
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_INT>(data, &node));
101
7
        break;
102
7
    }
103
16
    case TYPE_BIGINT: {
104
16
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_BIGINT>(data, &node));
105
16
        break;
106
16
    }
107
16
    case TYPE_LARGEINT: {
108
2
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_LARGEINT>(data, &node));
109
2
        break;
110
2
    }
111
2
    case TYPE_FLOAT: {
112
2
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_FLOAT>(data, &node));
113
2
        break;
114
2
    }
115
2
    case TYPE_DOUBLE: {
116
2
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DOUBLE>(data, &node));
117
2
        break;
118
2
    }
119
2
    case TYPE_DATEV2: {
120
2
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATEV2>(data, &node));
121
2
        break;
122
2
    }
123
2
    case TYPE_DATETIMEV2: {
124
2
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATETIMEV2>(data, &node, precision, scale));
125
2
        break;
126
2
    }
127
2
    case TYPE_DATE: {
128
2
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATE>(data, &node));
129
2
        break;
130
2
    }
131
2
    case TYPE_DATETIME: {
132
2
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATETIME>(data, &node));
133
2
        break;
134
2
    }
135
2
    case TYPE_DECIMALV2: {
136
2
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMALV2>(data, &node, precision, scale));
137
2
        break;
138
2
    }
139
2
    case TYPE_DECIMAL32: {
140
2
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMAL32>(data, &node, precision, scale));
141
2
        break;
142
2
    }
143
2
    case TYPE_DECIMAL64: {
144
2
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMAL64>(data, &node, precision, scale));
145
2
        break;
146
2
    }
147
2
    case TYPE_DECIMAL128I: {
148
2
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMAL128I>(data, &node, precision, scale));
149
2
        break;
150
2
    }
151
2
    case TYPE_DECIMAL256: {
152
2
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DECIMAL256>(data, &node, precision, scale));
153
2
        break;
154
2
    }
155
2
    case TYPE_CHAR: {
156
2
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_CHAR>(data, &node));
157
2
        break;
158
2
    }
159
2
    case TYPE_VARCHAR: {
160
2
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_VARCHAR>(data, &node));
161
2
        break;
162
2
    }
163
5
    case TYPE_STRING: {
164
5
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_STRING>(data, &node));
165
5
        break;
166
5
    }
167
5
    case TYPE_VARBINARY: {
168
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_VARBINARY>(data, &node));
169
0
        break;
170
0
    }
171
2
    case TYPE_IPV4: {
172
2
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_IPV4>(data, &node));
173
2
        break;
174
2
    }
175
2
    case TYPE_IPV6: {
176
2
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_IPV6>(data, &node));
177
2
        break;
178
2
    }
179
2
    case TYPE_TIMEV2: {
180
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_TIMEV2>(data, &node, precision, scale));
181
0
        break;
182
0
    }
183
3
    case TYPE_TIMESTAMPTZ: {
184
3
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_TIMESTAMPTZ>(data, &node, precision, scale));
185
3
        break;
186
3
    }
187
3
    default:
188
0
        throw Exception(ErrorCode::INTERNAL_ERROR, "runtime filter meet invalid type {}",
189
0
                        int(type));
190
69
    }
191
69
    return node;
192
69
}
193
194
TExprNode create_texpr_node_from(const Field& field, const PrimitiveType& type, int precision,
195
25
                                 int scale) {
196
25
    TExprNode node;
197
25
    switch (type) {
198
1
    case TYPE_BOOLEAN: {
199
1
        const auto& storage = static_cast<bool>(field.get<TYPE_BOOLEAN>());
200
1
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_BOOLEAN>(&storage, &node));
201
1
        break;
202
1
    }
203
1
    case TYPE_TINYINT: {
204
0
        const auto& storage = static_cast<int8_t>(field.get<TYPE_TINYINT>());
205
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_TINYINT>(&storage, &node));
206
0
        break;
207
0
    }
208
1
    case TYPE_SMALLINT: {
209
1
        const auto& storage = static_cast<int16_t>(field.get<TYPE_SMALLINT>());
210
1
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_SMALLINT>(&storage, &node));
211
1
        break;
212
1
    }
213
1
    case TYPE_INT: {
214
1
        const auto& storage = static_cast<int32_t>(field.get<TYPE_INT>());
215
1
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_INT>(&storage, &node));
216
1
        break;
217
1
    }
218
1
    case TYPE_BIGINT: {
219
1
        const auto& storage = static_cast<int64_t>(field.get<TYPE_BIGINT>());
220
1
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_BIGINT>(&storage, &node));
221
1
        break;
222
1
    }
223
1
    case TYPE_LARGEINT: {
224
1
        const auto& storage = static_cast<int128_t>(field.get<TYPE_LARGEINT>());
225
1
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_LARGEINT>(&storage, &node));
226
1
        break;
227
1
    }
228
1
    case TYPE_FLOAT: {
229
1
        const auto& storage = static_cast<float>(field.get<TYPE_FLOAT>());
230
1
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_FLOAT>(&storage, &node));
231
1
        break;
232
1
    }
233
1
    case TYPE_DOUBLE: {
234
1
        const auto& storage = static_cast<double>(field.get<TYPE_DOUBLE>());
235
1
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DOUBLE>(&storage, &node));
236
1
        break;
237
1
    }
238
1
    case TYPE_DATEV2: {
239
1
        const auto& storage = field.get<TYPE_DATEV2>();
240
241
1
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATEV2>(&storage, &node));
242
1
        break;
243
1
    }
244
1
    case TYPE_DATETIMEV2: {
245
1
        const auto& storage = field.get<TYPE_DATETIMEV2>();
246
1
        THROW_IF_ERROR(
247
1
                create_texpr_literal_node<TYPE_DATETIMEV2>(&storage, &node, precision, scale));
248
1
        break;
249
1
    }
250
1
    case TYPE_TIMESTAMPTZ: {
251
1
        const auto& storage = field.get<TYPE_TIMESTAMPTZ>();
252
253
1
        THROW_IF_ERROR(
254
1
                create_texpr_literal_node<TYPE_TIMESTAMPTZ>(&storage, &node, precision, scale));
255
1
        break;
256
1
    }
257
1
    case TYPE_DATE: {
258
1
        const auto& storage = field.get<TYPE_DATE>();
259
1
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATE>(&storage, &node));
260
1
        break;
261
1
    }
262
1
    case TYPE_DATETIME: {
263
1
        const auto& storage = field.get<TYPE_DATETIME>();
264
1
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_DATETIME>(&storage, &node));
265
1
        break;
266
1
    }
267
1
    case TYPE_DECIMALV2: {
268
1
        const auto& storage = field.get<TYPE_DECIMALV2>();
269
270
1
        THROW_IF_ERROR(
271
1
                create_texpr_literal_node<TYPE_DECIMALV2>(&storage, &node, precision, scale));
272
1
        break;
273
1
    }
274
2
    case TYPE_DECIMAL32: {
275
2
        const auto& storage = field.get<TYPE_DECIMAL32>();
276
2
        THROW_IF_ERROR(
277
2
                create_texpr_literal_node<TYPE_DECIMAL32>(&storage, &node, precision, scale));
278
2
        break;
279
2
    }
280
2
    case TYPE_DECIMAL64: {
281
2
        const auto& storage = field.get<TYPE_DECIMAL64>();
282
2
        THROW_IF_ERROR(
283
2
                create_texpr_literal_node<TYPE_DECIMAL64>(&storage, &node, precision, scale));
284
2
        break;
285
2
    }
286
2
    case TYPE_DECIMAL128I: {
287
2
        const auto& storage = field.get<TYPE_DECIMAL128I>();
288
2
        THROW_IF_ERROR(
289
2
                create_texpr_literal_node<TYPE_DECIMAL128I>(&storage, &node, precision, scale));
290
2
        break;
291
2
    }
292
2
    case TYPE_DECIMAL256: {
293
2
        const auto& storage = field.get<TYPE_DECIMAL256>();
294
2
        THROW_IF_ERROR(
295
2
                create_texpr_literal_node<TYPE_DECIMAL256>(&storage, &node, precision, scale));
296
2
        break;
297
2
    }
298
2
    case TYPE_CHAR: {
299
0
        const auto& storage = field.get<TYPE_CHAR>();
300
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_CHAR>(&storage, &node));
301
0
        break;
302
0
    }
303
0
    case TYPE_VARCHAR: {
304
0
        const auto& storage = field.get<TYPE_VARCHAR>();
305
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_VARCHAR>(&storage, &node));
306
0
        break;
307
0
    }
308
1
    case TYPE_STRING: {
309
1
        const auto& storage = field.get<TYPE_STRING>();
310
1
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_STRING>(&storage, &node));
311
1
        break;
312
1
    }
313
1
    case TYPE_IPV4: {
314
0
        const auto& storage = field.get<TYPE_IPV4>();
315
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_IPV4>(&storage, &node));
316
0
        break;
317
0
    }
318
0
    case TYPE_IPV6: {
319
0
        const auto& storage = field.get<TYPE_IPV6>();
320
0
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_IPV6>(&storage, &node));
321
0
        break;
322
0
    }
323
1
    case TYPE_TIMEV2: {
324
1
        const auto& storage = field.get<TYPE_TIMEV2>();
325
1
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_TIMEV2>(&storage, &node));
326
1
        break;
327
1
    }
328
2
    case TYPE_VARBINARY: {
329
2
        const auto& svf = field.get<TYPE_VARBINARY>();
330
2
        THROW_IF_ERROR(create_texpr_literal_node<TYPE_VARBINARY>(&svf, &node));
331
2
        break;
332
2
    }
333
2
    default:
334
0
        throw Exception(ErrorCode::INTERNAL_ERROR, "runtime filter meet invalid type {}",
335
0
                        int(type));
336
25
    }
337
25
    return node;
338
25
}
339
340
// NOLINTEND(readability-function-size)
341
// NOLINTEND(readability-function-cognitive-complexity)
342
} // namespace doris
343
344
namespace doris {
345
346
VExpr::VExpr(const TExprNode& node)
347
400k
        : _node_type(node.node_type),
348
400k
          _opcode(node.__isset.opcode ? node.opcode : TExprOpcode::INVALID_OPCODE) {
349
400k
    if (node.__isset.fn) {
350
189
        _fn = node.fn;
351
189
    }
352
353
400k
    bool is_nullable = true;
354
400k
    if (node.__isset.is_nullable) {
355
396k
        is_nullable = node.is_nullable;
356
396k
    }
357
    // If we define null literal ,should make nullable data type to get correct field instead of undefined ptr
358
400k
    if (node.node_type == TExprNodeType::NULL_LITERAL) {
359
1
        CHECK(is_nullable);
360
1
    }
361
400k
    _data_type = get_data_type_with_default_argument(
362
400k
            DataTypeFactory::instance().create_data_type(node.type, is_nullable));
363
400k
}
364
365
42
VExpr::VExpr(const VExpr& vexpr) = default;
366
367
VExpr::VExpr(DataTypePtr type, bool is_slotref)
368
960
        : _opcode(TExprOpcode::INVALID_OPCODE),
369
960
          _data_type(get_data_type_with_default_argument(type)) {
370
960
    if (is_slotref) {
371
418
        _node_type = TExprNodeType::SLOT_REF;
372
418
    }
373
960
}
374
375
17
TExprNode VExpr::clone_texpr_node() const {
376
17
    TExprNode node;
377
17
    node.__set_node_type(_node_type);
378
17
    node.__set_opcode(_opcode);
379
17
    node.__set_type(create_type_desc(remove_nullable(_data_type)->get_primitive_type(),
380
17
                                     static_cast<int>(_data_type->get_precision()),
381
17
                                     static_cast<int>(_data_type->get_scale())));
382
17
    node.__set_is_nullable(_data_type->is_nullable());
383
17
    node.__set_num_children(get_num_children());
384
17
    node.__set_fn(_fn);
385
17
    return node;
386
17
}
387
388
0
Status VExpr::clone_node(VExprSPtr* cloned_expr) const {
389
0
    DORIS_CHECK(cloned_expr != nullptr);
390
0
    return Status::NotSupported("Cannot clone expression {} for file-local rewrite", expr_name());
391
0
}
392
393
Status VExpr::deep_clone(VExprSPtr* cloned_expr,
394
351
                         const VExprCloneNodeOverride& clone_node_override) const {
395
351
    DORIS_CHECK(cloned_expr != nullptr);
396
397
351
    VExprSPtr cloned;
398
351
    if (clone_node_override) {
399
339
        RETURN_IF_ERROR(clone_node_override(*this, &cloned));
400
339
    }
401
351
    if (cloned == nullptr) {
402
351
        RETURN_IF_ERROR(clone_node(&cloned));
403
351
    }
404
351
    DORIS_CHECK(cloned != nullptr);
405
406
351
    VExprSPtrs cloned_children;
407
351
    cloned_children.reserve(_children.size());
408
351
    for (const auto& child : _children) {
409
260
        DORIS_CHECK(child != nullptr);
410
260
        VExprSPtr cloned_child;
411
260
        RETURN_IF_ERROR(child->deep_clone(&cloned_child, clone_node_override));
412
260
        cloned_children.push_back(std::move(cloned_child));
413
260
    }
414
351
    cloned->set_children(std::move(cloned_children));
415
351
    cloned->reset_prepare_state();
416
351
    *cloned_expr = std::move(cloned);
417
351
    return Status::OK();
418
351
}
419
420
258k
Status VExpr::prepare(RuntimeState* state, const RowDescriptor& row_desc, VExprContext* context) {
421
258k
    ++context->_depth_num;
422
258k
    if (context->_depth_num > config::max_depth_of_expr_tree) {
423
0
        return Status::Error<ErrorCode::EXCEEDED_LIMIT>(
424
0
                "The depth of the expression tree is too big, make it less than {}",
425
0
                config::max_depth_of_expr_tree);
426
0
    }
427
428
258k
    for (auto& i : _children) {
429
428
        RETURN_IF_ERROR(i->prepare(state, row_desc, context));
430
428
    }
431
258k
    --context->_depth_num;
432
#ifndef BE_TEST
433
    _enable_inverted_index_query = state->query_options().enable_inverted_index_query;
434
#endif
435
258k
    return Status::OK();
436
258k
}
437
438
Status VExpr::open(RuntimeState* state, VExprContext* context,
439
513k
                   FunctionContext::FunctionStateScope scope) {
440
513k
    for (auto& i : _children) {
441
105
        RETURN_IF_ERROR(i->open(state, context, scope));
442
105
    }
443
513k
    if (scope == FunctionContext::FRAGMENT_LOCAL) {
444
258k
        RETURN_IF_ERROR(VExpr::get_const_col(context, nullptr));
445
258k
    }
446
513k
    return Status::OK();
447
513k
}
448
449
746
void VExpr::reset_prepare_state() {
450
746
    _prepared = false;
451
746
    _prepare_finished = false;
452
746
    _open_finished = false;
453
746
    for (auto& child : _children) {
454
395
        child->reset_prepare_state();
455
395
    }
456
746
}
457
458
514k
void VExpr::close(VExprContext* context, FunctionContext::FunctionStateScope scope) {
459
514k
    for (auto& i : _children) {
460
500
        i->close(context, scope);
461
500
    }
462
514k
}
463
464
// NOLINTBEGIN(readability-function-size)
465
400k
Status VExpr::create_expr(const TExprNode& expr_node, VExprSPtr& expr) {
466
400k
    try {
467
400k
        switch (expr_node.node_type) {
468
2
        case TExprNodeType::BOOL_LITERAL:
469
36
        case TExprNodeType::INT_LITERAL:
470
36
        case TExprNodeType::LARGE_INT_LITERAL:
471
37
        case TExprNodeType::IPV4_LITERAL:
472
37
        case TExprNodeType::IPV6_LITERAL:
473
166
        case TExprNodeType::FLOAT_LITERAL:
474
167
        case TExprNodeType::DECIMAL_LITERAL:
475
170
        case TExprNodeType::DATE_LITERAL:
476
170
        case TExprNodeType::TIMEV2_LITERAL:
477
179
        case TExprNodeType::STRING_LITERAL:
478
179
        case TExprNodeType::JSON_LITERAL:
479
179
        case TExprNodeType::VARBINARY_LITERAL:
480
180
        case TExprNodeType::NULL_LITERAL: {
481
180
            expr = VLiteral::create_shared(expr_node);
482
180
            break;
483
179
        }
484
15
        case TExprNodeType::ARRAY_LITERAL: {
485
15
            expr = VArrayLiteral::create_shared(expr_node);
486
15
            break;
487
179
        }
488
0
        case TExprNodeType::MAP_LITERAL: {
489
0
            expr = VMapLiteral::create_shared(expr_node);
490
0
            break;
491
179
        }
492
0
        case TExprNodeType::STRUCT_LITERAL: {
493
0
            expr = VStructLiteral::create_shared(expr_node);
494
0
            break;
495
179
        }
496
400k
        case TExprNodeType::SLOT_REF: {
497
400k
            if (expr_node.slot_ref.__isset.is_virtual_slot && expr_node.slot_ref.is_virtual_slot) {
498
13
                expr = VirtualSlotRef::create_shared(expr_node);
499
13
                expr->_node_type = TExprNodeType::VIRTUAL_SLOT_REF;
500
400k
            } else {
501
400k
                expr = VSlotRef::create_shared(expr_node);
502
400k
            }
503
400k
            break;
504
179
        }
505
0
        case TExprNodeType::COLUMN_REF: {
506
0
            expr = VColumnRef::create_shared(expr_node);
507
0
            break;
508
179
        }
509
17
        case TExprNodeType::COMPOUND_PRED: {
510
17
            expr = VCompoundPred::create_shared(expr_node);
511
17
            break;
512
179
        }
513
0
        case TExprNodeType::LAMBDA_FUNCTION_EXPR: {
514
0
            expr = VLambdaFunctionExpr::create_shared(expr_node);
515
0
            break;
516
179
        }
517
0
        case TExprNodeType::LAMBDA_FUNCTION_CALL_EXPR: {
518
0
            expr = VLambdaFunctionCallExpr::create_shared(expr_node);
519
0
            break;
520
179
        }
521
0
        case TExprNodeType::ARITHMETIC_EXPR:
522
71
        case TExprNodeType::BINARY_PRED:
523
72
        case TExprNodeType::NULL_AWARE_BINARY_PRED:
524
72
        case TExprNodeType::COMPUTE_FUNCTION_CALL: {
525
72
            expr = VectorizedFnCall::create_shared(expr_node);
526
72
            break;
527
72
        }
528
34
        case TExprNodeType::FUNCTION_CALL: {
529
34
            if (expr_node.fn.name.function_name == "if") {
530
2
                if (expr_node.__isset.short_circuit_evaluation &&
531
2
                    expr_node.short_circuit_evaluation) {
532
1
                    expr = ShortCircuitIfExpr::create_shared(expr_node);
533
1
                } else {
534
1
                    expr = VectorizedIfExpr::create_shared(expr_node);
535
1
                }
536
2
                break;
537
32
            } else if (expr_node.fn.name.function_name == "ifnull" ||
538
32
                       expr_node.fn.name.function_name == "nvl") {
539
2
                if (expr_node.__isset.short_circuit_evaluation &&
540
2
                    expr_node.short_circuit_evaluation) {
541
1
                    expr = ShortCircuitIfNullExpr::create_shared(expr_node);
542
1
                } else {
543
1
                    expr = VectorizedIfNullExpr::create_shared(expr_node);
544
1
                }
545
2
                break;
546
30
            } else if (expr_node.fn.name.function_name == "coalesce") {
547
2
                if (expr_node.__isset.short_circuit_evaluation &&
548
2
                    expr_node.short_circuit_evaluation) {
549
1
                    expr = ShortCircuitCoalesceExpr::create_shared(expr_node);
550
1
                } else {
551
1
                    expr = VectorizedCoalesceExpr::create_shared(expr_node);
552
1
                }
553
2
                break;
554
2
            }
555
28
            expr = VectorizedFnCall::create_shared(expr_node);
556
28
            break;
557
34
        }
558
0
        case TExprNodeType::MATCH_PRED: {
559
0
            expr = VMatchPredicate::create_shared(expr_node);
560
0
            break;
561
34
        }
562
2
        case TExprNodeType::CAST_EXPR: {
563
2
            expr = VCastExpr::create_shared(expr_node);
564
2
            break;
565
34
        }
566
0
        case TExprNodeType::TRY_CAST_EXPR: {
567
0
            expr = TryCastExpr::create_shared(expr_node);
568
0
            break;
569
34
        }
570
5
        case TExprNodeType::IN_PRED: {
571
5
            expr = VInPredicate::create_shared(expr_node);
572
5
            break;
573
34
        }
574
2
        case TExprNodeType::CASE_EXPR: {
575
2
            if (!expr_node.__isset.case_expr) {
576
0
                return Status::InternalError("Case expression not set in thrift node");
577
0
            }
578
2
            if (expr_node.__isset.short_circuit_evaluation && expr_node.short_circuit_evaluation) {
579
1
                expr = ShortCircuitCaseExpr::create_shared(expr_node);
580
1
            } else {
581
1
                expr = VCaseExpr::create_shared(expr_node);
582
1
            }
583
2
            break;
584
2
        }
585
0
        case TExprNodeType::INFO_FUNC: {
586
0
            expr = VInfoFunc::create_shared(expr_node);
587
0
            break;
588
2
        }
589
0
        case TExprNodeType::SEARCH_EXPR: {
590
0
            expr = VSearchExpr::create_shared(expr_node);
591
0
            break;
592
2
        }
593
0
        default:
594
0
            return Status::InternalError("Unknown expr node type: {}", expr_node.node_type);
595
400k
        }
596
400k
    } catch (const Exception& e) {
597
0
        if (e.code() == ErrorCode::INTERNAL_ERROR) {
598
0
            return Status::InternalError("Create Expr failed because {}\nTExprNode={}", e.what(),
599
0
                                         apache::thrift::ThriftDebugString(expr_node));
600
0
        }
601
0
        return Status::Error<false>(e.code(), "Create Expr failed because {}", e.what());
602
0
        LOG(WARNING) << "create expr failed, TExprNode={}, reason={}"
603
0
                     << apache::thrift::ThriftDebugString(expr_node) << e.what();
604
0
    }
605
400k
    if (!expr->data_type()) {
606
0
        return Status::InvalidArgument("Unknown expr type: {}", expr_node.node_type);
607
0
    }
608
400k
    return Status::OK();
609
400k
}
610
// NOLINTEND(readability-function-size)
611
612
Status VExpr::create_tree_from_thrift(const std::vector<TExprNode>& nodes, int* node_idx,
613
400k
                                      VExprSPtr& root_expr, VExprContextSPtr& ctx) {
614
    // propagate error case
615
400k
    if (*node_idx >= nodes.size()) {
616
0
        return Status::InternalError("Failed to reconstruct expression tree from thrift.");
617
0
    }
618
619
    // create root expr
620
400k
    int root_children = nodes[*node_idx].num_children;
621
400k
    VExprSPtr root;
622
400k
    RETURN_IF_ERROR(create_expr(nodes[*node_idx], root));
623
400k
    DCHECK(root != nullptr);
624
400k
    root_expr = root;
625
400k
    ctx = std::make_shared<VExprContext>(root);
626
    // short path for leaf node
627
400k
    if (root_children <= 0) {
628
400k
        return Status::OK();
629
400k
    }
630
631
    // non-recursive traversal
632
56
    using VExprSPtrCountPair = std::pair<VExprSPtr, int>;
633
56
    std::stack<std::shared_ptr<VExprSPtrCountPair>> s;
634
56
    s.emplace(std::make_shared<VExprSPtrCountPair>(root, root_children));
635
356
    while (!s.empty()) {
636
        // copy the shared ptr resource to avoid dangling reference
637
300
        auto parent = s.top();
638
        // Decrement or pop
639
300
        if (parent->second > 1) {
640
193
            parent->second -= 1;
641
193
        } else {
642
107
            s.pop();
643
107
        }
644
645
300
        DCHECK(parent->first != nullptr);
646
300
        if (++*node_idx >= nodes.size()) {
647
0
            return Status::InternalError("Failed to reconstruct expression tree from thrift.");
648
0
        }
649
650
300
        VExprSPtr expr;
651
300
        RETURN_IF_ERROR(create_expr(nodes[*node_idx], expr));
652
300
        DCHECK(expr != nullptr);
653
300
        parent->first->add_child(expr);
654
        // push to stack if has children
655
300
        int num_children = nodes[*node_idx].num_children;
656
300
        if (num_children > 0) {
657
51
            s.emplace(std::make_shared<VExprSPtrCountPair>(expr, num_children));
658
51
        }
659
300
    }
660
56
    return Status::OK();
661
56
}
662
663
400k
Status VExpr::create_expr_tree(const TExpr& texpr, VExprContextSPtr& ctx) {
664
400k
    if (texpr.nodes.empty()) {
665
9
        ctx = nullptr;
666
9
        return Status::OK();
667
9
    }
668
400k
    int node_idx = 0;
669
400k
    VExprSPtr e;
670
400k
    Status status = create_tree_from_thrift(texpr.nodes, &node_idx, e, ctx);
671
400k
    if (status.ok() && node_idx + 1 != texpr.nodes.size()) {
672
0
        status = Status::InternalError(
673
0
                "Expression tree only partially reconstructed. Not all thrift nodes were "
674
0
                "used.");
675
0
    }
676
400k
    if (!status.ok()) {
677
0
        LOG(ERROR) << "Could not construct expr tree.\n"
678
0
                   << status << "\n"
679
0
                   << apache::thrift::ThriftDebugString(texpr);
680
0
    }
681
400k
    return status;
682
400k
}
683
684
28.1k
Status VExpr::create_expr_trees(const std::vector<TExpr>& texprs, VExprContextSPtrs& ctxs) {
685
28.1k
    ctxs.clear();
686
70.1k
    for (const auto& texpr : texprs) {
687
70.1k
        VExprContextSPtr ctx;
688
70.1k
        RETURN_IF_ERROR(create_expr_tree(texpr, ctx));
689
70.1k
        ctxs.push_back(ctx);
690
70.1k
    }
691
28.1k
    return Status::OK();
692
28.1k
}
693
694
Status VExpr::check_expr_output_type(const VExprContextSPtrs& ctxs,
695
24.0k
                                     const RowDescriptor& output_row_desc) {
696
24.0k
    if (ctxs.empty()) {
697
6
        return Status::OK();
698
6
    }
699
24.0k
    auto name_and_types = VectorizedUtils::create_name_and_data_types(output_row_desc);
700
24.0k
    if (ctxs.size() != name_and_types.size()) {
701
0
        return Status::InternalError(
702
0
                "output type size not match expr size {} , expected output size {} ", ctxs.size(),
703
0
                name_and_types.size());
704
0
    }
705
65.9k
    auto check_type_can_be_converted = [](DataTypePtr& from, DataTypePtr& to) -> bool {
706
65.9k
        if (to->equals(*from)) {
707
65.9k
            return true;
708
65.9k
        }
709
0
        if (to->is_nullable() && !from->is_nullable()) {
710
0
            return remove_nullable(to)->equals(*from);
711
0
        }
712
0
        return false;
713
0
    };
714
90.0k
    for (int i = 0; i < ctxs.size(); i++) {
715
65.9k
        auto real_expr_type = get_data_type_with_default_argument(ctxs[i]->root()->data_type());
716
65.9k
        auto&& [name, expected_type] = name_and_types[i];
717
65.9k
        if (!check_type_can_be_converted(real_expr_type, expected_type)) {
718
0
            return Status::InternalError(
719
0
                    "output type not match expr type, col name {} , expected type {} , real type "
720
0
                    "{}",
721
0
                    name, expected_type->get_name(), real_expr_type->get_name());
722
0
        }
723
65.9k
    }
724
24.0k
    return Status::OK();
725
24.0k
}
726
727
Status VExpr::prepare(const VExprContextSPtrs& ctxs, RuntimeState* state,
728
148k
                      const RowDescriptor& row_desc) {
729
258k
    for (auto ctx : ctxs) {
730
258k
        RETURN_IF_ERROR(ctx->prepare(state, row_desc));
731
258k
    }
732
148k
    return Status::OK();
733
148k
}
734
735
148k
Status VExpr::open(const VExprContextSPtrs& ctxs, RuntimeState* state) {
736
258k
    for (const auto& ctx : ctxs) {
737
258k
        RETURN_IF_ERROR(ctx->open(state));
738
258k
    }
739
148k
    return Status::OK();
740
148k
}
741
742
96.4k
bool VExpr::contains_blockable_function(const VExprContextSPtrs& ctxs) {
743
96.4k
    return std::any_of(ctxs.begin(), ctxs.end(), [](const VExprContextSPtr& ctx) {
744
65.9k
        return ctx != nullptr && ctx->root() != nullptr && ctx->root()->is_blockable();
745
65.9k
    });
746
96.4k
}
747
748
Status VExpr::clone_if_not_exists(const VExprContextSPtrs& ctxs, RuntimeState* state,
749
0
                                  VExprContextSPtrs& new_ctxs) {
750
0
    if (!new_ctxs.empty()) {
751
        // 'ctxs' was already cloned into '*new_ctxs', nothing to do.
752
0
        DCHECK_EQ(new_ctxs.size(), ctxs.size());
753
0
        for (auto& new_ctx : new_ctxs) {
754
0
            DCHECK(new_ctx->_is_clone);
755
0
        }
756
0
        return Status::OK();
757
0
    }
758
0
    new_ctxs.resize(ctxs.size());
759
0
    for (int i = 0; i < ctxs.size(); ++i) {
760
0
        RETURN_IF_ERROR(ctxs[i]->clone(state, new_ctxs[i]));
761
0
    }
762
0
    return Status::OK();
763
0
}
764
765
7
std::string VExpr::debug_string() const {
766
    // TODO: implement partial debug string for member vars
767
7
    std::stringstream out;
768
7
    out << " type=" << _data_type->get_name();
769
770
7
    if (!_children.empty()) {
771
0
        out << " children=" << debug_string(_children);
772
0
    }
773
774
7
    return out.str();
775
7
}
776
777
0
std::string VExpr::debug_string(const VExprSPtrs& exprs) {
778
0
    std::stringstream out;
779
0
    out << "[";
780
781
0
    for (int i = 0; i < exprs.size(); ++i) {
782
0
        out << (i == 0 ? "" : " ") << exprs[i]->debug_string();
783
0
    }
784
785
0
    out << "]";
786
0
    return out.str();
787
0
}
788
789
0
std::string VExpr::debug_string(const VExprContextSPtrs& ctxs) {
790
0
    VExprSPtrs exprs;
791
0
    for (const auto& ctx : ctxs) {
792
0
        exprs.push_back(ctx->root());
793
0
    }
794
0
    return debug_string(exprs);
795
0
}
796
797
979
bool VExpr::is_constant() const {
798
979
    return std::all_of(_children.begin(), _children.end(),
799
979
                       [](const VExprSPtr& expr) { return expr->is_constant(); });
800
979
}
801
802
Status VExpr::get_const_col(VExprContext* context,
803
259k
                            std::shared_ptr<ColumnPtrWrapper>* column_wrapper) {
804
259k
    if (!is_constant()) {
805
258k
        return Status::OK();
806
258k
    }
807
808
269
    if (_constant_col != nullptr && column_wrapper == nullptr) {
809
0
        return Status::OK();
810
269
    } else if (_constant_col != nullptr) {
811
92
        *column_wrapper = _constant_col;
812
92
        return Status::OK();
813
92
    }
814
815
177
    ColumnPtr result;
816
177
    RETURN_IF_ERROR(execute_column(context, nullptr, nullptr, 1, result));
817
177
    _constant_col = std::make_shared<ColumnPtrWrapper>(result);
818
177
    if (column_wrapper != nullptr) {
819
0
        *column_wrapper = _constant_col;
820
0
    }
821
822
177
    return Status::OK();
823
177
}
824
825
173
void VExpr::register_function_context(RuntimeState* state, VExprContext* context) {
826
173
    std::vector<DataTypePtr> arg_types;
827
315
    for (auto& i : _children) {
828
315
        arg_types.push_back(i->data_type());
829
315
    }
830
831
173
    _fn_context_index = context->register_function_context(state, _data_type, arg_types);
832
173
}
833
834
Status VExpr::init_function_context(RuntimeState* state, VExprContext* context,
835
                                    FunctionContext::FunctionStateScope scope,
836
158
                                    const FunctionBasePtr& function) const {
837
158
    FunctionContext* fn_ctx = context->fn_context(_fn_context_index);
838
158
    if (scope == FunctionContext::FRAGMENT_LOCAL) {
839
115
        std::vector<std::shared_ptr<ColumnPtrWrapper>> constant_cols;
840
205
        for (auto c : _children) {
841
205
            std::shared_ptr<ColumnPtrWrapper> const_col;
842
205
            RETURN_IF_ERROR(c->get_const_col(context, &const_col));
843
205
            constant_cols.push_back(const_col);
844
205
        }
845
115
        fn_ctx->set_constant_cols(constant_cols);
846
115
    }
847
848
158
    if (scope == FunctionContext::FRAGMENT_LOCAL) {
849
115
        RETURN_IF_ERROR(function->open(fn_ctx, FunctionContext::FRAGMENT_LOCAL));
850
115
    }
851
158
    RETURN_IF_ERROR(function->open(fn_ctx, FunctionContext::THREAD_LOCAL));
852
158
    return Status::OK();
853
158
}
854
855
void VExpr::close_function_context(VExprContext* context, FunctionContext::FunctionStateScope scope,
856
271
                                   const FunctionBasePtr& function) const {
857
271
    if (_fn_context_index != -1) {
858
164
        FunctionContext* fn_ctx = context->fn_context(_fn_context_index);
859
        // `close_function_context` is called in VExprContext's destructor so do not throw exceptions here.
860
164
        static_cast<void>(function->close(fn_ctx, FunctionContext::THREAD_LOCAL));
861
164
        if (scope == FunctionContext::FRAGMENT_LOCAL) {
862
123
            static_cast<void>(function->close(fn_ctx, FunctionContext::FRAGMENT_LOCAL));
863
123
        }
864
164
    }
865
271
}
866
867
0
Status VExpr::check_constant(const Block& block, ColumnNumbers arguments) const {
868
0
    if (is_constant() && !VectorizedUtils::all_arguments_are_constant(block, arguments)) {
869
0
        return Status::InternalError("const check failed, expr={}", debug_string());
870
0
    }
871
0
    return Status::OK();
872
0
}
873
874
0
uint64_t VExpr::get_digest(uint64_t seed) const {
875
0
    auto digest = seed;
876
0
    for (auto child : _children) {
877
0
        digest = child->get_digest(digest);
878
0
        if (digest == 0) {
879
0
            return 0;
880
0
        }
881
0
    }
882
883
0
    auto& fn_name = _fn.name.function_name;
884
0
    if (!fn_name.empty()) {
885
0
        digest = HashUtil::hash64(fn_name.c_str(), fn_name.size(), digest);
886
0
    } else {
887
0
        digest = HashUtil::hash64((const char*)&_node_type, sizeof(_node_type), digest);
888
0
        digest = HashUtil::hash64((const char*)&_opcode, sizeof(_opcode), digest);
889
0
    }
890
0
    return digest;
891
0
}
892
893
1
ColumnPtr VExpr::get_result_from_const(size_t count) const {
894
1
    return ColumnConst::create(_constant_col->column_ptr, count);
895
1
}
896
897
Status VExpr::_evaluate_inverted_index(VExprContext* context, const FunctionBasePtr& function,
898
19
                                       uint32_t segment_num_rows) {
899
    // Pre-allocate vectors based on an estimated or known size
900
19
    std::vector<segment_v2::IndexIterator*> iterators;
901
19
    std::vector<IndexFieldNameAndTypePair> data_type_with_names;
902
19
    std::vector<int> column_ids;
903
19
    ColumnsWithTypeAndName arguments;
904
19
    VExprSPtrs children_exprs;
905
906
    // Reserve space to avoid multiple reallocations
907
19
    const size_t estimated_size = get_num_children();
908
19
    iterators.reserve(estimated_size);
909
19
    data_type_with_names.reserve(estimated_size);
910
19
    column_ids.reserve(estimated_size);
911
19
    children_exprs.reserve(estimated_size);
912
913
19
    auto index_context = context->get_index_context();
914
915
    // if child is cast expr, we need to ensure target data type is the same with storage data type.
916
    // or they are all string type
917
    // and if data type is array, we need to get the nested data type to ensure that.
918
28
    for (const auto& child : children()) {
919
28
        if (child->node_type() == TExprNodeType::CAST_EXPR) {
920
1
            auto* cast_expr = assert_cast<VCastExpr*>(child.get());
921
1
            DCHECK_EQ(cast_expr->get_num_children(), 1);
922
1
            if (cast_expr->get_child(0)->is_slot_ref()) {
923
0
                auto* column_slot_ref = assert_cast<VSlotRef*>(cast_expr->get_child(0).get());
924
0
                auto column_id = column_slot_ref->column_id();
925
0
                const auto* storage_name_type =
926
0
                        context->get_index_context()->get_storage_name_and_type_by_column_id(
927
0
                                column_id);
928
0
                auto storage_type = remove_nullable(storage_name_type->second);
929
0
                auto target_type = remove_nullable(cast_expr->get_target_type());
930
0
                auto origin_primitive_type = storage_type->get_primitive_type();
931
0
                auto target_primitive_type = target_type->get_primitive_type();
932
0
                if (is_complex_type(storage_type->get_primitive_type())) {
933
0
                    if (storage_type->get_primitive_type() == TYPE_ARRAY &&
934
0
                        target_type->get_primitive_type() == TYPE_ARRAY) {
935
0
                        auto nested_storage_type =
936
0
                                (assert_cast<const DataTypeArray*>(storage_type.get()))
937
0
                                        ->get_nested_type();
938
0
                        origin_primitive_type = nested_storage_type->get_primitive_type();
939
0
                        auto nested_target_type =
940
0
                                (assert_cast<const DataTypeArray*>(target_type.get()))
941
0
                                        ->get_nested_type();
942
0
                        target_primitive_type = nested_target_type->get_primitive_type();
943
0
                    } else {
944
0
                        continue;
945
0
                    }
946
0
                }
947
0
                if (origin_primitive_type != TYPE_VARIANT &&
948
0
                    (storage_type->equals(*target_type) ||
949
0
                     (is_string_type(target_primitive_type) &&
950
0
                      is_string_type(origin_primitive_type)))) {
951
0
                    children_exprs.emplace_back(expr_without_cast(child));
952
0
                }
953
1
            } else {
954
1
                return Status::OK(); // for example: cast("abc") as ipv4 case
955
1
            }
956
27
        } else {
957
27
            children_exprs.emplace_back(child);
958
27
        }
959
28
    }
960
961
18
    if (children_exprs.empty()) {
962
0
        return Status::OK(); // Early exit if no children to process
963
0
    }
964
965
26
    for (const auto& child : children_exprs) {
966
26
        if (child->is_slot_ref()) {
967
18
            auto* column_slot_ref = assert_cast<VSlotRef*>(child.get());
968
18
            auto column_id = column_slot_ref->column_id();
969
18
            auto* iter = context->get_index_context()->get_inverted_index_iterator_by_column_id(
970
18
                    column_id);
971
            //column does not have inverted index
972
18
            if (iter == nullptr) {
973
4
                continue;
974
4
            }
975
14
            const auto* storage_name_type =
976
14
                    context->get_index_context()->get_storage_name_and_type_by_column_id(column_id);
977
14
            if (storage_name_type == nullptr) {
978
0
                auto err_msg = fmt::format(
979
0
                        "storage_name_type cannot be found for column {} while in {} "
980
0
                        "evaluate_inverted_index",
981
0
                        column_id, expr_name());
982
0
                LOG(ERROR) << err_msg;
983
0
                return Status::InternalError(err_msg);
984
0
            }
985
14
            iterators.emplace_back(iter);
986
14
            data_type_with_names.emplace_back(*storage_name_type);
987
14
            column_ids.emplace_back(column_id);
988
14
        } else if (child->is_literal()) {
989
8
            auto* column_literal = assert_cast<VLiteral*>(child.get());
990
8
            arguments.emplace_back(column_literal->get_column_ptr(),
991
8
                                   column_literal->get_data_type(), column_literal->expr_name());
992
8
        } else if (child->can_push_down_to_index()) {
993
0
            RETURN_IF_ERROR(child->evaluate_inverted_index(context, segment_num_rows));
994
0
        } else {
995
0
            return Status::OK(); // others cases
996
0
        }
997
26
    }
998
999
    // is null or is not null has no arguments
1000
18
    if (iterators.empty() || (arguments.empty() && !(function->get_name() == "is_not_null_pred" ||
1001
6
                                                     function->get_name() == "is_null_pred"))) {
1002
4
        return Status::OK(); // Nothing to evaluate or no literals to compare against
1003
4
    }
1004
1005
14
    const InvertedIndexAnalyzerCtx* analyzer_ctx = nullptr;
1006
14
    if (auto index_ctx = context->get_index_context(); index_ctx != nullptr) {
1007
14
        analyzer_ctx = index_ctx->get_analyzer_ctx_for_expr(this);
1008
14
    }
1009
1010
14
    auto result_bitmap = segment_v2::InvertedIndexResultBitmap();
1011
    // Pass analyzer_key to function (used by match predicates for multi-analyzer index selection)
1012
14
    auto res = function->evaluate_inverted_index(arguments, data_type_with_names, iterators,
1013
14
                                                 segment_num_rows, analyzer_ctx, result_bitmap);
1014
14
    if (!res.ok()) {
1015
0
        return res;
1016
0
    }
1017
14
    if (!result_bitmap.is_empty()) {
1018
14
        index_context->set_index_result_for_expr(this, result_bitmap);
1019
14
        for (int column_id : column_ids) {
1020
14
            index_context->set_true_for_index_status(this, column_id);
1021
14
        }
1022
14
    }
1023
14
    return Status::OK();
1024
14
}
1025
1026
0
size_t VExpr::estimate_memory(const size_t rows) {
1027
0
    if (is_const_and_have_executed()) {
1028
0
        return 0;
1029
0
    }
1030
1031
0
    size_t estimate_size = 0;
1032
0
    for (auto& child : _children) {
1033
0
        estimate_size += child->estimate_memory(rows);
1034
0
    }
1035
1036
0
    if (_data_type->have_maximum_size_of_value()) {
1037
0
        estimate_size += rows * _data_type->get_size_of_value_in_memory();
1038
0
    } else {
1039
0
        estimate_size += rows * 64; /// TODO: need a more reasonable value
1040
0
    }
1041
0
    return estimate_size;
1042
0
}
1043
1044
Status VExpr::execute_column(VExprContext* context, const Block* block, const Selector* selector,
1045
1.01k
                             size_t count, ColumnPtr& result_column) const {
1046
1.01k
    RETURN_IF_ERROR(execute_column_impl(context, block, selector, count, result_column));
1047
1.01k
    if (result_column->size() != count) {
1048
1
        return Status::InternalError("Expr {} return column size {} not equal to expected size {}",
1049
1
                                     expr_name(), result_column->size(), count);
1050
1
    }
1051
1.01k
    DCHECK(selector == nullptr || selector->size() == count);
1052
    // Validate type match. ColumnNothing is exempt (used as a placeholder in tests/stubs).
1053
1.01k
    if (!check_and_get_column<ColumnNothing>(result_column.get())) {
1054
1.00k
        auto result_type = execute_type(block);
1055
1.00k
        if (result_type != nullptr) {
1056
969
            Status st = result_type->check_column(*result_column);
1057
969
            if (!st.ok()) {
1058
                // Nullable(T) may legitimately produce a non-nullable T column when all rows are
1059
                // non-null (use_default_implementation_for_nulls optimization). Allow this.
1060
35
                const auto* nullable_type =
1061
35
                        check_and_get_data_type<DataTypeNullable>(result_type.get());
1062
35
                if (nullable_type && !check_and_get_column<ColumnNullable>(result_column.get())) {
1063
34
                    st = nullable_type->get_nested_type()->check_column(*result_column);
1064
34
                }
1065
35
            }
1066
969
            if (!st.ok()) {
1067
1
                return Status::InternalError(
1068
1
                        "Expr {} return column type mismatch: declared={}, actual={}", expr_name(),
1069
1
                        result_type->get_name(), result_column->get_name());
1070
1
            }
1071
969
        }
1072
1.00k
    }
1073
1.01k
    return Status::OK();
1074
1.01k
}
1075
1076
bool VExpr::fast_execute(VExprContext* context, const Selector* selector, size_t count,
1077
114
                         ColumnPtr& result_column) const {
1078
114
    if (context->get_index_context() &&
1079
114
        context->get_index_context()->get_index_result_column().contains(this)) {
1080
        // prepare a column to save result
1081
1
        result_column = filter_column_with_selector(
1082
1
                context->get_index_context()->get_index_result_column()[this], selector, count);
1083
1
        if (_data_type->is_nullable()) {
1084
1
            result_column = make_nullable(result_column);
1085
1
        }
1086
1
        return true;
1087
1
    }
1088
113
    return false;
1089
114
}
1090
1091
0
bool VExpr::equals(const VExpr& other) {
1092
0
    return false;
1093
0
}
1094
1095
Status VExpr::evaluate_ann_range_search(
1096
        const segment_v2::AnnRangeSearchRuntime& runtime,
1097
        const std::vector<std::unique_ptr<segment_v2::IndexIterator>>& index_iterators,
1098
        const std::vector<ColumnId>& idx_to_cid,
1099
        const std::vector<std::unique_ptr<segment_v2::ColumnIterator>>& column_iterators,
1100
        size_t rows_of_segment, roaring::Roaring& row_bitmap, AnnIndexStats& ann_index_stats,
1101
0
        bool enable_result_cache, AnnRangeSearchEvaluationResult& result) {
1102
0
    result = {};
1103
0
    return Status::OK();
1104
0
}
1105
1106
void VExpr::prepare_ann_range_search(const doris::VectorSearchUserParams& params,
1107
                                     segment_v2::AnnRangeSearchRuntime& range_search_runtime,
1108
0
                                     bool& suitable_for_ann_index) {
1109
0
    if (!suitable_for_ann_index) {
1110
0
        return;
1111
0
    }
1112
0
    for (auto& child : _children) {
1113
0
        child->prepare_ann_range_search(params, range_search_runtime, suitable_for_ann_index);
1114
0
        if (!suitable_for_ann_index) {
1115
0
            return;
1116
0
        }
1117
0
    }
1118
0
}
1119
1120
Status VExpr::execute_filter(VExprContext* context, const Block* block,
1121
                             uint8_t* __restrict result_filter_data, size_t rows, bool accept_null,
1122
135
                             bool* can_filter_all) const {
1123
135
    ColumnPtr filter_column;
1124
135
    RETURN_IF_ERROR(execute_column(context, block, nullptr, rows, filter_column));
1125
135
    if (const auto* const_column = check_and_get_column<ColumnConst>(*filter_column)) {
1126
        // const(nullable) or const(bool)
1127
0
        const bool result = accept_null
1128
0
                                    ? (const_column->is_null_at(0) || const_column->get_bool(0))
1129
0
                                    : (!const_column->is_null_at(0) && const_column->get_bool(0));
1130
0
        if (!result) {
1131
            // filter all
1132
0
            *can_filter_all = true;
1133
0
            memset(result_filter_data, 0, rows);
1134
0
            return Status::OK();
1135
0
        }
1136
135
    } else if (const auto* nullable_column = check_and_get_column<ColumnNullable>(*filter_column)) {
1137
        // nullable(bool)
1138
68
        const ColumnPtr& nested_column = nullable_column->get_nested_column_ptr();
1139
68
        const IColumn::Filter& filter = assert_cast<const ColumnUInt8&>(*nested_column).get_data();
1140
68
        const auto* __restrict filter_data = filter.data();
1141
68
        const auto* __restrict null_map_data = nullable_column->get_null_map_data().data();
1142
1143
68
        if (accept_null) {
1144
0
            for (size_t i = 0; i < rows; ++i) {
1145
0
                result_filter_data[i] &= (null_map_data[i]) || filter_data[i];
1146
0
            }
1147
68
        } else {
1148
40.1k
            for (size_t i = 0; i < rows; ++i) {
1149
40.0k
                result_filter_data[i] &= (!null_map_data[i]) & filter_data[i];
1150
40.0k
            }
1151
68
        }
1152
1153
68
        if (!simd::contain_one(result_filter_data, rows)) {
1154
27
            *can_filter_all = true;
1155
27
            return Status::OK();
1156
27
        }
1157
68
    } else {
1158
        // bool
1159
67
        const IColumn::Filter& filter = assert_cast<const ColumnUInt8&>(*filter_column).get_data();
1160
67
        const auto* __restrict filter_data = filter.data();
1161
1162
36.3k
        for (size_t i = 0; i < rows; ++i) {
1163
36.2k
            result_filter_data[i] &= filter_data[i];
1164
36.2k
        }
1165
1166
67
        if (!simd::contain_one(result_filter_data, rows)) {
1167
1
            *can_filter_all = true;
1168
1
            return Status::OK();
1169
1
        }
1170
67
    }
1171
1172
107
    return Status::OK();
1173
135
}
1174
1175
} // namespace doris