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

[BugFix] fix incorrect result of yearweek (backport #51065) #51243

Merged
merged 1 commit into from
Sep 21, 2024
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
11 changes: 0 additions & 11 deletions be/src/exprs/time_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -571,17 +571,6 @@ DEFINE_BINARY_FUNCTION_WITH_IMPL(year_week_with_modeImpl, t, m) {
date_value.to_date(&year, &month, &day);
uint to_year = 0;
int week = TimeFunctions::compute_week(year, month, day, TimeFunctions::week_mode(m | 2), &to_year);

if (week == 53 && day >= 29 && !(m & 4)) {
int monday_first = m & WEEK_MONDAY_FIRST;
int daynr_of_last_day = TimeFunctions::compute_daynr(year, 12, 31);
int weekday_of_last_day = TimeFunctions::compute_weekday(daynr_of_last_day, !monday_first);

if (weekday_of_last_day - monday_first < 2) {
++to_year;
week = 1;
}
}
return to_year * 100 + week;
}
DEFINE_TIME_BINARY_FN(year_week_with_mode, TYPE_DATETIME, TYPE_INT, TYPE_INT);
Expand Down
101 changes: 101 additions & 0 deletions test/sql/test_datetime/R/test_yearweek
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
-- name: test_yearweek
select yearweek('2023-12-31', 2), yearweek('2024-01-01', 2);
-- result:
202353 202353
-- !result
select yearweek('1900-01-01', 0), yearweek('1900-12-31', 0), yearweek('2000-01-01', 0), yearweek('2000-12-31', 0);
-- result:
189953 190052 199952 200053
-- !result
select yearweek('1900-01-01', 1), yearweek('1900-12-31', 1), yearweek('2000-01-01', 1), yearweek('2000-12-31', 1);
-- result:
190001 190101 199952 200052
-- !result
select yearweek('1900-01-01', 2), yearweek('1900-12-31', 2), yearweek('2000-01-01', 2), yearweek('2000-12-31', 2);
-- result:
189953 190052 199952 200053
-- !result
select yearweek('1900-01-01', 3), yearweek('1900-12-31', 3), yearweek('2000-01-01', 3), yearweek('2000-12-31', 3);
-- result:
190001 190101 199952 200052
-- !result
select yearweek('1900-01-01', 4), yearweek('1900-12-31', 4), yearweek('2000-01-01', 4), yearweek('2000-12-31', 4);
-- result:
190001 190101 199952 200101
-- !result
select yearweek('1900-01-01', 5), yearweek('1900-12-31', 5), yearweek('2000-01-01', 5), yearweek('2000-12-31', 5);
-- result:
190001 190053 199952 200052
-- !result
select yearweek('1900-01-01', 6), yearweek('1900-12-31', 6), yearweek('2000-01-01', 6), yearweek('2000-12-31', 6);
-- result:
190001 190101 199952 200101
-- !result
select yearweek('1900-01-01', 7), yearweek('1900-12-31', 7), yearweek('2000-01-01', 7), yearweek('2000-12-31', 7);
-- result:
190001 190053 199952 200052
-- !result
select yearweek('1910-01-05', 0), yearweek('1910-12-27', 0), yearweek('2010-01-05', 0), yearweek('2010-12-27', 0);
-- result:
191001 191052 201001 201052
-- !result
select yearweek('1910-01-05', 1), yearweek('1910-12-27', 1), yearweek('2010-01-05', 1), yearweek('2010-12-27', 1);
-- result:
191001 191052 201001 201052
-- !result
select yearweek('1910-01-05', 2), yearweek('1910-12-27', 2), yearweek('2010-01-05', 2), yearweek('2010-12-27', 2);
-- result:
191001 191052 201001 201052
-- !result
select yearweek('1910-01-05', 3), yearweek('1910-12-27', 3), yearweek('2010-01-05', 3), yearweek('2010-12-27', 3);
-- result:
191001 191052 201001 201052
-- !result
select yearweek('1910-01-05', 4), yearweek('1910-12-27', 4), yearweek('2010-01-05', 4), yearweek('2010-12-27', 4);
-- result:
191001 191052 201001 201052
-- !result
select yearweek('1910-01-05', 5), yearweek('1910-12-27', 5), yearweek('2010-01-05', 5), yearweek('2010-12-27', 5);
-- result:
191001 191052 201001 201052
-- !result
select yearweek('1910-01-05', 6), yearweek('1910-12-27', 6), yearweek('2010-01-05', 6), yearweek('2010-12-27', 6);
-- result:
191001 191052 201001 201052
-- !result
select yearweek('1910-01-05', 7), yearweek('1910-12-27', 7), yearweek('2010-01-05', 7), yearweek('2010-12-27', 7);
-- result:
191001 191052 201001 201052
-- !result
select yearweek('1940-01-11', 0), yearweek('1940-12-21', 0), yearweek('2040-01-11', 0), yearweek('2040-12-21', 0);
-- result:
194001 194050 204002 204051
-- !result
select yearweek('1940-01-11', 1), yearweek('1940-12-21', 1), yearweek('2040-01-11', 1), yearweek('2040-12-21', 1);
-- result:
194002 194051 204002 204051
-- !result
select yearweek('1940-01-11', 2), yearweek('1940-12-21', 2), yearweek('2040-01-11', 2), yearweek('2040-12-21', 2);
-- result:
194001 194050 204002 204051
-- !result
select yearweek('1940-01-11', 3), yearweek('1940-12-21', 3), yearweek('2040-01-11', 3), yearweek('2040-12-21', 3);
-- result:
194002 194051 204002 204051
-- !result
select yearweek('1940-01-11', 4), yearweek('1940-12-21', 4), yearweek('2040-01-11', 4), yearweek('2040-12-21', 4);
-- result:
194002 194051 204002 204051
-- !result
select yearweek('1940-01-11', 5), yearweek('1940-12-21', 5), yearweek('2040-01-11', 5), yearweek('2040-12-21', 5);
-- result:
194002 194051 204002 204051
-- !result
select yearweek('1940-01-11', 6), yearweek('1940-12-21', 6), yearweek('2040-01-11', 6), yearweek('2040-12-21', 6);
-- result:
194002 194051 204002 204051
-- !result
select yearweek('1940-01-11', 7), yearweek('1940-12-21', 7), yearweek('2040-01-11', 7), yearweek('2040-12-21', 7);
-- result:
194002 194051 204002 204051
-- !result
26 changes: 26 additions & 0 deletions test/sql/test_datetime/T/test_yearweek
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-- name: test_yearweek
select yearweek('2023-12-31', 2), yearweek('2024-01-01', 2);
select yearweek('1900-01-01', 0), yearweek('1900-12-31', 0), yearweek('2000-01-01', 0), yearweek('2000-12-31', 0);
select yearweek('1900-01-01', 1), yearweek('1900-12-31', 1), yearweek('2000-01-01', 1), yearweek('2000-12-31', 1);
select yearweek('1900-01-01', 2), yearweek('1900-12-31', 2), yearweek('2000-01-01', 2), yearweek('2000-12-31', 2);
select yearweek('1900-01-01', 3), yearweek('1900-12-31', 3), yearweek('2000-01-01', 3), yearweek('2000-12-31', 3);
select yearweek('1900-01-01', 4), yearweek('1900-12-31', 4), yearweek('2000-01-01', 4), yearweek('2000-12-31', 4);
select yearweek('1900-01-01', 5), yearweek('1900-12-31', 5), yearweek('2000-01-01', 5), yearweek('2000-12-31', 5);
select yearweek('1900-01-01', 6), yearweek('1900-12-31', 6), yearweek('2000-01-01', 6), yearweek('2000-12-31', 6);
select yearweek('1900-01-01', 7), yearweek('1900-12-31', 7), yearweek('2000-01-01', 7), yearweek('2000-12-31', 7);
select yearweek('1910-01-05', 0), yearweek('1910-12-27', 0), yearweek('2010-01-05', 0), yearweek('2010-12-27', 0);
select yearweek('1910-01-05', 1), yearweek('1910-12-27', 1), yearweek('2010-01-05', 1), yearweek('2010-12-27', 1);
select yearweek('1910-01-05', 2), yearweek('1910-12-27', 2), yearweek('2010-01-05', 2), yearweek('2010-12-27', 2);
select yearweek('1910-01-05', 3), yearweek('1910-12-27', 3), yearweek('2010-01-05', 3), yearweek('2010-12-27', 3);
select yearweek('1910-01-05', 4), yearweek('1910-12-27', 4), yearweek('2010-01-05', 4), yearweek('2010-12-27', 4);
select yearweek('1910-01-05', 5), yearweek('1910-12-27', 5), yearweek('2010-01-05', 5), yearweek('2010-12-27', 5);
select yearweek('1910-01-05', 6), yearweek('1910-12-27', 6), yearweek('2010-01-05', 6), yearweek('2010-12-27', 6);
select yearweek('1910-01-05', 7), yearweek('1910-12-27', 7), yearweek('2010-01-05', 7), yearweek('2010-12-27', 7);
select yearweek('1940-01-11', 0), yearweek('1940-12-21', 0), yearweek('2040-01-11', 0), yearweek('2040-12-21', 0);
select yearweek('1940-01-11', 1), yearweek('1940-12-21', 1), yearweek('2040-01-11', 1), yearweek('2040-12-21', 1);
select yearweek('1940-01-11', 2), yearweek('1940-12-21', 2), yearweek('2040-01-11', 2), yearweek('2040-12-21', 2);
select yearweek('1940-01-11', 3), yearweek('1940-12-21', 3), yearweek('2040-01-11', 3), yearweek('2040-12-21', 3);
select yearweek('1940-01-11', 4), yearweek('1940-12-21', 4), yearweek('2040-01-11', 4), yearweek('2040-12-21', 4);
select yearweek('1940-01-11', 5), yearweek('1940-12-21', 5), yearweek('2040-01-11', 5), yearweek('2040-12-21', 5);
select yearweek('1940-01-11', 6), yearweek('1940-12-21', 6), yearweek('2040-01-11', 6), yearweek('2040-12-21', 6);
select yearweek('1940-01-11', 7), yearweek('1940-12-21', 7), yearweek('2040-01-11', 7), yearweek('2040-12-21', 7);
Loading