Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation UTC_DATE/UTC_TIME/UTC_TIMESTAMP #127

Merged
merged 15 commits into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 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 @@ -373,6 +373,18 @@ public FunctionExpression timestamp(Expression... expressions) {
return function(BuiltinFunctionName.TIMESTAMP, expressions);
}

public FunctionExpression utc_time(Expression... expressions) {
return function(BuiltinFunctionName.UTC_TIME, expressions);
}

public FunctionExpression utc_date(Expression... expressions) {
return function(BuiltinFunctionName.UTC_DATE, expressions);
}

public FunctionExpression utc_timestamp(Expression... expressions) {
return function(BuiltinFunctionName.UTC_TIMESTAMP, expressions);
}

public FunctionExpression date_format(Expression... expressions) {
return function(BuiltinFunctionName.DATE_FORMAT, expressions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,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 @@ -532,6 +535,30 @@ 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(),
impl(DateTimeFunction::exprUtcDate, DATE));
}

/**
* UTC_TIME(). return the current UTC Time in format HH:mm:ss
*/
private DefaultFunctionResolver utc_time() {
return define(BuiltinFunctionName.UTC_TIME.getName(),
impl(DateTimeFunction::exprUtcTime, 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(),
impl(DateTimeFunction::exprUtcTimeStamp, DATETIME));
}

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

/**
* UTC_DATE implementation for ExprValue.
*
* @return ExprValue.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JavaDoc should explain what the return is.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Yury-Fridlyand any advice on how to address this if not including "yyyy-MM-dd"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can mention return value type.

*/
private ExprValue exprUtcDate() {
return new ExprDateValue(exprUtcTimeStamp().dateValue());

}

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

/**
* UTC_TIMESTAMP implementation for ExprValue.
*
* @return ExprValue.
*/
private ExprValue exprUtcTimeStamp() {
return new ExprDatetimeValue(LocalDateTime.now(ZoneId.of("UTC"))
.withNano(0));
}

/**
* To_days implementation for ExprValue.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,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")),
YEAR(FunctionName.of("year")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
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.TIME;
import static org.opensearch.sql.data.type.ExprCoreType.TIMESTAMP;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.temporal.Temporal;
import java.util.List;
import java.util.function.Function;
Expand Down Expand Up @@ -55,7 +57,26 @@ private static Stream<Arguments> functionNames() {
Arguments.of((Function<Expression[], FunctionExpression>)dsl::curdate,
"curdate", DATE, false, (Supplier<Temporal>)LocalDate::now),
Arguments.of((Function<Expression[], FunctionExpression>)dsl::current_date,
"current_date", DATE, false, (Supplier<Temporal>)LocalDate::now));
"current_date", DATE, false, (Supplier<Temporal>)LocalDate::now),
Arguments.of((Function<Expression[], FunctionExpression>)dsl::utc_date,
"utc_date", DATE, false, (Supplier<Temporal>)NowLikeFunctionTest::getUtcDateRefValue),
Arguments.of((Function<Expression[], FunctionExpression>)dsl::utc_time,
"utc_time", TIME, false, (Supplier<Temporal>)NowLikeFunctionTest::getUtcTimeRefValue),
Arguments.of((Function<Expression[], FunctionExpression>)dsl::utc_timestamp,
"utc_timestamp", DATETIME, false,
(Supplier<Temporal>)NowLikeFunctionTest::getUtcTimestampRefValue));
}

private static LocalDateTime getUtcTimestampRefValue() {
return LocalDateTime.now(ZoneId.of("UTC"));
}

private static LocalDate getUtcDateRefValue() {
return getUtcTimestampRefValue().toLocalDate();
}

private static LocalTime getUtcTimeRefValue() {
return getUtcTimestampRefValue().toLocalTime();
}

private Temporal extractValue(FunctionExpression func) {
Expand Down Expand Up @@ -86,11 +107,11 @@ private long getDiff(Temporal sample, Temporal reference) {
@ParameterizedTest(name = "{1}")
@MethodSource("functionNames")
public void test_now_like_functions(Function<Expression[], FunctionExpression> function,
@SuppressWarnings("unused") // Used in the test name above
String name,
ExprCoreType resType,
Boolean hasFsp,
Supplier<Temporal> referenceGetter) {
@SuppressWarnings("unused") // Used in the test name above

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this indentation change needed?

String name,
ExprCoreType resType,
Boolean hasFsp,
Supplier<Temporal> referenceGetter) {
// Check return types:
// `func()`
FunctionExpression expr = function.apply(new Expression[]{});
Expand All @@ -113,16 +134,18 @@ public void test_now_like_functions(Function<Expression[], FunctionExpression> f

// Check how calculations are precise:
// `func()`
// 17:59:59.99999 can pass as being equal to 18:00:00
var delta = resType == DATE ? 0 : 1;
assertTrue(Math.abs(getDiff(
extractValue(function.apply(new Expression[]{})),
referenceGetter.get()
)) <= 1);
extractValue(function.apply(new Expression[]{})),
referenceGetter.get()
)) <= delta);
if (hasFsp) {
// `func(fsp)`
assertTrue(Math.abs(getDiff(
extractValue(function.apply(new Expression[]{DSL.literal(0)})),
referenceGetter.get()
)) <= 1);
extractValue(function.apply(new Expression[]{DSL.literal(0)})),
referenceGetter.get()
)) <= delta);
}
}
}
69 changes: 69 additions & 0 deletions docs/user/dql/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1711,6 +1711,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::

> 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
acarbonetto marked this conversation as resolved.
Show resolved Hide resolved

Example::

> SELECT UTC_TIMESTAMP();
fetched rows / total rows = 1/1
+---------------------+
| utc_timestamp() |
|---------------------|
| 2022-10-03 17:54:28 |
+---------------------+


CURTIME
-------

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 @@ -1152,6 +1152,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 @@ -20,6 +20,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;
Expand Down Expand Up @@ -674,6 +676,10 @@ public void testMakeDate() throws IOException {
verifySome(result.getJSONArray("datarows"), rows("1945-01-06", "1989-06-06"));
}

private static LocalDateTime utcDateTimeNow() {
return LocalDateTime.now(ZoneId.of("UTC"));
}

private List<ImmutableMap<Object, Object>> nowLikeFunctionsData() {
return List.of(
ImmutableMap.builder()
Expand Down Expand Up @@ -756,6 +762,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", true)
.put("constValue", true)
.put("referenceGetter", (Supplier<Temporal>) (()-> DateTimeFunctionIT.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", true)
.put("constValue", true)
.put("referenceGetter", (Supplier<Temporal>) (()-> DateTimeFunctionIT.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", true)
.put("constValue", true)
.put("referenceGetter", (Supplier<Temporal>) DateTimeFunctionIT::utcDateTimeNow)
.put("parser", (BiFunction<CharSequence, DateTimeFormatter, Temporal>) LocalDateTime::parse)
.put("serializationPattern", "uuuu-MM-dd HH:mm:ss")
.build()
);
}
Expand Down
Loading