-
Notifications
You must be signed in to change notification settings - Fork 28.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SPARK-24884][SQL] add regexp_extract_all support #21985
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,8 @@ package org.apache.spark.sql.catalyst.expressions | |
import java.util.Locale | ||
import java.util.regex.{MatchResult, Pattern} | ||
|
||
import scala.collection.mutable.ArrayBuffer | ||
|
||
import org.apache.commons.text.StringEscapeUtils | ||
|
||
import org.apache.spark.sql.catalyst.expressions.codegen._ | ||
|
@@ -386,6 +388,40 @@ case class RegExpReplace(subject: Expression, regexp: Expression, rep: Expressio | |
} | ||
} | ||
|
||
abstract class RegExpExtractBase extends TernaryExpression with ImplicitCastInputTypes { | ||
// last regex in string, we will update the pattern iff regexp value changed. | ||
@transient protected var lastRegex: UTF8String = _ | ||
// last regex pattern, we cache it for performance concern | ||
@transient protected var pattern: Pattern = _ | ||
override def inputTypes: Seq[AbstractDataType] = Seq(StringType, StringType, IntegerType) | ||
|
||
protected def getMatcher(s: Any, p: Any) = { | ||
if (!p.equals(lastRegex)) { | ||
// regex value changed | ||
lastRegex = p.asInstanceOf[UTF8String].clone() | ||
pattern = Pattern.compile(lastRegex.toString) | ||
} | ||
pattern.matcher(s.toString) | ||
} | ||
|
||
protected def getDoGenCodeVals(ctx: CodegenContext, ev: ExprCode) = { | ||
val classNamePattern = classOf[Pattern].getCanonicalName | ||
val matcher = ctx.freshName("matcher") | ||
val matchResult = ctx.freshName("matchResult") | ||
|
||
val termLastRegex = ctx.addMutableState("UTF8String", "lastRegex") | ||
val termPattern = ctx.addMutableState(classNamePattern, "pattern") | ||
|
||
val setEvNotNull = if (nullable) { | ||
s"${ev.isNull} = false;" | ||
} else { | ||
"" | ||
} | ||
|
||
(classNamePattern, matcher, matchResult, termLastRegex, termPattern, setEvNotNull) | ||
} | ||
} | ||
|
||
/** | ||
* Extract a specific(idx) group identified by a Java regex. | ||
* | ||
|
@@ -400,21 +436,13 @@ case class RegExpReplace(subject: Expression, regexp: Expression, rep: Expressio | |
""", | ||
since = "1.5.0") | ||
case class RegExpExtract(subject: Expression, regexp: Expression, idx: Expression) | ||
extends TernaryExpression with ImplicitCastInputTypes { | ||
def this(s: Expression, r: Expression) = this(s, r, Literal(1)) | ||
extends RegExpExtractBase { | ||
|
||
// last regex in string, we will update the pattern iff regexp value changed. | ||
@transient private var lastRegex: UTF8String = _ | ||
// last regex pattern, we cache it for performance concern | ||
@transient private var pattern: Pattern = _ | ||
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 = { | ||
if (!p.equals(lastRegex)) { | ||
// regex value changed | ||
lastRegex = p.asInstanceOf[UTF8String].clone() | ||
pattern = Pattern.compile(lastRegex.toString) | ||
} | ||
val m = pattern.matcher(s.toString) | ||
val m = getMatcher(s, p) | ||
if (m.find) { | ||
val mr: MatchResult = m.toMatchResult | ||
val group = mr.group(r.asInstanceOf[Int]) | ||
|
@@ -429,24 +457,11 @@ case class RegExpExtract(subject: Expression, regexp: Expression, idx: Expressio | |
} | ||
|
||
override def dataType: DataType = StringType | ||
override def inputTypes: Seq[AbstractDataType] = Seq(StringType, StringType, IntegerType) | ||
override def children: Seq[Expression] = subject :: regexp :: idx :: Nil | ||
override def prettyName: String = "regexp_extract" | ||
|
||
override protected def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = { | ||
val classNamePattern = classOf[Pattern].getCanonicalName | ||
val matcher = ctx.freshName("matcher") | ||
val matchResult = ctx.freshName("matchResult") | ||
|
||
val termLastRegex = ctx.addMutableState("UTF8String", "lastRegex") | ||
val termPattern = ctx.addMutableState(classNamePattern, "pattern") | ||
|
||
val setEvNotNull = if (nullable) { | ||
s"${ev.isNull} = false;" | ||
} else { | ||
"" | ||
} | ||
|
||
val (classNamePattern, matcher, matchResult, termLastRegex, termPattern, setEvNotNull) = | ||
getDoGenCodeVals(ctx, ev) | ||
nullSafeCodeGen(ctx, ev, (subject, regexp, idx) => { | ||
s""" | ||
if (!$regexp.equals($termLastRegex)) { | ||
|
@@ -471,3 +486,74 @@ 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add an abstract class to reduce duplicated code between |
||
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)) { | ||
| // regex value changed | ||
| $termLastRegex = $regexp.clone(); | ||
| $termPattern = $classNamePattern.compile($termLastRegex.toString()); | ||
|} | ||
|java.util.regex.Matcher $matcher = | ||
| $termPattern.matcher($subject.toString()); | ||
|java.util.ArrayList $groupArray = | ||
| new java.util.ArrayList<UTF8String>(); | ||
|
||
|while ($matcher.find()) { | ||
| java.util.regex.MatchResult $matchResult = $matcher.toMatchResult(); | ||
| if ($matchResult.group($idx) == null) { | ||
| $groupArray.add(UTF8String.EMPTY_UTF8); | ||
| } else { | ||
| $groupArray.add(UTF8String.fromString($matchResult.group($idx))); | ||
| } | ||
|} | ||
|${ev.value} = new $arrayClass($groupArray.toArray(new UTF8String[$groupArray.size()])); | ||
|$setEvNotNull | ||
|""" | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Do we need to clonep
?nvm, it follows the original code