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

    https://github.com/apache/spark/pull/21193#discussion_r189264892
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/codegen/javaCode.scala
 ---
    @@ -114,6 +114,115 @@ object JavaCode {
       }
     }
     
    +/**
    + * A trait representing a block of java code.
    + */
    +trait Block extends JavaCode {
    +
    +  // The expressions to be evaluated inside this block.
    +  def exprValues: Set[ExprValue]
    +
    +  // Returns java code string for this code block.
    +  override def toString: String = _marginChar match {
    +    case Some(c) => code.stripMargin(c).trim
    +    case _ => code.trim
    +  }
    +
    +  def length: Int = toString.length
    +
    +  // The leading prefix that should be stripped from each line.
    +  // By default we strip blanks or control characters followed by '|' from 
the line.
    +  var _marginChar: Option[Char] = Some('|')
    +
    +  def stripMargin(c: Char): this.type = {
    +    _marginChar = Some(c)
    +    this
    +  }
    +
    +  def stripMargin: this.type = {
    +    _marginChar = Some('|')
    +    this
    +  }
    +
    +  // Concatenates this block with other block.
    +  def + (other: Block): Block
    +}
    +
    +object Block {
    +
    +  val CODE_BLOCK_BUFFER_LENGTH: Int = 512
    +
    +  implicit def blocksToBlock(blocks: Seq[Block]): Block = Blocks(blocks)
    +
    +  implicit class BlockHelper(val sc: StringContext) extends AnyVal {
    +    def code(args: Any*): Block = {
    +      sc.checkLengths(args)
    +      if (sc.parts.length == 0) {
    +        EmptyBlock
    +      } else {
    +        args.foreach {
    +          case _: ExprValue =>
    +          case _: Int | _: Long | _: Float | _: Double | _: String =>
    +          case _: Block =>
    +          case other => throw new IllegalArgumentException(
    +            s"Can not interpolate ${other.getClass.getName} into code 
block.")
    +        }
    +        CodeBlock(sc.parts, args)
    +      }
    +    }
    +  }
    +}
    +
    +/**
    + * A block of java code. Including a sequence of code parts and some 
inputs to this block.
    + * The actual java code is generated by embedding the inputs into the code 
parts.
    + */
    +case class CodeBlock(codeParts: Seq[String], blockInputs: Seq[Any]) 
extends Block {
    +  override lazy val exprValues: Set[ExprValue] = {
    +    blockInputs.flatMap {
    +      case b: Block => b.exprValues
    +      case e: ExprValue => Set(e)
    +      case _ => Set.empty[ExprValue]
    +    }.toSet
    +  }
    +
    +  override def code: String = {
    +    val strings = codeParts.iterator
    +    val inputs = blockInputs.iterator
    +    val buf = new StringBuilder(Block.CODE_BLOCK_BUFFER_LENGTH)
    +    buf append StringContext.treatEscapes(strings.next)
    +    while (strings.hasNext) {
    +      buf append inputs.next
    +      buf append StringContext.treatEscapes(strings.next)
    +    }
    +    buf.toString
    +  }
    +
    +  override def + (other: Block): Block = other match {
    +    case c: CodeBlock => Blocks(Seq(this, c))
    +    case b: Blocks => Blocks(Seq(this) ++ b.blocks)
    +    case EmptyBlock => this
    +  }
    +}
    +
    +case class Blocks(blocks: Seq[Block]) extends Block {
    +  override lazy val exprValues: Set[ExprValue] = 
blocks.flatMap(_.exprValues).toSet
    +  override def code: String = blocks.map(_.toString).mkString("\n")
    --- End diff --
    
    Yeah, I think so. Ideally I think `JavaCode` should be immutable. Once we 
change code in it, we should have a new object instead of chancing current one.


---

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

Reply via email to