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

fix: avro bytes test for Literal #80

Merged
merged 3 commits into from
Oct 18, 2023
Merged
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
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
Loading