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

Fixed DateTimeFunctionTest.testWeekOfYearWithTimeType and YearWeekTestt.testYearWeekWithTimeType Test Failures #3235

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

package org.opensearch.sql.expression.datetime;

import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR;
import static java.time.DayOfWeek.SUNDAY;
import static java.time.temporal.TemporalAdjusters.nextOrSame;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand All @@ -24,6 +25,7 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.stream.Stream;
import lombok.AllArgsConstructor;
Expand Down Expand Up @@ -1232,26 +1234,37 @@ public void testWeekFormats(
// Issue: https://github.com/opensearch-project/sql/issues/2477
@Test
public void testWeekOfYearWithTimeType() {
// The following calculation is needed to correct the discrepancy in how ISO 8601
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this logic to a common test util class to avoid duplication?

Unfortunately, neither YearweekTest nor DateTimeFunctionTest extends DateTimeTestBase. But I think that might be a good place to move the function (maybe as a static method?) unless you can find a better place.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed comment

// and our implementation calculates. ISO 8601 calculates weeks using the following criteria:
// - Weeks start on Monday
// - The first week of a year is any week containing 4 or more days in the new year.
// Whereas we only count full weeks, where weeks start on Sunday. To fix the discrepancy we find
// the first
// Sunday of the year and start counting weeks from that date.
LocalDate today = LocalDate.now(functionProperties.getQueryStartClock());
LocalDate firstSundayOfYear = today.withDayOfYear(1).with(nextOrSame(SUNDAY));
int week =
today.isBefore(firstSundayOfYear)
? 52
: (int) ChronoUnit.WEEKS.between(firstSundayOfYear, today) + 1;

assertAll(
() ->
validateStringFormat(
DSL.week(
functionProperties, DSL.literal(new ExprTimeValue("12:23:34")), DSL.literal(0)),
"week(TIME '12:23:34', 0)",
LocalDate.now(functionProperties.getQueryStartClock()).get(ALIGNED_WEEK_OF_YEAR)
- 1),
week),
() ->
validateStringFormat(
DSL.week_of_year(functionProperties, DSL.literal(new ExprTimeValue("12:23:34"))),
"week_of_year(TIME '12:23:34')",
LocalDate.now(functionProperties.getQueryStartClock()).get(ALIGNED_WEEK_OF_YEAR)
- 1),
week),
() ->
validateStringFormat(
DSL.weekofyear(functionProperties, DSL.literal(new ExprTimeValue("12:23:34"))),
"weekofyear(TIME '12:23:34')",
LocalDate.now(functionProperties.getQueryStartClock()).get(ALIGNED_WEEK_OF_YEAR)
- 1));
week));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@

package org.opensearch.sql.expression.datetime;

import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR;
import static java.time.DayOfWeek.SUNDAY;
import static java.time.temporal.TemporalAdjusters.nextOrSame;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.opensearch.sql.data.model.ExprValueUtils.integerValue;
import static org.opensearch.sql.data.type.ExprCoreType.INTEGER;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -101,7 +103,22 @@ public void testYearweekWithoutMode() {
// Issue: https://github.com/opensearch-project/sql/issues/2477
@Test
public void testYearweekWithTimeType() {
int week = LocalDate.now(functionProperties.getQueryStartClock()).get(ALIGNED_WEEK_OF_YEAR) - 1;
LocalDate today = LocalDate.now(functionProperties.getQueryStartClock());

// The following calculation is needed to correct the discrepancy in how ISO 8601
// and our implementation of YEARWEEK calculates. ISO 8601 calculates weeks using the following
// criteria:
// - Weeks start on Monday
// - The first week of a year is any week containing 4 or more days in the new year.
// Whereas YEARWEEK counts only full weeks, where weeks start on Sunday. To fix the discrepancy
// we find the first
// Sunday of the year and start counting weeks from that date.
LocalDate firstSundayOfYear = today.withDayOfYear(1).with(nextOrSame(SUNDAY));
int week =
today.isBefore(firstSundayOfYear)
? 52
: (int) ChronoUnit.WEEKS.between(firstSundayOfYear, today) + 1;

int year = LocalDate.now(functionProperties.getQueryStartClock()).getYear();
int expected = Integer.parseInt(String.format("%d%02d", year, week));

Expand Down
Loading