Coverage Report

Created: 2026-03-25 20:26

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