pola-rs/polars · error

Given array is not a DictionaryArray

Error message

Given array is not a DictionaryArray

What it means

encode_dictionary downcasts the given array to DictionaryArray and panics if the downcast fails. It fires when the dictionary-tracking logic is handed an array whose physical type is not a DictionaryArray — typically a nested array child passed by mistake during IPC dictionary encoding.

Solutions

  1. Ensure only DictionaryArray columns are routed to encode_dictionary
  2. Check the schema-to-dict-id mapping so nested fields resolve to the correct dictionary arrays
  3. Guard the downcast and return a PolarsError instead of panicking
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at crates/polars-arrow/src/io/ipc/write/common.rs:156 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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

Appendix: source

Thrown at crates/polars-arrow/src/io/ipc/write/common.rs:156

            let values = array.as_any().downcast_ref::<MapArray>().unwrap().field();
            let field = field.fields.first().ok_or_else(|| polars_err!(ComputeError: "Invalid IPC field structure: expected nested field but fields vector is empty"))?;
            dictionaries_to_encode(field, values.as_ref(), dictionary_tracker, dicts_to_encode)
        },
    }
}

/// Encode a dictionary array with a certain id.
///
/// # Panics
///
/// This will panic if the given array is not a [`DictionaryArray`].
pub fn encode_dictionary(
    dict_id: i64,
    array: &dyn Array,
    options: &WriteOptions,
) -> PolarsResult<EncodedData> {
    let PhysicalType::Dictionary(key_type) = array.dtype().to_physical_type() else {
        panic!("Given array is not a DictionaryArray")
    };

    match_integer_type!(key_type, |$T| {
        let array: &DictionaryArray<$T> = array.as_any().downcast_ref().unwrap();

        encode_dictionary_values(dict_id, array.values().as_ref(), options)
    })
}

pub fn encode_new_dictionaries(
    field: &IpcField,
    array: &dyn Array,
    options: &WriteOptions,
    dictionary_tracker: &mut DictionaryTracker,
    encoded_dictionaries: &mut Vec<EncodedData>,
) -> PolarsResult<()> {
    let mut dicts_to_encode = Vec::new();
    dictionaries_to_encode(field, array, dictionary_tracker, &mut dicts_to_encode)?;

View on GitHub (pinned to fe841f959e)