Skip to content

Commit ec36f8f

Browse files
morningmanzhangstar333claude
committed
[fix](be) Correct the external row fetch InitReaderAvgTime and GetBlockAvgTime
### What problem does this PR solve? Issue Number: None Related PR: #67005 Problem Summary: The second phase of TopN lazy materialization on an external table (RowIdStorageReader::read_batch_external_row) reads each scan range of a request block with its own reader and reports two profile values per request block: InitReaderAvgTime and GetBlockAvgTime, the mean reader-init and block-read time over those ranges. MaterializationOperator lists them per backend in the query profile. Both are wrong on master: - InitReaderAvgTime assigns each range's init time instead of adding it, so it reports the last range's time divided by the number of ranges: ranges that took 10, 20 and 60 ms give 20 ms instead of 30 ms. - GetBlockAvgTime is filled with the init-time average, so the block-read time never reaches the profile. - The averages are accumulated in the caller's variables, which read_by_rowids() zeroes once per RPC and passes to every request block. When an RPC fetches two lazily materialized external tables, the second block's sums start from the first block's averages. branch-4.1/4.2 fixed the first two in #67005 (a Lance profile PR) by summing the init times and reporting the get-block average, but kept the shared accumulators, so a second request block reports inflated averages there: 46ms and 466ms instead of 30ms and 300ms in the unit test below once the first block left 50 and 500 ms. The summary now lives in RowIdStorageReader::add_external_fetch_avg_time_profile(). It sums both times over the block's ranges from zero, overwrites the caller's variables with the averages and reports each average under its own key. The "Query stats" log line at the end of read_by_rowids() therefore prints the averages of the RPC's last external request block, the same block whose scan range count it already prints. ### Release note None ### Check List (For Author) - Test: Unit Test - BE UT RowIdStorageReaderTest.ExternalFetchProfileAveragesInitAndGetBlockTimes: three ranges (10/100, 20/200, 60/600 ms) with the caller's variables preset to 50/500 ms by a previous request block give 30ms and 300ms. - Behavior changed: No - Does this need documentation: No Co-authored-by: zhangstar333 <[email protected]> Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 7c049b0 commit ec36f8f

3 files changed

Lines changed: 66 additions & 8 deletions

File tree

‎be/src/exec/rowid_fetcher.cpp‎

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,27 @@ TFileScanRangeParams RowIdStorageReader::build_external_scan_params(
685685
return params;
686686
}
687687

