Skip to content
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

Update DATE_ADD/ADDDATE and DATE_SUB/SUBDATE functions. #122

Merged
merged 12 commits into from
Dec 16, 2022

Conversation

Yury-Fridlyand
Copy link

@Yury-Fridlyand Yury-Fridlyand commented Sep 19, 2022

Signed-off-by: Yury-Fridlyand [email protected]

Description

I referred to MySQL docs and tried to reproduce MySQL v.8.0.30 behavior as a reference.

Rework on DATE_ADD/ADDDATE functions.

Changes

Fixes:

  1. Function accepts TIME, considering today's date. If argument is DATE, it interpreted as midnight of this DATE.
  2. Function returns always DATETIME.
  3. Function can interact with any combination of argument types and date and time intervals both.

Future changes (TODOs):

  1. Rework after [FEATURE] Add 3 interval types instead of 1 to support complex interval expressions opensearch-project/sql#859
  2. Accept and return STRING

Removed function DATE_ADD(date, int)

Left function (matches MySQL):

ADDDATE(date, interval)
DATE_ADD(date, interval)
ADDDATE(date, int)

Update signature list

  1. Added TIME
  2. Removed STRING - all datetime types have implicit cast from a string already
More details
FunctionBeforeAfter

DATE_ADD

(STRING/DATE/DATETIME/TIMESTAMP, INTERVAL) -> DATETIME
(DATE/DATETIME/TIMESTAMP/TIME, INTERVAL) -> DATETIME

ADDDATE

(DATE, LONG) -> DATE
(STRING/DATETIME/TIMESTAMP, LONG) -> DATETIME
(DATE, LONG) -> DATE
(TIME/DATETIME/TIMESTAMP, LONG) -> DATETIME

Rework on DATE_SUB/SUBDATE functions.

Changes

Removed function DATE_SUB(date, int)

The same fixes and notes as for DATE_ADD/ADDDATE functions.

Test queries:

NB: Due to opensearch-project#853 you can't test with DATETIME (without code changes), but I tested with it

OpenSearch
SELECT date0, DATE_ADD(CAST(date0 AS date), INTERVAL 1 HOUR) AS `date + 1h`,
       time0, DATE_ADD(CAST(time0 AS datetime), INTERVAL 1 HOUR) AS `datetime + 1h`,
	   time1, DATE_ADD(CAST(time1 AS time), INTERVAL 1 HOUR) AS `time + 1h`,
   datetime0, DATE_ADD(CAST(datetime0 AS timestamp), INTERVAL 1 HOUR) AS `timestamp + 1h` FROM calcs LIMIT 6;
   
SELECT date0, DATE_ADD(CAST(date0 AS date), INTERVAL 1 DAY) AS `date + 1d`,
       time0, DATE_ADD(CAST(time0 AS datetime), INTERVAL 1 DAY) AS `datetime + 1d`,
	   time1, DATE_ADD(CAST(time1 AS time), INTERVAL 1 DAY) AS `time + 1d`,
   datetime0, DATE_ADD(CAST(datetime0 AS timestamp), INTERVAL 1 DAY) AS `timestamp + 1d` FROM calcs LIMIT 6;
   
SELECT date0, DATE_ADD(CAST(date0 AS date), INTERVAL 1 HOUR) AS `date + 1h`, DATE_ADD(CAST(date0 AS date), INTERVAL 1 DAY) AS `date + 1d` FROM calcs LIMIT 6;
SELECT time0, DATE_ADD(CAST(time0 AS datetime), INTERVAL 1 HOUR) AS `datetime + 1h`, DATE_ADD(CAST(time0 AS datetime), INTERVAL 1 DAY) AS `datetime + 1d` FROM calcs LIMIT 6;
SELECT time1, DATE_ADD(CAST(time1 AS time), INTERVAL 24 HOUR) AS `time + 24h` FROM calcs LIMIT 6;
SELECT datetime0, DATE_ADD(CAST(datetime0 AS timestamp), INTERVAL 1 HOUR) AS `timestamp + 1h`, DATE_ADD(CAST(datetime0 AS timestamp), INTERVAL 1 DAY) AS `timestamp + 1d` FROM calcs LIMIT 6;

