Skip to content

Commit

Permalink
Adding UTC_DATE, UTC_TIME, UTC_TIMESTAMP (#1193) (#1198)
Browse files Browse the repository at this point in the history
* Updated UTC TIME Functions for like now FunctionProperties.

Signed-off-by: MitchellGale-BitQuill <mitchellg@bitquilltech.com>

* Finished updates to tests.

Signed-off-by: MitchellGale-BitQuill <mitchellg@bitquilltech.com>

* Removed unused import.

Signed-off-by: MitchellGale-BitQuill <mitchellg@bitquilltech.com>

* Addressed PR comments.

Signed-off-by: MitchellGale-BitQuill <mitchellg@bitquilltech.com>

* Removed extra import.

Signed-off-by: MitchellGale-BitQuill <mitchellg@bitquilltech.com>

* removed `DateTimeFunction` used within class.

Signed-off-by: MitchellGale-BitQuill <mitchellg@bitquilltech.com>

Signed-off-by: MitchellGale-BitQuill <mitchellg@bitquilltech.com>
(cherry picked from commit 94b6bec)

Co-authored-by: MitchellGale-BitQuill <104795536+MitchellGale-BitQuill@users.noreply.github.com>
  • Loading branch information
opensearch-trigger-bot[bot] and MitchellGale committed Dec 22, 2022
1 parent 7edc72e commit 13474e3
Show file tree
Hide file tree
Showing 13 changed files with 330 additions and 6 deletions.
16 changes: 16 additions & 0 deletions core/src/main/java/org/opensearch/sql/expression/DSL.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 extends FunctionImplementation>
T compile(FunctionProperties functionProperties,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,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;

Expand Down Expand Up @@ -126,6 +127,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());
Expand Down Expand Up @@ -566,6 +570,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
-> 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
-> 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
-> exprUtcTimeStamp(functionProperties), DATETIME));
}

/**
* WEEK(DATE[,mode]). return the week number for date.
*/
Expand Down Expand Up @@ -1063,6 +1094,38 @@ private ExprValue exprTimeToSec(ExprValue time) {
return new ExprLongValue(time.timeValue().toSecondOfDay());
}

/**
* UTC_DATE implementation for ExprValue.
*
* @param functionProperties FunctionProperties.
* @return ExprValue.
*/
private ExprValue exprUtcDate(FunctionProperties functionProperties) {
return new ExprDateValue(exprUtcTimeStamp(functionProperties).dateValue());
}

/**
* UTC_TIME implementation for ExprValue.
*
* @param functionProperties FunctionProperties.
* @return ExprValue.
*/
private ExprValue exprUtcTime(FunctionProperties functionProperties) {
return new ExprTimeValue(exprUtcTimeStamp(functionProperties).timeValue());
}

/**
* UTC_TIMESTAMP implementation for ExprValue.
*
* @param functionProperties FunctionProperties.
* @return ExprValue.
*/
private ExprValue exprUtcTimeStamp(FunctionProperties functionProperties) {
var zdt = ZonedDateTime.now(functionProperties.getQueryStartClock())
.withZoneSameInstant(ZoneId.of("UTC"));
return new ExprDatetimeValue(zdt.toLocalDateTime());
}

/**
* To_days implementation for ExprValue.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -103,6 +106,31 @@ void current_date() {
() -> LocalDate.now(functionProperties.getQueryStartClock()));
}

@Test
void utc_date() {
test_now_like_functions(DSL::utc_date, DATE, false,
() -> utcDateTimeNow(functionProperties).toLocalDate());
}

@Test
void utc_time() {
test_now_like_functions(DSL::utc_time, TIME, false,
() -> utcDateTimeNow(functionProperties).toLocalTime());
}

@Test
void utc_timestamp() {
test_now_like_functions(DSL::utc_timestamp, DATETIME, false,
() -> 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();
}

/**
* Check how NOW-like functions are processed.
*
Expand Down
68 changes: 68 additions & 0 deletions docs/user/dql/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
----

Expand Down
69 changes: 69 additions & 0 deletions docs/user/ppl/functions/datetime.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@

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.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;
Expand Down Expand Up @@ -754,6 +757,33 @@ private List<ImmutableMap<Object, Object>> nowLikeFunctionsData() {
.put("referenceGetter", (Supplier<Temporal>) LocalDate::now)
.put("parser", (BiFunction<CharSequence, DateTimeFormatter, Temporal>) LocalDate::parse)
.put("serializationPattern", "uuuu-MM-dd")
.build(),
ImmutableMap.builder()
.put("name", "utc_date")
.put("hasFsp", false)
.put("hasShortcut", false)
.put("constValue", true)
.put("referenceGetter", (Supplier<Temporal>) ()-> utcDateTimeNow().toLocalDate())
.put("parser", (BiFunction<CharSequence, DateTimeFormatter, Temporal>) LocalDate::parse)
.put("serializationPattern", "uuuu-MM-dd")
.build(),
ImmutableMap.builder()
.put("name", "utc_time")
.put("hasFsp", false)
.put("hasShortcut", false)
.put("constValue", true)
.put("referenceGetter", (Supplier<Temporal>) ()-> utcDateTimeNow().toLocalTime())
.put("parser", (BiFunction<CharSequence, DateTimeFormatter, Temporal>) LocalTime::parse)
.put("serializationPattern", "HH:mm:ss")
.build(),
ImmutableMap.builder()
.put("name", "utc_timestamp")
.put("hasFsp", false)
.put("hasShortcut", false)
.put("constValue", true)
.put("referenceGetter", (Supplier<Temporal>) ()-> utcDateTimeNow())
.put("parser", (BiFunction<CharSequence, DateTimeFormatter, Temporal>) LocalDateTime::parse)
.put("serializationPattern", "uuuu-MM-dd HH:mm:ss")
.build()
);
}
Expand Down
Loading

0 comments on commit 13474e3

Please sign in to comment.