From 9e2defcc16f15e693ca3d4d8b2df904dc93b655c Mon Sep 17 00:00:00 2001 From: Lantao Jin Date: Mon, 30 Dec 2024 11:24:52 +0800 Subject: [PATCH 1/4] Fix the failure test testExtractDatePartWithTimeType() Signed-off-by: Lantao Jin --- .../org/opensearch/sql/expression/datetime/ExtractTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/test/java/org/opensearch/sql/expression/datetime/ExtractTest.java b/core/src/test/java/org/opensearch/sql/expression/datetime/ExtractTest.java index 02d50d0b59..600a365a96 100644 --- a/core/src/test/java/org/opensearch/sql/expression/datetime/ExtractTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/datetime/ExtractTest.java @@ -5,7 +5,7 @@ package org.opensearch.sql.expression.datetime; -import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR; +import static java.time.temporal.ChronoField.DAY_OF_WEEK; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.opensearch.sql.data.type.ExprCoreType.LONG; @@ -98,7 +98,7 @@ public void testExtractDatePartWithTimeType() { datePartWithTimeArgQuery( "WEEK", timeInput, - LocalDate.now(functionProperties.getQueryStartClock()).get(ALIGNED_WEEK_OF_YEAR)); + LocalDate.now(functionProperties.getQueryStartClock()).get(DAY_OF_WEEK)); datePartWithTimeArgQuery( "MONTH", timeInput, LocalDate.now(functionProperties.getQueryStartClock()).getMonthValue()); From d63d9cc6cc53bb9e0e411dac9c58ef530733b2cd Mon Sep 17 00:00:00 2001 From: Lantao Jin Date: Mon, 30 Dec 2024 12:04:22 +0800 Subject: [PATCH 2/4] skip test week with time type in Jan and Dec Signed-off-by: Lantao Jin --- .../sql/expression/datetime/ExtractTest.java | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/core/src/test/java/org/opensearch/sql/expression/datetime/ExtractTest.java b/core/src/test/java/org/opensearch/sql/expression/datetime/ExtractTest.java index 600a365a96..d7635de610 100644 --- a/core/src/test/java/org/opensearch/sql/expression/datetime/ExtractTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/datetime/ExtractTest.java @@ -5,7 +5,7 @@ package org.opensearch.sql.expression.datetime; -import static java.time.temporal.ChronoField.DAY_OF_WEEK; +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; @@ -92,19 +92,23 @@ private void datePartWithTimeArgQuery(String part, String time, long expected) { @Test public void testExtractDatePartWithTimeType() { - datePartWithTimeArgQuery( - "DAY", timeInput, LocalDate.now(functionProperties.getQueryStartClock()).getDayOfMonth()); + LocalDate now = LocalDate.now(functionProperties.getQueryStartClock()); - datePartWithTimeArgQuery( - "WEEK", - timeInput, - LocalDate.now(functionProperties.getQueryStartClock()).get(DAY_OF_WEEK)); + datePartWithTimeArgQuery("DAY", timeInput, now.getDayOfMonth()); - datePartWithTimeArgQuery( - "MONTH", timeInput, LocalDate.now(functionProperties.getQueryStartClock()).getMonthValue()); + // 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( - "YEAR", timeInput, LocalDate.now(functionProperties.getQueryStartClock()).getYear()); + datePartWithTimeArgQuery("MONTH", timeInput, now.getMonthValue()); + + datePartWithTimeArgQuery("YEAR", timeInput, now.getYear()); } @ParameterizedTest(name = "{0}") From 3f39cba8c967a65e2170fdbaf5814058e2274f5c Mon Sep 17 00:00:00 2001 From: Lantao Jin Date: Mon, 30 Dec 2024 12:17:28 +0800 Subject: [PATCH 3/4] add doc Signed-off-by: Lantao Jin --- docs/user/dql/functions.rst | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/docs/user/dql/functions.rst b/docs/user/dql/functions.rst index a347614ba4..6b39713daa 100644 --- a/docs/user/dql/functions.rst +++ b/docs/user/dql/functions.rst @@ -1944,7 +1944,7 @@ The format specifiers found in this table are the same as those found in the `DA * - DAY - %d * - WEEK - - %X + - %W * - MONTH - %m * - YEAR @@ -1984,6 +1984,10 @@ Example:: | 202302 | +------------------------------------------------+ +Notice: + +Function `extract(WEEK FROM ...)` returns the number of the ISO 8601 week-of-week-based-year. ISO week-numbering is considered to start on a Monday and week 1 is the first week with >3 days. In the ISO week-numbering system, 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. Ref https://en.wikipedia.org/wiki/ISO_week_date + FROM_DAYS --------- From 3861e5fcb217df4c9e189238c031ca5785c172c2 Mon Sep 17 00:00:00 2001 From: Lantao Jin Date: Mon, 30 Dec 2024 12:29:01 +0800 Subject: [PATCH 4/4] fix incorrect format Signed-off-by: Lantao Jin --- docs/user/dql/functions.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/user/dql/functions.rst b/docs/user/dql/functions.rst index 6b39713daa..bc967b305f 100644 --- a/docs/user/dql/functions.rst +++ b/docs/user/dql/functions.rst @@ -1944,11 +1944,11 @@ The format specifiers found in this table are the same as those found in the `DA * - DAY - %d * - WEEK - - %W + - %v * - MONTH - %m * - YEAR - - %V + - %Y * - SECOND_MICROSECOND - %s%f * - MINUTE_MICROSECOND