688+
void RowIdStorageReader::add_external_fetch_avg_time_profile(
689+
const std::vector<ExternalFetchStatistics>& fetch_statistics,
690+
RuntimeProfile* runtime_profile, int64_t* init_reader_avg_ms, int64_t* get_block_avg_ms) {
691+
// read_by_rowids() reads a request block only when it has a row ID, and every row ID lands
692+
// in a scan range, so there is at least one range to average over.
693+
DORIS_CHECK(!fetch_statistics.empty());
694+
int64_t init_reader_total_ms = 0;
695+
int64_t get_block_total_ms = 0;
696+
for (const auto& statistics : fetch_statistics) {
697+
init_reader_total_ms += statistics.init_reader_ms;
698+
get_block_total_ms += statistics.get_block_ms;
699+
}
700+
const auto range_count = static_cast<int64_t>(fetch_statistics.size());
701+
*init_reader_avg_ms = init_reader_total_ms / range_count;
702+
*get_block_avg_ms = get_block_total_ms / range_count;
703+
runtime_profile->add_info_string(InitReaderAvgTimeProfile,
704+
std::to_string(*init_reader_avg_ms) + "ms");
705+
runtime_profile->add_info_string(GetBlockAvgTimeProfile,
706+
std::to_string(*get_block_avg_ms) + "ms");
707+
}
708+
688709
Status RowIdStorageReader::read_batch_external_row(
689710
const uint64_t workload_group_id, const PRequestBlockDesc& request_block_desc,
690711
std::shared_ptr<IdFileMap> id_file_map, std::vector<SlotDescriptor>& slots,
@@ -902,8 +923,6 @@ Status RowIdStorageReader::read_batch_external_row(
902923
size_t idx = 0;
903924
for (const auto& [_, scan_info] : scan_rows) {
904925
format_to(file_read_lines_buffer, "{}, ", scan_info.first.size());
905-
*init_reader_avg_ms = fetch_statistics[idx].init_reader_ms;
906-
*get_block_avg_ms += fetch_statistics[idx].get_block_ms;
907926
format_to(file_read_bytes_buffer, "{}, ", fetch_statistics[idx].file_read_bytes);
908927
format_to(file_read_times_buffer, "{}, ", fetch_statistics[idx].file_read_times);
909928
idx++;
@@ -913,12 +932,8 @@ Status RowIdStorageReader::read_batch_external_row(
913932
format_to(file_read_bytes_buffer, "]");
914933
format_to(file_read_times_buffer, "]");
915934

916-
*init_reader_avg_ms /= fetch_statistics.size();
917-
*get_block_avg_ms /= fetch_statistics.size();
918-
runtime_profile->add_info_string(InitReaderAvgTimeProfile,
919-
std::to_string(*init_reader_avg_ms) + "ms");
920-
runtime_profile->add_info_string(GetBlockAvgTimeProfile,
921-
std::to_string(*init_reader_avg_ms) + "ms");
935+
add_external_fetch_avg_time_profile(fetch_statistics, runtime_profile.get(),
936+
init_reader_avg_ms, get_block_avg_ms);
922937
runtime_profile->add_info_string(FileReadLinesProfile,
923938
fmt::to_string(file_read_lines_buffer));
924939
runtime_profile->add_info_string(FileScannerV2::FileReadBytesProfile,

‎be/src/exec/rowid_fetcher.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
namespace doris {
3737

38+
class RuntimeProfile;
3839
class RuntimeState;
3940
class TQueryOptions;
4041
class TupleDescriptor;
@@ -148,6 +149,16 @@ class RowIdStorageReader {
148149
const std::function<std::string(size_t)>& make_task_id,
149150
const std::function<Status(size_t)>& run_task);
150151

152+
// Reports InitReaderAvgTime and GetBlockAvgTime of one request block: the mean reader-init
153+
// and block-read times over its scan ranges, e.g. 20ms and 200ms for two ranges that took
154+
// 10/100 ms and 30/300 ms. Both outputs are overwritten rather than accumulated, because an
155+
// RPC that fetches two lazily materialized tables reads its two request blocks into the same
156+
// caller variables.
157+
static void add_external_fetch_avg_time_profile(
158+
const std::vector<ExternalFetchStatistics>& fetch_statistics,
159+
RuntimeProfile* runtime_profile, int64_t* init_reader_avg_ms,
160+
int64_t* get_block_avg_ms);
161+
151162
struct ExternalFetchStatistics {
152163
int64_t init_reader_ms = 0;
153164
int64_t get_block_ms = 0;

‎be/test/exec/rowid_fetcher_test.cpp‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "runtime/descriptor_helper.h"
3434
#include "runtime/descriptors.h"
3535
#include "runtime/exec_env.h"
36+
#include "runtime/runtime_profile.h"
3637
#include "runtime/runtime_state.h"
3738
#include "storage/id_manager.h"
3839
#include "storage/rowset_id.h"
@@ -301,6 +302,37 @@ TEST_F(RowIdStorageReaderTest, ExternalFetchPartitionSlotsPreserveHivePositionMa
301302
}
302303
}
303304

305+
// A request block reads each of its scan ranges with a separate reader, and its profile reports the
306+
// mean reader-init and block-read time over those ranges. When an RPC fetches two lazily
307+
// materialized tables, the second request block starts with the first block's averages in the
308+
// caller's variables; they must not leak into its own.
309+
TEST_F(RowIdStorageReaderTest, ExternalFetchProfileAveragesInitAndGetBlockTimes) {
310+
std::vector<RowIdStorageReader::ExternalFetchStatistics> fetch_statistics(3);
311+
fetch_statistics[0].init_reader_ms = 10;
312+
fetch_statistics[0].get_block_ms = 100;
313+
fetch_statistics[1].init_reader_ms = 20;
314+
fetch_statistics[1].get_block_ms = 200;
315+
fetch_statistics[2].init_reader_ms = 60;
316+
fetch_statistics[2].get_block_ms = 600;
317+
// The averages a previous request block of the same RPC left behind.
318+
int64_t init_reader_avg_ms = 50;
319+
int64_t get_block_avg_ms = 500;
320+
RuntimeProfile profile("ExternalRowIDFetcher");
321+
322+
RowIdStorageReader::add_external_fetch_avg_time_profile(fetch_statistics, &profile,
323+
&init_reader_avg_ms, &get_block_avg_ms);
324+
325+
EXPECT_EQ(init_reader_avg_ms, 30);
326+
EXPECT_EQ(get_block_avg_ms, 300);
327+
const auto* init_reader_avg =
328+
profile.get_info_string(RowIdStorageReader::InitReaderAvgTimeProfile);
329+
ASSERT_NE(init_reader_avg, nullptr);
330+
EXPECT_EQ(*init_reader_avg, "30ms");
331+
const auto* get_block_avg = profile.get_info_string(RowIdStorageReader::GetBlockAvgTimeProfile);
332+
ASSERT_NE(get_block_avg, nullptr);
333+
EXPECT_EQ(*get_block_avg, "300ms");
334+
}
335+
304336
TEST_F(RowIdStorageReaderTest, SameSourceColumnSharesKey) {
305337
// The bug case: one physical column projected twice must dedup onto one scan column.
306338
const SlotDescriptor first = make_slot({});

0 commit comments

Comments
 (0)