{"record":{"id":"89249507193ac650","repo":"pola-rs/polars","slug":"union-struct-must-be-created-with-the-correspondin","errorCode":null,"errorMessage":"Union struct must be created with the corresponding Union DataType","messagePattern":"Union struct must be created with the corresponding Union DataType","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/polars-arrow/src/array/union/mod.rs","lineNumber":192,"sourceCode":"        if let ArrowDataType::Union(u) = &dtype {\n            let fields = u\n                .fields\n                .iter()\n                .map(|x| new_null_array(x.dtype().clone(), length))\n                .collect();\n\n            let offsets = if u.mode.is_sparse() {\n                None\n            } else {\n                Some((0..length as i32).collect::<Vec<_>>().into())\n            };\n\n            // all from the same field\n            let types = vec![0i8; length].into();\n\n            Self::new(dtype, types, fields, offsets)\n        } else {\n            panic!(\"Union struct must be created with the corresponding Union DataType\")\n        }\n    }\n\n    /// Creates a new empty [`UnionArray`].\n    pub fn new_empty(dtype: ArrowDataType) -> Self {\n        if let ArrowDataType::Union(u) = dtype.to_storage() {\n            let fields = u\n                .fields\n                .iter()\n                .map(|x| new_empty_array(x.dtype().clone()))\n                .collect();\n\n            let offsets = if u.mode.is_sparse() {\n                None\n            } else {\n                Some(Buffer::default())\n            };\n","sourceCodeStart":174,"sourceCodeEnd":210,"githubUrl":"https://github.com/pola-rs/polars/blob/9b5d73fd00236295624374b075d16b1fe6ec6df9/crates/polars-arrow/src/array/union/mod.rs#L174-L210","documentation":"UnionArray::new_null builds a null union only from ArrowDataType::Union(..): it materializes one null child per field, sets types = 0 for every row and, for dense mode, offsets 0..length. Any other dtype panics with this message.","triggerScenarios":"UnionArray::new_null(dtype, n) where dtype is not the Union variant — e.g. a Struct dtype, or a union column represented by a different variant after a schema round-trip.","commonSituations":"Generic null-column factories whose match forgot the union arm; IPC/readers constructing null padding rows for union columns for the first time.","solutions":["Use polars_arrow::array::new_null_array(dtype, length), which handles Union correctly","Ensure the dtype passed is literally ArrowDataType::Union(fields, mode) before calling the concrete constructor","Add a Union arm to generic builders so the concrete constructor is never reached with a foreign dtype"],"exampleFix":"// before\nlet nulls = UnionArray::new_null(dtype, len);\n\n// after\nlet nulls = polars_arrow::array::new_null_array(dtype, len);","handlingStrategy":"type-guard","validationCode":"fn union_only(dtype: &ArrowDataType) -> Result<&ArrowDataType, String> {\n    match dtype.to_storage() {\n        ArrowDataType::Union(_) => Ok(dtype),\n        other => Err(format!(\"expected Union dtype, got {other:?}\")),\n    }\n}","typeGuard":"fn is_union_dtype(dtype: &ArrowDataType) -> bool {\n    matches!(dtype.to_storage(), ArrowDataType::Union(_))\n}","tryCatchPattern":null,"preventionTips":["Route null-column creation through polars_arrow::array::new_null_array","Add a Union arm to any dtype dispatch table that constructs arrays","Test null/empty construction for every dtype your schemas can contain"],"tags":["rust","polars","arrow","union","null","dtype"],"backgroundTag":"dtype-mismatch","analyzedSha":"9b5d73fd00236295624374b075d16b1fe6ec6df9","analyzedAt":"2026-08-19T12:15:06.350Z","contentChangedAt":"2026-08-19T12:15:06.350Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}