Skip to content

Commit

Permalink
Add test cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
beliefer committed Jul 31, 2020
1 parent 3a98bbe commit 82a384e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -323,45 +323,47 @@ class RegexpExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
}

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 row1 = create_row("100-200,300-400,500-600", "(\\d+)-(\\d+)", 0)
val row2 = create_row("100-200,300-400,500-600", "(\\d+)-(\\d+)", 1)
val row3 = create_row("100-200,300-400,500-600", "(\\d+)-(\\d+)", 2)
val row4 = create_row("100-200,300-400,500-600", "(\\d+).*", 1)
val row5 = create_row("100-200,300-400,500-600", "([a-z])", 1)
val row6 = create_row(null, "([a-z])", 1)
val row7 = create_row("100-200,300-400,500-600", null, 1)
val row8 = 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, Seq("100-200", "300-400", "500-600"), row1)
checkEvaluation(expr, Seq("100", "300", "500"), row2)
checkEvaluation(expr, Seq("200", "400", "600"), row3)
checkEvaluation(expr, Seq("100"), row4)
checkEvaluation(expr, Seq(), row5)
checkEvaluation(expr, null, row6)
checkEvaluation(expr, null, row7)
checkEvaluation(expr, null, row8)

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

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

// invalid group index
val row8 = create_row("100-200,300-400,500-600", "(\\d+)-(\\d+)", 3)
val row9 = create_row("100-200,300-400,500-600", "(\\d+).*", 2)
val row10 = create_row("100-200,300-400,500-600", "\\d+", 1)
val row9 = create_row("100-200,300-400,500-600", "(\\d+)-(\\d+)", 3)
val row10 = create_row("100-200,300-400,500-600", "(\\d+).*", 2)
val row11 = create_row("100-200,300-400,500-600", "\\d+", 1)

checkExceptionInExpression[IllegalArgumentException](
expr, row8, "Regex group count is 2, but the specified group index is 3")
expr, row9, "Regex group count is 2, but the specified group index is 3")
checkExceptionInExpression[IllegalArgumentException](
expr, row9, "Regex group count is 1, but the specified group index is 2")
expr, row10, "Regex group count is 1, but the specified group index is 2")
checkExceptionInExpression[IllegalArgumentException](
expr, row10, "Regex group count is 0, but the specified group index is 1")
expr, row11, "Regex group count is 0, but the specified group index is 1")
}

test("SPLIT") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ struct<regexp_extract_all(1a 2b 14m, (\d+)?([a-z]+), 1):array<string>>


-- !query
SELECT regexp_extract_all('1a 2b 14m', '(\\d+)?([a-z]+)', 1)
SELECT regexp_extract_all('a 2b 14m', '(\\d+)?([a-z]+)', 1)
-- !query schema
struct<regexp_extract_all(1a 2b 14m, (\d+)?([a-z]+), 1):array<string>>
struct<regexp_extract_all(a 2b 14m, (\d+)?([a-z]+), 1):array<string>>
-- !query output
["","2","14"]
["","2","14"]

0 comments on commit 82a384e

Please sign in to comment.