Coverage Report

Created: 2026-08-01 19:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/runtime/runtime_state.cpp
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
// This file is copied from
18
// https://github.com/apache/impala/blob/branch-2.9.0/be/src/runtime/runtime-state.cpp
19
// and modified by Doris
20
21
#include "runtime/runtime_state.h"
22
23
#include <fmt/format.h>
24
#include <gen_cpp/PaloInternalService_types.h>
25
#include <gen_cpp/Types_types.h>
26
#include <glog/logging.h>
27
28
#include <fstream>
29
#include <memory>
30
#include <string>
31
32
#include "cloud/cloud_storage_engine.h"
33
#include "cloud/config.h"
34
#include "common/config.h"
35
#include "common/logging.h"
36
#include "common/object_pool.h"
37
#include "common/status.h"
38
#include "core/value/vdatetime_value.h"
39
#include "exec/operator/operator.h"
40
#include "exec/pipeline/pipeline_fragment_context.h"
41
#include "exec/pipeline/pipeline_task.h"
42
#include "exec/runtime_filter/runtime_filter_consumer.h"
43
#include "exec/runtime_filter/runtime_filter_mgr.h"
44
#include "exec/runtime_filter/runtime_filter_producer.h"
45
#include "exprs/function/cast/cast_to_date_or_datetime_impl.hpp"
46
#include "io/fs/s3_file_system.h"
47
#include "load/load_path_mgr.h"
48
#include "runtime/exec_env.h"
49
#include "runtime/fragment_mgr.h"
50
#include "runtime/memory/mem_tracker_limiter.h"
51
#include "runtime/query_context.h"
52
#include "runtime/thread_context.h"
53
#include "storage/id_manager.h"
54
#include "storage/storage_engine.h"
55
#include "util/thrift_util.h"
56
#include "util/timezone_utils.h"
57
#include "util/uid_util.h"
58
59
namespace doris {
60
using namespace ErrorCode;
61
62
1
Status RuntimeState::add_iceberg_commit_datas(TIcebergCommitData iceberg_commit_data) {
63
1
    ThriftSerializer serializer(false, 256);
64
1
    uint32_t serialized_size = 0;
65
1
    uint8_t* buffer = nullptr;
66
1
    RETURN_IF_ERROR(serializer.serialize(&iceberg_commit_data, &serialized_size, &buffer));
67
68
1
    constexpr size_t report_envelope_headroom = 1024 * 1024;
69
1
    const size_t thrift_limit = static_cast<size_t>(std::max(config::thrift_max_message_size, 0));
70
1
    const size_t commit_data_limit =
71
1
            thrift_limit > report_envelope_headroom ? thrift_limit - report_envelope_headroom : 0;
72
1
    std::lock_guard<std::mutex> lock(_iceberg_commit_datas_mutex);
73
    // Reject metadata while its file can still be removed; a later oversized report would strand every output.
74
1
    if (_iceberg_commit_datas_serialized_bytes + serialized_size + sizeof(uint32_t) >
75
1
        commit_data_limit) {
76
1
        return Status::InternalError(
77
1
                "Iceberg commit metadata exceeds the Thrift report limit; reduce output file "
78
1
                "count");
79
1
    }
80
0
    _iceberg_commit_datas_serialized_bytes += serialized_size + sizeof(uint32_t);
81
0
    _iceberg_commit_datas.emplace_back(std::move(iceberg_commit_data));
82
0
    return Status::OK();
83
1
}
84
85
RuntimeState::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)
89
0
        : _profile("Fragment " + print_id(fragment_exec_params.fragment_instance_id)),
90
0
          _load_channel_profile("<unnamed>"),
91
0
          _obj_pool(new ObjectPool()),
92
0
          _unreported_error_idx(0),
93
0
          _query_id(fragment_exec_params.query_id),
94
0
          _per_fragment_instance_idx(0),
95
0
          _num_rows_load_total(0),
96
0
          _num_rows_load_filtered(0),
97
0
          _num_rows_load_unselected(0),
98
0
          _num_print_error_rows(0),
99
0
          _num_bytes_load_total(0),
100
0
          _num_finished_scan_range(0),
