Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make regexp pattern [^a] consistent with Spark for multiline strings #4255

Merged
merged 5 commits into from
Dec 6, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions integration_tests/src/main/python/string_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,21 @@ def test_regexp_replace_null_pattern_fallback():
'RegExpReplace',
conf={'spark.rapids.sql.expression.RegExpReplace': 'true'})

def test_regexp_replace_character_set_negated():
gen = mk_str_gen('[abcd]{0,3}[\r\n]{0,2}[abcd]{0,3}')
assert_gpu_and_cpu_are_equal_collect(
lambda spark: unary_op_df(spark, gen).selectExpr(
'regexp_replace(a, "([^a])|([^b])", "1")',
'regexp_replace(a, "[^a]", "1")',
'regexp_replace(a, "([^a]|[\r\n])", "1")',
'regexp_replace(a, "[^a\r\n]", "1")',
'regexp_replace(a, "[^a\r]", "1")',
'regexp_replace(a, "[^a\n]", "1")',
'regexp_replace(a, "[^\r\n]", "1")',
'regexp_replace(a, "[^\r]", "1")',
'regexp_replace(a, "[^\n]", "1")'),
conf={'spark.rapids.sql.expression.RegExpReplace': 'true'})

def test_rlike():
gen = mk_str_gen('[abcd]{1,3}')
assert_gpu_and_cpu_are_equal_collect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,40 @@ class CudfRegexTranspiler(replace: Boolean) {
}
val components: Seq[RegexCharacterClassComponent] = characters
.map(x => rewrite(x).asInstanceOf[RegexCharacterClassComponent])
RegexCharacterClass(negated, ListBuffer(components: _*))

if (negated) {
// There are differences between cuDF and Java handling of newlines
// for negative character matches. The expression `[^a]` will match
// `\r` and `\n` in Java but not in cuDF, so we replace `[^a]` with
// `(?:[\r\n]|[^a])`. We also have to take into account whether any
jlowe marked this conversation as resolved.
Show resolved Hide resolved
// newline characters are included in the character range.
//
// Examples:
//
// `[^a]` => `(?:[\r\n]|[^a])`
// `[^a\r]` => `(?:[\n]|[^a])`
// `[^a\n]` => `(?:[\r]|[^a])`
// `[^a\r\n]` => `[^a]`

val newlineCharsInClass = characters.flatMap {
case RegexChar(ch) if ch == '\n' || ch == '\r' =>
Seq(ch)
case _ =>
Seq.empty
}
val negatedNewlines = Seq('\r', '\n').diff(newlineCharsInClass)
if (negatedNewlines.isEmpty) {
RegexCharacterClass(negated, ListBuffer(components: _*))
} else {
RegexGroup(capture = false,
RegexChoice(
RegexCharacterClass(negated = false,
characters = ListBuffer(negatedNewlines.map(RegexChar): _*)),
RegexCharacterClass(negated, ListBuffer(components: _*))))
}
} else {
RegexCharacterClass(negated, ListBuffer(components: _*))
}

case RegexSequence(parts) =>
if (parts.isEmpty) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ class RegularExpressionTranspilerSuite extends FunSuite with Arm {

test("transpile character class unescaped range symbol") {
val patterns = Seq("a[-b]", "a[+-]", "a[-+]", "a[-]", "a[^-]")
val expected = Seq(raw"a[\-b]", raw"a[+\-]", raw"a[\-+]", raw"a[\-]", raw"a[^\-]")
val expected = Seq(raw"a[\-b]", raw"a[+\-]", raw"a[\-+]", raw"a[\-]", "a(?:[\r\n]|[^\\-])")
val transpiler = new CudfRegexTranspiler(replace=false)
val transpiled = patterns.map(transpiler.transpile)
assert(transpiled === expected)
Expand Down Expand Up @@ -248,6 +248,12 @@ class RegularExpressionTranspilerSuite extends FunSuite with Arm {
assertCpuGpuMatchesRegexpReplace(patterns, inputs)
}

test("compare CPU and GPU: regexp replace negated character class") {
val inputs = Seq("a", "b", "a\nb")
val patterns = Seq("[^z]")
assertCpuGpuMatchesRegexpReplace(patterns, inputs)
}

test("compare CPU and GPU: regexp replace fuzz test with limited chars") {
// testing with this limited set of characters finds issues much
// faster than using the full ASCII set
Expand Down