Skip to content

Commit c5d751a

Browse files
committed
Support timestamp binaryArithmetic string
1 parent 971e832 commit c5d751a

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercion.scala

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff 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()) =>

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercionSuite.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)