pola-rs/polars · error

Type does not support logical type

Error message

Type {} does not support logical type {:?}

What it means

`PrimitiveScalar::new` validates that the physical type of the given `ArrowDataType` matches the `NativeType` `T` the scalar is being built with. If `T::PRIMITIVE` does not equal the dtype's physical primitive, it panics with "Type {} does not support logical type {:?}". This prevents constructing a scalar whose Rust value type is incompatible with the declared Arrow logical type.

Solutions

  1. Match the native type to the dtype's physical type before constructing: use `dtype.to_physical_type()` to select the correct `T`.
  2. Use a dtype constructor (e.g. `ArrowDataType::Int32` for i32 values) so logical and physical types agree.
  3. Validate the pairing in a debug branch or with `debug_assert!` before calling `new` in generic code.

Example fix

// before
let s = PrimitiveScalar::<i32>::new(ArrowDataType::Int64, Some(5)); // panics
// after
let s = PrimitiveScalar::<i64>::new(ArrowDataType::Int64, Some(5));
Defensive patterns

Strategy: validation

Validate before calling

fn primitive_matches<T: NativeType>(dtype: &ArrowDataType) -> bool {
    dtype.to_physical_type().eq_primitive(T::PRIMITIVE)
}

Type guard

fn as_i32_scalar(dtype: ArrowDataType, v: i32) -> Option<PrimitiveScalar<i32>> {
    (dtype.to_physical_type().eq_primitive(i32::PRIMITIVE))
        .then(|| PrimitiveScalar::new(dtype, Some(v)))
}

Prevention

When it happens

Trigger: Calling `PrimitiveScalar::<T>::new(dtype, value)` where `dtype.to_physical_type()` is not the same primitive as `T::PRIMITIVE` — e.g. constructing a `PrimitiveScalar::<i32>` with a Date64 (i64 physical) dtype.

Common situations: Generic code mapping logical dtypes to native types incorrectly; hard-coding i32/i64 when building test scalars; dtype changes after refactoring that no longer match the value type.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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

Appendix: source

Thrown at crates/polars-arrow/src/scalar/primitive.rs:18

use super::Scalar;
use crate::datatypes::ArrowDataType;
use crate::types::NativeType;

/// The implementation of [`Scalar`] for primitive, semantically equivalent to [`Option<T>`]
/// with [`ArrowDataType`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PrimitiveScalar<T: NativeType> {
    value: Option<T>,
    dtype: ArrowDataType,
}

impl<T: NativeType> PrimitiveScalar<T> {
    /// Returns a new [`PrimitiveScalar`].
    #[inline]
    pub fn new(dtype: ArrowDataType, value: Option<T>) -> Self {
        if !dtype.to_physical_type().eq_primitive(T::PRIMITIVE) {
            panic!(
                "Type {} does not support logical type {:?}",
                std::any::type_name::<T>(),
                dtype
            )
        }
        Self { value, dtype }
    }

    /// Returns the optional value.
    #[inline]
    pub fn value(&self) -> &Option<T> {
        &self.value
    }

    /// Returns a new `PrimitiveScalar` with the same value but different [`ArrowDataType`]
    /// # Panic
    /// This function panics if the `dtype` is not valid for self's physical type `T`.
    pub fn to(self, dtype: ArrowDataType) -> Self {

View on GitHub (pinned to fe841f959e)