amanomer commented on a change in pull request #26472: [SPARK-29838][SQL] 
PostgreSQL dialect: cast to timestamp
URL: https://github.com/apache/spark/pull/26472#discussion_r348912394
 
 

 ##########
 File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/postgreSQL/PostgreCastBase.scala
 ##########
 @@ -75,9 +85,57 @@ case class PostgreCastToBoolean(child: Expression, 
timeZoneId: Option[String])
 
   override def dataType: DataType = BooleanType
 
-  override def nullable: Boolean = child.nullable
-
   override def toString: String = s"PostgreCastToBoolean($child as 
${dataType.simpleString})"
 
   override def sql: String = s"CAST(${child.sql} AS ${dataType.sql})"
 }
+
+case class PostgreCastToTimestamp(child: Expression, timeZoneId: 
Option[String])
+  extends PostgreCastBase {
+  override def dataType: DataType = TimestampType
+
+  override def checkInputDataTypes(): TypeCheckResult = child.dataType match {
+    case StringType | DateType =>
+      TypeCheckResult.TypeCheckSuccess
+    case _ =>
+      TypeCheckResult.TypeCheckFailure(s"cannot cast type ${child.dataType} to 
timestamp")
+  }
+  /** Returns a copy of this expression with the specified timeZoneId. */
+  override def withTimeZone(timeZoneId: String): TimeZoneAwareExpression =
+    copy(timeZoneId = Option(timeZoneId))
+
+  override def castToTimestamp(from: DataType): Any => Any = from match {
+    case StringType =>
+      buildCast[UTF8String](_, utfs => DateTimeUtils.stringToTimestamp(utfs, 
zoneId)
+        .getOrElse(throw new AnalysisException(s"invalid input syntax for type 
timestamp:$utfs")))
+    case DateType =>
+      super.castToTimestamp(from)
+  }
+
+  override def castToTimestampCode(
+      from: DataType,
+      ctx: CodegenContext): CastFunction = from match {
+    case StringType =>
+      val zoneIdClass = classOf[ZoneId]
+      val zid = JavaCode.global(
+        ctx.addReferenceObj("zoneId", zoneId, zoneIdClass.getName),
+        zoneIdClass)
+      val longOpt = ctx.freshVariable("longOpt", classOf[Option[Long]])
+      (c, evPrim, evNull) =>
+        code"""
+          scala.Option<Long> $longOpt =
+            
org.apache.spark.sql.catalyst.util.DateTimeUtils.stringToTimestamp($c, $zid);
+          if ($longOpt.isDefined()) {
+            $evPrim = ((Long) $longOpt.get()).longValue();
+          } else {
+            $evNull = throw new AnalysisException(s"invalid input syntax for 
type timestamp:$c");
+          }
+         """
+    case DateType =>
+      super.castToTimestampCode(from, ctx)
+  }
+
+  override def toString: String = s"PostgreCastToTimestamp($child as 
${dataType.simpleString})"
+
+  override def sql: String = s"CAST(${child.sql} AS ${dataType.sql})"
 
 Review comment:
   Done

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