forked from alibaba/paimon-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathavro_file_batch_reader.h
More file actions
116 lines (95 loc) · 4.27 KB
/
Copy pathavro_file_batch_reader.h
File metadata and controls
116 lines (95 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* Copyright 2024-present Alibaba Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <limits>
#include <memory>
#include <set>
#include <utility>
#include <vector>
#include "avro/DataFile.hh"
#include "fmt/format.h"
#include "paimon/format/avro/avro_direct_decoder.h"
#include "paimon/memory/memory_pool.h"
#include "paimon/metrics.h"
#include "paimon/reader/file_batch_reader.h"
#include "paimon/result.h"
namespace paimon::avro {
class AvroFileBatchReader : public FileBatchReader {
public:
static Result<std::unique_ptr<AvroFileBatchReader>> Create(
const std::shared_ptr<InputStream>& input_stream, int32_t batch_size,
const std::shared_ptr<MemoryPool>& pool);
~AvroFileBatchReader() override;
Result<BatchReader::ReadBatch> NextBatch() override;
Result<std::unique_ptr<::ArrowSchema>> GetFileSchema() const override;
Status SetReadSchema(::ArrowSchema* read_schema, const std::shared_ptr<Predicate>& predicate,
const std::optional<RoaringBitmap32>& selection_bitmap) override;
Result<uint64_t> GetPreviousBatchFileRowId(uint64_t batch_row_id) const override {
if (previous_batch_row_count_ == 0) {
if (previous_first_row_ == std::numeric_limits<uint64_t>::max()) {
return Status::Invalid("No batch has been read yet.");
} else {
return Status::Invalid("Last batch was EOF.");
}
}
if (batch_row_id >= previous_batch_row_count_) {
return Status::Invalid(
fmt::format("batch_row_id {} is out of range, last batch row count is {}",
batch_row_id, previous_batch_row_count_));
}
return previous_first_row_ + batch_row_id;
}
Result<uint64_t> GetNumberOfRows() const override;
std::shared_ptr<Metrics> GetReaderMetrics() const override {
return metrics_;
}
void Close() override {
DoClose();
}
bool SupportPreciseBitmapSelection() const override {
return false;
}
private:
void DoClose();
static Result<std::unique_ptr<::avro::DataFileReaderBase>> CreateDataFileReader(
const std::shared_ptr<InputStream>& input_stream, const std::shared_ptr<MemoryPool>& pool);
static Result<std::set<size_t>> CalculateReadFieldsProjection(
const std::shared_ptr<::arrow::Schema>& file_schema, const arrow::FieldVector& read_fields);
AvroFileBatchReader(const std::shared_ptr<InputStream>& input_stream,
const std::shared_ptr<::arrow::DataType>& file_data_type,
std::unique_ptr<::avro::DataFileReaderBase>&& reader,
std::unique_ptr<arrow::ArrayBuilder>&& array_builder,
std::unique_ptr<arrow::MemoryPool>&& arrow_pool, int32_t batch_size,
const std::shared_ptr<MemoryPool>& pool);
static constexpr size_t BUFFER_SIZE = 1024 * 1024; // 1M
std::shared_ptr<MemoryPool> pool_;
std::unique_ptr<arrow::MemoryPool> arrow_pool_;
std::shared_ptr<InputStream> input_stream_;
std::shared_ptr<::arrow::DataType> file_data_type_;
std::unique_ptr<::avro::DataFileReaderBase> reader_;
std::unique_ptr<arrow::ArrayBuilder> array_builder_;
std::optional<std::set<size_t>> read_fields_projection_;
uint64_t previous_first_row_ = std::numeric_limits<uint64_t>::max();
uint64_t next_row_to_read_ = std::numeric_limits<uint64_t>::max();
uint64_t previous_batch_row_count_ = 0;
mutable std::optional<uint64_t> total_rows_ = std::nullopt;
const int32_t batch_size_;
bool close_ = false;
std::shared_ptr<Metrics> metrics_;
// Decode context for reusing scratch buffers
AvroDirectDecoder::DecodeContext decode_context_;
};
} // namespace paimon::avro