nathaniel-d-ef commented on code in PR #8298:
URL: https://github.com/apache/arrow-rs/pull/8298#discussion_r2344288046
##########
arrow-avro/src/writer/encoder.rs:
##########
@@ -763,4 +1179,100 @@ mod tests {
let got = encode_all(&arr, &FieldPlan::Scalar, None);
assert_bytes_eq(&got, &expected);
}
+
+ #[test]
+ fn list_encoder_int32() {
+ // Build ListArray [[1,2], [], [3]]
+ let values = Int32Array::from(vec![1, 2, 3]);
+ let offsets = vec![0, 2, 2, 3];
+ let list = ListArray::new(
+ Field::new("item", DataType::Int32, true).into(),
+ arrow_buffer::OffsetBuffer::new(offsets.into()),
+ Arc::new(values) as ArrayRef,
+ None,
+ );
+ // Avro array encoding per row
+ let mut expected = Vec::new();
+ // row 0: block len 2, items 1,2 then 0
+ expected.extend(avro_long_bytes(2));
+ expected.extend(avro_long_bytes(1));
+ expected.extend(avro_long_bytes(2));
+ expected.extend(avro_long_bytes(0));
+ // row 1: empty
+ expected.extend(avro_long_bytes(0));
+ // row 2: one item 3
+ expected.extend(avro_long_bytes(1));
+ expected.extend(avro_long_bytes(3));
+ expected.extend(avro_long_bytes(0));
+
+ let plan = FieldPlan::List {
+ items_nullability: None,
+ item_plan: Box::new(FieldPlan::Scalar),
+ };
+ let got = encode_all(&list, &plan, None);
+ assert_bytes_eq(&got, &expected);
+ }
+
+ #[test]
+ fn struct_encoder_two_fields() {
+ // Struct { a: Int32, b: Utf8 }
+ let a = Int32Array::from(vec![1, 2]);
+ let b = StringArray::from(vec!["x", "y"]);
+ let fields = Fields::from(vec![
+ Field::new("a", DataType::Int32, true),
+ Field::new("b", DataType::Utf8, true),
+ ]);
+ let struct_arr = StructArray::new(
+ fields.clone(),
+ vec![Arc::new(a) as ArrayRef, Arc::new(b) as ArrayRef],
+ None,
+ );
+ let plan = FieldPlan::Struct {
+ encoders: vec![
+ FieldBinding {
+ arrow_index: 0,
+ nullability: None,
+ plan: FieldPlan::Scalar,
+ },
+ FieldBinding {
+ arrow_index: 1,
+ nullability: None,
+ plan: FieldPlan::Scalar,
+ },
+ ],
+ };
+ let got = encode_all(&struct_arr, &plan, None);
+ // Expected: rows concatenated: a then b
+ let mut expected = Vec::new();
+ expected.extend(avro_long_bytes(1)); // a=1
+ expected.extend(avro_len_prefixed_bytes(b"x")); // b="x"
+ expected.extend(avro_long_bytes(2)); // a=2
+ expected.extend(avro_len_prefixed_bytes(b"y")); // b="y"
+ assert_bytes_eq(&got, &expected);
+ }
+
+ #[test]
+ fn decimal_bytes_and_fixed() {
+ // Use Decimal128 with small positives and negatives
+ let dec = Decimal128Array::from(vec![1i128, -1i128, 0i128])
+ .with_precision_and_scale(20, 0)
+ .unwrap();
+ // bytes(decimal): minimal two's complement length-prefixed
+ let plan_bytes = FieldPlan::Decimal { size: None };
+ let got_bytes = encode_all(&dec, &plan_bytes, None);
+ // 1 -> 0x01; -1 -> 0xFF; 0 -> 0x00
+ let mut expected_bytes = Vec::new();
+ expected_bytes.extend(avro_len_prefixed_bytes(&[0x01]));
+ expected_bytes.extend(avro_len_prefixed_bytes(&[0xFF]));
+ expected_bytes.extend(avro_len_prefixed_bytes(&[0x00]));
+ assert_bytes_eq(&got_bytes, &expected_bytes);
+
+ let plan_fixed = FieldPlan::Decimal { size: Some(16) };
+ let got_fixed = encode_all(&dec, &plan_fixed, None);
+ let mut expected_fixed = Vec::new();
+ expected_fixed.extend_from_slice(&1i128.to_be_bytes());
+ expected_fixed.extend_from_slice(&(-1i128).to_be_bytes());
+ expected_fixed.extend_from_slice(&0i128.to_be_bytes());
+ assert_bytes_eq(&got_fixed, &expected_fixed);
+ }
}
Review Comment:
Added this and several more tests for uncovered paths.
--
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]