-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add functions ADDTIME
and SUBTIME
.
#132
Changes from all commits
b4dd93c
7462e82
08355f8
1b07499
c09812b
00c3a84
ee5eb3e
fcb361a
e6843e2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,7 @@ public class ExprTimestampValue extends AbstractExprValue { | |
/** | ||
* todo. only support UTC now. | ||
*/ | ||
private static final ZoneId ZONE = ZoneId.of("UTC"); | ||
public static final ZoneId ZONE = ZoneId.of("UTC"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps specify this is |
||
|
||
private final Instant timestamp; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,17 +20,20 @@ | |
import static org.opensearch.sql.expression.function.FunctionDSL.impl; | ||
import static org.opensearch.sql.expression.function.FunctionDSL.implWithProperties; | ||
import static org.opensearch.sql.expression.function.FunctionDSL.nullMissingHandling; | ||
import static org.opensearch.sql.expression.function.FunctionDSL.nullMissingHandlingWithProperties; | ||
import static org.opensearch.sql.utils.DateTimeFormatters.DATE_FORMATTER_LONG_YEAR; | ||
import static org.opensearch.sql.utils.DateTimeFormatters.DATE_FORMATTER_SHORT_YEAR; | ||
import static org.opensearch.sql.utils.DateTimeFormatters.DATE_TIME_FORMATTER_LONG_YEAR; | ||
import static org.opensearch.sql.utils.DateTimeFormatters.DATE_TIME_FORMATTER_SHORT_YEAR; | ||
import static org.opensearch.sql.utils.DateTimeFormatters.DATE_TIME_FORMATTER_STRICT_WITH_TZ; | ||
import static org.opensearch.sql.utils.DateTimeUtils.extractDateTime; | ||
|
||
import java.math.BigDecimal; | ||
import java.math.RoundingMode; | ||
import java.text.DecimalFormat; | ||
import java.time.Clock; | ||
import java.time.DateTimeException; | ||
import java.time.Duration; | ||
import java.time.Instant; | ||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
|
@@ -63,6 +66,7 @@ | |
import org.opensearch.sql.expression.function.DefaultFunctionResolver; | ||
import org.opensearch.sql.expression.function.FunctionDSL; | ||
import org.opensearch.sql.expression.function.FunctionName; | ||
import org.opensearch.sql.expression.function.FunctionProperties; | ||
import org.opensearch.sql.expression.function.FunctionResolver; | ||
import org.opensearch.sql.utils.DateTimeUtils; | ||
|
||
|
@@ -88,6 +92,7 @@ public class DateTimeFunction { | |
*/ | ||
public void register(BuiltinFunctionRepository repository) { | ||
repository.register(adddate()); | ||
repository.register(addtime()); | ||
repository.register(convert_tz()); | ||
repository.register(curtime()); | ||
repository.register(curdate()); | ||
|
@@ -122,6 +127,7 @@ public void register(BuiltinFunctionRepository repository) { | |
repository.register(quarter()); | ||
repository.register(second()); | ||
repository.register(subdate()); | ||
repository.register(subtime()); | ||
repository.register(sysdate()); | ||
repository.register(time()); | ||
repository.register(time_to_sec()); | ||
|
@@ -233,6 +239,52 @@ private DefaultFunctionResolver adddate() { | |
return add_date(BuiltinFunctionName.ADDDATE.getName()); | ||
} | ||
|
||
/** | ||
* Adds expr2 to expr1 and returns the result. | ||
* (TIME, TIME/DATE/DATETIME/TIMESTAMP) -> TIME | ||
* (DATE/DATETIME/TIMESTAMP, TIME/DATE/DATETIME/TIMESTAMP) -> DATETIME | ||
* TODO: MySQL has these signatures too | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we going to implement these other signatures in a separate PR or were they meant to be done here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In another PR |
||
* (STRING, STRING/TIME) -> STRING // second arg - string with time only | ||
* (x, STRING) -> NULL // second arg - string with timestamp | ||
* (x, STRING/DATE) -> x // second arg - string with date only | ||
*/ | ||
private DefaultFunctionResolver addtime() { | ||
return define(BuiltinFunctionName.ADDTIME.getName(), | ||
implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), | ||
TIME, TIME, TIME), | ||
implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), | ||
TIME, TIME, DATE), | ||
implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), | ||
TIME, TIME, DATETIME), | ||
implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), | ||
TIME, TIME, TIMESTAMP), | ||
implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), | ||
DATETIME, DATETIME, TIME), | ||
implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), | ||
DATETIME, DATETIME, DATE), | ||
implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), | ||
DATETIME, DATETIME, DATETIME), | ||
implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), | ||
DATETIME, DATETIME, TIMESTAMP), | ||
implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), | ||
DATETIME, DATE, TIME), | ||
implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), | ||
DATETIME, DATE, DATE), | ||
implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), | ||
DATETIME, DATE, DATETIME), | ||
implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), | ||
DATETIME, DATE, TIMESTAMP), | ||
implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), | ||
DATETIME, TIMESTAMP, TIME), | ||
implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), | ||
DATETIME, TIMESTAMP, DATE), | ||
implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), | ||
DATETIME, TIMESTAMP, DATETIME), | ||
implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), | ||
DATETIME, TIMESTAMP, TIMESTAMP) | ||
); | ||
} | ||
|
||
/** | ||
* Converts date/time from a specified timezone to another specified timezone. | ||
* The supported signatures: | ||
|
@@ -505,6 +557,52 @@ private DefaultFunctionResolver subdate() { | |
return sub_date(BuiltinFunctionName.SUBDATE.getName()); | ||
} | ||
|
||
/** | ||
* Subtracts expr2 from expr1 and returns the result. | ||
* (TIME, TIME/DATE/DATETIME/TIMESTAMP) -> TIME | ||
* (DATE/DATETIME/TIMESTAMP, TIME/DATE/DATETIME/TIMESTAMP) -> DATETIME | ||
* TODO: MySQL has these signatures too | ||
* (STRING, STRING/TIME) -> STRING // second arg - string with time only | ||
* (x, STRING) -> NULL // second arg - string with timestamp | ||
* (x, STRING/DATE) -> x // second arg - string with date only | ||
*/ | ||
private DefaultFunctionResolver subtime() { | ||
return define(BuiltinFunctionName.SUBTIME.getName(), | ||
implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), | ||
TIME, TIME, TIME), | ||
implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), | ||
TIME, TIME, DATE), | ||
implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), | ||
TIME, TIME, DATETIME), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does this case match with SQL? |
||
implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), | ||
TIME, TIME, TIMESTAMP), | ||
implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), | ||
DATETIME, DATETIME, TIME), | ||
implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), | ||
DATETIME, DATETIME, DATE), | ||
implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), | ||
DATETIME, DATETIME, DATETIME), | ||
implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), | ||
DATETIME, DATETIME, TIMESTAMP), | ||
implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), | ||
DATETIME, DATE, TIME), | ||
implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), | ||
DATETIME, DATE, DATE), | ||
implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), | ||
DATETIME, DATE, DATETIME), | ||
implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), | ||
DATETIME, DATE, TIMESTAMP), | ||
implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), | ||
DATETIME, TIMESTAMP, TIME), | ||
implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), | ||
DATETIME, TIMESTAMP, DATE), | ||
implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), | ||
DATETIME, TIMESTAMP, DATETIME), | ||
implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), | ||
DATETIME, TIMESTAMP, TIMESTAMP) | ||
); | ||
} | ||
|
||
/** | ||
* Extracts the time part of a date and time value. | ||
* Also to construct a time type. The supported signatures: | ||
|
@@ -641,6 +739,39 @@ private ExprValue exprAddDateDays(ExprValue date, ExprValue days) { | |
: exprValue); | ||
} | ||
|
||
/** | ||
* Adds or subtracts time to/from date and returns the result. | ||
* | ||
* @param functionProperties A FunctionProperties object. | ||
* @param temporal A Date/Time/Datetime/Timestamp value to change. | ||
* @param temporalDelta A Date/Time/Datetime/Timestamp object to add/subtract time from. | ||
* @param isAdd A flag: true to add, false to subtract. | ||
* @return A value calculated. | ||
*/ | ||
private ExprValue exprApplyTime(FunctionProperties functionProperties, | ||
ExprValue temporal, ExprValue temporalDelta, Boolean isAdd) { | ||
var interval = Duration.between(LocalTime.MIN, temporalDelta.timeValue()); | ||
var result = isAdd | ||
? extractDateTime(temporal, functionProperties).plus(interval) | ||
: extractDateTime(temporal, functionProperties).minus(interval); | ||
return temporal.type() == TIME | ||
? new ExprTimeValue(result.toLocalTime()) | ||
: new ExprDatetimeValue(result); | ||
} | ||
|
||
/** | ||
* Adds time to date and returns the result. | ||
* | ||
* @param functionProperties A FunctionProperties object. | ||
* @param temporal A Date/Time/Datetime/Timestamp value to change. | ||
* @param temporalDelta A Date/Time/Datetime/Timestamp object to add time from. | ||
* @return A value calculated. | ||
*/ | ||
private ExprValue exprAddTime(FunctionProperties functionProperties, | ||
ExprValue temporal, ExprValue temporalDelta) { | ||
return exprApplyTime(functionProperties, temporal, temporalDelta, true); | ||
} | ||
|
||
/** | ||
* CONVERT_TZ function implementation for ExprValue. | ||
* Returns null for time zones outside of +13:00 and -12:00. | ||
|
@@ -1025,6 +1156,18 @@ private ExprValue exprSubDateInterval(ExprValue date, ExprValue expr) { | |
: exprValue); | ||
} | ||
|
||
/** | ||
* Subtracts expr2 from expr1 and returns the result. | ||
* | ||
* @param temporal A Date/Time/Datetime/Timestamp value to change. | ||
* @param temporalDelta A Date/Time/Datetime/Timestamp to subtract time from. | ||
* @return A value calculated. | ||
*/ | ||
private ExprValue exprSubTime(FunctionProperties functionProperties, | ||
ExprValue temporal, ExprValue temporalDelta) { | ||
return exprApplyTime(functionProperties, temporal, temporalDelta, false); | ||
} | ||
|
||
/** | ||
* Time implementation for ExprValue. | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any chance you know about this TODO?