Skip to content

Commit

Permalink
fix: avro bytes test for Literal (#80)
Browse files Browse the repository at this point in the history
* fix avro bytes test

* impl from Literal for vec u8

* fix clippy warnings
  • Loading branch information
JanKaul authored Oct 18, 2023
1 parent 66ff7ce commit 9f94186
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions crates/iceberg/src/spec/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,9 +450,9 @@ impl From<Literal> for ByteBuf {
Literal::Primitive(prim) => match prim {
PrimitiveLiteral::Boolean(val) => {
if val {
ByteBuf::from([0u8])
} else {
ByteBuf::from([1u8])
} else {
ByteBuf::from([0u8])
}
}
PrimitiveLiteral::Int(val) => ByteBuf::from(val.to_le_bytes()),
Expand All @@ -474,6 +474,36 @@ impl From<Literal> for ByteBuf {
}
}

impl From<Literal> for Vec<u8> {
fn from(value: Literal) -> Self {
match value {
Literal::Primitive(prim) => match prim {
PrimitiveLiteral::Boolean(val) => {
if val {
Vec::from([1u8])
} else {
Vec::from([0u8])
}
}
PrimitiveLiteral::Int(val) => Vec::from(val.to_le_bytes()),
PrimitiveLiteral::Long(val) => Vec::from(val.to_le_bytes()),
PrimitiveLiteral::Float(val) => Vec::from(val.to_le_bytes()),
PrimitiveLiteral::Double(val) => Vec::from(val.to_le_bytes()),
PrimitiveLiteral::Date(val) => Vec::from(val.to_le_bytes()),
PrimitiveLiteral::Time(val) => Vec::from(val.to_le_bytes()),
PrimitiveLiteral::Timestamp(val) => Vec::from(val.to_le_bytes()),
PrimitiveLiteral::TimestampTZ(val) => Vec::from(val.to_le_bytes()),
PrimitiveLiteral::String(val) => Vec::from(val.as_bytes()),
PrimitiveLiteral::UUID(val) => Vec::from(val.as_u128().to_be_bytes()),
PrimitiveLiteral::Fixed(val) => val,
PrimitiveLiteral::Binary(val) => val,
PrimitiveLiteral::Decimal(_) => todo!(),
},
_ => unimplemented!(),
}
}
}

impl From<&Literal> for JsonValue {
fn from(value: &Literal) -> Self {
match value {
Expand Down Expand Up @@ -995,7 +1025,7 @@ mod tests {
assert_eq!(literal, expected_literal);

let mut writer = apache_avro::Writer::new(&schema, Vec::new());
writer.append_ser(bytes).unwrap();
writer.append_ser(ByteBuf::from(literal)).unwrap();
let encoded = writer.into_inner().unwrap();
let reader = apache_avro::Reader::new(&*encoded).unwrap();

Expand Down

0 comments on commit 9f94186

Please sign in to comment.