Skip to content

Commit

Permalink
Added UTC_DATE, UTC_TIME, UTC_TIMESTAMP implementations.
Browse files Browse the repository at this point in the history
Signed-off-by: MitchellGale-BitQuill <[email protected]>
  • Loading branch information
MitchellGale committed Oct 3, 2022
1 parent 057fa44 commit 134596a
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 3 deletions.
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 @@ -21,6 +21,7 @@
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;
import static org.opensearch.sql.utils.DateTimeFormatters.DATE_TIME_FORMATTER_NON_STRUCT;
import static org.opensearch.sql.utils.DateTimeFormatters.DATE_TIME_FORMATTER_SHORT_YEAR;
import static org.opensearch.sql.utils.DateTimeFormatters.DATE_TIME_FORMATTER_STRICT_WITH_TZ;

Expand Down Expand Up @@ -108,6 +109,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 +536,30 @@ private FunctionResolver unix_timestamp() {
);
}

/**
* UTC_DATE(). return the current UTC Date
*/
private DefaultFunctionResolver utc_date() {
return define(BuiltinFunctionName.UTC_DATE.getName(),
impl(DateTimeFunction::exprUtcDate, DATE));
}

/**
* UTC_TIME(). return the current UTC Time
*/
private DefaultFunctionResolver utc_time() {
return define(BuiltinFunctionName.UTC_TIME.getName(),
impl(DateTimeFunction::exprUtcTime, TIME));
}

/**
* UTC_TIMESTAMP(). return the current UTC TimeStamp
*/
private DefaultFunctionResolver utc_timestamp() {
return define(BuiltinFunctionName.UTC_TIMESTAMP.getName(),
impl(DateTimeFunction::exprUtcTimeStamp, TIMESTAMP));
}

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

/**
* To_days implementation for ExprValue.
*
* @return ExprValue.
*/
private ExprValue exprUtcDate() {
return new ExprDateValue(exprUtcTimeStamp().dateValue());

}

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

/**
* To_days implementation for ExprValue.
*
* @return ExprValue.
*/
private ExprValue exprUtcTimeStamp() {
String formattedLDT = LocalDateTime.now().format(DATE_TIME_FORMATTER_NON_STRUCT);
ExprStringValue localDateValue = new ExprStringValue(formattedLDT);
ExprStringValue toTZ = new ExprStringValue("+00:00");

return exprDateTime(localDateValue, toTZ);
}

/**
* 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 @@ -168,4 +168,10 @@ public class DateTimeFormatters {
.appendPattern("uuuu-MM-dd HH:mm:ss[xxx]")
.toFormatter()
.withResolverStyle(ResolverStyle.STRICT);

// yyyy-MM-dd HH:mm:ss
public static final DateTimeFormatter DATE_TIME_FORMATTER_NON_STRUCT =
new DateTimeFormatterBuilder()
.appendPattern("yyyy-MM-dd HH:mm:ss")
.toFormatter();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/


package org.opensearch.sql.expression.datetime;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.opensearch.sql.data.type.ExprCoreType.TIMESTAMP;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opensearch.sql.data.model.ExprValue;
import org.opensearch.sql.data.type.ExprCoreType;
import org.opensearch.sql.expression.Expression;
import org.opensearch.sql.expression.ExpressionTestBase;
import org.opensearch.sql.expression.FunctionExpression;
import org.opensearch.sql.expression.env.Environment;



@ExtendWith(MockitoExtension.class)
class UTCFunctionsTest extends ExpressionTestBase {
int allowedSecondsOffset = 60;

@Mock
Environment<Expression, ExprValue> env;

@Test
public void utcTimeStamp() {
FunctionExpression expr = dsl.utc_timestamp();
LocalDateTime result = expr.valueOf(env).datetimeValue();
assertEquals(TIMESTAMP, expr.type());

LocalDateTime currentUTC = LocalDateTime.now(ZoneOffset.UTC);
LocalDateTime lowerUTC = currentUTC.minus(allowedSecondsOffset, ChronoUnit.SECONDS);
LocalDateTime upperUTC = currentUTC.minus(-allowedSecondsOffset, ChronoUnit.SECONDS);

if (!lowerUTC.isBefore(result) || !upperUTC.isAfter(result)) {
assertTrue(Boolean.parseBoolean("TimeStamp was not within range"));
}
}

@Test
public void utcTime() {
FunctionExpression expr = dsl.utc_time();
LocalTime result = expr.valueOf(env).timeValue();
assertEquals(ExprCoreType.TIME, expr.type());

LocalDateTime currentUTC = LocalDateTime.now(ZoneOffset.UTC);
LocalTime lowerUTC = currentUTC.minus(allowedSecondsOffset, ChronoUnit.SECONDS).toLocalTime();
LocalTime upperUTC = currentUTC.minus(-allowedSecondsOffset, ChronoUnit.SECONDS).toLocalTime();

if (!lowerUTC.isBefore(result) || !upperUTC.isAfter(result)) {
assertTrue(Boolean.parseBoolean("Time was not within range"));
}
}

@Test
public void utcDate() {
FunctionExpression expr = dsl.utc_date();
LocalDate result = expr.valueOf(env).dateValue();
assertEquals(ExprCoreType.DATE, expr.type());

LocalDateTime currentUTC = LocalDateTime.now(ZoneOffset.UTC);
LocalDate lowerUTC = currentUTC.minus(allowedSecondsOffset, ChronoUnit.SECONDS).toLocalDate();
LocalDate upperUTC = currentUTC.minus(-allowedSecondsOffset, ChronoUnit.SECONDS).toLocalDate();

if (lowerUTC.isAfter(result) || upperUTC.isBefore(result)) {
assertTrue(Boolean.parseBoolean("Time was not within range"));
}
}
}
2 changes: 1 addition & 1 deletion ppl/src/main/antlr/OpenSearchPPLParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ dateAndTimeFunctionBase
: ADDDATE | CONVERT_TZ | DATE | DATETIME | DATE_ADD | DATE_FORMAT | DATE_SUB | DAY | DAYNAME | DAYOFMONTH | DAYOFWEEK
| DAYOFYEAR | FROM_DAYS | FROM_UNIXTIME | HOUR | MAKEDATE | MAKETIME | MICROSECOND | MINUTE
| MONTH | MONTHNAME | QUARTER | SECOND | SUBDATE | SYSDATE | TIME | TIMESTAMP | TIME_TO_SEC
| TO_DAYS | UNIX_TIMESTAMP | WEEK | YEAR
| TO_DAYS | UNIX_TIMESTAMP | UTC_DATE | UTC_TIME | UTC_DATETIME | WEEK | YEAR
;

// Functions which value could be cached in scope of a single query
Expand Down
2 changes: 1 addition & 1 deletion sql/src/main/antlr/OpenSearchSQLLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,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';

Expand Down
2 changes: 1 addition & 1 deletion sql/src/main/antlr/OpenSearchSQLParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ dateTimeFunctionName
: ADDDATE | CONVERT_TZ | DATE | DATETIME | DATE_ADD | DATE_FORMAT | DATE_SUB | DAY | DAYNAME | DAYOFMONTH | DAYOFWEEK
| DAYOFYEAR | FROM_DAYS | FROM_UNIXTIME | HOUR | MAKEDATE | MAKETIME | MICROSECOND | MINUTE
| MONTH | MONTHNAME | QUARTER | SECOND | SUBDATE | SYSDATE | TIME | TIME_TO_SEC | TIMESTAMP
| TO_DAYS | UNIX_TIMESTAMP | WEEK | YEAR
| TO_DAYS | UNIX_TIMESTAMP | UTC_DATE | UTC_TIME | UTC_DATETIME | WEEK | YEAR
;

// Functions which value could be cached in scope of a single query
Expand Down

0 comments on commit 134596a

Please sign in to comment.