File tree Expand file tree Collapse file tree
main/scala/org/apache/spark/sql/catalyst/analysis
test/scala/org/apache/spark/sql/catalyst/analysis Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -412,12 +412,17 @@ object TypeCoercion {
412412 case e if ! e.childrenResolved => e
413413
414414 case a @ BinaryArithmetic (left @ StringType (), right)
415- if right.dataType != CalendarIntervalType =>
415+ if right.dataType != CalendarIntervalType && right.dataType != TimestampType =>
416416 a.makeCopy(Array (Cast (left, DoubleType ), right))
417417 case a @ BinaryArithmetic (left, right @ StringType ())
418- if left.dataType != CalendarIntervalType =>
418+ if left.dataType != CalendarIntervalType && left.dataType != TimestampType =>
419419 a.makeCopy(Array (left, Cast (right, DoubleType )))
420420
421+ case a @ BinaryArithmetic (left @ TimestampType (), right @ StringType ()) =>
422+ a.makeCopy(Array (left, Cast (right, CalendarIntervalType )))
423+ case a @ BinaryArithmetic (left @ StringType (), right @ TimestampType ()) =>
424+ a.makeCopy(Array (Cast (left, CalendarIntervalType ), right))
425+
421426 // For equality between string and timestamp we cast the string to a timestamp
422427 // so that things like rounding of subsecond precision does not affect the comparison.
423428 case p @ Equality (left @ StringType (), right @ TimestampType ()) =>
Original file line number Diff line number Diff line change @@ -1576,6 +1576,21 @@ class TypeCoercionSuite extends AnalysisTest {
15761576 SpecifiedWindowFrame (RangeFrame , CurrentRow , UnboundedFollowing ))
15771577 )
15781578 }
1579+
1580+ test(" binary arithmetic with string promotion" ) {
1581+ val rule = TypeCoercion .PromoteStrings (conf)
1582+ val timestamp = Literal (new Timestamp (0L ))
1583+ val intervalStr = Literal (" interval 3 month 7 hours" )
1584+ ruleTest(rule,
1585+ Add (timestamp, intervalStr),
1586+ Add (timestamp, Cast (intervalStr, CalendarIntervalType )))
1587+ ruleTest(rule,
1588+ Add (intervalStr, timestamp),
1589+ Add (Cast (intervalStr, CalendarIntervalType ), timestamp))
1590+ ruleTest(rule,
1591+ Subtract (timestamp, intervalStr),
1592+ Subtract (timestamp, Cast (intervalStr, CalendarIntervalType )))
1593+ }
15791594}
15801595
15811596
You can’t perform that action at this time.
0 commit comments