Skip to content

Commit 7e38bbb

Browse files
scovichalamb
andauthored
[Variant] Move VariantAsPrimitive to type_conversions.rs (#8321)
# Which issue does this PR close? * Follow-up to #8280 # Rationale for this change See #8280 (comment) # What changes are included in this PR? See description. # Are these changes tested? Code movement. Compilation suffices. # Are there any user-facing changes? No. --------- Co-authored-by: Andrew Lamb <[email protected]>
1 parent 2824638 commit 7e38bbb

File tree

2 files changed

+45
-41
lines changed

2 files changed

+45
-41
lines changed

parquet-variant-compute/src/type_conversion.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
//! Module for transforming a typed arrow `Array` to `VariantArray`.
1919
20+
use arrow::datatypes::{self, ArrowPrimitiveType};
21+
use parquet_variant::Variant;
22+
2023
/// Options for controlling the behavior of `cast_to_variant_with_options`.
2124
#[derive(Debug, Clone, PartialEq, Eq)]
2225
pub struct CastOptions {
@@ -30,6 +33,47 @@ impl Default for CastOptions {
3033
}
3134
}
3235

36+
/// Helper trait for converting `Variant` values to arrow primitive values.
37+
pub(crate) trait VariantAsPrimitive<T: ArrowPrimitiveType> {
38+
fn as_primitive(&self) -> Option<T::Native>;
39+
}
40+
41+
impl VariantAsPrimitive<datatypes::Int32Type> for Variant<'_, '_> {
42+
fn as_primitive(&self) -> Option<i32> {
43+
self.as_int32()
44+
}
45+
}
46+
impl VariantAsPrimitive<datatypes::Int16Type> for Variant<'_, '_> {
47+
fn as_primitive(&self) -> Option<i16> {
48+
self.as_int16()
49+
}
50+
}
51+
impl VariantAsPrimitive<datatypes::Int8Type> for Variant<'_, '_> {
52+
fn as_primitive(&self) -> Option<i8> {
53+
self.as_int8()
54+
}
55+
}
56+
impl VariantAsPrimitive<datatypes::Int64Type> for Variant<'_, '_> {
57+
fn as_primitive(&self) -> Option<i64> {
58+
self.as_int64()
59+
}
60+
}
61+
impl VariantAsPrimitive<datatypes::Float16Type> for Variant<'_, '_> {
62+
fn as_primitive(&self) -> Option<half::f16> {
63+
self.as_f16()
64+
}
65+
}
66+
impl VariantAsPrimitive<datatypes::Float32Type> for Variant<'_, '_> {
67+
fn as_primitive(&self) -> Option<f32> {
68+
self.as_f32()
69+
}
70+
}
71+
impl VariantAsPrimitive<datatypes::Float64Type> for Variant<'_, '_> {
72+
fn as_primitive(&self) -> Option<f64> {
73+
self.as_f64()
74+
}
75+
}
76+
3377
/// Convert the value at a specific index in the given array into a `Variant`.
3478
macro_rules! non_generic_conversion_single_value {
3579
($array:expr, $cast_fn:expr, $index:expr) => {{

parquet-variant-compute/src/variant_get/output/row_builder.rs

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use arrow::datatypes::ArrowPrimitiveType;
2222
use arrow::error::{ArrowError, Result};
2323
use parquet_variant::{Variant, VariantPath};
2424

25+
use crate::type_conversion::VariantAsPrimitive;
2526
use crate::VariantArrayBuilder;
2627

2728
use std::sync::Arc;
@@ -124,47 +125,6 @@ impl<T: VariantShreddingRowBuilder> VariantShreddingRowBuilder for VariantPathRo
124125
}
125126
}
126127

127-
/// Helper trait for converting `Variant` values to arrow primitive values.
128-
trait VariantAsPrimitive<T: ArrowPrimitiveType> {
129-
fn as_primitive(&self) -> Option<T::Native>;
130-
}
131-
132-
impl VariantAsPrimitive<datatypes::Int32Type> for Variant<'_, '_> {
133-
fn as_primitive(&self) -> Option<i32> {
134-
self.as_int32()
135-
}
136-
}
137-
impl VariantAsPrimitive<datatypes::Int16Type> for Variant<'_, '_> {
138-
fn as_primitive(&self) -> Option<i16> {
139-
self.as_int16()
140-
}
141-
}
142-
impl VariantAsPrimitive<datatypes::Int8Type> for Variant<'_, '_> {
143-
fn as_primitive(&self) -> Option<i8> {
144-
self.as_int8()
145-
}
146-
}
147-
impl VariantAsPrimitive<datatypes::Int64Type> for Variant<'_, '_> {
148-
fn as_primitive(&self) -> Option<i64> {
149-
self.as_int64()
150-
}
151-
}
152-
impl VariantAsPrimitive<datatypes::Float16Type> for Variant<'_, '_> {
153-
fn as_primitive(&self) -> Option<half::f16> {
154-
self.as_f16()
155-
}
156-
}
157-
impl VariantAsPrimitive<datatypes::Float32Type> for Variant<'_, '_> {
158-
fn as_primitive(&self) -> Option<f32> {
159-
self.as_f32()
160-
}
161-
}
162-
impl VariantAsPrimitive<datatypes::Float64Type> for Variant<'_, '_> {
163-
fn as_primitive(&self) -> Option<f64> {
164-
self.as_f64()
165-
}
166-
}
167-
168128
/// Helper function to get a user-friendly type name
169129
fn get_type_name<T: ArrowPrimitiveType>() -> &'static str {
170130
match std::any::type_name::<T>() {

0 commit comments

Comments
 (0)