You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2022/02/03 00:49:46 UTC

[GitHub] [spark] HyukjinKwon commented on a change in pull request #35352: [SPARK-38063][SQL] Support split_part Function

HyukjinKwon commented on a change in pull request #35352:
URL: https://github.com/apache/spark/pull/35352#discussion_r798132509



##########
File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringExpressions.scala
##########
@@ -2795,3 +2795,88 @@ case class Sentences(
     copy(str = newFirst, language = newSecond, country = newThird)
 
 }
+
+/**
+ * Splits a given string by a specified delimiter.
+ */
+case class SplitByDelimiter(
+    str: Expression,
+    delimiter: Expression)
+  extends BinaryExpression with ImplicitCastInputTypes with NullIntolerant {
+  override def dataType: DataType = ArrayType(StringType, containsNull = false)
+  override def inputTypes: Seq[DataType] = Seq(StringType, StringType)
+  override def left: Expression = str
+  override def right: Expression = delimiter
+
+  override def nullSafeEval(string: Any, delimiter: Any): Any = {
+    val strings = {
+      // if delimiter is empty string, skip the regex based splitting directly as regex
+      // treats empty string as matching anything, thus use the input directly.
+      if (delimiter.asInstanceOf[UTF8String].numBytes() == 0) {
+        Array(string)
+      } else {
+        string.asInstanceOf[UTF8String].split(
+          delimiter.asInstanceOf[UTF8String], -1, true)
+      }
+    }
+    new GenericArrayData(strings.asInstanceOf[Array[Any]])
+  }
+
+  override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
+    val arrayClass = classOf[GenericArrayData].getName
+    nullSafeCodeGen(ctx, ev, (str, delimiter) => {
+      if (delimiter.asInstanceOf[UTF8String].numBytes() == 0) {
+        s"""${ev.value} = Array($str)""".stripMargin
+      } else {
+        // Array in java is covariant, so we don't need to cast UTF8String[] to Object[].
+        s"""${ev.value} = new $arrayClass($str.split($delimiter,-1, true));""".stripMargin
+      }
+    })
+  }
+
+  override protected def withNewChildrenInternal(
+    newFirst: Expression, newSecond: Expression): SplitByDelimiter =
+    copy(str = newFirst, delimiter = newSecond)
+}
+
+/**
+ * Splits a given string by a specified delimiter and returns the requested part.
+ * If any input is null, or index is out of range of split parts, returns null.
+ * If index is 0, throws an ArrayIndexOutOfBoundsException.
+ */
+@ExpressionDescription(
+  usage =
+    """
+    _FUNC_(str, delimiter, partNum) - Splits `str` by delimiter and return
+      requested part of the split (1-based). If any input is null, or partNum
+      is out of range of split parts, returns null. If partNum is 0, throws an
+      ArrayIndexOutOfBoundsException. If partNum is negative, the parts are

Review comment:
       (sorry nit but `` `partNum` ``. this backquotes are used in generated docs so it will markdown the argument properly.)




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org