klion26 commented on code in PR #8516:
URL: https://github.com/apache/arrow-rs/pull/8516#discussion_r2404454934
##########
parquet-variant-compute/src/type_conversion.rs:
##########
@@ -60,6 +89,35 @@ impl_primitive_from_variant!(datatypes::UInt64Type, as_u64);
impl_primitive_from_variant!(datatypes::Float16Type, as_f16);
impl_primitive_from_variant!(datatypes::Float32Type, as_f32);
impl_primitive_from_variant!(datatypes::Float64Type, as_f64);
+impl_primitive_from_variant!(
+ datatypes::Date32Type,
+ as_naive_date,
+ Date32Type::from_naive_date
+);
+impl_timestamp_from_variant!(
+ datatypes::TimestampMicrosecondType,
+ as_timestamp_ntz_micros,
+ ntz = true,
+ Self::make_value,
Review Comment:
Here use `Self::make_value` instread of `make_value` because I think
`Self::make_value` is the whole function name, can change if needed
##########
parquet-variant-compute/src/type_conversion.rs:
##########
@@ -38,12 +39,40 @@ pub(crate) trait PrimitiveFromVariant: ArrowPrimitiveType {
fn from_variant(variant: &Variant<'_, '_>) -> Option<Self::Native>;
}
+/// Extension trait for Arrow timestamp types that can extract their native
value from a Variant
+/// We can't use [`PrimitiveFromVariant`] directly because we might need to
use methods that
+/// are only available on [`ArrowTimestampType`] (such as with_timezone_opt)
+pub(crate) trait TimestampFromVariant<const NTZ: bool>: ArrowTimestampType {
+ fn from_variant(variant: &Variant<'_, '_>) -> Option<Self::Native>;
+}
+
+/// Extension trait that [`ArrowTimestampType`] handle [`DateTime<Utc>`] like
[`NaiveDateTime`]
+trait MakeValueTz: ArrowTimestampType {
+ fn make_value_tz(timestamp: DateTime<Utc>) -> Option<i64> {
+ Self::make_value(timestamp.naive_utc())
+ }
+}
+
+impl<T: ArrowTimestampType> MakeValueTz for T {}
+
/// Macro to generate PrimitiveFromVariant implementations for Arrow primitive
types
macro_rules! impl_primitive_from_variant {
- ($arrow_type:ty, $variant_method:ident) => {
+ ($arrow_type:ty, $variant_method:ident $(, $cast_fn:expr)?) => {
impl PrimitiveFromVariant for $arrow_type {
fn from_variant(variant: &Variant<'_, '_>) -> Option<Self::Native>
{
- variant.$variant_method()
+ let value = variant.$variant_method();
+ $( let value = value.map($cast_fn); )?
+ value
+ }
+ }
+ };
+}
+
+macro_rules! impl_timestamp_from_variant {
+ ($timestamp_type:ty, $variant_method:ident, ntz=$ntz:ident, $cast_fn:expr
$(,)?) => {
Review Comment:
After this change, I removed the `micro` -> `nano`, because I'm not sure how
to define the `macro_rules` here,
the macro rule in my head now is something like below
```
($timestamp_type:ty, $variant_method_a:ident,
$(opt=$variant_method_b:ident,)? ntz=$ntz:ident, $cast_fn:expr $(,)?) => {
impl TimestampFromVariant<{ $ntz }> for $timestamp_type {
fn from_variant(variant: &Variant<'_, '_>) ->
Option<Self::Native> {
let value = variant.$variant_method_a();
if value.is_some() {
return value.and_then($cast_fn);
} else {
#[allow(unused_mut)]
let mut value = None;
$(
value = variant.$variant_method_b();
)?
return value.and_then($cast_fn);
}
}
}
};
```
--
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]