Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] Use libcramjam for compression codecs #2

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,11 @@ bytes = "1.4"
chrono = { version = "0.4.37", default-features = false, features = ["std"] }
chrono-tz = "0.9"
fallible-streaming-iterator = { version = "0.1" }
flate2 = "1"
lz4_flex = "0.11"
lzokay-native = "0.1"
num = "0.4.1"
prost = { version = "0.12" }
snafu = "0.8"
snap = "1.1"
zstd = "0.12"
libcramjam = { version = "*", default-features = false, features = ["snappy", "zstd", "lz4", "zlib", "deflate"] }

# async support
async-trait = { version = "0.1.77", optional = true }
Expand Down
30 changes: 11 additions & 19 deletions src/compression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
// Modified from https://github.com/DataEngineeringLabs/orc-format/blob/416490db0214fc51d53289253c0ee91f7fc9bc17/src/read/decompress/mod.rs
//! Related code for handling decompression of ORC files.

use std::io::Read;

use bytes::{Bytes, BytesMut};
use fallible_streaming_iterator::FallibleStreamingIterator;
use snafu::ResultExt;
Expand Down Expand Up @@ -141,32 +139,28 @@ struct Lz4 {

impl DecompressorVariant for Zlib {
fn decompress_block(&self, compressed_bytes: &[u8], scratch: &mut Vec<u8>) -> Result<()> {
let mut gz = flate2::read::DeflateDecoder::new(compressed_bytes);
scratch.clear();
gz.read_to_end(scratch).context(error::IoSnafu)?;
libcramjam::deflate::decompress(compressed_bytes, scratch).context(error::IoSnafu)?;
Ok(())
}
}

impl DecompressorVariant for Zstd {
fn decompress_block(&self, compressed_bytes: &[u8], scratch: &mut Vec<u8>) -> Result<()> {
let mut reader =
zstd::Decoder::new(compressed_bytes).context(error::BuildZstdDecoderSnafu)?;
scratch.clear();
reader.read_to_end(scratch).context(error::IoSnafu)?;
libcramjam::zstd::decompress(compressed_bytes, scratch).context(error::IoSnafu)?;
Ok(())
}
}

impl DecompressorVariant for Snappy {
fn decompress_block(&self, compressed_bytes: &[u8], scratch: &mut Vec<u8>) -> Result<()> {
let len =
snap::raw::decompress_len(compressed_bytes).context(error::BuildSnappyDecoderSnafu)?;
scratch.resize(len, 0);
let mut decoder = snap::raw::Decoder::new();
decoder
.decompress(compressed_bytes, scratch)
let len = libcramjam::snappy::snap::raw::decompress_len(compressed_bytes)
.context(error::BuildSnappyDecoderSnafu)?;
scratch.resize(len, 0);
let n = libcramjam::snappy::raw::decompress(compressed_bytes, scratch.as_mut_slice())
.context(error::IoSnafu)?;
scratch.truncate(n);
Ok(())
}
}
Expand All @@ -184,12 +178,10 @@ impl DecompressorVariant for Lzo {

impl DecompressorVariant for Lz4 {
fn decompress_block(&self, compressed_bytes: &[u8], scratch: &mut Vec<u8>) -> Result<()> {
let decompressed =
lz4_flex::block::decompress(compressed_bytes, self.max_decompressed_block_size)
.context(error::BuildLz4DecoderSnafu)?;
// TODO: better way to utilize scratch here
scratch.clear();
scratch.extend(decompressed);
scratch.resize(self.max_decompressed_block_size, 0);
let n = libcramjam::lz4::block::decompress_into(compressed_bytes, scratch, None)
.context(error::IoSnafu)?;
scratch.truncate(n);
Ok(())
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ pub enum OrcError {
BuildSnappyDecoder {
#[snafu(implicit)]
location: Location,
source: snap::Error,
source: libcramjam::snappy::snap::Error,
},

#[snafu(display("Failed to build lzo decoder: {}", source))]
Expand All @@ -156,7 +156,7 @@ pub enum OrcError {
BuildLz4Decoder {
#[snafu(implicit)]
location: Location,
source: lz4_flex::block::DecompressError,
source: io::Error,
},

#[snafu(display("Arrow error: {}", source))]
Expand Down
Loading