Skip to content

Commit

Permalink
Fix date-part function (#4682)
Browse files Browse the repository at this point in the history
  • Loading branch information
acquamarin authored Jan 7, 2025
1 parent 69bd461 commit 59ed5a8
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 18 deletions.
10 changes: 5 additions & 5 deletions src/common/types/date_t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,21 +390,21 @@ int32_t Date::monthDays(int32_t year, int32_t month) {
return Date::isLeapYear(year) ? Date::LEAP_DAYS[month] : Date::NORMAL_DAYS[month];
}

std::string Date::getDayName(date_t& date) {
std::string Date::getDayName(date_t date) {
std::string dayNames[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
"Saturday"};
return dayNames[(date.days < 0 ? 7 - ((-date.days + 3) % 7) : ((date.days + 3) % 7) + 1) % 7];
}

std::string Date::getMonthName(date_t& date) {
std::string Date::getMonthName(date_t date) {
std::string monthNames[] = {"January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"};
int32_t year = 0, month = 0, day = 0;
Date::convert(date, year, month, day);
return monthNames[month - 1];
}

date_t Date::getLastDay(date_t& date) {
date_t Date::getLastDay(date_t date) {
int32_t year = 0, month = 0, day = 0;
Date::convert(date, year, month, day);
year += (month / 12);
Expand All @@ -413,7 +413,7 @@ date_t Date::getLastDay(date_t& date) {
return Date::fromDate(year, month, 1) - 1;
}

int32_t Date::getDatePart(DatePartSpecifier specifier, date_t& date) {
int32_t Date::getDatePart(DatePartSpecifier specifier, date_t date) {
int32_t year = 0, month = 0, day = 0;
Date::convert(date, year, month, day);
switch (specifier) {
Expand Down Expand Up @@ -446,7 +446,7 @@ int32_t Date::getDatePart(DatePartSpecifier specifier, date_t& date) {
}
}

date_t Date::trunc(DatePartSpecifier specifier, date_t& date) {
date_t Date::trunc(DatePartSpecifier specifier, date_t date) {
switch (specifier) {
case DatePartSpecifier::YEAR:
return Date::fromDate(Date::getDatePart(DatePartSpecifier::YEAR, date), 1 /* month */,
Expand Down
2 changes: 1 addition & 1 deletion src/common/types/interval_t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ void Interval::tryGetDatePartSpecifier(std::string specifier, DatePartSpecifier&
}
}

int32_t Interval::getIntervalPart(DatePartSpecifier specifier, interval_t& interval) {
int32_t Interval::getIntervalPart(DatePartSpecifier specifier, interval_t interval) {
switch (specifier) {
case DatePartSpecifier::YEAR:
return interval.months / Interval::MONTHS_PER_YEAR;
Expand Down
4 changes: 2 additions & 2 deletions src/common/types/timestamp_t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ timestamp_t Timestamp::fromEpochNanoSeconds(int64_t ns) {
return fromEpochMicroSeconds(ns / 1000);
}

int32_t Timestamp::getTimestampPart(DatePartSpecifier specifier, timestamp_t& timestamp) {
int32_t Timestamp::getTimestampPart(DatePartSpecifier specifier, timestamp_t timestamp) {
switch (specifier) {
case DatePartSpecifier::MICROSECOND:
return getTime(timestamp).micros % Interval::MICROS_PER_MINUTE;
Expand All @@ -303,7 +303,7 @@ int32_t Timestamp::getTimestampPart(DatePartSpecifier specifier, timestamp_t& ti
}
}

timestamp_t Timestamp::trunc(DatePartSpecifier specifier, timestamp_t& timestamp) {
timestamp_t Timestamp::trunc(DatePartSpecifier specifier, timestamp_t timestamp) {
int32_t hour = 0, min = 0, sec = 0, micros = 0;
date_t date;
dtime_t time;
Expand Down
10 changes: 5 additions & 5 deletions src/include/common/types/date_t.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,15 @@ class Date {

KUZU_API static int32_t monthDays(int32_t year, int32_t month);

KUZU_API static std::string getDayName(date_t& date);
KUZU_API static std::string getDayName(date_t date);

KUZU_API static std::string getMonthName(date_t& date);
KUZU_API static std::string getMonthName(date_t date);

KUZU_API static date_t getLastDay(date_t& date);
KUZU_API static date_t getLastDay(date_t date);

KUZU_API static int32_t getDatePart(DatePartSpecifier specifier, date_t& date);
KUZU_API static int32_t getDatePart(DatePartSpecifier specifier, date_t date);

KUZU_API static date_t trunc(DatePartSpecifier specifier, date_t& date);
KUZU_API static date_t trunc(DatePartSpecifier specifier, date_t date);

KUZU_API static int64_t getEpochNanoSeconds(const date_t& date);

Expand Down
2 changes: 1 addition & 1 deletion src/include/common/types/interval_t.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class Interval {
KUZU_API static void normalizeIntervalEntries(interval_t input, int64_t& months, int64_t& days,
int64_t& micros);
KUZU_API static void tryGetDatePartSpecifier(std::string specifier, DatePartSpecifier& result);
KUZU_API static int32_t getIntervalPart(DatePartSpecifier specifier, interval_t& timestamp);
KUZU_API static int32_t getIntervalPart(DatePartSpecifier specifier, interval_t timestamp);
KUZU_API static int64_t getMicro(const interval_t& val);
KUZU_API static int64_t getNanoseconds(const interval_t& val);
KUZU_API static const regex::RE2& regexPattern1();
Expand Down
4 changes: 2 additions & 2 deletions src/include/common/types/timestamp_t.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ class Timestamp {
// Create a Timestamp object from the specified epochNs.
KUZU_API static timestamp_t fromEpochNanoSeconds(int64_t ns);

KUZU_API static int32_t getTimestampPart(DatePartSpecifier specifier, timestamp_t& timestamp);
KUZU_API static int32_t getTimestampPart(DatePartSpecifier specifier, timestamp_t timestamp);

KUZU_API static timestamp_t trunc(DatePartSpecifier specifier, timestamp_t& date);
KUZU_API static timestamp_t trunc(DatePartSpecifier specifier, timestamp_t date);

KUZU_API static int64_t getEpochNanoSeconds(const timestamp_t& timestamp);

Expand Down
28 changes: 26 additions & 2 deletions test/test_files/function/date.test
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ October
October
November

-LOG StructuredDateLastDayFuncTest
-LOG DateLastDayFuncTest
-STATEMENT MATCH (p:person) RETURN last_day(p.birthdate)
---- 8
1900-01-31
Expand All @@ -145,6 +145,20 @@ November
1980-10-31
1990-11-30

-LOG LastDayInFilter
-STATEMENT MATCH (p:person) WHERE last_day(p.birthdate) > date('1940-06-30') RETURN p.birthdate
---- 5
1950-07-23
1980-10-26
1980-10-26
1980-10-26
1990-11-27

-LOG DatePartWithFilterTest
-STATEMENT MATCH (p:person) WHERE date_part("year", p.birthdate) > 1940 and date_part("year", p.birthdate) < 1976 RETURN p.fName
---- 1
Dan

-LOG StructuredDateExtractYearFuncTest
-STATEMENT MATCH (p:person) RETURN date_part("year", p.birthdate)
---- 8
Expand Down Expand Up @@ -325,7 +339,7 @@ November
1980-10-01
1990-10-01

-LOG StructuredDateTruncSecondFuncTest
-LOG DateTruncSecondFuncTest
-STATEMENT MATCH (p:person) RETURN date_trunc("second", p.birthdate)
---- 8
1900-01-01
Expand All @@ -337,6 +351,16 @@ November
1980-10-26
1990-11-27

-LOG DateTruncInFilter
-STATEMENT MATCH (p:person) where date_trunc("second", p.birthdate) <> date('1900-01-01') RETURN p.birthdate
---- 6
1940-06-22
1950-07-23
1980-10-26
1980-10-26
1980-10-26
1990-11-27

-LOG StructuredDateGreatestFuncTest
-STATEMENT MATCH (p:person) RETURN greatest(p.birthdate, date("1980-10-02"))
---- 8
Expand Down

0 comments on commit 59ed5a8

Please sign in to comment.