cloud-fan commented on a change in pull request #34494:
URL: https://github.com/apache/spark/pull/34494#discussion_r748032282



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala
##########
@@ -81,9 +82,13 @@ object Cast {
     case (StringType, CalendarIntervalType) => true
     case (StringType, _: DayTimeIntervalType) => true
     case (StringType, _: YearMonthIntervalType) => true
+    case (_: IntegralType, DayTimeIntervalType(s, e)) if s == e => true
+    case (_: IntegralType, YearMonthIntervalType(s, e)) if s == e => true
 
     case (_: DayTimeIntervalType, _: DayTimeIntervalType) => true
     case (_: YearMonthIntervalType, _: YearMonthIntervalType) => true
+    case (DayTimeIntervalType(s, e), _: IntegralType) if s == e => true
+    case (YearMonthIntervalType(s, e), _: IntegralType) if s == e => true

Review comment:
       Since we can cast `INTERVAL DAY TO SECOND` to `INTERVAL SECOND`, I think 
it's a reasonable and convenient extension to cast `INTERVAL DAY TO SECOND` to 
integral type directly. That said, we can allow casting any interval type to 
integral type. For casting integral to interval, we should still only allow 
single field.

##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala
##########
@@ -598,6 +606,15 @@ abstract class CastBase extends UnaryExpression with 
TimeZoneAwareExpression wit
       IntervalUtils.castStringToYMInterval(s, it.startField, it.endField))
     case _: YearMonthIntervalType => buildCast[Int](_, s =>
       IntervalUtils.periodToMonths(IntervalUtils.monthsToPeriod(s), 
it.endField))
+    case x: IntegralType if it.startField == it.endField =>
+      b =>
+        val intValue = try {
+          x.exactNumeric.asInstanceOf[Numeric[Any]].toInt(b)

Review comment:
       how about we let `intToYearMonthInterval` take long? then only 
`intToYearMonthInterval` need to do overflow check, not here.

##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala
##########
@@ -598,6 +606,15 @@ abstract class CastBase extends UnaryExpression with 
TimeZoneAwareExpression wit
       IntervalUtils.castStringToYMInterval(s, it.startField, it.endField))
     case _: YearMonthIntervalType => buildCast[Int](_, s =>
       IntervalUtils.periodToMonths(IntervalUtils.monthsToPeriod(s), 
it.endField))
+    case x: IntegralType if it.startField == it.endField =>

Review comment:
       > So I think no need add error message here
   
   +1




-- 
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]

Reply via email to