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-3892: [Rust] Support to resolve fixed from bytes and deserialize bytes in deserialize_any #2567

Merged
merged 7 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion lang/rust/avro/examples/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fn benchmark(

let start = Instant::now();
let mut writer = Writer::new(schema, BufWriter::new(Vec::new()));
writer.extend(records.into_iter())?;
writer.extend(records)?;

let duration = Instant::now().duration_since(start);
durations.push(duration);
Expand Down
5 changes: 5 additions & 0 deletions lang/rust/avro/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ impl<'a, 'de> de::Deserializer<'de> for &'a Deserializer<'de> {
Value::String(ref s) => visitor.visit_borrowed_str(s),
Value::Uuid(uuid) => visitor.visit_str(&uuid.to_string()),
Value::Map(ref items) => visitor.visit_map(MapDeserializer::new(items)),
Value::Bytes(ref bytes) | Value::Fixed(_, ref bytes) => visitor.visit_bytes(bytes),
Value::Decimal(ref d) => visitor.visit_bytes(&d.to_vec()?),
_ => Err(de::Error::custom(format!(
"unsupported union: {:?}",
self.input
Expand All @@ -276,6 +278,8 @@ impl<'a, 'de> de::Deserializer<'de> for &'a Deserializer<'de> {
Value::String(ref s) => visitor.visit_borrowed_str(s),
Value::Uuid(uuid) => visitor.visit_str(&uuid.to_string()),
Value::Map(ref items) => visitor.visit_map(MapDeserializer::new(items)),
Value::Bytes(ref bytes) | Value::Fixed(_, ref bytes) => visitor.visit_bytes(bytes),
Value::Decimal(ref d) => visitor.visit_bytes(&d.to_vec()?),
value => Err(de::Error::custom(format!(
"incorrect value of type: {:?}",
crate::schema::SchemaKind::from(value)
Expand Down Expand Up @@ -350,6 +354,7 @@ impl<'a, 'de> de::Deserializer<'de> for &'a Deserializer<'de> {
Value::String(ref s) => visitor.visit_bytes(s.as_bytes()),
Value::Bytes(ref bytes) | Value::Fixed(_, ref bytes) => visitor.visit_bytes(bytes),
Value::Uuid(ref u) => visitor.visit_bytes(u.as_bytes()),
Value::Decimal(ref d) => visitor.visit_bytes(&d.to_vec()?),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be nice to add new unit tests for the changes above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for review! I have added the test for it.

_ => Err(de::Error::custom(format!(
"Expected a String|Bytes|Fixed|Uuid, but got {:?}",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"Expected a String|Bytes|Fixed|Uuid, but got {:?}",
"Expected a String|Bytes|Fixed|Uuid|Decimal, but got {:?}",

self.input
Expand Down
2 changes: 1 addition & 1 deletion lang/rust/avro/src/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl Decimal {
self.len
}

fn to_vec(&self) -> AvroResult<Vec<u8>> {
pub(crate) fn to_vec(&self) -> AvroResult<Vec<u8>> {
self.to_sign_extended_bytes_with_len(self.len)
}

Expand Down
46 changes: 46 additions & 0 deletions lang/rust/avro/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,13 @@ impl Value {
}
}
Value::String(s) => Ok(Value::Fixed(s.len(), s.into_bytes())),
Value::Bytes(s) => {
if s.len() == size {
Ok(Value::Fixed(size, s))
} else {
Err(Error::CompareFixedSizes { size, n: s.len() })
}
}
other => Err(Error::GetStringForFixed(other.into())),
}
}
Expand Down Expand Up @@ -2958,4 +2965,43 @@ Field with name '"b"' is not a member of the map items"#,

Ok(())
}

#[test]
fn resolve_fixed_from_bytes() -> TestResult {
let value = Value::Bytes(vec![97, 98, 99]);
assert_eq!(
value.resolve(&Schema::Fixed(FixedSchema {
name: "test".into(),
aliases: None,
doc: None,
size: 3,
attributes: Default::default()
}))?,
Value::Fixed(3, vec![97, 98, 99])
);

let value = Value::Bytes(vec![97, 99]);
assert!(value
.resolve(&Schema::Fixed(FixedSchema {
name: "test".into(),
aliases: None,
doc: None,
size: 3,
attributes: Default::default()
}))
.is_err(),);

let value = Value::Bytes(vec![97, 98, 99, 100]);
assert!(value
.resolve(&Schema::Fixed(FixedSchema {
name: "test".into(),
aliases: None,
doc: None,
size: 3,
attributes: Default::default()
}))
.is_err(),);

Ok(())
}
}
4 changes: 2 additions & 2 deletions lang/rust/avro/src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,7 @@ mod tests {
let record_copy = record.clone();
let records = vec![record, record_copy];

let n1 = writer.extend(records.into_iter())?;
let n1 = writer.extend(records)?;
let n2 = writer.flush()?;
let result = writer.into_inner()?;

Expand Down Expand Up @@ -970,7 +970,7 @@ mod tests {
let record_copy = record.clone();
let records = vec![record, record_copy];

let n1 = writer.extend_ser(records.into_iter())?;
let n1 = writer.extend_ser(records)?;
let n2 = writer.flush()?;
let result = writer.into_inner()?;

Expand Down