Skip to content
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

add toStartOfYear,toStartOfISOYear #1826

Merged
merged 1 commit into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions common/functions/src/scalars/dates/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ use common_exception::Result;

use super::now::NowFunction;
use super::RoundFunction;
use super::ToStartOfISOYearFunction;
use super::ToStartOfYearFunction;
use super::ToYYYYMMDDFunction;
use super::ToYYYYMMDDhhmmssFunction;
use super::ToYYYYMMFunction;
Expand All @@ -40,6 +42,11 @@ impl DateFunction {
"toYYYYMMDDhhmmss".into(),
ToYYYYMMDDhhmmssFunction::try_create,
);
map.insert("toStartOfYear".into(), ToStartOfYearFunction::try_create);
map.insert(
"toStartOfISOYear".into(),
ToStartOfISOYearFunction::try_create,
);

// rounders
{
Expand Down
2 changes: 2 additions & 0 deletions common/functions/src/scalars/dates/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ mod round_function;
mod simple_date;

pub use date::DateFunction;
pub use number_function::ToStartOfISOYearFunction;
pub use number_function::ToStartOfYearFunction;
pub use number_function::ToYYYYMMDDFunction;
pub use number_function::ToYYYYMMDDhhmmssFunction;
pub use number_function::ToYYYYMMFunction;
Expand Down
55 changes: 50 additions & 5 deletions common/functions/src/scalars/dates/number_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,49 @@ impl NumberResultFunction<u64> for ToYYYYMMDDhhmmss {
}
}

#[derive(Clone)]
pub struct ToStartOfYear;

impl NumberResultFunction<u32> for ToStartOfYear {
fn return_type() -> Result<DataType> {
Ok(DataType::Date16)
}
fn to_number(value: DateTime<Utc>) -> u32 {
let start: DateTime<Utc> = Utc.ymd(1970, 1, 1).and_hms(0, 0, 0);
let end: DateTime<Utc> = Utc.ymd(value.year(), 1, 1).and_hms(0, 0, 0);
let duration = end.signed_duration_since(start);
duration.num_days() as u32
}

fn to_constant_value(value: DateTime<Utc>) -> DataValue {
DataValue::UInt16(Some(Self::to_number(value) as u16))
}
}

#[derive(Clone)]
pub struct ToStartOfISOYear;

impl NumberResultFunction<u32> for ToStartOfISOYear {
fn return_type() -> Result<DataType> {
Ok(DataType::Date16)
}
fn to_number(value: DateTime<Utc>) -> u32 {
let start: DateTime<Utc> = Utc.ymd(1970, 1, 1).and_hms(0, 0, 0);
let week_day = value.weekday().num_days_from_monday();
let iso_week = value.iso_week();
let iso_week_num = iso_week.week();
let sub_days = (iso_week_num - 1) * 7 + week_day;
let result = value.timestamp_millis() - sub_days as i64 * 24 * 3600 * 1000;
let end: DateTime<Utc> = Utc.timestamp_millis(result);
let duration = end.signed_duration_since(start);
duration.num_days() as u32
}

fn to_constant_value(value: DateTime<Utc>) -> DataValue {
DataValue::UInt16(Some(Self::to_number(value) as u16))
}
}

impl<T, R> NumberFunction<T, R>
where
T: NumberResultFunction<R> + Clone + Sync + Send + 'static,
Expand Down Expand Up @@ -146,7 +189,7 @@ where
let date_time = Utc.timestamp(v as i64 * 24 * 3600, 0_u32);
T::to_number(date_time)
}
);
);
Ok(result.into())
}
},
Expand All @@ -163,7 +206,7 @@ where
let date_time = Utc.timestamp(v as i64 * 24 * 3600, 0_u32);
T::to_number(date_time)
}
);
);
Ok(result.into())
}
},
Expand All @@ -180,14 +223,14 @@ where
let date_time = Utc.timestamp(v as i64, 0_u32);
T::to_number(date_time)
}
);
);
Ok(result.into())
}
},
other => Result::Err(ErrorCode::IllegalDataType(format!(
"Illegal type {:?} of argument of function {}.Should be a date16/data32 or a dateTime32",
"Illegal type {:?} of argument of function {}.Should be a date16/data32 or a dateTime32",
other,
self.name()))),
self.name()))),
}?;
Ok(number_array)
}
Expand All @@ -202,3 +245,5 @@ impl<T, R> fmt::Display for NumberFunction<T, R> {
pub type ToYYYYMMFunction = NumberFunction<ToYYYYMM, u32>;
pub type ToYYYYMMDDFunction = NumberFunction<ToYYYYMMDD, u32>;
pub type ToYYYYMMDDhhmmssFunction = NumberFunction<ToYYYYMMDDhhmmss, u64>;
pub type ToStartOfISOYearFunction = NumberFunction<ToStartOfISOYear, u32>;
pub type ToStartOfYearFunction = NumberFunction<ToStartOfYear, u32>;
6 changes: 6 additions & 0 deletions tests/suites/0_stateless/02_0012_function_datetimes.result
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,9 @@
1
1
===toYYYYMMDD===
===toStartOf===
2021-01-01
2021-01-04
2021-01-01
2021-01-04
===toStartOf===
7 changes: 6 additions & 1 deletion tests/suites/0_stateless/02_0012_function_datetimes.sql
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,9 @@ select toYYYYMMDD(toDateTime(1630833797)) = 20210905;
select toYYYYMMDD(toDate(18875)) = 20210905;
select '===toYYYYMMDD===';


select '===toStartOf===';
select toStartOfYear(toDateTime(1630812366));
select toStartOfISOYear(toDateTime(1630812366));
select toStartOfYear(toDate(18869));
select toStartOfISOYear(toDate(18869));
select '===toStartOf===';