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

Reimplement check for non-regexp strings using RegexParser #4681

Merged
merged 6 commits into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -492,11 +492,6 @@ object GpuOverrides extends Logging {
listeners.clear()
}

def canRegexpBeTreatedLikeARegularString(strLit: UTF8String): Boolean = {
val s = strLit.toString
!regexList.exists(pattern => s.contains(pattern))
}

private def convertPartToGpuIfPossible(part: Partitioning, conf: RapidsConf): Partitioning = {
part match {
case _: GpuPartitioning => part
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,32 @@ class RegexParser(pattern: String) {

}

object RegexParser {
private val regexpChars = Seq('\u0000', '\\', '.', '^', '$', '\f')
private val regexpEscapedChars = Seq('\\', 'b', 'B', 's', 'S', 'w', 'W', 'd', 'D', 'Q', 'E',
'h', 'H', 'v', 'V', 'A', 'a', 'z', 'Z', 'x', 'e', 'c', 'p', 'G', 'R', 'k')

def isRegExpString(s: String): Boolean = {

def isRegExpString(ast: RegexAST): Boolean = ast match {
case RegexChar(ch) => regexpChars.contains(ch)
case RegexEscaped(ch) => regexpEscapedChars.contains(ch)
case RegexSequence(parts) => parts.exists(isRegExpString)
case _ => true
}

try {
val parser = new RegexParser(s)
val ast = parser.parse()
isRegExpString(ast)
} catch {
case _: RegexUnsupportedException =>
// if we cannot parse it then assume that it might be valid regexp
true
}
}
}

/**
* Transpile Java/Spark regular expression to a format that cuDF supports, or throw an exception
* if this is not possible.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1296,7 +1296,7 @@ class GpuStringSplitMeta(
} else {
val str = regexp.get.value.asInstanceOf[UTF8String]
if (str != null) {
if (!canRegexpBeTreatedLikeARegularString(str)) {
if (RegexParser.isRegExpString(str.toString)) {
willNotWorkOnGpu("regular expressions are not supported yet")
}
if (str.numChars() == 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, NVIDIA CORPORATION.
* Copyright (c) 2021-2022, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,6 +21,24 @@ import org.scalatest.FunSuite

class RegularExpressionParserSuite extends FunSuite {

test("detect regexp strings") {
// Based on https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html
val strings: Seq[String] = Seq("\\", "\u0000", "\\x00",
"\f", "\\a", "\\e", "\\cx", "[abc]", "^", "[a-z&&[def]]", ".", "*", "\\d", "\\D",
"\\h", "\\H", "\\s", "\\S", "\\v", "\\V", "\\w", "\\w", "\\p", "$", "\\b", "\\B",
"\\A", "\\G", "\\Z", "\\z", "\\R", "?", "|", "(abc)", "a{1,}", "\\k", "\\Q", "\\E")
for (string <- strings) {
assert(RegexParser.isRegExpString(string))
}
}

test("detect non-regexp strings") {
val strings = Seq("\\.", "A", ",", "\t")
andygrove marked this conversation as resolved.
Show resolved Hide resolved
for (string <- strings) {
assert(!RegexParser.isRegExpString(string))
}
}

test("empty pattern") {
assert(parse("") === RegexSequence(ListBuffer()))
ttnghia marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down