spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [spark] yaooqinn commented on a change in pull request #26412: [SPARK-29774][SQL] Date and Timestamp type +/- null should be null as Postgres
Date Mon, 02 Dec 2019 13:51:04 GMT
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_r352604626
 
 

 ##########
 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:
   `case (_, CalendarIntervalType) => MultiplyInterval(r, l)` might be enough for this
case.

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Mime
View raw message