Github user ueshin commented on a diff in the pull request: https://github.com/apache/spark/pull/1586#discussion_r15725455 --- Diff: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringOperations.scala --- @@ -208,6 +211,96 @@ case class EndsWith(left: Expression, right: Expression) def compare(l: String, r: String) = l.endsWith(r) } + +/** + * A function that returns the number of bytes in an expression + */ +case class Length(child: Expression) extends UnaryExpression { + + type EvaluatedType = Any + + override def dataType = IntegerType + + override def foldable = child.foldable + + override def nullable = child.nullable + + override def toString = s"Length($child)" + + override def eval(input: Row): EvaluatedType = { + val inputVal = child.eval(input) + if (inputVal == null) { + null + } else if (!inputVal.isInstanceOf[String]) { + inputVal.toString.length + } else { + OctetLenUtils.len(inputVal.asInstanceOf[String]) --- End diff -- We can just use `String#codePointCount`, which can do the same thing as Hive's implementation. `OctetLenUtils.len` implementation is not the same as Hive's one. Hive uses `o.a.h.io.Text` as an argument of the method, which returns UTF-8 bytes by `getBytes()` but `java.lang.String#getBytes` returns platform-dependent bytes. The reason why Hive checks one by one here is not to decode string from UTF-8 bytes. Doing encode/decode is heavy, so we should do it as less as possible.
--- 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 infrastruct...@apache.org or file a JIRA ticket with INFRA. ---