{"record":{"id":"32af88d92d24fed5","repo":"pola-rs/polars","slug":"type-does-not-support-logical-type","errorCode":null,"errorMessage":"Type {} does not support logical type {:?}","messagePattern":"Type (.+?) does not support logical type (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/polars-arrow/src/scalar/primitive.rs","lineNumber":18,"sourceCode":"use super::Scalar;\nuse crate::datatypes::ArrowDataType;\nuse crate::types::NativeType;\n\n/// The implementation of [`Scalar`] for primitive, semantically equivalent to [`Option<T>`]\n/// with [`ArrowDataType`].\n#[derive(Debug, Clone, PartialEq, Eq)]\npub struct PrimitiveScalar<T: NativeType> {\n    value: Option<T>,\n    dtype: ArrowDataType,\n}\n\nimpl<T: NativeType> PrimitiveScalar<T> {\n    /// Returns a new [`PrimitiveScalar`].\n    #[inline]\n    pub fn new(dtype: ArrowDataType, value: Option<T>) -> Self {\n        if !dtype.to_physical_type().eq_primitive(T::PRIMITIVE) {\n            panic!(\n                \"Type {} does not support logical type {:?}\",\n                std::any::type_name::<T>(),\n                dtype\n            )\n        }\n        Self { value, dtype }\n    }\n\n    /// Returns the optional value.\n    #[inline]\n    pub fn value(&self) -> &Option<T> {\n        &self.value\n    }\n\n    /// Returns a new `PrimitiveScalar` with the same value but different [`ArrowDataType`]\n    /// # Panic\n    /// This function panics if the `dtype` is not valid for self's physical type `T`.\n    pub fn to(self, dtype: ArrowDataType) -> Self {","sourceCodeStart":1,"sourceCodeEnd":36,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/crates/polars-arrow/src/scalar/primitive.rs#L1-L36","documentation":"`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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Match the native type to the dtype's physical type before constructing: use `dtype.to_physical_type()` to select the correct `T`.","Use a dtype constructor (e.g. `ArrowDataType::Int32` for i32 values) so logical and physical types agree.","Validate the pairing in a debug branch or with `debug_assert!` before calling `new` in generic code."],"exampleFix":"// before\nlet s = PrimitiveScalar::<i32>::new(ArrowDataType::Int64, Some(5)); // panics\n// after\nlet s = PrimitiveScalar::<i64>::new(ArrowDataType::Int64, Some(5));","handlingStrategy":"validation","validationCode":"fn primitive_matches<T: NativeType>(dtype: &ArrowDataType) -> bool {\n    dtype.to_physical_type().eq_primitive(T::PRIMITIVE)\n}","typeGuard":"fn as_i32_scalar(dtype: ArrowDataType, v: i32) -> Option<PrimitiveScalar<i32>> {\n    (dtype.to_physical_type().eq_primitive(i32::PRIMITIVE))\n        .then(|| PrimitiveScalar::new(dtype, Some(v)))\n}","tryCatchPattern":null,"preventionTips":["Always derive the native type from `dtype.to_physical_type()` in generic builders","Avoid hard-coding numeric types when constructing PrimitiveScalars","Add a debug_assert! with `eq_primitive` in helper constructors"],"tags":["arrow","rust","type-mismatch","dtype"],"backgroundTag":"type-mismatch","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}