Uploaded image for project: 'Spark'
  1. Spark
  2. SPARK-8829 Improve expression performance
  3. SPARK-8935

Implement code generation for all casts

    XMLWordPrintableJSON

Details

    • Sub-task
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • None
    • 1.5.0
    • SQL
    • None

    Description

      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

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

      Attachments

        Activity

          People

            yijieshen Yijie Shen
            rxin Reynold Xin
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: