Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/2.5' into 2.5
Browse files Browse the repository at this point in the history
Signed-off-by: YANGDB <[email protected]>

# Conflicts:
#	integ-test/build.gradle
  • Loading branch information
YANG-DB committed Jan 10, 2023
2 parents ae9f140 + 72a3867 commit 680e057
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 680e057

Please sign in to comment.