Github user chenghao-intel commented on a diff in the pull request:
https://github.com/apache/spark/pull/7533#discussion_r35331274
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringOperations.scala
---
@@ -356,6 +355,63 @@ case class StringInstr(str: Expression, substr:
Expression)
}
/**
+ * Returns the substring from string str before count occurrences of the
delimiter delim.
+ * If count is positive, everything the left of the final delimiter
(counting from left) is
+ * returned. If count is negative, every to the right of the final
delimiter (counting from the
+ * right) is returned. substring_index performs a case-sensitive match
when searching for delim.
+ */
+case class Substring_index(strExpr: Expression, delimExpr: Expression,
countExpr: Expression)
+ extends Expression with ImplicitCastInputTypes {
+
+ override def dataType: DataType = StringType
+ override def inputTypes: Seq[DataType] = Seq(StringType, StringType,
IntegerType)
+ override def nullable: Boolean = strExpr.nullable || delimExpr.nullable
|| countExpr.nullable
+ override def children: Seq[Expression] = Seq(strExpr, delimExpr,
countExpr)
+ override def prettyName: String = "substring_index"
+
+ override def eval(input: InternalRow): Any = {
+ val str = strExpr.eval(input)
+ if (str != null) {
+ val delim = delimExpr.eval(input)
+ if (delim != null) {
+ val count = countExpr.eval(input)
+ if (count != null) {
+ return UTF8String.subStringIndex(
+ str.asInstanceOf[UTF8String],
+ delim.asInstanceOf[UTF8String],
+ count.asInstanceOf[Int])
+ }
+ }
+ }
+ null
+ }
+
+ override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode):
String = {
+ val str = strExpr.gen(ctx)
+ val delim = delimExpr.gen(ctx)
+ val count = countExpr.gen(ctx)
+ val resultCode =
+ s"""org.apache.spark.unsafe.types.UTF8String.subStringIndex(
--- End diff --
if the `substringIndex` is not the static function, the code can be
simplified as:
```
${str.primitive}.subStringIndex(${delim.primitive}, ${count.primitive})
```
---
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]