Skip to content

Commit e7be8ca

Browse files
Fixed DateTimeFunctionTest.testWeekOfYearWithTimeType and YearWeekTestt.testYearWeekWithTimeType Test Failures (#3235)
* Fixed DateTimeFunctionTest.testWeekOfYearWithTimeType and YearWeekTest.testYearWeekWithTimeType test failures --------- Signed-off-by: Kenrick Yap <[email protected]> Signed-off-by: Andrew Carbonetto <[email protected]> Co-authored-by: Andrew Carbonetto <[email protected]>
1 parent aa0857d commit e7be8ca

File tree

3 files changed

+41
-20
lines changed

3 files changed

+41
-20
lines changed

core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeFunctionTest.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
package org.opensearch.sql.expression.datetime;
77

8-
import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR;
8+
import static java.time.DayOfWeek.SUNDAY;
9+
import static java.time.temporal.TemporalAdjusters.nextOrSame;
910
import static org.junit.jupiter.api.Assertions.assertAll;
1011
import static org.junit.jupiter.api.Assertions.assertEquals;
1112
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -24,6 +25,7 @@
2425
import java.time.LocalDate;
2526
import java.time.LocalDateTime;
2627
import java.time.format.DateTimeFormatter;
28+
import java.time.temporal.ChronoUnit;
2729
import java.util.List;
2830
import java.util.stream.Stream;
2931
import lombok.AllArgsConstructor;
@@ -1228,30 +1230,34 @@ public void testWeekFormats(
12281230
expectedInteger);
12291231
}
12301232

1231-
// subtracting 1 as a temporary fix for year 2024.
1232-
// Issue: https://github.com/opensearch-project/sql/issues/2477
12331233
@Test
12341234
public void testWeekOfYearWithTimeType() {
1235+
LocalDate today = LocalDate.now(functionProperties.getQueryStartClock());
1236+
1237+
// week is based on the first sunday of the year
1238+
LocalDate firstSundayOfYear = today.withDayOfYear(1).with(nextOrSame(SUNDAY));
1239+
int week =
1240+
today.isBefore(firstSundayOfYear)
1241+
? 0
1242+
: (int) ChronoUnit.WEEKS.between(firstSundayOfYear, today) + 1;
1243+
12351244
assertAll(
12361245
() ->
12371246
validateStringFormat(
12381247
DSL.week(
12391248
functionProperties, DSL.literal(new ExprTimeValue("12:23:34")), DSL.literal(0)),
12401249
"week(TIME '12:23:34', 0)",
1241-
LocalDate.now(functionProperties.getQueryStartClock()).get(ALIGNED_WEEK_OF_YEAR)
1242-
- 1),
1250+
week),
12431251
() ->
12441252
validateStringFormat(
12451253
DSL.week_of_year(functionProperties, DSL.literal(new ExprTimeValue("12:23:34"))),
12461254
"week_of_year(TIME '12:23:34')",
1247-
LocalDate.now(functionProperties.getQueryStartClock()).get(ALIGNED_WEEK_OF_YEAR)
1248-
- 1),
1255+
week),
12491256
() ->
12501257
validateStringFormat(
12511258
DSL.weekofyear(functionProperties, DSL.literal(new ExprTimeValue("12:23:34"))),
12521259
"weekofyear(TIME '12:23:34')",
1253-
LocalDate.now(functionProperties.getQueryStartClock()).get(ALIGNED_WEEK_OF_YEAR)
1254-
- 1));
1260+
week));
12551261
}
12561262

12571263
@Test

core/src/test/java/org/opensearch/sql/expression/datetime/YearweekTest.java

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55

66
package org.opensearch.sql.expression.datetime;
77

8-
import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR;
8+
import static java.time.DayOfWeek.SUNDAY;
9+
import static java.time.temporal.TemporalAdjusters.nextOrSame;
910
import static org.junit.jupiter.api.Assertions.assertAll;
1011
import static org.junit.jupiter.api.Assertions.assertEquals;
1112
import static org.junit.jupiter.api.Assertions.assertThrows;
1213
import static org.opensearch.sql.data.model.ExprValueUtils.integerValue;
1314
import static org.opensearch.sql.data.type.ExprCoreType.INTEGER;
1415

1516
import java.time.LocalDate;
17+
import java.time.temporal.ChronoUnit;
1618
import java.util.stream.Stream;
1719
import org.junit.jupiter.api.Test;
1820
import org.junit.jupiter.params.ParameterizedTest;
@@ -97,13 +99,9 @@ public void testYearweekWithoutMode() {
9799
assertEquals(eval(expression), eval(expressionWithoutMode));
98100
}
99101

100-
// subtracting 1 as a temporary fix for year 2024.
101-
// Issue: https://github.com/opensearch-project/sql/issues/2477
102102
@Test
103103
public void testYearweekWithTimeType() {
104-
int week = LocalDate.now(functionProperties.getQueryStartClock()).get(ALIGNED_WEEK_OF_YEAR) - 1;
105-
int year = LocalDate.now(functionProperties.getQueryStartClock()).getYear();
106-
int expected = Integer.parseInt(String.format("%d%02d", year, week));
104+
int expected = getYearWeekBeforeSunday(LocalDate.now(functionProperties.getQueryStartClock()));
107105

108106
FunctionExpression expression =
109107
DSL.yearweek(
@@ -112,9 +110,27 @@ public void testYearweekWithTimeType() {
112110
FunctionExpression expressionWithoutMode =
113111
DSL.yearweek(functionProperties, DSL.literal(new ExprTimeValue("10:11:12")));
114112

115-
assertAll(
116-
() -> assertEquals(expected, eval(expression).integerValue()),
117-
() -> assertEquals(expected, eval(expressionWithoutMode).integerValue()));
113+
assertEquals(
114+
expected,
115+
eval(expression).integerValue(),
116+
String.format(
117+
"Expected year week: %d, got %s (test with mode)", expected, eval(expression)));
118+
assertEquals(
119+
expected,
120+
eval(expressionWithoutMode).integerValue(),
121+
String.format(
122+
"Expected year week: %d, got %s (test without mode)", expected, eval(expression)));
123+
}
124+
125+
private int getYearWeekBeforeSunday(LocalDate date) {
126+
LocalDate firstSundayOfYear = date.withDayOfYear(1).with(nextOrSame(SUNDAY));
127+
if (date.isBefore(firstSundayOfYear)) {
128+
return getYearWeekBeforeSunday(date.minusDays(1));
129+
}
130+
131+
int week = (int) ChronoUnit.WEEKS.between(firstSundayOfYear, date) + 1;
132+
int year = date.getYear();
133+
return Integer.parseInt(String.format("%d%02d", year, week));
118134
}
119135

120136
@Test

docs/user/ppl/functions/datetime.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2169,7 +2169,7 @@ YEARWEEK
21692169
Description
21702170
>>>>>>>>>>>
21712171

2172-
Usage: yearweek(date) returns the year and week for date as an integer. It accepts and optional mode arguments aligned with those available for the `WEEK`_ function.
2172+
Usage: yearweek(date[, mode]) returns the year and week for date as an integer. It accepts and optional mode arguments aligned with those available for the `WEEK`_ function.
21732173

21742174
Argument type: STRING/DATE/TIME/TIMESTAMP
21752175

@@ -2185,4 +2185,3 @@ Example::
21852185
| 202034 | 201901 |
21862186
+------------------------+---------------------------+
21872187

2188-

0 commit comments

Comments
 (0)