SELECT time1, CAST(time1 AS time), ADDDATE(CAST(time1 AS time), 1) FROM calcs LIMIT 6;
SELECT date0, CAST(date0 AS date), ADDDATE(CAST(date0 AS date), 1) FROM calcs LIMIT 6;
SELECT time0, CAST(time0 AS datetime), ADDDATE(CAST(time0 AS datetime), 1) FROM calcs LIMIT 6;
SELECT datetime0, CAST(datetime0 AS timestamp), ADDDATE(CAST(datetime0 AS timestamp), 1) FROM calcs LIMIT 6;
MySQL
SELECT date0, DATE_ADD(date0, INTERVAL 1 HOUR) AS `date + 1h`, time0, DATE_ADD(time0, INTERVAL 1 HOUR) AS `datetime + 1h`, time1, DATE_ADD(time1, INTERVAL 1 HOUR) AS `time + 1h`, datetime0, DATE_ADD(datetime0, INTERVAL 1 HOUR) AS `timestamp + 1h` FROM calcs LIMIT 6;
SELECT date0, DATE_ADD(date0, INTERVAL 1 DAY) AS `date + 1d`, time0, DATE_ADD(time0, INTERVAL 1 DAY) AS `datetime + 1d`, time1, DATE_ADD(time1, INTERVAL 1 DAY) AS `time + 1d`, datetime0, DATE_ADD(datetime0, INTERVAL 1 DAY) AS `timestamp + 1d` FROM calcs LIMIT 6;

SELECT time1, DATE_ADD(time1, INTERVAL 20 HOUR) AS `time + 20h` FROM calcs LIMIT 6;
SELECT date0, DATE_ADD(date0, INTERVAL 1 HOUR) AS `date + 1h`, DATE_ADD(date0, INTERVAL 1 DAY) AS `date + 1d` FROM calcs LIMIT 6;
SELECT time0, DATE_ADD(time0, INTERVAL 1 HOUR) AS `datetime + 1h`, DATE_ADD(time0, INTERVAL 1 DAY) AS `datetime + 1d` FROM calcs LIMIT 6;
SELECT datetime0, DATE_ADD(datetime0, INTERVAL 1 HOUR) AS `timestamp + 1h`, DATE_ADD(datetime0, INTERVAL 1 DAY) AS `timestamp + 1d` FROM calcs LIMIT 6;

SELECT time1, ADDDATE(time1, 1) FROM calcs LIMIT 6;
SELECT date0, ADDDATE(date0, 1) FROM calcs LIMIT 6;
SELECT time0, ADDDATE(time0, 1) FROM calcs LIMIT 6;
SELECT datetime0, ADDDATE(datetime0, 1) FROM calcs LIMIT 6;
PostgreSQL
SELECT time1, pg_typeof(time1), "time + 5h", pg_typeof("time + 5h"), "time + 1d", pg_typeof("time + 1d") FROM (SELECT time1, time1 + INTERVAL '5 HOURS' AS "time + 5h", time1 + INTERVAL '1 DAYS' AS "time + 1d" FROM calcs LIMIT 6) foo;

SELECT date0, pg_typeof(date0), "date + 5h", pg_typeof("date + 5h"), "date + 1d", pg_typeof("date + 1d") FROM (SELECT date0, date0 + INTERVAL '5 HOURS' AS "date + 5h", date0 + INTERVAL '1 DAYS' AS "date + 1d" FROM calcs LIMIT 6) foo;

Test data

I found that first 6 rows from date0, time0, time1, datetime0 are good for testing - these columns have different data types in MySQL. In OpenSearch SQL all [date][time] columns have timestamp type, so I use CAST for clear testing.

