-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
EXTRACT
Function To OpenSearch SQL Plugin (#1421)
* Add `EXTRACT` Function To OpenSearch SQL Plugin * Added Basic Tests Signed-off-by: GabeFernandez310 <[email protected]> * Added Lexer And Parser Language Signed-off-by: GabeFernandez310 <[email protected]> * Added Implementation And Fixed Tests Signed-off-by: GabeFernandez310 <[email protected]> * Added Documentation Signed-off-by: GabeFernandez310 <[email protected]> * Modified Implementation And Tests Signed-off-by: GabeFernandez310 <[email protected]> * Fixed Checkstyle Issues Signed-off-by: GabeFernandez310 <[email protected]> * Added Javadoc comments and Tests Signed-off-by: GabeFernandez310 <[email protected]> * Changed Implementation Signed-off-by: GabeFernandez310 <[email protected]> * Changed A Test Signed-off-by: GabeFernandez310 <[email protected]> * Fixed Integration Tests Signed-off-by: GabeFernandez310 <[email protected]> * Refactored Extract Tests Into New File Signed-off-by: GabeFernandez310 <[email protected]> * Refactored Tests Signed-off-by: GabeFernandez310 <[email protected]> * Renamed Rule In Parser Signed-off-by: GabeFernandez310 <[email protected]> * Fixed AstExpressionBuilder Signed-off-by: GabeFernandez310 <[email protected]> --------- Signed-off-by: GabeFernandez310 <[email protected]> * Removed Unneeded Code And Added Parser Test Signed-off-by: GabeFernandez310 <[email protected]> --------- Signed-off-by: GabeFernandez310 <[email protected]>
- Loading branch information
1 parent
03e4f97
commit 87018c6
Showing
11 changed files
with
459 additions
and
0 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
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
156 changes: 156 additions & 0 deletions
156
core/src/test/java/org/opensearch/sql/expression/datetime/ExtractTest.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,156 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
|
||
package org.opensearch.sql.expression.datetime; | ||
|
||
import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.opensearch.sql.data.type.ExprCoreType.LONG; | ||
|
||
import java.time.LocalDate; | ||
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.ExprTimeValue; | ||
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; | ||
|
||
class ExtractTest extends ExpressionTestBase { | ||
|
||
private final String datetimeInput = "2023-02-11 10:11:12.123"; | ||
|
||
private final String timeInput = "10:11:12.123"; | ||
|
||
private final String dateInput = "2023-02-11"; | ||
|
||
private static Stream<Arguments> getDatetimeResultsForExtractFunction() { | ||
return Stream.of( | ||
Arguments.of("DAY_MICROSECOND", 11101112123000L), | ||
Arguments.of("DAY_SECOND", 11101112), | ||
Arguments.of("DAY_MINUTE", 111011), | ||
Arguments.of("DAY_HOUR", 1110) | ||
); | ||
} | ||
|
||
private static Stream<Arguments> getTimeResultsForExtractFunction() { | ||
return Stream.of( | ||
Arguments.of("MICROSECOND", 123000), | ||
Arguments.of("SECOND", 12), | ||
Arguments.of("MINUTE", 11), | ||
Arguments.of("HOUR", 10), | ||
Arguments.of("SECOND_MICROSECOND", 12123000), | ||
Arguments.of("MINUTE_MICROSECOND", 1112123000), | ||
Arguments.of("MINUTE_SECOND", 1112), | ||
Arguments.of("HOUR_MICROSECOND", 101112123000L), | ||
Arguments.of("HOUR_SECOND", 101112), | ||
Arguments.of("HOUR_MINUTE", 1011) | ||
); | ||
} | ||
|
||
private static Stream<Arguments> getDateResultsForExtractFunction() { | ||
return Stream.of( | ||
Arguments.of("DAY", 11), | ||
Arguments.of("WEEK", 6), | ||
Arguments.of("MONTH", 2), | ||
Arguments.of("QUARTER", 1), | ||
Arguments.of("YEAR", 2023), | ||
Arguments.of("YEAR_MONTH", 202302) | ||
); | ||
} | ||
|
||
@ParameterizedTest(name = "{0}") | ||
@MethodSource({ | ||
"getDatetimeResultsForExtractFunction", | ||
"getTimeResultsForExtractFunction", | ||
"getDateResultsForExtractFunction"}) | ||
public void testExtractWithDatetime(String part, long expected) { | ||
FunctionExpression datetimeExpression = DSL.extract( | ||
DSL.literal(part), | ||
DSL.literal(new ExprDatetimeValue(datetimeInput))); | ||
|
||
assertEquals(LONG, datetimeExpression.type()); | ||
assertEquals(expected, eval(datetimeExpression).longValue()); | ||
assertEquals( | ||
String.format("extract(\"%s\", DATETIME '2023-02-11 10:11:12.123')", part), | ||
datetimeExpression.toString()); | ||
} | ||
|
||
private void datePartWithTimeArgQuery(String part, String time, long expected) { | ||
ExprTimeValue timeValue = new ExprTimeValue(time); | ||
FunctionExpression datetimeExpression = DSL.extract( | ||
functionProperties, | ||
DSL.literal(part), | ||
DSL.literal(timeValue)); | ||
|
||
assertEquals(LONG, datetimeExpression.type()); | ||
assertEquals(expected, | ||
eval(datetimeExpression).longValue()); | ||
} | ||
|
||
|
||
@Test | ||
public void testExtractDatePartWithTimeType() { | ||
datePartWithTimeArgQuery( | ||
"DAY", | ||
timeInput, | ||
LocalDate.now(functionProperties.getQueryStartClock()).getDayOfMonth()); | ||
|
||
datePartWithTimeArgQuery( | ||
"WEEK", | ||
timeInput, | ||
LocalDate.now(functionProperties.getQueryStartClock()).get(ALIGNED_WEEK_OF_YEAR)); | ||
|
||
datePartWithTimeArgQuery( | ||
"MONTH", | ||
timeInput, | ||
LocalDate.now(functionProperties.getQueryStartClock()).getMonthValue()); | ||
|
||
datePartWithTimeArgQuery( | ||
"YEAR", | ||
timeInput, | ||
LocalDate.now(functionProperties.getQueryStartClock()).getYear()); | ||
} | ||
|
||
@ParameterizedTest(name = "{0}") | ||
@MethodSource("getDateResultsForExtractFunction") | ||
public void testExtractWithDate(String part, long expected) { | ||
FunctionExpression datetimeExpression = DSL.extract( | ||
DSL.literal(part), | ||
DSL.literal(new ExprDateValue(dateInput))); | ||
|
||
assertEquals(LONG, datetimeExpression.type()); | ||
assertEquals(expected, eval(datetimeExpression).longValue()); | ||
assertEquals( | ||
String.format("extract(\"%s\", DATE '2023-02-11')", part), | ||
datetimeExpression.toString()); | ||
} | ||
|
||
@ParameterizedTest(name = "{0}") | ||
@MethodSource("getTimeResultsForExtractFunction") | ||
public void testExtractWithTime(String part, long expected) { | ||
FunctionExpression datetimeExpression = DSL.extract( | ||
functionProperties, | ||
DSL.literal(part), | ||
DSL.literal(new ExprTimeValue(timeInput))); | ||
|
||
assertEquals(LONG, datetimeExpression.type()); | ||
assertEquals(expected, eval(datetimeExpression).longValue()); | ||
assertEquals( | ||
String.format("extract(\"%s\", TIME '10:11:12.123')", part), | ||
datetimeExpression.toString()); | ||
} | ||
|
||
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.