sundy-li commented on code in PR #4585: URL: https://github.com/apache/arrow-rs/pull/4585#discussion_r1498570645
########## arrow-data/src/view.rs: ########## @@ -0,0 +1,192 @@ +// 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. + +//! View array utilities + +use arrow_buffer::Buffer; +use arrow_schema::ArrowError; + +/// The element layout of a view buffer +/// +/// See [`DataType::Utf8View`](arrow_schema::DataType) +pub struct View { + /// The length of the string + pub length: u32, + /// The first 4 bytes of string data + pub prefix: u32, + /// The buffer index + pub buffer_index: u32, + /// The offset into the buffer + pub offset: u32, +} + +impl From<u128> for View { + #[inline] + fn from(value: u128) -> Self { + Self { + length: value as u32, + prefix: (value >> 32) as u32, + buffer_index: (value >> 64) as u32, + offset: (value >> 96) as u32, + } + } +} + +impl From<View> for u128 { + #[inline] + fn from(value: View) -> Self { + (value.length as u128) + | ((value.prefix as u128) << 32) + | ((value.buffer_index as u128) << 64) + | ((value.offset as u128) << 96) + } +} + +/// Validates the combination of `views` and `buffers` is a valid BinaryView +pub fn validate_binary_view( + views: &[u128], + buffers: &[Buffer], +) -> Result<(), ArrowError> { + validate_view_impl(views, buffers, |_, _| Ok(())) +} + +/// Validates the combination of `views` and `buffers` is a valid StringView +pub fn validate_string_view( + views: &[u128], + buffers: &[Buffer], +) -> Result<(), ArrowError> { + validate_view_impl(views, buffers, |idx, b| { + std::str::from_utf8(b).map_err(|e| { Review Comment: `simdutf8` crate may be better than this. -- 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]
