aboutsummaryrefslogtreecommitdiff
path: root/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/RegexpExpressionsSuite.scala
diff options
context:
space:
mode:
Diffstat (limited to 'sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/RegexpExpressionsSuite.scala')
-rw-r--r--sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/RegexpExpressionsSuite.scala187
1 files changed, 75 insertions, 112 deletions
diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/RegexpExpressionsSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/RegexpExpressionsSuite.scala
index 6de2ba2131..1ce150e091 100644
--- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/RegexpExpressionsSuite.scala
+++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/RegexpExpressionsSuite.scala
@@ -27,10 +27,29 @@ import org.apache.spark.sql.types.{IntegerType, StringType}
*/
class RegexpExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
- test("LIKE literal Regular Expression") {
+ /**
+ * Check if a given expression evaluates to an expected output, in case the input is
+ * a literal and in case the input is in the form of a row.
+ * @tparam A type of input
+ * @param mkExpr the expression to test for a given input
+ * @param input value that will be used to create the expression, as literal and in the form
+ * of a row
+ * @param expected the expected output of the expression
+ * @param inputToExpression an implicit conversion from the input type to its corresponding
+ * sql expression
+ */
+ def checkLiteralRow[A](mkExpr: Expression => Expression, input: A, expected: Any)
+ (implicit inputToExpression: A => Expression): Unit = {
+ checkEvaluation(mkExpr(input), expected) // check literal input
+
+ val regex = 'a.string.at(0)
+ checkEvaluation(mkExpr(regex), expected, create_row(input)) // check row input
+ }
+
+ test("LIKE Pattern") {
// null handling
- checkEvaluation(Literal.create(null, StringType).like("a"), null)
+ checkLiteralRow(Literal.create(null, StringType).like(_), "a", null)
checkEvaluation(Literal.create("a", StringType).like(Literal.create(null, StringType)), null)
checkEvaluation(Literal.create(null, StringType).like(Literal.create(null, StringType)), null)
checkEvaluation(
@@ -43,109 +62,63 @@ class RegexpExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
Literal.create(null, StringType).like(NonFoldableLiteral.create(null, StringType)), null)
// simple patterns
- checkEvaluation("abdef" like "abdef", true)
- checkEvaluation("a_%b" like "a\\__b", true)
- checkEvaluation("addb" like "a_%b", true)
- checkEvaluation("addb" like "a\\__b", false)
- checkEvaluation("addb" like "a%\\%b", false)
- checkEvaluation("a_%b" like "a%\\%b", true)
- checkEvaluation("addb" like "a%", true)
- checkEvaluation("addb" like "**", false)
- checkEvaluation("abc" like "a%", true)
- checkEvaluation("abc" like "b%", false)
- checkEvaluation("abc" like "bc%", false)
- checkEvaluation("a\nb" like "a_b", true)
- checkEvaluation("ab" like "a%b", true)
- checkEvaluation("a\nb" like "a%b", true)
+ checkLiteralRow("abdef" like _, "abdef", true)
+ checkLiteralRow("a_%b" like _, "a\\__b", true)
+ checkLiteralRow("addb" like _, "a_%b", true)
+ checkLiteralRow("addb" like _, "a\\__b", false)
+ checkLiteralRow("addb" like _, "a%\\%b", false)
+ checkLiteralRow("a_%b" like _, "a%\\%b", true)
+ checkLiteralRow("addb" like _, "a%", true)
+ checkLiteralRow("addb" like _, "**", false)
+ checkLiteralRow("abc" like _, "a%", true)
+ checkLiteralRow("abc" like _, "b%", false)
+ checkLiteralRow("abc" like _, "bc%", false)
+ checkLiteralRow("a\nb" like _, "a_b", true)
+ checkLiteralRow("ab" like _, "a%b", true)
+ checkLiteralRow("a\nb" like _, "a%b", true)
// empty input
- checkEvaluation("" like "", true)
- checkEvaluation("a" like "", false)
- checkEvaluation("" like "a", false)
+ checkLiteralRow("" like _, "", true)
+ checkLiteralRow("a" like _, "", false)
+ checkLiteralRow("" like _, "a", false)
// SI-17647 double-escaping backslash
- checkEvaluation("""\\\\""" like """%\\%""", true) // triple quotes to avoid java string escaping
- checkEvaluation("""%%""" like """%%""", true)
- checkEvaluation("""\__""" like """\\\__""", true)
- checkEvaluation("""\\\__""" like """%\\%\%""", false)
- checkEvaluation("""_\\\%""" like """%\\""", false)
+ checkLiteralRow("""\\\\""" like _, """%\\%""", true)
+ checkLiteralRow("""%%""" like _, """%%""", true)
+ checkLiteralRow("""\__""" like _, """\\\__""", true)
+ checkLiteralRow("""\\\__""" like _, """%\\%\%""", false)
+ checkLiteralRow("""_\\\%""" like _, """%\\""", false)
// unicode
// scalastyle:off nonascii
- checkEvaluation("a\u20ACa" like "_\u20AC_", true)
- checkEvaluation("a€a" like "_€_", true)
- checkEvaluation("a€a" like "_\u20AC_", true)
- checkEvaluation("a\u20ACa" like "_€_", true)
+ checkLiteralRow("a\u20ACa" like _, "_\u20AC_", true)
+ checkLiteralRow("a€a" like _, "_€_", true)
+ checkLiteralRow("a€a" like _, "_\u20AC_", true)
+ checkLiteralRow("a\u20ACa" like _, "_€_", true)
// scalastyle:on nonascii
// invalid escaping
- intercept[AnalysisException] {
+ val invalidEscape = intercept[AnalysisException] {
evaluate("""a""" like """\a""")
}
- intercept[AnalysisException] {
+ assert(invalidEscape.getMessage.contains("pattern"))
+
+ val endEscape = intercept[AnalysisException] {
evaluate("""a""" like """a\""")
}
+ assert(endEscape.getMessage.contains("pattern"))
// case
- checkEvaluation("A" like "a%", false)
- checkEvaluation("a" like "A%", false)
- checkEvaluation("AaA" like "_a_", true)
-
- }
-
- test("LIKE Non-literal Regular Expression") {
- val regEx = 'a.string.at(0)
- checkEvaluation("abcd" like regEx, null, create_row(null))
- checkEvaluation("abdef" like regEx, true, create_row("abdef"))
- checkEvaluation("a_%b" like regEx, true, create_row("a\\__b"))
- checkEvaluation("addb" like regEx, true, create_row("a_%b"))
- checkEvaluation("addb" like regEx, false, create_row("a\\__b"))
- checkEvaluation("addb" like regEx, false, create_row("a%\\%b"))
- checkEvaluation("a_%b" like regEx, true, create_row("a%\\%b"))
- checkEvaluation("addb" like regEx, true, create_row("a%"))
- checkEvaluation("addb" like regEx, false, create_row("**"))
- checkEvaluation("abc" like regEx, true, create_row("a%"))
- checkEvaluation("abc" like regEx, false, create_row("b%"))
- checkEvaluation("abc" like regEx, false, create_row("bc%"))
- checkEvaluation("a\nb" like regEx, true, create_row("a_b"))
- checkEvaluation("ab" like regEx, true, create_row("a%b"))
- checkEvaluation("a\nb" like regEx, true, create_row("a%b"))
-
- checkEvaluation(Literal.create(null, StringType) like regEx, null, create_row("bc%"))
-
- checkEvaluation("" like regEx, true, create_row(""))
- checkEvaluation("a" like regEx, false, create_row(""))
- checkEvaluation("" like regEx, false, create_row("a"))
-
- checkEvaluation("""\\\\""" like regEx, true, create_row("""%\\%"""))
- checkEvaluation("""%%""" like regEx, true, create_row("""%%"""))
- checkEvaluation("""\__""" like regEx, true, create_row("""\\\__"""))
- checkEvaluation("""\\\__""" like regEx, false, create_row("""%\\%\%"""))
- checkEvaluation("""_\\\%""" like regEx, false, create_row("""%\\"""))
-
- // scalastyle:off nonascii
- checkEvaluation("a\u20ACa" like regEx, true, create_row("_\u20AC_"))
- checkEvaluation("a€a" like regEx, true, create_row("_€_"))
- checkEvaluation("a€a" like regEx, true, create_row("_\u20AC_"))
- checkEvaluation("a\u20ACa" like regEx, true, create_row("_€_"))
- // scalastyle:on nonascii
-
- // invalid escaping
- intercept[AnalysisException] {
- evaluate("""a""" like regEx, create_row("""\a"""))
- }
- intercept[AnalysisException] {
- evaluate("""a""" like regEx, create_row("""a\"""))
- }
-
- checkEvaluation("A" like regEx, false, create_row("a%"))
- checkEvaluation("a" like regEx, false, create_row("A%"))
- checkEvaluation("AaA" like regEx, true, create_row("_a_"))
+ checkLiteralRow("A" like _, "a%", false)
+ checkLiteralRow("a" like _, "A%", false)
+ checkLiteralRow("AaA" like _, "_a_", true)
+ // example
+ checkLiteralRow("""%SystemDrive%\Users\John""" like _, """\%SystemDrive\%\\Users%""", true)
}
- test("RLIKE literal Regular Expression") {
- checkEvaluation(Literal.create(null, StringType) rlike "abdef", null)
+ test("RLIKE Regular Expression") {
+ checkLiteralRow(Literal.create(null, StringType) rlike _, "abdef", null)
checkEvaluation("abdef" rlike Literal.create(null, StringType), null)
checkEvaluation(Literal.create(null, StringType) rlike Literal.create(null, StringType), null)
checkEvaluation("abdef" rlike NonFoldableLiteral.create("abdef", StringType), true)
@@ -155,42 +128,32 @@ class RegexpExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
checkEvaluation(
Literal.create(null, StringType) rlike NonFoldableLiteral.create(null, StringType), null)
- checkEvaluation("abdef" rlike "abdef", true)
- checkEvaluation("abbbbc" rlike "a.*c", true)
+ checkLiteralRow("abdef" rlike _, "abdef", true)
+ checkLiteralRow("abbbbc" rlike _, "a.*c", true)
- checkEvaluation("fofo" rlike "^fo", true)
- checkEvaluation("fo\no" rlike "^fo\no$", true)
- checkEvaluation("Bn" rlike "^Ba*n", true)
- checkEvaluation("afofo" rlike "fo", true)
- checkEvaluation("afofo" rlike "^fo", false)
- checkEvaluation("Baan" rlike "^Ba?n", false)
- checkEvaluation("axe" rlike "pi|apa", false)
- checkEvaluation("pip" rlike "^(pi)*$", false)
+ checkLiteralRow("fofo" rlike _, "^fo", true)
+ checkLiteralRow("fo\no" rlike _, "^fo\no$", true)
+ checkLiteralRow("Bn" rlike _, "^Ba*n", true)
+ checkLiteralRow("afofo" rlike _, "fo", true)
+ checkLiteralRow("afofo" rlike _, "^fo", false)
+ checkLiteralRow("Baan" rlike _, "^Ba?n", false)
+ checkLiteralRow("axe" rlike _, "pi|apa", false)
+ checkLiteralRow("pip" rlike _, "^(pi)*$", false)
- checkEvaluation("abc" rlike "^ab", true)
- checkEvaluation("abc" rlike "^bc", false)
- checkEvaluation("abc" rlike "^ab", true)
- checkEvaluation("abc" rlike "^bc", false)
+ checkLiteralRow("abc" rlike _, "^ab", true)
+ checkLiteralRow("abc" rlike _, "^bc", false)
+ checkLiteralRow("abc" rlike _, "^ab", true)
+ checkLiteralRow("abc" rlike _, "^bc", false)
intercept[java.util.regex.PatternSyntaxException] {
evaluate("abbbbc" rlike "**")
}
- }
-
- test("RLIKE Non-literal Regular Expression") {
- val regEx = 'a.string.at(0)
- checkEvaluation("abdef" rlike regEx, true, create_row("abdef"))
- checkEvaluation("abbbbc" rlike regEx, true, create_row("a.*c"))
- checkEvaluation("fofo" rlike regEx, true, create_row("^fo"))
- checkEvaluation("fo\no" rlike regEx, true, create_row("^fo\no$"))
- checkEvaluation("Bn" rlike regEx, true, create_row("^Ba*n"))
-
intercept[java.util.regex.PatternSyntaxException] {
- evaluate("abbbbc" rlike regEx, create_row("**"))
+ val regex = 'a.string.at(0)
+ evaluate("abbbbc" rlike regex, create_row("**"))
}
}
-
test("RegexReplace") {
val row1 = create_row("100-200", "(\\d+)", "num")
val row2 = create_row("100-200", "(\\d+)", "###")