-
Notifications
You must be signed in to change notification settings - Fork 1k
[Variant] Add variant to arrow primitive support for boolean/timestamp/time #8516
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2134dcc
[Variant] Support primitive variant to arrow row for boolean
klion26 76f51ad
[Variant] Support primitive variant to arrow row for timestamp(micro…
klion26 fdfb93c
address comment
klion26 48047fe
address comment
klion26 b074728
add Variant::{as_timestamp_micros/as_timestamp_nanos} and simplify th…
klion26 b62e15b
improve the macro rules for impl_primitive_form_variant for timestamp
klion26 d1a7d06
address comments
klion26 6762eff
fix style
klion26 2505dfa
make doc clean
klion26 f325746
address comments
klion26 0da1287
simplify logic for timestamp tz, add tests micro->nano back
klion26 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,7 @@ | |
|
||
//! Module for transforming a typed arrow `Array` to `VariantArray`. | ||
|
||
use arrow::datatypes::{self, ArrowPrimitiveType}; | ||
use arrow::datatypes::{self, ArrowPrimitiveType, ArrowTimestampType, Date32Type}; | ||
use parquet_variant::Variant; | ||
|
||
/// Options for controlling the behavior of `cast_to_variant_with_options`. | ||
|
@@ -38,12 +38,31 @@ 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 need _two_ implementations for each | ||
/// timestamp type -- the `NTZ` param here. | ||
pub(crate) trait TimestampFromVariant<const NTZ: bool>: ArrowTimestampType { | ||
fn from_variant(variant: &Variant<'_, '_>) -> Option<Self::Native>; | ||
} | ||
|
||
/// 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 $(,)?) => { | ||
impl TimestampFromVariant<{ $ntz }> for $timestamp_type { | ||
fn from_variant(variant: &Variant<'_, '_>) -> Option<Self::Native> { | ||
variant.$variant_method().and_then($cast_fn) | ||
} | ||
} | ||
}; | ||
|
@@ -60,6 +79,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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here use |
||
); | ||
impl_timestamp_from_variant!( | ||
datatypes::TimestampMicrosecondType, | ||
as_timestamp_micros, | ||
ntz = false, | ||
|timestamp| Self::make_value(timestamp.naive_utc()) | ||
); | ||
impl_timestamp_from_variant!( | ||
datatypes::TimestampNanosecondType, | ||
as_timestamp_ntz_nanos, | ||
ntz = true, | ||
Self::make_value | ||
); | ||
impl_timestamp_from_variant!( | ||
datatypes::TimestampNanosecondType, | ||
as_timestamp_nanos, | ||
ntz = false, | ||
|timestamp| Self::make_value(timestamp.naive_utc()) | ||
); | ||
|
||
/// Convert the value at a specific index in the given array into a `Variant`. | ||
macro_rules! non_generic_conversion_single_value { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After this change, I removed the
micro
->nano
, because I'm not sure how to define themacro_rules
here,the macro rule in my head now is something like below
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The micro -> nano conversion actually happens inside
as_timestamp_[ntz_]nanos
, I left a comment there about it.