1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
use std::convert::TryFrom;
use crate::error::{ArrowError, Result};
use crate::{array::*, datatypes::DataType, types::NativeType};
use super::CastOptions;
pub fn binary_to_large_binary(from: &BinaryArray<i32>, to_data_type: DataType) -> BinaryArray<i64> {
let values = from.values().clone();
let offsets = from.offsets().iter().map(|x| *x as i64).collect::<Vec<_>>();
BinaryArray::<i64>::new(
to_data_type,
offsets.into(),
values,
from.validity().cloned(),
)
}
pub fn binary_large_to_binary(
from: &BinaryArray<i64>,
to_data_type: DataType,
) -> Result<BinaryArray<i32>> {
let values = from.values().clone();
let _ =
i32::try_from(*from.offsets().last().unwrap()).map_err(ArrowError::from_external_error)?;
let offsets = from.offsets().iter().map(|x| *x as i32).collect::<Vec<_>>();
Ok(BinaryArray::<i32>::new(
to_data_type,
offsets.into(),
values,
from.validity().cloned(),
))
}
pub fn partial_binary_to_primitive<O: Offset, T>(
from: &BinaryArray<O>,
to: &DataType,
) -> PrimitiveArray<T>
where
T: NativeType + lexical_core::FromLexical,
{
let iter = from
.iter()
.map(|x| x.and_then::<T, _>(|x| lexical_core::parse_partial(x).ok().map(|x| x.0)));
PrimitiveArray::<T>::from_trusted_len_iter(iter).to(to.clone())
}
pub fn binary_to_primitive<O: Offset, T>(from: &BinaryArray<O>, to: &DataType) -> PrimitiveArray<T>
where
T: NativeType + lexical_core::FromLexical,
{
let iter = from
.iter()
.map(|x| x.and_then::<T, _>(|x| lexical_core::parse(x).ok()));
PrimitiveArray::<T>::from_trusted_len_iter(iter).to(to.clone())
}
pub(super) fn binary_to_primitive_dyn<O: Offset, T>(
from: &dyn Array,
to: &DataType,
options: CastOptions,
) -> Result<Box<dyn Array>>
where
T: NativeType + lexical_core::FromLexical,
{
let from = from.as_any().downcast_ref().unwrap();
if options.partial {
Ok(Box::new(partial_binary_to_primitive::<O, T>(from, to)))
} else {
Ok(Box::new(binary_to_primitive::<O, T>(from, to)))
}
}
pub fn binary_to_dictionary<O: Offset, K: DictionaryKey>(
from: &BinaryArray<O>,
) -> Result<DictionaryArray<K>> {
let mut array = MutableDictionaryArray::<K, MutableBinaryArray<O>>::new();
array.try_extend(from.iter())?;
Ok(array.into())
}
pub(super) fn binary_to_dictionary_dyn<O: Offset, K: DictionaryKey>(
from: &dyn Array,
) -> Result<Box<dyn Array>> {
let values = from.as_any().downcast_ref().unwrap();
binary_to_dictionary::<O, K>(values).map(|x| Box::new(x) as Box<dyn Array>)
}