-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-27839][SQL] Change UTF8String.replace() to operate on UTF8 bytes #24707
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
Changes from 2 commits
f3082d3
b06d917
b51035b
6fd7714
ec423f1
5c74048
8123e42
6188dcd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ | |
| import java.util.Arrays; | ||
| import java.util.Map; | ||
|
|
||
| import org.apache.commons.lang3.StringUtils; | ||
| import com.esotericsoftware.kryo.Kryo; | ||
| import com.esotericsoftware.kryo.KryoSerializable; | ||
| import com.esotericsoftware.kryo.io.Input; | ||
|
|
@@ -976,9 +977,28 @@ public UTF8String replace(UTF8String search, UTF8String replace) { | |
| if (EMPTY_UTF8.equals(search)) { | ||
| return this; | ||
| } | ||
| String replaced = toString().replace( | ||
| search.toString(), replace.toString()); | ||
| return fromString(replaced); | ||
| return replace(search.toString(), replace.toString()); | ||
| } | ||
|
|
||
| public UTF8String replace(String search, String replace) { | ||
| String before = toString(); | ||
| String after; | ||
| if (search.length() == 1 && replace.length() == 1) { | ||
| // Use single-character-replacement fast path | ||
| after = before.replace(search.charAt(0), replace.charAt(0)); | ||
| } else { | ||
| // In Java 8, String.replace() is implemented using a regex and is therefore | ||
| // somewhat inefficient (see https://bugs.openjdk.java.net/browse/JDK-8058779). | ||
| // This is fixed in Java 9, but in Java 8 we can use Commons StringUtils instead: | ||
| after = StringUtils.replace(before, search, replace); | ||
| } | ||
| // Use reference equality to cheaply detect whether the replacement had no effect, | ||
| // in which case we can simply return the original UTF8String and save some copying. | ||
| if (before == after) { | ||
| return this; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One consideration here: do we need to make a defensive copy? If so, we can't do this optimization. Why might we need to copy a I think the most common case to worry about would be a UTF8String which is backed by memory that is part of a larger My intuition is that we probably don't need to make a defensive copy here because I doubt we have parts of the code which specifically assume that I'd love to get additional sets of eyes on this, though, and I'd ultimately be ok with changing
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think your intuition is right here. |
||
| } else { | ||
| return fromString(after); | ||
| } | ||
| } | ||
|
|
||
| // TODO: Need to use `Code Point` here instead of Char in case the character longer than 2 bytes | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.