data
mysql> show fields from Calcs where field IN ('date0', 'time0', 'time1', 'datetime0');
+-----------+-----------+------+-----+---------+-------+
| Field     | Type      | Null | Key | Default | Extra |
+-----------+-----------+------+-----+---------+-------+
| date0     | date      | YES  |     | NULL    |       |
| time0     | datetime  | YES  |     | NULL    |       |
| time1     | time      | YES  |     | NULL    |       |
| datetime0 | timestamp | YES  |     | NULL    |       |
+-----------+-----------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> select date0, time0, time1, datetime0 from calcs;
+------------+---------------------+----------+---------------------+
| date0      | time0               | time1    | datetime0           |
+------------+---------------------+----------+---------------------+
| 2004-04-15 | 1899-12-30 21:07:32 | 19:36:22 | 2004-07-09 10:17:35 |
| 1972-07-04 | 1900-01-01 13:48:48 | 02:05:25 | 2004-07-26 12:30:34 |
| 1975-11-12 | 1900-01-01 18:21:08 | 09:33:31 | 2004-08-02 07:59:23 |
| 2004-06-04 | 1900-01-01 18:51:48 | 22:50:16 | 2004-07-05 13:14:20 |
| 2004-06-19 | 1900-01-01 15:01:19 | NULL     | 2004-07-28 23:30:22 |
| NULL       | 1900-01-01 08:59:39 | 19:57:33 | 2004-07-22 00:30:23 |
| NULL       | 1900-01-01 07:37:48 | NULL     | 2004-07-28 06:54:50 |
| NULL       | 1900-01-01 19:45:54 | 19:48:23 | 2004-07-12 17:30:16 |
| NULL       | 1900-01-01 09:00:59 | 22:20:14 | 2004-07-04 22:49:28 |
| NULL       | 1900-01-01 20:36:00 | NULL     | 2004-07-23 21:13:37 |
| NULL       | 1900-01-01 01:31:32 | 00:05:57 | 2004-07-14 08:16:44 |
| NULL       | 1899-12-30 22:15:40 | 04:40:49 | 2004-07-25 15:22:26 |
| NULL       | 1900-01-01 13:53:46 | 04:48:07 | 2004-07-17 14:01:56 |
| NULL       | 1900-01-01 04:57:51 | NULL     | 2004-07-19 22:21:31 |
| NULL       | 1899-12-30 22:42:43 | 18:58:41 | 2004-07-31 11:57:52 |
| NULL       | 1899-12-30 22:24:08 | NULL     | 2004-07-14 07:43:00 |
| NULL       | 1900-01-01 11:58:29 | 12:33:57 | 2004-07-28 12:34:28 |
+------------+---------------------+----------+---------------------+
17 rows in set (0.00 sec)

Related issue:

opensearch-project#291

Check List

  • New functionality includes testing.
    • All tests pass, including unit test, integration test and doctest
  • New functionality has been documented.
    • New functionality has javadoc added
    • New functionality has user manual doc added
  • Commits are signed per the DCO using --signoff

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

@codecov

This comment was marked as spam.

@Yury-Fridlyand Yury-Fridlyand marked this pull request as ready for review September 28, 2022 02:51
@Yury-Fridlyand Yury-Fridlyand changed the title Update date/time calculation functions Add and update date/time calculation functions Sep 29, 2022
@Yury-Fridlyand Yury-Fridlyand force-pushed the dev-datetime-calculations branch from d1dfe33 to 315bc23 Compare September 29, 2022 03:52
@Yury-Fridlyand
Copy link
Author

Oops. Do I need to update docs with DATETIME examples?

@MaxKsyunz
Copy link

MaxKsyunz commented Sep 29, 2022

@Yury-Fridlyand could yup update integ-datetime-calculations from upstream/2.x -- commit list includes unrelated ones.

Also SQL Java CI build says there's a failing integration test.

@Yury-Fridlyand Yury-Fridlyand force-pushed the dev-datetime-calculations branch from 315bc23 to c690f52 Compare October 3, 2022 17:48
Comment on lines 27 to 38
"%\\{"
+ "(?<name>"
+ "(?<pattern>[A-z0-9]+)"
+ "(?::(?<subname>[A-z0-9_:;,\\-\\/\\s\\.']+))?"
+ ")"
+ "(?:=(?<definition>"
+ "(?:"
+ "(?:[^{}]+|\\.+)+"
+ ")+"
+ ")"
+ ")?"
+ "\\}");

Check failure

Code scanning / CodeQL

Inefficient regular expression

This part of the regular expression may cause exponential backtracking on strings starting with '%{{0=' and containing many repetitions of 'z'.
Comment on lines 27 to 38
"%\\{"
+ "(?<name>"
+ "(?<pattern>[A-z0-9]+)"
+ "(?::(?<subname>[A-z0-9_:;,\\-\\/\\s\\.']+))?"
+ ")"
+ "(?:=(?<definition>"
+ "(?:"
+ "(?:[^{}]+|\\.+)+"
+ ")+"
+ ")"
+ ")?"
+ "\\}");

Check failure

Code scanning / CodeQL

Inefficient regular expression

