Github user javadba commented on a diff in the pull request:

    https://github.com/apache/spark/pull/1586#discussion_r15547795
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringOperations.scala
 ---
    @@ -209,6 +212,82 @@ case class EndsWith(left: Expression, right: 
Expression)
     }
     
     /**
    + * 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 string = child.eval(input)
    +    if (string == null) {
    +      null
    +    } else if (!string.isInstanceOf[String]) {
    +      string.toString.length
    +    } else {
    +      new String(string.toString.getBytes, 
StrlenConstants.DefaultEncoding).length
    +    }
    +  }
    +
    +}
    +
    +object StrlenConstants {
    +  val DefaultEncoding = "ISO-8859-1"
    +}
    +
    +/**
    + * A function that returns the number of characters in a string expression
    + */
    +case class Strlen(child: Expression, encoding : Expression) extends 
UnaryExpression
    +  with Logging {
    +
    +  type EvaluatedType = Any
    +
    +  override def dataType = IntegerType
    +
    +  override def foldable = child.foldable
    +
    +  override def nullable = true
    +
    +  override def toString = s"Strlen($child, $encoding)"
    +
    +  override def eval(input: Row): EvaluatedType = {
    +    val string = child.eval(input)
    +    if (string == null) {
    +      null
    +    } else if (!string.isInstanceOf[String]) {
    +      log.debug(s"Non-string value [$string] provided to strlen")
    +      null
    +    } else {
    +      var evalEncoding = encoding.eval(input)
    +      val strEncoding =
    +        if (evalEncoding != null) {
    +          evalEncoding.toString
    +        } else {
    +          StrlenConstants.DefaultEncoding
    +        }
    +      val s: String = ""
    +      try {
    +        new String(string.asInstanceOf[String].getBytes, 
strEncoding).length
    +      } catch {
    +        case ue : UnsupportedEncodingException => {
    +          log.debug(s"strlen: Caught UnsupportedEncodingException for 
encoding=[$strEncoding]")
    --- End diff --
    
    I am fine to throw an exception in these two cases: it was suggested by one 
of the reviewers to send null instead. personally as a developer of an app 
using the api I would likely consider it to be a bug that an encoding were 
incorrect so the exception makes more sense.
    
    So we have t


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

Reply via email to