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

Avro codec enhancements + Avro Writer Schema Generator #6965

Draft
wants to merge 17 commits 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
830 changes: 789 additions & 41 deletions arrow-avro/src/codec.rs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions arrow-avro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ mod schema;
mod compression;

mod codec;
mod writer;

#[cfg(test)]
mod test_util {
Expand Down
26 changes: 21 additions & 5 deletions arrow-avro/src/reader/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

use crate::reader::vlq::read_varint;
use arrow_schema::ArrowError;

Expand Down Expand Up @@ -65,6 +64,7 @@ impl<'a> AvroCursor<'a> {
Ok(val)
}

/// Decode a zig-zag encoded Avro int (32-bit).
#[inline]
pub(crate) fn get_int(&mut self) -> Result<i32, ArrowError> {
let varint = self.read_vlq()?;
Expand All @@ -74,18 +74,20 @@ impl<'a> AvroCursor<'a> {
Ok((val >> 1) as i32 ^ -((val & 1) as i32))
}

/// Decode a zig-zag encoded Avro long (64-bit).
#[inline]
pub(crate) fn get_long(&mut self) -> Result<i64, ArrowError> {
let val = self.read_vlq()?;
Ok((val >> 1) as i64 ^ -((val & 1) as i64))
}

/// Read a variable-length byte array from Avro (where the length is stored as an Avro long).
pub(crate) fn get_bytes(&mut self) -> Result<&'a [u8], ArrowError> {
let len: usize = self.get_long()?.try_into().map_err(|_| {
ArrowError::ParseError("offset overflow reading avro bytes".to_string())
})?;

if (self.buf.len() < len) {
if self.buf.len() < len {
return Err(ArrowError::ParseError(
"Unexpected EOF reading bytes".to_string(),
));
Expand All @@ -95,9 +97,10 @@ impl<'a> AvroCursor<'a> {
Ok(ret)
}

/// Read a little-endian 32-bit float
#[inline]
pub(crate) fn get_float(&mut self) -> Result<f32, ArrowError> {
if (self.buf.len() < 4) {
if self.buf.len() < 4 {
return Err(ArrowError::ParseError(
"Unexpected EOF reading float".to_string(),
));
Expand All @@ -107,15 +110,28 @@ impl<'a> AvroCursor<'a> {
Ok(ret)
}

/// Read a little-endian 64-bit float
#[inline]
pub(crate) fn get_double(&mut self) -> Result<f64, ArrowError> {
if (self.buf.len() < 8) {
if self.buf.len() < 8 {
return Err(ArrowError::ParseError(
"Unexpected EOF reading float".to_string(),
"Unexpected EOF reading double".to_string(),
));
}
let ret = f64::from_le_bytes(self.buf[..8].try_into().unwrap());
self.buf = &self.buf[8..];
Ok(ret)
}

/// Read exactly `n` bytes from the buffer (e.g. for Avro `fixed`).
pub(crate) fn get_fixed(&mut self, n: usize) -> Result<&'a [u8], ArrowError> {
if self.buf.len() < n {
return Err(ArrowError::ParseError(
"Unexpected EOF reading fixed".to_string(),
));
}
let ret = &self.buf[..n];
self.buf = &self.buf[n..];
Ok(ret)
}
}
Loading
Loading