This part of the regular expression may cause exponential backtracking on strings starting with '%{{0=' and containing many repetitions of '.'.
public class GrokCompiler implements Serializable {

// We don't want \n and commented line
private static final Pattern patternLinePattern = Pattern.compile("^([A-z0-9_]+)\\s+(.*)$");

Check warning

Code scanning / CodeQL

Overly permissive regular expression range

Suspicious character range that is equivalent to \[A-Z\\[\\\\]^_`a-z\].
Comment on lines 27 to 38
"%\\{"
+ "(?<name>"
+ "(?<pattern>[A-z0-9]+)"
+ "(?::(?<subname>[A-z0-9_:;,\\-\\/\\s\\.']+))?"
+ ")"
+ "(?:=(?<definition>"
+ "(?:"
+ "(?:[^{}]+|\\.+)+"
+ ")+"
+ ")"
+ ")?"
+ "\\}");

Check warning

Code scanning / CodeQL

Overly permissive regular expression range

Suspicious character range that is equivalent to \[A-Z\\[\\\\]^_`a-z\].
Comment on lines 27 to 38
"%\\{"
+ "(?<name>"
+ "(?<pattern>[A-z0-9]+)"
+ "(?::(?<subname>[A-z0-9_:;,\\-\\/\\s\\.']+))?"
+ ")"
+ "(?:=(?<definition>"
+ "(?:"
+ "(?:[^{}]+|\\.+)+"
+ ")+"
+ ")"
+ ")?"
+ "\\}");

Check warning

Code scanning / CodeQL

Overly permissive regular expression range

