This is an automated email from the ASF dual-hosted git repository.
fokko pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/iceberg-rust.git
The following commit(s) were added to refs/heads/main by this push:
new 9f94186 fix: avro bytes test for Literal (#80)
9f94186 is described below
commit 9f94186301a41d6c7c05a1b98059c24b2fddf069
Author: JanKaul <[email protected]>
AuthorDate: Wed Oct 18 12:52:48 2023 +0200
fix: avro bytes test for Literal (#80)
* fix avro bytes test
* impl from Literal for vec u8
* fix clippy warnings
---
crates/iceberg/src/spec/values.rs | 36 +++++++++++++++++++++++++++++++++---
1 file changed, 33 insertions(+), 3 deletions(-)
diff --git a/crates/iceberg/src/spec/values.rs
b/crates/iceberg/src/spec/values.rs
index fd1b2b5..d924bd5 100644
--- a/crates/iceberg/src/spec/values.rs
+++ b/crates/iceberg/src/spec/values.rs
@@ -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()),
@@ -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 {
@@ -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();