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

Enables $, \z, and \Z in REGEXP_REPLACE on the GPU #5491

Merged

Conversation

NVnavkumar
Copy link
Collaborator

Fixes #4425.

This enables the end-of-line anchor $, end-of-input anchor \z, and end-of-input-before-line-termination anchor \Z to be used in regular expressions in REGEXP_REPLACE(...) on the GPU. This does this by doing minor manipulation on the replacement string in addition to regular expression transpiling. The idea is to potentially match a line termination in a capture group and use that as an available back-reference in the replacement string to preserve it in the output to match how Spark does this on the CPU.

…ng replacement if necessary for regexp_replace

Signed-off-by: Navin Kumar <navink@nvidia.com>
Signed-off-by: Navin Kumar <navink@nvidia.com>
Signed-off-by: Navin Kumar <navink@nvidia.com>
Signed-off-by: Navin Kumar <navink@nvidia.com>
…roups

Signed-off-by: Navin Kumar <navink@nvidia.com>
Signed-off-by: Navin Kumar <navink@nvidia.com>
@NVnavkumar NVnavkumar self-assigned this May 14, 2022
@NVnavkumar NVnavkumar requested a review from andygrove May 14, 2022 01:19
@sameerz sameerz added the feature request New feature or request label May 15, 2022
@sameerz sameerz added this to the May 2 - May 20 milestone May 15, 2022
@NVnavkumar
Copy link
Collaborator Author

build

containsBackref = hasBackref
replacement = Some(GpuRegExpUtils.unescapeReplaceString(convertedRep))
}
case _ =>
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: If this is really a noop, then it is cleaner to to a foreach instead of a match

repl.foreach { rep => ... }

Copy link
Contributor

Choose a reason for hiding this comment

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

fixed via code suggestion that was applied

@@ -583,6 +689,10 @@ class CudfRegexTranspiler(mode: RegexMode) {
// Java, it's treated as a single (b$ and b$$ are synonymous), so we create
// an empty RegexAST that outputs to empty string
RegexEmpty()
case Some(RegexChar(ch)) if mode == RegexReplaceMode
&& lineTerminatorChars.contains(ch) =>
throw new RegexUnsupportedException("Regex sequences with a line terminator "
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we add a fallback test to verify that this is working as expected? Does this conflict with the test just below it? What happens if we have something like ABC$A\n is this going to do the right thing?

Copy link
Contributor

Choose a reason for hiding this comment

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

I will follow up on this in a separate PR today

Copy link
Contributor

Choose a reason for hiding this comment

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

I added a test for this in the follow-up PR #5519

This case statement is guarded on the condition mode == RegexReplaceMode and the one below it is not so there is no conflict between the two. The ch being tested here is prior to the $ so would not match the ABC$A\n case.

…ing anchors regex replace integration test

Signed-off-by: Navin Kumar <navink@nvidia.com>
revans2
revans2 previously approved these changes May 16, 2022
Copy link
Collaborator

@revans2 revans2 left a comment

Choose a reason for hiding this comment

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

Still have a few nits that would be nice to have fixed, but they are not blockers.

@NVnavkumar
Copy link
Collaborator Author

build

Signed-off-by: Navin Kumar <navink@nvidia.com>
@NVnavkumar
Copy link
Collaborator Author

build

Comment on lines 51 to 59
repl match {
case Some(rep) =>
GpuRegExpUtils.backrefConversion(rep) match {
case (hasBackref, convertedRep) =>
containsBackref = hasBackref
replacement = Some(GpuRegExpUtils.unescapeReplaceString(convertedRep))
}
case _ =>
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
repl match {
case Some(rep) =>
GpuRegExpUtils.backrefConversion(rep) match {
case (hasBackref, convertedRep) =>
containsBackref = hasBackref
replacement = Some(GpuRegExpUtils.unescapeReplaceString(convertedRep))
}
case _ =>
}
repl.map(GpuRegExpUtils.backrefConversion).foreach {
case (hasBackref, convertedRep) =>
containsBackref = hasBackref
replacement = Some(GpuRegExpUtils.unescapeReplaceString(convertedRep))
}

@@ -853,7 +853,7 @@ class GpuRLikeMeta(
case Literal(str: UTF8String, DataTypes.StringType) if str != null =>
try {
// verify that we support this regex and can transpile it to cuDF format
pattern = Some(new CudfRegexTranspiler(RegexFindMode).transpile(str.toString))
pattern = (new CudfRegexTranspiler(RegexFindMode).transpile(str.toString, None))._1
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: it isn't obvious when reading this code what _1 represents - I will follow up with a separate PR to make this clearer

@andygrove
Copy link
Contributor

build

Copy link
Contributor

@andygrove andygrove left a comment

Choose a reason for hiding this comment

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

LGTM overall. I will follow up with a PR to fix outstanding feedback from this PR.

/**
* Parse Java regular expression and translate into cuDF regular expression.
*
* @param pattern Regular expression that is valid in Java's engine
* @return Regular expression in cuDF format
*/
def transpile(pattern: String): String = {
def transpile(pattern: String, repl: Option[String]): (Option[String], Option[String]) = {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we need the first item in the tuple to be optional since we always return a value

@andygrove andygrove merged commit a1fb914 into NVIDIA:branch-22.06 May 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FEA] Support line anchor $ and string anchors \z and \Z in regexp_replace
4 participants