{"record":{"id":"8e9c0b672308657d","repo":"pola-rs/polars","slug":"could-not-unwrap-required-zipvalidity-iterato","errorCode":null,"errorMessage":"Could not 'unwrap_required'. 'ZipValidity' iterator has nulls.","messagePattern":"Could not 'unwrap_required'\\. 'ZipValidity' iterator has nulls\\.","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/polars-arrow/src/bitmap/utils/zip_validity.rs","lineNumber":206,"sourceCode":"}\n\nunsafe impl<T, I, V> TrustedLen for ZipValidity<T, I, V>\nwhere\n    I: TrustedLen<Item = T>,\n    V: TrustedLen<Item = bool>,\n{\n}\n\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":188,"sourceCodeEnd":218,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/crates/polars-arrow/src/bitmap/utils/zip_validity.rs#L188-L218","documentation":"A `ZipValidity` iterator wraps a base iterator together with an optional validity bitmap; it exists in `Required` variant only when it is provably free of nulls. `unwrap_required` is a contract-based accessor: calling it on a ZipValidity that still has a validity bitmap (or mixed state) means the caller's 'no nulls' assumption is false, so it panics.","triggerScenarios":"Calling `.unwrap_required()` on a `ZipValidityIter` obtained from an array whose validity is `Some(_)` (or whose null-count was not proven zero) — e.g. after `with_validity(Some(...))`, sliced arrays retaining a bitmap, or values that logically contain nulls.","commonSituations":"Optimized read paths that assume a column has no nulls (e.g. after a filter or a non-nullable schema promise) but receive a validity bitmap anyway; parquet/IPC files where a column declared non-nullable still carries a validity buffer; upstream Polars code changes making a previously-`Required` iterator `Optional`.","solutions":["Check `array.null_count() == 0` (or drop the bitmap with `with_validity(None)`) before choosing the `unwrap_required` path.","Use `unwrap_optional()` and handle `Option<T>` items when nulls are possible.","Ensure producers don't attach an all-true validity bitmap to arrays known to have no nulls.","Restructure the code to branch on nullability at the array level rather than panicking in the iterator adapter."],"exampleFix":"// before\nlet iter = arr.iter().unwrap_required();\n// after\nlet iter = if arr.null_count() == 0 {\n    arr.iter().unwrap_required()\n} else {\n    arr.iter().unwrap_optional().map(|v| v.expect(\"null\"))\n};","handlingStrategy":"validation","validationCode":"fn can_unwrap_required<T, I: Iterator, V: Iterator<Item = bool>>(z: &ZipValidity<T, I, V>) -> bool {\n    matches!(z, ZipValidity::Required(_))\n}\n// at the array level:\nlet safe = arr.null_count() == 0;","typeGuard":"fn as_required<'a, T, I: Iterator<Item = T>, V: Iterator<Item = bool>>(\n    z: &'a mut ZipValidity<T, I, V>,\n) -> Option<&'a mut I> {\n    match z { ZipValidity::Required(i) => Some(i), _ => None }\n}","tryCatchPattern":null,"preventionTips":["Check `null_count() == 0` before taking the required fast path","Default to `unwrap_optional` and handle Options unless null-freedom is proven","Never attach an all-true validity bitmap to known-null-free arrays"],"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"}