You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "RunyaoChen (via GitHub)" <gi...@apache.org> on 2023/02/01 23:03:19 UTC

[GitHub] [spark] RunyaoChen commented on a diff in pull request #39855: [SPARK-42286][SQL] Fallback to previous codegen code path for complex expr with CAST

RunyaoChen commented on code in PR #39855:
URL: https://github.com/apache/spark/pull/39855#discussion_r1093823654


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -2511,40 +2511,41 @@ case class UpCast(child: Expression, target: AbstractDataType, walkedTypePath: S
  */
 case class CheckOverflowInTableInsert(child: Expression, columnName: String)
     extends UnaryExpression {
-  checkChild(child)
-
-  private def checkChild(child: Expression): Unit = child match {
-    case _: Cast =>
-    case ExpressionProxy(c, _, _) if c.isInstanceOf[Cast] =>
-    case _ =>
-      throw SparkException.internalError("Child is not Cast or ExpressionProxy of Cast")
-  }
 
   override protected def withNewChildInternal(newChild: Expression): Expression = {
-    checkChild(newChild)
     copy(child = newChild)
   }
 
-  private def getCast: Cast = child match {
+  private def getCast: Option[Cast] = child match {
     case c: Cast =>
-      c
+      Some(c)
     case ExpressionProxy(c, _, _) =>
-      c.asInstanceOf[Cast]
+      Some(c.asInstanceOf[Cast])

Review Comment:
   Done.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -2511,40 +2511,41 @@ case class UpCast(child: Expression, target: AbstractDataType, walkedTypePath: S
  */
 case class CheckOverflowInTableInsert(child: Expression, columnName: String)
     extends UnaryExpression {
-  checkChild(child)
-
-  private def checkChild(child: Expression): Unit = child match {
-    case _: Cast =>
-    case ExpressionProxy(c, _, _) if c.isInstanceOf[Cast] =>
-    case _ =>
-      throw SparkException.internalError("Child is not Cast or ExpressionProxy of Cast")
-  }
 
   override protected def withNewChildInternal(newChild: Expression): Expression = {
-    checkChild(newChild)
     copy(child = newChild)
   }
 
-  private def getCast: Cast = child match {
+  private def getCast: Option[Cast] = child match {
     case c: Cast =>
-      c
+      Some(c)
     case ExpressionProxy(c, _, _) =>
-      c.asInstanceOf[Cast]
+      Some(c.asInstanceOf[Cast])
+    case _ => None
   }
 
   override def eval(input: InternalRow): Any = try {
     child.eval(input)
   } catch {
     case e: SparkArithmeticException =>
-      val cast = getCast
-      throw QueryExecutionErrors.castingCauseOverflowErrorInTableInsert(
-        cast.child.dataType,
-        cast.dataType,
-        columnName)
+      getCast match {
+        case Some(cast) =>
+          throw QueryExecutionErrors.castingCauseOverflowErrorInTableInsert(
+            cast.child.dataType,
+            cast.dataType,
+            columnName)
+        case None => throw e
+      }
   }
 
   override protected def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
-    val child = getCast
+    getCast match {
+      case Some(child) => doGenCodeWithNewErrorMsg(ctx, ev, child)
+      case None => child.genCode(ctx)
+    }
+  }
+
+  def doGenCodeWithNewErrorMsg(ctx: CodegenContext, ev: ExprCode, child: Cast): ExprCode = {

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.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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