gengliangwang commented on a change in pull request #35607:
URL: https://github.com/apache/spark/pull/35607#discussion_r815816857
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/datetimeExpressions.scala
##########
@@ -3131,3 +3131,88 @@ case class TimestampAdd(
copy(unit = newFirst, quantity = newSecond, timestamp = newThird)
}
}
+
+// scalastyle:off line.size.limit
+@ExpressionDescription(
+ usage = "_FUNC_(unit, startTimestamp, endTimestamp) - Gets the difference
between the timestamps `endTimestamp` and `startTimestamp` in the specified
units by truncating the fraction part.",
+ arguments = """
+ Arguments:
+ * unit - this indicates the units of the difference between the given
timestamps.
+ Supported string values of `unit` are (case insensitive):
+ - "YEAR"
+ - "QUARTER" - 3 months
+ - "MONTH"
+ - "WEEK" - 7 days
+ - "DAY"
+ - "HOUR"
+ - "MINUTE"
+ - "SECOND"
+ - "MILLISECOND"
+ - "MICROSECOND"
+ * startTimestamp - A timestamp which the expression subtracts from
`endTimestamp`.
+ * endTimestamp - A timestamp from which the expression subtracts
`startTimestamp`.
+ """,
+ examples = """
+ Examples:
+ > SELECT _FUNC_('HOUR', timestamp_ntz'2022-02-11 20:30:00',
timestamp_ntz'2022-02-12 04:30:00');
+ 8
+ > SELECT _FUNC_('MONTH', timestamp_ltz'2022-01-01 00:00:00',
timestamp_ltz'2022-02-28 00:00:00');
+ 1
+ > SELECT _FUNC_(SECOND, date'2022-01-01', timestamp'2021-12-31
23:59:50');
+ -10
+ > SELECT _FUNC_(YEAR, timestamp'2000-01-01 01:02:03.123456',
timestamp'2010-01-01 01:02:03.123456');
+ 10
+ """,
+ group = "datetime_funcs",
+ since = "3.3.0")
+// scalastyle:on line.size.limit
+case class TimestampDiff(
+ unit: Expression,
+ startTimestamp: Expression,
+ endTimestamp: Expression,
+ timeZoneId: Option[String] = None)
+ extends TernaryExpression
+ with ImplicitCastInputTypes
+ with NullIntolerant
+ with TimeZoneAwareExpression {
+
+ def this(unit: Expression, quantity: Expression, timestamp: Expression) =
+ this(unit, quantity, timestamp, None)
+
+ override def first: Expression = unit
+ override def second: Expression = startTimestamp
+ override def third: Expression = endTimestamp
+
+ override def inputTypes: Seq[AbstractDataType] =
+ Seq(StringType, AnyTimestampType, AnyTimestampType)
Review comment:
@cloud-fan good point.
I just tried running some tests:
```
> select timestampdiff(second, timestamp_ntz'2021-01-01 00:00:00',
timestamp_ltz'2021-01-01 00:00:00'
28800
> select timestamp_ntz'2021-01-01 00:00:00' - timestamp_ltz'2021-01-01
00:00:00'
INTERVAL '0 00:00:00' DAY TO SECOND
```
I think we can simply write the input type as TimestampType and the implicit
cast trait will handle the rest input combination.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]