forked from apache/horaedb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathipc.rs
329 lines (286 loc) · 10.1 KB
/
ipc.rs
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// Copyright 2022-2023 CeresDB Project Authors. Licensed under Apache-2.0.
//! Utilities for `RecordBatch` serialization using Arrow IPC
use std::io::Cursor;
use arrow::{
ipc::{reader::StreamReader, writer::StreamWriter},
record_batch::RecordBatch,
};
use serde::{Deserialize, Serialize};
use snafu::{Backtrace, ResultExt, Snafu};
#[derive(Snafu, Debug)]
#[snafu(visibility(pub))]
pub enum Error {
#[snafu(display("Arrow error, err:{}.\nBacktrace:\n{}", source, backtrace))]
ArrowError {
source: arrow::error::ArrowError,
backtrace: Backtrace,
},
#[snafu(display("Zstd decode error, err:{}.\nBacktrace:\n{}", source, backtrace))]
ZstdError {
source: std::io::Error,
backtrace: Backtrace,
},
}
type Result<T> = std::result::Result<T, Error>;
const DEFAULT_COMPRESS_MIN_LENGTH: usize = 80 * 1024;
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Deserialize, Serialize)]
pub enum CompressionMethod {
#[default]
None,
Zstd,
}
// https://facebook.github.io/zstd/zstd_manual.html
// The lower the level, the faster the speed (at the cost of compression).
const ZSTD_LEVEL: i32 = 3;
#[derive(Default)]
/// Encoder that can encode a batch of record batches with specific compression
/// options.
pub struct RecordBatchesEncoder {
stream_writer: Option<StreamWriter<Vec<u8>>>,
num_rows: usize,
compress_opts: CompressOptions,
}
#[derive(Debug, Clone, Copy, Deserialize, Serialize)]
pub struct CompressOptions {
/// The minimum length of the payload to be compressed.
pub compress_min_length: usize,
pub method: CompressionMethod,
}
impl Default for CompressOptions {
fn default() -> Self {
Self {
compress_min_length: DEFAULT_COMPRESS_MIN_LENGTH,
method: CompressionMethod::Zstd,
}
}
}
#[derive(Clone, Default, Debug)]
pub struct CompressOutput {
pub method: CompressionMethod,
pub payload: Vec<u8>,
}
impl CompressOutput {
#[inline]
pub fn no_compression(payload: Vec<u8>) -> Self {
Self {
method: CompressionMethod::None,
payload,
}
}
}
impl CompressOptions {
pub fn maybe_compress(&self, input: Vec<u8>) -> Result<CompressOutput> {
if input.len() < self.compress_min_length {
return Ok(CompressOutput::no_compression(input));
}
match self.method {
CompressionMethod::None => Ok(CompressOutput::no_compression(input)),
CompressionMethod::Zstd => {
let payload = zstd::bulk::compress(&input, ZSTD_LEVEL).context(ZstdError)?;
Ok(CompressOutput {
method: CompressionMethod::Zstd,
payload,
})
}
}
}
}
impl RecordBatchesEncoder {
pub fn new(compress_opts: CompressOptions) -> Self {
Self {
stream_writer: None,
num_rows: 0,
compress_opts,
}
}
/// Get the number of rows that have been encoded.
pub fn num_rows(&self) -> usize {
self.num_rows
}
/// Append one batch into the encoder for encoding.
pub fn write(&mut self, batch: &RecordBatch) -> Result<()> {
let stream_writer = if let Some(v) = &mut self.stream_writer {
v
} else {
// TODO: pre-allocate the buffer.
let buffer: Vec<u8> = Vec::new();
let stream_writer =
StreamWriter::try_new(buffer, &batch.schema()).context(ArrowError)?;
self.stream_writer = Some(stream_writer);
self.stream_writer.as_mut().unwrap()
};
stream_writer.write(batch).context(ArrowError)?;
self.num_rows += batch.num_rows();
Ok(())
}
/// Finish encoding and generate the final encoded bytes, which may be
/// compressed.
pub fn finish(mut self) -> Result<CompressOutput> {
let stream_writer = match self.stream_writer.take() {
None => return Ok(CompressOutput::no_compression(Vec::new())),
Some(v) => v,
};
let encoded_bytes = stream_writer.into_inner().context(ArrowError)?;
self.compress_opts.maybe_compress(encoded_bytes)
}
}
/// Encode one record batch with given compression.
pub fn encode_record_batch(
batch: &RecordBatch,
compress_opts: CompressOptions,
) -> Result<CompressOutput> {
let mut encoder = RecordBatchesEncoder::new(compress_opts);
encoder.write(batch)?;
encoder.finish()
}
/// Decode multiple record batches from the encoded bytes.
pub fn decode_record_batches(
bytes: Vec<u8>,
compression: CompressionMethod,
) -> Result<Vec<RecordBatch>> {
if bytes.is_empty() {
return Ok(Vec::new());
}
let bytes = match compression {
CompressionMethod::None => bytes,
CompressionMethod::Zstd => {
zstd::stream::decode_all(Cursor::new(bytes)).context(ZstdError)?
}
};
let stream_reader = StreamReader::try_new(Cursor::new(bytes), None).context(ArrowError)?;
stream_reader
.collect::<std::result::Result<Vec<RecordBatch>, _>>()
.context(ArrowError)
}
#[cfg(test)]
mod tests {
use std::sync::Arc;
use arrow::{
array::{Int32Array, StringArray, StringDictionaryBuilder},
datatypes::{DataType, Field, Int32Type, Schema},
};
use super::*;
fn create_dictionary_record_batch() -> RecordBatch {
let col1 = Field::new_dict(
"dic1",
DataType::Dictionary(Box::new(DataType::Int32), Box::new(DataType::Utf8)),
false,
1,
false,
);
let col2 = Field::new_dict(
"dic2",
DataType::Dictionary(Box::new(DataType::Int32), Box::new(DataType::Utf8)),
true,
0,
false,
);
let schema = Schema::new(vec![col1, col2]);
let mut builder = StringDictionaryBuilder::<Int32Type>::new();
builder.append_value("d1");
builder.append_value("d2");
let dic1 = builder.finish();
let mut builder = StringDictionaryBuilder::<Int32Type>::new();
builder.append_null();
builder.append_value("d3");
let dic2 = builder.finish();
RecordBatch::try_new(Arc::new(schema), vec![Arc::new(dic1), Arc::new(dic2)]).unwrap()
}
fn create_batch(rows: usize) -> RecordBatch {
let schema = Schema::new(vec![
Field::new("a", DataType::Int32, false),
Field::new("b", DataType::Utf8, false),
]);
let a = Int32Array::from_iter_values(0..rows as i32);
let b = StringArray::from_iter_values((0..rows).map(|i| i.to_string()));
RecordBatch::try_new(Arc::new(schema), vec![Arc::new(a), Arc::new(b)]).unwrap()
}
fn ensure_encoding_and_decoding(
input: &RecordBatch,
compress_opts: CompressOptions,
expect_compress_method: CompressionMethod,
) {
let output = encode_record_batch(input, compress_opts).unwrap();
assert_eq!(output.method, expect_compress_method);
let decoded_batches = decode_record_batches(output.payload, output.method).unwrap();
assert_eq!(decoded_batches.len(), 1);
assert_eq!(input, &decoded_batches[0]);
}
#[test]
fn test_ipc_encode_decode() {
let batch = create_batch(1024);
for compression in [CompressionMethod::None, CompressionMethod::Zstd] {
let compress_opts = CompressOptions {
compress_min_length: 0,
method: compression,
};
ensure_encoding_and_decoding(&batch, compress_opts, compression);
}
}
#[test]
fn test_ipc_encode_decode_with_dicitonary_encode() {
let batch = create_dictionary_record_batch();
for compression in [CompressionMethod::None, CompressionMethod::Zstd] {
let compress_opts = CompressOptions {
compress_min_length: 0,
method: compression,
};
ensure_encoding_and_decoding(&batch, compress_opts, compression);
}
}
#[test]
fn test_encode_multiple_record_batches() {
let num_batches = 1000;
let mut batches = Vec::with_capacity(num_batches);
for _ in 0..num_batches {
batches.push(create_batch(1024));
}
let compress_opts = CompressOptions {
compress_min_length: 0,
method: CompressionMethod::Zstd,
};
let mut encoder = RecordBatchesEncoder::new(compress_opts);
for batch in &batches {
encoder.write(batch).unwrap();
}
let output = encoder.finish().unwrap();
assert_eq!(output.method, CompressionMethod::Zstd);
let decoded_batches =
decode_record_batches(output.payload, CompressionMethod::Zstd).unwrap();
assert_eq!(decoded_batches, batches);
}
#[test]
fn test_compression_decision() {
let batch = create_batch(1024);
{
// Encode the record batch with a large `compress_min_length`, so the output
// should not be compressed.
let compress_opts = CompressOptions {
compress_min_length: 1024 * 1024 * 1024,
method: CompressionMethod::Zstd,
};
ensure_encoding_and_decoding(&batch, compress_opts, CompressionMethod::None);
}
{
// Encode the record batch with a small `compress_min_length`, so the output
// should be compressed.
let compress_opts = CompressOptions {
compress_min_length: 10,
method: CompressionMethod::Zstd,
};
ensure_encoding_and_decoding(&batch, compress_opts, CompressionMethod::Zstd);
}
}
// Test that encoding without any record batch should not panic.
#[test]
fn test_encode_no_record_batch() {
let compress_opts = CompressOptions {
compress_min_length: 0,
method: CompressionMethod::Zstd,
};
let encoder = RecordBatchesEncoder::new(compress_opts);
let output = encoder.finish().unwrap();
assert_eq!(output.method, CompressionMethod::None);
assert!(output.payload.is_empty());
}
}