Github user yjshen commented on a diff in the pull request:

    https://github.com/apache/spark/pull/7365#discussion_r35615752
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala
 ---
    @@ -418,51 +418,508 @@ case class Cast(child: Expression, dataType: 
DataType)
       protected override def nullSafeEval(input: Any): Any = cast(input)
     
       override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode): 
String = {
    -    // TODO: Add support for more data types.
    -    (child.dataType, dataType) match {
    +    val nullSafeCast = nullSafeCastFunction(child.dataType, dataType, ctx)
    +    if (nullSafeCast != null) {
    +      val eval = child.gen(ctx)
    +      eval.code +
    +        castCode(ctx, eval.primitive, eval.isNull, ev.primitive, 
ev.isNull, dataType, nullSafeCast)
    +    } else {
    +      super.genCode(ctx, ev)
    +    }
    +  }
    +
    +  // three function arguments are: child.primitive, result.primitive and 
result.isNull
    +  // it returns the code snippets to be put in null safe evaluation region
    +  private[this] type CastFunction = (String, String, String) => String
    +
    +  private[this] def nullSafeCastFunction(
    +      from: DataType,
    +      to: DataType,
    +      ctx: CodeGenContext): CastFunction = to match {
    +
    +    case _ if from == NullType => (c, evPrim, evNull) => s"$evNull = true;"
    +    case _ if to == from => (c, evPrim, evNull) => s"$evPrim = $c;"
    --- End diff --
    
    cast expression will be inserted during physical operator 
transformation,for example `Average`, in which case its possible we add a `cast 
to self` expression. 


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

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

Reply via email to