Coverage Report

Created: 2026-03-04 15:22

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