xueyumusic commented on a change in pull request #21985: [SPARK-24884][SQL] add
regexp_extract_all support
URL: https://github.com/apache/spark/pull/21985#discussion_r317443513
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/regexpExpressions.scala
##########
@@ -471,3 +485,72 @@ case class RegExpExtract(subject: Expression, regexp:
Expression, idx: Expressio
})
}
}
+
+/**
+ * Extract all specific(idx) groups identified by a Java regex.
+ *
+ * NOTE: this expression is not THREAD-SAFE, as it has some internal mutable
status.
+ */
+@ExpressionDescription(
+ usage = "_FUNC_(str, regexp[, idx]) - Extracts all groups that matches
`regexp`.",
+ examples = """
+ Examples:
+ > SELECT _FUNC_('100-200,300-400', '(\\d+)-(\\d+)', 1);
+ [100, 300]
+ """)
+case class RegExpExtractAll(subject: Expression, regexp: Expression, idx:
Expression) extends RegExpExtractBase {
+ def this(s: Expression, r: Expression) = this(s, r, Literal(1))
+ override def children: Seq[Expression] = subject :: regexp :: idx :: Nil
+
+ override def nullSafeEval(s: Any, p: Any, r: Any): Any = {
+ val m = getMatcher(s, p)
+ var groupArrayBuffer = new ArrayBuffer[UTF8String]();
+
+ while (m.find) {
+ val mr: MatchResult = m.toMatchResult
+ val group = mr.group(r.asInstanceOf[Int])
+ if (group == null) { // Pattern matched, but not optional group
+ groupArrayBuffer += UTF8String.EMPTY_UTF8
+ } else {
+ groupArrayBuffer += UTF8String.fromString(group)
+ }
+ }
+
+ new GenericArrayData(groupArrayBuffer.toArray.asInstanceOf[Array[Any]])
+ }
+
+ override def dataType: DataType = ArrayType(StringType)
+ override def prettyName: String = "regexp_extract_all"
+
+ override protected def doGenCode(ctx: CodegenContext, ev: ExprCode):
ExprCode = {
+ val groupArray = ctx.freshName("groupArray")
+ val arrayClass = classOf[GenericArrayData].getName
+ val (classNamePattern, matcher, matchResult, termLastRegex, termPattern,
setEvNotNull) =
+ getDoGenCodeVals(ctx, ev)
+
+ nullSafeCodeGen(ctx, ev, (subject, regexp, idx) => {
+ s"""
+ if (!$regexp.equals($termLastRegex)) {
Review comment:
Thanks, @kiszk , updated.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]