pola-rs/polars · error

Could not 'unwrap_optional'. 'ZipValidity' iterator has no…

Error message

Could not 'unwrap_optional'. 'ZipValidity' iterator has no nulls.

What it means

`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.

Solutions

  1. Branch on `array.null_count()` / validity presence and call `unwrap_required()` when there are no nulls.
  2. Force the optional path by keeping the validity bitmap (don't strip it with `with_validity(None)` prematurely).
  3. Handle both cases generically instead of unwrapping: match on the ZipValidity variants.
  4. If nulls are semantically possible, ensure producers retain `Some(validity)` even when all values are valid.

Example fix

// before
let iter = arr.iter().unwrap_optional();
// after
let iter = if arr.null_count() > 0 {
    arr.iter().unwrap_optional()
} else {
    arr.iter().unwrap_required().map(Some)
};
Defensive patterns

Strategy: validation

Validate before calling

fn can_unwrap_optional<T, I: Iterator, V: Iterator<Item = bool>>(z: &ZipValidity<T, I, V>) -> bool {
    matches!(z, ZipValidity::Optional(_))
}
// at the array level:
let has_nulls = arr.null_count() > 0 || arr.validity().is_some();

Type guard

fn as_optional<'a, T, I: Iterator<Item = T>, V: Iterator<Item = bool>>(
    z: &'a mut ZipValidity<T, I, V>,
) -> Option<&'a mut ZipValidityIter<T, I, V>> {
    match z { ZipValidity::Optional(i) => Some(i), _ => None }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/3cac6a99b389b75c. Report an issue: GitHub.

Appendix: source

Thrown at crates/polars-arrow/src/bitmap/utils/zip_validity.rs:214

impl<T, I, V> ZipValidity<T, I, V>
where
    I: Iterator<Item = T>,
    V: Iterator<Item = bool>,
{
    /// Unwrap into an iterator that has no null values.
    pub fn unwrap_required(self) -> I {
        match self {
            ZipValidity::Required(i) => i,
            _ => panic!("Could not 'unwrap_required'. 'ZipValidity' iterator has nulls."),
        }
    }

    /// Unwrap into an iterator that has null values.
    pub fn unwrap_optional(self) -> ZipValidityIter<T, I, V> {
        match self {
            ZipValidity::Optional(i) => i,
            _ => panic!("Could not 'unwrap_optional'. 'ZipValidity' iterator has no nulls."),
        }
    }
}

View on GitHub (pinned to fe841f959e)