-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add replace SparkSQL function (#4922)
Summary: The `replace` function behavior is different in Spark and Presto. `replace(str, replaced, replacement)`, when `replaced` is empty: - Presto inserts `replacement` before and after each character of `str` - Spark returns the original `str` string ```sql replace("aa", "", "x") -- "xaxax" (Presto) replace("aa", "", "x") -- "aa" (Spark) ``` > [SparkSQL replace:](https://github.com/apache/spark/blob/39d43e0ac3b58fb7e804362bb07665e8d6536250/common/unsafe/src/main/java/org/apache/spark/unsafe/types/UTF8String.java#L1058) > > ```scala > public UTF8String replace(UTF8String search, UTF8String replace) { > // This implementation is loosely based on commons-lang3's StringUtils.replace(). > if (numBytes == 0 || search.numBytes == 0) { > return this; > } > // Find the first occurrence of the search string. > int start = 0; > int end = this.find(search, start); > if (end == -1) { > // Search string was not found, so string is unchanged. > return this; > } > // At least one match was found. Estimate space needed for result. > // The 16x multiplier here is chosen to match commons-lang3's implementation. > int increase = Math.max(0, replace.numBytes - search.numBytes) * 16; > final UTF8StringBuilder buf = new UTF8StringBuilder(numBytes + increase); > while (end != -1) { > buf.appendBytes(this.base, this.offset + start, end - start); > buf.append(replace); > start = end + search.numBytes; > end = this.find(search, start); > } > buf.appendBytes(this.base, this.offset + start, numBytes - start); > return buf.build(); > } > ``` Pull Request resolved: #4922 Reviewed By: xiaoxmeng Differential Revision: D50044543 Pulled By: kgpai fbshipit-source-id: abee68e5ea7529be60b4f7a3eb5c1f2feb5521ff
- Loading branch information
1 parent
3ac35c9
commit 005e545
Showing
5 changed files
with
116 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters