Github user rxin commented on a diff in the pull request:
https://github.com/apache/spark/pull/7034#discussion_r34650807
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringOperations.scala
---
@@ -553,17 +552,23 @@ case class Substring(str: Expression, pos:
Expression, len: Expression)
}
/**
- * A function that return the length of the given string expression.
+ * A function that return the length of the given string or binary
expression.
*/
-case class StringLength(child: Expression) extends UnaryExpression with
ImplicitCastInputTypes {
+case class Length(child: Expression) extends UnaryExpression with
ExpectsInputTypes {
override def dataType: DataType = IntegerType
- override def inputTypes: Seq[DataType] = Seq(StringType)
+ override def inputTypes: Seq[AbstractDataType] =
Seq(TypeCollection(StringType, BinaryType))
- protected override def nullSafeEval(string: Any): Any =
- string.asInstanceOf[UTF8String].numChars
+ protected override def nullSafeEval(value: Any): Any = child.dataType
match {
+ case StringType => value.asInstanceOf[UTF8String].numChars
+ case BinaryType => value.asInstanceOf[Array[Byte]].length
+ }
override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode):
String = {
- defineCodeGen(ctx, ev, c => s"($c).numChars()")
+ child.dataType match {
+ case StringType => defineCodeGen(ctx, ev, c => s"($c).numChars()")
+ case BinaryType => defineCodeGen(ctx, ev, c => s"($c).length")
+ case NullType => defineCodeGen(ctx, ev, c => s"-1")
--- End diff --
don't need to support NullType here
---
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]