Coverage Report

Created: 2026-04-10 18:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exec/spill/spill_file_reader.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
18
#pragma once
19
20
#include <gen_cpp/data.pb.h>
21
22
#include <memory>
23
#include <string>
24
#include <vector>
25
26
#include "common/status.h"
27
#include "core/pod_array.h"
28
#include "core/pod_array_fwd.h"
29
#include "io/fs/file_reader_writer_fwd.h"
30
#include "runtime/runtime_profile.h"
31
#include "runtime/workload_management/resource_context.h"
32
33
namespace doris {
34
class RuntimeState;
35
class Block;
36
37
/// SpillFileReader reads blocks sequentially across all parts of a SpillFile.
38
///
39
/// Usage:
40
///   auto reader = spill_file->create_reader(state, profile);
41
///   RETURN_IF_ERROR(reader->open());
42
///   bool eos = false;
43
///   while (!eos) { RETURN_IF_ERROR(reader->read(&block, &eos)); }
44
///
45
/// Part boundaries are transparent to the caller. When the current part is
46
/// exhausted, the reader automatically opens the next part.
47
class SpillFileReader {
48
public:
49
    SpillFileReader(RuntimeState* state, RuntimeProfile* profile, std::string spill_dir,
50
                    size_t part_count);
51
52
185
    ~SpillFileReader() { (void)close(); }
53
54
    /// Open the first part and read its footer metadata.
55
    Status open();
56
57
    /// Read the next block. Automatically advances across part boundaries.
58
    /// Sets *eos = true when all parts are exhausted.
59
    Status read(Block* block, bool* eos);
60
61
    /// Seek to a global block index within the whole spill file.
62
    /// block_index is 0-based across all parts.
63
    /// If block_index is out of range, the reader is positioned at EOS.
64
    Status seek(size_t block_index);
65
66
    Status close();
67
68
private:
69
    /// Open a specific part file and read its footer.
70
    Status _open_part(size_t part_index);
71
72
    /// Seek implementation with status propagation.
73
    Status _seek_to_block(size_t block_index);
74
75
    /// Close the current part's file reader.
76
    void _close_current_part();
77
78
    // ── Configuration ──
79
    std::string _spill_dir;
80
    size_t _part_count;
81
82
    // ── Current part state ──
83
    size_t _current_part_index = 0;
84
    bool _is_open = false;
85
    bool _part_opened = false;
86
    io::FileReaderSPtr _file_reader;
87
    size_t _part_block_count = 0;
88
    size_t _part_read_block_index = 0;
89
    size_t _part_max_sub_block_size = 0;
90
    PaddedPODArray<char> _read_buff;
91
    std::vector<size_t> _block_start_offsets;
92
93
    PBlock _pb_block;
94
95
    // ── Counters ──
96
    RuntimeProfile::Counter* _read_file_timer = nullptr;
97
    RuntimeProfile::Counter* _deserialize_timer = nullptr;
98
    RuntimeProfile::Counter* _read_block_count = nullptr;
99
    RuntimeProfile::Counter* _read_block_data_size = nullptr;
100
    RuntimeProfile::Counter* _read_file_size = nullptr;
101
    RuntimeProfile::Counter* _read_rows_count = nullptr;
102
    RuntimeProfile::Counter* _read_file_count = nullptr;
103
104
    std::shared_ptr<ResourceContext> _resource_ctx = nullptr;
105
};
106
107
using SpillFileReaderSPtr = std::shared_ptr<SpillFileReader>;
108
109
} // namespace doris