Skip to content

Commit

Permalink
optimize the function performance
Browse files Browse the repository at this point in the history
  • Loading branch information
kche0169 committed Jan 16, 2025
1 parent bdcfb42 commit 3683314
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 16 deletions.
8 changes: 3 additions & 5 deletions src/function/scalar/day_of_month.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,10 @@ struct DayOfMonthFunction {

template <>
inline bool DayOfMonthFunction::Run(DateT left, BigIntT &result) {
auto given_year = DateT::GetDatePart(left, TimeUnit::kYear);
auto given_month = DateT::GetDatePart(left, TimeUnit::kMonth);
auto given_day = DateT::GetDatePart(left, TimeUnit::kDay);
year_month_day ymd{year(given_year), month(given_month), day(given_day)};
year_month_day ymd;
DateT::OuterDate2YMD(left, ymd);
sys_days sd = sys_days(ymd);
year_month_day start{year(given_year), month(given_month), day(1)};
year_month_day start{ymd.year(), ymd.month(), day(1)};
sys_days start_sd = sys_days(start);
auto days_diff = sd - start_sd;
result = days_diff.count() + 1;
Expand Down
6 changes: 2 additions & 4 deletions src/function/scalar/day_of_week.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ struct DayOfWeekFunction {

template <>
inline bool DayOfWeekFunction::Run(DateT left, BigIntT &result) {
auto given_year = DateT::GetDatePart(left, TimeUnit::kYear);
auto given_month = DateT::GetDatePart(left, TimeUnit::kMonth);
auto given_day = DateT::GetDatePart(left, TimeUnit::kDay);
year_month_day ymd{year(given_year), month(given_month), day(given_day)};
year_month_day ymd;
DateT::OuterDate2YMD(left, ymd);
weekday wd = weekday{ymd};
days diff = (wd - weekday{0}) % days{7};
sys_days ymd_sys_days = sys_days(ymd);
Expand Down
8 changes: 3 additions & 5 deletions src/function/scalar/day_of_year.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,10 @@ struct DayOfYearFunction {

template <>
inline bool DayOfYearFunction::Run(DateT left, BigIntT &result) {
auto given_year = DateT::GetDatePart(left, TimeUnit::kYear);
auto given_month = DateT::GetDatePart(left, TimeUnit::kMonth);
auto given_day = DateT::GetDatePart(left, TimeUnit::kDay);
year_month_day ymd{year(given_year), month(given_month), day(given_day)};
year_month_day ymd;
DateT::OuterDate2YMD(left, ymd);
sys_days sd = sys_days(ymd);
year_month_day start{year(given_year), month(1), day(1)};
year_month_day start{ymd.year(), month(1), day(1)};
sys_days start_sd = sys_days(start);
auto days_diff = sd - start_sd;
result = days_diff.count() + 1;
Expand Down
4 changes: 3 additions & 1 deletion src/parser/type/datetime/date_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,5 +414,7 @@ int64_t DateType::GetDatePart(DateType input, TimeUnit unit) {
}
return -1;
}

bool DateType::OuterDate2YMD(DateType input, int32_t &year, int32_t &month, int32_t &day) {
return Date2YMD(input, year, month, day);
}
} // namespace infinity
2 changes: 2 additions & 0 deletions src/parser/type/datetime/date_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ struct DateType {
static bool Subtract(DateType input, IntervalType interval, DateType &output);

static int64_t GetDatePart(DateType input, TimeUnit unit);

static bool OuterDate2YMD(DateType input, int32_t &year, int32_t &month, int32_t &day);
};

} // namespace infinity
4 changes: 3 additions & 1 deletion src/parser/type/datetime/date_type_std.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,5 +216,7 @@ int64_t DateTypeStd::GetDatePart(DateTypeStd input, TimeUnit unit) {
}
return -1;
}

bool DateTypeStd::OuterDate2YMD(int32_t days, std::chrono::year_month_day &ymd) {
return Date2YMD( days, ymd);
}
} // namespace infinity
2 changes: 2 additions & 0 deletions src/parser/type/datetime/date_type_std.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ struct DateTypeStd {
static bool Subtract(DateTypeStd input, IntervalType interval, DateTypeStd &output);

static int64_t GetDatePart(DateTypeStd input, TimeUnit unit);

static bool OuterDate2YMD(int32_t days, std::chrono::year_month_day &ymd);
};

} // namespace infinity

0 comments on commit 3683314

Please sign in to comment.