| 85 | } |
| 86 | |
| 87 | fn parse_string_iter< |
| 88 | 'a, |
| 89 | P: Parser, |
| 90 | I: Iterator<Item = Option<&'a str>>, |
| 91 | F: FnOnce() -> Option<NullBuffer>, |
| 92 | >( |
| 93 | iter: I, |
| 94 | cast_options: &CastOptions, |
| 95 | nulls: F, |
| 96 | ) -> Result<ArrayRef, ArrowError> { |
| 97 | let array = if cast_options.safe { |
| 98 | let iter = iter.map(|x| x.and_then(P::parse)); |
| 99 | |
| 100 | // Benefit: |
| 101 | // 20% performance improvement |
| 102 | // Soundness: |
| 103 | // The iterator is trustedLen because it comes from an `StringArray`. |
| 104 | unsafe { PrimitiveArray::<P>::from_trusted_len_iter(iter) } |
| 105 | } else { |
| 106 | let v = iter |
| 107 | .map(|x| match x { |
| 108 | Some(v) => P::parse(v).ok_or_else(|| { |
| 109 | ArrowError::CastError(format!( |
| 110 | "Cannot cast string '{v}' to value of {} type", |
| 111 | P::DATA_TYPE |
| 112 | )) |
| 113 | }), |
| 114 | None => Ok(P::Native::default()), |
| 115 | }) |
| 116 | .collect::<Result<Vec<_>, ArrowError>>()?; |
| 117 | PrimitiveArray::try_new(v.into(), nulls())? |
| 118 | }; |
| 119 | |
| 120 | Ok(Arc::new(array) as ArrayRef) |
| 121 | } |
| 122 | |
| 123 | /// Casts generic string arrays to an ArrowTimestampType (TimeStampNanosecondArray, etc.) |
| 124 | pub(crate) fn cast_string_to_timestamp<O: OffsetSizeTrait, T: ArrowTimestampType>( |