Skip to content

Commit

Permalink
AVRO-3779: fix from martin review
Browse files Browse the repository at this point in the history
  • Loading branch information
clesaec committed Jun 23, 2023
1 parent b744220 commit 7be6009
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 15 deletions.
17 changes: 7 additions & 10 deletions lang/rust/avro/src/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,21 +106,21 @@ impl<T: AsRef<[u8]>> From<T> for Decimal {
}
}

pub(crate) fn serialize_big_decimal(decimal: &BigDecimal) -> Result<Vec<u8>, Error> {
pub(crate) fn serialize_big_decimal(decimal: &BigDecimal) -> Vec<u8> {
let mut buffer: Vec<u8> = Vec::new();
let (big_int, exponent): (BigInt, i64) = decimal.as_bigint_and_exponent();
let big_endian_value: Vec<u8> = big_int.to_signed_bytes_be();
encode_bytes(&big_endian_value, &mut buffer);
encode_long(exponent, &mut buffer);

Ok(buffer)
buffer
}

pub(crate) fn deserialize_big_decimal(bytes: &Vec<u8>) -> Result<BigDecimal, Error> {
let mut bytes: &[u8] = bytes.as_slice();
let mut big_decimal_buffer = match decode_len(&mut bytes) {
Ok(size) => vec![0u8; size],
Err(_err) => return Err(Error::BigDecimalSign),
Err(_err) => return Err(Error::BigDecimalLen),
};

bytes
Expand Down Expand Up @@ -177,11 +177,10 @@ mod tests {
let mut current: bigdecimal::BigDecimal = bigdecimal::BigDecimal::one();

for iter in 1..180 {
let result: Result<Vec<u8>, Error> = serialize_big_decimal(&current);
assert!(result.is_ok(), "can't serialize for iter {iter}");
let result: Vec<u8> = serialize_big_decimal(&current);

let deserialize_big_decimal: Result<bigdecimal::BigDecimal, Error> =
deserialize_big_decimal(&result.unwrap());
deserialize_big_decimal(&result);
assert!(
deserialize_big_decimal.is_ok(),
"can't deserialize for iter {iter}"
Expand All @@ -194,11 +193,9 @@ mod tests {
current = current.mul(&value);
}

let result: Result<Vec<u8>, Error> = serialize_big_decimal(&BigDecimal::zero());
assert!(result.is_ok(), "can't serialize for zero");

let result: Vec<u8> = serialize_big_decimal(&BigDecimal::zero());
let deserialize_big_decimal: Result<bigdecimal::BigDecimal, Error> =
deserialize_big_decimal(&result.unwrap());
deserialize_big_decimal(&result);
assert!(
deserialize_big_decimal.is_ok(),
"can't deserialize for zero"
Expand Down
6 changes: 2 additions & 4 deletions lang/rust/avro/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,8 @@ pub(crate) fn encode_internal<S: Borrow<Schema>>(
buffer,
),
Value::BigDecimal(bg) => {
let result: Result<Vec<u8>, Error> = serialize_big_decimal(bg);
if let Ok(mut buf) = result {
buffer.append(&mut buf);
}
let mut buf: Vec<u8> = serialize_big_decimal(bg);
buffer.append(&mut buf);
}
Value::Bytes(bytes) => match *schema {
Schema::Bytes => encode_bytes(bytes, buffer),
Expand Down
3 changes: 3 additions & 0 deletions lang/rust/avro/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,9 @@ pub enum Error {
#[error("Unreadable decimal sign")]
BigDecimalSign,

#[error("Unreadable length for decimal inner bytes")]
BigDecimalLen,

#[error("Unreadable decimal scale")]
BigDecimalScale,

Expand Down
2 changes: 1 addition & 1 deletion lang/rust/avro/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ impl TryFrom<Value> for JsonValue {
Value::Decimal(ref d) => <Vec<u8>>::try_from(d)
.map(|vec| Self::Array(vec.into_iter().map(|v| v.into()).collect())),
Value::BigDecimal(ref bg) => {
let vec1: Vec<u8> = serialize_big_decimal(bg).unwrap();
let vec1: Vec<u8> = serialize_big_decimal(bg);
Ok(Self::Array(vec1.into_iter().map(|b| b.into()).collect()))
}
Value::TimeMillis(t) => Ok(Self::Number(t.into())),
Expand Down

0 comments on commit 7be6009

Please sign in to comment.