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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
use super::{primitive_as_primitive, primitive_to_primitive, CastOptions};
use crate::{
array::{Array, DictionaryArray, DictionaryKey, PrimitiveArray},
compute::{cast::cast, take::take},
datatypes::DataType,
error::{Error, Result},
};
macro_rules! key_cast {
($keys:expr, $values:expr, $array:expr, $to_keys_type:expr, $to_type:ty, $to_datatype:expr) => {{
let cast_keys = primitive_to_primitive::<_, $to_type>($keys, $to_keys_type);
if cast_keys.null_count() > $keys.null_count() {
return Err(Error::Overflow);
}
unsafe {
DictionaryArray::try_new_unchecked($to_datatype, cast_keys, $values.clone())
}
.map(|x| x.boxed())
}};
}
pub fn dictionary_to_dictionary_values<K: DictionaryKey>(
from: &DictionaryArray<K>,
values_type: &DataType,
) -> Result<DictionaryArray<K>> {
let keys = from.keys();
let values = from.values();
let length = values.len();
let values = cast(values.as_ref(), values_type, CastOptions::default())?;
assert_eq!(values.len(), length); unsafe {
DictionaryArray::try_new_unchecked(from.data_type().clone(), keys.clone(), values.clone())
}
}
pub fn wrapping_dictionary_to_dictionary_values<K: DictionaryKey>(
from: &DictionaryArray<K>,
values_type: &DataType,
) -> Result<DictionaryArray<K>> {
let keys = from.keys();
let values = from.values();
let length = values.len();
let values = cast(
values.as_ref(),
values_type,
CastOptions {
wrapped: true,
partial: false,
},
)?;
assert_eq!(values.len(), length); unsafe {
DictionaryArray::try_new_unchecked(from.data_type().clone(), keys.clone(), values.clone())
}
}
pub fn dictionary_to_dictionary_keys<K1, K2>(
from: &DictionaryArray<K1>,
) -> Result<DictionaryArray<K2>>
where
K1: DictionaryKey + num_traits::NumCast,
K2: DictionaryKey + num_traits::NumCast,
{
let keys = from.keys();
let values = from.values();
let is_ordered = from.is_ordered();
let casted_keys = primitive_to_primitive::<K1, K2>(keys, &K2::PRIMITIVE.into());
if casted_keys.null_count() > keys.null_count() {
Err(Error::Overflow)
} else {
let data_type = DataType::Dictionary(
K2::KEY_TYPE,
Box::new(values.data_type().clone()),
is_ordered,
);
unsafe { DictionaryArray::try_new_unchecked(data_type, casted_keys, values.clone()) }
}
}
pub fn wrapping_dictionary_to_dictionary_keys<K1, K2>(
from: &DictionaryArray<K1>,
) -> Result<DictionaryArray<K2>>
where
K1: DictionaryKey + num_traits::AsPrimitive<K2>,
K2: DictionaryKey,
{
let keys = from.keys();
let values = from.values();
let is_ordered = from.is_ordered();
let casted_keys = primitive_as_primitive::<K1, K2>(keys, &K2::PRIMITIVE.into());
if casted_keys.null_count() > keys.null_count() {
Err(Error::Overflow)
} else {
let data_type = DataType::Dictionary(
K2::KEY_TYPE,
Box::new(values.data_type().clone()),
is_ordered,
);
DictionaryArray::try_new(data_type, casted_keys, values.clone())
}
}
pub(super) fn dictionary_cast_dyn<K: DictionaryKey + num_traits::NumCast>(
array: &dyn Array,
to_type: &DataType,
options: CastOptions,
) -> Result<Box<dyn Array>> {
let array = array.as_any().downcast_ref::<DictionaryArray<K>>().unwrap();
let keys = array.keys();
let values = array.values();
match to_type {
DataType::Dictionary(to_keys_type, to_values_type, _) => {
let values = cast(values.as_ref(), to_values_type, options)?;
let to_key_type = (*to_keys_type).into();
match_integer_type!(to_keys_type, |$T| {
key_cast!(keys, values, array, &to_key_type, $T, to_type.clone())
})
}
_ => unpack_dictionary::<K>(keys, values.as_ref(), to_type, options),
}
}
fn unpack_dictionary<K>(
keys: &PrimitiveArray<K>,
values: &dyn Array,
to_type: &DataType,
options: CastOptions,
) -> Result<Box<dyn Array>>
where
K: DictionaryKey + num_traits::NumCast,
{
let values = cast(values, to_type, options)?;
let indices = primitive_to_primitive::<_, i32>(keys, &DataType::Int32);
take(values.as_ref(), &indices)
}
pub fn dictionary_to_values<K>(from: &DictionaryArray<K>) -> Box<dyn Array>
where
K: DictionaryKey + num_traits::NumCast,
{
let indices = primitive_to_primitive::<_, i64>(from.keys(), &DataType::Int64);
take(from.values().as_ref(), &indices).unwrap()
}