beliefer commented on a change in pull request #24918: [SPARK-28077][SQL] Support ANSI SQL OVERLAY function. URL: https://github.com/apache/spark/pull/24918#discussion_r297459952
########## File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringExpressions.scala ########## @@ -454,6 +454,68 @@ case class StringReplace(srcExpr: Expression, searchExpr: Expression, replaceExp override def prettyName: String = "replace" } +object Overlay { + + def calcuate(input: UTF8String, replace: UTF8String, pos: Integer, len: Integer): UTF8String = { + val header = input.substringSQL(1, pos - 1) + var length = len + if (len < 0) { + length = replace.toString().length() + } + val tailer = input.substringSQL(pos + length, Int.MaxValue) + UTF8String.fromString(header.toString + replace.toString + tailer.toString) + } +} + +// scalastyle:off line.size.limit +@ExpressionDescription( + usage = "_FUNC_(input, replace, pos[, len]) - Replace `input` with `replace` that starts at `pos` and is of length `len`.", + examples = """ + Examples: + > SELECT _FUNC_('Spark SQL' PLACING '_' FROM 6); + Spark_SQL + > SELECT _FUNC_('Spark SQL' PLACING 'CORE' FROM 7); + Spark CORE + > SELECT _FUNC_('Spark SQL' PLACING 'ANSI ' FROM 7 FOR 0); + Spark ANSI SQL + > SELECT _FUNC_('Spark SQL' PLACING 'tructured' FROM 2 FOR 4); + Structured SQL + """) +// scalastyle:on line.size.limit +case class Overlay(input: Expression, replace: Expression, pos: Expression, len: Expression) + extends QuaternaryExpression with ImplicitCastInputTypes with NullIntolerant { + + def this(str: Expression, replace: Expression, pos: Expression) = { + this(str, replace, pos, Literal.create(-1, IntegerType)) + } + + override def dataType: DataType = StringType + + override def inputTypes: Seq[AbstractDataType] = + Seq(StringType, StringType, IntegerType, IntegerType) + + override def children: Seq[Expression] = input :: replace :: pos :: len :: Nil + + override def nullSafeEval(inputEval: Any, replaceEval: Any, posEval: Any, lenEval: Any): Any = { + val inputStr = inputEval.asInstanceOf[UTF8String] + val replaceStr = replaceEval.asInstanceOf[UTF8String] + val position = posEval.asInstanceOf[Int] + val length = lenEval.asInstanceOf[Int] + Overlay.calcuate(inputStr, replaceStr, position, length) + } + + override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = { + val result = ctx.addMutableState("UTF8String", "result") Review comment: @ueshin OK. I removed the unreasonable mutable state. ---------------------------------------------------------------- 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 With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org