{"record":{"id":"1cb2e7e1e921d7bc","repo":"pola-rs/polars","slug":"the-length-of-the-values-must-be-equal-to-the-last","errorCode":null,"errorMessage":"The length of the values must be equal to the last offset value","messagePattern":"The length of the values must be equal to the last offset value","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/polars-arrow/src/array/utf8/mutable_values.rs","lineNumber":110,"sourceCode":"    ///\n    /// # Panic\n    /// This function does not panic iff:\n    /// * `offsets.last()` is greater than `values.len()`\n    /// * The `dtype`'s [`crate::datatypes::PhysicalType`] is equal to either `Utf8` or `LargeUtf8`.\n    ///\n    /// # Safety\n    /// This function is safe iff:\n    /// * the offsets are monotonically increasing\n    /// * The `values` between two consecutive `offsets` are not valid utf8\n    /// # Implementation\n    /// This function is `O(1)`\n    pub unsafe fn new_unchecked(\n        dtype: ArrowDataType,\n        offsets: Offsets<O>,\n        values: Vec<u8>,\n    ) -> Self {\n        try_check_offsets_bounds(&offsets, values.len())\n            .expect(\"The length of the values must be equal to the last offset value\");\n\n        if dtype.to_physical_type() != Self::default_dtype().to_physical_type() {\n            panic!(\n                \"MutableUtf8ValuesArray can only be initialized with DataType::Utf8 or DataType::LargeUtf8\"\n            )\n        }\n\n        Self {\n            dtype,\n            offsets,\n            values,\n        }\n    }\n\n    /// Returns the default [`ArrowDataType`] of this container: [`ArrowDataType::Utf8`] or [`ArrowDataType::LargeUtf8`]\n    /// depending on the generic [`Offset`].\n    pub fn default_dtype() -> ArrowDataType {\n        Utf8Array::<O>::default_dtype()","sourceCodeStart":92,"sourceCodeEnd":128,"githubUrl":"https://github.com/pola-rs/polars/blob/9b5d73fd00236295624374b075d16b1fe6ec6df9/crates/polars-arrow/src/array/utf8/mutable_values.rs#L92-L128","documentation":"MutableUtf8ValuesArray::new_unchecked validates (despite the name) that the offsets buffer ends exactly at values.len() via try_check_offsets_bounds; a last offset smaller or larger than the values length returns Err and the .expect turns it into this panic.","triggerScenarios":"Constructing new_unchecked with offsets and values buffers from different chunk lengths: last offset < values.len() (trailing garbage bytes) or > values.len() (truncated values buffer); slicing the values buffer without rebasing the final offset.","commonSituations":"FFI/imported buffers from arrow-rs or IPC data; hand-rolled concatenation that copies offsets from one array and values from another; off-by-one when dropping a prefix of the values bytes.","solutions":["Assert offsets.last_offset() == values.len() before constructing, and truncate or extend values to match","Rebuild the array by pushing each slice (push keeps offsets and values consistent)","When slicing, keep the offsets relative and resize the values buffer to offsets.last_offset()"],"exampleFix":"// before\nlet arr = unsafe { MutableUtf8ValuesArray::new_unchecked(dtype, offsets, values) };\n\n// after: make the values buffer end exactly at the last offset\nlet last = offsets.last_offset();\nlet values = values[..last.min(values.len())].to_vec();\n// pad if values.len() < last instead of truncating, then:\nlet arr = unsafe { MutableUtf8ValuesArray::new_unchecked(dtype, offsets, values) };","handlingStrategy":"validation","validationCode":"fn offsets_match<O: polars_arrow::offset::Offset>(offsets: &polars_arrow::offset::Offsets<O>, values: &[u8]) -> bool {\n    offsets.last_offset() == values.len()\n}\n// assert!(offsets_match(&offsets, &values)); before new_unchecked","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pair offsets and values from the same source chunk","Rebase the final offset after slicing the values buffer","Prefer the push() API over manual buffer stitching for imported data"],"tags":["rust","polars","arrow","utf8","offsets"],"backgroundTag":"offsets-length-mismatch","analyzedSha":"9b5d73fd00236295624374b075d16b1fe6ec6df9","analyzedAt":"2026-08-19T12:15:06.350Z","contentChangedAt":"2026-08-19T12:15:06.350Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}