From 5760a6bda2e1783810c0f51373fc8a7841cd5ee3 Mon Sep 17 00:00:00 2001 From: GabeFernandez310 Date: Fri, 18 Nov 2022 08:03:23 -0800 Subject: [PATCH 01/18] Added Tests Signed-off-by: GabeFernandez310 --- .../datetime/DateTimeFunctionTest.java | 22 +++++++++++++++++++ .../sql/sql/DateTimeFunctionIT.java | 11 ++++++++++ 2 files changed, 33 insertions(+) diff --git a/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeFunctionTest.java b/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeFunctionTest.java index 28a7113ca9..cc692bd008 100644 --- a/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeFunctionTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeFunctionTest.java @@ -479,6 +479,28 @@ public void dayOfYear() { assertEquals(integerValue(220), eval(expression)); } + @Test + public void day_Of_Year() { + when(nullRef.type()).thenReturn(DATE); + when(missingRef.type()).thenReturn(DATE); + assertEquals(nullValue(), eval(dsl.day_of_year(nullRef))); + assertEquals(missingValue(), eval(dsl.day_of_year(missingRef))); + + FunctionExpression expression = dsl.day_of_year(DSL.literal(new ExprDateValue("2020-08-07"))); + assertEquals(INTEGER, expression.type()); + assertEquals("day_of_year(DATE '2020-08-07')", expression.toString()); + assertEquals(integerValue(220), eval(expression)); + + expression = dsl.day_of_year(DSL.literal("2020-08-07")); + assertEquals(INTEGER, expression.type()); + assertEquals("day_of_year(\"2020-08-07\")", expression.toString()); + assertEquals(integerValue(220), eval(expression)); + + expression = dsl.day_of_year(DSL.literal("2020-08-07 01:02:03")); + assertEquals(INTEGER, expression.type()); + assertEquals("day_of_year(\"2020-08-07 01:02:03\")", expression.toString()); + assertEquals(integerValue(220), eval(expression)); + } @Test public void from_days() { when(nullRef.type()).thenReturn(LONG); diff --git a/integ-test/src/test/java/org/opensearch/sql/sql/DateTimeFunctionIT.java b/integ-test/src/test/java/org/opensearch/sql/sql/DateTimeFunctionIT.java index 8c47966e52..d09f9b1a2b 100644 --- a/integ-test/src/test/java/org/opensearch/sql/sql/DateTimeFunctionIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/sql/DateTimeFunctionIT.java @@ -225,6 +225,17 @@ public void testDayOfYear() throws IOException { verifyDataRows(result, rows(260)); } + @Test + public void testDay_Of_Year() throws IOException { + JSONObject result = executeQuery("select day_of_year(date('2020-09-16'))"); + verifySchema(result, schema("day_of_year(date('2020-09-16'))", null, "integer")); + verifyDataRows(result, rows(260)); + + result = executeQuery("select day_of_year('2020-09-16')"); + verifySchema(result, schema("day_of_year('2020-09-16')", null, "integer")); + verifyDataRows(result, rows(260)); + } + @Test public void testFromDays() throws IOException { JSONObject result = executeQuery("select from_days(738049)"); From 97669b18c6aa7571d260dc853c28694373bcc629 Mon Sep 17 00:00:00 2001 From: GabeFernandez310 Date: Fri, 18 Nov 2022 08:07:49 -0800 Subject: [PATCH 02/18] Modified Docs Signed-off-by: GabeFernandez310 --- docs/user/dql/functions.rst | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/docs/user/dql/functions.rst b/docs/user/dql/functions.rst index 788cac0433..330de6551a 100644 --- a/docs/user/dql/functions.rst +++ b/docs/user/dql/functions.rst @@ -1442,7 +1442,7 @@ DAYOFYEAR Description >>>>>>>>>>> -Usage: dayofyear(date) returns the day of the year for date, in the range 1 to 366. +Usage: dayofyear(date) returns the day of the year for date, in the range 1 to 366. Also supports day_of_year(date) as alternate syntax. Argument type: STRING/DATE/DATETIME/TIMESTAMP @@ -1458,6 +1458,14 @@ Example:: | 239 | +---------------------------------+ + os> SELECT DAY_OF_YEAR(DATE('2020-08-26')) + fetched rows / total rows = 1/1 + +---------------------------------+ + | DAY_OF_YEAR(DATE('2020-08-26')) | + |---------------------------------| + | 239 | + +---------------------------------+ + FROM_DAYS --------- From 740a0bf24cb0e06f7649d9d3578cf6919310675d Mon Sep 17 00:00:00 2001 From: GabeFernandez310 Date: Fri, 18 Nov 2022 08:13:55 -0800 Subject: [PATCH 03/18] Added Unit Test Signed-off-by: GabeFernandez310 --- .../org/opensearch/sql/sql/antlr/SQLSyntaxParserTest.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/sql/src/test/java/org/opensearch/sql/sql/antlr/SQLSyntaxParserTest.java b/sql/src/test/java/org/opensearch/sql/sql/antlr/SQLSyntaxParserTest.java index 6b78376d45..9c926cfb75 100644 --- a/sql/src/test/java/org/opensearch/sql/sql/antlr/SQLSyntaxParserTest.java +++ b/sql/src/test/java/org/opensearch/sql/sql/antlr/SQLSyntaxParserTest.java @@ -191,6 +191,12 @@ public void can_parse_now_like_functions(String name, Boolean hasFsp, Boolean ha assertNotNull(parser.parse("SELECT id FROM test WHERE " + String.join(" AND ", calls))); } + @Test + public void can_parse_dayofyear_functions() { + assertNotNull(parser.parse("SELECT dayofyear('2022-11-18')")); + assertNotNull(parser.parse("SELECT day_of_year('2022-11-18')")); + } + @Test public void can_parse_multi_match_relevance_function() { assertNotNull(parser.parse( From 0c6dcae5154658d2e938f20243ad7e906aa4068b Mon Sep 17 00:00:00 2001 From: GabeFernandez310 Date: Fri, 18 Nov 2022 12:20:04 -0800 Subject: [PATCH 04/18] Added Implementation Of Day_Of_Year Function Signed-off-by: GabeFernandez310 --- .../main/java/org/opensearch/sql/expression/DSL.java | 5 ++++- .../sql/expression/datetime/DateTimeFunction.java | 7 ++++--- .../sql/expression/function/BuiltinFunctionName.java | 1 + .../sql/expression/datetime/DateTimeFunctionTest.java | 10 +++++----- sql/src/main/antlr/OpenSearchSQLParser.g4 | 2 +- 5 files changed, 15 insertions(+), 10 deletions(-) diff --git a/core/src/main/java/org/opensearch/sql/expression/DSL.java b/core/src/main/java/org/opensearch/sql/expression/DSL.java index 4ba4f4fe45..17896af953 100644 --- a/core/src/main/java/org/opensearch/sql/expression/DSL.java +++ b/core/src/main/java/org/opensearch/sql/expression/DSL.java @@ -325,10 +325,13 @@ public static FunctionExpression dayofyear(Expression... expressions) { return compile(BuiltinFunctionName.DAYOFYEAR, expressions); } + public static FunctionExpression day_of_year(Expression... expressions) { + return compile(BuiltinFunctionName.DAY_OF_YEAR, expressions); + } + public static FunctionExpression from_days(Expression... expressions) { return compile(BuiltinFunctionName.FROM_DAYS, expressions); } - public static FunctionExpression hour(Expression... expressions) { return compile(BuiltinFunctionName.HOUR, expressions); } diff --git a/core/src/main/java/org/opensearch/sql/expression/datetime/DateTimeFunction.java b/core/src/main/java/org/opensearch/sql/expression/datetime/DateTimeFunction.java index 42274c0756..d8c6765464 100644 --- a/core/src/main/java/org/opensearch/sql/expression/datetime/DateTimeFunction.java +++ b/core/src/main/java/org/opensearch/sql/expression/datetime/DateTimeFunction.java @@ -99,7 +99,8 @@ public void register(BuiltinFunctionRepository repository) { repository.register(dayName()); repository.register(dayOfMonth()); repository.register(dayOfWeek()); - repository.register(dayOfYear()); + repository.register(dayOfYear(BuiltinFunctionName.DAYOFYEAR)); + repository.register(dayOfYear(BuiltinFunctionName.DAY_OF_YEAR)); repository.register(from_days()); repository.register(from_unixtime()); repository.register(hour()); @@ -319,8 +320,8 @@ private DefaultFunctionResolver dayOfWeek() { * DAYOFYEAR(STRING/DATE/DATETIME/TIMESTAMP). * return the day of the year for date (1-366). */ - private DefaultFunctionResolver dayOfYear() { - return define(BuiltinFunctionName.DAYOFYEAR.getName(), + private DefaultFunctionResolver dayOfYear(BuiltinFunctionName dayOfYear) { + return define(dayOfYear.getName(), impl(nullMissingHandling(DateTimeFunction::exprDayOfYear), INTEGER, DATE), impl(nullMissingHandling(DateTimeFunction::exprDayOfYear), INTEGER, DATETIME), impl(nullMissingHandling(DateTimeFunction::exprDayOfYear), INTEGER, TIMESTAMP), diff --git a/core/src/main/java/org/opensearch/sql/expression/function/BuiltinFunctionName.java b/core/src/main/java/org/opensearch/sql/expression/function/BuiltinFunctionName.java index 51d91eb372..1ef558661a 100644 --- a/core/src/main/java/org/opensearch/sql/expression/function/BuiltinFunctionName.java +++ b/core/src/main/java/org/opensearch/sql/expression/function/BuiltinFunctionName.java @@ -68,6 +68,7 @@ public enum BuiltinFunctionName { DAYOFMONTH(FunctionName.of("dayofmonth")), DAYOFWEEK(FunctionName.of("dayofweek")), DAYOFYEAR(FunctionName.of("dayofyear")), + DAY_OF_YEAR(FunctionName.of("day_of_year")), FROM_DAYS(FunctionName.of("from_days")), FROM_UNIXTIME(FunctionName.of("from_unixtime")), HOUR(FunctionName.of("hour")), diff --git a/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeFunctionTest.java b/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeFunctionTest.java index cc692bd008..62f59cf161 100644 --- a/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeFunctionTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeFunctionTest.java @@ -483,20 +483,20 @@ public void dayOfYear() { public void day_Of_Year() { when(nullRef.type()).thenReturn(DATE); when(missingRef.type()).thenReturn(DATE); - assertEquals(nullValue(), eval(dsl.day_of_year(nullRef))); - assertEquals(missingValue(), eval(dsl.day_of_year(missingRef))); + assertEquals(nullValue(), eval(DSL.day_of_year(nullRef))); + assertEquals(missingValue(), eval(DSL.day_of_year(missingRef))); - FunctionExpression expression = dsl.day_of_year(DSL.literal(new ExprDateValue("2020-08-07"))); + FunctionExpression expression = DSL.day_of_year(DSL.literal(new ExprDateValue("2020-08-07"))); assertEquals(INTEGER, expression.type()); assertEquals("day_of_year(DATE '2020-08-07')", expression.toString()); assertEquals(integerValue(220), eval(expression)); - expression = dsl.day_of_year(DSL.literal("2020-08-07")); + expression = DSL.day_of_year(DSL.literal("2020-08-07")); assertEquals(INTEGER, expression.type()); assertEquals("day_of_year(\"2020-08-07\")", expression.toString()); assertEquals(integerValue(220), eval(expression)); - expression = dsl.day_of_year(DSL.literal("2020-08-07 01:02:03")); + expression = DSL.day_of_year(DSL.literal("2020-08-07 01:02:03")); assertEquals(INTEGER, expression.type()); assertEquals("day_of_year(\"2020-08-07 01:02:03\")", expression.toString()); assertEquals(integerValue(220), eval(expression)); diff --git a/sql/src/main/antlr/OpenSearchSQLParser.g4 b/sql/src/main/antlr/OpenSearchSQLParser.g4 index c803f2b5c3..0969c15fd0 100644 --- a/sql/src/main/antlr/OpenSearchSQLParser.g4 +++ b/sql/src/main/antlr/OpenSearchSQLParser.g4 @@ -394,7 +394,7 @@ trigonometricFunctionName dateTimeFunctionName : ADDDATE | CONVERT_TZ | DATE | DATE_ADD | DATE_FORMAT | DATE_SUB - | DATETIME | DAY | DAYNAME | DAYOFMONTH | DAYOFWEEK | DAYOFYEAR | FROM_DAYS | FROM_UNIXTIME + | DATETIME | DAY | DAYNAME | DAYOFMONTH | DAYOFWEEK | DAYOFYEAR | DAY_OF_YEAR | FROM_DAYS | FROM_UNIXTIME | HOUR | MAKEDATE | MAKETIME | MICROSECOND | MINUTE | MONTH | MONTHNAME | PERIOD_ADD | PERIOD_DIFF | QUARTER | SECOND | SUBDATE | SYSDATE | TIME | TIME_TO_SEC | TIMESTAMP | TO_DAYS | UNIX_TIMESTAMP | WEEK | YEAR From 3f3c8666cedc0b2387cb8fa2ab38711fbde3c28f Mon Sep 17 00:00:00 2001 From: GabeFernandez310 Date: Tue, 22 Nov 2022 08:20:31 -0800 Subject: [PATCH 05/18] Fixed Checkstyle Error Signed-off-by: GabeFernandez310 --- core/src/main/java/org/opensearch/sql/expression/DSL.java | 1 + 1 file changed, 1 insertion(+) diff --git a/core/src/main/java/org/opensearch/sql/expression/DSL.java b/core/src/main/java/org/opensearch/sql/expression/DSL.java index 17896af953..cb593f607d 100644 --- a/core/src/main/java/org/opensearch/sql/expression/DSL.java +++ b/core/src/main/java/org/opensearch/sql/expression/DSL.java @@ -332,6 +332,7 @@ public static FunctionExpression day_of_year(Expression... expressions) { public static FunctionExpression from_days(Expression... expressions) { return compile(BuiltinFunctionName.FROM_DAYS, expressions); } + public static FunctionExpression hour(Expression... expressions) { return compile(BuiltinFunctionName.HOUR, expressions); } From d2c9b361879f8b44e78b4efdb6d526aaef7228d1 Mon Sep 17 00:00:00 2001 From: GabeFernandez310 Date: Tue, 22 Nov 2022 10:54:46 -0800 Subject: [PATCH 06/18] Fixed Doctests Signed-off-by: GabeFernandez310 --- docs/user/dql/functions.rst | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/user/dql/functions.rst b/docs/user/dql/functions.rst index 330de6551a..b5588822b8 100644 --- a/docs/user/dql/functions.rst +++ b/docs/user/dql/functions.rst @@ -1460,11 +1460,11 @@ Example:: os> SELECT DAY_OF_YEAR(DATE('2020-08-26')) fetched rows / total rows = 1/1 - +---------------------------------+ - | DAY_OF_YEAR(DATE('2020-08-26')) | - |---------------------------------| - | 239 | - +---------------------------------+ + +-----------------------------------+ + | DAY_OF_YEAR(DATE('2020-08-26')) | + |-----------------------------------| + | 239 | + +-----------------------------------+ FROM_DAYS From 11fecf1cc71be617ba6c585aa2d2566eef2cdac2 Mon Sep 17 00:00:00 2001 From: GabeFernandez310 Date: Wed, 23 Nov 2022 07:40:40 -0800 Subject: [PATCH 07/18] Fixed Checkystyle Error Signed-off-by: GabeFernandez310 --- .../opensearch/sql/expression/datetime/DateTimeFunctionTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeFunctionTest.java b/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeFunctionTest.java index 62f59cf161..02cebeba80 100644 --- a/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeFunctionTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeFunctionTest.java @@ -501,6 +501,7 @@ public void day_Of_Year() { assertEquals("day_of_year(\"2020-08-07 01:02:03\")", expression.toString()); assertEquals(integerValue(220), eval(expression)); } + @Test public void from_days() { when(nullRef.type()).thenReturn(LONG); From 09e5a3637655c6deb1f65a9282291c8cce0e4a4e Mon Sep 17 00:00:00 2001 From: GabeFernandez310 Date: Mon, 28 Nov 2022 09:41:14 -0800 Subject: [PATCH 08/18] Added Test Cases Surrounding Leap Years Signed-off-by: GabeFernandez310 --- .../datetime/DateTimeFunctionTest.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeFunctionTest.java b/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeFunctionTest.java index 02cebeba80..f15278ecb1 100644 --- a/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeFunctionTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeFunctionTest.java @@ -479,6 +479,12 @@ public void dayOfYear() { assertEquals(integerValue(220), eval(expression)); } + public void test_day_of_year(String date, int dayOfYear) { + FunctionExpression expression = DSL.day_of_year(DSL.literal(new ExprDateValue(date))); + assertEquals(INTEGER, expression.type()); + assertEquals(integerValue(dayOfYear), eval(expression)); + } + @Test public void day_Of_Year() { when(nullRef.type()).thenReturn(DATE); @@ -500,6 +506,34 @@ public void day_Of_Year() { assertEquals(INTEGER, expression.type()); assertEquals("day_of_year(\"2020-08-07 01:02:03\")", expression.toString()); assertEquals(integerValue(220), eval(expression)); + + //28th of Feb + test_day_of_year("2020-02-28", 59); + //29th of Feb during leap year + test_day_of_year("2020-02-29", 60); + + //1st of March during leap year + test_day_of_year("2020-03-01", 61); + + //1st of March during non leap year + test_day_of_year("2019-03-01", 60); + + //31st of December during leap year (should be 366) + test_day_of_year("2020-12-31", 366); + + //Year 1200 + test_day_of_year("1200-02-28", 59); + + //Year 4000 + test_day_of_year("4000-02-28", 59); + } + + @Test + public void invalid_day_of_year() { + //todo -ve year + //TODO: (invalid) 29th of Feb non-leapyear + //TODO: 13th month + //TODO: incorrect format for type tests } @Test From 6c4d02c85ec5eccbb9eaed11952fc18d32eb29a2 Mon Sep 17 00:00:00 2001 From: GabeFernandez310 Date: Mon, 28 Nov 2022 10:31:27 -0800 Subject: [PATCH 09/18] Added Test Cases For Invalid Arguments Signed-off-by: GabeFernandez310 --- .../datetime/DateTimeFunctionTest.java | 30 ++++++++++++++----- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeFunctionTest.java b/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeFunctionTest.java index f15278ecb1..6f8acad208 100644 --- a/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeFunctionTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeFunctionTest.java @@ -8,6 +8,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.lenient; import static org.mockito.Mockito.when; import static org.opensearch.sql.data.model.ExprValueUtils.integerValue; import static org.opensearch.sql.data.model.ExprValueUtils.longValue; @@ -487,10 +488,8 @@ public void test_day_of_year(String date, int dayOfYear) { @Test public void day_Of_Year() { - when(nullRef.type()).thenReturn(DATE); - when(missingRef.type()).thenReturn(DATE); - assertEquals(nullValue(), eval(DSL.day_of_year(nullRef))); - assertEquals(missingValue(), eval(DSL.day_of_year(missingRef))); + lenient().when(nullRef.valueOf(env)).thenReturn(nullValue()); + lenient().when(missingRef.valueOf(env)).thenReturn(missingValue()); FunctionExpression expression = DSL.day_of_year(DSL.literal(new ExprDateValue("2020-08-07"))); assertEquals(INTEGER, expression.type()); @@ -509,10 +508,13 @@ public void day_Of_Year() { //28th of Feb test_day_of_year("2020-02-28", 59); + //29th of Feb during leap year + test_day_of_year("2020-02-29 23:59:59", 60); test_day_of_year("2020-02-29", 60); //1st of March during leap year + test_day_of_year("2020-03-01 00:00:00", 61); test_day_of_year("2020-03-01", 61); //1st of March during non leap year @@ -528,12 +530,24 @@ public void day_Of_Year() { test_day_of_year("4000-02-28", 59); } + public void test_invalid_day_of_year(String date) { + FunctionExpression expression = DSL.day_of_year(DSL.literal(new ExprDateValue(date))); + eval(expression); + } + @Test public void invalid_day_of_year() { - //todo -ve year - //TODO: (invalid) 29th of Feb non-leapyear - //TODO: 13th month - //TODO: incorrect format for type tests + when(nullRef.type()).thenReturn(DATE); + when(missingRef.type()).thenReturn(DATE); + assertEquals(nullValue(), eval(DSL.day_of_year(nullRef))); + assertEquals(missingValue(), eval(DSL.day_of_year(missingRef))); + + //invalid) 29th of Feb non-leapyear + assertThrows(SemanticCheckException.class, () -> test_invalid_day_of_year("2019-02-29")); + //13th month + assertThrows(SemanticCheckException.class, () -> test_invalid_day_of_year("2019-13-15")); + //incorrect format for type + assertThrows(SemanticCheckException.class, () -> test_invalid_day_of_year("2019-13-15")); } @Test From 4b09fb86c531a619961d6d6691268e97948e67d9 Mon Sep 17 00:00:00 2001 From: GabeFernandez310 Date: Mon, 28 Nov 2022 10:58:03 -0800 Subject: [PATCH 10/18] Added Integration Test Cases For Other Types Signed-off-by: GabeFernandez310 --- .../org/opensearch/sql/sql/DateTimeFunctionIT.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/integ-test/src/test/java/org/opensearch/sql/sql/DateTimeFunctionIT.java b/integ-test/src/test/java/org/opensearch/sql/sql/DateTimeFunctionIT.java index d09f9b1a2b..9945b9060d 100644 --- a/integ-test/src/test/java/org/opensearch/sql/sql/DateTimeFunctionIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/sql/DateTimeFunctionIT.java @@ -226,11 +226,19 @@ public void testDayOfYear() throws IOException { } @Test - public void testDay_Of_Year() throws IOException { + public void testDayOfYearWithUnderscores() throws IOException { JSONObject result = executeQuery("select day_of_year(date('2020-09-16'))"); verifySchema(result, schema("day_of_year(date('2020-09-16'))", null, "integer")); verifyDataRows(result, rows(260)); + result = executeQuery("select day_of_year(datetime('2020-09-16 00:00:00'))"); + verifySchema(result, schema("day_of_year(datetime('2020-09-16 00:00:00'))", null, "integer")); + verifyDataRows(result, rows(260)); + + result = executeQuery("select day_of_year(timestamp('2020-09-16 00:00:00'))"); + verifySchema(result, schema("day_of_year(timestamp('2020-09-16 00:00:00'))", null, "integer")); + verifyDataRows(result, rows(260)); + result = executeQuery("select day_of_year('2020-09-16')"); verifySchema(result, schema("day_of_year('2020-09-16')", null, "integer")); verifyDataRows(result, rows(260)); From 97b1f11477fae16a66e839fe3f5131da66ae49cb Mon Sep 17 00:00:00 2001 From: GabeFernandez310 Date: Mon, 28 Nov 2022 14:01:23 -0800 Subject: [PATCH 11/18] Fixed A Comment Signed-off-by: GabeFernandez310 --- .../sql/expression/datetime/DateTimeFunctionTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeFunctionTest.java b/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeFunctionTest.java index 6f8acad208..f189c1be26 100644 --- a/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeFunctionTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeFunctionTest.java @@ -542,7 +542,7 @@ public void invalid_day_of_year() { assertEquals(nullValue(), eval(DSL.day_of_year(nullRef))); assertEquals(missingValue(), eval(DSL.day_of_year(missingRef))); - //invalid) 29th of Feb non-leapyear + //29th of Feb non-leapyear assertThrows(SemanticCheckException.class, () -> test_invalid_day_of_year("2019-02-29")); //13th month assertThrows(SemanticCheckException.class, () -> test_invalid_day_of_year("2019-13-15")); From e65720e678d1579f334e6ffddf1c82390f55a068 Mon Sep 17 00:00:00 2001 From: GabeFernandez310 Date: Mon, 28 Nov 2022 14:04:23 -0800 Subject: [PATCH 12/18] Fixed Unit Test Signed-off-by: GabeFernandez310 --- .../sql/expression/datetime/DateTimeFunctionTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeFunctionTest.java b/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeFunctionTest.java index f189c1be26..ec9c4218a9 100644 --- a/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeFunctionTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeFunctionTest.java @@ -547,7 +547,7 @@ public void invalid_day_of_year() { //13th month assertThrows(SemanticCheckException.class, () -> test_invalid_day_of_year("2019-13-15")); //incorrect format for type - assertThrows(SemanticCheckException.class, () -> test_invalid_day_of_year("2019-13-15")); + assertThrows(SemanticCheckException.class, () -> test_invalid_day_of_year("asdfasdfasdf")); } @Test From 226c1a58d51cc544925bb59cc65f9bbf38863105 Mon Sep 17 00:00:00 2001 From: GabeFernandez310 Date: Mon, 28 Nov 2022 14:21:01 -0800 Subject: [PATCH 13/18] Modified Documentation Signed-off-by: GabeFernandez310 --- docs/user/dql/functions.rst | 51 ++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/docs/user/dql/functions.rst b/docs/user/dql/functions.rst index b5588822b8..a843a38269 100644 --- a/docs/user/dql/functions.rst +++ b/docs/user/dql/functions.rst @@ -1442,7 +1442,8 @@ DAYOFYEAR Description >>>>>>>>>>> -Usage: dayofyear(date) returns the day of the year for date, in the range 1 to 366. Also supports day_of_year(date) as alternate syntax. +Usage: dayofyear(date) returns the day of the year for date, in the range 1 to 366. +The function `day_of_year`_ is also provided as an alias. Argument type: STRING/DATE/DATETIME/TIMESTAMP @@ -1458,6 +1459,37 @@ Example:: | 239 | +---------------------------------+ + os> SELECT DAYOFYEAR(DATETIME('2020-08-26 00:00:00')) + fetched rows / total rows = 1/1 + +------------------------------------------+ + | DAYOFYEAR(DATE('2020-08-26 00:00:00')) | + |------------------------------------------| + | 239 | + +------------------------------------------+ + + os> SELECT DAYOFYEAR(DATE('2020-08-26 00:00:00')) + fetched rows / total rows = 1/1 + +-----------------------------------------------+ + | DAYOFYEAR(TIMESTAMP('2020-08-26 00:00:00')) | + |-----------------------------------------------| + | 239 | + +-----------------------------------------------+ + + +DAY_OF_YEAR +--------- + +Description +>>>>>>>>>>> + +This function is an alias to the `dayofyear`_ function + +Argument type: STRING/DATE/DATETIME/TIMESTAMP + +Return type: INTEGER + +Example:: + os> SELECT DAY_OF_YEAR(DATE('2020-08-26')) fetched rows / total rows = 1/1 +-----------------------------------+ @@ -1466,6 +1498,23 @@ Example:: | 239 | +-----------------------------------+ + os> SELECT DAY_OF_YEAR(DATETIME('2020-08-26 00:00:00')) + fetched rows / total rows = 1/1 + +--------------------------------------------+ + | DAY_OF_YEAR(DATE('2020-08-26 00:00:00')) | + |--------------------------------------------| + | 239 | + +--------------------------------------------+ + + os> SELECT DAY_OF_YEAR(DATE('2020-08-26 00:00:00')) + fetched rows / total rows = 1/1 + +-------------------------------------------------+ + | DAY_OF_YEAR(TIMESTAMP('2020-08-26 00:00:00')) | + |-------------------------------------------------| + | 239 | + +-------------------------------------------------+ + + FROM_DAYS --------- From f1ed83261c96515f29fae59c500fee844cf23820 Mon Sep 17 00:00:00 2001 From: GabeFernandez310 Date: Mon, 28 Nov 2022 14:23:35 -0800 Subject: [PATCH 14/18] Fixed Datetime In Doctests Signed-off-by: GabeFernandez310 --- docs/user/dql/functions.rst | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/docs/user/dql/functions.rst b/docs/user/dql/functions.rst index a843a38269..ff4b0942b3 100644 --- a/docs/user/dql/functions.rst +++ b/docs/user/dql/functions.rst @@ -1461,11 +1461,11 @@ Example:: os> SELECT DAYOFYEAR(DATETIME('2020-08-26 00:00:00')) fetched rows / total rows = 1/1 - +------------------------------------------+ - | DAYOFYEAR(DATE('2020-08-26 00:00:00')) | - |------------------------------------------| - | 239 | - +------------------------------------------+ + +----------------------------------------------+ + | DAYOFYEAR(DATETIME('2020-08-26 00:00:00')) | + |----------------------------------------------| + | 239 | + +----------------------------------------------+ os> SELECT DAYOFYEAR(DATE('2020-08-26 00:00:00')) fetched rows / total rows = 1/1 @@ -1500,11 +1500,11 @@ Example:: os> SELECT DAY_OF_YEAR(DATETIME('2020-08-26 00:00:00')) fetched rows / total rows = 1/1 - +--------------------------------------------+ - | DAY_OF_YEAR(DATE('2020-08-26 00:00:00')) | - |--------------------------------------------| - | 239 | - +--------------------------------------------+ + +------------------------------------------------+ + | DAY_OF_YEAR(DATETIME('2020-08-26 00:00:00')) | + |------------------------------------------------| + | 239 | + +------------------------------------------------+ os> SELECT DAY_OF_YEAR(DATE('2020-08-26 00:00:00')) fetched rows / total rows = 1/1 From 9c3c1f18a471900bea402048557ac187443e4763 Mon Sep 17 00:00:00 2001 From: GabeFernandez310 Date: Tue, 29 Nov 2022 08:45:50 -0800 Subject: [PATCH 15/18] Separated Unit Tests Signed-off-by: GabeFernandez310 --- .../datetime/DateTimeFunctionTest.java | 88 +++++++++++-------- 1 file changed, 53 insertions(+), 35 deletions(-) diff --git a/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeFunctionTest.java b/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeFunctionTest.java index ec9c4218a9..4b0bfbb380 100644 --- a/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeFunctionTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeFunctionTest.java @@ -6,6 +6,7 @@ package org.opensearch.sql.expression.datetime; +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.mockito.Mockito.lenient; @@ -480,74 +481,91 @@ public void dayOfYear() { assertEquals(integerValue(220), eval(expression)); } - public void test_day_of_year(String date, int dayOfYear) { + public void testDayOfYearWithUnderscores(String date, int dayOfYear) { FunctionExpression expression = DSL.day_of_year(DSL.literal(new ExprDateValue(date))); assertEquals(INTEGER, expression.type()); assertEquals(integerValue(dayOfYear), eval(expression)); } @Test - public void day_Of_Year() { + public void DayOfYearWithUnderscoresDifferentArgumentFormats() { lenient().when(nullRef.valueOf(env)).thenReturn(nullValue()); lenient().when(missingRef.valueOf(env)).thenReturn(missingValue()); - FunctionExpression expression = DSL.day_of_year(DSL.literal(new ExprDateValue("2020-08-07"))); - assertEquals(INTEGER, expression.type()); - assertEquals("day_of_year(DATE '2020-08-07')", expression.toString()); - assertEquals(integerValue(220), eval(expression)); + FunctionExpression expression1 = DSL.day_of_year(DSL.literal(new ExprDateValue("2020-08-07"))); + FunctionExpression expression2 = DSL.day_of_year(DSL.literal("2020-08-07")); + FunctionExpression expression3 = DSL.day_of_year(DSL.literal("2020-08-07 01:02:03")); - expression = DSL.day_of_year(DSL.literal("2020-08-07")); - assertEquals(INTEGER, expression.type()); - assertEquals("day_of_year(\"2020-08-07\")", expression.toString()); - assertEquals(integerValue(220), eval(expression)); + assertAll( + () -> testDayOfYearWithUnderscores("2020-08-07", 220), + () -> assertEquals("day_of_year(DATE '2020-08-07')", expression1.toString()), - expression = DSL.day_of_year(DSL.literal("2020-08-07 01:02:03")); - assertEquals(INTEGER, expression.type()); - assertEquals("day_of_year(\"2020-08-07 01:02:03\")", expression.toString()); - assertEquals(integerValue(220), eval(expression)); + () -> testDayOfYearWithUnderscores("2020-08-07", 220), + () -> assertEquals("day_of_year(\"2020-08-07\")", expression2.toString()), + + () -> testDayOfYearWithUnderscores("2020-08-07 01:02:03", 220), + () -> assertEquals("day_of_year(\"2020-08-07 01:02:03\")", expression3.toString()) + ); + } - //28th of Feb - test_day_of_year("2020-02-28", 59); + @Test + public void DayOfYearWithUnderscoresCornerCaseDates() { + lenient().when(nullRef.valueOf(env)).thenReturn(nullValue()); + lenient().when(missingRef.valueOf(env)).thenReturn(missingValue()); + + assertAll( + //31st of December during non leap year (should be 365) + () -> testDayOfYearWithUnderscores("2019-12-31", 365), + //Year 1200 + () -> testDayOfYearWithUnderscores("1200-02-28", 59), + //Year 4000 + () -> testDayOfYearWithUnderscores("4000-02-28", 59) + ); + } - //29th of Feb during leap year - test_day_of_year("2020-02-29 23:59:59", 60); - test_day_of_year("2020-02-29", 60); + @Test + public void DayOfYearWithUnderscoresLeapYear() { + lenient().when(nullRef.valueOf(env)).thenReturn(nullValue()); + lenient().when(missingRef.valueOf(env)).thenReturn(missingValue()); - //1st of March during leap year - test_day_of_year("2020-03-01 00:00:00", 61); - test_day_of_year("2020-03-01", 61); + assertAll( + //28th of Feb + () -> testDayOfYearWithUnderscores("2020-02-28", 59), - //1st of March during non leap year - test_day_of_year("2019-03-01", 60); + //29th of Feb during leap year + () -> testDayOfYearWithUnderscores("2020-02-29 23:59:59", 60), + () -> testDayOfYearWithUnderscores("2020-02-29", 60), - //31st of December during leap year (should be 366) - test_day_of_year("2020-12-31", 366); + //1st of March during leap year + () -> testDayOfYearWithUnderscores("2020-03-01 00:00:00", 61), + () -> testDayOfYearWithUnderscores("2020-03-01", 61), - //Year 1200 - test_day_of_year("1200-02-28", 59); + //1st of March during non leap year + () -> testDayOfYearWithUnderscores("2019-03-01", 60), - //Year 4000 - test_day_of_year("4000-02-28", 59); + //31st of December during leap year (should be 366) + () -> testDayOfYearWithUnderscores("2020-12-31", 366) + ); } - public void test_invalid_day_of_year(String date) { + public void testInvalidDayOfYear(String date) { FunctionExpression expression = DSL.day_of_year(DSL.literal(new ExprDateValue(date))); eval(expression); } @Test - public void invalid_day_of_year() { + public void invalidDayOfYearArgument() { when(nullRef.type()).thenReturn(DATE); when(missingRef.type()).thenReturn(DATE); assertEquals(nullValue(), eval(DSL.day_of_year(nullRef))); assertEquals(missingValue(), eval(DSL.day_of_year(missingRef))); //29th of Feb non-leapyear - assertThrows(SemanticCheckException.class, () -> test_invalid_day_of_year("2019-02-29")); + assertThrows(SemanticCheckException.class, () -> testInvalidDayOfYear("2019-02-29")); //13th month - assertThrows(SemanticCheckException.class, () -> test_invalid_day_of_year("2019-13-15")); + assertThrows(SemanticCheckException.class, () -> testInvalidDayOfYear("2019-13-15")); //incorrect format for type - assertThrows(SemanticCheckException.class, () -> test_invalid_day_of_year("asdfasdfasdf")); + assertThrows(SemanticCheckException.class, () -> testInvalidDayOfYear("asdfasdfasdf")); } @Test From 140756f4337b68c025f6b9a422e16434ee397747 Mon Sep 17 00:00:00 2001 From: GabeFernandez310 Date: Tue, 29 Nov 2022 08:57:35 -0800 Subject: [PATCH 16/18] Fixed Doctests Signed-off-by: GabeFernandez310 --- 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 ff4b0942b3..7469a3954e 100644 --- a/docs/user/dql/functions.rst +++ b/docs/user/dql/functions.rst @@ -1467,7 +1467,7 @@ Example:: | 239 | +----------------------------------------------+ - os> SELECT DAYOFYEAR(DATE('2020-08-26 00:00:00')) + os> SELECT DAYOFYEAR(TIMESTAMP('2020-08-26 00:00:00')) fetched rows / total rows = 1/1 +-----------------------------------------------+ | DAYOFYEAR(TIMESTAMP('2020-08-26 00:00:00')) | @@ -1506,7 +1506,7 @@ Example:: | 239 | +------------------------------------------------+ - os> SELECT DAY_OF_YEAR(DATE('2020-08-26 00:00:00')) + os> SELECT DAY_OF_YEAR(TIMESTAMP('2020-08-26 00:00:00')) fetched rows / total rows = 1/1 +-------------------------------------------------+ | DAY_OF_YEAR(TIMESTAMP('2020-08-26 00:00:00')) | From 1c7075b78475dffdfc2e93c1669af2159ba9c271 Mon Sep 17 00:00:00 2001 From: GabeFernandez310 Date: Tue, 29 Nov 2022 09:13:31 -0800 Subject: [PATCH 17/18] Fixed Checkstyle Errors Signed-off-by: GabeFernandez310 --- .../sql/expression/datetime/DateTimeFunctionTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeFunctionTest.java b/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeFunctionTest.java index 4b0bfbb380..af68186d5a 100644 --- a/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeFunctionTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeFunctionTest.java @@ -488,7 +488,7 @@ public void testDayOfYearWithUnderscores(String date, int dayOfYear) { } @Test - public void DayOfYearWithUnderscoresDifferentArgumentFormats() { + public void dayOfYearWithUnderscoresDifferentArgumentFormats() { lenient().when(nullRef.valueOf(env)).thenReturn(nullValue()); lenient().when(missingRef.valueOf(env)).thenReturn(missingValue()); @@ -509,7 +509,7 @@ public void DayOfYearWithUnderscoresDifferentArgumentFormats() { } @Test - public void DayOfYearWithUnderscoresCornerCaseDates() { + public void dayOfYearWithUnderscoresCornerCaseDates() { lenient().when(nullRef.valueOf(env)).thenReturn(nullValue()); lenient().when(missingRef.valueOf(env)).thenReturn(missingValue()); @@ -524,7 +524,7 @@ public void DayOfYearWithUnderscoresCornerCaseDates() { } @Test - public void DayOfYearWithUnderscoresLeapYear() { + public void dayOfYearWithUnderscoresLeapYear() { lenient().when(nullRef.valueOf(env)).thenReturn(nullValue()); lenient().when(missingRef.valueOf(env)).thenReturn(missingValue()); From c5c5bddb9abbc162583eae9453faec792be9cb74 Mon Sep 17 00:00:00 2001 From: GabeFernandez310 Date: Tue, 29 Nov 2022 13:59:22 -0800 Subject: [PATCH 18/18] Added Integration Test Signed-off-by: GabeFernandez310 --- .../sql/sql/DateTimeFunctionIT.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/integ-test/src/test/java/org/opensearch/sql/sql/DateTimeFunctionIT.java b/integ-test/src/test/java/org/opensearch/sql/sql/DateTimeFunctionIT.java index 9945b9060d..8033e72521 100644 --- a/integ-test/src/test/java/org/opensearch/sql/sql/DateTimeFunctionIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/sql/DateTimeFunctionIT.java @@ -7,6 +7,7 @@ package org.opensearch.sql.sql; import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_BANK; +import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_CALCS; import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_PEOPLE2; import static org.opensearch.sql.legacy.plugin.RestSqlAction.QUERY_API_ENDPOINT; import static org.opensearch.sql.util.MatcherUtils.rows; @@ -49,6 +50,7 @@ public void init() throws Exception { super.init(); loadIndex(Index.BANK); loadIndex(Index.PEOPLE2); + loadIndex(Index.CALCS); } @Test @@ -244,6 +246,37 @@ public void testDayOfYearWithUnderscores() throws IOException { verifyDataRows(result, rows(260)); } + @Test + public void testDayOfYearAlternateSyntaxesReturnTheSameResults() throws IOException { + JSONObject result1 = executeQuery("SELECT dayofyear(date('2022-11-22'))"); + JSONObject result2 = executeQuery("SELECT day_of_year(date('2022-11-22'))"); + verifyDataRows(result1, rows(326)); + result1.getJSONArray("datarows").similar(result2.getJSONArray("datarows")); + + result1 = executeQuery(String.format( + "SELECT dayofyear(CAST(date0 AS date)) FROM %s", TEST_INDEX_CALCS)); + result2 = executeQuery(String.format( + "SELECT day_of_year(CAST(date0 AS date)) FROM %s", TEST_INDEX_CALCS)); + result1.getJSONArray("datarows").similar(result2.getJSONArray("datarows")); + + result1 = executeQuery(String.format( + "SELECT dayofyear(datetime(CAST(time0 AS STRING))) FROM %s", TEST_INDEX_CALCS)); + result2 = executeQuery(String.format( + "SELECT day_of_year(datetime(CAST(time0 AS STRING))) FROM %s", TEST_INDEX_CALCS)); + result1.getJSONArray("datarows").similar(result2.getJSONArray("datarows")); + + result1 = executeQuery(String.format( + "SELECT dayofyear(CAST(time0 AS STRING)) FROM %s", TEST_INDEX_CALCS)); + result2 = executeQuery(String.format( + "SELECT day_of_year(CAST(time0 AS STRING)) FROM %s", TEST_INDEX_CALCS)); + result1.getJSONArray("datarows").similar(result2.getJSONArray("datarows")); + + result1 = executeQuery(String.format( + "SELECT dayofyear(CAST(datetime0 AS timestamp)) FROM %s", TEST_INDEX_CALCS)); + result2 = executeQuery(String.format( + "SELECT day_of_year(CAST(datetime0 AS timestamp)) FROM %s", TEST_INDEX_CALCS)); + result1.getJSONArray("datarows").similar(result2.getJSONArray("datarows")); + } @Test public void testFromDays() throws IOException { JSONObject result = executeQuery("select from_days(738049)");