This is an automated email from the ASF dual-hosted git repository.

mbrobbel pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git


The following commit(s) were added to refs/heads/main by this push:
     new 986a7d4175 [Variant] Add Variant::as_f16 (#8232)
986a7d4175 is described below

commit 986a7d417531784c029b7535d05e85dfa8640cd9
Author: Congxian Qiu <[email protected]>
AuthorDate: Fri Aug 29 18:26:50 2025 +0800

    [Variant] Add Variant::as_f16 (#8232)
    
    # Which issue does this PR close?
    
    - Closes #8228.
    
    
    # What changes are included in this PR?
    
    Add `Variant::as_f16`
    
    # Are these changes tested?
    
    Added  doc tests
    
    # Are there any user-facing changes?
    
    Added doc for the function
    
    ---------
    
    Co-authored-by: Matthijs Brobbel <[email protected]>
---
 parquet-variant/Cargo.toml     |  1 +
 parquet-variant/src/variant.rs | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 33 insertions(+)

diff --git a/parquet-variant/Cargo.toml b/parquet-variant/Cargo.toml
index a4d4792e09..6e88bff6bd 100644
--- a/parquet-variant/Cargo.toml
+++ b/parquet-variant/Cargo.toml
@@ -33,6 +33,7 @@ rust-version = { workspace = true }
 [dependencies]
 arrow-schema = { workspace = true }
 chrono = { workspace = true }
+half = { version = "2.1", default-features = false }
 indexmap = "2.10.0"
 uuid = { version = "1.18.0", features = ["v4"]}
 
diff --git a/parquet-variant/src/variant.rs b/parquet-variant/src/variant.rs
index 64458c669e..ea1f3d9bae 100644
--- a/parquet-variant/src/variant.rs
+++ b/parquet-variant/src/variant.rs
@@ -28,6 +28,7 @@ use std::ops::Deref;
 
 use arrow_schema::ArrowError;
 use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime, Timelike, Utc};
+use half::f16;
 use uuid::Uuid;
 
 mod decimal;
@@ -915,6 +916,37 @@ impl<'m, 'v> Variant<'m, 'v> {
             _ => None,
         }
     }
+
+    /// Converts this variant to an `f16` if possible.
+    ///
+    /// Returns `Some(f16)` for float and double variants,
+    /// `None` for non-floating-point variants.
+    ///
+    /// # Example
+    ///
+    /// ```
+    /// use parquet_variant::Variant;
+    /// use half::f16;
+    ///
+    /// // you can extract an f16 from a float variant
+    /// let v1 = Variant::from(std::f32::consts::PI);
+    /// assert_eq!(v1.as_f16(), Some(f16::from_f32(std::f32::consts::PI)));
+    ///
+    /// // and from a double variant (with loss of precision to nearest f16)
+    /// let v2 = Variant::from(std::f64::consts::PI);
+    /// assert_eq!(v2.as_f16(), Some(f16::from_f64(std::f64::consts::PI)));
+    ///
+    /// // but not from other variants
+    /// let v3 = Variant::from("hello!");
+    /// assert_eq!(v3.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)),
+            _ => None,
+        }
+    }
+
     /// Converts this variant to an `f32` if possible.
     ///
     /// Returns `Some(f32)` for float and double variants,

Reply via email to