Skip to content

Commit

Permalink
Merge pull request #1260 from penghuo/hp/2.5-release
Browse files Browse the repository at this point in the history
* Add BWC tests for running against distribution bundle.  (#1209)

Signed-off-by: Zelin Hao <[email protected]>

* Add Alternate Syntax For Match_Query And Other Functions (#1166)

Added Tests And Implementation For Match_Query, Match_Phrase, and Multi_Match Functions

Signed-off-by: GabeFernandez310 <[email protected]>

Signed-off-by: GabeFernandez310 <[email protected]>
Signed-off-by: GabeFernandez310 <[email protected]>

* Merge pull request #1241 from Bit-Quill/Failing-CI-Hot-Fix

Hot Fix For CI Build

(cherry picked from commit aae57a0)
Signed-off-by: GabeFernandez310 <[email protected]>

* Fixed error with single timestamp query (#1244) (#1246)

Signed-off-by: vamsi-amazon <[email protected]>

Signed-off-by: vamsi-amazon <[email protected]>
(cherry picked from commit ee949cc)

Co-authored-by: vamsi-amazon <[email protected]>

* Add Second_Of_Minute Function As An Alias Of The Second Function (#1231) (#1237)

Added Testing And Implementation For Second_Of_Minute Function

Signed-off-by: GabeFernandez310 <[email protected]>

Signed-off-by: GabeFernandez310 <[email protected]>
(cherry picked from commit dce7d0e)

Co-authored-by: GabeFernandez310 <[email protected]>

* Add functions `ADDTIME` and `SUBTIME`. (#132) (#1194) (#1252)

* Add functions `ADDTIME` and `SUBTIME`. (#132)

Signed-off-by: Yury-Fridlyand <[email protected]>
(cherry picked from commit 7630f87)

Co-authored-by: Yury-Fridlyand <[email protected]>

* Add Day_Of_Week Function As An Alias Of DayOfWeek (#190) (#1228) (#1239)

Added Implementation And Testing For Day_Of_Week Function

Signed-off-by: GabeFernandez310 <[email protected]>

Signed-off-by: GabeFernandez310 <[email protected]>
(cherry picked from commit bac9c37)

Co-authored-by: GabeFernandez310 <[email protected]>

* [Backport 2.x] Add Minute_Of_Hour Function As An Alias Of Minute Function (#1253)

* Add Minute_Of_Hour Function As An Alias Of Minute Function (#196) (#1230)

Added Testing And Implementation For Minute_Of_Hour Function

Signed-off-by: GabeFernandez310 <[email protected]>

Signed-off-by: GabeFernandez310 <[email protected]>
(cherry picked from commit 61e2374)

* Added Missing Imports

Signed-off-by: GabeFernandez310 <[email protected]>

Signed-off-by: GabeFernandez310 <[email protected]>

* Add support for long value return for CEIL, CEILING and FLOOR math functions (#1205) (#1255)

* Added long fix for CEIL, CEILING and FLOOR functions using LONG instead of INT for RETURN.

Signed-off-by: MitchellGale-BitQuill <[email protected]>
Signed-off-by: Yury-Fridlyand <[email protected]>
Co-authored-by: Yury-Fridlyand <[email protected]>

* Support JOIN query on object field with unexpanded name (#1229) (#1250)

* Resolve sub object field in search hit source

Signed-off-by: Chen Dai <[email protected]>

* Rename to unexpanded object

Signed-off-by: Chen Dai <[email protected]>

* Update IT with where condition

Signed-off-by: Chen Dai <[email protected]>

* Fix test index mapping

Signed-off-by: Chen Dai <[email protected]>

Signed-off-by: Chen Dai <[email protected]>
(cherry picked from commit 151f4cc)

Co-authored-by: Chen Dai <[email protected]>

* Remove unnecessary scripts after repo split (#1256)

Signed-off-by: Joshua Li <[email protected]>

* Add Support For `TIME` Type in "*_OF_YEAR" Functions (#199) (#1223) (#1258)

Added Support And Tests For Time Type in day_of_year, week_of_year, month_of_year Functions
Signed-off-by: GabeFernandez310 <[email protected]>
(cherry picked from commit 6e72f18)

Co-authored-by: GabeFernandez310 <[email protected]>

Signed-off-by: Zelin Hao <[email protected]>
Signed-off-by: GabeFernandez310 <[email protected]>
Signed-off-by: GabeFernandez310 <[email protected]>
Signed-off-by: MitchellGale-BitQuill <[email protected]>
Signed-off-by: Yury-Fridlyand <[email protected]>
Signed-off-by: Joshua Li <[email protected]>
Co-authored-by: Zelin Hao <[email protected]>
Co-authored-by: GabeFernandez310 <[email protected]>
Co-authored-by: YANGDB <[email protected]>
Co-authored-by: opensearch-trigger-bot[bot] <98922864+opensearch-trigger-bot[bot]@users.noreply.github.com>
Co-authored-by: vamsi-amazon <[email protected]>
Co-authored-by: Yury-Fridlyand <[email protected]>
Co-authored-by: GabeFernandez310 <[email protected]>
Co-authored-by: Chen Dai <[email protected]>
Co-authored-by: Joshua Li <[email protected]>
  • Loading branch information
10 people authored Jan 10, 2023
2 parents 6332529 + 4349116 commit 72a3867
Show file tree
Hide file tree
Showing 46 changed files with 2,519 additions and 366 deletions.
38 changes: 28 additions & 10 deletions core/src/main/java/org/opensearch/sql/expression/DSL.java
Original file line number Diff line number Diff line change
Expand Up @@ -326,16 +326,23 @@ public static FunctionExpression dayofmonth(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.DAYOFMONTH, expressions);
}

public static FunctionExpression dayofweek(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.DAYOFWEEK, expressions);
public static FunctionExpression dayofweek(
FunctionProperties functionProperties, Expression... expressions) {
return compile(functionProperties, BuiltinFunctionName.DAYOFWEEK, expressions);
}

public static FunctionExpression dayofyear(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.DAYOFYEAR, expressions);
}

public static FunctionExpression day_of_year(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.DAY_OF_YEAR, expressions);
public static FunctionExpression day_of_year(
FunctionProperties functionProperties, Expression... expressions) {
return compile(functionProperties, BuiltinFunctionName.DAY_OF_YEAR, expressions);
}

public static FunctionExpression day_of_week(
FunctionProperties functionProperties, Expression... expressions) {
return compile(functionProperties, BuiltinFunctionName.DAY_OF_WEEK, expressions);
}

public static FunctionExpression from_days(Expression... expressions) {
Expand All @@ -358,12 +365,17 @@ public static FunctionExpression minute_of_day(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.MINUTE_OF_DAY, expressions);
}

public static FunctionExpression minute_of_hour(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.MINUTE_OF_HOUR, expressions);
}

public static FunctionExpression month(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.MONTH, expressions);
}

public static FunctionExpression month_of_year(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.MONTH_OF_YEAR, expressions);
public static FunctionExpression month_of_year(
FunctionProperties functionProperties, Expression... expressions) {
return compile(functionProperties, BuiltinFunctionName.MONTH_OF_YEAR, expressions);
}

public static FunctionExpression monthname(Expression... expressions) {
Expand All @@ -378,6 +390,10 @@ public static FunctionExpression second(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.SECOND, expressions);
}

public static FunctionExpression second_of_minute(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.SECOND_OF_MINUTE, expressions);
}

public static FunctionExpression subdate(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.SUBDATE, expressions);
}
Expand All @@ -402,12 +418,14 @@ public static FunctionExpression to_days(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.TO_DAYS, expressions);
}

public static FunctionExpression week(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.WEEK, expressions);
public static FunctionExpression week(
FunctionProperties functionProperties, Expression... expressions) {
return compile(functionProperties, BuiltinFunctionName.WEEK, expressions);
}

public static FunctionExpression week_of_year(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.WEEK_OF_YEAR, expressions);
public static FunctionExpression week_of_year(
FunctionProperties functionProperties, Expression... expressions) {
return compile(functionProperties, BuiltinFunctionName.WEEK_OF_YEAR, expressions);
}

public static FunctionExpression year(Expression... expressions) {
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public enum BuiltinFunctionName {
* Date and Time Functions.
*/
ADDDATE(FunctionName.of("adddate")),
ADDTIME(FunctionName.of("addtime")),
CONVERT_TZ(FunctionName.of("convert_tz")),
DATE(FunctionName.of("date")),
DATEDIFF(FunctionName.of("datediff")),
Expand All @@ -70,6 +71,7 @@ public enum BuiltinFunctionName {
DAYOFMONTH(FunctionName.of("dayofmonth")),
DAYOFWEEK(FunctionName.of("dayofweek")),
DAYOFYEAR(FunctionName.of("dayofyear")),
DAY_OF_WEEK(FunctionName.of("day_of_week")),
DAY_OF_YEAR(FunctionName.of("day_of_year")),
FROM_DAYS(FunctionName.of("from_days")),
FROM_UNIXTIME(FunctionName.of("from_unixtime")),
Expand All @@ -79,14 +81,17 @@ public enum BuiltinFunctionName {
MICROSECOND(FunctionName.of("microsecond")),
MINUTE(FunctionName.of("minute")),
MINUTE_OF_DAY(FunctionName.of("minute_of_day")),
MINUTE_OF_HOUR(FunctionName.of("minute_of_hour")),
MONTH(FunctionName.of("month")),
MONTH_OF_YEAR(FunctionName.of("month_of_year")),
MONTHNAME(FunctionName.of("monthname")),
PERIOD_ADD(FunctionName.of("period_add")),
PERIOD_DIFF(FunctionName.of("period_diff")),
QUARTER(FunctionName.of("quarter")),
SECOND(FunctionName.of("second")),
SECOND_OF_MINUTE(FunctionName.of("second_of_minute")),
SUBDATE(FunctionName.of("subdate")),
SUBTIME(FunctionName.of("subtime")),
TIME(FunctionName.of("time")),
TIMEDIFF(FunctionName.of("timediff")),
TIME_TO_SEC(FunctionName.of("time_to_sec")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,11 @@ public String toString() {
* Implementation of a function that takes two arguments, returns a value, and
* requires FunctionProperties to complete.
*
* @param function {@link ExprValue} based unary function.
* @param function {@link ExprValue} based Binary function.
* @param returnType return type.
* @param args1Type first argument type.
* @param args2Type second argument type.
* @return Unary Function Implementation.
* @return Binary Function Implementation.
*/
public static SerializableFunction<FunctionName, Pair<FunctionSignature, FunctionBuilder>>
implWithProperties(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,16 @@ private static DefaultFunctionResolver abs() {
private static DefaultFunctionResolver ceil() {
return FunctionDSL.define(BuiltinFunctionName.CEIL.getName(),
FunctionDSL.impl(
FunctionDSL.nullMissingHandling(v -> new ExprIntegerValue(Math.ceil(v.doubleValue()))),
INTEGER, DOUBLE)
FunctionDSL.nullMissingHandling(v -> new ExprLongValue(Math.ceil(v.doubleValue()))),
LONG, DOUBLE)
);
}

private static DefaultFunctionResolver ceiling() {
return FunctionDSL.define(BuiltinFunctionName.CEILING.getName(),
FunctionDSL.impl(
FunctionDSL.nullMissingHandling(v -> new ExprIntegerValue(Math.ceil(v.doubleValue()))),
INTEGER, DOUBLE)
FunctionDSL.nullMissingHandling(v -> new ExprLongValue(Math.ceil(v.doubleValue()))),
LONG, DOUBLE)
);
}

Expand Down Expand Up @@ -204,8 +204,8 @@ private static DefaultFunctionResolver exp() {
private static DefaultFunctionResolver floor() {
return FunctionDSL.define(BuiltinFunctionName.FLOOR.getName(),
FunctionDSL.impl(
FunctionDSL.nullMissingHandling(v -> new ExprIntegerValue(Math.floor(v.doubleValue()))),
INTEGER, DOUBLE)
FunctionDSL.nullMissingHandling(v -> new ExprLongValue(Math.floor(v.doubleValue()))),
LONG, DOUBLE)
);
}

Expand Down
11 changes: 11 additions & 0 deletions core/src/main/java/org/opensearch/sql/utils/DateTimeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,17 @@ public Boolean isValidMySqlTimeZoneId(ZoneId zone) {
|| passedTzValidator.isEqual(minTzValidator));
}

/**
* Extracts LocalDateTime from a datetime ExprValue.
* Uses `FunctionProperties` for `ExprTimeValue`.
*/
public static LocalDateTime extractDateTime(ExprValue value,
FunctionProperties functionProperties) {
return value instanceof ExprTimeValue
? ((ExprTimeValue) value).datetimeValue(functionProperties)
: value.datetimeValue();
}

/**
* Extracts LocalDate from a datetime ExprValue.
* Uses `FunctionProperties` for `ExprTimeValue`.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* 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.type.ExprCoreType.DATETIME;
import static org.opensearch.sql.data.type.ExprCoreType.TIME;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.Temporal;
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;

public class AddTimeAndSubTimeTest extends DateTimeTestBase {

@Test
// (TIME, TIME/DATE/DATETIME/TIMESTAMP) -> TIME
public void return_time_when_first_arg_is_time() {
var res = addtime(LocalTime.of(21, 0), LocalTime.of(0, 5));
assertEquals(TIME, res.type());
assertEquals(LocalTime.of(21, 5), res.timeValue());

res = subtime(LocalTime.of(21, 0), LocalTime.of(0, 5));
assertEquals(TIME, res.type());
assertEquals(LocalTime.of(20, 55), res.timeValue());

res = addtime(LocalTime.of(12, 20), Instant.ofEpochSecond(42));
assertEquals(TIME, res.type());
assertEquals(LocalTime.of(12, 20, 42), res.timeValue());

res = subtime(LocalTime.of(10, 0), Instant.ofEpochSecond(42));
assertEquals(TIME, res.type());
assertEquals(LocalTime.of(9, 59, 18), res.timeValue());

res = addtime(LocalTime.of(2, 3, 4), LocalDateTime.of(1961, 4, 12, 9, 7));
assertEquals(TIME, res.type());
assertEquals(LocalTime.of(11, 10, 4), res.timeValue());

res = subtime(LocalTime.of(12, 3, 4), LocalDateTime.of(1961, 4, 12, 9, 7));
assertEquals(TIME, res.type());
assertEquals(LocalTime.of(2, 56, 4), res.timeValue());

res = addtime(LocalTime.of(9, 7), LocalDate.now());
assertEquals(TIME, res.type());
assertEquals(LocalTime.of(9, 7), res.timeValue());

res = subtime(LocalTime.of(9, 7), LocalDate.of(1961, 4, 12));
assertEquals(TIME, res.type());
assertEquals(LocalTime.of(9, 7), res.timeValue());
}

@Test
public void time_limited_by_24_hours() {
var res = addtime(LocalTime.of(21, 0), LocalTime.of(14, 5));
assertEquals(TIME, res.type());
assertEquals(LocalTime.of(11, 5), res.timeValue());

res = subtime(LocalTime.of(14, 0), LocalTime.of(21, 5));
assertEquals(TIME, res.type());
assertEquals(LocalTime.of(16, 55), res.timeValue());
}

// Function signature is:
// (DATE/DATETIME/TIMESTAMP, TIME/DATE/DATETIME/TIMESTAMP) -> DATETIME
private static Stream<Arguments> getTestData() {
return Stream.of(
// DATETIME and TIME/DATE/DATETIME/TIMESTAMP
Arguments.of(LocalDateTime.of(1961, 4, 12, 9, 7), LocalTime.of(1, 48),
LocalDateTime.of(1961, 4, 12, 10, 55), LocalDateTime.of(1961, 4, 12, 7, 19)),
Arguments.of(LocalDateTime.of(1961, 4, 12, 9, 7), LocalDate.of(2000, 1, 1),
LocalDateTime.of(1961, 4, 12, 9, 7), LocalDateTime.of(1961, 4, 12, 9, 7)),
Arguments.of(LocalDateTime.of(1961, 4, 12, 9, 7), LocalDateTime.of(1235, 5, 6, 1, 48),
LocalDateTime.of(1961, 4, 12, 10, 55), LocalDateTime.of(1961, 4, 12, 7, 19)),
Arguments.of(LocalDateTime.of(1961, 4, 12, 9, 7), Instant.ofEpochSecond(42),
LocalDateTime.of(1961, 4, 12, 9, 7, 42), LocalDateTime.of(1961, 4, 12, 9, 6, 18)),
// DATE and TIME/DATE/DATETIME/TIMESTAMP
Arguments.of(LocalDate.of(1961, 4, 12), LocalTime.of(9, 7),
LocalDateTime.of(1961, 4, 12, 9, 7), LocalDateTime.of(1961, 4, 11, 14, 53)),
Arguments.of(LocalDate.of(1961, 4, 12), LocalDate.of(2000, 1, 1),
LocalDateTime.of(1961, 4, 12, 0, 0), LocalDateTime.of(1961, 4, 12, 0, 0)),
Arguments.of(LocalDate.of(1961, 4, 12), LocalDateTime.of(1235, 5, 6, 1, 48),
LocalDateTime.of(1961, 4, 12, 1, 48), LocalDateTime.of(1961, 4, 11, 22, 12)),
Arguments.of(LocalDate.of(1961, 4, 12), Instant.ofEpochSecond(42),
LocalDateTime.of(1961, 4, 12, 0, 0, 42), LocalDateTime.of(1961, 4, 11, 23, 59, 18)),
// TIMESTAMP and TIME/DATE/DATETIME/TIMESTAMP
Arguments.of(Instant.ofEpochSecond(42), LocalTime.of(9, 7),
LocalDateTime.of(1970, 1, 1, 9, 7, 42), LocalDateTime.of(1969, 12, 31, 14, 53, 42)),
Arguments.of(Instant.ofEpochSecond(42), LocalDate.of(1961, 4, 12),
LocalDateTime.of(1970, 1, 1, 0, 0, 42), LocalDateTime.of(1970, 1, 1, 0, 0, 42)),
Arguments.of(Instant.ofEpochSecond(42), LocalDateTime.of(1961, 4, 12, 9, 7),
LocalDateTime.of(1970, 1, 1, 9, 7, 42), LocalDateTime.of(1969, 12, 31, 14, 53, 42)),
Arguments.of(Instant.ofEpochSecond(42), Instant.ofEpochMilli(42),
LocalDateTime.of(1970, 1, 1, 0, 0, 42, 42000000),
LocalDateTime.of(1970, 1, 1, 0, 0, 41, 958000000))
);
}

/**
* Check that `ADDTIME` and `SUBTIME` functions result value and type.
* @param arg1 First argument.
* @param arg2 Second argument.
* @param addTimeExpectedResult Expected result for `ADDTIME`.
* @param subTimeExpectedResult Expected result for `SUBTIME`.
*/
@ParameterizedTest
@MethodSource("getTestData")
public void return_datetime_when_first_arg_is_not_time(Temporal arg1, Temporal arg2,
LocalDateTime addTimeExpectedResult,
LocalDateTime subTimeExpectedResult) {
var res = addtime(arg1, arg2);
assertEquals(DATETIME, res.type());
assertEquals(addTimeExpectedResult, res.datetimeValue());

res = subtime(arg1, arg2);
assertEquals(DATETIME, res.type());
assertEquals(subTimeExpectedResult, res.datetimeValue());
}
}
Loading

0 comments on commit 72a3867

Please sign in to comment.