MCPcopy Create free account
hub / github.com/apache/arrow-rs / test_default_encoder_byte_array

Function test_default_encoder_byte_array

arrow-json/src/writer/mod.rs:2488–2565  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

2486
2487 #[test]
2488 fn test_default_encoder_byte_array() {
2489 struct IntArrayBinaryEncoder<B> {
2490 array: B,
2491 }
2492
2493 impl<'a, B> Encoder for IntArrayBinaryEncoder<B>
2494 where
2495 B: ArrayAccessor<Item = &'a [u8]>,
2496 {
2497 fn encode(&mut self, idx: usize, out: &mut Vec<u8>) {
2498 out.push(b'[');
2499 let child = self.array.value(idx);
2500 for (idx, byte) in child.iter().enumerate() {
2501 write!(out, "{byte}").unwrap();
2502 if idx < child.len() - 1 {
2503 out.push(b',');
2504 }
2505 }
2506 out.push(b']');
2507 }
2508 }
2509
2510 #[derive(Debug)]
2511 struct IntArayBinaryEncoderFactory;
2512
2513 impl EncoderFactory for IntArayBinaryEncoderFactory {
2514 fn make_default_encoder<'a>(
2515 &self,
2516 _field: &'a FieldRef,
2517 array: &'a dyn Array,
2518 _options: &'a EncoderOptions,
2519 ) -> Result<Option<NullableEncoder<'a>>, ArrowError> {
2520 match array.data_type() {
2521 DataType::Binary => {
2522 let array = array.as_binary::<i32>();
2523 let encoder = IntArrayBinaryEncoder { array };
2524 let array_encoder = Box::new(encoder) as Box<dyn Encoder + 'a>;
2525 let nulls = array.nulls().cloned();
2526 Ok(Some(NullableEncoder::new(array_encoder, nulls)))
2527 }
2528 _ => Ok(None),
2529 }
2530 }
2531 }
2532
2533 let binary_array = BinaryArray::from_opt_vec(vec![Some(b"a"), None, Some(b"b")]);
2534 let float_array = Float64Array::from(vec![Some(1.0), Some(2.3), None]);
2535 let fields = vec![
2536 Field::new("bytes", DataType::Binary, true),
2537 Field::new("float", DataType::Float64, true),
2538 ];
2539 let batch = RecordBatch::try_new(
2540 Arc::new(Schema::new(fields)),
2541 vec![
2542 Arc::new(binary_array) as Arc<dyn Array>,
2543 Arc::new(float_array) as Arc<dyn Array>,
2544 ],
2545 )

Callers

nothing calls this directly

Calls 5

try_newFunction · 0.85
from_sliceFunction · 0.85
with_encoder_factoryMethod · 0.45
write_batchesMethod · 0.45
finishMethod · 0.45

Tested by

no test coverage detected