From 78323013b5e0fb6397073ce60c242cd0abd20d11 Mon Sep 17 00:00:00 2001 From: GabeFernandez310 Date: Thu, 15 Dec 2022 09:05:03 -0800 Subject: [PATCH 01/14] Added Tests Signed-off-by: GabeFernandez310 --- .../datetime/DateTimeFunctionTest.java | 69 +++++++++++++++++++ .../sql/sql/DateTimeFunctionIT.java | 43 ++++++++++++ .../sql/sql/antlr/SQLSyntaxParserTest.java | 6 ++ 3 files changed, 118 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 092b64d5d75..f3abfc720d9 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 @@ -458,6 +458,75 @@ public void dayOfWeek() { assertEquals(integerValue(1), eval(expression)); } + public void testDayOfWeekWithUnderscoress(FunctionExpression dateExpression, int dayOfWeek) { + assertEquals(INTEGER, dateExpression.type()); + assertEquals(integerValue(dayOfWeek), eval(dateExpression)); + } + + @Test + public void dayOfWeekWithUnderscores() { + lenient().when(nullRef.type()).thenReturn(DATE); + lenient().when(missingRef.type()).thenReturn(DATE); + + FunctionExpression expression1 = DSL.day_of_week(DSL.literal(new ExprDateValue("2020-08-07"))); + FunctionExpression expression2 = DSL.day_of_week(DSL.literal(new ExprDateValue("2020-08-09"))); + FunctionExpression expression3 = DSL.day_of_week(DSL.literal("2020-08-09")); + FunctionExpression expression4 = DSL.day_of_week(DSL.literal("2020-08-09 01:02:03")); + + assertAll( + () -> testDayOfWeekWithUnderscoress(expression1, 6), + () -> assertEquals("day_of_week(DATE '2020-08-07')", expression1.toString()), + + () -> testDayOfWeekWithUnderscoress(expression2, 1), + () -> assertEquals("day_of_week(DATE '2020-08-09')", expression2.toString()), + + + () -> testDayOfWeekWithUnderscoress(expression3, 1), + () -> assertEquals("day_of_week(\"2020-08-09\")", expression3.toString()), + + () -> testDayOfWeekWithUnderscoress(expression4, 1), + () -> assertEquals("day_of_week(\"2020-08-09 01:02:03\")", expression4.toString()) + ); + } + + public void testInvalidDayOfWeek(String date) { + FunctionExpression expression = DSL.day_of_year(DSL.literal(new ExprDateValue(date))); + eval(expression); + } + public void dayOfWeekWithUnderscoresLeapYear() { + lenient().when(nullRef.type()).thenReturn(DATE); + lenient().when(missingRef.type()).thenReturn(DATE); + + //Feb. 29 of a leap year + testDayOfWeekWithUnderscoress(DSL.day_of_week(DSL.literal("2020-02-29")), 7); + + //day after Feb. 29 of a leap year + testDayOfWeekWithUnderscoress(DSL.day_of_week(DSL.literal("2020-03-01")), 1); + + //Feb. 28 of a non-leap year + testDayOfWeekWithUnderscoress(DSL.day_of_week(DSL.literal("2021-02-28")), 7); + + //Feb. 29 of a non-leap year + assertThrows(SemanticCheckException.class, () -> testInvalidDayOfWeek("2021-02-29")); + + } + + public void dayOfWeekWithUnderscoresInvalidArgument() { + when(nullRef.type()).thenReturn(DATE); + when(missingRef.type()).thenReturn(DATE); + assertEquals(nullValue(), eval(DSL.day_of_week(nullRef))); + assertEquals(missingValue(), eval(DSL.day_of_week(missingRef))); + + //40th day of the month + assertThrows(SemanticCheckException.class, () -> testInvalidDayOfWeek("2021-02-40")); + + //13th month of the year + assertThrows(SemanticCheckException.class, () -> testInvalidDayOfWeek("2021-13-29")); + + //incorrect format + assertThrows(SemanticCheckException.class, () -> testInvalidDayOfWeek("asdfasdf")); + } + @Test public void dayOfYear() { when(nullRef.type()).thenReturn(DATE); 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 3503877d647..7b72bc775a1 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 @@ -217,6 +217,49 @@ public void testDayOfWeek() throws IOException { verifyDataRows(result, rows(4)); } + @Test + public void testDayOfWeekWithUnderscores() throws IOException { + JSONObject result = executeQuery("select day_of_week(date('2020-09-16'))"); + verifySchema(result, schema("day_of_week(date('2020-09-16'))", null, "integer")); + verifyDataRows(result, rows(4)); + + result = executeQuery("select day_of_week('2020-09-16')"); + verifySchema(result, schema("day_of_week('2020-09-16')", null, "integer")); + verifyDataRows(result, rows(4)); + } + + @Test + public void testDayOfWeekAliasesReturnTheSameResults() throws IOException { + JSONObject result1 = executeQuery("SELECT dayofweek(date('2022-11-22'))"); + JSONObject result2 = executeQuery("SELECT day_of_week(date('2022-11-22'))"); + verifyDataRows(result1, rows(3)); + result1.getJSONArray("datarows").similar(result2.getJSONArray("datarows")); + + result1 = executeQuery(String.format( + "SELECT dayofweek(CAST(date0 AS date)) FROM %s", TEST_INDEX_CALCS)); + result2 = executeQuery(String.format( + "SELECT day_of_week(CAST(date0 AS date)) FROM %s", TEST_INDEX_CALCS)); + result1.getJSONArray("datarows").similar(result2.getJSONArray("datarows")); + + result1 = executeQuery(String.format( + "SELECT dayofweek(datetime(CAST(time0 AS STRING))) FROM %s", TEST_INDEX_CALCS)); + result2 = executeQuery(String.format( + "SELECT day_of_week(datetime(CAST(time0 AS STRING))) FROM %s", TEST_INDEX_CALCS)); + result1.getJSONArray("datarows").similar(result2.getJSONArray("datarows")); + + result1 = executeQuery(String.format( + "SELECT dayofweek(CAST(time0 AS STRING)) FROM %s", TEST_INDEX_CALCS)); + result2 = executeQuery(String.format( + "SELECT day_of_week(CAST(time0 AS STRING)) FROM %s", TEST_INDEX_CALCS)); + result1.getJSONArray("datarows").similar(result2.getJSONArray("datarows")); + + result1 = executeQuery(String.format( + "SELECT dayofweek(CAST(datetime0 AS timestamp)) FROM %s", TEST_INDEX_CALCS)); + result2 = executeQuery(String.format( + "SELECT day_of_week(CAST(datetime0 AS timestamp)) FROM %s", TEST_INDEX_CALCS)); + result1.getJSONArray("datarows").similar(result2.getJSONArray("datarows")); + } + @Test public void testDayOfYear() throws IOException { JSONObject result = executeQuery("select dayofyear(date('2020-09-16'))"); 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 bfd0f93ec98..b06598c2700 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 @@ -197,6 +197,12 @@ public void can_parse_week_of_year_functions() { assertNotNull(parser.parse("SELECT week_of_year('2022-11-18')")); } + @Test + public void can_parse_day_of_week_functions() { + assertNotNull(parser.parse("SELECT dayofweek('2022-11-18')")); + assertNotNull(parser.parse("SELECT day_of_week('2022-11-18')")); + } + @Test public void can_parse_dayofyear_functions() { assertNotNull(parser.parse("SELECT dayofyear('2022-11-18')")); From 42742732ce0cabe37c38519fa6da3473a7ad6f73 Mon Sep 17 00:00:00 2001 From: GabeFernandez310 Date: Thu, 15 Dec 2022 09:43:44 -0800 Subject: [PATCH 02/14] Added Implementation Signed-off-by: GabeFernandez310 --- .../org/opensearch/sql/expression/DSL.java | 4 ++ .../expression/datetime/DateTimeFunction.java | 7 +-- .../function/BuiltinFunctionName.java | 1 + .../datetime/DateTimeFunctionTest.java | 13 +++--- docs/user/dql/functions.rst | 43 +++++++++++++++++++ sql/src/main/antlr/OpenSearchSQLParser.g4 | 1 + 6 files changed, 61 insertions(+), 8 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 fc425c6c20b..4d6664b9d43 100644 --- a/core/src/main/java/org/opensearch/sql/expression/DSL.java +++ b/core/src/main/java/org/opensearch/sql/expression/DSL.java @@ -338,6 +338,10 @@ public static FunctionExpression day_of_year(Expression... expressions) { return compile(FunctionProperties.None, BuiltinFunctionName.DAY_OF_YEAR, expressions); } + public static FunctionExpression day_of_week(Expression... expressions) { + return compile(FunctionProperties.None, BuiltinFunctionName.DAY_OF_WEEK, expressions); + } + public static FunctionExpression from_days(Expression... expressions) { return compile(FunctionProperties.None, BuiltinFunctionName.FROM_DAYS, 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 a111f672af1..7d026bc9735 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 @@ -101,7 +101,8 @@ public void register(BuiltinFunctionRepository repository) { repository.register(day()); repository.register(dayName()); repository.register(dayOfMonth()); - repository.register(dayOfWeek()); + repository.register(dayOfWeek(BuiltinFunctionName.DAYOFWEEK.getName())); + repository.register(dayOfWeek(BuiltinFunctionName.DAY_OF_WEEK.getName())); repository.register(dayOfYear(BuiltinFunctionName.DAYOFYEAR)); repository.register(dayOfYear(BuiltinFunctionName.DAY_OF_YEAR)); repository.register(from_days()); @@ -349,8 +350,8 @@ private DefaultFunctionResolver dayOfMonth() { * DAYOFWEEK(STRING/DATE/DATETIME/TIMESTAMP). * return the weekday index for date (1 = Sunday, 2 = Monday, …, 7 = Saturday). */ - private DefaultFunctionResolver dayOfWeek() { - return define(BuiltinFunctionName.DAYOFWEEK.getName(), + private DefaultFunctionResolver dayOfWeek(FunctionName name) { + return define(name, impl(nullMissingHandling(DateTimeFunction::exprDayOfWeek), INTEGER, DATE), impl(nullMissingHandling(DateTimeFunction::exprDayOfWeek), INTEGER, DATETIME), impl(nullMissingHandling(DateTimeFunction::exprDayOfWeek), 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 b23c7613d68..34fa81ce136 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 @@ -69,6 +69,7 @@ public enum BuiltinFunctionName { DAYOFMONTH(FunctionName.of("dayofmonth")), DAYOFWEEK(FunctionName.of("dayofweek")), DAYOFYEAR(FunctionName.of("dayofyear")), + DAY_OF_WEEK(FunctionName.of("day_of_week")), DAY_OF_YEAR(FunctionName.of("day_of_year")), FROM_DAYS(FunctionName.of("from_days")), FROM_UNIXTIME(FunctionName.of("from_unixtime")), 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 f3abfc720d9..74e0ea8a275 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 @@ -465,8 +465,8 @@ public void testDayOfWeekWithUnderscoress(FunctionExpression dateExpression, int @Test public void dayOfWeekWithUnderscores() { - lenient().when(nullRef.type()).thenReturn(DATE); - lenient().when(missingRef.type()).thenReturn(DATE); + lenient().when(nullRef.valueOf(env)).thenReturn(nullValue()); + lenient().when(missingRef.valueOf(env)).thenReturn(missingValue()); FunctionExpression expression1 = DSL.day_of_week(DSL.literal(new ExprDateValue("2020-08-07"))); FunctionExpression expression2 = DSL.day_of_week(DSL.literal(new ExprDateValue("2020-08-09"))); @@ -493,9 +493,11 @@ public void testInvalidDayOfWeek(String date) { FunctionExpression expression = DSL.day_of_year(DSL.literal(new ExprDateValue(date))); eval(expression); } + + @Test public void dayOfWeekWithUnderscoresLeapYear() { - lenient().when(nullRef.type()).thenReturn(DATE); - lenient().when(missingRef.type()).thenReturn(DATE); + lenient().when(nullRef.valueOf(env)).thenReturn(nullValue()); + lenient().when(missingRef.valueOf(env)).thenReturn(missingValue()); //Feb. 29 of a leap year testDayOfWeekWithUnderscoress(DSL.day_of_week(DSL.literal("2020-02-29")), 7); @@ -504,13 +506,14 @@ public void dayOfWeekWithUnderscoresLeapYear() { testDayOfWeekWithUnderscoress(DSL.day_of_week(DSL.literal("2020-03-01")), 1); //Feb. 28 of a non-leap year - testDayOfWeekWithUnderscoress(DSL.day_of_week(DSL.literal("2021-02-28")), 7); + testDayOfWeekWithUnderscoress(DSL.day_of_week(DSL.literal("2021-02-28")), 1); //Feb. 29 of a non-leap year assertThrows(SemanticCheckException.class, () -> testInvalidDayOfWeek("2021-02-29")); } + @Test public void dayOfWeekWithUnderscoresInvalidArgument() { when(nullRef.type()).thenReturn(DATE); when(missingRef.type()).thenReturn(DATE); diff --git a/docs/user/dql/functions.rst b/docs/user/dql/functions.rst index 843d6c7e455..a1d3d8def3e 100644 --- a/docs/user/dql/functions.rst +++ b/docs/user/dql/functions.rst @@ -1434,6 +1434,8 @@ Description Usage: dayofweek(date) returns the weekday index for date (1 = Sunday, 2 = Monday, …, 7 = Saturday). +The `day_of_week` function is also provided as an alias. + Argument type: STRING/DATE/DATETIME/TIMESTAMP Return type: INTEGER @@ -1448,6 +1450,47 @@ Example:: | 4 | +---------------------------------+ + os> SELECT DAYOFWEEK(DATETIME('2020-08-26')) + fetched rows / total rows = 1/1 + +-------------------------------------+ + | DAYOFWEEK(DATETIME('2020-08-26')) | + |-------------------------------------| + | 4 | + +-------------------------------------+ + + os> SELECT DAYOFWEEK(TIMESTAMP('2020-08-26')) + fetched rows / total rows = 1/1 + +--------------------------------------+ + | DAYOFWEEK(TIMESTAMP('2020-08-26')) | + |--------------------------------------| + | 4 | + +--------------------------------------+ + + os> SELECT DAY_OF_WEEK(DATE('2020-08-26')) + fetched rows / total rows = 1/1 + +-----------------------------------+ + | DAY_OF_WEEK(DATE('2020-08-26')) | + |-----------------------------------| + | 4 | + +-----------------------------------+ + + os> SELECT DAY_OF_WEEK(DATETIME('2020-08-26')) + fetched rows / total rows = 1/1 + +---------------------------------------+ + | DAY_OF_WEEK(DATETIME('2020-08-26')) | + |---------------------------------------| + | 4 | + +---------------------------------------+ + + os> SELECT DAY_OF_WEEK(TIMESTAMP('2020-08-26')) + fetched rows / total rows = 1/1 + +----------------------------------------+ + | DAY_OF_WEEK(TIMESTAMP('2020-08-26')) | + |----------------------------------------| + | 4 | + +----------------------------------------+ + + DAYOFYEAR diff --git a/sql/src/main/antlr/OpenSearchSQLParser.g4 b/sql/src/main/antlr/OpenSearchSQLParser.g4 index 58d4be18130..30a99ccb34b 100644 --- a/sql/src/main/antlr/OpenSearchSQLParser.g4 +++ b/sql/src/main/antlr/OpenSearchSQLParser.g4 @@ -244,6 +244,7 @@ datetimeConstantLiteral : CURRENT_DATE | CURRENT_TIME | CURRENT_TIMESTAMP + | DAY_OF_WEEK | DAY_OF_YEAR | LOCALTIME | LOCALTIMESTAMP From 4a6f083d6eea1f4a4439234c6f0749e8c210b8d4 Mon Sep 17 00:00:00 2001 From: GabeFernandez310 Date: Thu, 15 Dec 2022 15:00:12 -0800 Subject: [PATCH 03/14] Added Documentation Signed-off-by: GabeFernandez310 --- docs/user/dql/functions.rst | 48 ++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/docs/user/dql/functions.rst b/docs/user/dql/functions.rst index a1d3d8def3e..b1f22e7454d 100644 --- a/docs/user/dql/functions.rst +++ b/docs/user/dql/functions.rst @@ -1450,21 +1450,21 @@ Example:: | 4 | +---------------------------------+ - os> SELECT DAYOFWEEK(DATETIME('2020-08-26')) + os> SELECT DAYOFWEEK(DATETIME('2020-08-26 00:00:00')) fetched rows / total rows = 1/1 - +-------------------------------------+ - | DAYOFWEEK(DATETIME('2020-08-26')) | - |-------------------------------------| - | 4 | - +-------------------------------------+ + +----------------------------------------------+ + | DAYOFWEEK(DATETIME('2020-08-26 00:00:00')) | + |----------------------------------------------| + | 4 | + +----------------------------------------------+ - os> SELECT DAYOFWEEK(TIMESTAMP('2020-08-26')) + os> SELECT DAYOFWEEK(TIMESTAMP('2020-08-26 00:00:00')) fetched rows / total rows = 1/1 - +--------------------------------------+ - | DAYOFWEEK(TIMESTAMP('2020-08-26')) | - |--------------------------------------| - | 4 | - +--------------------------------------+ + +-----------------------------------------------+ + | DAYOFWEEK(TIMESTAMP('2020-08-26 00:00:00')) | + |-----------------------------------------------| + | 4 | + +-----------------------------------------------+ os> SELECT DAY_OF_WEEK(DATE('2020-08-26')) fetched rows / total rows = 1/1 @@ -1474,21 +1474,21 @@ Example:: | 4 | +-----------------------------------+ - os> SELECT DAY_OF_WEEK(DATETIME('2020-08-26')) + os> SELECT DAY_OF_WEEK(DATETIME('2020-08-26 00:00:00')) fetched rows / total rows = 1/1 - +---------------------------------------+ - | DAY_OF_WEEK(DATETIME('2020-08-26')) | - |---------------------------------------| - | 4 | - +---------------------------------------+ + +------------------------------------------------+ + | DAY_OF_WEEK(DATETIME('2020-08-26 00:00:00')) | + |------------------------------------------------| + | 4 | + +------------------------------------------------+ - os> SELECT DAY_OF_WEEK(TIMESTAMP('2020-08-26')) + os> SELECT DAY_OF_WEEK(TIMESTAMP('2020-08-26 00:00:00')) fetched rows / total rows = 1/1 - +----------------------------------------+ - | DAY_OF_WEEK(TIMESTAMP('2020-08-26')) | - |----------------------------------------| - | 4 | - +----------------------------------------+ + +-------------------------------------------------+ + | DAY_OF_WEEK(TIMESTAMP('2020-08-26 00:00:00')) | + |-------------------------------------------------| + | 4 | + +-------------------------------------------------+ From 485330461e7dcbdebc74fa9ae959fd3835c2ed3c Mon Sep 17 00:00:00 2001 From: GabeFernandez310 Date: Fri, 16 Dec 2022 07:10:53 -0800 Subject: [PATCH 04/14] Fixed Checkstyle Issues 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 74e0ea8a275..19c94c165b8 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 @@ -486,7 +486,7 @@ public void dayOfWeekWithUnderscores() { () -> testDayOfWeekWithUnderscoress(expression4, 1), () -> assertEquals("day_of_week(\"2020-08-09 01:02:03\")", expression4.toString()) - ); + ); } public void testInvalidDayOfWeek(String date) { From 9a061eaa386e7eed76fb42472e450a01461c2e79 Mon Sep 17 00:00:00 2001 From: GabeFernandez310 Date: Mon, 19 Dec 2022 10:19:14 -0800 Subject: [PATCH 05/14] Added Doctests Signed-off-by: GabeFernandez310 --- docs/user/dql/functions.rst | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/docs/user/dql/functions.rst b/docs/user/dql/functions.rst index b1f22e7454d..e4404593962 100644 --- a/docs/user/dql/functions.rst +++ b/docs/user/dql/functions.rst @@ -1442,6 +1442,14 @@ Return type: INTEGER Example:: + os> SELECT DAYOFWEEK('2020-08-26') + fetched rows / total rows = 1/1 + +---------------------------+ + | DAYOFWEEK('2020-08-26') | + |---------------------------| + | 4 | + +---------------------------+ + os> SELECT DAYOFWEEK(DATE('2020-08-26')) fetched rows / total rows = 1/1 +---------------------------------+ @@ -1466,6 +1474,14 @@ Example:: | 4 | +-----------------------------------------------+ + os> SELECT DAY_OF_WEEK('2020-08-26') + fetched rows / total rows = 1/1 + +-----------------------------+ + | DAY_OF_WEEK('2020-08-26') | + |-----------------------------| + | 4 | + +-----------------------------+ + os> SELECT DAY_OF_WEEK(DATE('2020-08-26')) fetched rows / total rows = 1/1 +-----------------------------------+ From f0448301a63c71a2f94f34fefc9e4840961cc437 Mon Sep 17 00:00:00 2001 From: GabeFernandez310 Date: Wed, 21 Dec 2022 08:14:59 -0800 Subject: [PATCH 06/14] Fixed Function Names And Public Functions Signed-off-by: GabeFernandez310 --- .../datetime/DateTimeFunctionTest.java | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 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 19c94c165b8..98c20f9eef9 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 @@ -458,7 +458,7 @@ public void dayOfWeek() { assertEquals(integerValue(1), eval(expression)); } - public void testDayOfWeekWithUnderscoress(FunctionExpression dateExpression, int dayOfWeek) { + private void testDayOfWeekWithUnderscores(FunctionExpression dateExpression, int dayOfWeek) { assertEquals(INTEGER, dateExpression.type()); assertEquals(integerValue(dayOfWeek), eval(dateExpression)); } @@ -474,23 +474,23 @@ public void dayOfWeekWithUnderscores() { FunctionExpression expression4 = DSL.day_of_week(DSL.literal("2020-08-09 01:02:03")); assertAll( - () -> testDayOfWeekWithUnderscoress(expression1, 6), + () -> testDayOfWeekWithUnderscores(expression1, 6), () -> assertEquals("day_of_week(DATE '2020-08-07')", expression1.toString()), - () -> testDayOfWeekWithUnderscoress(expression2, 1), + () -> testDayOfWeekWithUnderscores(expression2, 1), () -> assertEquals("day_of_week(DATE '2020-08-09')", expression2.toString()), - () -> testDayOfWeekWithUnderscoress(expression3, 1), + () -> testDayOfWeekWithUnderscores(expression3, 1), () -> assertEquals("day_of_week(\"2020-08-09\")", expression3.toString()), - () -> testDayOfWeekWithUnderscoress(expression4, 1), + () -> testDayOfWeekWithUnderscores(expression4, 1), () -> assertEquals("day_of_week(\"2020-08-09 01:02:03\")", expression4.toString()) ); } - public void testInvalidDayOfWeek(String date) { - FunctionExpression expression = DSL.day_of_year(DSL.literal(new ExprDateValue(date))); + private void testInvalidDayOfWeek(String date) { + FunctionExpression expression = DSL.day_of_week(DSL.literal(new ExprDateValue(date))); eval(expression); } @@ -500,13 +500,13 @@ public void dayOfWeekWithUnderscoresLeapYear() { lenient().when(missingRef.valueOf(env)).thenReturn(missingValue()); //Feb. 29 of a leap year - testDayOfWeekWithUnderscoress(DSL.day_of_week(DSL.literal("2020-02-29")), 7); + testDayOfWeekWithUnderscores(DSL.day_of_week(DSL.literal("2020-02-29")), 7); //day after Feb. 29 of a leap year - testDayOfWeekWithUnderscoress(DSL.day_of_week(DSL.literal("2020-03-01")), 1); + testDayOfWeekWithUnderscores(DSL.day_of_week(DSL.literal("2020-03-01")), 1); //Feb. 28 of a non-leap year - testDayOfWeekWithUnderscoress(DSL.day_of_week(DSL.literal("2021-02-28")), 1); + testDayOfWeekWithUnderscores(DSL.day_of_week(DSL.literal("2021-02-28")), 1); //Feb. 29 of a non-leap year assertThrows(SemanticCheckException.class, () -> testInvalidDayOfWeek("2021-02-29")); @@ -553,7 +553,7 @@ public void dayOfYear() { assertEquals(integerValue(220), eval(expression)); } - public void testDayOfYearWithUnderscores(String date, int dayOfYear) { + private 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)); @@ -620,7 +620,7 @@ public void dayOfYearWithUnderscoresLeapYear() { ); } - public void testInvalidDayOfYear(String date) { + private void testInvalidDayOfYear(String date) { FunctionExpression expression = DSL.day_of_year(DSL.literal(new ExprDateValue(date))); eval(expression); } @@ -786,7 +786,7 @@ public void month() { assertEquals(integerValue(8), eval(expression)); } - public void testInvalidDates(String date) throws SemanticCheckException { + private void testInvalidDates(String date) throws SemanticCheckException { FunctionExpression expression = DSL.month_of_year(DSL.literal(new ExprDateValue(date))); eval(expression); } From e571bf04368910cfc8be87e69e2cf901bad7a46e Mon Sep 17 00:00:00 2001 From: GabeFernandez310 Date: Wed, 21 Dec 2022 08:30:41 -0800 Subject: [PATCH 07/14] Fixed Parser and Documentation Signed-off-by: GabeFernandez310 --- docs/user/dql/functions.rst | 70 ++--------------------- sql/src/main/antlr/OpenSearchSQLParser.g4 | 2 +- 2 files changed, 7 insertions(+), 65 deletions(-) diff --git a/docs/user/dql/functions.rst b/docs/user/dql/functions.rst index e4404593962..5c8536f13d7 100644 --- a/docs/user/dql/functions.rst +++ b/docs/user/dql/functions.rst @@ -1442,71 +1442,13 @@ Return type: INTEGER Example:: - os> SELECT DAYOFWEEK('2020-08-26') + os> SELECT DAYOFWEEK('2020-08-26'), DAY_OF_WEEK('2020-08-26') fetched rows / total rows = 1/1 - +---------------------------+ - | DAYOFWEEK('2020-08-26') | - |---------------------------| - | 4 | - +---------------------------+ - - os> SELECT DAYOFWEEK(DATE('2020-08-26')) - fetched rows / total rows = 1/1 - +---------------------------------+ - | DAYOFWEEK(DATE('2020-08-26')) | - |---------------------------------| - | 4 | - +---------------------------------+ - - os> SELECT DAYOFWEEK(DATETIME('2020-08-26 00:00:00')) - fetched rows / total rows = 1/1 - +----------------------------------------------+ - | DAYOFWEEK(DATETIME('2020-08-26 00:00:00')) | - |----------------------------------------------| - | 4 | - +----------------------------------------------+ - - os> SELECT DAYOFWEEK(TIMESTAMP('2020-08-26 00:00:00')) - fetched rows / total rows = 1/1 - +-----------------------------------------------+ - | DAYOFWEEK(TIMESTAMP('2020-08-26 00:00:00')) | - |-----------------------------------------------| - | 4 | - +-----------------------------------------------+ - - os> SELECT DAY_OF_WEEK('2020-08-26') - fetched rows / total rows = 1/1 - +-----------------------------+ - | DAY_OF_WEEK('2020-08-26') | - |-----------------------------| - | 4 | - +-----------------------------+ - - os> SELECT DAY_OF_WEEK(DATE('2020-08-26')) - fetched rows / total rows = 1/1 - +-----------------------------------+ - | DAY_OF_WEEK(DATE('2020-08-26')) | - |-----------------------------------| - | 4 | - +-----------------------------------+ - - os> SELECT DAY_OF_WEEK(DATETIME('2020-08-26 00:00:00')) - fetched rows / total rows = 1/1 - +------------------------------------------------+ - | DAY_OF_WEEK(DATETIME('2020-08-26 00:00:00')) | - |------------------------------------------------| - | 4 | - +------------------------------------------------+ - - os> SELECT DAY_OF_WEEK(TIMESTAMP('2020-08-26 00:00:00')) - fetched rows / total rows = 1/1 - +-------------------------------------------------+ - | DAY_OF_WEEK(TIMESTAMP('2020-08-26 00:00:00')) | - |-------------------------------------------------| - | 4 | - +-------------------------------------------------+ - - + +---------------------------+-----------------------------+ + | DAYOFWEEK('2020-08-26') | DAY_OF_WEEK('2020-08-26') | + |---------------------------+-----------------------------| + | 4 | 4 | + +---------------------------+-----------------------------+ DAYOFYEAR diff --git a/sql/src/main/antlr/OpenSearchSQLParser.g4 b/sql/src/main/antlr/OpenSearchSQLParser.g4 index 30a99ccb34b..32c50d742cd 100644 --- a/sql/src/main/antlr/OpenSearchSQLParser.g4 +++ b/sql/src/main/antlr/OpenSearchSQLParser.g4 @@ -244,7 +244,6 @@ datetimeConstantLiteral : CURRENT_DATE | CURRENT_TIME | CURRENT_TIMESTAMP - | DAY_OF_WEEK | DAY_OF_YEAR | LOCALTIME | LOCALTIMESTAMP @@ -427,6 +426,7 @@ dateTimeFunctionName | DAYOFMONTH | DAYOFWEEK | DAYOFYEAR + | DAY_OF_WEEK | FROM_DAYS | FROM_UNIXTIME | HOUR From 14279edda261b976d7fc3976c76fbfa0899e5c0c Mon Sep 17 00:00:00 2001 From: GabeFernandez310 Date: Wed, 21 Dec 2022 14:46:22 -0800 Subject: [PATCH 08/14] Added Support For Time Type Signed-off-by: GabeFernandez310 --- .../sql/expression/datetime/DateTimeFunction.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) 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 7d026bc9735..b587504397d 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 @@ -355,7 +355,8 @@ private DefaultFunctionResolver dayOfWeek(FunctionName name) { impl(nullMissingHandling(DateTimeFunction::exprDayOfWeek), INTEGER, DATE), impl(nullMissingHandling(DateTimeFunction::exprDayOfWeek), INTEGER, DATETIME), impl(nullMissingHandling(DateTimeFunction::exprDayOfWeek), INTEGER, TIMESTAMP), - impl(nullMissingHandling(DateTimeFunction::exprDayOfWeek), INTEGER, STRING) + impl(nullMissingHandling(DateTimeFunction::exprDayOfWeek), INTEGER, STRING), + impl(nullMissingHandling(DateTimeFunction::exprDayOfWeek), INTEGER, TIME) ); } @@ -772,11 +773,17 @@ private ExprValue exprDayOfMonth(ExprValue date) { /** * Day of Week implementation for ExprValue. * - * @param date ExprValue of Date/String type. + * @param exprValue ExprValue of Date/Datetime/String/Time type. * @return ExprValue. */ - private ExprValue exprDayOfWeek(ExprValue date) { - return new ExprIntegerValue((date.dateValue().getDayOfWeek().getValue() % 7) + 1); + private ExprValue exprDayOfWeek(ExprValue exprValue) { + switch ((ExprCoreType) exprValue.type()){ + case TIME: + return new ExprIntegerValue( + (formatNow(Clock.systemDefaultZone()).getDayOfWeek().getValue() % 7) + 1); + default: + return new ExprIntegerValue((exprValue.dateValue().getDayOfWeek().getValue() % 7) + 1); + } } /** From 0f083117f5391bd193f294a0765214e3a8d28b93 Mon Sep 17 00:00:00 2001 From: GabeFernandez310 Date: Wed, 21 Dec 2022 16:00:38 -0800 Subject: [PATCH 09/14] Added Basic Test For Time Type Signed-off-by: GabeFernandez310 --- .../datetime/DateTimeFunctionTest.java | 36 ++++++++++++------- 1 file changed, 23 insertions(+), 13 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 98c20f9eef9..145ed90de0f 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 @@ -458,9 +458,10 @@ public void dayOfWeek() { assertEquals(integerValue(1), eval(expression)); } - private void testDayOfWeekWithUnderscores(FunctionExpression dateExpression, int dayOfWeek) { + private void testDayOfWeekWithUnderscores(FunctionExpression dateExpression, int dayOfWeek, String testExpr) { assertEquals(INTEGER, dateExpression.type()); assertEquals(integerValue(dayOfWeek), eval(dateExpression)); + assertEquals(testExpr, dateExpression.toString()); } @Test @@ -474,21 +475,27 @@ public void dayOfWeekWithUnderscores() { FunctionExpression expression4 = DSL.day_of_week(DSL.literal("2020-08-09 01:02:03")); assertAll( - () -> testDayOfWeekWithUnderscores(expression1, 6), - () -> assertEquals("day_of_week(DATE '2020-08-07')", expression1.toString()), + () -> testDayOfWeekWithUnderscores(expression1, 6, "day_of_week(DATE '2020-08-07')"), - () -> testDayOfWeekWithUnderscores(expression2, 1), - () -> assertEquals("day_of_week(DATE '2020-08-09')", expression2.toString()), + () -> testDayOfWeekWithUnderscores(expression2, 1, "day_of_week(DATE '2020-08-09')"), + () -> testDayOfWeekWithUnderscores(expression3, 1, "day_of_week(\"2020-08-09\")"), - () -> testDayOfWeekWithUnderscores(expression3, 1), - () -> assertEquals("day_of_week(\"2020-08-09\")", expression3.toString()), - - () -> testDayOfWeekWithUnderscores(expression4, 1), - () -> assertEquals("day_of_week(\"2020-08-09 01:02:03\")", expression4.toString()) + () -> testDayOfWeekWithUnderscores(expression4, 1, "day_of_week(\"2020-08-09 01:02:03\")") ); } + @Test + public void testDayOfWeekWithTimeType() { + lenient().when(nullRef.valueOf(env)).thenReturn(nullValue()); + lenient().when(missingRef.valueOf(env)).thenReturn(missingValue()); + FunctionExpression expression = DSL.day_of_week(DSL.literal(new ExprTimeValue("12:23:34"))); + + assertEquals(INTEGER, expression.type()); + //assertEquals(integerValue(4), eval(expression)); + assertEquals("day_of_week(TIME '12:23:34')", expression.toString()); + } + private void testInvalidDayOfWeek(String date) { FunctionExpression expression = DSL.day_of_week(DSL.literal(new ExprDateValue(date))); eval(expression); @@ -500,13 +507,16 @@ public void dayOfWeekWithUnderscoresLeapYear() { lenient().when(missingRef.valueOf(env)).thenReturn(missingValue()); //Feb. 29 of a leap year - testDayOfWeekWithUnderscores(DSL.day_of_week(DSL.literal("2020-02-29")), 7); + testDayOfWeekWithUnderscores(DSL.day_of_week( + DSL.literal("2020-02-29")), 7, "day_of_week(\"2020-02-29\")"); //day after Feb. 29 of a leap year - testDayOfWeekWithUnderscores(DSL.day_of_week(DSL.literal("2020-03-01")), 1); + testDayOfWeekWithUnderscores(DSL.day_of_week( + DSL.literal("2020-03-01")), 1, "day_of_week(\"2020-03-01\")"); //Feb. 28 of a non-leap year - testDayOfWeekWithUnderscores(DSL.day_of_week(DSL.literal("2021-02-28")), 1); + testDayOfWeekWithUnderscores( + DSL.day_of_week(DSL.literal("2021-02-28")), 1, "day_of_week(\"2021-02-28\")"); //Feb. 29 of a non-leap year assertThrows(SemanticCheckException.class, () -> testInvalidDayOfWeek("2021-02-29")); From 0db6299864c5a3c0051162623f2c824bac8e58b4 Mon Sep 17 00:00:00 2001 From: GabeFernandez310 Date: Wed, 21 Dec 2022 16:37:36 -0800 Subject: [PATCH 10/14] 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 145ed90de0f..f5f05454449 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 @@ -491,7 +491,7 @@ public void testDayOfWeekWithTimeType() { lenient().when(missingRef.valueOf(env)).thenReturn(missingValue()); FunctionExpression expression = DSL.day_of_week(DSL.literal(new ExprTimeValue("12:23:34"))); - assertEquals(INTEGER, expression.type()); + assertEquals(INTEGER, eval(expression).type()); //assertEquals(integerValue(4), eval(expression)); assertEquals("day_of_week(TIME '12:23:34')", expression.toString()); } From 2364e6866cda7b9a4b28b028d32290017fefe60b Mon Sep 17 00:00:00 2001 From: GabeFernandez310 Date: Thu, 22 Dec 2022 07:56:19 -0800 Subject: [PATCH 11/14] Fixed Checkstyle Signed-off-by: GabeFernandez310 --- .../opensearch/sql/expression/datetime/DateTimeFunction.java | 2 +- .../sql/expression/datetime/DateTimeFunctionTest.java | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) 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 b587504397d..ce258d4805f 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 @@ -777,7 +777,7 @@ private ExprValue exprDayOfMonth(ExprValue date) { * @return ExprValue. */ private ExprValue exprDayOfWeek(ExprValue exprValue) { - switch ((ExprCoreType) exprValue.type()){ + switch ((ExprCoreType) exprValue.type()) { case TIME: return new ExprIntegerValue( (formatNow(Clock.systemDefaultZone()).getDayOfWeek().getValue() % 7) + 1); 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 f5f05454449..f3cd4a3b371 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 @@ -458,7 +458,10 @@ public void dayOfWeek() { assertEquals(integerValue(1), eval(expression)); } - private void testDayOfWeekWithUnderscores(FunctionExpression dateExpression, int dayOfWeek, String testExpr) { + private void testDayOfWeekWithUnderscores( + FunctionExpression dateExpression, + int dayOfWeek, + String testExpr) { assertEquals(INTEGER, dateExpression.type()); assertEquals(integerValue(dayOfWeek), eval(dateExpression)); assertEquals(testExpr, dateExpression.toString()); From 996d30046da42f5c7d62412bd4606c9fff1b0854 Mon Sep 17 00:00:00 2001 From: GabeFernandez310 Date: Thu, 22 Dec 2022 13:37:19 -0800 Subject: [PATCH 12/14] Altered Implementation And Test For Time Type Signed-off-by: GabeFernandez310 --- .../org/opensearch/sql/expression/DSL.java | 10 ++-- .../expression/datetime/DateTimeFunction.java | 21 ++++--- .../datetime/DateTimeFunctionTest.java | 57 +++++++++++++------ 3 files changed, 56 insertions(+), 32 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 4d6664b9d43..f20ccec4bee 100644 --- a/core/src/main/java/org/opensearch/sql/expression/DSL.java +++ b/core/src/main/java/org/opensearch/sql/expression/DSL.java @@ -326,8 +326,9 @@ public static FunctionExpression dayofmonth(Expression... expressions) { return compile(FunctionProperties.None, BuiltinFunctionName.DAYOFMONTH, expressions); } - public static FunctionExpression dayofweek(Expression... expressions) { - return compile(FunctionProperties.None, BuiltinFunctionName.DAYOFWEEK, expressions); + public static FunctionExpression dayofweek( + FunctionProperties functionProperties, Expression... expressions) { + return compile(functionProperties, BuiltinFunctionName.DAYOFWEEK, expressions); } public static FunctionExpression dayofyear(Expression... expressions) { @@ -338,8 +339,9 @@ public static FunctionExpression day_of_year(Expression... expressions) { return compile(FunctionProperties.None, BuiltinFunctionName.DAY_OF_YEAR, expressions); } - public static FunctionExpression day_of_week(Expression... expressions) { - return compile(FunctionProperties.None, BuiltinFunctionName.DAY_OF_WEEK, expressions); + public static FunctionExpression day_of_week( + FunctionProperties functionProperties, Expression... expressions) { + return compile(functionProperties, BuiltinFunctionName.DAY_OF_WEEK, expressions); } public static FunctionExpression from_days(Expression... 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 ce258d4805f..1d3ddaf4321 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 @@ -352,11 +352,12 @@ private DefaultFunctionResolver dayOfMonth() { */ private DefaultFunctionResolver dayOfWeek(FunctionName name) { return define(name, + implWithProperties((functionProperties, arg) -> DateTimeFunction.dayOfWeekToday( + functionProperties.getQueryStartClock()), INTEGER, TIME), impl(nullMissingHandling(DateTimeFunction::exprDayOfWeek), INTEGER, DATE), impl(nullMissingHandling(DateTimeFunction::exprDayOfWeek), INTEGER, DATETIME), impl(nullMissingHandling(DateTimeFunction::exprDayOfWeek), INTEGER, TIMESTAMP), - impl(nullMissingHandling(DateTimeFunction::exprDayOfWeek), INTEGER, STRING), - impl(nullMissingHandling(DateTimeFunction::exprDayOfWeek), INTEGER, TIME) + impl(nullMissingHandling(DateTimeFunction::exprDayOfWeek), INTEGER, STRING) ); } @@ -617,6 +618,10 @@ private DefaultFunctionResolver date_format() { ); } + private ExprValue dayOfWeekToday(Clock clock) { + return new ExprIntegerValue((formatNow(clock).getDayOfWeek().getValue() % 7) + 1); + } + /** * ADDDATE function implementation for ExprValue. * @@ -773,17 +778,11 @@ private ExprValue exprDayOfMonth(ExprValue date) { /** * Day of Week implementation for ExprValue. * - * @param exprValue ExprValue of Date/Datetime/String/Time type. + * @param date ExprValue of Date/Datetime/String type. * @return ExprValue. */ - private ExprValue exprDayOfWeek(ExprValue exprValue) { - switch ((ExprCoreType) exprValue.type()) { - case TIME: - return new ExprIntegerValue( - (formatNow(Clock.systemDefaultZone()).getDayOfWeek().getValue() % 7) + 1); - default: - return new ExprIntegerValue((exprValue.dateValue().getDayOfWeek().getValue() % 7) + 1); - } + private ExprValue exprDayOfWeek(ExprValue date) { + return new ExprIntegerValue((date.dateValue().getDayOfWeek().getValue() % 7) + 1); } /** 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 f3cd4a3b371..815b321e687 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 @@ -434,25 +434,33 @@ public void dayOfMonth() { public void dayOfWeek() { when(nullRef.type()).thenReturn(DATE); when(missingRef.type()).thenReturn(DATE); - assertEquals(nullValue(), eval(DSL.dayofweek(nullRef))); - assertEquals(missingValue(), eval(DSL.dayofweek(missingRef))); + assertEquals(nullValue(), eval(DSL.dayofweek(functionProperties, nullRef))); + assertEquals(missingValue(), eval(DSL.dayofweek(functionProperties, missingRef))); - FunctionExpression expression = DSL.dayofweek(DSL.literal(new ExprDateValue("2020-08-07"))); + FunctionExpression expression = DSL.dayofweek( + functionProperties, + DSL.literal(new ExprDateValue("2020-08-07"))); assertEquals(INTEGER, expression.type()); assertEquals("dayofweek(DATE '2020-08-07')", expression.toString()); assertEquals(integerValue(6), eval(expression)); - expression = DSL.dayofweek(DSL.literal(new ExprDateValue("2020-08-09"))); + expression = DSL.dayofweek( + functionProperties, + DSL.literal(new ExprDateValue("2020-08-09"))); assertEquals(INTEGER, expression.type()); assertEquals("dayofweek(DATE '2020-08-09')", expression.toString()); assertEquals(integerValue(1), eval(expression)); - expression = DSL.dayofweek(DSL.literal("2020-08-09")); + expression = DSL.dayofweek( + functionProperties, + DSL.literal("2020-08-09")); assertEquals(INTEGER, expression.type()); assertEquals("dayofweek(\"2020-08-09\")", expression.toString()); assertEquals(integerValue(1), eval(expression)); - expression = DSL.dayofweek(DSL.literal("2020-08-09 01:02:03")); + expression = DSL.dayofweek( + functionProperties, + DSL.literal("2020-08-09 01:02:03")); assertEquals(INTEGER, expression.type()); assertEquals("dayofweek(\"2020-08-09 01:02:03\")", expression.toString()); assertEquals(integerValue(1), eval(expression)); @@ -472,10 +480,18 @@ public void dayOfWeekWithUnderscores() { lenient().when(nullRef.valueOf(env)).thenReturn(nullValue()); lenient().when(missingRef.valueOf(env)).thenReturn(missingValue()); - FunctionExpression expression1 = DSL.day_of_week(DSL.literal(new ExprDateValue("2020-08-07"))); - FunctionExpression expression2 = DSL.day_of_week(DSL.literal(new ExprDateValue("2020-08-09"))); - FunctionExpression expression3 = DSL.day_of_week(DSL.literal("2020-08-09")); - FunctionExpression expression4 = DSL.day_of_week(DSL.literal("2020-08-09 01:02:03")); + FunctionExpression expression1 = DSL.day_of_week( + functionProperties, + DSL.literal(new ExprDateValue("2020-08-07"))); + FunctionExpression expression2 = DSL.day_of_week( + functionProperties, + DSL.literal(new ExprDateValue("2020-08-09"))); + FunctionExpression expression3 = DSL.day_of_week( + functionProperties, + DSL.literal("2020-08-09")); + FunctionExpression expression4 = DSL.day_of_week( + functionProperties, + DSL.literal("2020-08-09 01:02:03")); assertAll( () -> testDayOfWeekWithUnderscores(expression1, 6, "day_of_week(DATE '2020-08-07')"), @@ -492,15 +508,19 @@ public void dayOfWeekWithUnderscores() { public void testDayOfWeekWithTimeType() { lenient().when(nullRef.valueOf(env)).thenReturn(nullValue()); lenient().when(missingRef.valueOf(env)).thenReturn(missingValue()); - FunctionExpression expression = DSL.day_of_week(DSL.literal(new ExprTimeValue("12:23:34"))); + FunctionExpression expression = DSL.day_of_week( + functionProperties, DSL.literal(new ExprTimeValue("12:23:34"))); assertEquals(INTEGER, eval(expression).type()); - //assertEquals(integerValue(4), eval(expression)); + assertEquals((LocalDate.now( + functionProperties.getQueryStartClock()).getDayOfWeek().getValue() % 7) + 1, + eval(expression).integerValue()); assertEquals("day_of_week(TIME '12:23:34')", expression.toString()); } private void testInvalidDayOfWeek(String date) { - FunctionExpression expression = DSL.day_of_week(DSL.literal(new ExprDateValue(date))); + FunctionExpression expression = DSL.day_of_week( + functionProperties, DSL.literal(new ExprDateValue(date))); eval(expression); } @@ -511,15 +531,18 @@ public void dayOfWeekWithUnderscoresLeapYear() { //Feb. 29 of a leap year testDayOfWeekWithUnderscores(DSL.day_of_week( + functionProperties, DSL.literal("2020-02-29")), 7, "day_of_week(\"2020-02-29\")"); //day after Feb. 29 of a leap year testDayOfWeekWithUnderscores(DSL.day_of_week( + functionProperties, DSL.literal("2020-03-01")), 1, "day_of_week(\"2020-03-01\")"); //Feb. 28 of a non-leap year - testDayOfWeekWithUnderscores( - DSL.day_of_week(DSL.literal("2021-02-28")), 1, "day_of_week(\"2021-02-28\")"); + testDayOfWeekWithUnderscores(DSL.day_of_week( + functionProperties, + DSL.literal("2021-02-28")), 1, "day_of_week(\"2021-02-28\")"); //Feb. 29 of a non-leap year assertThrows(SemanticCheckException.class, () -> testInvalidDayOfWeek("2021-02-29")); @@ -530,8 +553,8 @@ public void dayOfWeekWithUnderscoresLeapYear() { public void dayOfWeekWithUnderscoresInvalidArgument() { when(nullRef.type()).thenReturn(DATE); when(missingRef.type()).thenReturn(DATE); - assertEquals(nullValue(), eval(DSL.day_of_week(nullRef))); - assertEquals(missingValue(), eval(DSL.day_of_week(missingRef))); + assertEquals(nullValue(), eval(DSL.day_of_week(functionProperties, nullRef))); + assertEquals(missingValue(), eval(DSL.day_of_week(functionProperties, missingRef))); //40th day of the month assertThrows(SemanticCheckException.class, () -> testInvalidDayOfWeek("2021-02-40")); From 8daa4baf8f4d26b229723884196bd1da23668861 Mon Sep 17 00:00:00 2001 From: GabeFernandez310 Date: Wed, 4 Jan 2023 15:42:16 -0800 Subject: [PATCH 13/14] Added Support For Null And Missing Args, Fixed Comments Signed-off-by: GabeFernandez310 --- .../sql/expression/datetime/DateTimeFunction.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) 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 1d3ddaf4321..dd2f5516824 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 @@ -20,6 +20,7 @@ import static org.opensearch.sql.expression.function.FunctionDSL.impl; import static org.opensearch.sql.expression.function.FunctionDSL.implWithProperties; import static org.opensearch.sql.expression.function.FunctionDSL.nullMissingHandling; +import static org.opensearch.sql.expression.function.FunctionDSL.nullMissingHandlingWithProperties; import static org.opensearch.sql.utils.DateTimeFormatters.DATE_FORMATTER_LONG_YEAR; import static org.opensearch.sql.utils.DateTimeFormatters.DATE_FORMATTER_SHORT_YEAR; import static org.opensearch.sql.utils.DateTimeFormatters.DATE_TIME_FORMATTER_LONG_YEAR; @@ -347,13 +348,13 @@ private DefaultFunctionResolver dayOfMonth() { } /** - * DAYOFWEEK(STRING/DATE/DATETIME/TIMESTAMP). + * DAYOFWEEK(STRING/DATE/DATETIME/TIME/TIMESTAMP). * return the weekday index for date (1 = Sunday, 2 = Monday, …, 7 = Saturday). */ private DefaultFunctionResolver dayOfWeek(FunctionName name) { return define(name, - implWithProperties((functionProperties, arg) -> DateTimeFunction.dayOfWeekToday( - functionProperties.getQueryStartClock()), INTEGER, TIME), + implWithProperties(nullMissingHandlingWithProperties((functionProperties, arg) -> DateTimeFunction.dayOfWeekToday( + functionProperties.getQueryStartClock())), INTEGER, TIME), impl(nullMissingHandling(DateTimeFunction::exprDayOfWeek), INTEGER, DATE), impl(nullMissingHandling(DateTimeFunction::exprDayOfWeek), INTEGER, DATETIME), impl(nullMissingHandling(DateTimeFunction::exprDayOfWeek), INTEGER, TIMESTAMP), @@ -618,6 +619,12 @@ private DefaultFunctionResolver date_format() { ); } + /** + * Day of Week implementation for ExprValue when passing in an arguemt of type TIME. + * + * @param clock Current clock taken from function properties + * @return ExprValue. + */ private ExprValue dayOfWeekToday(Clock clock) { return new ExprIntegerValue((formatNow(clock).getDayOfWeek().getValue() % 7) + 1); } @@ -778,7 +785,7 @@ private ExprValue exprDayOfMonth(ExprValue date) { /** * Day of Week implementation for ExprValue. * - * @param date ExprValue of Date/Datetime/String type. + * @param date ExprValue of Date/Datetime/String/Timstamp type. * @return ExprValue. */ private ExprValue exprDayOfWeek(ExprValue date) { From 20150cf7693164a1d1c9aeb54a7c8db02452db6d Mon Sep 17 00:00:00 2001 From: GabeFernandez310 Date: Thu, 5 Jan 2023 09:36:18 -0800 Subject: [PATCH 14/14] Refactored Tests Signed-off-by: GabeFernandez310 --- .../expression/datetime/DateTimeFunction.java | 5 +- .../datetime/DateTimeFunctionTest.java | 124 ++++++++++-------- 2 files changed, 70 insertions(+), 59 deletions(-) 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 dd2f5516824..d5320c452df 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 @@ -353,8 +353,9 @@ private DefaultFunctionResolver dayOfMonth() { */ private DefaultFunctionResolver dayOfWeek(FunctionName name) { return define(name, - implWithProperties(nullMissingHandlingWithProperties((functionProperties, arg) -> DateTimeFunction.dayOfWeekToday( - functionProperties.getQueryStartClock())), INTEGER, TIME), + implWithProperties(nullMissingHandlingWithProperties( + (functionProperties, arg) -> DateTimeFunction.dayOfWeekToday( + functionProperties.getQueryStartClock())), INTEGER, TIME), impl(nullMissingHandling(DateTimeFunction::exprDayOfWeek), INTEGER, DATE), impl(nullMissingHandling(DateTimeFunction::exprDayOfWeek), INTEGER, DATETIME), impl(nullMissingHandling(DateTimeFunction::exprDayOfWeek), INTEGER, TIMESTAMP), 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 815b321e687..ae99bb91f39 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 @@ -430,43 +430,45 @@ public void dayOfMonth() { assertEquals(integerValue(8), eval(expression)); } + private void dayOfWeekQuery( + FunctionExpression dateExpression, + int dayOfWeek, + String testExpr) { + assertEquals(INTEGER, dateExpression.type()); + assertEquals(integerValue(dayOfWeek), eval(dateExpression)); + assertEquals(testExpr, dateExpression.toString()); + } + @Test public void dayOfWeek() { - when(nullRef.type()).thenReturn(DATE); - when(missingRef.type()).thenReturn(DATE); - assertEquals(nullValue(), eval(DSL.dayofweek(functionProperties, nullRef))); - assertEquals(missingValue(), eval(DSL.dayofweek(functionProperties, missingRef))); + lenient().when(nullRef.valueOf(env)).thenReturn(nullValue()); + lenient().when(missingRef.valueOf(env)).thenReturn(missingValue()); - FunctionExpression expression = DSL.dayofweek( + FunctionExpression expression1 = DSL.dayofweek( functionProperties, DSL.literal(new ExprDateValue("2020-08-07"))); - assertEquals(INTEGER, expression.type()); - assertEquals("dayofweek(DATE '2020-08-07')", expression.toString()); - assertEquals(integerValue(6), eval(expression)); - - expression = DSL.dayofweek( + FunctionExpression expression2 = DSL.dayofweek( functionProperties, DSL.literal(new ExprDateValue("2020-08-09"))); - assertEquals(INTEGER, expression.type()); - assertEquals("dayofweek(DATE '2020-08-09')", expression.toString()); - assertEquals(integerValue(1), eval(expression)); - - expression = DSL.dayofweek( + FunctionExpression expression3 = DSL.dayofweek( functionProperties, DSL.literal("2020-08-09")); - assertEquals(INTEGER, expression.type()); - assertEquals("dayofweek(\"2020-08-09\")", expression.toString()); - assertEquals(integerValue(1), eval(expression)); - - expression = DSL.dayofweek( + FunctionExpression expression4 = DSL.dayofweek( functionProperties, DSL.literal("2020-08-09 01:02:03")); - assertEquals(INTEGER, expression.type()); - assertEquals("dayofweek(\"2020-08-09 01:02:03\")", expression.toString()); - assertEquals(integerValue(1), eval(expression)); + + assertAll( + () -> dayOfWeekQuery(expression1, 6, "dayofweek(DATE '2020-08-07')"), + + () -> dayOfWeekQuery(expression2, 1, "dayofweek(DATE '2020-08-09')"), + + () -> dayOfWeekQuery(expression3, 1, "dayofweek(\"2020-08-09\")"), + + () -> dayOfWeekQuery(expression4, 1, "dayofweek(\"2020-08-09 01:02:03\")") + ); } - private void testDayOfWeekWithUnderscores( + private void dayOfWeekWithUnderscoresQuery( FunctionExpression dateExpression, int dayOfWeek, String testExpr) { @@ -494,13 +496,14 @@ public void dayOfWeekWithUnderscores() { DSL.literal("2020-08-09 01:02:03")); assertAll( - () -> testDayOfWeekWithUnderscores(expression1, 6, "day_of_week(DATE '2020-08-07')"), + () -> dayOfWeekWithUnderscoresQuery(expression1, 6, "day_of_week(DATE '2020-08-07')"), - () -> testDayOfWeekWithUnderscores(expression2, 1, "day_of_week(DATE '2020-08-09')"), + () -> dayOfWeekWithUnderscoresQuery(expression2, 1, "day_of_week(DATE '2020-08-09')"), - () -> testDayOfWeekWithUnderscores(expression3, 1, "day_of_week(\"2020-08-09\")"), + () -> dayOfWeekWithUnderscoresQuery(expression3, 1, "day_of_week(\"2020-08-09\")"), - () -> testDayOfWeekWithUnderscores(expression4, 1, "day_of_week(\"2020-08-09 01:02:03\")") + () -> dayOfWeekWithUnderscoresQuery( + expression4, 1, "day_of_week(\"2020-08-09 01:02:03\")") ); } @@ -511,11 +514,14 @@ public void testDayOfWeekWithTimeType() { FunctionExpression expression = DSL.day_of_week( functionProperties, DSL.literal(new ExprTimeValue("12:23:34"))); - assertEquals(INTEGER, eval(expression).type()); - assertEquals((LocalDate.now( - functionProperties.getQueryStartClock()).getDayOfWeek().getValue() % 7) + 1, - eval(expression).integerValue()); - assertEquals("day_of_week(TIME '12:23:34')", expression.toString()); + assertAll( + () -> assertEquals(INTEGER, eval(expression).type()), + () -> assertEquals(( + LocalDate.now( + functionProperties.getQueryStartClock()).getDayOfWeek().getValue() % 7) + 1, + eval(expression).integerValue()), + () -> assertEquals("day_of_week(TIME '12:23:34')", expression.toString()) + ); } private void testInvalidDayOfWeek(String date) { @@ -529,24 +535,23 @@ public void dayOfWeekWithUnderscoresLeapYear() { lenient().when(nullRef.valueOf(env)).thenReturn(nullValue()); lenient().when(missingRef.valueOf(env)).thenReturn(missingValue()); - //Feb. 29 of a leap year - testDayOfWeekWithUnderscores(DSL.day_of_week( - functionProperties, - DSL.literal("2020-02-29")), 7, "day_of_week(\"2020-02-29\")"); - - //day after Feb. 29 of a leap year - testDayOfWeekWithUnderscores(DSL.day_of_week( - functionProperties, - DSL.literal("2020-03-01")), 1, "day_of_week(\"2020-03-01\")"); - - //Feb. 28 of a non-leap year - testDayOfWeekWithUnderscores(DSL.day_of_week( - functionProperties, - DSL.literal("2021-02-28")), 1, "day_of_week(\"2021-02-28\")"); - - //Feb. 29 of a non-leap year - assertThrows(SemanticCheckException.class, () -> testInvalidDayOfWeek("2021-02-29")); - + assertAll( + //Feb. 29 of a leap year + () -> dayOfWeekWithUnderscoresQuery(DSL.day_of_week( + functionProperties, + DSL.literal("2020-02-29")), 7, "day_of_week(\"2020-02-29\")"), + //day after Feb. 29 of a leap year + () -> dayOfWeekWithUnderscoresQuery(DSL.day_of_week( + functionProperties, + DSL.literal("2020-03-01")), 1, "day_of_week(\"2020-03-01\")"), + //Feb. 28 of a non-leap year + () -> dayOfWeekWithUnderscoresQuery(DSL.day_of_week( + functionProperties, + DSL.literal("2021-02-28")), 1, "day_of_week(\"2021-02-28\")"), + //Feb. 29 of a non-leap year + () -> assertThrows( + SemanticCheckException.class, () -> testInvalidDayOfWeek("2021-02-29")) + ); } @Test @@ -556,14 +561,19 @@ public void dayOfWeekWithUnderscoresInvalidArgument() { assertEquals(nullValue(), eval(DSL.day_of_week(functionProperties, nullRef))); assertEquals(missingValue(), eval(DSL.day_of_week(functionProperties, missingRef))); - //40th day of the month - assertThrows(SemanticCheckException.class, () -> testInvalidDayOfWeek("2021-02-40")); + assertAll( + //40th day of the month + () -> assertThrows(SemanticCheckException.class, + () -> testInvalidDayOfWeek("2021-02-40")), - //13th month of the year - assertThrows(SemanticCheckException.class, () -> testInvalidDayOfWeek("2021-13-29")); + //13th month of the year + () -> assertThrows(SemanticCheckException.class, + () -> testInvalidDayOfWeek("2021-13-29")), - //incorrect format - assertThrows(SemanticCheckException.class, () -> testInvalidDayOfWeek("asdfasdf")); + //incorrect format + () -> assertThrows(SemanticCheckException.class, + () -> testInvalidDayOfWeek("asdfasdf")) + ); } @Test