forked from opensearch-project/sql
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add The
TO_SECONDS
Function To The SQL Plugin (opensearch-project#1419
) * Add The `TO_SECONDS` Function To The SQL Plugin (#232) * Added Basic Tests Signed-off-by: GabeFernandez310 <[email protected]> * Added IT Test Signed-off-by: GabeFernandez310 <[email protected]> * Added Implementation Signed-off-by: GabeFernandez310 <[email protected]> * Changed Integration Tests Signed-off-by: GabeFernandez310 <[email protected]> * Added Test For Time Type Signed-off-by: GabeFernandez310 <[email protected]> * Added Documentation Signed-off-by: GabeFernandez310 <[email protected]> * Addressed PR Comments Signed-off-by: GabeFernandez310 <[email protected]> * Fixed Docs and Implementation Signed-off-by: GabeFernandez310 <[email protected]> * Fixed Checkstyle Signed-off-by: GabeFernandez310 <[email protected]> * Changed DateTimeFormatter Priority Signed-off-by: GabeFernandez310 <[email protected]> * Added More Formatters Signed-off-by: GabeFernandez310 <[email protected]> * Updated Docs Signed-off-by: GabeFernandez310 <[email protected]> * Reworked Implementation For Formatters Signed-off-by: GabeFernandez310 <[email protected]> * Cleanup Signed-off-by: GabeFernandez310 <[email protected]> * Added Test Signed-off-by: GabeFernandez310 <[email protected]> * Fixed Implementation And Code Coverage Signed-off-by: GabeFernandez310 <[email protected]> * Changed getFormatter Function Signed-off-by: GabeFernandez310 <[email protected]> * Added Comments Signed-off-by: GabeFernandez310 <[email protected]> --------- Signed-off-by: GabeFernandez310 <[email protected]> * Removed Unneeded Code Signed-off-by: GabeFernandez310 <[email protected]> --------- Signed-off-by: GabeFernandez310 <[email protected]>
- Loading branch information
1 parent
40336d4
commit d38a6ec
Showing
10 changed files
with
317 additions
and
1 deletion.
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
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
121 changes: 121 additions & 0 deletions
121
core/src/test/java/org/opensearch/sql/expression/datetime/ToSecondsTest.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,121 @@ | ||
/* | ||
* 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.assertThrows; | ||
import static org.opensearch.sql.data.type.ExprCoreType.LONG; | ||
import static org.opensearch.sql.expression.datetime.DateTimeFunction.SECONDS_PER_DAY; | ||
|
||
import java.time.Duration; | ||
import java.time.LocalDate; | ||
import java.time.LocalTime; | ||
import java.time.ZoneOffset; | ||
import java.util.stream.Stream; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
import org.opensearch.sql.data.model.ExprDateValue; | ||
import org.opensearch.sql.data.model.ExprDatetimeValue; | ||
import org.opensearch.sql.data.model.ExprIntervalValue; | ||
import org.opensearch.sql.data.model.ExprLongValue; | ||
import org.opensearch.sql.data.model.ExprNullValue; | ||
import org.opensearch.sql.data.model.ExprStringValue; | ||
import org.opensearch.sql.data.model.ExprTimeValue; | ||
import org.opensearch.sql.data.model.ExprTimestampValue; | ||
import org.opensearch.sql.data.model.ExprValue; | ||
import org.opensearch.sql.exception.SemanticCheckException; | ||
import org.opensearch.sql.expression.DSL; | ||
import org.opensearch.sql.expression.Expression; | ||
import org.opensearch.sql.expression.ExpressionTestBase; | ||
import org.opensearch.sql.expression.FunctionExpression; | ||
|
||
|
||
class ToSecondsTest extends ExpressionTestBase { | ||
|
||
private static final long SECONDS_FROM_0001_01_01_TO_EPOCH_START = 62167219200L; | ||
|
||
private static Stream<Arguments> getTestDataForToSeconds() { | ||
return Stream.of( | ||
Arguments.of(new ExprLongValue(101), new ExprLongValue(63113904000L)), | ||
Arguments.of(new ExprLongValue(1030), new ExprLongValue(63140083200L)), | ||
Arguments.of(new ExprLongValue(50101), new ExprLongValue(63271756800L)), | ||
Arguments.of(new ExprLongValue(950501), new ExprLongValue(62966505600L)), | ||
Arguments.of(new ExprLongValue(19950501), new ExprLongValue(62966505600L)), | ||
Arguments.of(new ExprLongValue(9950501), ExprNullValue.of()), | ||
Arguments.of(new ExprLongValue(-123L), ExprNullValue.of()), | ||
Arguments.of(new ExprLongValue(1), ExprNullValue.of()), | ||
Arguments.of(new ExprLongValue(919950501), ExprNullValue.of()), | ||
Arguments.of(new ExprStringValue("2009-11-29 00:00:00"), new ExprLongValue(63426672000L)), | ||
Arguments.of(new ExprStringValue("2009-11-29 13:43:32"), new ExprLongValue(63426721412L)), | ||
Arguments.of(new ExprDateValue("2009-11-29"), new ExprLongValue(63426672000L)), | ||
Arguments.of(new ExprDatetimeValue("2009-11-29 13:43:32"), | ||
new ExprLongValue(63426721412L)), | ||
Arguments.of(new ExprTimestampValue("2009-11-29 13:43:32"), | ||
new ExprLongValue(63426721412L)) | ||
); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("getTestDataForToSeconds") | ||
public void testToSeconds(ExprValue arg, ExprValue expected) { | ||
FunctionExpression expr = DSL.to_seconds(DSL.literal(arg)); | ||
assertEquals(LONG, expr.type()); | ||
assertEquals(expected, eval(expr)); | ||
} | ||
|
||
@Test | ||
public void testToSecondsWithTimeType() { | ||
FunctionExpression expr = DSL.to_seconds(functionProperties, | ||
DSL.literal(new ExprTimeValue("10:11:12"))); | ||
|
||
long expected = SECONDS_FROM_0001_01_01_TO_EPOCH_START | ||
+ LocalDate.now(functionProperties.getQueryStartClock()) | ||
.toEpochSecond(LocalTime.parse("10:11:12"), ZoneOffset.UTC); | ||
|
||
assertEquals(expected, eval(expr).longValue()); | ||
} | ||
|
||
private static Stream<Arguments> getInvalidTestDataForToSeconds() { | ||
return Stream.of( | ||
Arguments.of(new ExprStringValue("asdfasdf")), | ||
Arguments.of(new ExprStringValue("2000-14-10")), | ||
Arguments.of(new ExprStringValue("2000-10-45")), | ||
Arguments.of(new ExprStringValue("2000-10-10 70:00:00")), | ||
Arguments.of(new ExprStringValue("2000-10-10 00:70:00")), | ||
Arguments.of(new ExprStringValue("2000-10-10 00:00:70")) | ||
); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("getInvalidTestDataForToSeconds") | ||
public void testToSecondsInvalidArg(ExprValue arg) { | ||
FunctionExpression expr = DSL.to_seconds(DSL.literal(arg)); | ||
assertThrows(SemanticCheckException.class, () -> eval(expr)); | ||
} | ||
|
||
@Test | ||
public void testToSecondsWithDateAdd() { | ||
LocalDate date = LocalDate.of(2000, 1, 1); | ||
FunctionExpression dateExpr = DSL.to_seconds(DSL.literal(new ExprDateValue(date))); | ||
long addedSeconds = SECONDS_PER_DAY; | ||
long expected = eval(dateExpr).longValue() + addedSeconds; | ||
|
||
FunctionExpression dateAddExpr = DSL.date_add( | ||
DSL.literal(new ExprDateValue(date)), | ||
DSL.literal(new ExprIntervalValue(Duration.ofSeconds(addedSeconds)))); | ||
|
||
long result = eval(DSL.to_seconds(DSL.literal(eval(dateAddExpr)))).longValue(); | ||
|
||
assertEquals(expected, result); | ||
} | ||
|
||
private ExprValue eval(Expression expression) { | ||
return expression.valueOf(); | ||
} | ||
} |
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
Oops, something went wrong.