From 227e3894322b50c22f01677427ffd9764b326fc7 Mon Sep 17 00:00:00 2001 From: MitchellGale-BitQuill Date: Thu, 8 Dec 2022 01:18:56 -0800 Subject: [PATCH 1/6] Updated UTC TIME Functions for like now FunctionProperties. Signed-off-by: MitchellGale-BitQuill --- .../org/opensearch/sql/expression/DSL.java | 16 +++++ .../expression/datetime/DateTimeFunction.java | 61 ++++++++++++++++ .../function/BuiltinFunctionName.java | 3 + .../datetime/NowLikeFunctionTest.java | 18 +++++ docs/user/dql/functions.rst | 68 ++++++++++++++++++ docs/user/ppl/functions/datetime.rst | 69 +++++++++++++++++++ .../sql/ppl/DateTimeFunctionIT.java | 30 ++++++++ .../sql/sql/DateTimeFunctionIT.java | 35 ++++++++++ .../ppl/parser/AstNowLikeFunctionTest.java | 6 +- sql/src/main/antlr/OpenSearchSQLLexer.g4 | 2 +- .../sql/sql/antlr/SQLSyntaxParserTest.java | 5 +- 11 files changed, 310 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..26a15c0ee4 100644 --- a/core/src/main/java/org/opensearch/sql/expression/DSL.java +++ b/core/src/main/java/org/opensearch/sql/expression/DSL.java @@ -768,6 +768,22 @@ public static FunctionExpression current_date(FunctionProperties functionPropert return compile(functionProperties, BuiltinFunctionName.CURRENT_DATE, args); } + public static FunctionExpression utc_date(FunctionProperties functionProperties, + Expression... args) { + return compile(functionProperties, BuiltinFunctionName.UTC_DATE, args); + } + + public static FunctionExpression utc_time(FunctionProperties functionProperties, + Expression... args) { + return compile(functionProperties, BuiltinFunctionName.UTC_TIME, args); + } + + public static FunctionExpression utc_timestamp(FunctionProperties functionProperties, + Expression... args) { + return compile(functionProperties, BuiltinFunctionName.UTC_TIMESTAMP, args); + + } + @SuppressWarnings("unchecked") private static T compile(FunctionProperties functionProperties, 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..4d1f0ac9da 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; @@ -63,6 +64,7 @@ import org.opensearch.sql.expression.function.DefaultFunctionResolver; import org.opensearch.sql.expression.function.FunctionDSL; import org.opensearch.sql.expression.function.FunctionName; +import org.opensearch.sql.expression.function.FunctionProperties; import org.opensearch.sql.expression.function.FunctionResolver; import org.opensearch.sql.utils.DateTimeUtils; @@ -126,6 +128,9 @@ public void register(BuiltinFunctionRepository repository) { repository.register(time()); repository.register(time_to_sec()); repository.register(timestamp()); + repository.register(utc_date()); + repository.register(utc_time()); + repository.register(utc_timestamp()); repository.register(date_format()); repository.register(to_days()); repository.register(unix_timestamp()); @@ -566,6 +571,33 @@ private FunctionResolver unix_timestamp() { ); } + /** + * UTC_DATE(). return the current UTC Date in format yyyy-MM-dd + */ + private DefaultFunctionResolver utc_date() { + return define(BuiltinFunctionName.UTC_DATE.getName(), + implWithProperties(functionProperties + -> DateTimeFunction.exprUtcDate(functionProperties), DATE)); + } + + /** + * UTC_TIME(). return the current UTC Time in format HH:mm:ss + */ + private DefaultFunctionResolver utc_time() { + return define(BuiltinFunctionName.UTC_TIME.getName(), + implWithProperties(functionProperties + -> DateTimeFunction.exprUtcTime(functionProperties), TIME)); + } + + /** + * UTC_TIMESTAMP(). return the current UTC TimeStamp in format yyyy-MM-dd HH:mm:ss + */ + private DefaultFunctionResolver utc_timestamp() { + return define(BuiltinFunctionName.UTC_TIMESTAMP.getName(), + implWithProperties(functionProperties + -> DateTimeFunction.exprUtcTimeStamp(functionProperties), DATETIME)); + } + /** * WEEK(DATE[,mode]). return the week number for date. */ @@ -1063,6 +1095,35 @@ private ExprValue exprTimeToSec(ExprValue time) { return new ExprLongValue(time.timeValue().toSecondOfDay()); } + /** + * UTC_DATE implementation for ExprValue. + * + * @return ExprValue. + */ + private ExprValue exprUtcDate(FunctionProperties functionProperties) { + return new ExprDateValue(LocalDate.now(functionProperties.getQueryStartClock())); + } + + /** + * UTC_TIME implementation for ExprValue. + * + * @return ExprValue. + */ + private ExprValue exprUtcTime(FunctionProperties functionProperties) { + return new ExprTimeValue(LocalTime.now(functionProperties.getQueryStartClock()).withNano(0)); + } + + /** + * UTC_TIMESTAMP implementation for ExprValue. + * + * @return ExprValue. + */ + private ExprValue exprUtcTimeStamp(FunctionProperties functionProperties) { + return new ExprDatetimeValue( + LocalDateTime.now( + functionProperties.getQueryStartClock()).withNano(0)); + } + /** * To_days implementation for ExprValue. * 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..28d423676b 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 @@ -90,6 +90,9 @@ public enum BuiltinFunctionName { TIMESTAMP(FunctionName.of("timestamp")), DATE_FORMAT(FunctionName.of("date_format")), TO_DAYS(FunctionName.of("to_days")), + UTC_DATE(FunctionName.of("utc_date")), + UTC_TIME(FunctionName.of("utc_time")), + UTC_TIMESTAMP(FunctionName.of("utc_timestamp")), UNIX_TIMESTAMP(FunctionName.of("unix_timestamp")), WEEK(FunctionName.of("week")), WEEK_OF_YEAR(FunctionName.of("week_of_year")), diff --git a/core/src/test/java/org/opensearch/sql/expression/datetime/NowLikeFunctionTest.java b/core/src/test/java/org/opensearch/sql/expression/datetime/NowLikeFunctionTest.java index 6f7548b5cb..9b62114548 100644 --- a/core/src/test/java/org/opensearch/sql/expression/datetime/NowLikeFunctionTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/datetime/NowLikeFunctionTest.java @@ -103,6 +103,24 @@ void current_date() { () -> LocalDate.now(functionProperties.getQueryStartClock())); } + @Test + void utc_date() { + test_now_like_functions(DSL::utc_date, DATE, false, + () -> LocalDate.now(functionProperties.getQueryStartClock())); + } + + @Test + void utc_time() { + test_now_like_functions(DSL::utc_time, TIME, false, + () -> LocalTime.now(functionProperties.getQueryStartClock())); + } + + @Test + void utc_timestamp() { + test_now_like_functions(DSL::utc_timestamp, DATETIME, false, + () -> LocalDateTime.now(functionProperties.getQueryStartClock())); + } + /** * Check how NOW-like functions are processed. * diff --git a/docs/user/dql/functions.rst b/docs/user/dql/functions.rst index 843d6c7e45..733a555c81 100644 --- a/docs/user/dql/functions.rst +++ b/docs/user/dql/functions.rst @@ -2119,6 +2119,74 @@ Examples:: +----------------------------------------------------+ +UTC_DATE +-------- + +Description +>>>>>>>>>>> + +Returns the current UTC date as a value in 'YYYY-MM-DD'. + +Return type: DATE + +Specification: UTC_DATE() -> DATE + +Example:: + + > SELECT UTC_DATE(); + fetched rows / total rows = 1/1 + +--------------+ + | utc_date() | + |--------------| + | 2022-10-03 | + +--------------+ + + +UTC_TIME +-------- + +Description +>>>>>>>>>>> + +Returns the current UTC time as a value in 'hh:mm:ss'. + +Return type: TIME + +Specification: UTC_TIME() -> TIME + +Example:: + + > SELECT UTC_TIME(); + fetched rows / total rows = 1/1 + +--------------+ + | utc_time() | + |--------------| + | 17:54:27 | + +--------------+ + + +UTC_TIMESTAMP +------------- + +Description +>>>>>>>>>>> + +Returns the current UTC timestamp as a value in 'YYYY-MM-DD hh:mm:ss'. + +Return type: DATETIME + +Specification: UTC_TIMESTAMP() -> DATETIME + +Example:: + + > SELECT UTC_TIMESTAMP(); + fetched rows / total rows = 1/1 + +---------------------+ + | utc_timestamp() | + |---------------------| + | 2022-10-03 17:54:28 | + +---------------------+ + WEEK ---- diff --git a/docs/user/ppl/functions/datetime.rst b/docs/user/ppl/functions/datetime.rst index ca191e2426..99ccc8e360 100644 --- a/docs/user/ppl/functions/datetime.rst +++ b/docs/user/ppl/functions/datetime.rst @@ -1251,6 +1251,75 @@ Example:: +--------------------------+-----------------------------+ +UTC_DATE +-------- + +Description +>>>>>>>>>>> + +Returns the current UTC date as a value in 'YYYY-MM-DD'. + +Return type: DATE + +Specification: UTC_DATE() -> DATE + +Example:: + + > source=people | eval `UTC_DATE()` = UTC_DATE() | fields `UTC_DATE()` + fetched rows / total rows = 1/1 + +--------------+ + | UTC_DATE() | + |--------------| + | 2022-10-03 | + +--------------+ + + +UTC_TIME +-------- + +Description +>>>>>>>>>>> + +Returns the current UTC time as a value in 'hh:mm:ss'. + +Return type: TIME + +Specification: UTC_TIME() -> TIME + +Example:: + + > source=people | eval `UTC_TIME()` = UTC_TIME() | fields `UTC_TIME()` + fetched rows / total rows = 1/1 + +--------------+ + | UTC_TIME() | + |--------------| + | 17:54:27 | + +--------------+ + + +UTC_TIMESTAMP +------------- + +Description +>>>>>>>>>>> + +Returns the current UTC timestamp as a value in 'YYYY-MM-DD hh:mm:ss'. + +Return type: DATETIME + +Specification: UTC_TIMESTAMP() -> DATETIME + +Example:: + + > source=people | eval `UTC_TIMESTAMP()` = UTC_TIMESTAMP() | fields `UTC_TIMESTAMP()` + fetched rows / total rows = 1/1 + +---------------------+ + | UTC_TIMESTAMP() | + |---------------------| + | 2022-10-03 17:54:28 | + +---------------------+ + + WEEK ---- diff --git a/integ-test/src/test/java/org/opensearch/sql/ppl/DateTimeFunctionIT.java b/integ-test/src/test/java/org/opensearch/sql/ppl/DateTimeFunctionIT.java index afabc241fe..659cef417c 100644 --- a/integ-test/src/test/java/org/opensearch/sql/ppl/DateTimeFunctionIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/ppl/DateTimeFunctionIT.java @@ -6,6 +6,8 @@ package org.opensearch.sql.ppl; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_DATE; import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_PEOPLE2; import static org.opensearch.sql.util.MatcherUtils.rows; @@ -20,6 +22,7 @@ import java.time.Duration; import java.time.LocalDateTime; import java.time.Period; +import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatterBuilder; import java.time.temporal.ChronoField; @@ -754,6 +757,33 @@ private List> nowLikeFunctionsData() { .put("referenceGetter", (Supplier) LocalDate::now) .put("parser", (BiFunction) LocalDate::parse) .put("serializationPattern", "uuuu-MM-dd") + .build(), + ImmutableMap.builder() + .put("name", "utc_date") + .put("hasFsp", false) + .put("hasShortcut", true) + .put("constValue", true) + .put("referenceGetter", (Supplier) LocalDateTime::now) + .put("parser", (BiFunction) LocalDate::parse) + .put("serializationPattern", "uuuu-MM-dd") + .build(), + ImmutableMap.builder() + .put("name", "utc_time") + .put("hasFsp", false) + .put("hasShortcut", true) + .put("constValue", true) + .put("referenceGetter", (Supplier) LocalDateTime::now) + .put("parser", (BiFunction) LocalTime::parse) + .put("serializationPattern", "HH:mm:ss") + .build(), + ImmutableMap.builder() + .put("name", "utc_timestamp") + .put("hasFsp", false) + .put("hasShortcut", true) + .put("constValue", true) + .put("referenceGetter", (Supplier) LocalDateTime::now) + .put("parser", (BiFunction) LocalDateTime::parse) + .put("serializationPattern", "uuuu-MM-dd HH:mm:ss") .build() ); } 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..bc2d680008 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 @@ -24,6 +24,8 @@ import java.time.LocalDateTime; import java.time.LocalTime; import java.time.Period; +import java.time.ZoneId; +import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatterBuilder; import java.time.temporal.ChronoField; @@ -640,6 +642,12 @@ public void testMakeDate() throws IOException { verifyDataRows(result, rows("1945-01-06", "1989-06-06")); } + private static LocalDateTime utcDateTimeNow() { + ZonedDateTime zonedDateTime = + LocalDateTime.now().atZone(TimeZone.getDefault().toZoneId()); + return zonedDateTime.withZoneSameInstant(ZoneId.of("UTC")).toLocalDateTime(); + } + private List> nowLikeFunctionsData() { return List.of( ImmutableMap.builder() @@ -722,6 +730,33 @@ private List> nowLikeFunctionsData() { .put("referenceGetter", (Supplier) LocalDate::now) .put("parser", (BiFunction) LocalDate::parse) .put("serializationPattern", "uuuu-MM-dd") + .build(), + ImmutableMap.builder() + .put("name", "utc_date") + .put("hasFsp", false) + .put("hasShortcut", true) + .put("constValue", true) + .put("referenceGetter", (Supplier) (()-> org.opensearch.sql.sql.DateTimeFunctionIT.utcDateTimeNow().toLocalDate())) + .put("parser", (BiFunction) LocalDate::parse) + .put("serializationPattern", "uuuu-MM-dd") + .build(), + ImmutableMap.builder() + .put("name", "utc_time") + .put("hasFsp", false) + .put("hasShortcut", true) + .put("constValue", true) + .put("referenceGetter", (Supplier) (()-> org.opensearch.sql.sql.DateTimeFunctionIT.utcDateTimeNow().toLocalTime())) + .put("parser", (BiFunction) LocalTime::parse) + .put("serializationPattern", "HH:mm:ss") + .build(), + ImmutableMap.builder() + .put("name", "utc_timestamp") + .put("hasFsp", false) + .put("hasShortcut", true) + .put("constValue", true) + .put("referenceGetter", (Supplier) org.opensearch.sql.sql.DateTimeFunctionIT::utcDateTimeNow) + .put("parser", (BiFunction) LocalDateTime::parse) + .put("serializationPattern", "uuuu-MM-dd HH:mm:ss") .build() ); } diff --git a/ppl/src/test/java/org/opensearch/sql/ppl/parser/AstNowLikeFunctionTest.java b/ppl/src/test/java/org/opensearch/sql/ppl/parser/AstNowLikeFunctionTest.java index 711e780f3b..078abef017 100644 --- a/ppl/src/test/java/org/opensearch/sql/ppl/parser/AstNowLikeFunctionTest.java +++ b/ppl/src/test/java/org/opensearch/sql/ppl/parser/AstNowLikeFunctionTest.java @@ -56,7 +56,11 @@ public static Iterable functionNames() { {"curtime", false, false}, {"current_time", false, false}, {"curdate", false, false}, - {"current_date", false, false} + {"current_date", false, false}, + {"current_date", false, true, true}, + {"utc_date", false, true, true}, + {"utc_time", false, true, true}, + {"utc_timestamp", false, true, true} }); } diff --git a/sql/src/main/antlr/OpenSearchSQLLexer.g4 b/sql/src/main/antlr/OpenSearchSQLLexer.g4 index a359f48be3..01016992e9 100644 --- a/sql/src/main/antlr/OpenSearchSQLLexer.g4 +++ b/sql/src/main/antlr/OpenSearchSQLLexer.g4 @@ -258,9 +258,9 @@ TIME_TO_SEC: 'TIME_TO_SEC'; TIMESTAMP: 'TIMESTAMP'; TRUNCATE: 'TRUNCATE'; TO_DAYS: 'TO_DAYS'; -UTC_DATE: 'UTC_DATE'; UNIX_TIMESTAMP: 'UNIX_TIMESTAMP'; UPPER: 'UPPER'; +UTC_DATE: 'UTC_DATE'; UTC_TIME: 'UTC_TIME'; UTC_TIMESTAMP: 'UTC_TIMESTAMP'; 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..778a62ebd8 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 @@ -167,7 +167,10 @@ private static Stream nowLikeFunctionsData() { Arguments.of("curtime", true, false), Arguments.of("current_time", true, true), Arguments.of("curdate", false, false), - Arguments.of("current_date", false, true) + Arguments.of("current_date", false, true), + Arguments.of("utc_date", false, true), + Arguments.of("utc_time", false, true), + Arguments.of("utc_timestamp", false, true) ); } From 54edf065176f7b0f321d63491c96245dfa3cffb5 Mon Sep 17 00:00:00 2001 From: MitchellGale-BitQuill Date: Fri, 9 Dec 2022 14:15:24 -0800 Subject: [PATCH 2/6] Finished updates to tests. Signed-off-by: MitchellGale-BitQuill --- .../expression/datetime/DateTimeFunction.java | 13 ++++++++----- .../expression/datetime/NowLikeFunctionTest.java | 16 +++++++++++++--- .../opensearch/sql/ppl/DateTimeFunctionIT.java | 13 +++++++------ .../opensearch/sql/sql/DateTimeFunctionIT.java | 8 ++++---- .../sql/ppl/parser/AstNowLikeFunctionTest.java | 7 +++---- 5 files changed, 35 insertions(+), 22 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 4d1f0ac9da..201655a5fe 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 @@ -1098,30 +1098,33 @@ private ExprValue exprTimeToSec(ExprValue time) { /** * UTC_DATE implementation for ExprValue. * + * @param functionProperties FunctionProperties. * @return ExprValue. */ private ExprValue exprUtcDate(FunctionProperties functionProperties) { - return new ExprDateValue(LocalDate.now(functionProperties.getQueryStartClock())); + return new ExprDateValue(exprUtcTimeStamp(functionProperties).dateValue()); } /** * UTC_TIME implementation for ExprValue. * + * @param functionProperties FunctionProperties. * @return ExprValue. */ private ExprValue exprUtcTime(FunctionProperties functionProperties) { - return new ExprTimeValue(LocalTime.now(functionProperties.getQueryStartClock()).withNano(0)); + return new ExprTimeValue(exprUtcTimeStamp(functionProperties).timeValue()); } /** * UTC_TIMESTAMP implementation for ExprValue. * + * @param functionProperties FunctionProperties. * @return ExprValue. */ private ExprValue exprUtcTimeStamp(FunctionProperties functionProperties) { - return new ExprDatetimeValue( - LocalDateTime.now( - functionProperties.getQueryStartClock()).withNano(0)); + var zdt = ZonedDateTime.now(functionProperties.getQueryStartClock()) + .withZoneSameInstant(ZoneId.of("UTC")); + return new ExprDatetimeValue(DATE_TIME_FORMATTER_STRICT_WITH_TZ.format(zdt)); } /** diff --git a/core/src/test/java/org/opensearch/sql/expression/datetime/NowLikeFunctionTest.java b/core/src/test/java/org/opensearch/sql/expression/datetime/NowLikeFunctionTest.java index 9b62114548..4b8a4aac5e 100644 --- a/core/src/test/java/org/opensearch/sql/expression/datetime/NowLikeFunctionTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/datetime/NowLikeFunctionTest.java @@ -19,10 +19,13 @@ import java.time.LocalDateTime; import java.time.LocalTime; import java.time.Period; +import java.time.ZoneId; +import java.time.ZonedDateTime; import java.time.temporal.ChronoUnit; import java.time.temporal.Temporal; import java.time.temporal.TemporalUnit; import java.util.List; +import java.util.TimeZone; import java.util.concurrent.Callable; import java.util.function.BiFunction; import java.util.function.Function; @@ -106,19 +109,26 @@ void current_date() { @Test void utc_date() { test_now_like_functions(DSL::utc_date, DATE, false, - () -> LocalDate.now(functionProperties.getQueryStartClock())); + () -> utcDateTimeNow(functionProperties).toLocalDate()); } @Test void utc_time() { test_now_like_functions(DSL::utc_time, TIME, false, - () -> LocalTime.now(functionProperties.getQueryStartClock())); + () -> utcDateTimeNow(functionProperties).toLocalTime()); } @Test void utc_timestamp() { test_now_like_functions(DSL::utc_timestamp, DATETIME, false, - () -> LocalDateTime.now(functionProperties.getQueryStartClock())); + () -> utcDateTimeNow(functionProperties)); + } + + private static LocalDateTime utcDateTimeNow(FunctionProperties functionProperties) { + ZonedDateTime zonedDateTime = + LocalDateTime.now(functionProperties.getQueryStartClock()) + .atZone(TimeZone.getDefault().toZoneId()); + return zonedDateTime.withZoneSameInstant(ZoneId.of("UTC")).toLocalDateTime(); } /** diff --git a/integ-test/src/test/java/org/opensearch/sql/ppl/DateTimeFunctionIT.java b/integ-test/src/test/java/org/opensearch/sql/ppl/DateTimeFunctionIT.java index 659cef417c..3b03ddf0be 100644 --- a/integ-test/src/test/java/org/opensearch/sql/ppl/DateTimeFunctionIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/ppl/DateTimeFunctionIT.java @@ -10,6 +10,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_DATE; import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_PEOPLE2; +import static org.opensearch.sql.sql.DateTimeFunctionIT.utcDateTimeNow; import static org.opensearch.sql.util.MatcherUtils.rows; import static org.opensearch.sql.util.MatcherUtils.schema; import static org.opensearch.sql.util.MatcherUtils.verifySchema; @@ -761,27 +762,27 @@ private List> nowLikeFunctionsData() { ImmutableMap.builder() .put("name", "utc_date") .put("hasFsp", false) - .put("hasShortcut", true) + .put("hasShortcut", false) .put("constValue", true) - .put("referenceGetter", (Supplier) LocalDateTime::now) + .put("referenceGetter", (Supplier) ()-> utcDateTimeNow().toLocalDate()) .put("parser", (BiFunction) LocalDate::parse) .put("serializationPattern", "uuuu-MM-dd") .build(), ImmutableMap.builder() .put("name", "utc_time") .put("hasFsp", false) - .put("hasShortcut", true) + .put("hasShortcut", false) .put("constValue", true) - .put("referenceGetter", (Supplier) LocalDateTime::now) + .put("referenceGetter", (Supplier) ()-> utcDateTimeNow().toLocalTime()) .put("parser", (BiFunction) LocalTime::parse) .put("serializationPattern", "HH:mm:ss") .build(), ImmutableMap.builder() .put("name", "utc_timestamp") .put("hasFsp", false) - .put("hasShortcut", true) + .put("hasShortcut", false) .put("constValue", true) - .put("referenceGetter", (Supplier) LocalDateTime::now) + .put("referenceGetter", (Supplier) ()-> utcDateTimeNow()) .put("parser", (BiFunction) LocalDateTime::parse) .put("serializationPattern", "uuuu-MM-dd HH:mm:ss") .build() 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 bc2d680008..cb477b84c0 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 @@ -642,7 +642,7 @@ public void testMakeDate() throws IOException { verifyDataRows(result, rows("1945-01-06", "1989-06-06")); } - private static LocalDateTime utcDateTimeNow() { + public static LocalDateTime utcDateTimeNow() { ZonedDateTime zonedDateTime = LocalDateTime.now().atZone(TimeZone.getDefault().toZoneId()); return zonedDateTime.withZoneSameInstant(ZoneId.of("UTC")).toLocalDateTime(); @@ -734,7 +734,7 @@ private List> nowLikeFunctionsData() { ImmutableMap.builder() .put("name", "utc_date") .put("hasFsp", false) - .put("hasShortcut", true) + .put("hasShortcut", false) .put("constValue", true) .put("referenceGetter", (Supplier) (()-> org.opensearch.sql.sql.DateTimeFunctionIT.utcDateTimeNow().toLocalDate())) .put("parser", (BiFunction) LocalDate::parse) @@ -743,7 +743,7 @@ private List> nowLikeFunctionsData() { ImmutableMap.builder() .put("name", "utc_time") .put("hasFsp", false) - .put("hasShortcut", true) + .put("hasShortcut", false) .put("constValue", true) .put("referenceGetter", (Supplier) (()-> org.opensearch.sql.sql.DateTimeFunctionIT.utcDateTimeNow().toLocalTime())) .put("parser", (BiFunction) LocalTime::parse) @@ -752,7 +752,7 @@ private List> nowLikeFunctionsData() { ImmutableMap.builder() .put("name", "utc_timestamp") .put("hasFsp", false) - .put("hasShortcut", true) + .put("hasShortcut", false) .put("constValue", true) .put("referenceGetter", (Supplier) org.opensearch.sql.sql.DateTimeFunctionIT::utcDateTimeNow) .put("parser", (BiFunction) LocalDateTime::parse) diff --git a/ppl/src/test/java/org/opensearch/sql/ppl/parser/AstNowLikeFunctionTest.java b/ppl/src/test/java/org/opensearch/sql/ppl/parser/AstNowLikeFunctionTest.java index 078abef017..ddcde513dd 100644 --- a/ppl/src/test/java/org/opensearch/sql/ppl/parser/AstNowLikeFunctionTest.java +++ b/ppl/src/test/java/org/opensearch/sql/ppl/parser/AstNowLikeFunctionTest.java @@ -57,10 +57,9 @@ public static Iterable functionNames() { {"current_time", false, false}, {"curdate", false, false}, {"current_date", false, false}, - {"current_date", false, true, true}, - {"utc_date", false, true, true}, - {"utc_time", false, true, true}, - {"utc_timestamp", false, true, true} + {"utc_date", false, false}, + {"utc_time", false, false}, + {"utc_timestamp", false, false} }); } From 8e824a092e2f0544c43eeca30f4a25bd65e65db9 Mon Sep 17 00:00:00 2001 From: MitchellGale-BitQuill Date: Fri, 9 Dec 2022 14:34:02 -0800 Subject: [PATCH 3/6] Removed unused import. Signed-off-by: MitchellGale-BitQuill --- .../org/opensearch/sql/expression/datetime/DateTimeFunction.java | 1 - 1 file changed, 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 201655a5fe..c56977d20e 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,7 +20,6 @@ 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; From 07028c9c52ef355da42620cbe92c5b6f83ebf7ad Mon Sep 17 00:00:00 2001 From: MitchellGale-BitQuill Date: Fri, 9 Dec 2022 22:01:20 -0800 Subject: [PATCH 4/6] Addressed PR comments. Signed-off-by: MitchellGale-BitQuill --- .../sql/expression/datetime/DateTimeFunction.java | 2 +- .../java/org/opensearch/sql/sql/DateTimeFunctionIT.java | 6 +++--- .../sql/ppl/antlr/NowLikeFunctionParserTest.java | 5 ++++- .../opensearch/sql/sql/parser/AstNowLikeFunctionTest.java | 7 +++++-- 4 files changed, 13 insertions(+), 7 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 c56977d20e..cf4162ac7a 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 @@ -1123,7 +1123,7 @@ private ExprValue exprUtcTime(FunctionProperties functionProperties) { private ExprValue exprUtcTimeStamp(FunctionProperties functionProperties) { var zdt = ZonedDateTime.now(functionProperties.getQueryStartClock()) .withZoneSameInstant(ZoneId.of("UTC")); - return new ExprDatetimeValue(DATE_TIME_FORMATTER_STRICT_WITH_TZ.format(zdt)); + return new ExprDatetimeValue(zdt.toLocalDateTime()); } /** 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 cb477b84c0..f60bd1efb0 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 @@ -736,7 +736,7 @@ private List> nowLikeFunctionsData() { .put("hasFsp", false) .put("hasShortcut", false) .put("constValue", true) - .put("referenceGetter", (Supplier) (()-> org.opensearch.sql.sql.DateTimeFunctionIT.utcDateTimeNow().toLocalDate())) + .put("referenceGetter", (Supplier) (()-> utcDateTimeNow().toLocalDate())) .put("parser", (BiFunction) LocalDate::parse) .put("serializationPattern", "uuuu-MM-dd") .build(), @@ -745,7 +745,7 @@ private List> nowLikeFunctionsData() { .put("hasFsp", false) .put("hasShortcut", false) .put("constValue", true) - .put("referenceGetter", (Supplier) (()-> org.opensearch.sql.sql.DateTimeFunctionIT.utcDateTimeNow().toLocalTime())) + .put("referenceGetter", (Supplier) (()-> utcDateTimeNow().toLocalTime())) .put("parser", (BiFunction) LocalTime::parse) .put("serializationPattern", "HH:mm:ss") .build(), @@ -754,7 +754,7 @@ private List> nowLikeFunctionsData() { .put("hasFsp", false) .put("hasShortcut", false) .put("constValue", true) - .put("referenceGetter", (Supplier) org.opensearch.sql.sql.DateTimeFunctionIT::utcDateTimeNow) + .put("referenceGetter", (Supplier) DateTimeFunctionIT::utcDateTimeNow) .put("parser", (BiFunction) LocalDateTime::parse) .put("serializationPattern", "uuuu-MM-dd HH:mm:ss") .build() diff --git a/ppl/src/test/java/org/opensearch/sql/ppl/antlr/NowLikeFunctionParserTest.java b/ppl/src/test/java/org/opensearch/sql/ppl/antlr/NowLikeFunctionParserTest.java index dda404f29a..fb849f6de7 100644 --- a/ppl/src/test/java/org/opensearch/sql/ppl/antlr/NowLikeFunctionParserTest.java +++ b/ppl/src/test/java/org/opensearch/sql/ppl/antlr/NowLikeFunctionParserTest.java @@ -46,7 +46,10 @@ public static Iterable functionNames() { {"curtime", true, false}, {"current_time", true, true}, {"curdate", false, false}, - {"current_date", false, true} + {"current_date", false, true}, + {"utc_date", false, false}, + {"utc_time", false, false}, + {"utc_timestamp", false, false} }); } diff --git a/sql/src/test/java/org/opensearch/sql/sql/parser/AstNowLikeFunctionTest.java b/sql/src/test/java/org/opensearch/sql/sql/parser/AstNowLikeFunctionTest.java index 19b48ca0bd..4ce2a2d3f7 100644 --- a/sql/src/test/java/org/opensearch/sql/sql/parser/AstNowLikeFunctionTest.java +++ b/sql/src/test/java/org/opensearch/sql/sql/parser/AstNowLikeFunctionTest.java @@ -32,8 +32,11 @@ private static Stream allFunctions() { "curtime", "localtimestamp", "localtime", - "now", - "sysdate") + "now", + "sysdate", + "utc_date", + "utc_time", + "utc_timestamp") .map(Arguments::of); } From ac0f9a13df75d927a5191a5d54bbbe7ef5726c36 Mon Sep 17 00:00:00 2001 From: MitchellGale-BitQuill Date: Tue, 13 Dec 2022 11:55:12 -0800 Subject: [PATCH 5/6] Removed extra import. Signed-off-by: MitchellGale-BitQuill --- .../src/test/java/org/opensearch/sql/ppl/DateTimeFunctionIT.java | 1 - 1 file changed, 1 deletion(-) diff --git a/integ-test/src/test/java/org/opensearch/sql/ppl/DateTimeFunctionIT.java b/integ-test/src/test/java/org/opensearch/sql/ppl/DateTimeFunctionIT.java index 3b03ddf0be..004d8a0648 100644 --- a/integ-test/src/test/java/org/opensearch/sql/ppl/DateTimeFunctionIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/ppl/DateTimeFunctionIT.java @@ -23,7 +23,6 @@ import java.time.Duration; import java.time.LocalDateTime; import java.time.Period; -import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatterBuilder; import java.time.temporal.ChronoField; From 1462f535626cd45b339461078cab241f70db52c6 Mon Sep 17 00:00:00 2001 From: MitchellGale-BitQuill Date: Sun, 18 Dec 2022 18:22:35 -0800 Subject: [PATCH 6/6] removed `DateTimeFunction` used within class. Signed-off-by: MitchellGale-BitQuill --- .../sql/expression/datetime/DateTimeFunction.java | 6 +++--- 1 file changed, 3 insertions(+), 3 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 cf4162ac7a..ed8063d8ff 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 @@ -576,7 +576,7 @@ private FunctionResolver unix_timestamp() { private DefaultFunctionResolver utc_date() { return define(BuiltinFunctionName.UTC_DATE.getName(), implWithProperties(functionProperties - -> DateTimeFunction.exprUtcDate(functionProperties), DATE)); + -> exprUtcDate(functionProperties), DATE)); } /** @@ -585,7 +585,7 @@ private DefaultFunctionResolver utc_date() { private DefaultFunctionResolver utc_time() { return define(BuiltinFunctionName.UTC_TIME.getName(), implWithProperties(functionProperties - -> DateTimeFunction.exprUtcTime(functionProperties), TIME)); + -> exprUtcTime(functionProperties), TIME)); } /** @@ -594,7 +594,7 @@ private DefaultFunctionResolver utc_time() { private DefaultFunctionResolver utc_timestamp() { return define(BuiltinFunctionName.UTC_TIMESTAMP.getName(), implWithProperties(functionProperties - -> DateTimeFunction.exprUtcTimeStamp(functionProperties), DATETIME)); + -> exprUtcTimeStamp(functionProperties), DATETIME)); } /**