| 460 | } |
| 461 | |
| 462 | fn delegated_decode<'a, DecodeFunction, InputBinaryArray, OutputOffset>( |
| 463 | decode: DecodeFunction, |
| 464 | input: &InputBinaryArray, |
| 465 | conservative_upper_bound_size: usize, |
| 466 | ) -> Result<ArrayRef> |
| 467 | where |
| 468 | DecodeFunction: Fn(&[u8], &mut [u8]) -> Result<usize>, |
| 469 | InputBinaryArray: BinaryArrayType<'a>, |
| 470 | OutputOffset: OffsetSizeTrait, |
| 471 | { |
| 472 | let mut values = vec![0; conservative_upper_bound_size]; |
| 473 | let mut offsets = OffsetBufferBuilder::new(input.len()); |
| 474 | let mut total_bytes_decoded = 0; |
| 475 | for v in input.iter() { |
| 476 | if let Some(v) = v { |
| 477 | let cursor = &mut values[total_bytes_decoded..]; |
| 478 | let decoded = decode(v, cursor)?; |
| 479 | total_bytes_decoded += decoded; |
| 480 | offsets.push_length(decoded); |
| 481 | } else { |
| 482 | offsets.push_length(0); |
| 483 | } |
| 484 | } |
| 485 | // We reserved an upper bound size for the values buffer, but we only use the actual size |
| 486 | values.truncate(total_bytes_decoded); |
| 487 | let binary_array = GenericBinaryArray::<OutputOffset>::try_new( |
| 488 | offsets.finish(), |
| 489 | Buffer::from_vec(values), |
| 490 | input.nulls().cloned(), |
| 491 | )?; |
| 492 | Ok(Arc::new(binary_array)) |
| 493 | } |
| 494 | |
| 495 | #[cfg(test)] |
| 496 | mod tests { |