Github user mgaido91 commented on a diff in the pull request:
https://github.com/apache/spark/pull/21193#discussion_r185763081
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/codegen/javaCode.scala
---
@@ -112,6 +112,102 @@ object JavaCode {
def isNullExpression(code: String): SimpleExprValue = {
expression(code, BooleanType)
}
+
+ def block(code: String): Block = {
+ CodeBlock(codeParts = Seq(code), exprValues = Seq.empty)
+ }
+}
+
+/**
+ * A block of java code which involves some expressions represented by
`ExprValue`.
+ */
+trait Block extends JavaCode {
+ def exprValues: Seq[Any]
+
+ // This will be called during string interpolation.
+ override def toString: String = _marginChar match {
+ case Some(c) => code.stripMargin(c)
+ case _ => code
+ }
+
+ var _marginChar: Option[Char] = None
+
+ def stripMargin(c: Char): this.type = {
+ _marginChar = Some(c)
+ this
+ }
+
+ def stripMargin: this.type = {
+ _marginChar = Some('|')
+ this
+ }
+
+ def + (other: Block): Block
+}
+
+object Block {
+ implicit def blockToString(block: Block): String = block.toString
+
+ implicit def blocksToBlock(blocks: Seq[Block]): Block = Blocks(blocks)
+
+ implicit class BlockHelper(val sc: StringContext) extends AnyVal {
+ def code(args: Any*): Block = {
+ if (sc.parts.length == 0) {
+ EmptyBlock
+ } else {
+ args.foreach {
+ case _: ExprValue => true
+ case _: Int | _: Long | _: Float | _: Double | _: String => true
+ case _: Block => true
+ case other => throw new IllegalArgumentException(
+ s"Can not interpolate ${other.getClass} into code block.")
+ }
+ CodeBlock(sc.parts, args)
+ }
+ }
+ }
+}
+
+/**
+ * A block of java code.
+ */
+case class CodeBlock(codeParts: Seq[String], exprValues: Seq[Any]) extends
Block {
+ override def code: String = {
+ val strings = codeParts.iterator
+ val expressions = exprValues.iterator
+ var buf = new StringBuffer(strings.next)
+ while (strings.hasNext) {
+ if (expressions.hasNext) {
--- End diff --
this is not needed (especially if we add the `checkLengths`), because if it
is not true we are in a bad situation
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]