Skip to content
Closed
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 @@ -341,8 +341,17 @@ public UTF8String substringSQL(int pos, int length) {
// to the -ith element before the end of the sequence. If a start index i is 0, it
// refers to the first element.
int len = numChars();
// `len + pos` does not overflow as `len >= 0`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if len = 10 and pos = Integer.MIN_VALUE. I guess that start would have an incorrect value.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The negative pos here refers to the -ith element before the end of the sequence, so if pos = Integer.MIN_VALUE, then the start should be pos + len. The final result of EMPTY_UTF8 will be returned by substring when its param start and until are both negative. I also added a UT in 4dcfe81.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made misunderstaning. Thank you for clarification and adding a test.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for reviewing!

int start = (pos > 0) ? pos -1 : ((pos < 0) ? len + pos : 0);
int end = (length == Integer.MAX_VALUE) ? len : start + length;

int end;
if ((long) start + length > Integer.MAX_VALUE) {
end = Integer.MAX_VALUE;
} else if ((long) start + length < Integer.MIN_VALUE) {
end = Integer.MIN_VALUE;
} else {
end = start + length;
}
return substring(start, end);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,10 @@ public void substringSQL() {
assertEquals(fromString("example"), e.substringSQL(0, Integer.MAX_VALUE));
assertEquals(fromString("example"), e.substringSQL(1, Integer.MAX_VALUE));
assertEquals(fromString("xample"), e.substringSQL(2, Integer.MAX_VALUE));
assertEquals(EMPTY_UTF8, e.substringSQL(-100, -100));
assertEquals(EMPTY_UTF8, e.substringSQL(-1207959552, -1207959552));
assertEquals(fromString("pl"), e.substringSQL(-3, 2));
assertEquals(EMPTY_UTF8, e.substringSQL(Integer.MIN_VALUE, 6));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@ class StringExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
"xample",
row)

// Substring with from negative position with negative length
checkEvaluation(Substring(s, Literal.create(-1207959552, IntegerType),
Literal.create(-1207959552, IntegerType)), "", row)

val s_notNull = 'a.string.notNull.at(0)

assert(Substring(s, Literal.create(0, IntegerType), Literal.create(2, IntegerType)).nullable)
Expand Down