Skip to content

Commit 3056c06

Browse files
rework union casting to scalar
1 parent 92a239a commit 3056c06

File tree

2 files changed

+485
-1
lines changed

2 files changed

+485
-1
lines changed

arrow-cast/src/cast/mod.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,15 @@ mod list;
4343
mod map;
4444
mod run_array;
4545
mod string;
46+
mod union;
4647

4748
use crate::cast::decimal::*;
4849
use crate::cast::dictionary::*;
4950
use crate::cast::list::*;
5051
use crate::cast::map::*;
5152
use crate::cast::run_array::*;
5253
use crate::cast::string::*;
54+
pub use crate::cast::union::*;
5355

5456
use arrow_buffer::IntervalMonthDayNano;
5557
use arrow_data::ByteView;
@@ -230,7 +232,8 @@ pub fn can_cast_types(from_type: &DataType, to_type: &DataType) -> bool {
230232
}
231233
(Struct(_), _) => false,
232234
(_, Struct(_)) => false,
233-
235+
(Union(fields, _), _) => union::resolve_variant(fields, to_type).is_some(),
236+
(_, Union(_, _)) => false,
234237
(_, Boolean) => from_type.is_integer() || from_type.is_floating() || from_type.is_string(),
235238
(Boolean, _) => to_type.is_integer() || to_type.is_floating() || to_type.is_string(),
236239

@@ -1180,6 +1183,14 @@ pub fn cast_with_options(
11801183
(_, Struct(_)) => Err(ArrowError::CastError(format!(
11811184
"Casting from {from_type} to {to_type} not supported"
11821185
))),
1186+
(Union(_, _), _) => union_extract_by_type(
1187+
array.as_any().downcast_ref::<UnionArray>().unwrap(),
1188+
to_type,
1189+
cast_options,
1190+
),
1191+
(_, Union(_, _)) => Err(ArrowError::CastError(format!(
1192+
"Casting from {from_type} to {to_type} not supported"
1193+
))),
11831194
(_, Boolean) => match from_type {
11841195
UInt8 => cast_numeric_to_bool::<UInt8Type>(array),
11851196
UInt16 => cast_numeric_to_bool::<UInt16Type>(array),

0 commit comments

Comments
 (0)