Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Simplified enums in DataType (#379)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecarleitao authored Sep 5, 2021
1 parent d17e41d commit 3af5ffe
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 37 deletions.
10 changes: 5 additions & 5 deletions src/compute/arithmetics/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,20 @@ fn create_scale(lhs: &DataType, rhs: &DataType) -> Result<f64> {
| (DataType::Time32(timeunit_a), DataType::Duration(timeunit_b))
| (DataType::Time64(timeunit_a), DataType::Duration(timeunit_b)) => {
// The scale is based on the TimeUnit that each of the numbers have.
timeunit_scale(timeunit_a, timeunit_b)
timeunit_scale(*timeunit_a, *timeunit_b)
}
(DataType::Date32, DataType::Duration(timeunit)) => {
// Date32 represents the time elapsed time since UNIX epoch
// (1970-01-01) in days (32 bits). The duration value has to be
// scaled to days to be able to add the value to the Date.
timeunit_scale(&TimeUnit::Second, timeunit) / SECONDS_IN_DAY as f64
timeunit_scale(TimeUnit::Second, *timeunit) / SECONDS_IN_DAY as f64
}
(DataType::Date64, DataType::Duration(timeunit)) => {
// Date64 represents the time elapsed time since UNIX epoch
// (1970-01-01) in milliseconds (64 bits). The duration value has
// to be scaled to milliseconds to be able to add the value to the
// Date.
timeunit_scale(&TimeUnit::Millisecond, timeunit)
timeunit_scale(TimeUnit::Millisecond, *timeunit)
}
_ => {
return Err(ArrowError::InvalidArgumentError(
Expand Down Expand Up @@ -216,10 +216,10 @@ pub fn subtract_timestamps(
(DataType::Timestamp(timeunit_a, None), DataType::Timestamp(timeunit_b, None)) => {
// Closure for the binary operation. The closure contains the scale
// required to calculate the difference between the timestamps.
let scale = timeunit_scale(timeunit_a, timeunit_b);
let scale = timeunit_scale(*timeunit_a, *timeunit_b);
let op = move |a, b| a - (b as f64 * scale) as i64;

binary(lhs, rhs, DataType::Duration(timeunit_a.clone()), op)
binary(lhs, rhs, DataType::Duration(*timeunit_a), op)
}
_ => Err(ArrowError::InvalidArgumentError(
"Incorrect data type for the arguments".to_string(),
Expand Down
10 changes: 5 additions & 5 deletions src/compute/cast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ fn cast_with_options(
primitive_dyn!(array, time32ms_to_time32s)
}
(Time32(from_unit), Time64(to_unit)) => {
primitive_dyn!(array, time32_to_time64, from_unit, to_unit)
primitive_dyn!(array, time32_to_time64, *from_unit, *to_unit)
}
(Time64(TimeUnit::Microsecond), Time64(TimeUnit::Nanosecond)) => {
primitive_dyn!(array, time64us_to_time64ns)
Expand All @@ -789,15 +789,15 @@ fn cast_with_options(
primitive_dyn!(array, time64ns_to_time64us)
}
(Time64(from_unit), Time32(to_unit)) => {
primitive_dyn!(array, time64_to_time32, from_unit, to_unit)
primitive_dyn!(array, time64_to_time32, *from_unit, *to_unit)
}
(Timestamp(_, _), Int64) => primitive_to_same_primitive_dyn::<i64>(array, to_type),
(Int64, Timestamp(_, _)) => primitive_to_same_primitive_dyn::<i64>(array, to_type),
(Timestamp(from_unit, tz1), Timestamp(to_unit, tz2)) if tz1 == tz2 => {
primitive_dyn!(array, timestamp_to_timestamp, from_unit, to_unit, tz2)
primitive_dyn!(array, timestamp_to_timestamp, *from_unit, *to_unit, tz2)
}
(Timestamp(from_unit, _), Date32) => primitive_dyn!(array, timestamp_to_date32, from_unit),
(Timestamp(from_unit, _), Date64) => primitive_dyn!(array, timestamp_to_date64, from_unit),
(Timestamp(from_unit, _), Date32) => primitive_dyn!(array, timestamp_to_date32, *from_unit),
(Timestamp(from_unit, _), Date64) => primitive_dyn!(array, timestamp_to_date64, *from_unit),

(Int64, Duration(_)) => primitive_to_same_primitive_dyn::<i64>(array, to_type),
(Duration(_), Int64) => primitive_to_same_primitive_dyn::<i64>(array, to_type),
Expand Down
34 changes: 12 additions & 22 deletions src/compute/cast/primitive_to.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ pub fn primitive_to_dictionary<T: NativeType + Eq + Hash, K: DictionaryKey>(
}

/// Get the time unit as a multiple of a second
const fn time_unit_multiple(unit: &TimeUnit) -> i64 {
const fn time_unit_multiple(unit: TimeUnit) -> i64 {
match unit {
TimeUnit::Second => 1,
TimeUnit::Millisecond => MILLISECONDS,
Expand Down Expand Up @@ -205,10 +205,7 @@ pub fn time64ns_to_time64us(from: &PrimitiveArray<i64>) -> PrimitiveArray<i64> {
unary(from, |x| x / 1000, DataType::Time64(TimeUnit::Microsecond))
}

pub fn timestamp_to_date64(
from: &PrimitiveArray<i64>,
from_unit: &TimeUnit,
) -> PrimitiveArray<i64> {
pub fn timestamp_to_date64(from: &PrimitiveArray<i64>, from_unit: TimeUnit) -> PrimitiveArray<i64> {
let from_size = time_unit_multiple(from_unit);
let to_size = MILLISECONDS;
let to_type = DataType::Date64;
Expand All @@ -224,53 +221,46 @@ pub fn timestamp_to_date64(
}
}

pub fn timestamp_to_date32(
from: &PrimitiveArray<i64>,
from_unit: &TimeUnit,
) -> PrimitiveArray<i32> {
pub fn timestamp_to_date32(from: &PrimitiveArray<i64>, from_unit: TimeUnit) -> PrimitiveArray<i32> {
let from_size = time_unit_multiple(from_unit) * SECONDS_IN_DAY;
unary(from, |x| (x / from_size) as i32, DataType::Date32)
}

pub fn time32_to_time64(
from: &PrimitiveArray<i32>,
from_unit: &TimeUnit,
to_unit: &TimeUnit,
from_unit: TimeUnit,
to_unit: TimeUnit,
) -> PrimitiveArray<i64> {
let from_size = time_unit_multiple(from_unit);
let to_size = time_unit_multiple(to_unit);
let divisor = to_size / from_size;
unary(
from,
|x| (x as i64 * divisor),
DataType::Time64(to_unit.clone()),
)
unary(from, |x| (x as i64 * divisor), DataType::Time64(to_unit))
}

pub fn time64_to_time32(
from: &PrimitiveArray<i64>,
from_unit: &TimeUnit,
to_unit: &TimeUnit,
from_unit: TimeUnit,
to_unit: TimeUnit,
) -> PrimitiveArray<i32> {
let from_size = time_unit_multiple(from_unit);
let to_size = time_unit_multiple(to_unit);
let divisor = from_size / to_size;
unary(
from,
|x| (x as i64 / divisor) as i32,
DataType::Time32(to_unit.clone()),
DataType::Time32(to_unit),
)
}

pub fn timestamp_to_timestamp(
from: &PrimitiveArray<i64>,
from_unit: &TimeUnit,
to_unit: &TimeUnit,
from_unit: TimeUnit,
to_unit: TimeUnit,
tz: &Option<String>,
) -> PrimitiveArray<i64> {
let from_size = time_unit_multiple(from_unit);
let to_size = time_unit_multiple(to_unit);
let to_type = DataType::Timestamp(to_unit.clone(), tz.clone());
let to_type = DataType::Timestamp(to_unit, tz.clone());
// we either divide or multiply, depending on size of each unit
if from_size >= to_size {
unary(from, |x| (x / (from_size / to_size)), to_type)
Expand Down
8 changes: 4 additions & 4 deletions src/datatypes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ impl std::fmt::Display for DataType {
}
}

/// An absolute length of time in seconds, milliseconds, microseconds or nanoseconds.
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
/// Time units defined in Arrow.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TimeUnit {
/// Time in seconds.
Second,
Expand All @@ -141,8 +141,8 @@ pub enum TimeUnit {
Nanosecond,
}

/// YEAR_MONTH or DAY_TIME interval in SQL style.
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
/// Interval units defined in Arrow
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum IntervalUnit {
/// Indicates the number of elapsed whole months, stored as 4-byte integers.
YearMonth,
Expand Down
2 changes: 1 addition & 1 deletion src/temporal_conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ pub fn timestamp_ns_to_datetime(v: i64) -> NaiveDateTime {
/// Calculates the scale factor between two TimeUnits. The function returns the
/// scale that should multiply the TimeUnit "b" to have the same time scale as
/// the TimeUnit "a".
pub fn timeunit_scale(a: &TimeUnit, b: &TimeUnit) -> f64 {
pub fn timeunit_scale(a: TimeUnit, b: TimeUnit) -> f64 {
match (a, b) {
(TimeUnit::Second, TimeUnit::Second) => 1.0,
(TimeUnit::Second, TimeUnit::Millisecond) => 0.001,
Expand Down

0 comments on commit 3af5ffe

Please sign in to comment.