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

Handle escaping the dangling right ] and right } in the regexp transpiler #9239

Merged
merged 3 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
24 changes: 24 additions & 0 deletions integration_tests/src/main/python/regexp_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,25 @@ def test_split_re_no_limit():
'split(a, "^[o]")'),
conf=_regexp_conf)

def test_split_with_dangling_brackets():
data_gen = mk_str_gen('([bf]o{0,2}[.?+\\^$|{}]{1,2}){1,7}') \
.with_special_case('boo.and.foo') \
.with_special_case('boo?and?foo') \
.with_special_case('boo+and+foo') \
.with_special_case('boo^and^foo') \
.with_special_case('boo$and$foo') \
.with_special_case('boo|and|foo') \
.with_special_case('boo{and}foo') \
.with_special_case('boo$|and$|foo')
assert_gpu_and_cpu_are_equal_collect(
lambda spark : unary_op_df(spark, data_gen).selectExpr(
'split(a, "[a-z]]")',
'split(a, "[boo]]]")',
'split(a, "[foo]}")',
'split(a, "[foo]}}")'),
conf=_regexp_conf)


def test_split_optimized_no_re():
data_gen = mk_str_gen('([bf]o{0,2}[.?+\\^$|{}]{1,2}){1,7}') \
.with_special_case('boo.and.foo') \
Expand All @@ -134,6 +153,11 @@ def test_split_optimized_no_re():
.with_special_case('boo$|and$|foo')
assert_gpu_and_cpu_are_equal_collect(
lambda spark : unary_op_df(spark, data_gen).selectExpr(
'split(a, "]")',
'split(a, "]]")',
'split(a, "}")',
'split(a, "}}")',
'split(a, ",")',
'split(a, "\\\\.")',
'split(a, "\\\\?")',
'split(a, "\\\\+")',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ class RegexParser(pattern: String) {
parseGroup()
case '[' =>
parseCharacterClass()
case ']' =>
RegexEscaped(']')
case '}' =>
RegexEscaped('}')
case '\\' =>
parseEscapedCharacter()
case '\u0000' =>
Expand Down Expand Up @@ -1857,7 +1861,7 @@ sealed case class RegexChar(ch: Char) extends RegexCharacterClassComponent {
override def toRegexString: String = ch.toString
}

sealed case class RegexEscaped(a: Char) extends RegexCharacterClassComponent{
sealed case class RegexEscaped(a: Char) extends RegexCharacterClassComponent {
def this(a: Char, position: Int) {
this(a)
this.position = Some(position)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class RegularExpressionParserSuite extends AnyFunSuite {
test("not a quantifier") {
assert(parse("{1}") ===
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay so because there is nothing in front of the {1} that we do not parse it as a repetition.

RegexSequence(ListBuffer(
RegexChar('{'), RegexChar('1'),RegexChar('}'))))
RegexChar('{'), RegexChar('1'),RegexEscaped('}'))))
}

test("nested repetition") {
Expand Down Expand Up @@ -109,7 +109,7 @@ class RegularExpressionParserSuite extends AnyFunSuite {
assert(parse("[a]]") ===
RegexSequence(ListBuffer(
RegexCharacterClass(negated = false,
ListBuffer(RegexChar('a'))), RegexChar(']'))))
ListBuffer(RegexChar('a'))), RegexEscaped(']'))))
}

test("escaped brackets") {
Expand Down