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

Function cast_string_to_timestamp_impl

arrow-cast/src/cast/string.rs:157–205  ·  view source on GitHub ↗
(
    iter: I,
    tz: &Tz,
    cast_options: &CastOptions,
)

Source from the content-addressed store, hash-verified

155}
156
157fn cast_string_to_timestamp_impl<
158 'a,
159 I: Iterator<Item = Option<&'a str>>,
160 T: ArrowTimestampType,
161 Tz: TimeZone,
162>(
163 iter: I,
164 tz: &Tz,
165 cast_options: &CastOptions,
166) -> Result<PrimitiveArray<T>, ArrowError> {
167 if cast_options.safe {
168 let iter = iter.map(|v| {
169 v.and_then(|v| {
170 let naive = string_to_datetime(tz, v).ok()?.naive_utc();
171 T::from_naive_datetime(naive, None)
172 })
173 });
174 // Benefit:
175 // 20% performance improvement
176 // Soundness:
177 // The iterator is trustedLen because it comes from an `StringArray`.
178
179 Ok(unsafe { PrimitiveArray::from_trusted_len_iter(iter) })
180 } else {
181 let vec = iter
182 .map(|v| {
183 v.map(|v| {
184 let naive = string_to_datetime(tz, v)?.naive_utc();
185 T::from_naive_datetime(naive, None).ok_or_else(|| match T::UNIT {
186 TimeUnit::Nanosecond => ArrowError::CastError(format!(
187 "Overflow converting {naive} to Nanosecond. The dates that can be represented as nanoseconds have to be between 1677-09-21T00:12:44.0 and 2262-04-11T23:47:16.854775804"
188 )),
189 _ => ArrowError::CastError(format!(
190 "Overflow converting {naive} to {:?}",
191 T::UNIT
192 ))
193 })
194 })
195 .transpose()
196 })
197 .collect::<Result<Vec<Option<i64>>, _>>()?;
198
199 // Benefit:
200 // 20% performance improvement
201 // Soundness:
202 // The iterator is trustedLen because it comes from an `StringArray`.
203 Ok(unsafe { PrimitiveArray::from_trusted_len_iter(vec.iter()) })
204 }
205}
206
207pub(crate) fn cast_string_to_interval<Offset, F, ArrowType>(
208 array: &dyn Array,

Callers 2

cast_string_to_timestampFunction · 0.85
cast_view_to_timestampFunction · 0.85

Calls 3

string_to_datetimeFunction · 0.85
and_thenMethod · 0.80
iterMethod · 0.45

Tested by

no test coverage detected