Skip to content
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
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ object FunctionRegistry {
expression[StringLocate]("position"),
expression[FormatString]("printf"),
expression[RegExpExtract]("regexp_extract"),
expression[RegExpExtractAll]("regexp_extract_all"),
expression[RegExpReplace]("regexp_replace"),
expression[StringRepeat]("repeat"),
expression[StringReplace]("replace"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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._
Expand Down Expand Up @@ -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()
Copy link
Member

@kiszk kiszk Sep 3, 2019

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 clone p?
nvm, it follows the original code

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.
*
Expand All @@ -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])
Expand All @@ -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)) {
Expand All @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add an abstract class to reduce duplicated code between RegExpExtractAll and RegExpExtract?

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
|"""
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,36 @@ class RegexpExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
checkEvaluation(nonNullExpr, "100", row1)
}

test("RegexExtractAll") {
val row1 = create_row("100-200,300-400,500-600", "(\\d+)-(\\d+)", 1)
val row2 = create_row("100-200,300-400,500-600", "(\\d+)-(\\d+)", 2)
val row3 = create_row("100-200,300-400,500-600", "(\\d+).*", 1)
val row4 = create_row("100-200,300-400,500-600", "([a-z])", 1)
val row5 = create_row(null, "([a-z])", 1)
val row6 = create_row("100-200,300-400,500-600", null, 1)
val row7 = create_row("100-200,300-400,500-600", "([a-z])", null)

val s = 's.string.at(0)
val p = 'p.string.at(1)
val r = 'r.int.at(2)

val expr = RegExpExtractAll(s, p, r)
checkEvaluation(expr, Seq("100", "300", "500"), row1)
checkEvaluation(expr, Seq("200", "400", "600"), row2)
checkEvaluation(expr, Seq("100"), row3)
checkEvaluation(expr, Seq(), row4)
checkEvaluation(expr, null, row5)
checkEvaluation(expr, null, row6)
checkEvaluation(expr, null, row7)

val expr1 = new RegExpExtractAll(s, p)
checkEvaluation(expr1, Seq("100", "300", "500"), row1)

val nonNullExpr = RegExpExtractAll(Literal("100-200,300-400,500-600"),
Literal("(\\d+)-(\\d+)"), Literal(1))
checkEvaluation(nonNullExpr, Seq("100", "300", "500"), row1)
}

test("SPLIT") {
val s1 = 'a.string.at(0)
val s2 = 'b.string.at(1)
Expand Down