pola-rs/polars · error
Union struct must be created with the corresponding Union…
Error message
Union struct must be created with the corresponding Union DataType
What it means
`UnionArray::new_null` needs the union's field definitions and offset/type configuration from `ArrowDataType::Union(union_dtype)` to build the null child arrays and the all-zero types buffer. Any non-Union dtype cannot supply those components, so the constructor panics.
Solutions
- Guard with `matches!(dtype.to_storage(), ArrowDataType::Union(_))` before calling `new_null`.
- Use the generic `new_null_array(dtype, length)` dispatcher instead of the Union-specific constructor.
- Fix the dtype source so a genuine Union dtype (with fields) is passed.
- Convert the panic path into a Result in your own wrapper for user-facing error handling.
Example fix
// before
let arr = UnionArray::new_null(dtype.clone(), length);
// after
let arr = match dtype.to_storage() {
ArrowDataType::Union(_) => UnionArray::new_null(dtype.clone(), length),
_ => new_null_array(dtype.clone(), length),
}; Defensive patterns
Strategy: validation
Validate before calling
fn is_union_dtype(dtype: &ArrowDataType) -> bool {
matches!(dtype.to_storage(), ArrowDataType::Union(_))
} Type guard
fn as_union_dtype(dtype: &ArrowDataType) -> Option<&UnionDataType> {
match dtype.to_storage() {
ArrowDataType::Union(u) => Some(u),
_ => None,
}
} Try / catch
// check before construction:
if as_union_dtype(&dtype).is_none() {
return Err(PolarsError::InvalidOperation("expected Union dtype".into()));
}
let arr = UnionArray::new_null(dtype, length); Prevention
- Route generic array creation through `new_null_array` instead of Union-specific constructors
- Match on `to_storage()` when special-casing unions
- Test dtype-dispatch code with Dense and Sparse union dtypes
When it happens
Trigger: Calling `UnionArray::new_null(dtype, length)` with a dtype that is not `ArrowDataType::Union` (checked via `dtype.to_storage()`).
Common situations: Generic array factories dispatching on runtime dtypes that feed a Union constructor the wrong variant, schema deserialization producing a flattened/incorrect dtype, or a Dense/Sparse union variable replaced by a different physical type during refactoring.
Related errors
- MutableUtf8ValuesArray can only be initialized with…
- not implemented
- StructArray must be initialized with DataType::Struct
- activate dtype
- cannot set validity of a union array
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/89249507193ac650.
Report an issue: GitHub.
Appendix: source
Thrown at crates/polars-arrow/src/array/union/mod.rs:192
if let ArrowDataType::Union(u) = &dtype {
let fields = u
.fields
.iter()
.map(|x| new_null_array(x.dtype().clone(), length))
.collect();
let offsets = if u.mode.is_sparse() {
None
} else {
Some((0..length as i32).collect::<Vec<_>>().into())
};
// all from the same field
let types = vec![0i8; length].into();
Self::new(dtype, types, fields, offsets)
} else {
panic!("Union struct must be created with the corresponding Union DataType")
}
}
/// Creates a new empty [`UnionArray`].
pub fn new_empty(dtype: ArrowDataType) -> Self {
if let ArrowDataType::Union(u) = dtype.to_storage() {
let fields = u
.fields
.iter()
.map(|x| new_empty_array(x.dtype().clone()))
.collect();
let offsets = if u.mode.is_sparse() {
None
} else {
Some(Buffer::default())
};
View on GitHub (pinned to fe841f959e)