Coverage Report

Created: 2026-03-04 17:29

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