-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(rust): Refactor decompression checks and add support for decompre…
…ssing JSON (#18536) Co-authored-by: Ohan Fillbach <ofillbach@vectra.ai>
- Loading branch information
Showing
12 changed files
with
130 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,66 @@ | ||
// magic numbers | ||
pub mod magic { | ||
pub const GZIP: [u8; 2] = [31, 139]; | ||
pub const ZLIB0: [u8; 2] = [0x78, 0x01]; | ||
pub const ZLIB1: [u8; 2] = [0x78, 0x9C]; | ||
pub const ZLIB2: [u8; 2] = [0x78, 0xDA]; | ||
pub const ZSTD: [u8; 4] = [0x28, 0xB5, 0x2F, 0xFD]; | ||
use std::io::Read; | ||
|
||
use polars_core::prelude::*; | ||
use polars_error::to_compute_err; | ||
|
||
/// Represents the compression algorithms that we have decoders for | ||
pub enum SupportedCompression { | ||
GZIP, | ||
ZLIB, | ||
ZSTD, | ||
} | ||
|
||
/// check if csv file is compressed | ||
pub fn is_compressed(bytes: &[u8]) -> bool { | ||
use magic::*; | ||
impl SupportedCompression { | ||
/// If the given byte slice starts with the "magic" bytes for a supported compression family, return | ||
/// that family, for unsupported/uncompressed slices, return None | ||
pub fn check(bytes: &[u8]) -> Option<Self> { | ||
if bytes.len() < 4 { | ||
// not enough bytes to perform prefix checks | ||
return None; | ||
} | ||
match bytes[..4] { | ||
[31, 139, _, _] => Some(Self::GZIP), | ||
[0x78, 0x01, _, _] | // ZLIB0 | ||
[0x78, 0x9C, _, _] | // ZLIB1 | ||
[0x78, 0xDA, _, _] // ZLIB2 | ||
=> Some(Self::ZLIB), | ||
[0x28, 0xB5, 0x2F, 0xFD] => Some(Self::ZSTD), | ||
_ => None, | ||
} | ||
} | ||
} | ||
|
||
/// Decompress `bytes` if compression is detected, otherwise simply return it. | ||
/// An `out` vec must be given for ownership of the decompressed data. | ||
pub fn maybe_decompress_bytes<'a>(bytes: &'a [u8], out: &'a mut Vec<u8>) -> PolarsResult<&'a [u8]> { | ||
assert!(out.is_empty()); | ||
|
||
if let Some(algo) = SupportedCompression::check(bytes) { | ||
#[cfg(any(feature = "decompress", feature = "decompress-fast"))] | ||
{ | ||
match algo { | ||
SupportedCompression::GZIP => { | ||
flate2::read::MultiGzDecoder::new(bytes) | ||
.read_to_end(out) | ||
.map_err(to_compute_err)?; | ||
}, | ||
SupportedCompression::ZLIB => { | ||
flate2::read::ZlibDecoder::new(bytes) | ||
.read_to_end(out) | ||
.map_err(to_compute_err)?; | ||
}, | ||
SupportedCompression::ZSTD => { | ||
zstd::Decoder::new(bytes)?.read_to_end(out)?; | ||
}, | ||
} | ||
|
||
bytes.starts_with(&ZLIB0) | ||
|| bytes.starts_with(&ZLIB1) | ||
|| bytes.starts_with(&ZLIB2) | ||
|| bytes.starts_with(&GZIP) | ||
|| bytes.starts_with(&ZSTD) | ||
Ok(out) | ||
} | ||
#[cfg(not(any(feature = "decompress", feature = "decompress-fast")))] | ||
{ | ||
panic!("cannot decompress without 'decompress' or 'decompress-fast' feature") | ||
} | ||
} else { | ||
Ok(bytes) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters