Coverage Report

Created: 2026-01-04 19:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/runtime/runtime_state.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
// This file is copied from
18
// https://github.com/apache/impala/blob/branch-2.9.0/be/src/runtime/runtime-state.h
19
// and modified by Doris
20
21
#pragma once
22
23
#include <gen_cpp/PaloInternalService_types.h>
24
#include <gen_cpp/Types_types.h>
25
#include <gen_cpp/segment_v2.pb.h>
26
#include <stdint.h>
27
28
#include <atomic>
29
#include <cstdint>
30
#include <fstream>
31
#include <functional>
32
#include <memory>
33
#include <mutex>
34
#include <shared_mutex>
35
#include <string>
36
#include <vector>
37
38
#include "agent/be_exec_version_manager.h"
39
#include "cctz/time_zone.h"
40
#include "common/be_mock_util.h"
41
#include "common/compiler_util.h" // IWYU pragma: keep
42
#include "common/config.h"
43
#include "common/factory_creator.h"
44
#include "common/status.h"
45
#include "io/fs/s3_file_system.h"
46
#include "runtime/task_execution_context.h"
47
#include "runtime/workload_group/workload_group.h"
48
#include "util/debug_util.h"
49
#include "util/runtime_profile.h"
50
#include "vec/runtime/vector_search_user_params.h"
51
52
namespace doris {
53
class RuntimeFilter;
54
55
23
inline int32_t get_execution_rpc_timeout_ms(int32_t execution_timeout_sec) {
56
23
    return std::min(config::execution_max_rpc_timeout_sec, execution_timeout_sec) * 1000;
57
23
}
58
59
namespace pipeline {
60
class PipelineXLocalStateBase;
61
class PipelineXSinkLocalStateBase;
62
class PipelineFragmentContext;
63
class PipelineTask;
64
class Dependency;
65
} // namespace pipeline
66
67
class DescriptorTbl;
68
class ObjectPool;
69
class ExecEnv;
70
class IdFileMap;
71
class RuntimeFilterMgr;
72
class MemTrackerLimiter;
73
class QueryContext;
74
class RuntimeFilterConsumer;
75
class RuntimeFilterProducer;
76
77
// A collection of items that are part of the global state of a
78
// query and shared across all execution nodes of that query.
79
class RuntimeState {
80
    ENABLE_FACTORY_CREATOR(RuntimeState);
81
82
public:
83
    RuntimeState(const TPlanFragmentExecParams& fragment_exec_params,
84
                 const TQueryOptions& query_options, const TQueryGlobals& query_globals,
85
                 ExecEnv* exec_env, QueryContext* ctx,
86
                 const std::shared_ptr<MemTrackerLimiter>& query_mem_tracker = nullptr);
87
88
    RuntimeState(const TUniqueId& instance_id, const TUniqueId& query_id, int32_t fragment_id,
89
                 const TQueryOptions& query_options, const TQueryGlobals& query_globals,
90
                 ExecEnv* exec_env, QueryContext* ctx);
91
92
    // Used by pipeline. This runtime state is only used for setup.
93
    RuntimeState(const TUniqueId& query_id, int32_t fragment_id, const TQueryOptions& query_options,
94
                 const TQueryGlobals& query_globals, ExecEnv* exec_env, QueryContext* ctx);
95
96
    // Only used in the materialization phase of delayed materialization,
97
    // when there may be no corresponding QueryContext.
98
    RuntimeState(const TUniqueId& query_id, int32_t fragment_id, const TQueryOptions& query_options,
99
                 const TQueryGlobals& query_globals, ExecEnv* exec_env,
100
                 const std::shared_ptr<MemTrackerLimiter>& query_mem_tracker);
101
102
    // RuntimeState for executing expr in fe-support.
103
    RuntimeState(const TQueryOptions& query_options, const TQueryGlobals& query_globals);
104
105
    // for job task only
106
    RuntimeState();
107
108
    // Empty d'tor to avoid issues with unique_ptr.
109
    MOCK_DEFINE(virtual) ~RuntimeState();
110
111
    // Set per-query state.
112
    Status init(const TUniqueId& fragment_instance_id, const TQueryOptions& query_options,
113
                const TQueryGlobals& query_globals, ExecEnv* exec_env);
114
115
    // for ut and non-query.
116
0
    void set_exec_env(ExecEnv* exec_env) { _exec_env = exec_env; }
117
118
    // for ut and non-query.
119
    void init_mem_trackers(const std::string& name = "ut", const TUniqueId& id = TUniqueId());
120
121
865
    const TQueryOptions& query_options() const { return _query_options; }
122
8
    int64_t scan_queue_mem_limit() const {
123
8
        return _query_options.__isset.scan_queue_mem_limit ? _query_options.scan_queue_mem_limit
124
8
                                                           : _query_options.mem_limit / 20;
125
8
    }
126
127
6
    int32_t max_column_reader_num() const {
128
6
        return _query_options.__isset.max_column_reader_num ? _query_options.max_column_reader_num
129
6
                                                            : 20000;
130
6
    }
131
132
220k
    ObjectPool* obj_pool() const { return _obj_pool.get(); }
133
134
546k
    const DescriptorTbl& desc_tbl() const { return *_desc_tbl; }
135
270k
    void set_desc_tbl(const DescriptorTbl* desc_tbl) { _desc_tbl = desc_tbl; }
136
54
    MOCK_FUNCTION int batch_size() const { return _query_options.batch_size; }
137
30
    int query_parallel_instance_num() const { return _query_options.parallel_instance; }
138
0
    int max_errors() const { return _query_options.max_errors; }
139
28
    int execution_timeout() const {
140
28
        return _query_options.__isset.execution_timeout ? _query_options.execution_timeout
141
28
                                                        : _query_options.query_timeout;
142
28
    }
143
0
    int max_scanners_concurrency() const {
144
0
        return _query_options.__isset.max_scanners_concurrency
145
0
                       ? _query_options.max_scanners_concurrency
146
0
                       : 0;
147
0
    }
148
0
    int max_file_scanners_concurrency() const {
149
0
        return _query_options.__isset.max_file_scanners_concurrency
150
0
                       ? _query_options.max_file_scanners_concurrency
151
0
                       : max_scanners_concurrency();
152
0
    }
153
154
28
    int min_scanners_concurrency() const {
155
28
        return _query_options.__isset.min_scanners_concurrency
156
28
                       ? _query_options.min_scanners_concurrency
157
28
                       : 1;
158
28
    }
159
160
0
    int min_file_scanners_concurrency() const {
161
0
        return _query_options.__isset.min_file_scanners_concurrency
162
0
                       ? _query_options.min_file_scanners_concurrency
163
0
                       : 1;
164
0
    }
165
166
    // Support extended regex
167
    // like look-around zero-width assertions(`?=`, `?!`, `?<=`, `?<!`)
168
26
    bool enable_extended_regex() const {
169
26
        return _query_options.__isset.enable_extended_regex && _query_options.enable_extended_regex;
170
26
    }
171
172
0
    TQueryType::type query_type() const { return _query_options.query_type; }
173
21
    int64_t timestamp_ms() const { return _timestamp_ms; }
174
12
    int32_t nano_seconds() const { return _nano_seconds; }
175
    // if possible, use timezone_obj() rather than timezone()
176
25
    const std::string& timezone() const { return _timezone; }
177
248
    const cctz::time_zone& timezone_obj() const { return _timezone_obj; }
178
3
    const std::string& lc_time_names() const { return _lc_time_names; }
179
0
    const std::string& user() const { return _user; }
180
433
    const TUniqueId& query_id() const { return _query_id; }
181
24.7k
    const TUniqueId& fragment_instance_id() const { return _fragment_instance_id; }
182
    // should only be called in pipeline engine
183
42
    int32_t fragment_id() const { return _fragment_id; }
184
50
    ExecEnv* exec_env() { return _exec_env; }
185
    std::shared_ptr<MemTrackerLimiter> query_mem_tracker() const;
186
187
    // Returns runtime state profile
188
2
    RuntimeProfile* runtime_profile() { return &_profile; }
189
0
    RuntimeProfile* load_channel_profile() { return &_load_channel_profile; }
190
191
1
    bool enable_function_pushdown() const {
192
1
        return _query_options.__isset.enable_function_pushdown &&
193
1
               _query_options.enable_function_pushdown;
194
1
    }
195
196
79
    bool check_overflow_for_decimal() const {
197
79
        return _query_options.__isset.check_overflow_for_decimal &&
198
79
               _query_options.check_overflow_for_decimal;
199
79
    }
200
201
79
    bool enable_strict_mode() const {
202
79
        return _query_options.__isset.enable_strict_cast && _query_options.enable_strict_cast;
203
79
    }
204
205
0
    bool enable_insert_strict() const {
206
0
        return _query_options.__isset.enable_insert_strict && _query_options.enable_insert_strict;
207
0
    }
208
209
0
    bool enable_common_expr_pushdown() const {
210
0
        return _query_options.__isset.enable_common_expr_pushdown &&
211
0
               _query_options.enable_common_expr_pushdown;
212
0
    }
213
214
0
    bool enable_common_expr_pushdown_for_inverted_index() const {
215
0
        return enable_common_expr_pushdown() &&
216
0
               _query_options.__isset.enable_common_expr_pushdown_for_inverted_index &&
217
0
               _query_options.enable_common_expr_pushdown_for_inverted_index;
218
0
    };
219
220
0
    bool mysql_row_binary_format() const {
221
0
        return _query_options.__isset.mysql_row_binary_format &&
222
0
               _query_options.mysql_row_binary_format;
223
0
    }
224
225
0
    bool enable_short_circuit_query_access_column_store() const {
226
0
        return _query_options.__isset.enable_short_circuit_query_access_column_store &&
227
0
               _query_options.enable_short_circuit_query_access_column_store;
228
0
    }
229
230
    // Appends error to the _error_log if there is space
231
    bool log_error(const std::string& error);
232
233
    // Returns true if the error log has not reached _max_errors.
234
0
    bool log_has_space() {
235
0
        std::lock_guard<std::mutex> l(_error_log_lock);
236
0
        return _error_log.size() < _query_options.max_errors;
237
0
    }
238
239
    // Append all _error_log[_unreported_error_idx+] to new_errors and set
240
    // _unreported_error_idx to _errors_log.size()
241
    void get_unreported_errors(std::vector<std::string>* new_errors);
242
243
    [[nodiscard]] MOCK_FUNCTION bool is_cancelled() const;
244
    MOCK_FUNCTION Status cancel_reason() const;
245
0
    void cancel(const Status& reason) {
246
0
        if (_exec_status.update(reason)) {
247
            // Create a error status, so that we could print error stack, and
248
            // we could know which path call cancel.
249
0
            LOG(WARNING) << "Task is cancelled, instance: "
250
0
                         << PrintInstanceStandardInfo(_query_id, _fragment_instance_id)
251
0
                         << ", st = " << reason;
252
0
        } else {
253
0
            LOG(WARNING) << "Task is already cancelled, instance: "
254
0
                         << PrintInstanceStandardInfo(_query_id, _fragment_instance_id)
255
0
                         << ", original cancel msg: " << _exec_status.status()
256
0
                         << ", new cancel msg: " << reason;
257
0
        }
258
0
    }
259
260
0
    void set_backend_id(int64_t backend_id) { _backend_id = backend_id; }
261
0
    int64_t backend_id() const { return _backend_id; }
262
263
46
    void set_be_number(int be_number) { _be_number = be_number; }
264
93
    int be_number(void) const { return _be_number; }
265
266
0
    std::vector<std::string>& output_files() { return _output_files; }
267
268
0
    void set_import_label(const std::string& import_label) { _import_label = import_label; }
269
270
0
    const std::vector<std::string>& export_output_files() const { return _export_output_files; }
271
272
0
    void add_export_output_file(const std::string& file) { _export_output_files.push_back(file); }
273
274
0
    void set_db_name(const std::string& db_name) { _db_name = db_name; }
275
276
0
    const std::string& db_name() { return _db_name; }
277
278
0
    void set_wal_id(int64_t wal_id) { _wal_id = wal_id; }
279
280
0
    int64_t wal_id() const { return _wal_id; }
281
282
0
    void set_content_length(size_t content_length) { _content_length = content_length; }
283
284
0
    size_t content_length() const { return _content_length; }
285
286
0
    const std::string& import_label() { return _import_label; }
287
288
0
    const std::string& load_dir() const { return _load_dir; }
289
290
0
    void set_load_job_id(int64_t job_id) { _load_job_id = job_id; }
291
292
0
    int64_t load_job_id() const { return _load_job_id; }
293
294
    std::string get_error_log_file_path();
295
296
0
    std::string get_first_error_msg() const { return _first_error_msg; }
297
298
    // append error msg and error line to file when loading data.
299
    // is_summary is true, means we are going to write the summary line
300
    // If we need to stop the processing, set stop_processing to true
301
    Status append_error_msg_to_file(std::function<std::string()> line,
302
                                    std::function<std::string()> error_msg);
303
304
0
    int64_t num_bytes_load_total() { return _num_bytes_load_total.load(); }
305
306
0
    int64_t num_finished_range() { return _num_finished_scan_range.load(); }
307
308
0
    int64_t num_rows_load_total() { return _num_rows_load_total.load(); }
309
310
0
    int64_t num_rows_load_filtered() { return _num_rows_load_filtered.load(); }
311
312
0
    int64_t num_rows_load_unselected() { return _num_rows_load_unselected.load(); }
313
314
0
    int64_t num_rows_filtered_in_strict_mode_partial_update() {
315
0
        return _num_rows_filtered_in_strict_mode_partial_update;
316
0
    }
317
318
0
    int64_t num_rows_load_success() {
319
0
        return num_rows_load_total() - num_rows_load_filtered() - num_rows_load_unselected();
320
0
    }
321
322
0
    void update_num_rows_load_total(int64_t num_rows) { _num_rows_load_total.fetch_add(num_rows); }
323
324
0
    void set_num_rows_load_total(int64_t num_rows) { _num_rows_load_total.store(num_rows); }
325
326
0
    void update_num_bytes_load_total(int64_t bytes_load) {
327
0
        _num_bytes_load_total.fetch_add(bytes_load);
328
0
    }
329
330
0
    void update_num_finished_scan_range(int64_t finished_range) {
331
0
        _num_finished_scan_range.fetch_add(finished_range);
332
0
    }
333
334
0
    void update_num_rows_load_filtered(int64_t num_rows) {
335
0
        _num_rows_load_filtered.fetch_add(num_rows);
336
0
    }
337
338
0
    void update_num_rows_load_unselected(int64_t num_rows) {
339
0
        _num_rows_load_unselected.fetch_add(num_rows);
340
0
    }
341
342
0
    void set_num_rows_filtered_in_strict_mode_partial_update(int64_t num_rows) {
343
0
        _num_rows_filtered_in_strict_mode_partial_update = num_rows;
344
0
    }
345
346
5
    void set_per_fragment_instance_idx(int idx) { _per_fragment_instance_idx = idx; }
347
348
15
    int per_fragment_instance_idx() const { return _per_fragment_instance_idx; }
349
350
5
    void set_num_per_fragment_instances(int num_instances) {
351
5
        _num_per_fragment_instances = num_instances;
352
5
    }
353
354
0
    int num_per_fragment_instances() const { return _num_per_fragment_instances; }
355
356
5
    void set_load_stream_per_node(int load_stream_per_node) {
357
5
        _load_stream_per_node = load_stream_per_node;
358
5
    }
359
360
0
    int load_stream_per_node() const { return _load_stream_per_node; }
361
362
5
    void set_total_load_streams(int total_load_streams) {
363
5
        _total_load_streams = total_load_streams;
364
5
    }
365
366
0
    int total_load_streams() const { return _total_load_streams; }
367
368
5
    void set_num_local_sink(int num_local_sink) { _num_local_sink = num_local_sink; }
369
370
0
    int num_local_sink() const { return _num_local_sink; }
371
372
0
    bool disable_stream_preaggregations() const {
373
0
        return _query_options.disable_stream_preaggregations;
374
0
    }
375
376
0
    int32_t runtime_filter_wait_time_ms() const {
377
0
        return _query_options.runtime_filter_wait_time_ms;
378
0
    }
379
380
0
    int32_t runtime_filter_max_in_num() const { return _query_options.runtime_filter_max_in_num; }
381
382
121
    int be_exec_version() const {
383
121
        DCHECK(_query_options.__isset.be_exec_version &&
384
121
               BeExecVersionManager::check_be_exec_version(_query_options.be_exec_version));
385
121
        return _query_options.be_exec_version;
386
121
    }
387
2
    bool enable_local_shuffle() const {
388
2
        return _query_options.__isset.enable_local_shuffle && _query_options.enable_local_shuffle;
389
2
    }
390
391
3
    MOCK_FUNCTION bool enable_local_exchange() const {
392
3
        return _query_options.__isset.enable_local_exchange && _query_options.enable_local_exchange;
393
3
    }
394
395
0
    bool trim_tailing_spaces_for_external_table_query() const {
396
0
        return _query_options.trim_tailing_spaces_for_external_table_query;
397
0
    }
398
399
0
    bool return_object_data_as_binary() const {
400
0
        return _query_options.return_object_data_as_binary;
401
0
    }
402
403
0
    bool enable_fuzzy_blockable_task() const {
404
0
        return _query_options.__isset.enable_fuzzy_blockable_task &&
405
0
               _query_options.enable_fuzzy_blockable_task;
406
0
    }
407
408
19
    segment_v2::CompressionTypePB fragement_transmission_compression_type() const {
409
19
        if (_query_options.__isset.fragment_transmission_compression_codec) {
410
0
            if (_query_options.fragment_transmission_compression_codec == "lz4") {
411
0
                return segment_v2::CompressionTypePB::LZ4;
412
0
            } else if (_query_options.fragment_transmission_compression_codec == "snappy") {
413
0
                return segment_v2::CompressionTypePB::SNAPPY;
414
0
            } else {
415
0
                return segment_v2::CompressionTypePB::NO_COMPRESSION;
416
0
            }
417
0
        }
418
19
        return segment_v2::CompressionTypePB::NO_COMPRESSION;
419
19
    }
420
421
0
    bool skip_storage_engine_merge() const {
422
0
        return _query_options.__isset.skip_storage_engine_merge &&
423
0
               _query_options.skip_storage_engine_merge;
424
0
    }
425
426
0
    bool skip_delete_predicate() const {
427
0
        return _query_options.__isset.skip_delete_predicate && _query_options.skip_delete_predicate;
428
0
    }
429
430
11
    bool skip_delete_bitmap() const {
431
11
        return _query_options.__isset.skip_delete_bitmap && _query_options.skip_delete_bitmap;
432
11
    }
433
434
0
    bool skip_missing_version() const {
435
0
        return _query_options.__isset.skip_missing_version && _query_options.skip_missing_version;
436
0
    }
437
438
6
    int64_t data_queue_max_blocks() const {
439
6
        return _query_options.__isset.data_queue_max_blocks ? _query_options.data_queue_max_blocks
440
6
                                                            : 1;
441
6
    }
442
443
    bool enable_page_cache() const;
444
445
0
    bool enable_prefer_cached_rowset() const {
446
0
        return _query_options.__isset.enable_prefer_cached_rowset &&
447
0
               _query_options.enable_prefer_cached_rowset;
448
0
    }
449
450
0
    int64_t query_freshness_tolerance_ms() const {
451
0
        return _query_options.query_freshness_tolerance_ms;
452
0
    }
453
454
0
    bool enable_query_freshness_tolerance() const {
455
0
        return _query_options.__isset.query_freshness_tolerance_ms &&
456
0
               _query_options.query_freshness_tolerance_ms > 0;
457
0
    }
458
459
0
    std::vector<TTabletCommitInfo> tablet_commit_infos() const {
460
0
        std::lock_guard<std::mutex> lock(_tablet_infos_mutex);
461
0
        return _tablet_commit_infos;
462
0
    }
463
464
0
    void add_tablet_commit_infos(std::vector<TTabletCommitInfo>& commit_infos) {
465
0
        std::lock_guard<std::mutex> lock(_tablet_infos_mutex);
466
0
        _tablet_commit_infos.insert(_tablet_commit_infos.end(),
467
0
                                    std::make_move_iterator(commit_infos.begin()),
468
0
                                    std::make_move_iterator(commit_infos.end()));
469
0
    }
470
471
0
    std::vector<TErrorTabletInfo> error_tablet_infos() const {
472
0
        std::lock_guard<std::mutex> lock(_tablet_infos_mutex);
473
0
        return _error_tablet_infos;
474
0
    }
475
476
0
    void add_error_tablet_infos(std::vector<TErrorTabletInfo>& tablet_infos) {
477
0
        std::lock_guard<std::mutex> lock(_tablet_infos_mutex);
478
0
        _error_tablet_infos.insert(_error_tablet_infos.end(),
479
0
                                   std::make_move_iterator(tablet_infos.begin()),
480
0
                                   std::make_move_iterator(tablet_infos.end()));
481
0
    }
482
483
0
    std::vector<THivePartitionUpdate> hive_partition_updates() const {
484
0
        std::lock_guard<std::mutex> lock(_hive_partition_updates_mutex);
485
0
        return _hive_partition_updates;
486
0
    }
487
488
0
    void add_hive_partition_updates(const THivePartitionUpdate& hive_partition_update) {
489
0
        std::lock_guard<std::mutex> lock(_hive_partition_updates_mutex);
490
0
        _hive_partition_updates.emplace_back(hive_partition_update);
491
0
    }
492
493
0
    std::vector<TIcebergCommitData> iceberg_commit_datas() const {
494
0
        std::lock_guard<std::mutex> lock(_iceberg_commit_datas_mutex);
495
0
        return _iceberg_commit_datas;
496
0
    }
497
498
0
    void add_iceberg_commit_datas(const TIcebergCommitData& iceberg_commit_data) {
499
0
        std::lock_guard<std::mutex> lock(_iceberg_commit_datas_mutex);
500
0
        _iceberg_commit_datas.emplace_back(iceberg_commit_data);
501
0
    }
502
503
    // local runtime filter mgr, the runtime filter do not have remote target or
504
    // not need local merge should regist here. the instance exec finish, the local
505
    // runtime filter mgr can release the memory of local runtime filter
506
24.7k
    RuntimeFilterMgr* local_runtime_filter_mgr() { return _runtime_filter_mgr; }
507
508
    RuntimeFilterMgr* global_runtime_filter_mgr();
509
510
122
    void set_runtime_filter_mgr(RuntimeFilterMgr* runtime_filter_mgr) {
511
122
        _runtime_filter_mgr = runtime_filter_mgr;
512
122
    }
513
514
190k
    QueryContext* get_query_ctx() { return _query_ctx; }
515
516
    [[nodiscard]] bool low_memory_mode() const;
517
518
    std::weak_ptr<QueryContext> get_query_ctx_weak();
519
    MOCK_FUNCTION WorkloadGroupPtr workload_group();
520
521
0
    void set_query_mem_tracker(const std::shared_ptr<MemTrackerLimiter>& tracker) {
522
0
        _query_mem_tracker = tracker;
523
0
    }
524
525
4.11k
    void set_query_options(const TQueryOptions& query_options) { _query_options = query_options; }
526
527
0
    bool enable_profile() const {
528
0
        return _query_options.__isset.enable_profile && _query_options.enable_profile;
529
0
    }
530
531
0
    int rpc_verbose_profile_max_instance_count() const {
532
0
        return _query_options.__isset.rpc_verbose_profile_max_instance_count
533
0
                       ? _query_options.rpc_verbose_profile_max_instance_count
534
0
                       : 0;
535
0
    }
536
537
0
    MOCK_FUNCTION bool enable_share_hash_table_for_broadcast_join() const {
538
0
        return _query_options.__isset.enable_share_hash_table_for_broadcast_join &&
539
0
               _query_options.enable_share_hash_table_for_broadcast_join;
540
0
    }
541
542
0
    bool enable_parallel_scan() const {
543
0
        return _query_options.__isset.enable_parallel_scan && _query_options.enable_parallel_scan;
544
0
    }
545
546
0
    bool is_read_csv_empty_line_as_null() const {
547
0
        return _query_options.__isset.read_csv_empty_line_as_null &&
548
0
               _query_options.read_csv_empty_line_as_null;
549
0
    }
550
551
0
    int parallel_scan_max_scanners_count() const {
552
0
        return _query_options.__isset.parallel_scan_max_scanners_count
553
0
                       ? _query_options.parallel_scan_max_scanners_count
554
0
                       : 0;
555
0
    }
556
557
431
    int partition_topn_max_partitions() const {
558
431
        return _query_options.__isset.partition_topn_max_partitions
559
431
                       ? _query_options.partition_topn_max_partitions
560
431
                       : 1024;
561
431
    }
562
563
0
    int partition_topn_per_partition_rows() const {
564
0
        return _query_options.__isset.partition_topn_pre_partition_rows
565
0
                       ? _query_options.partition_topn_pre_partition_rows
566
0
                       : 1000;
567
0
    }
568
569
0
    int64_t parallel_scan_min_rows_per_scanner() const {
570
0
        return _query_options.__isset.parallel_scan_min_rows_per_scanner
571
0
                       ? _query_options.parallel_scan_min_rows_per_scanner
572
0
                       : 0;
573
0
    }
574
575
0
    void set_be_exec_version(int32_t version) noexcept { _query_options.be_exec_version = version; }
576
577
    using LocalState = doris::pipeline::PipelineXLocalStateBase;
578
    using SinkLocalState = doris::pipeline::PipelineXSinkLocalStateBase;
579
    // get result can return an error message, and we will only call it during the prepare.
580
    void emplace_local_state(int id, std::unique_ptr<LocalState> state);
581
582
    LocalState* get_local_state(int id);
583
    Result<LocalState*> get_local_state_result(int id);
584
585
    void emplace_sink_local_state(int id, std::unique_ptr<SinkLocalState> state);
586
587
    SinkLocalState* get_sink_local_state();
588
589
    Result<SinkLocalState*> get_sink_local_state_result();
590
591
    void resize_op_id_to_local_state(int operator_size);
592
593
    std::vector<std::shared_ptr<RuntimeProfile>> pipeline_id_to_profile();
594
595
    std::vector<std::shared_ptr<RuntimeProfile>> build_pipeline_profile(std::size_t pipeline_size);
596
597
72.6k
    void set_task_execution_context(std::shared_ptr<TaskExecutionContext> context) {
598
72.6k
        _task_execution_context_inited = true;
599
72.6k
        _task_execution_context = context;
600
72.6k
    }
601
602
86
    std::weak_ptr<TaskExecutionContext> get_task_execution_context() {
603
86
        CHECK(_task_execution_context_inited)
604
0
                << "_task_execution_context_inited == false, the ctx is not inited";
605
86
        return _task_execution_context;
606
86
    }
607
608
    Status register_producer_runtime_filter(
609
            const doris::TRuntimeFilterDesc& desc,
610
            std::shared_ptr<RuntimeFilterProducer>* producer_filter);
611
612
    Status register_consumer_runtime_filter(
613
            const doris::TRuntimeFilterDesc& desc, bool need_local_merge, int node_id,
614
            std::shared_ptr<RuntimeFilterConsumer>* consumer_filter);
615
616
    bool is_nereids() const;
617
618
1.91k
    bool enable_spill() const {
619
1.91k
        return (_query_options.__isset.enable_force_spill && _query_options.enable_force_spill) ||
620
1.91k
               (_query_options.__isset.enable_spill && _query_options.enable_spill);
621
1.91k
    }
622
623
0
    bool enable_force_spill() const {
624
0
        return _query_options.__isset.enable_force_spill && _query_options.enable_force_spill;
625
0
    }
626
627
0
    int64_t spill_min_revocable_mem() const {
628
0
        if (_query_options.__isset.min_revocable_mem) {
629
0
            return std::max(_query_options.min_revocable_mem, (int64_t)1);
630
0
        }
631
0
        return 1;
632
0
    }
633
634
5
    int64_t spill_sort_mem_limit() const {
635
5
        if (_query_options.__isset.spill_sort_mem_limit) {
636
5
            return std::max(_query_options.spill_sort_mem_limit, (int64_t)16777216);
637
5
        }
638
0
        return 134217728;
639
5
    }
640
641
15
    int64_t spill_sort_batch_bytes() const {
642
15
        if (_query_options.__isset.spill_sort_batch_bytes) {
643
15
            return std::max(_query_options.spill_sort_batch_bytes, (int64_t)8388608);
644
15
        }
645
0
        return 8388608;
646
15
    }
647
648
11
    int spill_aggregation_partition_count() const {
649
11
        if (_query_options.__isset.spill_aggregation_partition_count) {
650
11
            return std::min(std::max(_query_options.spill_aggregation_partition_count, 16), 8192);
651
11
        }
652
0
        return 32;
653
11
    }
654
655
0
    int spill_hash_join_partition_count() const {
656
0
        if (_query_options.__isset.spill_hash_join_partition_count) {
657
0
            return std::min(std::max(_query_options.spill_hash_join_partition_count, 16), 8192);
658
0
        }
659
0
        return 32;
660
0
    }
661
662
0
    int64_t low_memory_mode_buffer_limit() const {
663
0
        if (_query_options.__isset.low_memory_mode_buffer_limit) {
664
0
            return std::max(_query_options.low_memory_mode_buffer_limit, (int64_t)1);
665
0
        }
666
0
        return 32L * 1024 * 1024;
667
0
    }
668
669
1
    int spill_revocable_memory_high_watermark_percent() const {
670
1
        if (_query_options.__isset.revocable_memory_high_watermark_percent) {
671
1
            return _query_options.revocable_memory_high_watermark_percent;
672
1
        }
673
0
        return -1;
674
1
    }
675
676
0
    MOCK_FUNCTION bool enable_shared_exchange_sink_buffer() const {
677
0
        return _query_options.__isset.enable_shared_exchange_sink_buffer &&
678
0
               _query_options.enable_shared_exchange_sink_buffer;
679
0
    }
680
681
799k
    size_t minimum_operator_memory_required_bytes() const {
682
799k
        if (_query_options.__isset.minimum_operator_memory_required_kb) {
683
799k
            return _query_options.minimum_operator_memory_required_kb * 1024;
684
799k
        } else {
685
            // refer other database
686
0
            return 100 * 1024;
687
0
        }
688
799k
    }
689
690
72.0k
    void set_max_operator_id(int max_operator_id) { _max_operator_id = max_operator_id; }
691
692
10
    int max_operator_id() const { return _max_operator_id; }
693
694
16
    void set_task_id(int id) { _task_id = id; }
695
696
3.63k
    int task_id() const { return _task_id; }
697
698
5
    void set_task_num(int task_num) { _task_num = task_num; }
699
700
0
    int task_num() const { return _task_num; }
701
702
0
    int profile_level() const { return _profile_level; }
703
704
0
    std::shared_ptr<IdFileMap>& get_id_file_map() { return _id_file_map; }
705
706
    void set_id_file_map();
707
14
    VectorSearchUserParams get_vector_search_params() const {
708
14
        return VectorSearchUserParams(_query_options.hnsw_ef_search,
709
14
                                      _query_options.hnsw_check_relative_distance,
710
14
                                      _query_options.hnsw_bounded_queue, _query_options.ivf_nprobe);
711
14
    }
712
713
private:
714
    Status create_error_log_file();
715
716
    static const int DEFAULT_BATCH_SIZE = 4062;
717
718
    std::shared_ptr<MemTrackerLimiter> _query_mem_tracker = nullptr;
719
720
    // Could not find a better way to record if the weak ptr is inited, use a bool to record
721
    // it. In some unit test cases, the runtime state's task ctx is not inited, then the test
722
    // hang, it is very hard to debug.
723
    bool _task_execution_context_inited = false;
724
    // Hold execution context for other threads
725
    std::weak_ptr<TaskExecutionContext> _task_execution_context;
726
727
    // put runtime state before _obj_pool, so that it will be deconstructed after
728
    // _obj_pool. Because some of object in _obj_pool will use profile when deconstructing.
729
    RuntimeProfile _profile;
730
    RuntimeProfile _load_channel_profile;
731
    // Why 2?
732
    // During cluster upgrade, fe will not pass profile_level to be, so we need to set it to 2
733
    // to make sure user can see all profile counters like before.
734
    int _profile_level = 2;
735
736
    const DescriptorTbl* _desc_tbl = nullptr;
737
    std::shared_ptr<ObjectPool> _obj_pool;
738
739
    // owned by PipelineFragmentContext
740
    RuntimeFilterMgr* _runtime_filter_mgr = nullptr;
741
742
    // Lock protecting _error_log and _unreported_error_idx
743
    std::mutex _error_log_lock;
744
745
    // Logs error messages.
746
    std::vector<std::string> _error_log;
747
748
    // _error_log[_unreported_error_idx+] has been not reported to the coordinator.
749
    int _unreported_error_idx;
750
751
    // Username of user that is executing the query to which this RuntimeState belongs.
752
    std::string _user;
753
754
    //Query-global timestamp_ms
755
    int64_t _timestamp_ms;
756
    int32_t _nano_seconds;
757
    std::string _timezone;
758
    cctz::time_zone _timezone_obj;
759
    std::string _lc_time_names;
760
761
    TUniqueId _query_id;
762
    // fragment id for each TPipelineFragmentParams
763
    int32_t _fragment_id;
764
    TUniqueId _fragment_instance_id;
765
    TQueryOptions _query_options;
766
    ExecEnv* _exec_env = nullptr;
767
768
    AtomicStatus _exec_status;
769
770
    int _per_fragment_instance_idx;
771
    int _num_per_fragment_instances = 0;
772
    int _load_stream_per_node = 0;
773
    int _total_load_streams = 0;
774
    int _num_local_sink = 0;
775
776
    // The backend id on which this fragment instance runs
777
    int64_t _backend_id = -1;
778
779
    // used as send id
780
    int _be_number;
781
782
    // put here to collect files??
783
    std::vector<std::string> _output_files;
784
    std::atomic<int64_t> _num_rows_load_total;      // total rows read from source
785
    std::atomic<int64_t> _num_rows_load_filtered;   // unqualified rows
786
    std::atomic<int64_t> _num_rows_load_unselected; // rows filtered by predicates
787
    std::atomic<int64_t> _num_rows_filtered_in_strict_mode_partial_update;
788
    std::atomic<int64_t> _num_print_error_rows;
789
790
    std::atomic<int64_t> _num_bytes_load_total; // total bytes read from source
791
    std::atomic<int64_t> _num_finished_scan_range;
792
793
    std::vector<std::string> _export_output_files;
794
    std::string _import_label;
795
    std::string _db_name;
796
    std::string _load_dir;
797
    int64_t _load_job_id;
798
    int64_t _wal_id = -1;
799
    size_t _content_length = 0;
800
801
    // mini load
802
    std::string _error_log_file_path;
803
    std::unique_ptr<std::ofstream> _error_log_file; // error file path, absolute path
804
    std::string _first_error_msg = "";
805
    mutable std::mutex _tablet_infos_mutex;
806
    std::vector<TTabletCommitInfo> _tablet_commit_infos;
807
    std::vector<TErrorTabletInfo> _error_tablet_infos;
808
    int _max_operator_id = 0;
809
    int _task_id = -1;
810
    int _task_num = 0;
811
812
    mutable std::mutex _hive_partition_updates_mutex;
813
    std::vector<THivePartitionUpdate> _hive_partition_updates;
814
815
    mutable std::mutex _iceberg_commit_datas_mutex;
816
    std::vector<TIcebergCommitData> _iceberg_commit_datas;
817
818
    std::vector<std::unique_ptr<doris::pipeline::PipelineXLocalStateBase>> _op_id_to_local_state;
819
820
    std::unique_ptr<doris::pipeline::PipelineXSinkLocalStateBase> _sink_local_state;
821
822
    QueryContext* _query_ctx = nullptr;
823
824
    // true if max_filter_ratio is 0
825
    bool _load_zero_tolerance = false;
826
827
    // only to lock _pipeline_id_to_profile
828
    std::shared_mutex _pipeline_profile_lock;
829
    std::vector<std::shared_ptr<RuntimeProfile>> _pipeline_id_to_profile;
830
831
    // save error log to s3
832
    std::shared_ptr<io::S3FileSystem> _s3_error_fs;
833
    // error file path on s3, ${bucket}/${prefix}/error_log/${label}_${fragment_instance_id}
834
    std::string _s3_error_log_file_path;
835
    std::mutex _s3_error_log_file_lock;
836
837
    // used for encoding the global lazy materialize
838
    std::shared_ptr<IdFileMap> _id_file_map = nullptr;
839
};
840
841
#define RETURN_IF_CANCELLED(state)               \
842
667
    do {                                         \
843
667
        if (UNLIKELY((state)->is_cancelled())) { \
844
0
            return (state)->cancel_reason();     \
845
0
        }                                        \
846
667
    } while (false)
847
848
} // namespace doris