Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -825,8 +825,8 @@ private static int lowercaseIndexOfSlow(final UTF8String target, final UTF8Strin
public static int lowercaseIndexOf(final UTF8String target, final UTF8String pattern,
final int start, final int occurrence) {
assert occurrence > 0;
if (pattern.numBytes() == 0) return target.indexOfEmpty(start);
if (start == 0) return MATCH_NOT_FOUND;
if (pattern.numBytes() == 0) return target.indexOfEmpty(start);
if (target.isFullAscii() && pattern.isFullAscii()) {
return target.toLowerCase().indexOf(pattern.toLowerCase(), start, occurrence);
}
Expand Down Expand Up @@ -928,8 +928,9 @@ public static int indexOf(final UTF8String target, final UTF8String pattern,
public static int indexOf(final UTF8String target, final UTF8String pattern,
final int start, final int occurrence, final int collationId) {
assert occurrence > 0;
if (start == 0) return MATCH_NOT_FOUND;
if (pattern.numBytes() == 0) return target.indexOfEmpty(start);
if (target.numBytes() == 0 || start == 0) return MATCH_NOT_FOUND;
if (target.numBytes() == 0) return MATCH_NOT_FOUND;

String targetStr = target.toValidString();
String patternStr = pattern.toValidString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1319,12 +1319,12 @@ public int indexOf(UTF8String v, int start) {
*/
public int indexOf(UTF8String pattern, int start, int occurrence) {
assert occurrence > 0;
if (pattern.numBytes() == 0) {
return indexOfEmpty(start);
}
if (start == 0) {
return -1;
}
if (pattern.numBytes() == 0) {
return indexOfEmpty(start);
}

int charCount = 0;
int byteIdx;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4047,6 +4047,19 @@ private void assertStringInstrWithOccurrence(String string, String substring, in
assertEquals(expected, res);
}

@Test
public void testStringInstrWithOccurrenceZeroStart() throws SparkException {
for (String collationName : testSupportedCollations) {
for (String string : new String[]{"", "abc", "\u4F60\u597D"}) {
for (String substring : new String[]{"", "a"}) {
for (int occurrence : new int[]{1, 2}) {
assertStringInstrWithOccurrence(string, substring, 0, occurrence, collationName, 0);
}
}
}
}
}

@Test
public void testStringInstrWithOccurrence() throws SparkException {
// Test start = 1 and occurrence = 1 (equivalent to StringInstr)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,10 @@ public void indexOf() {
assertEquals(0, fromString("hello").indexOf(EMPTY_UTF8, 1, 1));
assertEquals(0, fromString("hello").indexOf(EMPTY_UTF8, 5, 1));
assertEquals(0, fromString("hello").indexOf(EMPTY_UTF8, -1, 1));
assertEquals(-1, fromString("hello").indexOf(EMPTY_UTF8, 0, 1));
assertEquals(-1, fromString("hello").indexOf(EMPTY_UTF8, 0, 2));
assertEquals(-1, EMPTY_UTF8.indexOf(EMPTY_UTF8, 0, 1));
assertEquals(-1, EMPTY_UTF8.indexOf(EMPTY_UTF8, 0, 2));

// Boundary cases
assertEquals(0, fromString("x").indexOf(fromString("x"), 1, 1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1348,6 +1348,24 @@ class StringExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
// scalastyle:on
}

test("INSTR with start = 0 returns 0 for empty substrings") {
for (collation <- Seq("UTF8_BINARY", "UTF8_LCASE", "UNICODE", "UNICODE_CI")) {
val stringType = StringType(collation)
val expression = StringInstrWithOccurrence(
BoundReference(0, stringType, nullable = false),
BoundReference(1, stringType, nullable = false),
BoundReference(2, IntegerType, nullable = false),
BoundReference(3, IntegerType, nullable = false))
for {
string <- Seq("", "abc", "\u4F60\u597D")
substring <- Seq("", "a")
occurrence <- Seq(1, 2)
} {
checkEvaluation(expression, 0, create_row(string, substring, 0, occurrence))
}
}
}

test("StringInstrExpressionBuilder") {
val seq1 = Seq(Literal("abcabc"), Literal("a"), Literal(-1), Literal(2))
val seq2 = Seq(Literal("abcabc"), Literal("a"), Literal(-1))
Expand Down