yaooqinn commented on a change in pull request #26412: [SPARK-29774][SQL] Date 
and Timestamp type +/- null should be null as Postgres
URL: https://github.com/apache/spark/pull/26412#discussion_r352602498
 
 

 ##########
 File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala
 ##########
 @@ -246,6 +247,54 @@ class Analyzer(
       CleanupAliases)
   )
 
+  /**
+   * 1. Turns Add/Subtract of DateType/TimestampType/StringType and 
CalendarIntervalType
+   *    to TimeAdd/TimeSub.
+   * 2. Turns Add/Subtract of TimestampType/DateType/IntegerType
+   *    and TimestampType/IntegerType/DateType to 
DateAdd/DateSub/SubtractDates and
+   *    to SubtractTimestamps.
+   * 3. Turns Multiply/Divide of CalendarIntervalType and NumericType
+   *    to MultiplyInterval/DivideInterval
+   */
+  case class ResolveBinaryArithmetic(conf: SQLConf) extends Rule[LogicalPlan] {
+    override def apply(plan: LogicalPlan): LogicalPlan = 
plan.resolveOperatorsUp {
+      case p: LogicalPlan => p.transformExpressionsUp {
+        case u @ UnresolvedAdd(l, r) if u.childrenResolved => (l.dataType, 
r.dataType) match {
+          case (TimestampType | DateType | StringType, CalendarIntervalType) =>
+            Cast(TimeAdd(l, r), l.dataType)
+          case (CalendarIntervalType, TimestampType | DateType | StringType) =>
+            Cast(TimeAdd(r, l), r.dataType)
+          case (DateType, _) => DateAdd(l, r)
+          case (_, DateType) => DateAdd(r, l)
+          case (_, _) => Add(l, r)
+        }
+        case u @ UnresolvedSubtract(l, r) if u.childrenResolved => 
(l.dataType, r.dataType) match {
+          case (TimestampType | DateType | StringType, CalendarIntervalType) =>
+            Cast(TimeSub(l, r), l.dataType)
+          case (CalendarIntervalType, TimestampType | DateType | StringType) =>
+            Cast(TimeSub(r, l), r.dataType)
+          case (DateType | NullType, DateType) => if 
(conf.usePostgreSQLDialect) {
+            DateDiff(l, r)
+          } else {
+            SubtractDates(l, r)
+          }
+          case (TimestampType, TimestampType | DateType | NullType) => 
SubtractTimestamps(l, r)
+          case (DateType | NullType, TimestampType) => 
SubtractTimestamps(Cast(l, TimestampType), r)
+          case (DateType, _) => DateSub(l, r)
+          case (_, _) => Subtract(l, r)
+        }
+        case u @ UnresolvedMultiply(l, r) if u.childrenResolved => 
(l.dataType, r.dataType) match {
+          case (CalendarIntervalType, _: NumericType | NullType) => 
MultiplyInterval(l, r)
+          case (_: NumericType | NullType, CalendarIntervalType) => 
MultiplyInterval(r, l)
 
 Review comment:
   Here is another thing to discuss, do we have to only support `interval * 
numeric` and`interval / numeric`, this is not same with other type coercion 
rules, e.g. `1 / '2'` is valid, so `interval 1 day / '2'` should be valid too.
   
   In PostgreSQL, also valid
   ```
   postgres=# select interval '1' day * 2;
    ?column?
   ----------
    2 days
   (1 row)
   
   postgres=# select interval '1' day * '2';
    ?column?
   ----------
    2 days
   (1 row)
   
   postgres=# select interval '1' day / '2';
    ?column?
   ----------
    12:00:00
   (1 row)
   ```

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to