Github user chenghao-intel commented on a diff in the pull request:
https://github.com/apache/spark/pull/9902#discussion_r45689230
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringExpressions.scala
---
@@ -343,47 +343,71 @@ case class FindInSet(left: Expression, right:
Expression) extends BinaryExpressi
}
/**
- * A function that trim the spaces from both ends for the specified string.
+ * A function that trims the given character (by default space) from both
ends for given string.
*/
-case class StringTrim(child: Expression)
- extends UnaryExpression with String2StringExpression {
+case class StringTrim(left: Expression, right: Expression)
+ extends BinaryExpression with ImplicitCastInputTypes {
- def convert(v: UTF8String): UTF8String = v.trim()
+ def this(token: Expression) = {
+ this(token, Literal(' '))
+ }
+
+ override def dataType: DataType = StringType
+ override def inputTypes: Seq[DataType] = Seq(StringType, ByteType)
+
+ protected override def nullSafeEval(token: Any, ch: Any): Any =
+ token.asInstanceOf[UTF8String].trim(ch.asInstanceOf[Byte])
override def prettyName: String = "trim"
override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode):
String = {
- defineCodeGen(ctx, ev, c => s"($c).trim()")
+ defineCodeGen(ctx, ev, (t, c) => s"($t).trim($c)")
}
}
/**
- * A function that trim the spaces from left end for given string.
+ * A function that trims the given character (by default space) from left
end for given string.
*/
-case class StringTrimLeft(child: Expression)
- extends UnaryExpression with String2StringExpression {
+case class StringTrimLeft(left: Expression, right: Expression)
+ extends BinaryExpression with ImplicitCastInputTypes {
+
+ def this(token: Expression) = {
+ this(token, Literal(' '))
+ }
+
+ override def dataType: DataType = StringType
+ override def inputTypes: Seq[DataType] = Seq(StringType, ByteType)
- def convert(v: UTF8String): UTF8String = v.trimLeft()
+ protected override def nullSafeEval(token: Any, ch: Any): Any =
+ token.asInstanceOf[UTF8String].trimLeft(ch.asInstanceOf[Byte])
override def prettyName: String = "ltrim"
override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode):
String = {
- defineCodeGen(ctx, ev, c => s"($c).trimLeft()")
+ defineCodeGen(ctx, ev, (t, c) => s"($t).trimLeft($c)")
}
}
/**
- * A function that trim the spaces from right end for given string.
+ * A function that trims the given character (by default space) from right
end for given string.
*/
-case class StringTrimRight(child: Expression)
- extends UnaryExpression with String2StringExpression {
+case class StringTrimRight(left: Expression, right: Expression)
+ extends BinaryExpression with ImplicitCastInputTypes {
+
+ def this(token: Expression) = {
+ this(token, Literal(' '))
+ }
+
+ override def dataType: DataType = StringType
+ override def inputTypes: Seq[DataType] = Seq(StringType, ByteType)
--- End diff --
Using the `StringType` also for the second argument? e.g. In MySQL, it
supports case like:
`SELECT TRIM(LEADING 'leading' FROM 'leadingtext' )` => `text`
---
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]