Suspicious character range that is equivalent to \[A-Z\\[\\\\]^_`a-z\].
Copy link

@MaxKsyunz MaxKsyunz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You know there are a lot of changes when GitHub refuses to load a diff...
image

These changes would be easier to understand if they were done as separate PRs.
It's easier to review six small PRs vs one large one.

Comment on lines 827 to 841
public void testSubTime() throws IOException {
var result = executeQuery("SELECT"
+ " SUBTIME(DATE('2008-12-12'), DATE('2008-11-15')) AS `'2008-12-12' - 0`,"
+ " SUBTIME(TIME('23:59:59'), DATE('2004-01-01')) AS `'23:59:59' - 0`,"
+ " SUBTIME(DATE('2004-01-01'), TIME('23:59:59')) AS `'2004-01-01' - '23:59:59'`,"
+ " SUBTIME(TIME('10:20:30'), TIME('00:05:42')) AS `'10:20:30' - '00:05:42'`,"
+ " SUBTIME(TIMESTAMP('1999-12-31 15:42:13'), DATETIME('1961-04-12 09:07:00')) AS `'15:42:13' - '09:07:00'`");
verifySchema(result,
schema("SUBTIME(DATE('2008-12-12'), DATE('2008-11-15'))", "'2008-12-12' - 0", "datetime"),
schema("SUBTIME(TIME('23:59:59'), DATE('2004-01-01'))", "'23:59:59' - 0", "time"),
schema("SUBTIME(DATE('2004-01-01'), TIME('23:59:59'))", "'2004-01-01' - '23:59:59'", "datetime"),
schema("SUBTIME(TIME('10:20:30'), TIME('00:05:42'))", "'10:20:30' - '00:05:42'", "time"),
schema("SUBTIME(TIMESTAMP('1999-12-31 15:42:13'), DATETIME('1961-04-12 09:07:00'))", "'15:42:13' - '09:07:00'", "datetime"));
verifyDataRows(result, rows("2008-12-12 00:00:00", "23:59:59", "2003-12-31 00:00:01", "10:14:48", "1999-12-31 06:35:13"));
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be a lot easier to understand and debug failures if these test cases were separated.

sql/src/main/antlr/OpenSearchSQLParser.g4 Outdated Show resolved Hide resolved
return new ExprIntervalValue(Duration.ofDays(getIntegerValue(value)));
return new ExprIntervalValue(Period.ofDays(getIntegerValue(value)));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why change from Duration to Period?

Copy link
Author

@Yury-Fridlyand Yury-Fridlyand Oct 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was required by changes in a4bca4f:

private ExprValue exprAddDateInterval(ExprValue date, ExprValue expr) {
var dt = LocalDateTime.MIN;
if (date.type() == TIME) {
if (expr.intervalValue() instanceof Duration) {
return new ExprTimeValue(date.timeValue().plus(expr.intervalValue()));
} else {
dt = LocalDateTime.of(LocalDate.now(), date.timeValue());
}
} else {
dt = date.datetimeValue();
}
dt = dt.plus(expr.intervalValue());
if (date.type() == DATE && expr.intervalValue() instanceof Period) {
return new ExprDateValue(dt.toLocalDate());
}
return new ExprDatetimeValue(dt);
}

These changes were mostly reverted, because they add ambiguous function signatures:

impl(nullMissingHandling(DateTimeFunction::exprAddDateInterval), DATETIME, DATE, INTERVAL),
impl(nullMissingHandling(DateTimeFunction::exprAddDateInterval), DATE, DATE, INTERVAL),

returnType, arg1Type, arg2Type

I still want to keep this change regardless of the revert, because this makes INTERVAL more consistent:

private ExprValue minute(ExprValue value) {
return new ExprIntervalValue(Duration.ofMinutes(getLongValue(value)));
}
private ExprValue hour(ExprValue value) {
return new ExprIntervalValue(Duration.ofHours(getLongValue(value)));
}
private ExprValue day(ExprValue value) {
return new ExprIntervalValue(Period.ofDays(getIntegerValue(value)));
}
private ExprValue week(ExprValue value) {
return new ExprIntervalValue(Period.ofWeeks(getIntegerValue(value)));
}

Period for Date-like intervals and Duration for Time-like intervals

"source=%s | eval f = adddate(TIME('07:40:00'), interval 1 day) | fields f", TEST_INDEX_DATE));
verifySchema(result,
schema("f", null, "datetime"));
verifySome(result.getJSONArray("datarows"),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you use the HEAD command, could you validate with verifyDataRows?

@Yury-Fridlyand Yury-Fridlyand force-pushed the dev-datetime-calculations branch from 82f47ac to 2c1e83d Compare October 7, 2022 04:05
@Yury-Fridlyand Yury-Fridlyand changed the title Add and update date/time calculation functions Update DATE_ADD/ADDDATE and DATE_SUB/SUBDATE functions. Oct 7, 2022
* (DATE, LONG) -> DATE
* (STRING/DATETIME/TIMESTAMP, LONG) -> DATETIME
* (DATE/DATETIME/TIMESTAMP/TIME, INTERVAL) -> DATETIME
* TODO: MySQL has these signatures too

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this a TODO? or just a comment?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a room for future changes to completely align with MySQL; they depends on other features which are not implemented yet.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If possible, don't write TODO then.
That will often get caught in checkstyle rules and cause an automatic failure.

* (DATE, LONG) -> DATE
* (STRING/DATETIME/TIMESTAMP, LONG) -> DATETIME
* (DATE/DATETIME/TIMESTAMP/TIME, INTERVAL) -> DATETIME
* TODO: MySQL has these signatures too

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this a TODO, or just a comment?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok - just avoid the word TODO since it will catch as a checkstyle warning in some sets

docs/user/dql/functions.rst Outdated Show resolved Hide resolved
docs/user/ppl/functions/datetime.rst Show resolved Hide resolved
docs/user/ppl/functions/datetime.rst Outdated Show resolved Hide resolved
docs/user/ppl/functions/datetime.rst Outdated Show resolved Hide resolved
docs/user/ppl/functions/datetime.rst Outdated Show resolved Hide resolved
* (DATE, LONG) -> DATE
* (STRING/DATETIME/TIMESTAMP, LONG) -> DATETIME
* (DATE/DATETIME/TIMESTAMP/TIME, INTERVAL) -> DATETIME
* TODO: MySQL has these signatures too

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If possible, don't write TODO then.
That will often get caught in checkstyle rules and cause an automatic failure.

* (DATE, LONG) -> DATE
* (STRING/DATETIME/TIMESTAMP, LONG) -> DATETIME
* (DATE/DATETIME/TIMESTAMP/TIME, INTERVAL) -> DATETIME
* TODO: MySQL has these signatures too

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok - just avoid the word TODO since it will catch as a checkstyle warning in some sets

@@ -51,6 +56,21 @@ public LocalTime timeValue() {
return time;
}

@Override
public LocalDate dateValue() {
return LocalDate.now();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dateValue() needs to return the same any time it's called when evaluating a query. Using LocalDate.now() means that queries like SELECT CAST(TIME(12, 4) AS DATE), CAST(TIME(12, 4) AS DATE) will produce different results depending on when they are called.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we discussed, that will be a separate feature.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in b19ea96 after rebase. Include FunctionProperties feature from opensearch-project#1047.


@Test
public void adddate_returns_date_when_args_are_date_and_days() {
var res = adddate(LocalDate.of(1961, 4, 12), 100500);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's 100500?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

100500 days. Why not?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not clear that it's an arbitrary number. Since it's repeated several times here, I started to wonder if it's in any way meaningful.

Comment on lines 27 to 29
var res = subdate(LocalTime.of(21, 0), Duration.ofHours(1).plusMinutes(2));
assertEquals(DATETIME, res.type());
assertEquals(LocalTime.of(19, 58).atDate(LocalDate.now()), res.datetimeValue());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will fail if lines 27 and 29 are executed on different sides of midnight.

Line 29 needs to use the "current date value for query".

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we discussed, that will be a separate feature.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in b19ea96 after rebase. Include FunctionProperties feature from opensearch-project#1047.

var res = date_sub(LocalTime.of(10, 20, 30),
Duration.ofHours(1).plusMinutes(2).plusSeconds(42));
assertEquals(DATETIME, res.type());
assertEquals(LocalTime.of(9, 17, 48).atDate(LocalDate.now()), res.datetimeValue());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WIll fail if test is executed around midnight.

Needs to use "current date value for this query"

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we discussed, that will be a separate feature.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in b19ea96 after rebase. Include FunctionProperties feature from opensearch-project#1047.

@Yury-Fridlyand Yury-Fridlyand force-pushed the dev-datetime-calculations branch from a64d6f9 to b19ea96 Compare December 8, 2022 02:32
@Yury-Fridlyand
Copy link
Author

Rebased and updated to include FunctionProperties feature from opensearch-project#1047.
Please, re-review.

@Yury-Fridlyand Yury-Fridlyand changed the title [BLOCKED] Update DATE_ADD/ADDDATE and DATE_SUB/SUBDATE functions. Update DATE_ADD/ADDDATE and DATE_SUB/SUBDATE functions. Dec 8, 2022
Signed-off-by: Yury-Fridlyand <[email protected]>
* (STRING, INTERVAL) -> STRING
* (DATE, INTERVAL) -> DATE // when interval has no time part
* (TIME, INTERVAL) -> TIME // when interval has no date part
* (STRING, INTERVAL) -> STRING // when argument has date or datetime string,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicate of line 226?

Signed-off-by: Yury-Fridlyand <[email protected]>

@Test
public void adddate_returns_date_when_args_are_date_and_days() {
var res = adddate(LocalDate.of(1961, 4, 12), 100500);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not clear that it's an arbitrary number. Since it's repeated several times here, I started to wonder if it's in any way meaningful.

Signed-off-by: Yury-Fridlyand <[email protected]>
Signed-off-by: Yury-Fridlyand <[email protected]>
@@ -290,10 +290,6 @@ public static FunctionExpression multiply(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.MULTIPLY, expressions);
}

public static FunctionExpression adddate(Expression... expressions) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was the reason for getting rid of all of these functions? Were they simply not used?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. They were used in tests only
  2. I rewrote tests and made new DSL definitions for those functions in DateTimeTestBase
  3. These functions require a FunctionProperties object as of now, they crash on FunctionProperties.None

@@ -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");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe specify that ZONE is UTC_ZONE?

@Yury-Fridlyand Yury-Fridlyand merged commit 679bf19 into integ-datetime-calculations Dec 16, 2022
@Yury-Fridlyand Yury-Fridlyand deleted the dev-datetime-calculations branch December 16, 2022 02:46
Yury-Fridlyand added a commit that referenced this pull request Jan 5, 2023
* Rework on `DATE_ADD`/`ADDDATE` and `DATE_SUB`/`SUBDATE` functions.

Signed-off-by: Yury-Fridlyand <[email protected]>
GabeFernandez310 pushed a commit that referenced this pull request Feb 6, 2023
opensearch-project#1182)

* Update `DATE_ADD`/`ADDDATE` and `DATE_SUB`/`SUBDATE` functions. (#122)

Signed-off-by: Yury-Fridlyand <[email protected]>
GumpacG pushed a commit that referenced this pull request Feb 15, 2023
opensearch-project#1182) (opensearch-project#1325)

* Update `DATE_ADD`/`ADDDATE` and `DATE_SUB`/`SUBDATE` functions. (#122)

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

Co-authored-by: Yury-Fridlyand <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants