ariesdevil commented on code in PR #5557: URL: https://github.com/apache/arrow-rs/pull/5557#discussion_r1559092055
########## parquet/src/arrow/buffer/view_buffer.rs: ########## @@ -0,0 +1,146 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use crate::arrow::buffer::bit_util::iter_set_bits_rev; +use crate::arrow::record_reader::buffer::ValuesBuffer; +use crate::errors::{ParquetError, Result}; +use arrow_array::{make_array, ArrayRef}; +use arrow_buffer::{ArrowNativeType, Buffer, NullBuffer}; +use arrow_data::{ArrayDataBuilder, ByteView}; +use arrow_schema::DataType as ArrowType; + +/// A buffer of variable-sized byte arrays that can be converted into +/// a corresponding [`ArrayRef`] +#[derive(Debug, Default)] +pub struct ViewBuffer { + pub views: Vec<u128>, + pub buffers: Vec<u8>, + pub nulls: Option<NullBuffer>, +} + +impl ViewBuffer { + /// Returns the number of byte arrays in this buffer + pub fn len(&self) -> usize { + self.views.len() + } + + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + pub fn try_push(&mut self, data: &[u8], validate_utf8: bool) -> Result<()> { + if validate_utf8 { + if let Some(&b) = data.first() { + // A valid code-point iff it does not start with 0b10xxxxxx + // Bit-magic taken from `std::str::is_char_boundary` + if (b as i8) < -0x40 { + return Err(ParquetError::General( + "encountered non UTF-8 data".to_string(), + )); + } + } + } + let length: u32 = data.len().try_into().unwrap(); + if length <= 12 { + let mut view_buffer = [0; 16]; + view_buffer[0..4].copy_from_slice(&length.to_le_bytes()); + view_buffer[4..4 + length as usize].copy_from_slice(data); + self.views.push(u128::from_le_bytes(view_buffer)); + return Ok(()); + } + + let offset = self.buffers.len() as u32; + self.buffers.extend_from_slice(data); + + let view = ByteView { + length, + prefix: u32::from_le_bytes(data[0..4].try_into().unwrap()), + buffer_index: 0, + offset, + }; + self.views.push(view.into()); + Ok(()) + } + + /// Extends this buffer with a list of keys + pub fn extend_from_dictionary<K: ArrowNativeType>( + &mut self, + keys: &[K], + dict_views: &[u128], + dict_buffers: &[u8], + ) -> Result<()> { + for key in keys { + let index = key.as_usize(); + if index >= dict_views.len() { + return Err(general_err!( + "dictionary key beyond bounds of dictionary: 0..{}", + dict_views.len() + )); + } + + let view = dict_views[index]; + let len = view as u32; + + // Dictionary values are verified when decoding dictionary page, so validate_utf8 is false here. + if len <= 12 { + self.try_push(&view.to_le_bytes()[4..4 + len as usize], false)?; + } else { + let offset = (view >> 96) as usize; + self.try_push(&dict_buffers[offset..offset + len as usize], false)? + }; + } + Ok(()) + } + + /// Converts this into an [`ArrayRef`] with the provided `data_type` and `null_buffer` + pub fn into_array(self, null_buffer: Option<Buffer>, data_type: ArrowType) -> ArrayRef { Review Comment: > I think this is an unsafe API and should be marked as such -- for example it would allow creating a StringViewArray from non utf8 data if `try_push` is called without `validate_utf8 =true`↳ > > Alternately, perhaps `validate_utf8` could be a member of this struct and then this call can simply validate that if we are building a `StringViewArray` that `self.validate_utf8` was set↳ This uses the behavior of `offset_buffer.rs` too. > Why does this also need to get a `null_buffer` passed in when there is also a field `self.null_buffer`?↳ Fixed -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
