scovich commented on code in PR #8274:
URL: https://github.com/apache/arrow-rs/pull/8274#discussion_r2326404190


##########
arrow-avro/src/writer/encoder.rs:
##########
@@ -83,181 +78,417 @@ fn write_bool<W: Write + ?Sized>(writer: &mut W, v: bool) 
-> Result<(), ArrowErr
 /// Branch index is 0-based per Avro unions:
 /// - Null-first (default): null => 0, value => 1
 /// - Null-second (Impala): value => 0, null => 1
-#[inline]
-fn write_optional_branch<W: Write + ?Sized>(
-    writer: &mut W,
+fn write_optional_index<W: Write + ?Sized>(
+    out: &mut W,
     is_null: bool,
-    impala_mode: bool,
+    null_order: Nullability,
 ) -> Result<(), ArrowError> {
-    let branch = if impala_mode == is_null { 1 } else { 0 };
-    write_int(writer, branch)
+    let byte = union_value_branch_byte(null_order, is_null);
+    out.write_all(&[byte])
+        .map_err(|e| ArrowError::IoError(format!("write union branch: {e}"), 
e))
 }
 
-/// Encode a `RecordBatch` in Avro binary format using **default options**.
-pub fn encode_record_batch<W: Write>(batch: &RecordBatch, out: &mut W) -> 
Result<(), ArrowError> {
-    encode_record_batch_with_options(batch, out, &EncoderOptions::default())
+#[derive(Debug, Clone)]
+enum NullState {
+    NonNullable,
+    NullableNoNulls {
+        union_value_byte: u8,
+    },
+    Nullable {
+        nulls: NullBuffer,
+        null_order: Nullability,
+    },
 }
 
-/// Encode a `RecordBatch` with explicit `EncoderOptions`.
-pub fn encode_record_batch_with_options<W: Write>(
-    batch: &RecordBatch,
-    out: &mut W,
-    opts: &EncoderOptions,
-) -> Result<(), ArrowError> {
-    let mut encoders = batch
-        .schema()
-        .fields()
-        .iter()
-        .zip(batch.columns())
-        .map(|(field, array)| Ok((field.is_nullable(), 
make_encoder(array.as_ref())?)))
-        .collect::<Result<Vec<_>, ArrowError>>()?;
-    (0..batch.num_rows()).try_for_each(|row| {
-        encoders.iter_mut().try_for_each(|(is_nullable, enc)| {
-            if *is_nullable {
-                let is_null = enc.is_null(row);
-                write_optional_branch(out, is_null, opts.impala_mode)?;
-                if is_null {
-                    return Ok(());
+/// Arrow to Avro FieldEncoder:
+/// - Holds the inner `Encoder` (by value)
+/// - Carries the per-site nullability **state** as a single enum that 
enforces invariants
+pub struct FieldEncoder<'a> {
+    encoder: Encoder<'a>,
+    null_state: NullState,
+}
+
+impl<'a> FieldEncoder<'a> {
+    fn make_encoder(
+        array: &'a dyn Array,
+        field: &Field,
+        plan: &FieldPlan,
+        nullability: Option<Nullability>,
+    ) -> Result<Self, ArrowError> {
+        let has_nulls = array.null_count() > 0;

Review Comment:
   ```suggestion
   ```
   no longer used?



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to