Github user tarekauel commented on a diff in the pull request:
https://github.com/apache/spark/pull/6762#discussion_r33700254
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringOperations.scala
---
@@ -220,6 +222,404 @@ case class EndsWith(left: Expression, right:
Expression)
}
/**
+ * A function that trim the spaces from both ends for the specified string.
+ */
+case class StringTrim(child: Expression)
+ extends UnaryExpression with String2StringExpression {
+
+ def convert(v: UTF8String): UTF8String = v.trim()
+
+ override def toString: String = s"TRIM($child)"
+ override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode):
String = {
+ defineCodeGen(ctx, ev, c => s"($c).trim()")
+ }
+}
+
+/**
+ * A function that trim the spaces from left end for given string.
+ */
+case class StringTrimLeft(child: Expression)
+ extends UnaryExpression with String2StringExpression {
+
+ def convert(v: UTF8String): UTF8String = v.trimLeft()
+
+ override def toString: String = s"LTRIM($child)"
+ override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode):
String = {
+ defineCodeGen(ctx, ev, c => s"($c).trimLeft()")
+ }
+}
+
+/**
+ * A function that trim the spaces from right end for given string.
+ */
+case class StringTrimRight(child: Expression)
+ extends UnaryExpression with String2StringExpression {
+
+ def convert(v: UTF8String): UTF8String = v.trimRight()
+
+ override def toString: String = s"RTRIM($child)"
+ override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode):
String = {
+ defineCodeGen(ctx, ev, c => s"($c).trimRight()")
+ }
+}
+
+/**
+ * A function that Returns the position of the first occurrence of substr
in the given string.
+ * Returns null if either of the arguments are null and
+ * returns 0 if substr could not be found in str.
+ *
+ * Be aware that this is not zero based. The first character in str has
index 1
+ */
+case class StringInstr(str: Expression, substr: Expression)
+ extends Expression with ExpectsInputTypes {
+
+ override def children: Seq[Expression] = str :: substr :: Nil
+ override def foldable: Boolean = str.foldable && substr.foldable
+ override def nullable: Boolean = str.nullable || substr.nullable
+ override def dataType: DataType = IntegerType
+ override def expectedChildTypes: Seq[DataType] = Seq(StringType,
StringType)
+
+ override def eval(input: InternalRow): Any = {
+ val l = str.eval(input)
+ if (l == null) {
+ null
+ } else {
+ val r = substr.eval(input)
+ if (r == null) {
+ null
+ } else {
+ l.asInstanceOf[UTF8String].instr(r.asInstanceOf[UTF8String], 0) + 1
+ }
+ }
+ }
+
+ override def toString: String = s"INSTR($str, $substr)"
+}
+
+/**
+ * A function that returns the position of the first occurrence of substr
+ * in given string after position pos.
+ */
+case class StringLocate(substr: Expression, str: Expression, start:
Expression)
+ extends Expression with ExpectsInputTypes {
+
+ def this(substr: Expression, str: Expression) = {
+ this(substr, str, Literal(0))
+ }
+
+ override def children: Seq[Expression] = substr :: str :: start :: Nil
+ override def foldable: Boolean = children.forall(_.foldable)
+ override def nullable: Boolean = substr.nullable || str.nullable
+ override def dataType: DataType = IntegerType
+ override def expectedChildTypes: Seq[DataType] = Seq(StringType,
StringType, IntegerType)
+
+ override def eval(input: InternalRow): Any = {
+ val s = start.eval(input)
+ if (s == null) {
+ // if the start position is null, we need to return 0, (keep it
conform to Hive)
+ 0
+ } else {
+ val r = substr.eval(input)
+ if (r == null) {
+ null
+ } else {
+ val l = str.eval(input)
+ if (l == null) {
+ null
+ } else {
+ l.asInstanceOf[UTF8String].instr(
+ r.asInstanceOf[UTF8String],
+ s.asInstanceOf[Int]) + 1
+ }
+ }
+ }
+ }
+
+ override def toString: String = s"LOCATE($substr, $str[, $start])"
+}
+
+/**
+ * Returns str, left-padded with pad to a length of len
--- End diff --
`.` at the end of the comment, same for `performOp`.
---
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]