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

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

package org.opensearch.sql.expression.datetime;

import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR;
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 Down Expand Up @@ -1228,30 +1227,28 @@ public void testWeekFormats(
expectedInteger);
}

// subtracting 1 as a temporary fix for year 2024.
// Issue: https://github.com/opensearch-project/sql/issues/2477
@Test
public void testWeekOfYearWithTimeType() {
LocalDate today = LocalDate.now(functionProperties.getQueryStartClock());
int week = DateTimeTestBase.getYearWeek(today);

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,13 +5,16 @@

package org.opensearch.sql.expression.datetime;

import static java.time.DayOfWeek.SUNDAY;
import static java.time.temporal.TemporalAdjusters.nextOrSame;
import static org.opensearch.sql.data.model.ExprValueUtils.fromObjectValue;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;
import java.time.temporal.Temporal;
import java.util.List;
import org.opensearch.sql.data.model.ExprDateValue;
Expand Down Expand Up @@ -231,4 +234,19 @@ protected Double unixTimeStampOf(LocalDateTime value) {
protected Double unixTimeStampOf(Instant value) {
return unixTimeStampOf(DSL.literal(new ExprTimestampValue(value))).valueOf().doubleValue();
}

// The following calculation is needed to correct the discrepancy in how ISO 860 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.
protected static int getYearWeek(LocalDate date) {
acarbonetto marked this conversation as resolved.
Show resolved Hide resolved
LocalDate firstSundayOfYear = date.withDayOfYear(1).with(nextOrSame(SUNDAY));
int week =
date.isBefore(firstSundayOfYear)
? 52
acarbonetto marked this conversation as resolved.
Show resolved Hide resolved
: (int) ChronoUnit.WEEKS.between(firstSundayOfYear, date) + 1;
return week;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@

package org.opensearch.sql.expression.datetime;

import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.opensearch.sql.data.type.ExprCoreType.LONG;

import java.time.LocalDate;
import java.time.temporal.WeekFields;
import java.util.Locale;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -96,15 +97,8 @@ public void testExtractDatePartWithTimeType() {

datePartWithTimeArgQuery("DAY", timeInput, now.getDayOfMonth());

// To avoid flaky test, skip the testing in December and January because the WEEK is ISO 8601
// week-of-week-based-year which is considered to start on a Monday and week 1 is the first week
// with >3 days. it is possible for early-January dates to be part of the 52nd or 53rd week of
// the previous year, and for late-December dates to be part of the first week of the next year.
// For example, 2005-01-02 is part of the 53rd week of year 2004, while 2012-12-31 is part of
// the first week of 2013
if (now.getMonthValue() != 1 && now.getMonthValue() != 12) {
datePartWithTimeArgQuery("WEEK", datetimeInput, now.get(ALIGNED_WEEK_OF_YEAR));
}
datePartWithTimeArgQuery(
"WEEK", timeInput, now.get(WeekFields.of(Locale.ENGLISH).weekOfYear()));

datePartWithTimeArgQuery("MONTH", timeInput, now.getMonthValue());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

package org.opensearch.sql.expression.datetime;

import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR;
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 Down Expand Up @@ -97,11 +96,10 @@ public void testYearweekWithoutMode() {
assertEquals(eval(expression), eval(expressionWithoutMode));
}

// subtracting 1 as a temporary fix for year 2024.
// 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());
int week = DateTimeTestBase.getYearWeek(today);
int year = LocalDate.now(functionProperties.getQueryStartClock()).getYear();
int expected = Integer.parseInt(String.format("%d%02d", year, week));

Expand Down
Loading