Reynold Xin created SPARK-8935:
----------------------------------

             Summary: Implement code generation for all casts
                 Key: SPARK-8935
                 URL: https://issues.apache.org/jira/browse/SPARK-8935
             Project: Spark
          Issue Type: Sub-task
          Components: SQL
            Reporter: Reynold Xin


Cast expression only supports a subset of type casts. We should just implement 
all the possible casts so we don't need to fall back to non-codegen mode.


See Cast.scala

{code}
  override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode): 
String = {
    // TODO: Add support for more data types.
    (child.dataType, dataType) match {

      case (BinaryType, StringType) =>
        defineCodeGen (ctx, ev, c =>
          s"${ctx.stringType}.fromBytes($c)")

      case (DateType, StringType) =>
        defineCodeGen(ctx, ev, c =>
          s"""${ctx.stringType}.fromString(
                
org.apache.spark.sql.catalyst.util.DateTimeUtils.dateToString($c))""")

      case (TimestampType, StringType) =>
        defineCodeGen(ctx, ev, c =>
          s"""${ctx.stringType}.fromString(
                
org.apache.spark.sql.catalyst.util.DateTimeUtils.timestampToString($c))""")

      case (_, StringType) =>
        defineCodeGen(ctx, ev, c => 
s"${ctx.stringType}.fromString(String.valueOf($c))")

      // fallback for DecimalType, this must be before other numeric types
      case (_, dt: DecimalType) =>
        super.genCode(ctx, ev)

      case (BooleanType, dt: NumericType) =>
        defineCodeGen(ctx, ev, c => s"(${ctx.javaType(dt)})($c ? 1 : 0)")

      case (dt: DecimalType, BooleanType) =>
        defineCodeGen(ctx, ev, c => s"!$c.isZero()")

      case (dt: NumericType, BooleanType) =>
        defineCodeGen(ctx, ev, c => s"$c != 0")

      case (_: DecimalType, dt: NumericType) =>
        defineCodeGen(ctx, ev, c => s"($c).to${ctx.primitiveTypeName(dt)}()")

      case (_: NumericType, dt: NumericType) =>
        defineCodeGen(ctx, ev, c => s"(${ctx.javaType(dt)})($c)")

      case other =>
        super.genCode(ctx, ev)
    }
  }
{code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

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

Reply via email to