pola-rs/polars · error
Could not 'unwrap_required'. 'ZipValidity' iterator has…
Error message
Could not 'unwrap_required'. 'ZipValidity' iterator has nulls.
What it means
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.
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.
Example fix
// before
let iter = arr.iter().unwrap_required();
// after
let iter = if arr.null_count() == 0 {
arr.iter().unwrap_required()
} else {
arr.iter().unwrap_optional().map(|v| v.expect("null"))
}; Defensive patterns
Strategy: validation
Validate before calling
fn can_unwrap_required<T, I: Iterator, V: Iterator<Item = bool>>(z: &ZipValidity<T, I, V>) -> bool {
matches!(z, ZipValidity::Required(_))
}
// at the array level:
let safe = arr.null_count() == 0; Type guard
fn as_required<'a, T, I: Iterator<Item = T>, V: Iterator<Item = bool>>(
z: &'a mut ZipValidity<T, I, V>,
) -> Option<&'a mut I> {
match z { ZipValidity::Required(i) => Some(i), _ => None }
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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`.
Related errors
- Could not 'unwrap_optional'. 'ZipValidity' iterator has no…
- internal error: entered unreachable code
- Length to be non-zero
- length to fit in `usize`
- MutableUtf8ValuesArray can only be initialized with…
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/8e9c0b672308657d.
Report an issue: GitHub.
Appendix: source
Thrown at crates/polars-arrow/src/bitmap/utils/zip_validity.rs:206
}
unsafe impl<T, I, V> TrustedLen for ZipValidity<T, I, V>
where
I: TrustedLen<Item = T>,
V: TrustedLen<Item = bool>,
{
}
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)