From 6f10e9626cc85e9f9bb578beff9e464ecbac45c7 Mon Sep 17 00:00:00 2001 From: GabeFernandez310 Date: Tue, 20 Dec 2022 13:37:52 -0800 Subject: [PATCH 01/11] Added Tests Signed-off-by: GabeFernandez310 --- .../datetime/DateTimeFunctionTest.java | 54 +++++++++++++++++++ .../sql/sql/DateTimeFunctionIT.java | 51 ++++++++++++++++++ .../sql/sql/antlr/SQLSyntaxParserTest.java | 8 +++ 3 files changed, 113 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 092b64d5d7..9aa8379ddc 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 @@ -831,6 +831,60 @@ public void second() { assertEquals("second(DATETIME '2020-08-17 01:02:03')", expression.toString()); } + public void testSecondOfMinute(FunctionExpression dateExpression, int second, String testExpr) { + assertEquals(INTEGER, dateExpression.type()); + assertEquals(integerValue(second), eval(dateExpression)); + assertEquals(testExpr, dateExpression.toString()); + } + + @Test + public void secondOfMinute() { + lenient().when(nullRef.valueOf(env)).thenReturn(nullValue()); + lenient().when(missingRef.valueOf(env)).thenReturn(missingValue()); + + FunctionExpression expression1 = DSL.second_of_minute(DSL.literal(new ExprTimeValue("01:02:03"))); + FunctionExpression expression2 = DSL.second_of_minute(DSL.literal("01:02:03")); + FunctionExpression expression3 = DSL.second_of_minute(DSL.literal("2020-08-17 01:02:03")); + FunctionExpression expression4 = DSL.second_of_minute(DSL.literal(new ExprTimestampValue("2020-08-17 01:02:03"))); + FunctionExpression expression5 = DSL.second_of_minute(DSL.literal(new ExprDatetimeValue("2020-08-17 01:02:03"))); + + assertAll( + () -> testSecondOfMinute(expression1, 3, "second_of_minute(TIME '01:02:03')"), + () -> testSecondOfMinute(expression2, 3, "second_of_minute(\"01:02:03\")"), + () -> testSecondOfMinute(expression3, 3, "second_of_minute(\"2020-08-17 01:02:03\")"), + () -> testSecondOfMinute( + expression4, 3, "second_of_minute(TIMESTAMP '2020-08-17 01:02:03')"), + () -> testSecondOfMinute( + expression5, 3, "second_of_minute(DATETIME '2020-08-17 01:02:03')") + ); + } + + public void testInvalidSecondOfMinute(String time) { + FunctionExpression expression = DSL.second_of_minute(DSL.literal(new ExprTimeValue(time))); + eval(expression); + } + + @Test + public void secondOfMinuteInvalidArguments() { + when(nullRef.type()).thenReturn(TIME); + when(missingRef.type()).thenReturn(TIME); + assertEquals(nullValue(), eval(DSL.second_of_minute(nullRef))); + assertEquals(missingValue(), eval(DSL.second_of_minute(missingRef))); + + //Invalid Seconds + assertThrows(SemanticCheckException.class, () -> testInvalidSecondOfMinute("12:23:61")); + + //Invalid Minutes + assertThrows(SemanticCheckException.class, () -> testInvalidSecondOfMinute("12:61:34")); + + //Invalid Hours + assertThrows(SemanticCheckException.class, () -> testInvalidSecondOfMinute("25:23:34")); + + //incorrect format + assertThrows(SemanticCheckException.class, () -> testInvalidSecondOfMinute("asdfasdf")); + } + + @Test public void subdate() { FunctionExpression expr = DSL.subdate(DSL.date(DSL.literal("2020-08-26")), DSL.literal(7)); 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 3503877d64..1640c19777 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 @@ -467,6 +467,57 @@ public void testSecond() throws IOException { verifyDataRows(result, rows(0)); } + @Test + public void testSecondOfMinute() throws IOException { + JSONObject result = executeQuery("select second_of_minute(timestamp('2020-09-16 17:30:00'))"); + verifySchema(result, schema("second_of_minute(timestamp('2020-09-16 17:30:00'))", null, "integer")); + verifyDataRows(result, rows(0)); + + result = executeQuery("select second_of_minute(time('17:30:00'))"); + verifySchema(result, schema("second_of_minute(time('17:30:00'))", null, "integer")); + verifyDataRows(result, rows(0)); + + result = executeQuery("select second_of_minute('2020-09-16 17:30:00')"); + verifySchema(result, schema("second_of_minute('2020-09-16 17:30:00')", null, "integer")); + verifyDataRows(result, rows(0)); + + result = executeQuery("select second_of_minute('17:30:00')"); + verifySchema(result, schema("second_of_minute('17:30:00')", null, "integer")); + verifyDataRows(result, rows(0)); + } + + @Test + public void testSecondFunctionAliasesReturnTheSameResults() throws IOException { + JSONObject result1 = executeQuery("SELECT second(date('2022-11-22 12:23:34'))"); + JSONObject result2 = executeQuery("SELECT second_of_minute(date('2022-11-22 12:23:34'))"); + verifyDataRows(result1, rows(34)); + result1.getJSONArray("datarows").similar(result2.getJSONArray("datarows")); + + result1 = executeQuery(String.format( + "SELECT second(CAST(date0 AS date)) FROM %s", TEST_INDEX_CALCS)); + result2 = executeQuery(String.format( + "SELECT second_of_minute(CAST(date0 AS date)) FROM %s", TEST_INDEX_CALCS)); + result1.getJSONArray("datarows").similar(result2.getJSONArray("datarows")); + + result1 = executeQuery(String.format( + "SELECT second(datetime(CAST(time0 AS STRING))) FROM %s", TEST_INDEX_CALCS)); + result2 = executeQuery(String.format( + "SELECT second_of_minute(datetime(CAST(time0 AS STRING))) FROM %s", TEST_INDEX_CALCS)); + result1.getJSONArray("datarows").similar(result2.getJSONArray("datarows")); + + result1 = executeQuery(String.format( + "SELECT second(CAST(time0 AS STRING)) FROM %s", TEST_INDEX_CALCS)); + result2 = executeQuery(String.format( + "SELECT second_of_minute(CAST(time0 AS STRING)) FROM %s", TEST_INDEX_CALCS)); + result1.getJSONArray("datarows").similar(result2.getJSONArray("datarows")); + + result1 = executeQuery(String.format( + "SELECT second(CAST(datetime0 AS timestamp)) FROM %s", TEST_INDEX_CALCS)); + result2 = executeQuery(String.format( + "SELECT second_of_minute(CAST(datetime0 AS timestamp)) FROM %s", TEST_INDEX_CALCS)); + result1.getJSONArray("datarows").similar(result2.getJSONArray("datarows")); + } + @Test public void testSubDate() throws IOException { JSONObject result = 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 bfd0f93ec9..c2ba176cda 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 @@ -265,6 +265,14 @@ public void can_parse_multi_match_relevance_function() { + "operator='AND', tie_breaker=0.3, type = \"most_fields\", fuzziness = \"AUTO\")")); } + @Test + public void can_parse_second_functions() { + assertNotNull(parser.parse("SELECT second('12:23:34')")); + assertNotNull(parser.parse("SELECT second_of_minute('2022-11-18')")); + assertNotNull(parser.parse("SELECT second('2022-11-18 12:23:34')")); + assertNotNull(parser.parse("SELECT second_of_minute('2022-11-18 12:23:34')")); + } + @Test public void can_parse_simple_query_string_relevance_function() { assertNotNull(parser.parse( From 640b04258dd69152449fc9f6dc44ad8985736988 Mon Sep 17 00:00:00 2001 From: GabeFernandez310 Date: Tue, 20 Dec 2022 15:10:58 -0800 Subject: [PATCH 02/11] Added Implementation and Docs Signed-off-by: GabeFernandez310 --- .../src/main/java/org/opensearch/sql/expression/DSL.java | 4 ++++ .../sql/expression/datetime/DateTimeFunction.java | 7 ++++--- .../sql/expression/function/BuiltinFunctionName.java | 1 + docs/user/dql/functions.rst | 9 +++++++++ sql/src/main/antlr/OpenSearchSQLParser.g4 | 1 + 5 files changed, 19 insertions(+), 3 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 fc425c6c20..1a1e939d37 100644 --- a/core/src/main/java/org/opensearch/sql/expression/DSL.java +++ b/core/src/main/java/org/opensearch/sql/expression/DSL.java @@ -374,6 +374,10 @@ public static FunctionExpression second(Expression... expressions) { return compile(FunctionProperties.None, BuiltinFunctionName.SECOND, expressions); } + public static FunctionExpression second_of_minute(Expression... expressions) { + return compile(FunctionProperties.None, BuiltinFunctionName.SECOND_OF_MINUTE, expressions); + } + public static FunctionExpression subdate(Expression... expressions) { return compile(FunctionProperties.None, BuiltinFunctionName.SUBDATE, 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 a111f672af..d2f7f3fccc 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 @@ -120,7 +120,8 @@ public void register(BuiltinFunctionRepository repository) { repository.register(period_add()); repository.register(period_diff()); repository.register(quarter()); - repository.register(second()); + repository.register(second(BuiltinFunctionName.SECOND)); + repository.register(second(BuiltinFunctionName.SECOND_OF_MINUTE)); repository.register(subdate()); repository.register(sysdate()); repository.register(time()); @@ -492,8 +493,8 @@ private DefaultFunctionResolver quarter() { /** * SECOND(STRING/TIME/DATETIME/TIMESTAMP). return the second value for time. */ - private DefaultFunctionResolver second() { - return define(BuiltinFunctionName.SECOND.getName(), + private DefaultFunctionResolver second(BuiltinFunctionName name) { + return define(name.getName(), impl(nullMissingHandling(DateTimeFunction::exprSecond), INTEGER, STRING), impl(nullMissingHandling(DateTimeFunction::exprSecond), INTEGER, TIME), impl(nullMissingHandling(DateTimeFunction::exprSecond), INTEGER, DATETIME), 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 b23c7613d6..0476a912f9 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 @@ -84,6 +84,7 @@ public enum BuiltinFunctionName { PERIOD_DIFF(FunctionName.of("period_diff")), QUARTER(FunctionName.of("quarter")), SECOND(FunctionName.of("second")), + SECOND_OF_MINUTE(FunctionName.of("second_of_minute")), SUBDATE(FunctionName.of("subdate")), TIME(FunctionName.of("time")), TIME_TO_SEC(FunctionName.of("time_to_sec")), diff --git a/docs/user/dql/functions.rst b/docs/user/dql/functions.rst index 843d6c7e45..bb130fd29b 100644 --- a/docs/user/dql/functions.rst +++ b/docs/user/dql/functions.rst @@ -1919,6 +1919,7 @@ Description >>>>>>>>>>> Usage: second(time) returns the second for time, in the range 0 to 59. +The function `second_of_minute` is provided as an alias Argument type: STRING/TIME/DATETIME/TIMESTAMP @@ -1934,6 +1935,14 @@ Example:: | 3 | +-----------------------------+ + os> SELECT SECOND_OF_MINUTE((TIME '01:02:03')) + fetched rows / total rows = 1/1 + +---------------------------------------+ + | SECOND_OF_MINUTE((TIME '01:02:03')) | + |---------------------------------------| + | 3 | + +---------------------------------------+ + SUBDATE ------- diff --git a/sql/src/main/antlr/OpenSearchSQLParser.g4 b/sql/src/main/antlr/OpenSearchSQLParser.g4 index 47a43362ea..b8a5acc2c7 100644 --- a/sql/src/main/antlr/OpenSearchSQLParser.g4 +++ b/sql/src/main/antlr/OpenSearchSQLParser.g4 @@ -442,6 +442,7 @@ dateTimeFunctionName | PERIOD_DIFF | QUARTER | SECOND + | SECOND_OF_MINUTE | SUBDATE | SYSDATE | TIME From c0175a8ae25acdb560b48c1365a887c2e32f1aee Mon Sep 17 00:00:00 2001 From: GabeFernandez310 Date: Tue, 20 Dec 2022 16:39:48 -0800 Subject: [PATCH 03/11] Fixed IT Test And Checkstyle Issue Signed-off-by: GabeFernandez310 --- .../sql/expression/datetime/DateTimeFunctionTest.java | 9 ++++++--- .../org/opensearch/sql/sql/DateTimeFunctionIT.java | 10 ++-------- 2 files changed, 8 insertions(+), 11 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 9aa8379ddc..2da9993f75 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 @@ -842,11 +842,14 @@ public void secondOfMinute() { lenient().when(nullRef.valueOf(env)).thenReturn(nullValue()); lenient().when(missingRef.valueOf(env)).thenReturn(missingValue()); - FunctionExpression expression1 = DSL.second_of_minute(DSL.literal(new ExprTimeValue("01:02:03"))); + FunctionExpression expression1 = DSL.second_of_minute( + DSL.literal(new ExprTimeValue("01:02:03"))); FunctionExpression expression2 = DSL.second_of_minute(DSL.literal("01:02:03")); FunctionExpression expression3 = DSL.second_of_minute(DSL.literal("2020-08-17 01:02:03")); - FunctionExpression expression4 = DSL.second_of_minute(DSL.literal(new ExprTimestampValue("2020-08-17 01:02:03"))); - FunctionExpression expression5 = DSL.second_of_minute(DSL.literal(new ExprDatetimeValue("2020-08-17 01:02:03"))); + FunctionExpression expression4 = DSL.second_of_minute( + DSL.literal(new ExprTimestampValue("2020-08-17 01:02:03"))); + FunctionExpression expression5 = DSL.second_of_minute( + DSL.literal(new ExprDatetimeValue("2020-08-17 01:02:03"))); assertAll( () -> testSecondOfMinute(expression1, 3, "second_of_minute(TIME '01:02:03')"), 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 1640c19777..b48bd0e36b 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 @@ -488,17 +488,11 @@ public void testSecondOfMinute() throws IOException { @Test public void testSecondFunctionAliasesReturnTheSameResults() throws IOException { - JSONObject result1 = executeQuery("SELECT second(date('2022-11-22 12:23:34'))"); - JSONObject result2 = executeQuery("SELECT second_of_minute(date('2022-11-22 12:23:34'))"); + JSONObject result1 = executeQuery("SELECT second('2022-11-22 12:23:34')"); + JSONObject result2 = executeQuery("SELECT second_of_minute('2022-11-22 12:23:34')"); verifyDataRows(result1, rows(34)); result1.getJSONArray("datarows").similar(result2.getJSONArray("datarows")); - result1 = executeQuery(String.format( - "SELECT second(CAST(date0 AS date)) FROM %s", TEST_INDEX_CALCS)); - result2 = executeQuery(String.format( - "SELECT second_of_minute(CAST(date0 AS date)) FROM %s", TEST_INDEX_CALCS)); - result1.getJSONArray("datarows").similar(result2.getJSONArray("datarows")); - result1 = executeQuery(String.format( "SELECT second(datetime(CAST(time0 AS STRING))) FROM %s", TEST_INDEX_CALCS)); result2 = executeQuery(String.format( From 1c3ac2789c290fa37f48ac743070eb80b6fba24f Mon Sep 17 00:00:00 2001 From: GabeFernandez310 Date: Tue, 27 Dec 2022 08:04:57 -0800 Subject: [PATCH 04/11] Added Support For Date Type Signed-off-by: GabeFernandez310 --- .../opensearch/sql/expression/datetime/DateTimeFunction.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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 d2f7f3fccc..8e0b28d28d 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 @@ -7,6 +7,7 @@ package org.opensearch.sql.expression.datetime; import static java.time.temporal.ChronoUnit.MONTHS; +import static java.time.temporal.ChronoUnit.SECONDS; import static org.opensearch.sql.data.type.ExprCoreType.DATE; import static org.opensearch.sql.data.type.ExprCoreType.DATETIME; import static org.opensearch.sql.data.type.ExprCoreType.DOUBLE; @@ -497,6 +498,7 @@ private DefaultFunctionResolver second(BuiltinFunctionName name) { return define(name.getName(), impl(nullMissingHandling(DateTimeFunction::exprSecond), INTEGER, STRING), impl(nullMissingHandling(DateTimeFunction::exprSecond), INTEGER, TIME), + impl(nullMissingHandling(DateTimeFunction::exprSecond), INTEGER, DATE), impl(nullMissingHandling(DateTimeFunction::exprSecond), INTEGER, DATETIME), impl(nullMissingHandling(DateTimeFunction::exprSecond), INTEGER, TIMESTAMP) ); @@ -997,7 +999,8 @@ private ExprValue exprQuarter(ExprValue date) { * @return ExprValue. */ private ExprValue exprSecond(ExprValue time) { - return new ExprIntegerValue(time.timeValue().getSecond()); + return new ExprIntegerValue( + (SECONDS.between(LocalTime.MIN, time.timeValue()) % 60)); } /** From 4767c53db25cd2f9492da3b83f4c45dd6452525a Mon Sep 17 00:00:00 2001 From: GabeFernandez310 Date: Fri, 30 Dec 2022 11:27:49 -0800 Subject: [PATCH 05/11] Changed Docs Signed-off-by: GabeFernandez310 --- docs/user/dql/functions.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/user/dql/functions.rst b/docs/user/dql/functions.rst index bb130fd29b..53d0b51ef6 100644 --- a/docs/user/dql/functions.rst +++ b/docs/user/dql/functions.rst @@ -1919,7 +1919,7 @@ Description >>>>>>>>>>> Usage: second(time) returns the second for time, in the range 0 to 59. -The function `second_of_minute` is provided as an alias +The function `second_of_minute`_ is provided as an alias Argument type: STRING/TIME/DATETIME/TIMESTAMP From 2b2f8b8b3873a049fe4c401c47a7eaaa71db154f Mon Sep 17 00:00:00 2001 From: GabeFernandez310 Date: Tue, 3 Jan 2023 12:10:19 -0800 Subject: [PATCH 06/11] Addressed PR Comments Signed-off-by: GabeFernandez310 --- .../datetime/DateTimeFunctionTest.java | 22 +++++++++---------- docs/user/dql/functions.rst | 12 +++++----- 2 files changed, 17 insertions(+), 17 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 2da9993f75..e6506807c0 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 @@ -831,7 +831,7 @@ public void second() { assertEquals("second(DATETIME '2020-08-17 01:02:03')", expression.toString()); } - public void testSecondOfMinute(FunctionExpression dateExpression, int second, String testExpr) { + private void secondOfMinuteQuery(FunctionExpression dateExpression, int second, String testExpr) { assertEquals(INTEGER, dateExpression.type()); assertEquals(integerValue(second), eval(dateExpression)); assertEquals(testExpr, dateExpression.toString()); @@ -852,17 +852,17 @@ public void secondOfMinute() { DSL.literal(new ExprDatetimeValue("2020-08-17 01:02:03"))); assertAll( - () -> testSecondOfMinute(expression1, 3, "second_of_minute(TIME '01:02:03')"), - () -> testSecondOfMinute(expression2, 3, "second_of_minute(\"01:02:03\")"), - () -> testSecondOfMinute(expression3, 3, "second_of_minute(\"2020-08-17 01:02:03\")"), - () -> testSecondOfMinute( + () -> secondOfMinuteQuery(expression1, 3, "second_of_minute(TIME '01:02:03')"), + () -> secondOfMinuteQuery(expression2, 3, "second_of_minute(\"01:02:03\")"), + () -> secondOfMinuteQuery(expression3, 3, "second_of_minute(\"2020-08-17 01:02:03\")"), + () -> secondOfMinuteQuery( expression4, 3, "second_of_minute(TIMESTAMP '2020-08-17 01:02:03')"), - () -> testSecondOfMinute( + () -> secondOfMinuteQuery( expression5, 3, "second_of_minute(DATETIME '2020-08-17 01:02:03')") ); } - public void testInvalidSecondOfMinute(String time) { + private void invalidSecondOfMinuteQuery(String time) { FunctionExpression expression = DSL.second_of_minute(DSL.literal(new ExprTimeValue(time))); eval(expression); } @@ -875,16 +875,16 @@ public void secondOfMinuteInvalidArguments() { assertEquals(missingValue(), eval(DSL.second_of_minute(missingRef))); //Invalid Seconds - assertThrows(SemanticCheckException.class, () -> testInvalidSecondOfMinute("12:23:61")); + assertThrows(SemanticCheckException.class, () -> invalidSecondOfMinuteQuery("12:23:61")); //Invalid Minutes - assertThrows(SemanticCheckException.class, () -> testInvalidSecondOfMinute("12:61:34")); + assertThrows(SemanticCheckException.class, () -> invalidSecondOfMinuteQuery("12:61:34")); //Invalid Hours - assertThrows(SemanticCheckException.class, () -> testInvalidSecondOfMinute("25:23:34")); + assertThrows(SemanticCheckException.class, () -> invalidSecondOfMinuteQuery("25:23:34")); //incorrect format - assertThrows(SemanticCheckException.class, () -> testInvalidSecondOfMinute("asdfasdf")); + assertThrows(SemanticCheckException.class, () -> invalidSecondOfMinuteQuery("asdfasdf")); } diff --git a/docs/user/dql/functions.rst b/docs/user/dql/functions.rst index 53d0b51ef6..0c606c1ac8 100644 --- a/docs/user/dql/functions.rst +++ b/docs/user/dql/functions.rst @@ -1935,13 +1935,13 @@ Example:: | 3 | +-----------------------------+ - os> SELECT SECOND_OF_MINUTE((TIME '01:02:03')) + os> SELECT SECOND_OF_MINUTE(time('01:02:03')) fetched rows / total rows = 1/1 - +---------------------------------------+ - | SECOND_OF_MINUTE((TIME '01:02:03')) | - |---------------------------------------| - | 3 | - +---------------------------------------+ + +--------------------------------------+ + | SECOND_OF_MINUTE(time('01:02:03')) | + |--------------------------------------| + | 3 | + +--------------------------------------+ SUBDATE From 4e5d2f8eee484f826fc7e07a7caa1c57bb0093d7 Mon Sep 17 00:00:00 2001 From: GabeFernandez310 Date: Tue, 3 Jan 2023 12:38:01 -0800 Subject: [PATCH 07/11] Fixed Test Signed-off-by: GabeFernandez310 --- .../datetime/DateTimeFunctionTest.java | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 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 e6506807c0..c0cb0b7c85 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 @@ -871,20 +871,23 @@ private void invalidSecondOfMinuteQuery(String time) { public void secondOfMinuteInvalidArguments() { when(nullRef.type()).thenReturn(TIME); when(missingRef.type()).thenReturn(TIME); - assertEquals(nullValue(), eval(DSL.second_of_minute(nullRef))); - assertEquals(missingValue(), eval(DSL.second_of_minute(missingRef))); - //Invalid Seconds - assertThrows(SemanticCheckException.class, () -> invalidSecondOfMinuteQuery("12:23:61")); + assertAll( + () -> assertEquals(nullValue(), eval(DSL.second_of_minute(nullRef))), + () -> assertEquals(missingValue(), eval(DSL.second_of_minute(missingRef))), + //Invalid Seconds + () -> assertThrows(SemanticCheckException.class, () -> invalidSecondOfMinuteQuery("12:23:61")), + //Invalid Minutes + () -> assertThrows(SemanticCheckException.class, () -> invalidSecondOfMinuteQuery("12:61:34")), + //Invalid Hours + () -> assertThrows(SemanticCheckException.class, () -> invalidSecondOfMinuteQuery("25:23:34")), + //incorrect format + () -> assertThrows(SemanticCheckException.class, () -> invalidSecondOfMinuteQuery("asdfasdf")) + ); + - //Invalid Minutes - assertThrows(SemanticCheckException.class, () -> invalidSecondOfMinuteQuery("12:61:34")); - //Invalid Hours - assertThrows(SemanticCheckException.class, () -> invalidSecondOfMinuteQuery("25:23:34")); - //incorrect format - assertThrows(SemanticCheckException.class, () -> invalidSecondOfMinuteQuery("asdfasdf")); } From e1b9ce50d90f7327b570bca7954b7dc6b03c8e9a Mon Sep 17 00:00:00 2001 From: GabeFernandez310 Date: Tue, 3 Jan 2023 13:00:02 -0800 Subject: [PATCH 08/11] Fixed Checkstyle Signed-off-by: GabeFernandez310 --- .../expression/datetime/DateTimeFunctionTest.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 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 c0cb0b7c85..454e8ebfe2 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 @@ -876,13 +876,17 @@ public void secondOfMinuteInvalidArguments() { () -> assertEquals(nullValue(), eval(DSL.second_of_minute(nullRef))), () -> assertEquals(missingValue(), eval(DSL.second_of_minute(missingRef))), //Invalid Seconds - () -> assertThrows(SemanticCheckException.class, () -> invalidSecondOfMinuteQuery("12:23:61")), + () -> assertThrows(SemanticCheckException.class, + () -> invalidSecondOfMinuteQuery("12:23:61")), //Invalid Minutes - () -> assertThrows(SemanticCheckException.class, () -> invalidSecondOfMinuteQuery("12:61:34")), + () -> assertThrows(SemanticCheckException.class, + () -> invalidSecondOfMinuteQuery("12:61:34")), //Invalid Hours - () -> assertThrows(SemanticCheckException.class, () -> invalidSecondOfMinuteQuery("25:23:34")), + () -> assertThrows(SemanticCheckException.class, + () -> invalidSecondOfMinuteQuery("25:23:34")), //incorrect format - () -> assertThrows(SemanticCheckException.class, () -> invalidSecondOfMinuteQuery("asdfasdf")) + () -> assertThrows(SemanticCheckException.class, + () -> invalidSecondOfMinuteQuery("asdfasdf")) ); From 38832330a5a51d3a90b66c3123fa9a9c9120aece Mon Sep 17 00:00:00 2001 From: GabeFernandez310 Date: Thu, 5 Jan 2023 08:42:12 -0800 Subject: [PATCH 09/11] Refactored Test Signed-off-by: GabeFernandez310 --- .../datetime/DateTimeFunctionTest.java | 57 ++++++++++++------- 1 file changed, 37 insertions(+), 20 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 454e8ebfe2..66c22907f1 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 @@ -28,10 +28,15 @@ import com.google.common.collect.ImmutableList; import java.time.LocalDate; import java.util.List; +import java.util.stream.Stream; + import lombok.AllArgsConstructor; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.opensearch.sql.data.model.ExprDateValue; @@ -46,6 +51,7 @@ import org.opensearch.sql.expression.Expression; import org.opensearch.sql.expression.ExpressionTestBase; import org.opensearch.sql.expression.FunctionExpression; +import org.opensearch.sql.expression.LiteralExpression; import org.opensearch.sql.expression.env.Environment; @ExtendWith(MockitoExtension.class) @@ -837,29 +843,40 @@ private void secondOfMinuteQuery(FunctionExpression dateExpression, int second, assertEquals(testExpr, dateExpression.toString()); } - @Test - public void secondOfMinute() { + private static Stream getTestDataForSecondOfMinute() { + return Stream.of( + Arguments.of( + DSL.literal(new ExprTimeValue("01:02:03")), + 3, + "second_of_minute(TIME '01:02:03')"), + Arguments.of( + DSL.literal("01:02:03"), + 3, + "second_of_minute(\"01:02:03\")"), + Arguments.of( + DSL.literal("2020-08-17 01:02:03"), + 3, + "second_of_minute(\"2020-08-17 01:02:03\")"), + Arguments.of( + + DSL.literal(new ExprTimestampValue("2020-08-17 01:02:03")), + 3, + "second_of_minute(TIMESTAMP '2020-08-17 01:02:03')"), + Arguments.of( + + DSL.literal(new ExprDatetimeValue("2020-08-17 01:02:03")), + 3, + "second_of_minute(DATETIME '2020-08-17 01:02:03')") + ); + } + + @ParameterizedTest(name="{2}") + @MethodSource("getTestDataForSecondOfMinute") + public void secondOfMinute(LiteralExpression arg, int expectedResult, String expectedString) { lenient().when(nullRef.valueOf(env)).thenReturn(nullValue()); lenient().when(missingRef.valueOf(env)).thenReturn(missingValue()); - FunctionExpression expression1 = DSL.second_of_minute( - DSL.literal(new ExprTimeValue("01:02:03"))); - FunctionExpression expression2 = DSL.second_of_minute(DSL.literal("01:02:03")); - FunctionExpression expression3 = DSL.second_of_minute(DSL.literal("2020-08-17 01:02:03")); - FunctionExpression expression4 = DSL.second_of_minute( - DSL.literal(new ExprTimestampValue("2020-08-17 01:02:03"))); - FunctionExpression expression5 = DSL.second_of_minute( - DSL.literal(new ExprDatetimeValue("2020-08-17 01:02:03"))); - - assertAll( - () -> secondOfMinuteQuery(expression1, 3, "second_of_minute(TIME '01:02:03')"), - () -> secondOfMinuteQuery(expression2, 3, "second_of_minute(\"01:02:03\")"), - () -> secondOfMinuteQuery(expression3, 3, "second_of_minute(\"2020-08-17 01:02:03\")"), - () -> secondOfMinuteQuery( - expression4, 3, "second_of_minute(TIMESTAMP '2020-08-17 01:02:03')"), - () -> secondOfMinuteQuery( - expression5, 3, "second_of_minute(DATETIME '2020-08-17 01:02:03')") - ); + secondOfMinuteQuery(DSL.second_of_minute(arg), expectedResult, expectedString); } private void invalidSecondOfMinuteQuery(String time) { From 26a9c3b8c97dd32b20600e684d471b436d0a52e5 Mon Sep 17 00:00:00 2001 From: GabeFernandez310 Date: Thu, 5 Jan 2023 09:45:23 -0800 Subject: [PATCH 10/11] Fixed Checkstyle Signed-off-by: GabeFernandez310 --- .../sql/expression/datetime/DateTimeFunctionTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 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 66c22907f1..cafe3db487 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 @@ -29,7 +29,6 @@ import java.time.LocalDate; import java.util.List; import java.util.stream.Stream; - import lombok.AllArgsConstructor; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -870,7 +869,7 @@ private static Stream getTestDataForSecondOfMinute() { ); } - @ParameterizedTest(name="{2}") + @ParameterizedTest(name = "{2}") @MethodSource("getTestDataForSecondOfMinute") public void secondOfMinute(LiteralExpression arg, int expectedResult, String expectedString) { lenient().when(nullRef.valueOf(env)).thenReturn(nullValue()); From 5d4f52db15307a1d070db31e8c26ed8c1943cf0a Mon Sep 17 00:00:00 2001 From: GabeFernandez310 Date: Thu, 5 Jan 2023 13:22:41 -0800 Subject: [PATCH 11/11] Fixed Whitespace Signed-off-by: GabeFernandez310 --- .../sql/expression/datetime/DateTimeFunctionTest.java | 4 ---- 1 file changed, 4 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 cafe3db487..17513a7d4b 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 @@ -904,10 +904,6 @@ public void secondOfMinuteInvalidArguments() { () -> assertThrows(SemanticCheckException.class, () -> invalidSecondOfMinuteQuery("asdfasdf")) ); - - - - }