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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
#pragma clang diagnostic ignored "-Wreserved-identifier"
#include "ArrowBufferedStreams.h"
#if USE_ARROW || USE_ORC || USE_PARQUET
#include <Common/assert_cast.h>
#include <IO/ReadBufferFromFileDescriptor.h>
#include <IO/WriteBufferFromString.h>
#include <IO/copyData.h>
#include <IO/PeekableReadBuffer.h>
#error #include <arrow/buffer.h>
#error #include <arrow/util/future.h>
#error #include <arrow/io/memory.h>
#error #include <arrow/result.h>
#include <Core/Settings.h>
#include <sys/stat.h>
namespace DB
{
namespace ErrorCodes
{
extern const int INCORRECT_DATA;
}
ArrowBufferedOutputStream::ArrowBufferedOutputStream(WriteBuffer & out_) : out{out_}, is_open{true}
{
}
arrow::Status ArrowBufferedOutputStream::Close()
{
is_open = false;
return arrow::Status::OK();
}
arrow::Result<int64_t> ArrowBufferedOutputStream::Tell() const
{
return arrow::Result<int64_t>(total_length);
}
arrow::Status ArrowBufferedOutputStream::Write(const void * data, int64_t length)
{
out.write(reinterpret_cast<const char *>(data), length);
total_length += length;
return arrow::Status::OK();
}
RandomAccessFileFromSeekableReadBuffer::RandomAccessFileFromSeekableReadBuffer(ReadBuffer & in_, std::optional<off_t> file_size_, bool avoid_buffering_)
: in{in_}, seekable_in{dynamic_cast<SeekableReadBuffer &>(in_)}, file_size{file_size_}, is_open{true}, avoid_buffering(avoid_buffering_)
{
}
arrow::Result<int64_t> RandomAccessFileFromSeekableReadBuffer::GetSize()
{
if (!file_size)
{
if (isBufferWithFileSize(in))
file_size = getFileSizeFromReadBuffer(in);
}
return arrow::Result<int64_t>(*file_size);
}
arrow::Status RandomAccessFileFromSeekableReadBuffer::Close()
{
is_open = false;
return arrow::Status::OK();
}
arrow::Result<int64_t> RandomAccessFileFromSeekableReadBuffer::Tell() const
{
return seekable_in.getPosition();
}
arrow::Result<int64_t> RandomAccessFileFromSeekableReadBuffer::Read(int64_t nbytes, void * out)
{
if (avoid_buffering)
in.setReadUntilPosition(seekable_in.getPosition() + nbytes);
return in.readBig(reinterpret_cast<char *>(out), nbytes);
}
arrow::Result<std::shared_ptr<arrow::Buffer>> RandomAccessFileFromSeekableReadBuffer::Read(int64_t nbytes)
{
ARROW_ASSIGN_OR_RAISE(auto buffer, arrow::AllocateResizableBuffer(nbytes))
ARROW_ASSIGN_OR_RAISE(int64_t bytes_read, Read(nbytes, buffer->mutable_data()))
if (bytes_read < nbytes)
RETURN_NOT_OK(buffer->Resize(bytes_read));
return buffer;
}
arrow::Future<std::shared_ptr<arrow::Buffer>> RandomAccessFileFromSeekableReadBuffer::ReadAsync(const arrow::io::IOContext &, int64_t position, int64_t nbytes)
{
/// Just a stub to to avoid using internal arrow thread pool
return arrow::Future<std::shared_ptr<arrow::Buffer>>::MakeFinished(ReadAt(position, nbytes));
}
arrow::Status RandomAccessFileFromSeekableReadBuffer::Seek(int64_t position)
{
if (avoid_buffering)
{
// Seeking to a position above a previous setReadUntilPosition() confuses some of the
// ReadBuffer implementations.
in.setReadUntilEnd();
}
seekable_in.seek(position, SEEK_SET);
return arrow::Status::OK();
}
ArrowInputStreamFromReadBuffer::ArrowInputStreamFromReadBuffer(ReadBuffer & in_) : in(in_), is_open{true}
{
}
arrow::Result<int64_t> ArrowInputStreamFromReadBuffer::Read(int64_t nbytes, void * out)
{
return in.readBig(reinterpret_cast<char *>(out), nbytes);
}
arrow::Result<std::shared_ptr<arrow::Buffer>> ArrowInputStreamFromReadBuffer::Read(int64_t nbytes)
{
ARROW_ASSIGN_OR_RAISE(auto buffer, arrow::AllocateResizableBuffer(nbytes))
ARROW_ASSIGN_OR_RAISE(int64_t bytes_read, Read(nbytes, buffer->mutable_data()))
if (bytes_read < nbytes)
RETURN_NOT_OK(buffer->Resize(bytes_read));
return buffer;
}
arrow::Status ArrowInputStreamFromReadBuffer::Abort()
{
return arrow::Status();
}
arrow::Result<int64_t> ArrowInputStreamFromReadBuffer::Tell() const
{
return in.count();
}
arrow::Status ArrowInputStreamFromReadBuffer::Close()
{
is_open = false;
return arrow::Status();
}
RandomAccessFileFromRandomAccessReadBuffer::RandomAccessFileFromRandomAccessReadBuffer(SeekableReadBuffer & in_, size_t file_size_) : in(in_), file_size(file_size_) {}
arrow::Result<int64_t> RandomAccessFileFromRandomAccessReadBuffer::GetSize()
{
return file_size;
}
arrow::Result<int64_t> RandomAccessFileFromRandomAccessReadBuffer::ReadAt(int64_t position, int64_t nbytes, void* out)
{
return in.readBigAt(reinterpret_cast<char*>(out), nbytes, position);
}
arrow::Result<std::shared_ptr<arrow::Buffer>> RandomAccessFileFromRandomAccessReadBuffer::ReadAt(int64_t position, int64_t nbytes)
{
ARROW_ASSIGN_OR_RAISE(auto buffer, arrow::AllocateResizableBuffer(nbytes))
ARROW_ASSIGN_OR_RAISE(int64_t bytes_read, ReadAt(position, nbytes, buffer->mutable_data()))
if (bytes_read < nbytes)
RETURN_NOT_OK(buffer->Resize(bytes_read));
return buffer;
}
arrow::Future<std::shared_ptr<arrow::Buffer>> RandomAccessFileFromRandomAccessReadBuffer::ReadAsync(const arrow::io::IOContext&, int64_t position, int64_t nbytes)
{
return arrow::Future<std::shared_ptr<arrow::Buffer>>::MakeFinished(ReadAt(position, nbytes));
}
arrow::Status RandomAccessFileFromRandomAccessReadBuffer::Close()
{
chassert(is_open);
is_open = false;
return arrow::Status::OK();
}
arrow::Status RandomAccessFileFromRandomAccessReadBuffer::Seek(int64_t) { return arrow::Status::NotImplemented(""); }
arrow::Result<int64_t> RandomAccessFileFromRandomAccessReadBuffer::Tell() const { return arrow::Status::NotImplemented(""); }
arrow::Result<int64_t> RandomAccessFileFromRandomAccessReadBuffer::Read(int64_t, void*) { return arrow::Status::NotImplemented(""); }
arrow::Result<std::shared_ptr<arrow::Buffer>> RandomAccessFileFromRandomAccessReadBuffer::Read(int64_t) { return arrow::Status::NotImplemented(""); }
std::shared_ptr<arrow::io::RandomAccessFile> asArrowFile(
ReadBuffer & in,
const FormatSettings & settings,
std::atomic<int> & is_cancelled,
const std::string & format_name,
const std::string & magic_bytes,
bool avoid_buffering)
{
bool has_file_size = isBufferWithFileSize(in);
auto * seekable_in = dynamic_cast<SeekableReadBuffer *>(&in);
if (has_file_size && seekable_in && settings.seekable_read)
{
if (avoid_buffering && seekable_in->supportsReadAt())
return std::make_shared<RandomAccessFileFromRandomAccessReadBuffer>(*seekable_in, getFileSizeFromReadBuffer(in));
if (seekable_in->checkIfActuallySeekable())
return std::make_shared<RandomAccessFileFromSeekableReadBuffer>(*seekable_in, std::nullopt, avoid_buffering);
}
// fallback to loading the entire file in memory
return asArrowFileLoadIntoMemory(in, is_cancelled, format_name, magic_bytes);
}
std::shared_ptr<arrow::io::RandomAccessFile> asArrowFileLoadIntoMemory(
ReadBuffer & in,
std::atomic<int> & is_cancelled,
const std::string & format_name,
const std::string & magic_bytes)
{
std::string file_data(magic_bytes.size(), '\0');
/// Avoid loading the whole file if it doesn't seem to even be in the correct format.
size_t bytes_read = in.read(file_data.data(), magic_bytes.size());
if (bytes_read < magic_bytes.size() || file_data != magic_bytes)
throw Exception(ErrorCodes::INCORRECT_DATA, "Not a {} file", format_name);
WriteBufferFromString file_buffer(file_data, AppendModeTag{});
copyData(in, file_buffer, is_cancelled);
file_buffer.finalize();
return std::make_shared<arrow::io::BufferReader>(arrow::Buffer::FromString(std::move(file_data)));
}
}
#endif
|