Skip to content

Commit

Permalink
fix clippy-too-many-args
Browse files Browse the repository at this point in the history
  • Loading branch information
akhilss99 committed Jul 25, 2024
1 parent 1d84907 commit 0687c49
Showing 1 changed file with 118 additions and 46 deletions.
164 changes: 118 additions & 46 deletions native/spark-expr/src/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,67 @@ static CAST_OPTIONS: CastOptions = CastOptions {
.with_timestamp_format(TIMESTAMP_FORMAT),
};

struct TimeStampInfo {
year: i32,
month: u32,
day: u32,
hour: u32,
minute: u32,
second: u32,
microsecond: u32,
}

impl Default for TimeStampInfo {
fn default() -> Self {
TimeStampInfo {
year: 1,
month: 1,
day: 1,
hour: 0,
minute: 0,
second: 0,
microsecond: 0,
}
}
}

impl TimeStampInfo {
pub fn with_year(&mut self, year: i32) -> &mut Self {
self.year = year;
self
}

pub fn with_month(&mut self, month: u32) -> &mut Self {
self.month = month;
self
}

pub fn with_day(&mut self, day: u32) -> &mut Self {
self.day = day;
self
}

pub fn with_hour(&mut self, hour: u32) -> &mut Self {
self.hour = hour;
self
}

pub fn with_minute(&mut self, minute: u32) -> &mut Self {
self.minute = minute;
self
}

pub fn with_second(&mut self, second: u32) -> &mut Self {
self.second = second;
self
}

pub fn with_microsecond(&mut self, microsecond: u32) -> &mut Self {
self.microsecond = microsecond;
self
}
}

#[derive(Debug, Hash)]
pub struct Cast {
pub child: Arc<dyn PhysicalExpr>,
Expand Down Expand Up @@ -1418,42 +1479,24 @@ fn timestamp_parser<T: TimeZone>(
}
}

fn parse_ymd_timestamp<T: TimeZone>(
year: i32,
month: u32,
day: u32,
tz: &T,
) -> SparkResult<Option<i64>> {
let datetime = tz.with_ymd_and_hms(year, month, day, 0, 0, 0);

// Check if datetime is not None
let datetime_with_tz = match datetime.single() {
Some(dt) => dt.with_timezone(tz),
None => {
return Err(SparkError::Internal(
"Failed to parse timestamp".to_string(),
));
}
};

Ok(Some(datetime_with_tz.timestamp_micros()))
}

fn parse_hms_timestamp<T: TimeZone>(
year: i32,
month: u32,
day: u32,
hour: u32,
minute: u32,
second: u32,
microsecond: u32,
fn parse_timestamp_to_micros<T: TimeZone>(
timestamp_info: &TimeStampInfo,
tz: &T,
) -> SparkResult<Option<i64>> {
let datetime = tz.with_ymd_and_hms(year, month, day, hour, minute, second);
let datetime = tz.with_ymd_and_hms(
timestamp_info.year,
timestamp_info.month,
timestamp_info.day,
timestamp_info.hour,
timestamp_info.minute,
timestamp_info.second,
);

// Check if datetime is not None
let tz_datetime = match datetime.single() {
Some(dt) => dt.with_timezone(tz).with_nanosecond(microsecond * 1000),
Some(dt) => dt
.with_timezone(tz)
.with_nanosecond(timestamp_info.microsecond * 1000),
None => {
return Err(SparkError::Internal(
"Failed to parse timestamp".to_string(),
Expand Down Expand Up @@ -1489,22 +1532,51 @@ fn get_timestamp_values<T: TimeZone>(
let second = values.get(5).map_or(0, |s| s.parse::<u32>().unwrap_or(0));
let microsecond = values.get(6).map_or(0, |ms| ms.parse::<u32>().unwrap_or(0));

match timestamp_type {
"year" => parse_ymd_timestamp(year, 1, 1, tz),
"month" => parse_ymd_timestamp(year, month, 1, tz),
"day" => parse_ymd_timestamp(year, month, day, tz),
"hour" => parse_hms_timestamp(year, month, day, hour, 0, 0, 0, tz),
"minute" => parse_hms_timestamp(year, month, day, hour, minute, 0, 0, tz),
"second" => parse_hms_timestamp(year, month, day, hour, minute, second, 0, tz),
"microsecond" => {
parse_hms_timestamp(year, month, day, hour, minute, second, microsecond, tz)
let mut timestamp_info = TimeStampInfo::default();

let timestamp_info = match timestamp_type {
"year" => timestamp_info.with_year(year),
"month" => timestamp_info.with_year(year).with_month(month),
"day" => timestamp_info
.with_year(year)
.with_month(month)
.with_day(day),
"hour" => timestamp_info
.with_year(year)
.with_month(month)
.with_day(day)
.with_hour(hour),
"minute" => timestamp_info
.with_year(year)
.with_month(month)
.with_day(day)
.with_hour(hour)
.with_minute(minute),
"second" => timestamp_info
.with_year(year)
.with_month(month)
.with_day(day)
.with_hour(hour)
.with_minute(minute)
.with_second(second),
"microsecond" => timestamp_info
.with_year(year)
.with_month(month)
.with_day(day)
.with_hour(hour)
.with_minute(minute)
.with_second(second)
.with_microsecond(microsecond),
_ => {
return Err(SparkError::CastInvalidValue {
value: value.to_string(),
from_type: "STRING".to_string(),
to_type: "TIMESTAMP".to_string(),
})
}
_ => Err(SparkError::CastInvalidValue {
value: value.to_string(),
from_type: "STRING".to_string(),
to_type: "TIMESTAMP".to_string(),
}),
}
};

parse_timestamp_to_micros(timestamp_info, tz)
}

fn parse_str_to_year_timestamp<T: TimeZone>(value: &str, tz: &T) -> SparkResult<Option<i64>> {
Expand Down

0 comments on commit 0687c49

Please sign in to comment.