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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use crate::chunked_array::builder::CategoricalChunkedBuilder;
use crate::chunked_array::kernels::{cast_numeric_from_dtype, transmute_array_from_dtype};
use crate::prelude::*;
use arrow::compute::cast;
use num::{NumCast, ToPrimitive};
fn cast_ca<N, T>(ca: &ChunkedArray<T>) -> Result<ChunkedArray<N>>
where
N: PolarsDataType,
T: PolarsDataType,
{
if N::get_dtype() == T::get_dtype() {
return unsafe {
let ca = std::mem::transmute(ca.clone());
Ok(ca)
};
};
let chunks = ca
.chunks
.iter()
.map(|arr| cast(arr, &N::get_dtype().to_arrow()))
.collect::<arrow::error::Result<Vec<_>>>()?;
Ok(ChunkedArray::new_from_chunks(ca.field.name(), chunks))
}
macro_rules! cast_from_dtype {
($self: expr, $kernel:expr, $dtype: expr) => {{
let chunks = $self
.downcast_iter()
.into_iter()
.map(|arr| $kernel(arr, $dtype))
.collect();
Ok(ChunkedArray::new_from_chunks($self.field.name(), chunks))
}};
}
impl ChunkCast for CategoricalChunked {
fn cast<N>(&self) -> Result<ChunkedArray<N>>
where
N: PolarsDataType,
{
match N::get_dtype() {
DataType::Utf8 => {
let mapping = &**self.categorical_map.as_ref().expect("should be set");
let mut builder = Utf8ChunkedBuilder::new(self.name(), self.len(), self.len() * 5);
let f = |idx: u32| mapping.get(idx);
if self.null_count() == 0 {
self.into_no_null_iter()
.for_each(|idx| builder.append_value(f(idx)));
} else {
self.into_iter().for_each(|opt_idx| {
builder.append_option(opt_idx.map(f));
});
}
let ca = builder.finish();
let ca = unsafe { std::mem::transmute(ca) };
Ok(ca)
}
DataType::UInt32 => {
let mut ca: ChunkedArray<N> = unsafe { std::mem::transmute(self.clone()) };
ca.field = Arc::new(Field::new(ca.name(), DataType::UInt32));
Ok(ca)
}
_ => cast_ca(self),
}
}
}
impl<T> ChunkCast for ChunkedArray<T>
where
T: PolarsNumericType,
T::Native: NumCast + ToPrimitive,
{
fn cast<N>(&self) -> Result<ChunkedArray<N>>
where
N: PolarsDataType,
{
use DataType::*;
let ca = match (T::get_dtype(), N::get_dtype()) {
(UInt32, Categorical) => {
let mut ca: ChunkedArray<N> = unsafe { std::mem::transmute(self.clone()) };
ca.field = Arc::new(Field::new(ca.name(), DataType::Categorical));
return Ok(ca);
}
(Duration(_), Int64) => unsafe {
cast_from_dtype!(self, transmute_array_from_dtype, Int64.to_arrow())
},
(Duration(_), Float32) | (Date32, Float32) | (Date64, Float32)
| (Duration(_), Float64) | (Date32, Float64) | (Date64, Float64)
| (Duration(_), UInt64)
=> {
cast_from_dtype!(self, cast_numeric_from_dtype, N::get_dtype().to_arrow())
}
(Float64, Date64) | (Float32, Date64) => {
let out: Result<Int64Chunked> = cast_from_dtype!(self, cast_numeric_from_dtype, Int64.to_arrow());
out?.cast::<N>()
}
(Float64, Date32) | (Float32, Date32) => {
let out: Result<Int32Chunked> = cast_from_dtype!(self, cast_numeric_from_dtype, Int32.to_arrow());
out?.cast::<N>()
}
_ => cast_ca(self),
};
ca.map(|mut ca| {
ca.field = Arc::new(Field::new(ca.name(), N::get_dtype()));
ca
})
}
}
macro_rules! impl_chunkcast {
($ca_type:ident) => {
impl ChunkCast for $ca_type {
fn cast<N>(&self) -> Result<ChunkedArray<N>>
where
N: PolarsDataType,
{
cast_ca(self)
}
}
};
}
impl ChunkCast for Utf8Chunked {
fn cast<N>(&self) -> Result<ChunkedArray<N>>
where
N: PolarsDataType,
{
match N::get_dtype() {
DataType::Categorical => {
let iter = self.into_iter();
let mut builder = CategoricalChunkedBuilder::new(self.name(), self.len());
builder.from_iter(iter);
let ca = builder.finish();
let ca = unsafe { std::mem::transmute(ca) };
Ok(ca)
}
_ => cast_ca(self),
}
}
}
impl_chunkcast!(BooleanChunked);
impl_chunkcast!(ListChunked);