Skip to content

Commit 4b28d4d

Browse files
Fixed DateTimeFunctionTest.testWeekOfYearWithTimeType and YearWeekTestt.testYearWeekWithTimeType Test Failures (#3235)
* Fixed DateTimeFunctionTest.testWeekOfYearWithTimeType and YearWeekTest.testYearWeekWithTimeType test failures --------- Signed-off-by: Kenrick Yap <kenrick.yap@improving.com> Signed-off-by: Andrew Carbonetto <andrew.carbonetto@improving.com> Co-authored-by: Andrew Carbonetto <andrew.carbonetto@improving.com> (cherry picked from commit e7be8ca) Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent df5afd7 commit 4b28d4d

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;
@@ -1283,30 +1285,34 @@ public void testWeekFormats(
12831285
expectedInteger);
12841286
}
12851287

1286-
// subtracting 1 as a temporary fix for year 2024.
1287-
// Issue: https://github.com/opensearch-project/sql/issues/2477
12881288
@Test
12891289
public void testWeekOfYearWithTimeType() {
1290+
LocalDate today = LocalDate.now(functionProperties.getQueryStartClock());
1291+
1292+
// week is based on the first sunday of the year
1293+
LocalDate firstSundayOfYear = today.withDayOfYear(1).with(nextOrSame(SUNDAY));
1294+
int week =
1295+
today.isBefore(firstSundayOfYear)
1296+
? 0
1297+
: (int) ChronoUnit.WEEKS.between(firstSundayOfYear, today) + 1;
1298+
12901299
assertAll(
12911300
() ->
12921301
validateStringFormat(
12931302
DSL.week(
12941303
functionProperties, DSL.literal(new ExprTimeValue("12:23:34")), DSL.literal(0)),
12951304
"week(TIME '12:23:34', 0)",
1296-
LocalDate.now(functionProperties.getQueryStartClock()).get(ALIGNED_WEEK_OF_YEAR)
1297-
- 1),
1305+
week),
12981306
() ->
12991307
validateStringFormat(
13001308
DSL.week_of_year(functionProperties, DSL.literal(new ExprTimeValue("12:23:34"))),
13011309
"week_of_year(TIME '12:23:34')",
1302-
LocalDate.now(functionProperties.getQueryStartClock()).get(ALIGNED_WEEK_OF_YEAR)
1303-
- 1),
1310+
week),
13041311
() ->
13051312
validateStringFormat(
13061313
DSL.weekofyear(functionProperties, DSL.literal(new ExprTimeValue("12:23:34"))),
13071314
"weekofyear(TIME '12:23:34')",
1308-
LocalDate.now(functionProperties.getQueryStartClock()).get(ALIGNED_WEEK_OF_YEAR)
1309-
- 1));
1315+
week));
13101316
}
13111317

13121318
@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
@@ -2180,7 +2180,7 @@ YEARWEEK
21802180
Description
21812181
>>>>>>>>>>>
21822182

2183-
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.
2183+
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.
21842184

21852185
Argument type: STRING/DATE/DATETIME/TIME/TIMESTAMP
21862186

@@ -2196,4 +2196,3 @@ Example::
21962196
| 202034 | 201901 |
21972197
+------------------------+---------------------------+
21982198

2199-

0 commit comments

Comments
 (0)