Skip to content

Commit

Permalink
Add test case for group value is null
Browse files Browse the repository at this point in the history
  • Loading branch information
beliefer committed Feb 13, 2020
1 parent 517251a commit 5e3c092
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ case class RegExpExtract(subject: Expression, regexp: Expression, idx: Expressio
There is a SQL config 'spark.sql.parser.escapedStringLiterals' that can be used to
fallback to the Spark 1.6 behavior regarding string literal parsing. For example,
if the config is enabled, the `regexp` that can match "\abc" is "^\abc$".
* idx - an int expression of the regex group index. The regex maybe contains multiple
* idx - an int expression of the regex group index. The regex may contains multiple
groups. `idx` indicates which regex group to extract.
""",
examples = """
Expand All @@ -571,7 +571,6 @@ case class RegExpExtractAll(subject: Expression, regexp: Expression, idx: Expres
override def nullSafeEval(s: Any, p: Any, r: Any): Any = {
val m = getLastMatcher(s, p)
val matchResults = new ArrayBuffer[UTF8String]()
val mr: MatchResult = m.toMatchResult
while(m.find) {
val mr: MatchResult = m.toMatchResult
val index = r.asInstanceOf[Int]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ class StringFunctionsSuite extends QueryTest with SharedSparkSession {

test("non-matching optional group") {
val df = Seq(Tuple1("aaaac")).toDF("s")

// regexp_extract
checkAnswer(
df.select(regexp_extract($"s", "(foo)", 1)),
Row("")
Expand All @@ -132,6 +134,16 @@ class StringFunctionsSuite extends QueryTest with SharedSparkSession {
df.select(regexp_extract($"s", "(a+)(b)?(c)", 2)),
Row("")
)

// regexp_extract_all
checkAnswer(
df.select(regexp_extract_all($"s", "(foo)", 1)),
Row(Seq())
)
checkAnswer(
df.select(regexp_extract_all($"s", "(a+)(b)?(c)", 2)),
Row(Seq(""))
)
}

test("string ascii function") {
Expand Down

0 comments on commit 5e3c092

Please sign in to comment.