Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions parquet-variant/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,20 @@ pub(crate) const fn expect_size_of<T>(expected: usize) {
let _ = [""; 0][size];
}
}

pub(crate) fn fits_precision<const N: u32>(n: impl Into<i64>) -> bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put N into the generic, not the parameter, because this can yield better performance, do I understand correctly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was mostly out of habit for small integer manipulation utilities... But now that you mention, it probably doesn't matter in the slightest -- the compiler will anyway inline aggressively and the constant arg will be folded in regardless of whether it's a generic arg or a function arg.

That said, there's one potential advantage to keeping the generic arg: Otherwise, it could be ambiguous which of two integer args is the precision and which is the actual value. Any preferences?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the detailed reply, I'm fine with the current implementation

n.into().unsigned_abs().leading_zeros() >= (i64::BITS - N)
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_fits_precision() {
assert!(fits_precision::<10>(1023));
assert!(!fits_precision::<10>(1024));
assert!(fits_precision::<10>(-1023));
assert!(!fits_precision::<10>(-1024));
}
}
50 changes: 37 additions & 13 deletions parquet-variant/src/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::decoder::{
self, get_basic_type, get_primitive_type, VariantBasicType, VariantPrimitiveType,
};
use crate::path::{VariantPath, VariantPathElement};
use crate::utils::{first_byte_from_slice, slice_from_slice};
use crate::utils::{first_byte_from_slice, fits_precision, slice_from_slice};
use std::ops::Deref;

use arrow_schema::ArrowError;
Expand Down Expand Up @@ -1082,8 +1082,8 @@ impl<'m, 'v> Variant<'m, 'v> {

/// Converts this variant to an `f16` if possible.
///
/// Returns `Some(f16)` for float and double variants,
/// `None` for non-floating-point variants.
/// Returns `Some(f16)` for floating point values, and integers with up to 11 bits of
/// precision. `None` otherwise.
///
/// # Example
///
Expand All @@ -1099,21 +1099,29 @@ impl<'m, 'v> Variant<'m, 'v> {
/// let v2 = Variant::from(std::f64::consts::PI);
/// assert_eq!(v2.as_f16(), Some(f16::from_f64(std::f64::consts::PI)));
///
/// // and from integers with no more than 11 bits of precision
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we need to add an overflow example here(e.g, Variant::from(2048) for f16)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The unit test for fits_precision does test overflow, both positive and negative.
Do we need additional (indirect) test coverage here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we need to update the doc(the comment said that -- Returns Some(f16) for float and double variants, None for non-floating-point variants) or add an example for integer overflow (`Variant::from(2048) for f16 -- will return None)

I don't have a strong preference here, it's just an idea that popped into my mind when I saw this

Copy link
Contributor Author

@scovich scovich Sep 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh! Good catch on the doc comment. Updated all three.

/// let v3 = Variant::from(2047);
/// assert_eq!(v3.as_f16(), Some(f16::from_f32(2047.0)));
///
/// // but not from other variants
/// let v3 = Variant::from("hello!");
/// assert_eq!(v3.as_f16(), None);
/// let v4 = Variant::from("hello!");
/// assert_eq!(v4.as_f16(), None);
pub fn as_f16(&self) -> Option<f16> {
match *self {
Variant::Float(i) => Some(f16::from_f32(i)),
Variant::Double(i) => Some(f16::from_f64(i)),
Variant::Int8(i) => Some(i.into()),
Variant::Int16(i) if fits_precision::<11>(i) => Some(f16::from_f32(i as _)),
Variant::Int32(i) if fits_precision::<11>(i) => Some(f16::from_f32(i as _)),
Variant::Int64(i) if fits_precision::<11>(i) => Some(f16::from_f32(i as _)),
_ => None,
}
}

/// Converts this variant to an `f32` if possible.
///
/// Returns `Some(f32)` for float and double variants,
/// `None` for non-floating-point variants.
/// Returns `Some(f32)` for floating point values, and integer values with up to 24 bits of
/// precision. `None` otherwise.
///
/// # Examples
///
Expand All @@ -1128,23 +1136,31 @@ impl<'m, 'v> Variant<'m, 'v> {
/// let v2 = Variant::from(std::f64::consts::PI);
/// assert_eq!(v2.as_f32(), Some(std::f32::consts::PI));
///
/// // and from integers with no more than 24 bits of precision
/// let v3 = Variant::from(16777215i64);
/// assert_eq!(v3.as_f32(), Some(16777215.0));
///
/// // but not from other variants
/// let v3 = Variant::from("hello!");
/// assert_eq!(v3.as_f32(), None);
/// let v4 = Variant::from("hello!");
/// assert_eq!(v4.as_f32(), None);
/// ```
#[allow(clippy::cast_possible_truncation)]
pub fn as_f32(&self) -> Option<f32> {
match *self {
Variant::Float(i) => Some(i),
Variant::Double(i) => Some(i as f32),
Variant::Int8(i) => Some(i.into()),
Variant::Int16(i) => Some(i.into()),
Variant::Int32(i) if fits_precision::<24>(i) => Some(i as _),
Variant::Int64(i) if fits_precision::<24>(i) => Some(i as _),
_ => None,
}
}

/// Converts this variant to an `f64` if possible.
///
/// Returns `Some(f64)` for float and double variants,
/// `None` for non-floating-point variants.
/// Returns `Some(f64)` for floating point values, and integer values with up to 53 bits of
/// precision. `None` otherwise.
///
/// # Examples
///
Expand All @@ -1159,14 +1175,22 @@ impl<'m, 'v> Variant<'m, 'v> {
/// let v2 = Variant::from(std::f64::consts::PI);
/// assert_eq!(v2.as_f64(), Some(std::f64::consts::PI));
///
/// // and from integers with no more than 53 bits of precision
/// let v3 = Variant::from(9007199254740991i64);
/// assert_eq!(v3.as_f64(), Some(9007199254740991.0));
///
/// // but not from other variants
/// let v3 = Variant::from("hello!");
/// assert_eq!(v3.as_f64(), None);
/// let v4 = Variant::from("hello!");
/// assert_eq!(v4.as_f64(), None);
/// ```
pub fn as_f64(&self) -> Option<f64> {
match *self {
Variant::Float(i) => Some(i.into()),
Variant::Double(i) => Some(i),
Variant::Int8(i) => Some(i.into()),
Variant::Int16(i) => Some(i.into()),
Variant::Int32(i) => Some(i.into()),
Variant::Int64(i) if fits_precision::<53>(i) => Some(i as _),
_ => None,
}
}
Expand Down
Loading