-
Notifications
You must be signed in to change notification settings - Fork 1k
[Variant] Define and use VariantDecimalType trait #8562
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
9 commits
Select commit
Hold shift + click to select a range
32bc99c
[Variant] Define and use VariantDecimalType trait
scovich 9f83ab8
additional cleanup
scovich bd7f3a7
better approach to MAX_UNSCALED_VALUE
scovich e6c88b9
simpler macro
scovich 3f66de4
simpler phantoms
scovich be9e2c9
another tweak
scovich 0b9c5df
minimize diff
scovich a731191
tiny tweak
scovich 6c0823a
Merge remote-tracking branch 'oss/main' into variant-decimal-trait
scovich 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 |
|---|---|---|
|
|
@@ -15,25 +15,22 @@ | |
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| use crate::type_conversion::{CastOptions, decimal_to_variant_decimal}; | ||
| use crate::type_conversion::CastOptions; | ||
| use arrow::array::{ | ||
| Array, AsArray, FixedSizeListArray, GenericBinaryArray, GenericListArray, GenericListViewArray, | ||
| GenericStringArray, OffsetSizeTrait, PrimitiveArray, | ||
| }; | ||
| use arrow::compute::kernels::cast; | ||
| use arrow::datatypes::{ | ||
| ArrowNativeType, ArrowPrimitiveType, ArrowTemporalType, ArrowTimestampType, Date32Type, | ||
| Date64Type, Float16Type, Float32Type, Float64Type, Int8Type, Int16Type, Int32Type, Int64Type, | ||
| RunEndIndexType, Time32MillisecondType, Time32SecondType, Time64MicrosecondType, | ||
| Time64NanosecondType, TimestampMicrosecondType, TimestampMillisecondType, | ||
| TimestampNanosecondType, TimestampSecondType, UInt8Type, UInt16Type, UInt32Type, UInt64Type, | ||
| self as datatypes, ArrowNativeType, ArrowPrimitiveType, ArrowTemporalType, ArrowTimestampType, | ||
| DecimalType, RunEndIndexType, | ||
| }; | ||
| use arrow::temporal_conversions::{as_date, as_datetime, as_time}; | ||
| use arrow_schema::{ArrowError, DataType, TimeUnit}; | ||
| use chrono::{DateTime, TimeZone, Utc}; | ||
| use parquet_variant::{ | ||
| ObjectFieldBuilder, Variant, VariantBuilderExt, VariantDecimal4, VariantDecimal8, | ||
| VariantDecimal16, | ||
| VariantDecimal16, VariantDecimalType, | ||
| }; | ||
| use std::collections::HashMap; | ||
| use std::ops::Range; | ||
|
|
@@ -46,31 +43,31 @@ use std::ops::Range; | |
| pub(crate) enum ArrowToVariantRowBuilder<'a> { | ||
| Null(NullArrowToVariantBuilder), | ||
| Boolean(BooleanArrowToVariantBuilder<'a>), | ||
| PrimitiveInt8(PrimitiveArrowToVariantBuilder<'a, Int8Type>), | ||
| PrimitiveInt16(PrimitiveArrowToVariantBuilder<'a, Int16Type>), | ||
| PrimitiveInt32(PrimitiveArrowToVariantBuilder<'a, Int32Type>), | ||
| PrimitiveInt64(PrimitiveArrowToVariantBuilder<'a, Int64Type>), | ||
| PrimitiveUInt8(PrimitiveArrowToVariantBuilder<'a, UInt8Type>), | ||
| PrimitiveUInt16(PrimitiveArrowToVariantBuilder<'a, UInt16Type>), | ||
| PrimitiveUInt32(PrimitiveArrowToVariantBuilder<'a, UInt32Type>), | ||
| PrimitiveUInt64(PrimitiveArrowToVariantBuilder<'a, UInt64Type>), | ||
| PrimitiveFloat16(PrimitiveArrowToVariantBuilder<'a, Float16Type>), | ||
| PrimitiveFloat32(PrimitiveArrowToVariantBuilder<'a, Float32Type>), | ||
| PrimitiveFloat64(PrimitiveArrowToVariantBuilder<'a, Float64Type>), | ||
| Decimal32(Decimal32ArrowToVariantBuilder<'a>), | ||
| Decimal64(Decimal64ArrowToVariantBuilder<'a>), | ||
| Decimal128(Decimal128ArrowToVariantBuilder<'a>), | ||
| PrimitiveInt8(PrimitiveArrowToVariantBuilder<'a, datatypes::Int8Type>), | ||
| PrimitiveInt16(PrimitiveArrowToVariantBuilder<'a, datatypes::Int16Type>), | ||
| PrimitiveInt32(PrimitiveArrowToVariantBuilder<'a, datatypes::Int32Type>), | ||
| PrimitiveInt64(PrimitiveArrowToVariantBuilder<'a, datatypes::Int64Type>), | ||
| PrimitiveUInt8(PrimitiveArrowToVariantBuilder<'a, datatypes::UInt8Type>), | ||
| PrimitiveUInt16(PrimitiveArrowToVariantBuilder<'a, datatypes::UInt16Type>), | ||
| PrimitiveUInt32(PrimitiveArrowToVariantBuilder<'a, datatypes::UInt32Type>), | ||
| PrimitiveUInt64(PrimitiveArrowToVariantBuilder<'a, datatypes::UInt64Type>), | ||
| PrimitiveFloat16(PrimitiveArrowToVariantBuilder<'a, datatypes::Float16Type>), | ||
| PrimitiveFloat32(PrimitiveArrowToVariantBuilder<'a, datatypes::Float32Type>), | ||
| PrimitiveFloat64(PrimitiveArrowToVariantBuilder<'a, datatypes::Float64Type>), | ||
| Decimal32(DecimalArrowToVariantBuilder<'a, datatypes::Decimal32Type, VariantDecimal4>), | ||
| Decimal64(DecimalArrowToVariantBuilder<'a, datatypes::Decimal64Type, VariantDecimal8>), | ||
| Decimal128(DecimalArrowToVariantBuilder<'a, datatypes::Decimal128Type, VariantDecimal16>), | ||
| Decimal256(Decimal256ArrowToVariantBuilder<'a>), | ||
| TimestampSecond(TimestampArrowToVariantBuilder<'a, TimestampSecondType>), | ||
| TimestampMillisecond(TimestampArrowToVariantBuilder<'a, TimestampMillisecondType>), | ||
| TimestampMicrosecond(TimestampArrowToVariantBuilder<'a, TimestampMicrosecondType>), | ||
| TimestampNanosecond(TimestampArrowToVariantBuilder<'a, TimestampNanosecondType>), | ||
| Date32(DateArrowToVariantBuilder<'a, Date32Type>), | ||
| Date64(DateArrowToVariantBuilder<'a, Date64Type>), | ||
| Time32Second(TimeArrowToVariantBuilder<'a, Time32SecondType>), | ||
| Time32Millisecond(TimeArrowToVariantBuilder<'a, Time32MillisecondType>), | ||
| Time64Microsecond(TimeArrowToVariantBuilder<'a, Time64MicrosecondType>), | ||
| Time64Nanosecond(TimeArrowToVariantBuilder<'a, Time64NanosecondType>), | ||
| TimestampSecond(TimestampArrowToVariantBuilder<'a, datatypes::TimestampSecondType>), | ||
| TimestampMillisecond(TimestampArrowToVariantBuilder<'a, datatypes::TimestampMillisecondType>), | ||
| TimestampMicrosecond(TimestampArrowToVariantBuilder<'a, datatypes::TimestampMicrosecondType>), | ||
| TimestampNanosecond(TimestampArrowToVariantBuilder<'a, datatypes::TimestampNanosecondType>), | ||
| Date32(DateArrowToVariantBuilder<'a, datatypes::Date32Type>), | ||
| Date64(DateArrowToVariantBuilder<'a, datatypes::Date64Type>), | ||
| Time32Second(TimeArrowToVariantBuilder<'a, datatypes::Time32SecondType>), | ||
| Time32Millisecond(TimeArrowToVariantBuilder<'a, datatypes::Time32MillisecondType>), | ||
| Time64Microsecond(TimeArrowToVariantBuilder<'a, datatypes::Time64MicrosecondType>), | ||
| Time64Nanosecond(TimeArrowToVariantBuilder<'a, datatypes::Time64NanosecondType>), | ||
| Binary(BinaryArrowToVariantBuilder<'a, i32>), | ||
| LargeBinary(BinaryArrowToVariantBuilder<'a, i64>), | ||
| BinaryView(BinaryViewArrowToVariantBuilder<'a>), | ||
|
|
@@ -87,9 +84,9 @@ pub(crate) enum ArrowToVariantRowBuilder<'a> { | |
| Map(MapArrowToVariantBuilder<'a>), | ||
| Union(UnionArrowToVariantBuilder<'a>), | ||
| Dictionary(DictionaryArrowToVariantBuilder<'a>), | ||
| RunEndEncodedInt16(RunEndEncodedArrowToVariantBuilder<'a, Int16Type>), | ||
| RunEndEncodedInt32(RunEndEncodedArrowToVariantBuilder<'a, Int32Type>), | ||
| RunEndEncodedInt64(RunEndEncodedArrowToVariantBuilder<'a, Int64Type>), | ||
| RunEndEncodedInt16(RunEndEncodedArrowToVariantBuilder<'a, datatypes::Int16Type>), | ||
| RunEndEncodedInt32(RunEndEncodedArrowToVariantBuilder<'a, datatypes::Int32Type>), | ||
| RunEndEncodedInt64(RunEndEncodedArrowToVariantBuilder<'a, datatypes::Int64Type>), | ||
| } | ||
|
|
||
| impl<'a> ArrowToVariantRowBuilder<'a> { | ||
|
|
@@ -174,13 +171,13 @@ pub(crate) fn make_arrow_to_variant_row_builder<'a>( | |
| DataType::Float32 => PrimitiveFloat32(PrimitiveArrowToVariantBuilder::new(array)), | ||
| DataType::Float64 => PrimitiveFloat64(PrimitiveArrowToVariantBuilder::new(array)), | ||
| DataType::Decimal32(_, scale) => { | ||
| Decimal32(Decimal32ArrowToVariantBuilder::new(array, options, *scale)) | ||
| Decimal32(DecimalArrowToVariantBuilder::new(array, options, *scale)) | ||
| } | ||
| DataType::Decimal64(_, scale) => { | ||
| Decimal64(Decimal64ArrowToVariantBuilder::new(array, options, *scale)) | ||
| Decimal64(DecimalArrowToVariantBuilder::new(array, options, *scale)) | ||
| } | ||
| DataType::Decimal128(_, scale) => { | ||
| Decimal128(Decimal128ArrowToVariantBuilder::new(array, options, *scale)) | ||
| Decimal128(DecimalArrowToVariantBuilder::new(array, options, *scale)) | ||
| } | ||
| DataType::Decimal256(_, scale) => { | ||
| Decimal256(Decimal256ArrowToVariantBuilder::new(array, options, *scale)) | ||
|
|
@@ -320,26 +317,28 @@ pub(crate) fn make_arrow_to_variant_row_builder<'a>( | |
| // worth the trouble, tho, because it makes for some pretty bulky and unwieldy macro expansions. | ||
| macro_rules! define_row_builder { | ||
| ( | ||
| struct $name:ident<$lifetime:lifetime $(, $generic:ident: $bound:path )?> | ||
| struct $name:ident<$lifetime:lifetime $(, $generic:ident $( : $bound:path )? )*> | ||
| $( where $where_path:path: $where_bound:path $(,)? )? | ||
| $({ $($field:ident: $field_type:ty),+ $(,)? })?, | ||
| $({ $( $field:ident: $field_type:ty ),+ $(,)? })?, | ||
| |$array_param:ident| -> $array_type:ty { $init_expr:expr } | ||
| $(, |$value:ident| $(-> Option<$option_ty:ty>)? $value_transform:expr)? | ||
| $(, |$value:ident| $(-> Option<$option_ty:ty>)? $value_transform:expr )? | ||
| ) => { | ||
| pub(crate) struct $name<$lifetime $(, $generic: $bound )?> | ||
| pub(crate) struct $name<$lifetime $(, $generic: $( $bound )? )*> | ||
| $( where $where_path: $where_bound )? | ||
| { | ||
| array: &$lifetime $array_type, | ||
| $( $( $field: $field_type, )+ )? | ||
| _phantom: std::marker::PhantomData<($( $generic, )*)>, // capture all type params | ||
|
Contributor
Author
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. Capture all generic params automatically (decimal needs this, doesn't hurt other types) |
||
| } | ||
|
|
||
| impl<$lifetime $(, $generic: $bound+ )?> $name<$lifetime $(, $generic)?> | ||
| impl<$lifetime $(, $generic: $( $bound )? )*> $name<$lifetime $(, $generic)*> | ||
| $( where $where_path: $where_bound )? | ||
| { | ||
| pub(crate) fn new($array_param: &$lifetime dyn Array $(, $( $field: $field_type ),+ )?) -> Self { | ||
| pub(crate) fn new($array_param: &$lifetime dyn Array $( $(, $field: $field_type )+ )?) -> Self { | ||
| Self { | ||
| array: $init_expr, | ||
| $( $( $field, )+ )? | ||
| _phantom: std::marker::PhantomData, | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -401,43 +400,27 @@ define_row_builder!( | |
| ); | ||
|
|
||
| define_row_builder!( | ||
| struct Decimal32ArrowToVariantBuilder<'a> { | ||
| options: &'a CastOptions, | ||
| scale: i8, | ||
| }, | ||
| |array| -> arrow::array::Decimal32Array { array.as_primitive() }, | ||
| |value| -> Option<_> { decimal_to_variant_decimal!(value, scale, i32, VariantDecimal4) } | ||
| ); | ||
|
|
||
| define_row_builder!( | ||
| struct Decimal64ArrowToVariantBuilder<'a> { | ||
| options: &'a CastOptions, | ||
| scale: i8, | ||
| }, | ||
| |array| -> arrow::array::Decimal64Array { array.as_primitive() }, | ||
| |value| -> Option<_> { decimal_to_variant_decimal!(value, scale, i64, VariantDecimal8) } | ||
| ); | ||
|
|
||
| define_row_builder!( | ||
| struct Decimal128ArrowToVariantBuilder<'a> { | ||
| struct DecimalArrowToVariantBuilder<'a, A: DecimalType, V> | ||
| where | ||
| V: VariantDecimalType<Native = A::Native>, | ||
| { | ||
| options: &'a CastOptions, | ||
| scale: i8, | ||
| }, | ||
| |array| -> arrow::array::Decimal128Array { array.as_primitive() }, | ||
| |value| -> Option<_> { decimal_to_variant_decimal!(value, scale, i128, VariantDecimal16) } | ||
| |array| -> PrimitiveArray<A> { array.as_primitive() }, | ||
| |value| -> Option<_> { V::try_new_with_signed_scale(value, *scale).ok() } | ||
| ); | ||
|
|
||
| // Decimal256 needs a two-stage conversion via i128 | ||
| define_row_builder!( | ||
| struct Decimal256ArrowToVariantBuilder<'a> { | ||
| options: &'a CastOptions, | ||
| scale: i8, | ||
| }, | ||
| |array| -> arrow::array::Decimal256Array { array.as_primitive() }, | ||
| |value| -> Option<_> { | ||
| // Decimal256 needs special handling - convert to i128 if possible | ||
| value.to_i128().and_then(|i128_val| { | ||
| decimal_to_variant_decimal!(i128_val, scale, i128, VariantDecimal16) | ||
| }) | ||
| let value = value.to_i128(); | ||
| value.and_then(|v| VariantDecimal16::try_new_with_signed_scale(v, *scale).ok()) | ||
| } | ||
| ); | ||
|
|
||
|
|
@@ -911,6 +894,7 @@ mod tests { | |
| use super::*; | ||
| use crate::{VariantArray, VariantArrayBuilder}; | ||
| use arrow::array::{ArrayRef, BooleanArray, Int32Array, StringArray}; | ||
| use arrow::datatypes::Int32Type; | ||
| use std::sync::Arc; | ||
|
|
||
| /// Builds a VariantArray from an Arrow array using the row builder. | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -150,22 +150,3 @@ macro_rules! primitive_conversion_single_value { | |
| }}; | ||
| } | ||
| pub(crate) use primitive_conversion_single_value; | ||
|
|
||
| /// Convert a decimal value to a `VariantDecimal` | ||
| macro_rules! decimal_to_variant_decimal { | ||
|
Contributor
Author
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. Replaced by |
||
| ($v:ident, $scale:expr, $value_type:ty, $variant_type:ty) => {{ | ||
| let (v, scale) = if *$scale < 0 { | ||
| // For negative scale, we need to multiply the value by 10^|scale| | ||
| // For example: 123 with scale -2 becomes 12300 with scale 0 | ||
| let multiplier = <$value_type>::pow(10, (-*$scale) as u32); | ||
| (<$value_type>::checked_mul($v, multiplier), 0u8) | ||
| } else { | ||
| (Some($v), *$scale as u8) | ||
| }; | ||
|
|
||
| // Return an Option to allow callers to decide whether to error (strict) | ||
| // or append null (non-strict) on conversion failure | ||
| v.and_then(|v| <$variant_type>::try_new(v, scale).ok()) | ||
| }}; | ||
| } | ||
| pub(crate) use decimal_to_variant_decimal; | ||
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.
Make the bound optional, because decimals use the where clause instead for better readability.
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.
Also allow multiple generic params instead of just one