Github user davies commented on a diff in the pull request:
https://github.com/apache/spark/pull/7709#discussion_r36105562
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringOperations.scala
---
@@ -350,6 +351,78 @@ case class EndsWith(left: Expression, right:
Expression)
}
}
+object StringTranslate {
+
+ def buildDict(matchingString: UTF8String, replaceString: UTF8String)
+ : JMap[Character, Character] = {
+ val matching = matchingString.toString()
+ val replace = replaceString.toString()
+ val dict = new HashMap[Character, Character]()
+ var i = 0
+ while (i < matching.length()) {
+ val rep = if (i < replace.length()) replace.charAt(i) else '0'
+ if (null == dict.get(matching.charAt(i))) {
+ dict.put(matching.charAt(i), rep)
+ }
+ i += 1
+ }
+ dict
+ }
+}
+
+/**
+ * A function translate any character in the `srcExpr` by a character in
`replaceExpr`.
+ * The characters in `replaceExpr` is corresponding to the characters in
`matchingExpr`.
+ * The translate will happen when any character in the string matching
with the character
+ * in the `matchingExpr`.
+ */
+case class StringTranslate(srcExpr: Expression, matchingExpr: Expression,
replaceExpr: Expression)
+ extends TernaryExpression with ImplicitCastInputTypes {
+
+ @transient private var lastMatching: UTF8String = _
+ @transient private var lastReplace: UTF8String = _
+ @transient private var dict: JMap[Character, Character] = _
+
+ override def nullSafeEval(srcEval: Any, matchingEval: Any, replaceEval:
Any): Any = {
+ if (matchingEval != lastMatching || replaceEval != lastReplace) {
+ lastMatching = matchingEval.asInstanceOf[UTF8String]
+ lastReplace = replaceEval.asInstanceOf[UTF8String]
+ dict = StringTranslate.buildDict(lastMatching, lastReplace)
+ }
+ srcEval.asInstanceOf[UTF8String].translate(dict)
+ }
+
+ override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode):
String = {
+ val termLastMatching = ctx.freshName("lastMatching")
+ val termLastReplace = ctx.freshName("lastReplace")
+ val termDict = ctx.freshName("dict")
+ val classNameUTF8String = classOf[UTF8String].getCanonicalName
+ val classNameDict = classOf[JMap[Character,
Character]].getCanonicalName
+
+ ctx.addMutableState(classNameUTF8String, termLastMatching,
s"${termLastMatching} = null;")
+ ctx.addMutableState(classNameUTF8String, termLastReplace,
s"${termLastReplace} = null;")
+ ctx.addMutableState(classNameDict, termDict, s"${termDict} = null;")
+
+ nullSafeCodeGen(ctx, ev, (src, matching, replace) => {
+ s"""if (!${matching}.equals(${termLastMatching}) ||
+ !${replace}.equals(${termLastReplace})) {
+ // matching or replace value changed
+ ${termLastMatching} = ${matching};
--- End diff --
`${matching}.clone()`
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]