Skip to content

Commit

Permalink
Make downcast macros hygenic (#6400)
Browse files Browse the repository at this point in the history
  • Loading branch information
tustvold committed Oct 24, 2024
1 parent 1295b00 commit 6924590
Showing 1 changed file with 40 additions and 35 deletions.
75 changes: 40 additions & 35 deletions arrow-array/src/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ use crate::array::*;
use crate::types::*;
use arrow_data::ArrayData;

#[doc(hidden)]
pub mod __private {
pub use arrow_schema::DataType;
}

/// Repeats the provided pattern based on the number of comma separated identifiers
#[doc(hidden)]
#[macro_export]
Expand Down Expand Up @@ -66,28 +71,28 @@ macro_rules! repeat_pat {
macro_rules! downcast_integer {
($($data_type:expr),+ => ($m:path $(, $args:tt)*), $($p:pat => $fallback:expr $(,)*)*) => {
match ($($data_type),+) {
$crate::repeat_pat!(arrow_schema::DataType::Int8, $($data_type),+) => {
$crate::repeat_pat!($crate::cast::__private::DataType::Int8, $($data_type),+) => {
$m!($crate::types::Int8Type $(, $args)*)
}
$crate::repeat_pat!(arrow_schema::DataType::Int16, $($data_type),+) => {
$crate::repeat_pat!($crate::cast::__private::DataType::Int16, $($data_type),+) => {
$m!($crate::types::Int16Type $(, $args)*)
}
$crate::repeat_pat!(arrow_schema::DataType::Int32, $($data_type),+) => {
$crate::repeat_pat!($crate::cast::__private::DataType::Int32, $($data_type),+) => {
$m!($crate::types::Int32Type $(, $args)*)
}
$crate::repeat_pat!(arrow_schema::DataType::Int64, $($data_type),+) => {
$crate::repeat_pat!($crate::cast::__private::DataType::Int64, $($data_type),+) => {
$m!($crate::types::Int64Type $(, $args)*)
}
$crate::repeat_pat!(arrow_schema::DataType::UInt8, $($data_type),+) => {
$crate::repeat_pat!($crate::cast::__private::DataType::UInt8, $($data_type),+) => {
$m!($crate::types::UInt8Type $(, $args)*)
}
$crate::repeat_pat!(arrow_schema::DataType::UInt16, $($data_type),+) => {
$crate::repeat_pat!($crate::cast::__private::DataType::UInt16, $($data_type),+) => {
$m!($crate::types::UInt16Type $(, $args)*)
}
$crate::repeat_pat!(arrow_schema::DataType::UInt32, $($data_type),+) => {
$crate::repeat_pat!($crate::cast::__private::DataType::UInt32, $($data_type),+) => {
$m!($crate::types::UInt32Type $(, $args)*)
}
$crate::repeat_pat!(arrow_schema::DataType::UInt64, $($data_type),+) => {
$crate::repeat_pat!($crate::cast::__private::DataType::UInt64, $($data_type),+) => {
$m!($crate::types::UInt64Type $(, $args)*)
}
$($p => $fallback,)*
Expand Down Expand Up @@ -129,13 +134,13 @@ macro_rules! downcast_integer {
macro_rules! downcast_run_end_index {
($($data_type:expr),+ => ($m:path $(, $args:tt)*), $($p:pat => $fallback:expr $(,)*)*) => {
match ($($data_type),+) {
$crate::repeat_pat!(arrow_schema::DataType::Int16, $($data_type),+) => {
$crate::repeat_pat!($crate::cast::__private::DataType::Int16, $($data_type),+) => {
$m!($crate::types::Int16Type $(, $args)*)
}
$crate::repeat_pat!(arrow_schema::DataType::Int32, $($data_type),+) => {
$crate::repeat_pat!($crate::cast::__private::DataType::Int32, $($data_type),+) => {
$m!($crate::types::Int32Type $(, $args)*)
}
$crate::repeat_pat!(arrow_schema::DataType::Int64, $($data_type),+) => {
$crate::repeat_pat!($crate::cast::__private::DataType::Int64, $($data_type),+) => {
$m!($crate::types::Int64Type $(, $args)*)
}
$($p => $fallback,)*
Expand Down Expand Up @@ -172,34 +177,34 @@ macro_rules! downcast_run_end_index {
macro_rules! downcast_temporal {
($($data_type:expr),+ => ($m:path $(, $args:tt)*), $($p:pat => $fallback:expr $(,)*)*) => {
match ($($data_type),+) {
$crate::repeat_pat!(arrow_schema::DataType::Time32(arrow_schema::TimeUnit::Second), $($data_type),+) => {
$crate::repeat_pat!($crate::cast::__private::DataType::Time32($crate::cast::__private::arrow_schema::TimeUnit::Second), $($data_type),+) => {
$m!($crate::types::Time32SecondType $(, $args)*)
}
$crate::repeat_pat!(arrow_schema::DataType::Time32(arrow_schema::TimeUnit::Millisecond), $($data_type),+) => {
$crate::repeat_pat!($crate::cast::__private::DataType::Time32($crate::cast::__private::arrow_schema::TimeUnit::Millisecond), $($data_type),+) => {
$m!($crate::types::Time32MillisecondType $(, $args)*)
}
$crate::repeat_pat!(arrow_schema::DataType::Time64(arrow_schema::TimeUnit::Microsecond), $($data_type),+) => {
$crate::repeat_pat!($crate::cast::__private::DataType::Time64($crate::cast::__private::arrow_schema::TimeUnit::Microsecond), $($data_type),+) => {
$m!($crate::types::Time64MicrosecondType $(, $args)*)
}
$crate::repeat_pat!(arrow_schema::DataType::Time64(arrow_schema::TimeUnit::Nanosecond), $($data_type),+) => {
$crate::repeat_pat!($crate::cast::__private::DataType::Time64($crate::cast::__private::arrow_schema::TimeUnit::Nanosecond), $($data_type),+) => {
$m!($crate::types::Time64NanosecondType $(, $args)*)
}
$crate::repeat_pat!(arrow_schema::DataType::Date32, $($data_type),+) => {
$crate::repeat_pat!($crate::cast::__private::DataType::Date32, $($data_type),+) => {
$m!($crate::types::Date32Type $(, $args)*)
}
$crate::repeat_pat!(arrow_schema::DataType::Date64, $($data_type),+) => {
$crate::repeat_pat!($crate::cast::__private::DataType::Date64, $($data_type),+) => {
$m!($crate::types::Date64Type $(, $args)*)
}
$crate::repeat_pat!(arrow_schema::DataType::Timestamp(arrow_schema::TimeUnit::Second, _), $($data_type),+) => {
$crate::repeat_pat!($crate::cast::__private::DataType::Timestamp($crate::cast::__private::arrow_schema::TimeUnit::Second, _), $($data_type),+) => {
$m!($crate::types::TimestampSecondType $(, $args)*)
}
$crate::repeat_pat!(arrow_schema::DataType::Timestamp(arrow_schema::TimeUnit::Millisecond, _), $($data_type),+) => {
$crate::repeat_pat!($crate::cast::__private::DataType::Timestamp($crate::cast::__private::arrow_schema::TimeUnit::Millisecond, _), $($data_type),+) => {
$m!($crate::types::TimestampMillisecondType $(, $args)*)
}
$crate::repeat_pat!(arrow_schema::DataType::Timestamp(arrow_schema::TimeUnit::Microsecond, _), $($data_type),+) => {
$crate::repeat_pat!($crate::cast::__private::DataType::Timestamp($crate::cast::__private::arrow_schema::TimeUnit::Microsecond, _), $($data_type),+) => {
$m!($crate::types::TimestampMicrosecondType $(, $args)*)
}
$crate::repeat_pat!(arrow_schema::DataType::Timestamp(arrow_schema::TimeUnit::Nanosecond, _), $($data_type),+) => {
$crate::repeat_pat!($crate::cast::__private::DataType::Timestamp($crate::cast::__private::arrow_schema::TimeUnit::Nanosecond, _), $($data_type),+) => {
$m!($crate::types::TimestampNanosecondType $(, $args)*)
}
$($p => $fallback,)*
Expand Down Expand Up @@ -284,40 +289,40 @@ macro_rules! downcast_primitive {
($($data_type:expr),+ => ($m:path $(, $args:tt)*), $($p:pat => $fallback:expr $(,)*)*) => {
$crate::downcast_integer! {
$($data_type),+ => ($m $(, $args)*),
$crate::repeat_pat!(arrow_schema::DataType::Float16, $($data_type),+) => {
$crate::repeat_pat!($crate::cast::__private::DataType::Float16, $($data_type),+) => {
$m!($crate::types::Float16Type $(, $args)*)
}
$crate::repeat_pat!(arrow_schema::DataType::Float32, $($data_type),+) => {
$crate::repeat_pat!($crate::cast::__private::DataType::Float32, $($data_type),+) => {
$m!($crate::types::Float32Type $(, $args)*)
}
$crate::repeat_pat!(arrow_schema::DataType::Float64, $($data_type),+) => {
$crate::repeat_pat!($crate::cast::__private::DataType::Float64, $($data_type),+) => {
$m!($crate::types::Float64Type $(, $args)*)
}
$crate::repeat_pat!(arrow_schema::DataType::Decimal128(_, _), $($data_type),+) => {
$crate::repeat_pat!($crate::cast::__private::DataType::Decimal128(_, _), $($data_type),+) => {
$m!($crate::types::Decimal128Type $(, $args)*)
}
$crate::repeat_pat!(arrow_schema::DataType::Decimal256(_, _), $($data_type),+) => {
$crate::repeat_pat!($crate::cast::__private::DataType::Decimal256(_, _), $($data_type),+) => {
$m!($crate::types::Decimal256Type $(, $args)*)
}
$crate::repeat_pat!(arrow_schema::DataType::Interval(arrow_schema::IntervalUnit::YearMonth), $($data_type),+) => {
$crate::repeat_pat!($crate::cast::__private::DataType::Interval($crate::cast::__private::arrow_schema::IntervalUnit::YearMonth), $($data_type),+) => {
$m!($crate::types::IntervalYearMonthType $(, $args)*)
}
$crate::repeat_pat!(arrow_schema::DataType::Interval(arrow_schema::IntervalUnit::DayTime), $($data_type),+) => {
$crate::repeat_pat!($crate::cast::__private::DataType::Interval($crate::cast::__private::arrow_schema::IntervalUnit::DayTime), $($data_type),+) => {
$m!($crate::types::IntervalDayTimeType $(, $args)*)
}
$crate::repeat_pat!(arrow_schema::DataType::Interval(arrow_schema::IntervalUnit::MonthDayNano), $($data_type),+) => {
$crate::repeat_pat!($crate::cast::__private::DataType::Interval($crate::cast::__private::arrow_schema::IntervalUnit::MonthDayNano), $($data_type),+) => {
$m!($crate::types::IntervalMonthDayNanoType $(, $args)*)
}
$crate::repeat_pat!(arrow_schema::DataType::Duration(arrow_schema::TimeUnit::Second), $($data_type),+) => {
$crate::repeat_pat!($crate::cast::__private::DataType::Duration($crate::cast::__private::arrow_schema::TimeUnit::Second), $($data_type),+) => {
$m!($crate::types::DurationSecondType $(, $args)*)
}
$crate::repeat_pat!(arrow_schema::DataType::Duration(arrow_schema::TimeUnit::Millisecond), $($data_type),+) => {
$crate::repeat_pat!($crate::cast::__private::DataType::Duration($crate::cast::__private::arrow_schema::TimeUnit::Millisecond), $($data_type),+) => {
$m!($crate::types::DurationMillisecondType $(, $args)*)
}
$crate::repeat_pat!(arrow_schema::DataType::Duration(arrow_schema::TimeUnit::Microsecond), $($data_type),+) => {
$crate::repeat_pat!($crate::cast::__private::DataType::Duration($crate::cast::__private::arrow_schema::TimeUnit::Microsecond), $($data_type),+) => {
$m!($crate::types::DurationMicrosecondType $(, $args)*)
}
$crate::repeat_pat!(arrow_schema::DataType::Duration(arrow_schema::TimeUnit::Nanosecond), $($data_type),+) => {
$crate::repeat_pat!($crate::cast::__private::DataType::Duration($crate::cast::__private::arrow_schema::TimeUnit::Nanosecond), $($data_type),+) => {
$m!($crate::types::DurationNanosecondType $(, $args)*)
}
_ => {
Expand Down Expand Up @@ -462,7 +467,7 @@ macro_rules! downcast_dictionary_array {

($values:ident => $e:block $($p:pat => $fallback:expr $(,)*)*) => {
match $values.data_type() {
arrow_schema::DataType::Dictionary(k, _) => {
$crate::cast::__private::DataType::Dictionary(k, _) => {
$crate::downcast_integer! {
k.as_ref() => ($crate::downcast_dictionary_array_helper, $values, $e),
k => unreachable!("unsupported dictionary key type: {}", k)
Expand Down Expand Up @@ -564,7 +569,7 @@ macro_rules! downcast_run_array {

($values:ident => $e:block $($p:pat => $fallback:expr $(,)*)*) => {
match $values.data_type() {
arrow_schema::DataType::RunEndEncoded(k, _) => {
$crate::cast::__private::DataType::RunEndEncoded(k, _) => {
$crate::downcast_run_end_index! {
k.data_type() => ($crate::downcast_run_array_helper, $values, $e),
k => unreachable!("unsupported run end index type: {}", k)
Expand Down

0 comments on commit 6924590

Please sign in to comment.