parquet_variant/
utils.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17use std::{array::TryFromSliceError, ops::Range, str};
18
19use crate::VariantPathElement;
20use arrow_schema::ArrowError;
21
22use std::cmp::Ordering;
23use std::fmt::Debug;
24use std::slice::SliceIndex;
25
26/// Helper for reporting integer overflow errors in a consistent way.
27pub(crate) fn overflow_error(msg: &str) -> ArrowError {
28    ArrowError::InvalidArgumentError(format!("Integer overflow computing {msg}"))
29}
30
31#[inline]
32pub(crate) fn slice_from_slice<I: SliceIndex<[u8]> + Clone + Debug>(
33    bytes: &[u8],
34    index: I,
35) -> Result<&I::Output, ArrowError> {
36    bytes.get(index.clone()).ok_or_else(|| {
37        ArrowError::InvalidArgumentError(format!(
38            "Tried to extract byte(s) {index:?} from {}-byte buffer",
39            bytes.len(),
40        ))
41    })
42}
43
44/// Helper to safely slice bytes with offset calculations.
45///
46/// Equivalent to `slice_from_slice(bytes, (base_offset + range.start)..(base_offset + range.end))`
47/// but using checked addition to prevent integer overflow panics on 32-bit systems.
48#[inline]
49pub(crate) fn slice_from_slice_at_offset(
50    bytes: &[u8],
51    base_offset: usize,
52    range: Range<usize>,
53) -> Result<&[u8], ArrowError> {
54    let start_byte = base_offset
55        .checked_add(range.start)
56        .ok_or_else(|| overflow_error("slice start"))?;
57    let end_byte = base_offset
58        .checked_add(range.end)
59        .ok_or_else(|| overflow_error("slice end"))?;
60    slice_from_slice(bytes, start_byte..end_byte)
61}
62
63pub(crate) fn array_from_slice<const N: usize>(
64    bytes: &[u8],
65    offset: usize,
66) -> Result<[u8; N], ArrowError> {
67    slice_from_slice_at_offset(bytes, offset, 0..N)?
68        .try_into()
69        .map_err(|e: TryFromSliceError| ArrowError::InvalidArgumentError(e.to_string()))
70}
71
72pub(crate) fn first_byte_from_slice(slice: &[u8]) -> Result<u8, ArrowError> {
73    slice
74        .first()
75        .copied()
76        .ok_or_else(|| ArrowError::InvalidArgumentError("Received empty bytes".to_string()))
77}
78
79/// Helper to get a &str from a slice at the given offset and range, or an error if it contains invalid UTF-8 data.
80#[inline]
81pub(crate) fn string_from_slice(
82    slice: &[u8],
83    offset: usize,
84    range: Range<usize>,
85) -> Result<&str, ArrowError> {
86    let offset_buffer = slice_from_slice_at_offset(slice, offset, range)?;
87
88    //Use simdutf8 by default
89    #[cfg(feature = "simdutf8")]
90    {
91        simdutf8::basic::from_utf8(offset_buffer).map_err(|_| {
92            // Use simdutf8::compat to return details about the decoding error
93            let e = simdutf8::compat::from_utf8(offset_buffer).unwrap_err();
94            ArrowError::InvalidArgumentError(format!("encountered non UTF-8 data: {e}"))
95        })
96    }
97
98    //Use std::str if simdutf8 is not enabled
99    #[cfg(not(feature = "simdutf8"))]
100    str::from_utf8(offset_buffer)
101        .map_err(|_| ArrowError::InvalidArgumentError("invalid UTF-8 string".to_string()))
102}
103
104/// Performs a binary search over a range using a fallible key extraction function; a failed key
105/// extraction immediately terminats the search.
106///
107/// This is similar to the standard library's `binary_search_by`, but generalized to ranges instead
108/// of slices.
109///
110/// # Arguments
111/// * `range` - The range to search in
112/// * `target` - The target value to search for
113/// * `key_extractor` - A function that extracts a comparable key from slice elements.
114///   This function can fail and return None.
115///
116/// # Returns
117/// * `Some(Ok(index))` - Element found at the given index
118/// * `Some(Err(index))` - Element not found, but would be inserted at the given index
119/// * `None` - Key extraction failed
120pub(crate) fn try_binary_search_range_by<F>(
121    range: Range<usize>,
122    cmp: F,
123) -> Option<Result<usize, usize>>
124where
125    F: Fn(usize) -> Option<Ordering>,
126{
127    let Range { mut start, mut end } = range;
128    while start < end {
129        let mid = start + (end - start) / 2;
130        match cmp(mid)? {
131            Ordering::Equal => return Some(Ok(mid)),
132            Ordering::Greater => end = mid,
133            Ordering::Less => start = mid + 1,
134        }
135    }
136
137    Some(Err(start))
138}
139
140/// Verifies the expected size of type T, for a type that should only grow if absolutely necessary.
141#[allow(unused)]
142pub(crate) const fn expect_size_of<T>(expected: usize) {
143    let size = std::mem::size_of::<T>();
144    if size != expected {
145        let _ = [""; 0][size];
146    }
147}
148
149pub(crate) fn fits_precision<const N: u32>(n: impl Into<i64>) -> bool {
150    n.into().unsigned_abs().leading_zeros() >= (i64::BITS - N)
151}
152
153// Helper fn to parse input segments like foo[0] or foo[0][0]
154#[inline]
155pub(crate) fn parse_path<'a>(segment: &'a str) -> Vec<VariantPathElement<'a>> {
156    if segment.is_empty() {
157        return Vec::new();
158    }
159
160    let mut path_elements = Vec::new();
161    let mut base = segment;
162
163    while let Some(stripped) = base.strip_suffix(']') {
164        let Some(open_pos) = stripped.rfind('[') else {
165            return vec![VariantPathElement::field(segment)];
166        };
167
168        let index_str = &stripped[open_pos + 1..];
169        let Ok(index) = index_str.parse::<usize>() else {
170            return vec![VariantPathElement::field(segment)];
171        };
172
173        path_elements.push(VariantPathElement::index(index));
174        base = &stripped[..open_pos];
175    }
176
177    if !base.is_empty() {
178        path_elements.push(VariantPathElement::field(base));
179    }
180
181    path_elements.reverse();
182    path_elements
183}
184
185#[cfg(test)]
186mod test {
187    use super::*;
188
189    #[test]
190    fn test_fits_precision() {
191        assert!(fits_precision::<10>(1023));
192        assert!(!fits_precision::<10>(1024));
193        assert!(fits_precision::<10>(-1023));
194        assert!(!fits_precision::<10>(-1024));
195    }
196}