Uh oh!
There was an error while loading. Please reload this page.
[SPARK-19555][SQL] Improve the performance of StringUtils.escapeLikeRegex method - #16893
[SPARK-19555][SQL] Improve the performance of StringUtils.escapeLikeRegex method#16893lins05 wants to merge 2 commits into
Conversation
SparkQA
commented
Feb 11, 2017
Test build #72733 has started for PR 16893 at commit |
JoshRosen
commented
Feb 12, 2017
jenkins retest this please |
There was a problem hiding this comment.
Since this PR is about improving performance, would recommend trying out with while loop and a counter instead of for(). I ran your test snippet on my box:
with for():
function took 5287 ms
function took 5234 ms
function took 5149 ms
with while loop and counter:
function took 4762 ms
function took 4216 ms
function took 4212 ms
Here is my version with while loop and counter:
def escapeLikeRegex(input: String): String = {
val builder = new StringBuilder("(?s)")
val length = input.length
var i = 0
var previousChar = ' '
while (i < length) {
val currentChar = input.charAt(i)
if (currentChar != '\\') {
val out = if (previousChar == '\\') {
currentChar match {
case '_' => "_"
case '%' => "%"
case _ => Pattern.quote("\\" + currentChar)
}
} else {
currentChar match {
case '_' => "."
case '%' => ".*"
case _ => Pattern.quote(Character.toString(currentChar))
}
}
builder.append(out)
}
previousChar = currentChar
i += 1
}
builder.toString()
}
| @@ -27,21 +28,28 @@ object StringUtils { | |||
| // replace the % with .*, match 0 or more times with any character | |||
| def escapeLikeRegex(v: String): String = { | |||
There was a problem hiding this comment.
nit: while you are at it, can you also make the var names better ?
v -> inputc -> currentCharprev -> previousChar
There was a problem hiding this comment.
I think we could further simplify this by replacing previousChar with a boolean, nextCharacterIsEscaped (or inEscape).
There was a problem hiding this comment.
Actually, I'm wrong: we can't quite do that simplification because the old code has a subtle bug related to backslash-escaping. Due to the complexity and terseness the old implementation, it's a little non-obvious to spot that the case (prev, '\\') => "" has the effect of always ignoring backslash characters, so this method is incapable of producing a backslash in its output. This is a problem if the user wants to write a LIKE pattern to match backslashes then this is impossible with the current code.
It turns out that this is covered by #15398, which also implements performance improvements for this code, so I guess this PR and JIRA is redundant :(
I thought #15398 had been merged / fixed by now, but I guess not.
SparkQA
commented
Feb 12, 2017
Test build #72788 has finished for PR 16893 at commit
|
HyukjinKwon
commented
May 11, 2017
Hi all, where are we on this? Is this still active? |
JoshRosen
commented
May 17, 2017
@HyukjinKwon, we can close this because its optimizations were incorporated into SPARK-17647 (I ran the benchmarks to verify this, too). I'm going to resolve this JIRA as fixed by that one as well. |
## What changes were proposed in this pull request? This PR proposes to close PRs ... - inactive to the review comments more than a month - WIP and inactive more than a month - with Jenkins build failure but inactive more than a month - suggested to be closed and no comment against that - obviously looking inappropriate (e.g., Branch 0.5) To make sure, I left a comment for each PR about a week ago and I could not have a response back from the author in these PRs below: Closesapache#11129Closesapache#12085Closesapache#12162Closesapache#12419Closesapache#12420Closesapache#12491Closesapache#13762Closesapache#13837Closesapache#13851Closesapache#13881Closesapache#13891Closesapache#13959Closesapache#14091Closesapache#14481Closesapache#14547Closesapache#14557Closesapache#14686Closesapache#15594Closesapache#15652Closesapache#15850Closesapache#15914Closesapache#15918Closesapache#16285Closesapache#16389Closesapache#16652Closesapache#16743Closesapache#16893Closesapache#16975Closesapache#17001Closesapache#17088Closesapache#17119Closesapache#17272Closesapache#17971 Added: Closesapache#17778Closesapache#17303Closesapache#17872 ## How was this patch tested? N/A Author: hyukjinkwon <gurwls223@gmail.com> Closesapache#18017 from HyukjinKwon/close-inactive-prs.
What changes were proposed in this pull request?
Copied from SPARK-19555 JIRA
Spark's
StringUtils.escapeLikeRegex()method is written inefficiently, performing tons of object allocations due to the usezip(),flatMap(), andmkString. Instead, I think method should be rewritten in an imperative style using a Java string builder.This method can become a performance bottleneck in cases where regex expressions are used with non-constant-foldable expressions (e.g. the regex expression comes from the data rather than being part of the query).
How was this patch tested?
Existing tests.
Performance Comparison
Perf testing code:
./bin/spark-shell --master "local-cluster[2,1,10240]"