cloud-fan commented on a change in pull request #30905: URL: https://github.com/apache/spark/pull/30905#discussion_r549605431
########## File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringExpressions.scala ########## @@ -756,6 +756,54 @@ trait String2TrimExpression extends Expression with ImplicitCastInputTypes { override def nullable: Boolean = children.exists(_.nullable) override def foldable: Boolean = children.forall(_.foldable) + protected def doEval(srcString: UTF8String): UTF8String + protected def doEval(srcString: UTF8String, trimString: UTF8String): UTF8String + + override def eval(input: InternalRow): Any = { + val srcString = srcStr.eval(input).asInstanceOf[UTF8String] + if (srcString == null) { + null + } else if (trimStr.isDefined) { + doEval(srcString, trimStr.get.eval(input).asInstanceOf[UTF8String]) + } else { + doEval(srcString) + } + } + + protected val trimMethod: String + + override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = { + val evals = children.map(_.genCode(ctx)) + val srcString = evals(0) + + if (evals.length == 1) { + ev.copy(code = evals.map(_.code) :+ + code""" + |boolean ${ev.isNull} = false; + |UTF8String ${ev.value} = null; + |if (${srcString.isNull}) { + | ${ev.isNull} = true; + |} else { + | ${ev.value} = ${srcString.value}.$trimMethod(); + |} + """) + } else { + val trimString = evals(1) + ev.copy(code = evals.map(_.code) :+ Review comment: We can skip evaluating trim string if possible ``` ev.copy(code = code""" |${evals.head.code} |if (${srcString.isNull}) { | ... |} else { | ${trimString.code} | if (${trimString.isNull}) ... ``` ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org