# 21.5 Nullable Enum Fix

## Problem

Nullable enum types (union with null, e.g., `["null", {"type": "enum", ...}]`) caused a polars-core panic with "not implemented" when trying to apply a null mask to Categorical/Enum types.

The error occurred in `NullableBuilder::finish()` which used `zip_with` to apply the validity mask:

```rust
let result = inner_series
    .zip_with(&mask, &null_series)
    .map_err(|e| DecodeError::InvalidData(format!("Failed to apply null mask: {}", e)))?;
```

This approach works for most types but fails for Polars Enum/Categorical types because `zip_with` is not implemented for them.

## Solution

Added a specialized `finish_with_validity` method to `EnumBuilder` that constructs the nullable categorical array directly using `Option<T>` indices, bypassing the need for `zip_with`.

### Key Changes

1. **Added `EnumBuilder::finish_with_validity()`** in `src/reader/record_decoder.rs`:
   - Takes a validity mask (`&[bool]`) as input
   - Zips indices with validity to create `Vec<Option<u8/u16/u32>>`
   - Uses `ChunkedArray::from_iter()` which properly handles `Option<T>` values
   - Creates the categorical with proper null handling

2. **Updated `NullableBuilder::finish()`** to use the specialized method for Enum types:
   ```rust
   if let FieldBuilder::Enum(b) = &mut *self.inner {
       return b.finish_with_validity(&validity);
   }
   ```

### Why This Works

Polars `ChunkedArray` implements `FromIterator<Option<T::Native>>`, which properly handles null values during array construction. By creating the physical array (UInt8/UInt16/UInt32) with nulls already embedded, we avoid the need for post-hoc null mask application via `zip_with`.

This is the same pattern used for `BinaryBuilder` and `StringBuilder` which also have specialized `finish_with_validity` methods.

## Testing

- Removed `@pytest.mark.xfail` from `test_enum_nullable` in `python/tests/types/test_enum_fixed.py`
- All 25 enum/fixed tests pass
- All 402 Rust tests pass
- All 366 Python tests pass (5 xfailed for other issues)

## Related

- Similar pattern to `BinaryBuilder::finish_with_validity()` and `StringBuilder::finish_with_validity()`
- See `devnotes/22.1-enum-builder.md` for general enum implementation details
