rluvaton commented on code in PR #11523: URL: https://github.com/apache/arrow-rs/pull/11523#discussion_r4303317024
########## parquet/src/column/writer/min_max.rs: ########## @@ -0,0 +1,389 @@ +// 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::basic::SortOrder; +use crate::column::writer::{compare_greater, is_nan}; +use crate::data_type::private::ParquetValueType; +use crate::schema::types::BasicTypeInfo; + +/// Accumulators for 64 bytes of values, i.e. a cache line and one AVX-512 +/// register, or four NEON / two AVX2 registers. +const LANES_32: usize = 16; +const LANES_64: usize = 8; + +/// Returns the minimum and maximum of `values` after mapping them with `key`, +/// or `None` if `values` is empty. +#[inline(always)] +fn min_max_by_key<T: Copy, K: Copy + Ord, const LANES: usize>( + values: &[T], + key: impl Fn(T) -> K, + key_min: K, + key_max: K, +) -> Option<(K, K)> { + if values.is_empty() { + return None; + } + + let mut mins = [key_max; LANES]; + let mut maxs = [key_min; LANES]; + + let (chunks, remainder) = values.as_chunks::<LANES>(); + for chunk in chunks { + for i in 0..LANES { + let k = key(chunk[i]); + mins[i] = mins[i].min(k); + maxs[i] = maxs[i].max(k); + } + } + for (i, v) in remainder.iter().enumerate() { + let k = key(*v); + mins[i] = mins[i].min(k); + maxs[i] = maxs[i].max(k); + } + + let min = mins.into_iter().min().unwrap(); + let max = maxs.into_iter().max().unwrap(); + Some((min, max)) +} + +#[inline] +fn is_unsigned(basic_type_info: &BasicTypeInfo) -> bool { + matches!(basic_type_info.sort_order(), SortOrder::UNSIGNED) +} + +/// Returns the min and max of an `INT32` column, or `None` if `values` is empty. +pub(crate) fn min_max_i32(basic_type_info: &BasicTypeInfo, values: &[i32]) -> Option<(i32, i32)> { + if is_unsigned(basic_type_info) { + min_max_by_key::<_, _, LANES_32>(values, |v| v as u32, u32::MIN, u32::MAX) + .map(|(min, max)| (min as i32, max as i32)) + } else { + min_max_by_key::<_, _, LANES_32>(values, |v| v, i32::MIN, i32::MAX) + } +} + +/// Returns the min and max of an `INT64` column, or `None` if `values` is empty. +pub(crate) fn min_max_i64(basic_type_info: &BasicTypeInfo, values: &[i64]) -> Option<(i64, i64)> { + if is_unsigned(basic_type_info) { + min_max_by_key::<_, _, LANES_64>(values, |v| v as u64, u64::MIN, u64::MAX) + .map(|(min, max)| (min as i64, max as i64)) + } else { + min_max_by_key::<_, _, LANES_64>(values, |v| v, i64::MIN, i64::MAX) + } +} + +macro_rules! float_min_max { + ($name:ident, $float:ty, $int:ty, $uint:ty, $lanes:expr) => { + /// Returns the min, max and NaN count of a floating point column, + /// comparing by IEEE 754 total order and skipping NaN. + /// + /// Returns `None` if `values` is empty or contains only NaN, in which + /// case the min and max are NaN themselves and the caller falls back + /// to the scalar implementation. + pub(crate) fn $name(values: &[$float]) -> Option<($float, $float, u64)> { + const SIGN_SHIFT: u32 = <$int>::BITS - 1; + + // Maps the bits of a float to an integer with the same ordering as + // `total_cmp`: negative values have their magnitude bits flipped + // so that they order in reverse. The mapping is its own inverse. + // + // To avoid calling `{f32,f64}::total_cmp` for both min and max values and every other comparison + // we transform the input once (like what rust is doing internally before comparing) and reuse it, this avoids the overhead of calling `total_cmp` twice for each value. + // and doing the same calculation. + // + // This is the same transformation used in Rust `f32::total_cmp` and `f64::total_cmp` + // but we avoid calling + // f32: https://github.com/rust-lang/rust/blob/b5427502dab50cb27d9327d2893ddf2337dd4388/library/core/src/num/f32.rs#L1626 + // f64: https://github.com/rust-lang/rust/blob/b5427502dab50cb27d9327d2893ddf2337dd4388/library/core/src/num/f64.rs#L1604 + #[inline(always)] + fn total_order_key(bits: $int) -> $int { Review Comment: I can't take the credit for it, this was Claude -- 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]
