Skip to content

Commit

Permalink
[FLINK-10009][table] Fix the casting problem for built-in TIMESTAMPADD.
Browse files Browse the repository at this point in the history
This closes apache#7155
  • Loading branch information
XuQianJin-Stars authored and sunjincheng121 committed Nov 27, 2018
1 parent 17e3576 commit cd09fbf
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -839,8 +839,15 @@ object ScalarOperators {
generateArithmeticOperator(op, nullCheck, l, left, right, config)

case (SqlTimeTypeInfo.DATE, TimeIntervalTypeInfo.INTERVAL_MILLIS) =>
generateOperatorIfNotNull(nullCheck, SqlTimeTypeInfo.DATE, left, right) {
(l, r) => s"$l $op ((int) ($r / ${MILLIS_PER_DAY}L))"
resultType match {
case SqlTimeTypeInfo.DATE =>
generateOperatorIfNotNull(nullCheck, SqlTimeTypeInfo.DATE, left, right) {
(l, r) => s"$l $op ((int) ($r / ${MILLIS_PER_DAY}L))"
}
case SqlTimeTypeInfo.TIMESTAMP =>
generateOperatorIfNotNull(nullCheck, SqlTimeTypeInfo.TIMESTAMP, left, right) {
(l, r) => s"$l * ${MILLIS_PER_DAY}L $op $r"
}
}

case (SqlTimeTypeInfo.DATE, TimeIntervalTypeInfo.INTERVAL_MONTHS) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2702,17 +2702,68 @@ class ScalarFunctionsTest extends ScalarTypesTestBase {
"TIMESTAMPADD(HOUR, -200, CAST(NULL AS TIMESTAMP))",
"null")

testAllApis(
"2016-06-15".toDate + 1.day,
"'2016-06-15'.toDate + 1.day",
"TIMESTAMPADD(DAY, 1, DATE '2016-06-15')",
"2016-06-16")

testAllApis(
Null(Types.SQL_TIMESTAMP) + 3.months,
"Null(SQL_TIMESTAMP) + 3.months",
"TIMESTAMPADD(MONTH, 3, CAST(NULL AS TIMESTAMP))",
"null")

// TIMESTAMPADD with DATE returns a TIMESTAMP value for sub-day intervals.
testAllApis("2016-06-15".toDate + 1.month,
"'2016-06-15'.toDate + 1.month",
"timestampadd(MONTH, 1, date '2016-06-15')",
"2016-07-15")

testAllApis("2016-06-15".toDate + 1.day,
"'2016-06-15'.toDate + 1.day",
"timestampadd(DAY, 1, date '2016-06-15')",
"2016-06-16")

testAllApis("2016-06-15".toTimestamp - 1.hour,
"'2016-06-15'.toTimestamp - 1.hour",
"timestampadd(HOUR, -1, date '2016-06-15')",
"2016-06-14 23:00:00.0")

testAllApis("2016-06-15".toTimestamp + 1.minute,
"'2016-06-15'.toTimestamp + 1.minute",
"timestampadd(MINUTE, 1, date '2016-06-15')",
"2016-06-15 00:01:00.0")

testAllApis("2016-06-15".toTimestamp - 1.second,
"'2016-06-15'.toTimestamp - 1.second",
"timestampadd(SQL_TSI_SECOND, -1, date '2016-06-15')",
"2016-06-14 23:59:59.0")

testAllApis("2016-06-15".toTimestamp + 1.second,
"'2016-06-15'.toTimestamp + 1.second",
"timestampadd(SECOND, 1, date '2016-06-15')",
"2016-06-15 00:00:01.0")

testAllApis(Null(Types.SQL_TIMESTAMP) + 1.second,
"Null(SQL_TIMESTAMP) + 1.second",
"timestampadd(SECOND, 1, cast(null as date))",
"null")

testAllApis(Null(Types.SQL_TIMESTAMP) + 1.day,
"Null(SQL_TIMESTAMP) + 1.day",
"timestampadd(DAY, 1, cast(null as date))",
"null")

// Round to the last day of previous month
testAllApis("2016-05-31".toDate + 1.month,
"'2016-05-31'.toDate + 1.month",
"timestampadd(MONTH, 1, date '2016-05-31')",
"2016-06-30")

testAllApis("2016-01-31".toDate + 5.month,
"'2016-01-31'.toDate + 5.month",
"timestampadd(MONTH, 5, date '2016-01-31')",
"2016-06-30")

testAllApis("2016-03-31".toDate - 1.month,
"'2016-03-31'.toDate - 1.month",
"timestampadd(MONTH, -1, date '2016-03-31')",
"2016-02-29")
}

// ----------------------------------------------------------------------------------------------
Expand Down

0 comments on commit cd09fbf

Please sign in to comment.