-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #109 from Bit-Quill/dev-Add-convert_tz-function
Adding convert_tz and datetime functions to SQL and PPL
- Loading branch information
Showing
18 changed files
with
1,768 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
216 changes: 187 additions & 29 deletions
216
core/src/main/java/org/opensearch/sql/expression/datetime/DateTimeFunction.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
183 changes: 183 additions & 0 deletions
183
core/src/test/java/org/opensearch/sql/expression/datetime/ConvertTZTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
/* | ||
* 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.opensearch.sql.data.model.ExprValueUtils.nullValue; | ||
import static org.opensearch.sql.data.type.ExprCoreType.DATETIME; | ||
|
||
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.ExprDatetimeValue; | ||
import org.opensearch.sql.data.model.ExprValue; | ||
import org.opensearch.sql.expression.DSL; | ||
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 ConvertTZTest extends ExpressionTestBase { | ||
|
||
@Mock | ||
Environment<Expression, ExprValue> env; | ||
|
||
@Test | ||
public void invalidDate() { | ||
FunctionExpression expr = dsl.convert_tz(dsl.datetime( | ||
DSL.literal("2021-04-31 10:00:00")), | ||
DSL.literal("+00:00"), | ||
DSL.literal("+00:00")); | ||
assertEquals(DATETIME, expr.type()); | ||
assertEquals(nullValue(), expr.valueOf(env)); | ||
} | ||
|
||
@Test | ||
public void conversionFromNoOffset() { | ||
FunctionExpression expr = dsl.convert_tz(dsl.datetime( | ||
DSL.literal("2008-05-15 22:00:00")), | ||
DSL.literal("+00:00"), | ||
DSL.literal("+10:00")); | ||
assertEquals(DATETIME, expr.type()); | ||
assertEquals(new ExprDatetimeValue("2008-05-16 08:00:00"), expr.valueOf(env)); | ||
} | ||
|
||
@Test | ||
public void conversionToInvalidInput3Over() { | ||
FunctionExpression expr = dsl.convert_tz(dsl.datetime( | ||
DSL.literal("2008-05-15 22:00:00")), | ||
DSL.literal("+00:00"), | ||
DSL.literal("+16:00")); | ||
assertEquals(DATETIME, expr.type()); | ||
assertEquals(nullValue(), expr.valueOf(env)); | ||
} | ||
|
||
@Test | ||
public void conversionToInvalidInput3Under() { | ||
FunctionExpression expr = dsl.convert_tz(dsl.datetime( | ||
DSL.literal("2008-05-15 22:00:00")), | ||
DSL.literal("+00:00"), | ||
DSL.literal("-16:00")); | ||
assertEquals(DATETIME, expr.type()); | ||
assertEquals(nullValue(), expr.valueOf(env)); | ||
} | ||
|
||
@Test | ||
public void conversionFromPositiveToPositive() { | ||
FunctionExpression expr = dsl.convert_tz(dsl.datetime( | ||
DSL.literal("2008-05-15 22:00:00")), | ||
DSL.literal("+15:00"), | ||
DSL.literal("+01:00")); | ||
assertEquals(DATETIME, expr.type()); | ||
assertEquals(nullValue(), expr.valueOf(env)); | ||
} | ||
|
||
@Test | ||
public void invalidInput2Under() { | ||
FunctionExpression expr = dsl.convert_tz(dsl.datetime( | ||
DSL.literal("2008-05-15 22:00:00")), | ||
DSL.literal("-15:00"), | ||
DSL.literal("+01:00")); | ||
assertEquals(DATETIME, expr.type()); | ||
assertEquals(nullValue(), expr.valueOf(env)); | ||
} | ||
|
||
@Test | ||
public void invalidInput3Over() { | ||
FunctionExpression expr = dsl.convert_tz(dsl.datetime( | ||
DSL.literal("2008-05-15 22:00:00")), | ||
DSL.literal("-12:00"), | ||
DSL.literal("+15:00")); | ||
assertEquals(DATETIME, expr.type()); | ||
assertEquals(nullValue(), expr.valueOf(env)); | ||
} | ||
|
||
@Test | ||
public void conversionToPositiveEdge() { | ||
FunctionExpression expr = dsl.convert_tz(dsl.datetime( | ||
DSL.literal("2008-05-15 22:00:00")), | ||
DSL.literal("+00:00"), | ||
DSL.literal("+14:00")); | ||
assertEquals(DATETIME, expr.type()); | ||
assertEquals(new ExprDatetimeValue("2008-05-16 12:00:00"), expr.valueOf(env)); | ||
} | ||
|
||
@Test | ||
public void conversionToNegativeEdge() { | ||
FunctionExpression expr = dsl.convert_tz(dsl.datetime( | ||
DSL.literal("2008-05-15 22:00:00")), | ||
DSL.literal("+00:01"), | ||
DSL.literal("-13:59")); | ||
assertEquals(DATETIME, expr.type()); | ||
assertEquals(new ExprDatetimeValue("2008-05-15 08:00:00"), expr.valueOf(env)); | ||
} | ||
|
||
@Test | ||
public void invalidInput2() { | ||
FunctionExpression expr = dsl.convert_tz(dsl.datetime( | ||
DSL.literal("2008-05-15 22:00:00")), | ||
DSL.literal("+)()"), | ||
DSL.literal("+12:00")); | ||
assertEquals(DATETIME, expr.type()); | ||
assertEquals(nullValue(), expr.valueOf(env)); | ||
} | ||
|
||
@Test | ||
public void invalidInput3() { | ||
FunctionExpression expr = dsl.convert_tz(dsl.datetime( | ||
DSL.literal("2008-05-15 22:00:00")), | ||
DSL.literal("+00:00"), | ||
DSL.literal("test")); | ||
assertEquals(DATETIME, expr.type()); | ||
assertEquals(nullValue(), expr.valueOf(env)); | ||
} | ||
|
||
@Test | ||
public void invalidInput1() { | ||
FunctionExpression expr = dsl.convert_tz( | ||
DSL.literal("test"), | ||
DSL.literal("+00:00"), | ||
DSL.literal("+00:00")); | ||
assertEquals(DATETIME, expr.type()); | ||
assertEquals(nullValue(), expr.valueOf(env)); | ||
} | ||
|
||
@Test | ||
public void invalidDateFeb30() { | ||
FunctionExpression expr = dsl.convert_tz(dsl.datetime( | ||
DSL.literal("2021-02-30 10:00:00")), | ||
DSL.literal("+00:00"), | ||
DSL.literal("+00:00")); | ||
assertEquals(DATETIME, expr.type()); | ||
assertEquals(nullValue(), expr.valueOf(env)); | ||
} | ||
|
||
@Test | ||
public void invalidDateApril31() { | ||
FunctionExpression expr = dsl.convert_tz(dsl.datetime( | ||
DSL.literal("2021-04-31 10:00:00")), | ||
DSL.literal("+00:00"), | ||
DSL.literal("+00:00")); | ||
assertEquals(DATETIME, expr.type()); | ||
assertEquals(nullValue(), expr.valueOf(env)); | ||
} | ||
|
||
@Test | ||
public void invalidMonth13() { | ||
FunctionExpression expr = dsl.convert_tz(dsl.datetime( | ||
DSL.literal("2021-13-03 10:00:00")), | ||
DSL.literal("+00:00"), | ||
DSL.literal("+00:00")); | ||
assertEquals(DATETIME, expr.type()); | ||
assertEquals(nullValue(), expr.valueOf(env)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* 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.opensearch.sql.data.model.ExprValueUtils.nullValue; | ||
import static org.opensearch.sql.data.type.ExprCoreType.DATETIME; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.ZoneId; | ||
import java.time.ZonedDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.TimeZone; | ||
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.ExprDatetimeValue; | ||
import org.opensearch.sql.data.model.ExprValue; | ||
import org.opensearch.sql.expression.DSL; | ||
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 DateTimeTest extends ExpressionTestBase { | ||
|
||
@Mock | ||
Environment<Expression, ExprValue> env; | ||
|
||
@Test | ||
public void noTimeZoneNoField2() { | ||
FunctionExpression expr = dsl.datetime(DSL.literal("2008-05-15 22:00:00")); | ||
assertEquals(DATETIME, expr.type()); | ||
assertEquals(new ExprDatetimeValue("2008-05-15 22:00:00"), expr.valueOf(env)); | ||
} | ||
|
||
@Test | ||
public void positiveTimeZoneNoField2() { | ||
FunctionExpression expr = dsl.datetime(DSL.literal("2008-05-15 22:00:00+01:00")); | ||
assertEquals(DATETIME, expr.type()); | ||
assertEquals(new ExprDatetimeValue("2008-05-15 22:00:00"), expr.valueOf(env)); | ||
} | ||
|
||
@Test | ||
public void positiveField1WrittenField2() { | ||
FunctionExpression expr = dsl.datetime(DSL.literal("2008-05-15 22:00:00+01:00"), | ||
DSL.literal("America/Los_Angeles")); | ||
assertEquals(DATETIME, expr.type()); | ||
assertEquals(new ExprDatetimeValue("2008-05-15 14:00:00"), expr.valueOf(env)); | ||
} | ||
|
||
// When no timezone argument is passed inside the datetime field, it assumes local time. | ||
@Test | ||
public void localDateTimeConversion() { | ||
// needs to work for all time zones because it defaults to local timezone. | ||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); | ||
String dt = "2008-05-15 22:00:00"; | ||
String timeZone = "America/Los_Angeles"; | ||
LocalDateTime timeConverted = LocalDateTime.parse(dt, formatter); | ||
ZonedDateTime timeZoneLocal = timeConverted.atZone(ZoneId.of(TimeZone.getDefault().getID())) | ||
.withZoneSameInstant(ZoneId.of(timeZone)); | ||
FunctionExpression expr = dsl.datetime(DSL.literal(dt), | ||
DSL.literal(timeZone)); | ||
assertEquals(DATETIME, expr.type()); | ||
assertEquals(new ExprDatetimeValue(timeZoneLocal.toLocalDateTime()), expr.valueOf(env)); | ||
} | ||
|
||
@Test | ||
public void negativeField1WrittenField2() { | ||
FunctionExpression expr = dsl.datetime(DSL.literal("2008-05-15 22:00:00-11:00"), | ||
DSL.literal("America/Los_Angeles")); | ||
assertEquals(DATETIME, expr.type()); | ||
assertEquals(new ExprDatetimeValue("2008-05-16 02:00:00"), expr.valueOf(env)); | ||
} | ||
|
||
@Test | ||
public void negativeField1PositiveField2() { | ||
FunctionExpression expr = dsl.datetime(DSL.literal("2008-05-15 22:00:00-12:00"), | ||
DSL.literal("+15:00")); | ||
assertEquals(DATETIME, expr.type()); | ||
assertEquals(nullValue(), expr.valueOf(env)); | ||
} | ||
|
||
@Test | ||
public void twentyFourHourDifference() { | ||
FunctionExpression expr = dsl.datetime(DSL.literal("2008-05-15 22:00:00-14:00"), | ||
DSL.literal("+10:00")); | ||
assertEquals(DATETIME, expr.type()); | ||
assertEquals(nullValue(), expr.valueOf(env)); | ||
} | ||
|
||
@Test | ||
public void negativeToNull() { | ||
FunctionExpression expr = dsl.datetime(DSL.literal("2008-05-15 22:00:00-11:00"), | ||
DSL.literal(nullValue())); | ||
assertEquals(DATETIME, expr.type()); | ||
assertEquals(nullValue(), expr.valueOf(env)); | ||
} | ||
|
||
@Test | ||
public void invalidDate() { | ||
FunctionExpression expr = dsl.datetime(DSL.literal("2008-04-31 22:00:00-11:00")); | ||
assertEquals(DATETIME, expr.type()); | ||
assertEquals(nullValue(), expr.valueOf(env)); | ||
} | ||
} |
Oops, something went wrong.