101
0
          _query_ctx(ctx) {
102
0
    Status status =
103
0
            init(fragment_exec_params.fragment_instance_id, query_options, query_globals, exec_env);
104
0
    DCHECK(status.ok());
105
0
    _query_mem_tracker = query_mem_tracker;
106
0
    DCHECK(_query_mem_tracker != nullptr);
107
0
}
108
109
RuntimeState::RuntimeState(const TUniqueId& instance_id, const TUniqueId& query_id,
110
                           int32_t fragment_id, const TQueryOptions& query_options,
111
                           const TQueryGlobals& query_globals, ExecEnv* exec_env, QueryContext* ctx)
112
81
        : _profile("Fragment " + print_id(instance_id)),
113
81
          _load_channel_profile("<unnamed>"),
114
81
          _obj_pool(new ObjectPool()),
115
81
          _unreported_error_idx(0),
116
81
          _query_id(query_id),
117
81
          _fragment_id(fragment_id),
118
81
          _per_fragment_instance_idx(0),
119
81
          _num_rows_load_total(0),
120
81
          _num_rows_load_filtered(0),
121
81
          _num_rows_load_unselected(0),
122
81
          _num_rows_filtered_in_strict_mode_partial_update(0),
123
81
          _num_print_error_rows(0),
124
81
          _num_bytes_load_total(0),
125
81
          _num_finished_scan_range(0),
126
81
          _query_ctx(ctx) {
127
81
    [[maybe_unused]] auto status = init(instance_id, query_options, query_globals, exec_env);
128
81
    DCHECK(status.ok());
129
81
    _query_mem_tracker = ctx->query_mem_tracker();
130
81
}
131
132
RuntimeState::RuntimeState(const TUniqueId& query_id, int32_t fragment_id,
133
                           const TQueryOptions& query_options, const TQueryGlobals& query_globals,
134
                           ExecEnv* exec_env, QueryContext* ctx)
135
184
        : _profile(fmt::format("PipelineX(fragment_id={})", fragment_id)),
136
184
          _load_channel_profile("<unnamed>"),
137
184
          _obj_pool(new ObjectPool()),
138
184
          _unreported_error_idx(0),
139
184
          _query_id(query_id),
140
184
          _fragment_id(fragment_id),
141
184
          _per_fragment_instance_idx(0),
142
184
          _num_rows_load_total(0),
143
184
          _num_rows_load_filtered(0),
144
184
          _num_rows_load_unselected(0),
145
184
          _num_rows_filtered_in_strict_mode_partial_update(0),
146
184
          _num_print_error_rows(0),
147
184
          _num_bytes_load_total(0),
148
184
          _num_finished_scan_range(0),
149
184
          _query_ctx(ctx) {
150
    // TODO: do we really need instance id?
151
184
    Status status = init(TUniqueId(), query_options, query_globals, exec_env);
152
184
    DCHECK(status.ok());
153
184
    _query_mem_tracker = ctx->query_mem_tracker();
154
184
}
155
156
RuntimeState::RuntimeState(const TUniqueId& query_id, int32_t fragment_id,
157
                           const TQueryOptions& query_options, const TQueryGlobals& query_globals,
158
                           ExecEnv* exec_env,
159
                           const std::shared_ptr<MemTrackerLimiter>& query_mem_tracker)
160
0
        : _profile(fmt::format("PipelineX(fragment_id={})", fragment_id)),
161
0
          _load_channel_profile("<unnamed>"),
162
0
          _obj_pool(new ObjectPool()),
163
0
          _unreported_error_idx(0),
164
0
          _query_id(query_id),
165
0
          _fragment_id(fragment_id),
166
0
          _per_fragment_instance_idx(0),
167
0
          _num_rows_load_total(0),
168
0
          _num_rows_load_filtered(0),
169
0
          _num_rows_load_unselected(0),
170
0
          _num_rows_filtered_in_strict_mode_partial_update(0),
171
0
          _num_print_error_rows(0),
172
0
          _num_bytes_load_total(0),
173
0
          _num_finished_scan_range(0) {
174
0
    Status status = init(TUniqueId(), query_options, query_globals, exec_env);
175
0
    DCHECK(status.ok());
176
0
    _query_mem_tracker = query_mem_tracker;
177
0
    DCHECK(_query_mem_tracker != nullptr);
178
0
}
179
180
RuntimeState::RuntimeState(const TQueryOptions& query_options, const TQueryGlobals& query_globals)
181
50.6k
        : _profile("<unnamed>"),
