-
Notifications
You must be signed in to change notification settings - Fork 28.1k
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
[SPARK-8264][SQL]add substring_index function #7533
Changes from 3 commits
52d7b03
d92951b
12e108f
ac863e9
b19b013
67c253a
9546991
515519b
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 |
---|---|---|
|
@@ -21,6 +21,8 @@ import java.text.DecimalFormat | |
import java.util.Locale | ||
import java.util.regex.{MatchResult, Pattern} | ||
|
||
import org.apache.commons.lang.StringUtils | ||
|
||
import org.apache.spark.sql.catalyst.InternalRow | ||
import org.apache.spark.sql.catalyst.analysis.UnresolvedException | ||
import org.apache.spark.sql.catalyst.expressions.codegen._ | ||
|
@@ -355,6 +357,92 @@ case class StringInstr(str: Expression, substr: Expression) | |
} | ||
} | ||
|
||
/** | ||
* Returns the substring from string str before count occurrences of the delimiter delim. | ||
* If count is positive, everything the left of the final delimiter (counting from left) is | ||
* returned. If count is negative, every to the right of the final delimiter (counting from the | ||
* right) is returned. substring_index performs a case-sensitive match when searching for delim. | ||
*/ | ||
case class Substring_index(strExpr: Expression, delimExpr: Expression, countExpr: Expression) | ||
extends Expression with ImplicitCastInputTypes with CodegenFallback { | ||
|
||
override def dataType: DataType = StringType | ||
override def inputTypes: Seq[DataType] = Seq(StringType, StringType, IntegerType) | ||
override def nullable: Boolean = strExpr.nullable || delimExpr.nullable || countExpr.nullable | ||
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. Sorry, actually I mean we need to override the function |
||
override def children: Seq[Expression] = Seq(strExpr, delimExpr, countExpr) | ||
override def prettyName: String = "substring_index" | ||
override def toString: String = s"substring_index($strExpr, $delimExpr, $countExpr)" | ||
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. We can remove this line, overriding the |
||
|
||
override def eval(input: InternalRow): Any = { | ||
val str = strExpr.eval(input) | ||
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. let's do that like: val str = ...
if (str != null) {
val delim = ...
if (delim != null) {
val count = ... That's kind of short circuit evaluation. |
||
val delim = delimExpr.eval(input) | ||
val count = countExpr.eval(input) | ||
if (str == null || delim == null || count == null) { | ||
null | ||
} else { | ||
subStrIndex( | ||
str.asInstanceOf[UTF8String], | ||
delim.asInstanceOf[UTF8String], | ||
count.asInstanceOf[Int]) | ||
} | ||
} | ||
|
||
private def lastOrdinalIndexOf( | ||
str: UTF8String, searchStr: UTF8String, ordinal: Int, lastIndex: Boolean = false): Int = { | ||
ordinalIndexOf(str, searchStr, ordinal, true) | ||
} | ||
|
||
private def ordinalIndexOf( | ||
str: UTF8String, searchStr: UTF8String, ordinal: Int, lastIndex: Boolean = false): Int = { | ||
if (str == null || searchStr == null || ordinal <= 0) { | ||
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. Can we remove the null checking here? |
||
return -1 | ||
} | ||
val strNumChars = str.numChars() | ||
if (searchStr.numBytes() == 0) { | ||
return if (lastIndex) {strNumChars} else {0} | ||
} | ||
var found = 0 | ||
var index = if (lastIndex) {strNumChars} else {0} | ||
do { | ||
if (lastIndex) { | ||
index = str.lastIndexOf(searchStr, index - 1) | ||
} else { | ||
index = str.indexOf(searchStr, index + 1) | ||
} | ||
if (index < 0) { | ||
return index | ||
} | ||
found += 1 | ||
} while (found < ordinal) | ||
index | ||
} | ||
|
||
private def subStrIndex(strUtf8: UTF8String, delimUtf8: UTF8String, count: Int): UTF8String = { | ||
if (strUtf8 == null || delimUtf8 == null || count == null) { | ||
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. Can we remove the null checking here? |
||
return null | ||
} | ||
if (strUtf8.numBytes() == 0 || delimUtf8.numBytes() == 0 || count == 0) { | ||
return UTF8String.fromString("") | ||
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.
|
||
} | ||
val res = if (count > 0) { | ||
val idx = ordinalIndexOf(strUtf8, delimUtf8, count) | ||
if (idx != -1) { | ||
strUtf8.substring(0, idx) | ||
} else { | ||
strUtf8 | ||
} | ||
} else { | ||
val idx = lastOrdinalIndexOf(strUtf8, delimUtf8, -count) | ||
if (idx != -1) { | ||
strUtf8.substring(idx + delimUtf8.numChars(), strUtf8.numChars()) | ||
} else { | ||
strUtf8 | ||
} | ||
} | ||
res | ||
} | ||
} | ||
|
||
/** | ||
* A function that returns the position of the first occurrence of substr | ||
* in given string after position pos. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1777,8 +1777,31 @@ object functions { | |
def instr(str: Column, substring: String): Column = StringInstr(str.expr, lit(substring).expr) | ||
|
||
/** | ||
* Locate the position of the first occurrence of substr in a string column. | ||
* Returns the substring from string str before count occurrences of the delimiter delim. | ||
* If count is positive, everything the left of the final delimiter (counting from left) is | ||
* returned. If count is negative, every to the right of the final delimiter (counting from the | ||
* right) is returned. substring_index performs a case-sensitive match when searching for delim. | ||
* | ||
* @group string_funcs | ||
* @since 1.5.0 | ||
*/ | ||
def substring_index(str: String, delim: String, count: Int): Column = | ||
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. Let's remove this version of API. @rxin actually made some clean up, and removed the |
||
substring_index(Column(str), delim, count) | ||
|
||
/** | ||
* Returns the substring from string str before count occurrences of the delimiter delim. | ||
* If count is positive, everything the left of the final delimiter (counting from left) is | ||
* returned. If count is negative, every to the right of the final delimiter (counting from the | ||
* right) is returned. substring_index performs a case-sensitive match when searching for delim. | ||
* | ||
* @group string_funcs | ||
* @since 1.5.0 | ||
*/ | ||
def substring_index(str: Column, delim: String, count: Int): Column = | ||
Substring_index(str.expr, lit(delim).expr, lit(count).expr) | ||
|
||
/** | ||
* Locate the position of the first occurrence of substr. | ||
* NOTE: The position is not zero based, but 1 based index, returns 0 if substr | ||
* could not be found in str. | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -352,6 +352,69 @@ public int indexOf(UTF8String v, int start) { | |
return -1; | ||
} | ||
|
||
private enum ByteType {FIRSTBYTE, MIDBYTE, SINGLEBYTECHAR}; | ||
|
||
private ByteType checkByteType(Byte b) { | ||
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.
|
||
int firstTwoBits = (b >>> 6) & 0x03; | ||
if (firstTwoBits == 3) { | ||
return ByteType.FIRSTBYTE; | ||
} else if (firstTwoBits == 2) { | ||
return ByteType.MIDBYTE; | ||
} else { | ||
return ByteType.SINGLEBYTECHAR; | ||
} | ||
} | ||
|
||
/** | ||
* Return the first byte position for a given byte which shared the same code point. | ||
* @param bytePos any byte within the code point | ||
* @return the first byte position of a given code point, throw exception if not a valid UTF8 str | ||
*/ | ||
private int firstOfCurrentCodePoint(int bytePos) { | ||
while (bytePos >= 0) { | ||
if (ByteType.FIRSTBYTE == checkByteType(getByte(bytePos)) | ||
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. use a tmp variable for the result of |
||
|| ByteType.SINGLEBYTECHAR == checkByteType(getByte(bytePos))) { | ||
return bytePos; | ||
} | ||
bytePos--; | ||
} | ||
throw new RuntimeException("Invalid utf8 string"); | ||
} | ||
|
||
private int indexEnd(int startCodePoint) { | ||
int i = numBytes -1; // position in byte | ||
int c = numChars() - 1; // position in character | ||
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.
|
||
while (i >=0 && c > startCodePoint) { | ||
i = firstOfCurrentCodePoint(i) - 1; | ||
c -= 1; | ||
} | ||
return i; | ||
} | ||
|
||
public int lastIndexOf(UTF8String v, int startCodePoint) { | ||
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. Should this be protected? (for tests) |
||
if (v.numBytes == 0) { | ||
return 0; | ||
} | ||
if (numBytes == 0) { | ||
return -1; | ||
} | ||
int fromIndexEnd = indexEnd(startCodePoint); | ||
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. Since |
||
int count = startCodePoint; | ||
int vNumChars = v.numChars(); | ||
do { | ||
if (fromIndexEnd - v.numBytes + 1 < 0 ) { | ||
return -1; | ||
} | ||
if (ByteArrayMethods.arrayEquals( | ||
base, offset + fromIndexEnd - v.numBytes + 1, v.base, v.offset, v.numBytes)) { | ||
return count - vNumChars + 1; | ||
} | ||
fromIndexEnd = firstOfCurrentCodePoint(fromIndexEnd) - 1; | ||
count--; | ||
} while (fromIndexEnd >= 0); | ||
return -1; | ||
} | ||
|
||
/** | ||
* Returns str, right-padded with pad to a length of len | ||
* For example: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to override the
nullable
method, as the default value isfalse
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it's abstract in Expression which Substring_index inherited from?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, my bad, actually I mean the function
foldable
, not thenullable
.