Github user zhichao-li commented on a diff in the pull request:

    https://github.com/apache/spark/pull/7709#discussion_r36381923
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringOperations.scala
 ---
    @@ -349,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]
    --- End diff --
    
    @davies just not quite sure why we need clone() here, seems like the 
UTF8String implementation is kind of like String which do the "copy on write". 
I guess we can somehow treat it as a constant value?   


---
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]

Reply via email to