{"record":{"id":"3cac6a99b389b75c","repo":"pola-rs/polars","slug":"could-not-unwrap-optional-zipvalidity-iterato","errorCode":null,"errorMessage":"Could not 'unwrap_optional'. 'ZipValidity' iterator has no nulls.","messagePattern":"Could not 'unwrap_optional'\\. 'ZipValidity' iterator has no nulls\\.","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/polars-arrow/src/bitmap/utils/zip_validity.rs","lineNumber":214,"sourceCode":"\nimpl<T, I, V> ZipValidity<T, I, V>\nwhere\n    I: Iterator<Item = T>,\n    V: Iterator<Item = bool>,\n{\n    /// Unwrap into an iterator that has no null values.\n    pub fn unwrap_required(self) -> I {\n        match self {\n            ZipValidity::Required(i) => i,\n            _ => panic!(\"Could not 'unwrap_required'. 'ZipValidity' iterator has nulls.\"),\n        }\n    }\n\n    /// Unwrap into an iterator that has null values.\n    pub fn unwrap_optional(self) -> ZipValidityIter<T, I, V> {\n        match self {\n            ZipValidity::Optional(i) => i,\n            _ => panic!(\"Could not 'unwrap_optional'. 'ZipValidity' iterator has no nulls.\"),\n        }\n    }\n}\n","sourceCodeStart":196,"sourceCodeEnd":218,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/crates/polars-arrow/src/bitmap/utils/zip_validity.rs#L196-L218","documentation":"`ZipValidity::unwrap_optional` returns the null-aware iterator only when the ZipValidity actually has an attached validity bitmap (`Optional` variant). If the underlying array has no nulls the iterator is `Required` and there is no `Option` layer to unwrap, so the accessor panics to surface the broken assumption.","triggerScenarios":"Calling `.unwrap_optional()` on a `ZipValidityIter` built from an array with `validity == None` / `null_count() == 0` — e.g. reading a fully non-null column from parquet and then running a null-handling code path.","commonSituations":"Null-handling pipelines that always assume optionality but receive columns stripped of their validity bitmap (optimized storage, non-nullable schema declarations); tests constructing arrays without validity then exercising optional paths; refactors where arrays that used to carry nulls no longer do.","solutions":["Branch on `array.null_count()` / validity presence and call `unwrap_required()` when there are no nulls.","Force the optional path by keeping the validity bitmap (don't strip it with `with_validity(None)` prematurely).","Handle both cases generically instead of unwrapping: match on the ZipValidity variants.","If nulls are semantically possible, ensure producers retain `Some(validity)` even when all values are valid."],"exampleFix":"// before\nlet iter = arr.iter().unwrap_optional();\n// after\nlet iter = if arr.null_count() > 0 {\n    arr.iter().unwrap_optional()\n} else {\n    arr.iter().unwrap_required().map(Some)\n};","handlingStrategy":"validation","validationCode":"fn can_unwrap_optional<T, I: Iterator, V: Iterator<Item = bool>>(z: &ZipValidity<T, I, V>) -> bool {\n    matches!(z, ZipValidity::Optional(_))\n}\n// at the array level:\nlet has_nulls = arr.null_count() > 0 || arr.validity().is_some();","typeGuard":"fn as_optional<'a, T, I: Iterator<Item = T>, V: Iterator<Item = bool>>(\n    z: &'a mut ZipValidity<T, I, V>,\n) -> Option<&'a mut ZipValidityIter<T, I, V>> {\n    match z { ZipValidity::Optional(i) => Some(i), _ => None }\n}","tryCatchPattern":null,"preventionTips":["Branch on `null_count()` before choosing required vs optional iteration","Retain the validity bitmap through transformations if downstream expects Option items","Write both null-free and nullable test cases for any iterator-consuming code"],"tags":["rust","arrow","iterator","nullability","panic"],"backgroundTag":"null-argument","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}