Skip to content

Commit fd828e1

Browse files
committed
[SPARK-16409][SQL] regexp_extract with optional groups causes NPE
## What changes were proposed in this pull request? regexp_extract actually returns null when it shouldn't when a regex matches but the requested optional group did not. This makes it return an empty string, as apparently designed. ## How was this patch tested? Additional unit test Author: Sean Owen <sowen@cloudera.com> Closes #14504 from srowen/SPARK-16409. (cherry picked from commit 8d87252) Signed-off-by: Sean Owen <sowen@cloudera.com>
1 parent 739a333 commit fd828e1

3 files changed

Lines changed: 22 additions & 2 deletions

File tree

python/pyspark/sql/functions.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,6 +1445,9 @@ def regexp_extract(str, pattern, idx):
14451445
>>> df = spark.createDataFrame([('100-200',)], ['str'])
14461446
>>> df.select(regexp_extract('str', '(\d+)-(\d+)', 1).alias('d')).collect()
14471447
[Row(d=u'100')]
1448+
>>> df = spark.createDataFrame([('aaaac',)], ['str'])
1449+
>>> df.select(regexp_extract('str', '(a+)(b)?(c)', 2).alias('d')).collect()
1450+
[Row(d=u'')]
14481451
"""
14491452
sc = SparkContext._active_spark_context
14501453
jc = sc._jvm.functions.regexp_extract(_to_java_column(str), pattern, idx)

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/regexpExpressions.scala

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,12 @@ case class RegExpExtract(subject: Expression, regexp: Expression, idx: Expressio
329329
val m = pattern.matcher(s.toString)
330330
if (m.find) {
331331
val mr: MatchResult = m.toMatchResult
332-
UTF8String.fromString(mr.group(r.asInstanceOf[Int]))
332+
val group = mr.group(r.asInstanceOf[Int])
333+
if (group == null) { // Pattern matched, but not optional group
334+
UTF8String.EMPTY_UTF8
335+
} else {
336+
UTF8String.fromString(group)
337+
}
333338
} else {
334339
UTF8String.EMPTY_UTF8
335340
}
@@ -367,7 +372,11 @@ case class RegExpExtract(subject: Expression, regexp: Expression, idx: Expressio
367372
${termPattern}.matcher($subject.toString());
368373
if (${matcher}.find()) {
369374
java.util.regex.MatchResult ${matchResult} = ${matcher}.toMatchResult();
370-
${ev.value} = UTF8String.fromString(${matchResult}.group($idx));
375+
if (${matchResult}.group($idx) == null) {
376+
${ev.value} = UTF8String.EMPTY_UTF8;
377+
} else {
378+
${ev.value} = UTF8String.fromString(${matchResult}.group($idx));
379+
}
371380
$setEvNotNull
372381
} else {
373382
${ev.value} = UTF8String.EMPTY_UTF8;

sql/core/src/test/scala/org/apache/spark/sql/StringFunctionsSuite.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ class StringFunctionsSuite extends QueryTest with SharedSQLContext {
9292
Row("300", "100") :: Row("400", "100") :: Row("400-400", "100") :: Nil)
9393
}
9494

95+
test("non-matching optional group") {
96+
val df = Seq("aaaac").toDF("s")
97+
checkAnswer(
98+
df.select(regexp_extract($"s", "(a+)(b)?(c)", 2)),
99+
Row("")
100+
)
101+
}
102+
95103
test("string ascii function") {
96104
val df = Seq(("abc", "")).toDF("a", "b")
97105
checkAnswer(

0 commit comments

Comments
 (0)