Skip to content

add native union type casting support#9544

Open
friendlymatthew wants to merge 1 commit intoapache:mainfrom
pydantic:friendlymatthew/cast-union
Open

add native union type casting support#9544
friendlymatthew wants to merge 1 commit intoapache:mainfrom
pydantic:friendlymatthew/cast-union

Conversation

@friendlymatthew
Copy link
Contributor

Which issue does this PR close?

Rationale for this change

This PR adds native support for casting Union arrays in the cast kernel. Previously, can_cast_types and cast_with_options had no handling for union types at all

@github-actions github-actions bot added the arrow Changes to the arrow crate label Mar 12, 2026
Copy link
Contributor Author

@friendlymatthew friendlymatthew left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self review

Comment on lines +1209 to +1211
(_, Union(_, _)) => Err(ArrowError::CastError(format!(
"Casting from {from_type} to {to_type} not supported"
))),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR does not support casting scalar types into a Union type

It's not a feature that we currently need and will probably need more complex work to get it correct. Though, I think this is a cool/valid feature. I can file an issue if liked

Comment on lines +2316 to +2333
let type_ids = array.type_ids().clone();
let offsets = array.offsets().cloned();

let new_children: Vec<ArrayRef> = from_fields
.iter()
.map(|(from_id, _from_field)| {
let (_, to_field) = to_fields
.iter()
.find(|(to_id, _)| *to_id == from_id)
.ok_or_else(|| {
ArrowError::CastError(format!(
"Cannot cast union: type_id {from_id} not found in target union"
))
})?;
let child = array.child(from_id);
cast_with_options(child.as_ref(), to_field.data_type(), cast_options)
})
.collect::<Result<_, _>>()?;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

look ups like this can go from n^2 to nlogn if we implement #8937, since sorting by type_id will give us logn searching

@friendlymatthew friendlymatthew force-pushed the friendlymatthew/cast-union branch from 26859a6 to 9c19cb1 Compare March 12, 2026 17:45
@friendlymatthew friendlymatthew force-pushed the friendlymatthew/cast-union branch from 9c19cb1 to 8dbe39f Compare March 12, 2026 18:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arrow Changes to the arrow crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Union type casting support to arrow-cast

1 participant