182
50.6k
          _load_channel_profile("<unnamed>"),
183
50.6k
          _obj_pool(new ObjectPool()),
184
50.6k
          _unreported_error_idx(0),
185
50.6k
          _per_fragment_instance_idx(0) {
186
50.6k
    Status status = init(TUniqueId(), query_options, query_globals, nullptr);
187
50.6k
    _exec_env = ExecEnv::GetInstance();
188
50.6k
    init_mem_trackers("<unnamed>");
189
50.6k
    DCHECK(status.ok());
190
50.6k
}
191
192
RuntimeState::RuntimeState()
193
131k
        : _profile("<unnamed>"),
194
131k
          _load_channel_profile("<unnamed>"),
195
131k
          _obj_pool(new ObjectPool()),
196
131k
          _unreported_error_idx(0),
197
131k
          _per_fragment_instance_idx(0) {
198
131k
    _query_options.batch_size = DEFAULT_BATCH_SIZE;
199
131k
    _query_options.be_exec_version = BeExecVersionManager::get_newest_version();
200
131k
    _timezone = TimezoneUtils::default_time_zone;
201
131k
    _timestamp_ms = 0;
202
131k
    _nano_seconds = 0;
203
131k
    TimezoneUtils::find_cctz_time_zone(_timezone, _timezone_obj);
204
131k
    _exec_env = ExecEnv::GetInstance();
205
131k
    init_mem_trackers("<unnamed>");
206
131k
}
207
208
182k
RuntimeState::~RuntimeState() {
209
182k
    SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER(_query_mem_tracker);
210
    // close error log file
211
182k
    if (_error_log_file != nullptr && _error_log_file->is_open()) {
212
0
        _error_log_file->close();
213
0
    }
214
182k
    _obj_pool->clear();
215
182k
}
216
217
2
const std::set<int>& RuntimeState::get_deregister_runtime_filter() const {
218
2
    return _registered_runtime_filter_ids;
219
2
}
220
221
2
void RuntimeState::merge_register_runtime_filter(const std::set<int>& runtime_filter_ids) {
222
2
    _registered_runtime_filter_ids.insert(runtime_filter_ids.begin(), runtime_filter_ids.end());
223
2
}
224
225
Status RuntimeState::init(const TUniqueId& fragment_instance_id, const TQueryOptions& query_options,
226
50.8k
                          const TQueryGlobals& query_globals, ExecEnv* exec_env) {
227
50.8k
    _fragment_instance_id = fragment_instance_id;
228
50.8k
    _query_options = query_options;
229
50.8k
    _lc_time_names = query_globals.lc_time_names;
230
50.8k
    if (query_globals.__isset.time_zone && query_globals.__isset.nano_seconds) {
231
49.5k
        _timezone = query_globals.time_zone;
232
49.5k
        _timestamp_ms = query_globals.timestamp_ms;
233
49.5k
        _nano_seconds = query_globals.nano_seconds;
234
49.5k
    } else if (query_globals.__isset.time_zone) {
235
0
        _timezone = query_globals.time_zone;
236
0
        _timestamp_ms = query_globals.timestamp_ms;
237
0
        _nano_seconds = 0;
238
1.38k
    } else if (!query_globals.now_string.empty()) {
239
0
        _timezone = TimezoneUtils::default_time_zone;
240
0
        VecDateTimeValue dt;
241
0
        CastParameters params;
242
0
        DORIS_CHECK((CastToDateOrDatetime::from_string_strict_mode<DatelikeParseMode::STRICT,
243
0
                                                                   DatelikeTargetType::DATE_TIME>(
244
0
                {query_globals.now_string.c_str(), query_globals.now_string.size()}, dt, nullptr,
245
0
                params)));
246
0
        int64_t timestamp;
247
0
        dt.unix_timestamp(&timestamp, _timezone);
248
0
        _timestamp_ms = timestamp * 1000;
249
0
        _nano_seconds = 0;
250
1.38k
    } else {
251
        //Unit test may set into here
252
1.38k
        _timezone = TimezoneUtils::default_time_zone;
253
1.38k
        _timestamp_ms = 0;
254
1.38k
        _nano_seconds = 0;
255
1.38k
    }
256
50.8k
    TimezoneUtils::find_cctz_time_zone(_timezone, _timezone_obj);
257
258
50.8k
    if (query_globals.__isset.load_zero_tolerance) {
259
50.8k
        _load_zero_tolerance = query_globals.load_zero_tolerance;
260
50.8k
    }
261
262
50.8k
    _exec_env = exec_env;
263
264
50.8k
    if (_query_options.max_errors <= 0) {
265
        // TODO: fix linker error and uncomment this
266
        //_query_options.max_errors = config::max_errors;
267
50.4k
        _query_options.max_errors = 100;
268
50.4k
    }
269
270
50.8k
    if (_query_options.batch_size <= 0) {
271
50.3k
        _query_options.batch_size = DEFAULT_BATCH_SIZE;
272
50.3k
    }
273
274
50.8k
    _db_name = "insert_stmt";
275
50.8k
    _import_label = print_id(fragment_instance_id);
276
277
50.8k
    _profile_level = query_options.__isset.profile_level ? query_options.profile_level : 2;
278
279
50.8k
    return Status::OK();
280
50.8k
}
281
282
0
std::weak_ptr<QueryContext> RuntimeState::get_query_ctx_weak() {
283
0
    return _exec_env->fragment_mgr()->get_query_ctx(_query_ctx->query_id());
284
0
}
285
286
181k
void RuntimeState::init_mem_trackers(const std::string& name, const TUniqueId& id) {
287
181k
    _query_mem_tracker = MemTrackerLimiter::create_shared(
288
181k
            MemTrackerLimiter::Type::OTHER, fmt::format("{}#Id={}", name, print_id(id)));
289
181k
}
290
291
203
std::shared_ptr<MemTrackerLimiter> RuntimeState::query_mem_tracker() const {
292
203
    CHECK(_query_mem_tracker != nullptr);
293
203
    return _query_mem_tracker;
294
203
}
295
296
11
WorkloadGroupPtr RuntimeState::workload_group() {
297
11
    return _query_ctx->workload_group();
298
11
}
299
300
0
bool RuntimeState::log_error(const std::string& error) {
301
0
    std::lock_guard<std::mutex> l(_error_log_lock);
302
303
0
    if (_error_log.size() < _query_options.max_errors) {
304
0
        _error_log.push_back(error);
305
0
        return true;
306
0
    }
307
308
0
    return false;
309
0
}
310
311
0
void RuntimeState::get_unreported_errors(std::vector<std::string>* new_errors) {
312
0
    std::lock_guard<std::mutex> l(_error_log_lock);
313
314
0
    if (_unreported_error_idx < _error_log.size()) {
315
0
        new_errors->assign(_error_log.begin() + _unreported_error_idx, _error_log.end());
316
0
        _unreported_error_idx = (int)_error_log.size();
317
0
    }
318
0
}
319
320
1.10M
bool RuntimeState::is_cancelled() const {
321
    // Maybe we should just return _is_cancelled.load()
322
1.10M
    return !_exec_status.ok() || (_query_ctx && _query_ctx->is_cancelled());
323
1.10M
}
324
325
13
Status RuntimeState::cancel_reason() const {
326
13
    if (!_exec_status.ok()) {
327
13
        return _exec_status.status();
328
13
    }
329
330
0
    if (_query_ctx) {
331
0
        return _query_ctx->exec_status();
332
0
    }
333
334
0
    return Status::Cancelled("Query cancelled");
335
0
}
336
337
const int64_t MAX_ERROR_NUM = 50;
338
339
0
Status RuntimeState::create_error_log_file() {
340
0
    if (config::save_load_error_log_to_s3 && config::is_cloud_mode()) {
341
0
        _s3_error_fs = std::dynamic_pointer_cast<io::S3FileSystem>(
342
0
                ExecEnv::GetInstance()->storage_engine().to_cloud().latest_fs());
343
0
        if (_s3_error_fs) {
344
0
            std::stringstream ss;
345
            // https://dev.mysql.com/doc/dev/mysql-server/latest/page_protocol_basic_err_packet.html
346
            // shorten the path as much as possible to prevent the length of the presigned URL from
347
            // exceeding the MySQL error packet size limit
348
0
            ss << "error_log/" << std::hex << _fragment_instance_id.lo;
349
0
            _s3_error_log_file_path = ss.str();
350
0
        }
351
0
    }
352
353
0
    static_cast<void>(_exec_env->load_path_mgr()->get_load_error_file_name(
354
0
            _db_name, _import_label, _fragment_instance_id, &_error_log_file_path));
355
0
    std::string error_log_absolute_path =
356
0
            _exec_env->load_path_mgr()->get_load_error_absolute_path(_error_log_file_path);
357
0
    _error_log_file = std::make_unique<std::ofstream>(error_log_absolute_path, std::ifstream::out);
358
0
    if (!_error_log_file->is_open()) {
359
0
        std::stringstream error_msg;
360
0
        error_msg << "Fail to open error file: [" << _error_log_file_path << "].";
361
0
        LOG(WARNING) << error_msg.str();
362
0
        return Status::InternalError(error_msg.str());
363
0
    }
364
0
    LOG(INFO) << "create error log file: " << _error_log_file_path
365
0
              << ", query id: " << print_id(_query_id)
366
0
              << ", fragment instance id: " << print_id(_fragment_instance_id);
367
368
0
    return Status::OK();
369
0
}
370
371
Status RuntimeState::append_error_msg_to_file(std::function<std::string()> line,
372
0
                                              std::function<std::string()> error_msg) {
373
0
    if (query_type() != TQueryType::LOAD) {
374
0
        return Status::OK();
375
0
    }
376
    // If file haven't been opened, open it here
377
0
    if (_error_log_file == nullptr) {
378
0
        Status status = create_error_log_file();
379
0
        if (!status.ok()) {
380
0
            LOG(WARNING) << "Create error file log failed. because: " << status;
381
0
            if (_error_log_file != nullptr) {
382
0
                _error_log_file->close();
383
0
            }
384
0
            return status;
385
0
        }
386
        // record the first error message if the file is just created
387
0
        _first_error_msg = error_msg() + ". Src line: " + line();
388
0
        LOG(INFO) << "The first error message: " << _first_error_msg;
389
0
    }
390
    // If num of printed error row exceeds the limit, don't add error messages to error log file any more
391
0
    if (_num_print_error_rows.fetch_add(1, std::memory_order_relaxed) > MAX_ERROR_NUM) {
392
        // if _load_zero_tolerance, return Error to stop the load process immediately.
393
0
        if (_load_zero_tolerance) {
394
0
            return Status::DataQualityError(
395
0
                    "Encountered unqualified data, stop processing. Please check if the source "
396
0
                    "data matches the schema, and consider disabling strict mode or increasing "
397
0
                    "max_filter_ratio.");
398
0
        }
399
0
        return Status::OK();
400
0
    }
401
402
0
    fmt::memory_buffer out;
403
    // Note: export reason first in case src line too long and be truncated.
404
0
    fmt::format_to(out, "Reason: {}. src line [{}]; ", error_msg(), line());
405
406
0
    size_t error_row_size = out.size();
407
0
    if (error_row_size > 0) {
408
0
        if (error_row_size > config::load_error_log_limit_bytes) {
409
0
            fmt::memory_buffer limit_byte_out;
410
0
            limit_byte_out.append(out.data(), out.data() + config::load_error_log_limit_bytes);
411
0
            (*_error_log_file) << fmt::to_string(limit_byte_out) + "error log is too long"
412
0
                               << std::endl;
413
0
        } else {
414
0
            (*_error_log_file) << fmt::to_string(out) << std::endl;
415
0
        }
416
0
    }
417
418
0
    return Status::OK();
419
0
}
420
421
1
std::string RuntimeState::get_error_log_file_path() {
422
1
    DBUG_EXECUTE_IF("RuntimeState::get_error_log_file_path.block", {
423
1
        if (!_error_log_file_path.empty()) {
424
1
            std::this_thread::sleep_for(std::chrono::seconds(1));
425
1
        }
426
1
    });
427
1
    std::lock_guard<std::mutex> l(_s3_error_log_file_lock);
428
1
    if (_s3_error_fs && _error_log_file && _error_log_file->is_open()) {
429
        // close error log file
430
0
        _error_log_file->close();
431
0
        std::string error_log_absolute_path =
432
0
                _exec_env->load_path_mgr()->get_load_error_absolute_path(_error_log_file_path);
433
        // upload error log file to s3
434
0
        Status st = _s3_error_fs->upload(error_log_absolute_path, _s3_error_log_file_path);
435
0
        if (!st.ok()) {
436
            // upload failed and return local error log file path
437
0
            LOG(WARNING) << "Fail to upload error file to s3, error_log_file_path="
438
0
                         << _error_log_file_path << ", error=" << st;
439
0
            return _error_log_file_path;
440
0
        }
441
        // expiration must be less than a week (in seconds) for presigned url
442
0
        static const unsigned EXPIRATION_SECONDS = 7 * 24 * 60 * 60 - 1;
443
        // Use public or private endpoint based on configuration
444
0
        _error_log_file_path =
445
0
                _s3_error_fs->generate_presigned_url(_s3_error_log_file_path, EXPIRATION_SECONDS,
446
0
                                                     config::use_public_endpoint_for_error_log);
447
0
    }
448
1
    return _error_log_file_path;
449
1
}
450
451
72.4k
void RuntimeState::resize_op_id_to_local_state(int operator_size) {
452
72.4k
    _op_id_to_local_state.resize(-operator_size);
453
72.4k
}
454
455
void RuntimeState::emplace_local_state(int id,
456
24.3k
                                       std::unique_ptr<doris::PipelineXLocalStateBase> state) {
457
24.3k
    id = -id;
458
24.3k
    DCHECK_LT(id, _op_id_to_local_state.size())
459
0
            << state->parent()->get_name() << " node id = " << state->parent()->node_id();
460
24.3k
    DCHECK(!_op_id_to_local_state[id]);
461
24.3k
    _op_id_to_local_state[id] = std::move(state);
462
24.3k
}
463
464
4.48M
doris::PipelineXLocalStateBase* RuntimeState::get_local_state(int id) {
465
4.48M
    DCHECK_GT(_op_id_to_local_state.size(), -id);
466
4.48M
    return _op_id_to_local_state[-id].get();
467
4.48M
}
468
469
24.0k
Result<RuntimeState::LocalState*> RuntimeState::get_local_state_result(int id) {
470
24.0k
    id = -id;
471
24.0k
    if (id >= _op_id_to_local_state.size()) {
472
0
        return ResultError(Status::InternalError("get_local_state out of range size:{} , id:{}",
473
0
                                                 _op_id_to_local_state.size(), id));
474
0
    }
475
24.0k
    if (!_op_id_to_local_state[id]) {
476
0
        return ResultError(Status::InternalError("get_local_state id:{} is null", id));
477
0
    }
478
24.0k
    return _op_id_to_local_state[id].get();
479
24.0k
};
480
481
void RuntimeState::emplace_sink_local_state(
482
72.3k
        int id, std::unique_ptr<doris::PipelineXSinkLocalStateBase> state) {
483
72.3k
    DCHECK(!_sink_local_state) << " id=" << id << " state: " << state->debug_string(0);
484
72.3k
    _sink_local_state = std::move(state);
485
72.3k
}
486
487
404k
doris::PipelineXSinkLocalStateBase* RuntimeState::get_sink_local_state() {
488
404k
    return _sink_local_state.get();
489
404k
}
490
491
1.73M
Result<RuntimeState::SinkLocalState*> RuntimeState::get_sink_local_state_result() {
492
1.73M
    if (!_sink_local_state) {
493
0
        return ResultError(Status::InternalError("_op_id_to_sink_local_state not exist"));
494
0
    }
495
1.73M
    return _sink_local_state.get();
496
1.73M
}
497
498
0
bool RuntimeState::enable_page_cache() const {
499
0
    return !config::disable_storage_page_cache &&
500
0
           (_query_options.__isset.enable_page_cache && _query_options.enable_page_cache);
501
0
}
502
503
65
RuntimeFilterMgr* RuntimeState::global_runtime_filter_mgr() {
504
65
    return _query_ctx->runtime_filter_mgr();
505
65
}
506
507
Status RuntimeState::register_producer_runtime_filter(
508
32
        const TRuntimeFilterDesc& desc, std::shared_ptr<RuntimeFilterProducer>* producer_filter) {
509
32
    _registered_runtime_filter_ids.insert(desc.filter_id);
510
    // Producers are created by local runtime filter mgr and shared by global runtime filter manager.
511
    // When RF is published, consumers in both global and local RF mgr will be found.
512
32
    RETURN_IF_ERROR(local_runtime_filter_mgr()->register_producer_filter(_query_ctx, desc,
513
32
                                                                         producer_filter));
514
    // Stamp the producer with the current recursive CTE stage so that outgoing merge RPCs
515
    // carry the correct round number and stale messages from old rounds are discarded.
516
    // PFC must still be alive: this runs inside a pipeline task, so the execution context
517
    // cannot have expired yet.
518
    // In unit-test scenarios the task execution context is never set (no PipelineFragmentContext
519
    // exists), so skip the stage stamping — the default stage (0) is correct.
520
31
    if (task_execution_context_inited()) {
521
2
        auto pfc = std::static_pointer_cast<PipelineFragmentContext>(
522
2
                get_task_execution_context().lock());
523
2
        DORIS_CHECK(pfc);
524
2
        (*producer_filter)->set_stage(pfc->rec_cte_stage());
525
2
    }
526
31
    RETURN_IF_ERROR(global_runtime_filter_mgr()->register_local_merge_producer_filter(
527
31
            _query_ctx, desc, *producer_filter));
528
31
    return Status::OK();
529
31
}
530
531
Status RuntimeState::register_consumer_runtime_filter(
532
        const TRuntimeFilterDesc& desc, bool need_local_merge, int node_id,
533
8
        std::shared_ptr<RuntimeFilterConsumer>* consumer_filter) {
534
8
    _registered_runtime_filter_ids.insert(desc.filter_id);
535
8
    bool need_merge = desc.has_remote_targets || need_local_merge ||
536
8
                      (desc.__isset.force_local_merge && desc.force_local_merge);
537
8
    RuntimeFilterMgr* mgr = need_merge ? global_runtime_filter_mgr() : local_runtime_filter_mgr();
538
8
    RETURN_IF_ERROR(mgr->register_consumer_filter(this, desc, node_id, consumer_filter));
539
    // Stamp the consumer with the current recursive CTE stage so that incoming publish RPCs
540
    // from old rounds are detected and discarded.
541
    // PFC must still be alive: this runs inside a pipeline task, so the execution context
542
    // cannot have expired yet.
543
    // In unit-test scenarios the task execution context is never set (no PipelineFragmentContext
544
    // exists), so skip the stage stamping — the default stage (0) is correct.
545
8
    if (task_execution_context_inited()) {
546
0
        auto pfc = std::static_pointer_cast<PipelineFragmentContext>(
547
0
                get_task_execution_context().lock());
548
0
        DORIS_CHECK(pfc);
549
0
        (*consumer_filter)->set_stage(pfc->rec_cte_stage());
550
0
    }
551
8
    return Status::OK();
552
8
}
553
554
74
bool RuntimeState::is_nereids() const {
555
74
    return _query_ctx->is_nereids();
556
74
}
557
558
0
std::vector<std::shared_ptr<RuntimeProfile>> RuntimeState::pipeline_id_to_profile() {
559
0
    std::shared_lock lc(_pipeline_profile_lock);
560
0
    return _pipeline_id_to_profile;
561
0
}
562
563
std::vector<std::shared_ptr<RuntimeProfile>> RuntimeState::build_pipeline_profile(
564
0
        std::size_t pipeline_size) {
565
0
    std::unique_lock lc(_pipeline_profile_lock);
566
0
    if (!_pipeline_id_to_profile.empty()) {
567
0
        return _pipeline_id_to_profile;
568
0
    }
569
0
    _pipeline_id_to_profile.resize(pipeline_size);
570
0
    {
571
0
        size_t pip_idx = 0;
572
0
        for (auto& pipeline_profile : _pipeline_id_to_profile) {
573
0
            pipeline_profile =
574
0
                    std::make_shared<RuntimeProfile>(fmt::format("Pipeline(id={})", pip_idx));
575
0
            pip_idx++;
576
0
        }
577
0
    }
578
0
    return _pipeline_id_to_profile;
579
0
}
580
581
1.47M
bool RuntimeState::low_memory_mode() const {
582
1.47M
#ifdef BE_TEST
583
1.47M
    if (!_query_ctx) {
584
0
        return false;
585
0
    }
586
1.47M
#endif
587
1.47M
    return _query_ctx->low_memory_mode();
588
1.47M
}
589
590
0
void RuntimeState::set_id_file_map() {
591
0
    _id_file_map = _exec_env->get_id_manager()->add_id_file_map(_query_id, execution_timeout());
592
0
}
593
} // end namespace doris