-
Notifications
You must be signed in to change notification settings - Fork 186
Support date and time functions: date_add, date_sub, day, dayname, dayofweek, dayofyear, from_days, hour, microsecond, minute, month, monthname, quarter, second, subdate, time_to_sec, to_days #746
Support date and time functions: date_add, date_sub, day, dayname, dayofweek, dayofyear, from_days, hour, microsecond, minute, month, monthname, quarter, second, subdate, time_to_sec, to_days #746
Conversation
…ime-functions # Conflicts: # core/src/main/java/com/amazon/opendistroforelasticsearch/sql/expression/datetime/DateTimeFunction.java # core/src/test/java/com/amazon/opendistroforelasticsearch/sql/expression/datetime/DateTimeFunctionTest.java
docs/user/dql/functions.rst
Outdated
(DATE, DATE INTERVAL) -> DATE | ||
(DATE, TIME INTERVAL) -> DATETIME | ||
(DATETIME/TIMESTAMP, INTERVAL) -> DATETIME |
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.
The format is the doc is not correct.
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.
could you add example for each case?
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.
fixed format & added example for each case.
docs/user/dql/functions.rst
Outdated
Description | ||
----------- | ||
|
||
Usage: date_add(date, INTERVAL expr unit) adds the time interval expr to date |
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.
please add other signature, e.g. data_add(date, expression) also
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.
done
docs/user/dql/functions.rst
Outdated
(DATE, TIME INTERVAL) -> DATETIME | ||
(DATETIME/TIMESTAMP, INTERVAL) -> DATETIME | ||
|
||
Synonyms: ADDDATE |
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.
do we support ADDDATE? if not, remove this.
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.
we support ADDDATE
impl(nullMissingHandling(DateTimeFunction::exprAddDateInterval), DATE, DATE, INTERVAL), | ||
impl(nullMissingHandling(DateTimeFunction::exprAddDateInterval), DATETIME, DATE, INTERVAL), |
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.
why the function has same input signature but has different output signature?
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.
some use cases required different return type. E.g select date_add(date('2020-09-16'), interval 1 hour)
should return 2020-09-16 01:00:00
but select date_add(date('2020-09-16'), interval 1 day)
should return 2020-09-17
. But this was always returning datetime in schema anyway so removed it.
/** | ||
* Specify a start date and add a temporal amount to the date. | ||
* The return type depends on the date type and the interval unit. Detailed supported signatures: | ||
* (DATE, DATETIME/TIMESTAMP, INTERVAL) -> DATETIME |
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.
do you mean (DATE/DATETIME/TIMESTAMP, INTERVAL) -> DATETIME
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.
yes, fixed it.
* | ||
* @param date ExprValue of Date/Datetime/Timestamp type. | ||
* @param expr ExprValue of Interval type, the temporal amount to add. | ||
* @return Date/Datetime resulted from expr added to date. |
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.
The ExprDatetimeValue only has type DATETIME.
*/ | ||
private ExprValue exprDayOfWeek(ExprValue date) { | ||
if (date instanceof ExprStringValue) { | ||
date = new ExprDateValue(date.stringValue()); |
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.
The input value should be final
Two questions about String data type.
I think we should have consistent interface for user. There are two options in my mind.
|
/** | ||
* DAY(DATE). return the day of the month (1-31). | ||
*/ | ||
private FunctionResolver day() { |
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.
e.g. day(date_add("2020-09-21", 1))
this will throw exception because day function can't accept datetime as paramater?
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.
added datetime & timestamp for all functions
Please also add test cases for PPL |
…ime-functions # Conflicts: # core/src/main/java/com/amazon/opendistroforelasticsearch/sql/expression/DSL.java # core/src/main/java/com/amazon/opendistroforelasticsearch/sql/expression/datetime/DateTimeFunction.java # core/src/main/java/com/amazon/opendistroforelasticsearch/sql/expression/function/BuiltinFunctionName.java # core/src/test/java/com/amazon/opendistroforelasticsearch/sql/expression/datetime/DateTimeFunctionTest.java # docs/user/dql/functions.rst # integ-test/src/test/java/com/amazon/opendistroforelasticsearch/sql/sql/DateTimeFunctionIT.java # ppl/src/main/antlr/OpenDistroPPLLexer.g4 # ppl/src/main/antlr/OpenDistroPPLParser.g4 # sql/src/main/antlr/OpenDistroSQLParser.g4
Added integration tests for PPL |
added string for all date-time functions. |
* @return Datetime resulted from expr added to date. | ||
*/ | ||
private ExprValue exprAddDateInterval(ExprValue date, ExprValue expr) { | ||
ExprValue exprValue = new ExprDatetimeValue(date.datetimeValue().plus(expr.intervalValue())); |
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.
adddate, date_add, date_sub & subdate need to return date/datetime according to calculated result. Here, I have returned date if time is zero for these functions to get correct result. But the return type will be still be datetime in schema because multiple return type can't be added in define function for the same input parameters.
@@ -43,6 +48,40 @@ public String stringValue() { | |||
return value; | |||
} | |||
|
|||
@Override |
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.
could you add the test cases for these functions?
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.
added test cases for date-time functions in ExprStringValue
…ime-functions # Conflicts: # sql/src/main/antlr/OpenDistroSQLIdentifierParser.g4
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.
Thanks for the change.
Issue #709
Description of changes:
Added implementation, unit test, manual IT, and doc updates for following date and time functions:
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.