nathaniel-d-ef commented on code in PR #8298:
URL: https://github.com/apache/arrow-rs/pull/8298#discussion_r2344294481


##########
arrow-avro/src/writer/encoder.rs:
##########
@@ -653,6 +893,182 @@ fn prepare_value_site_encoder<'a>(
     FieldEncoder::make_encoder(values_array, value_field, plan, nullability)
 }
 
+/// Avro `fixed` encoder for Arrow `FixedSizeBinaryArray`.
+/// Spec: a fixed is encoded as exactly `size` bytes, with no length prefix.
+struct FixedEncoder<'a>(&'a FixedSizeBinaryArray);
+impl FixedEncoder<'_> {
+    fn encode<W: Write + ?Sized>(&mut self, out: &mut W, idx: usize) -> 
Result<(), ArrowError> {
+        let v = self.0.value(idx); // &[u8] of fixed width
+        out.write_all(v)
+            .map_err(|e| ArrowError::IoError(format!("write fixed bytes: 
{e}"), e))
+    }
+}
+
+/// Avro UUID logical type encoder: Arrow FixedSizeBinary(16) → Avro string 
(UUID).
+/// Spec: uuid is a logical type over string (RFC‑4122). We output hyphenated 
form.
+struct UuidEncoder<'a>(&'a FixedSizeBinaryArray);
+impl UuidEncoder<'_> {
+    fn encode<W: Write + ?Sized>(&mut self, out: &mut W, idx: usize) -> 
Result<(), ArrowError> {
+        let v = self.0.value(idx);
+        if v.len() != 16 {
+            return Err(ArrowError::InvalidArgumentError(
+                "logicalType=uuid requires FixedSizeBinary(16)".into(),
+            ));
+        }
+        let u = Uuid::from_slice(v)
+            .map_err(|e| ArrowError::InvalidArgumentError(format!("Invalid 
UUID bytes: {e}")))?;
+        let mut tmp = [0u8; uuid::fmt::Hyphenated::LENGTH];
+        let s = u.hyphenated().encode_lower(&mut tmp);
+        write_len_prefixed(out, s.as_bytes())
+    }
+}

Review Comment:
   Agreed, this is much cleaner



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscr...@arrow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to