Coverage Report

Created: 2026-05-29 09:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/format/native/native_reader.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
18
#include "format/native/native_reader.h"
19
20
#include <gen_cpp/data.pb.h>
21
22
#include "core/block/block.h"
23
#include "format/native/native_format.h"
24
#include "io/file_factory.h"
25
#include "io/fs/buffered_reader.h"
26
#include "io/fs/file_reader.h"
27
#include "io/fs/tracing_file_reader.h"
28
#include "runtime/runtime_profile.h"
29
#include "runtime/runtime_state.h"
30
31
namespace doris {
32
33
NativeReader::NativeReader(RuntimeProfile* profile, const TFileScanRangeParams& params,
34
                           const TFileRangeDesc& range, io::IOContext* io_ctx, RuntimeState* state)
35
14
        : _profile(profile),
36
14
          _scan_params(params),
37
14
          _scan_range(range),
38
14
          _io_ctx(io_ctx),
39
14
          _state(state) {}
40
41
14
NativeReader::~NativeReader() {
42
14
    (void)close();
43
14
}
44
45
namespace {
46
47
Status validate_and_consume_header(io::FileReaderSPtr file_reader, const TFileRangeDesc& range,
48
                                   int64_t* file_size, int64_t* current_offset, bool* eof,
49
14
                                   const io::IOContext* io_ctx) {
50
14
    *file_size = file_reader->size();
51
14
    *current_offset = 0;
52
14
    *eof = (*file_size == 0);
53
54
    // Validate and consume Doris Native file header.
55
    // Expected layout:
56
    // [magic bytes "DORISN1\0"][uint32_t format_version][uint64_t block_size]...
57
14
    static constexpr size_t HEADER_SIZE = sizeof(DORIS_NATIVE_MAGIC) + sizeof(uint32_t);
58
14
    if (*eof || *file_size < static_cast<int64_t>(HEADER_SIZE)) {
59
0
        return Status::InternalError(
60
0
                "invalid Doris Native file {}, file size {} is smaller than header size {}",
61
0
                range.path, *file_size, HEADER_SIZE);
62
0
    }
63
64
14
    char header[HEADER_SIZE];
65
14
    Slice header_slice(header, sizeof(header));
66
14
    size_t bytes_read = 0;
67
14
    RETURN_IF_ERROR(file_reader->read_at(0, header_slice, &bytes_read, io_ctx));
68
14
    if (bytes_read != sizeof(header)) {
69
0
        return Status::InternalError(
70
0
                "failed to read Doris Native header from file {}, expect {} bytes, got {} bytes",
71
0
                range.path, sizeof(header), bytes_read);
72
0
    }
73
74
14
    if (memcmp(header, DORIS_NATIVE_MAGIC, sizeof(DORIS_NATIVE_MAGIC)) != 0) {
75
0
        return Status::InternalError("invalid Doris Native magic header in file {}", range.path);
76
0
    }
77
78
14
    uint32_t version = 0;
79
14
    memcpy(&version, header + sizeof(DORIS_NATIVE_MAGIC), sizeof(uint32_t));
80
14
    if (version != DORIS_NATIVE_FORMAT_VERSION) {
81
0
        return Status::InternalError(
82
0
                "unsupported Doris Native format version {} in file {}, expect {}", version,
83
0
                range.path, DORIS_NATIVE_FORMAT_VERSION);
84
0
    }
85
86
14
    *current_offset = sizeof(header);
87
14
    *eof = (*file_size == *current_offset);
88
14
    return Status::OK();
89
14
}
90
91
} // namespace
92
93
110
Status NativeReader::init_reader() {
94
110
    if (_file_reader != nullptr) {
95
96
        return Status::OK();
96
96
    }
97
98
    // Create underlying file reader. For now we always use random access mode.
99
14
    io::FileSystemProperties system_properties;
100
14
    io::FileDescription file_description;
101
14
    file_description.file_size = -1;
102
14
    if (_scan_range.__isset.file_size) {
103
14
        file_description.file_size = _scan_range.file_size;
104
14
    }
105
14
    file_description.path = _scan_range.path;
106
14
    if (_scan_range.__isset.fs_name) {
107
0
        file_description.fs_name = _scan_range.fs_name;
108
0
    }
109
14
    if (_scan_range.__isset.modification_time) {
110
5
        file_description.mtime = _scan_range.modification_time;
111
9
    } else {
112
9
        file_description.mtime = 0;
113
9
    }
114
115
14
    if (_scan_range.__isset.file_type) {
116
        // For compatibility with older FE.
117
13
        system_properties.system_type = _scan_range.file_type;
118
13
    } else {
119
1
        system_properties.system_type = _scan_params.file_type;
120
1
    }
121
14
    system_properties.properties = _scan_params.properties;
122
14
    system_properties.hdfs_params = _scan_params.hdfs_params;
123
14
    if (_scan_params.__isset.broker_addresses) {
124
1
        system_properties.broker_addresses.assign(_scan_params.broker_addresses.begin(),
125
1
                                                  _scan_params.broker_addresses.end());
126
1
    }
127
128
14
    io::FileReaderOptions reader_options =
129
14
            FileFactory::get_reader_options(_state, file_description);
130
14
    auto reader_res = io::DelegateReader::create_file_reader(
131
14
            _profile, system_properties, file_description, reader_options,
132
14
            io::DelegateReader::AccessMode::RANDOM, _io_ctx);
133
14
    if (!reader_res.has_value()) {
134
0
        return reader_res.error();
135
0
    }
136
14
    _file_reader = reader_res.value();
137
138
14
    if (_io_ctx) {
139
5
        _file_reader =
140
5
                std::make_shared<io::TracingFileReader>(_file_reader, _io_ctx->file_reader_stats);
141
5
    }
142
143
14
    RETURN_IF_ERROR(validate_and_consume_header(_file_reader, _scan_range, &_file_size,
144
14
                                                &_current_offset, &_eof, _io_ctx));
145
14
    return Status::OK();
146
14
}
147
148
106
Status NativeReader::_do_get_next_block(Block* block, size_t* read_rows, bool* eof) {
149
106
    if (_eof) {
150
8
        *read_rows = 0;
151
8
        *eof = true;
152
8
        return Status::OK();
153
8
    }
154
155
98
    RETURN_IF_ERROR(init_reader());
156
157
98
    std::string buff;
158
98
    bool local_eof = false;
159
160
    // If we have already loaded the first block for schema probing, use it first.
161
98
    if (_first_block_loaded && !_first_block_consumed) {
162
3
        buff = _first_block_buf;
163
3
        local_eof = false;
164
95
    } else {
165
95
        RETURN_IF_ERROR(_read_next_pblock(&buff, &local_eof));
166
95
    }
167
168
    // If we reach EOF and also read no data for this call, the whole file is considered finished.
169
98
    if (local_eof && buff.empty()) {
170
1
        *read_rows = 0;
171
1
        *eof = true;
172
1
        _eof = true;
173
1
        return Status::OK();
174
1
    }
175
    // If buffer is empty but we have not reached EOF yet, treat this as an error.
176
97
    if (buff.empty()) {
177
0
        return Status::InternalError("read empty native block from file {}", _scan_range.path);
178
0
    }
179
180
97
    PBlock pblock;
181
97
    if (!pblock.ParseFromArray(buff.data(), static_cast<int>(buff.size()))) {
182
0
        return Status::InternalError("Failed to parse native PBlock from file {}",
183
0
                                     _scan_range.path);
184
0
    }
185
186
    // Initialize schema from first block if not done yet.
187
97
    if (!_schema_inited) {
188
7
        RETURN_IF_ERROR(_init_schema_from_pblock(pblock));
189
7
    }
190
191
97
    size_t uncompressed_bytes = 0;
192
97
    int64_t decompress_time = 0;
193
97
    RETURN_IF_ERROR(block->deserialize(pblock, &uncompressed_bytes, &decompress_time));
194
195
    // For external file scan / TVF scenarios, unify all columns as nullable to match
196
    // GenericReader/SlotDescriptor convention. This ensures schema consistency when
197
    // some writers emit non-nullable columns.
198
683
    for (size_t i = 0; i < block->columns(); ++i) {
199
586
        auto& col_with_type = block->get_by_position(i);
200
586
        if (!col_with_type.type->is_nullable()) {
201
18
            col_with_type.column = make_nullable(col_with_type.column);
202
18
            col_with_type.type = make_nullable(col_with_type.type);
203
18
        }
204
586
    }
205
206
97
    *read_rows = block->rows();
207
97
    *eof = false;
208
209
97
    if (_first_block_loaded && !_first_block_consumed) {
210
3
        _first_block_consumed = true;
211
3
    }
212
213
    // If we reached the physical end of file, mark eof for subsequent calls.
214
97
    if (_current_offset >= _file_size) {
215
10
        _eof = true;
216
10
    }
217
218
97
    return Status::OK();
219
97
}
220
221
4
Status NativeReader::_get_columns_impl(std::unordered_map<std::string, DataTypePtr>* name_to_type) {
222
4
    RETURN_IF_ERROR(init_reader());
223
224
4
    if (!_schema_inited) {
225
        // Load first block lazily to initialize schema.
226
4
        if (!_first_block_loaded) {
227
4
            bool local_eof = false;
228
4
            RETURN_IF_ERROR(_read_next_pblock(&_first_block_buf, &local_eof));
229
            // Treat file as empty only if we reach EOF and there is no block data at all.
230
4
            if (local_eof && _first_block_buf.empty()) {
231
0
                return Status::EndOfFile("empty native file {}", _scan_range.path);
232
0
            }
233
            // Non-EOF but empty buffer means corrupted native file.
234
4
            if (_first_block_buf.empty()) {
235
0
                return Status::InternalError("first native block is empty {}", _scan_range.path);
236
0
            }
237
4
            _first_block_loaded = true;
238
4
        }
239
240
4
        PBlock pblock;
241
4
        if (!pblock.ParseFromArray(_first_block_buf.data(),
242
4
                                   static_cast<int>(_first_block_buf.size()))) {
243
0
            return Status::InternalError("Failed to parse native PBlock for schema from file {}",
244
0
                                         _scan_range.path);
245
0
        }
246
4
        RETURN_IF_ERROR(_init_schema_from_pblock(pblock));
247
4
    }
248
249
22
    for (size_t i = 0; i < _schema_col_names.size(); ++i) {
250
18
        name_to_type->emplace(_schema_col_names[i], _schema_col_types[i]);
251
18
    }
252
4
    return Status::OK();
253
4
}
254
255
2
Status NativeReader::init_schema_reader() {
256
2
    RETURN_IF_ERROR(init_reader());
257
2
    return Status::OK();
258
2
}
259
260
Status NativeReader::get_parsed_schema(std::vector<std::string>* col_names,
261
3
                                       std::vector<DataTypePtr>* col_types) {
262
3
    RETURN_IF_ERROR(init_reader());
263
264
3
    if (!_schema_inited) {
265
2
        if (!_first_block_loaded) {
266
2
            bool local_eof = false;
267
2
            RETURN_IF_ERROR(_read_next_pblock(&_first_block_buf, &local_eof));
268
            // Treat file as empty only if we reach EOF and there is no block data at all.
269
2
            if (local_eof && _first_block_buf.empty()) {
270
0
                return Status::EndOfFile("empty native file {}", _scan_range.path);
271
0
            }
272
            // Non-EOF but empty buffer means corrupted native file.
273
2
            if (_first_block_buf.empty()) {
274
0
                return Status::InternalError("first native block is empty {}", _scan_range.path);
275
0
            }
276
2
            _first_block_loaded = true;
277
2
        }
278
279
2
        PBlock pblock;
280
2
        if (!pblock.ParseFromArray(_first_block_buf.data(),
281
2
                                   static_cast<int>(_first_block_buf.size()))) {
282
0
            return Status::InternalError("Failed to parse native PBlock for schema from file {}",
283
0
                                         _scan_range.path);
284
0
        }
285
2
        RETURN_IF_ERROR(_init_schema_from_pblock(pblock));
286
2
    }
287
288
3
    *col_names = _schema_col_names;
289
3
    *col_types = _schema_col_types;
290
3
    return Status::OK();
291
3
}
292
293
17
Status NativeReader::close() {
294
17
    _file_reader.reset();
295
17
    return Status::OK();
296
17
}
297
298
101
Status NativeReader::_read_next_pblock(std::string* buff, bool* eof) {
299
101
    *eof = false;
300
101
    buff->clear();
301
302
101
    if (_file_reader == nullptr) {
303
0
        RETURN_IF_ERROR(init_reader());
304
0
    }
305
306
101
    if (_current_offset >= _file_size) {
307
1
        *eof = true;
308
1
        return Status::OK();
309
1
    }
310
311
100
    uint64_t len = 0;
312
100
    Slice len_slice(reinterpret_cast<char*>(&len), sizeof(len));
313
100
    size_t bytes_read = 0;
314
100
    RETURN_IF_ERROR(_file_reader->read_at(_current_offset, len_slice, &bytes_read, _io_ctx));
315
100
    if (bytes_read == 0) {
316
0
        *eof = true;
317
0
        return Status::OK();
318
0
    }
319
100
    if (bytes_read != sizeof(len)) {
320
0
        return Status::InternalError(
321
0
                "Failed to read native block length from file {}, expect {}, "
322
0
                "actual {}",
323
0
                _scan_range.path, sizeof(len), bytes_read);
324
0
    }
325
326
100
    _current_offset += sizeof(len);
327
100
    if (len == 0) {
328
        // Empty block, nothing to read.
329
0
        *eof = (_current_offset >= _file_size);
330
0
        return Status::OK();
331
0
    }
332
333
100
    buff->assign(len, '\0');
334
100
    Slice data_slice(buff->data(), len);
335
100
    bytes_read = 0;
336
100
    RETURN_IF_ERROR(_file_reader->read_at(_current_offset, data_slice, &bytes_read, _io_ctx));
337
100
    if (bytes_read != len) {
338
0
        return Status::InternalError(
339
0
                "Failed to read native block body from file {}, expect {}, "
340
0
                "actual {}",
341
0
                _scan_range.path, len, bytes_read);
342
0
    }
343
344
100
    _current_offset += len;
345
100
    *eof = (_current_offset >= _file_size);
346
100
    return Status::OK();
347
100
}
348
349
13
Status NativeReader::_init_schema_from_pblock(const PBlock& pblock) {
350
13
    _schema_col_names.clear();
351
13
    _schema_col_types.clear();
352
353
110
    for (const auto& pcol_meta : pblock.column_metas()) {
354
110
        DataTypePtr type = make_nullable(DataTypeFactory::instance().create_data_type(pcol_meta));
355
110
        VLOG_DEBUG << "init_schema_from_pblock, name=" << pcol_meta.name()
356
0
                   << ", type=" << type->get_name();
357
110
        _schema_col_names.emplace_back(pcol_meta.name());
358
110
        _schema_col_types.emplace_back(type);
359
110
    }
360
13
    _schema_inited = true;
361
13
    return Status::OK();
362
13
}
363
364
} // namespace doris