maropu commented on a change in pull request #25001: [SPARK-28083][SQL] Support LIKE ... ESCAPE syntax URL: https://github.com/apache/spark/pull/25001#discussion_r308052836
########## File path: sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/RegexpExpressionsSuite.scala ########## @@ -118,6 +118,295 @@ class RegexpExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper { checkLiteralRow("""%SystemDrive%\Users\John""" like _, """\%SystemDrive\%\\Users%""", true) } + test("LIKE Pattern ESCAPE '/'") { + + // null handling + 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( + Literal.create("a", StringType).like(NonFoldableLiteral.create("a", StringType), "/"), true) + checkEvaluation( + Literal.create("a", StringType).like(NonFoldableLiteral.create(null, StringType), "/"), null) + checkEvaluation( + Literal.create(null, StringType).like(NonFoldableLiteral.create("a", StringType), "/"), null) + checkEvaluation( + Literal.create(null, StringType).like(NonFoldableLiteral.create(null, StringType), "/"), null) + + // simple patterns + 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 + checkLiteralRow("" like(_, "/"), "", true) + checkLiteralRow("a" like(_, "/"), "", false) + checkLiteralRow("" like(_, "/"), "a", false) + + // SI-17647 double-escaping backslash + checkLiteralRow("""////""" like(_, "/"), """%//%""", true) + checkLiteralRow("""%%""" like(_, "/"), """%%""", true) + checkLiteralRow("""/__""" like(_, "/"), """///__""", true) + checkLiteralRow("""///__""" like(_, "/"), """%//%/%""", false) + checkLiteralRow("""_///%""" like(_, "/"), """%//""", false) + + // unicode + // scalastyle:off nonascii + 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 + val invalidEscape = intercept[AnalysisException] { + evaluateWithoutCodegen("""a""" like("""/a""", "/")) + } + assert(invalidEscape.getMessage.contains("pattern")) + + val endEscape = intercept[AnalysisException] { + evaluateWithoutCodegen("""a""" like("""a/""", "/")) + } + assert(endEscape.getMessage.contains("pattern")) + + // case + 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("LIKE Pattern ESCAPE '#'") { + + // null handling + 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( + Literal.create("a", StringType).like(NonFoldableLiteral.create("a", StringType), "#"), true) + checkEvaluation( + Literal.create("a", StringType).like(NonFoldableLiteral.create(null, StringType), "#"), null) + checkEvaluation( + Literal.create(null, StringType).like(NonFoldableLiteral.create("a", StringType), "#"), null) + checkEvaluation( + Literal.create(null, StringType).like(NonFoldableLiteral.create(null, StringType), "#"), null) + + // simple patterns + 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 + checkLiteralRow("" like(_, "#"), "", true) + checkLiteralRow("a" like(_, "#"), "", false) + checkLiteralRow("" like(_, "#"), "a", false) + + // SI-17647 double-escaping backslash + checkLiteralRow("""####""" like(_, "#"), """%##%""", true) + checkLiteralRow("""%%""" like(_, "#"), """%%""", true) + checkLiteralRow("""#__""" like(_, "#"), """###__""", true) + checkLiteralRow("""###__""" like(_, "#"), """%##%#%""", false) + checkLiteralRow("""_###%""" like(_, "#"), """%##""", false) + + // unicode + // scalastyle:off nonascii + 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 + val invalidEscape = intercept[AnalysisException] { + evaluateWithoutCodegen("""a""" like("""#a""", "#")) + } + assert(invalidEscape.getMessage.contains("pattern")) + + val endEscape = intercept[AnalysisException] { + evaluateWithoutCodegen("""a""" like("""a#""", "#")) + } + assert(endEscape.getMessage.contains("pattern")) + + // case + 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("LIKE Pattern ESCAPE '\"'") { + Review comment: ditto ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org