Skip to content

Commit cd9f2dd

Browse files
committed
Fix strrpos function
1 parent 8ed1609 commit cd9f2dd

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

presto-main/src/main/java/com/facebook/presto/operator/scalar/StringFunctions.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,21 +267,25 @@ private static long stringPositionFromEnd(Slice string, Slice substring, long in
267267
return 1;
268268
}
269269

270+
String stringUtf8 = string.toStringUtf8();
271+
String substringUtf8 = substring.toStringUtf8();
270272
int foundInstances = 0;
271273
// set the initial index just after the end of the string
272274
// this is to allow for the initial index decrement
273-
int index = string.length();
275+
int index = stringUtf8.length();
274276
do {
275277
// step backwards through string
276-
index = string.toStringUtf8().lastIndexOf(substring.toStringUtf8(), index - 1);
278+
index = stringUtf8.lastIndexOf(substringUtf8, index - 1);
277279
if (index < 0) {
278280
return 0;
279281
}
280282
foundInstances++;
281283
}
282284
while (foundInstances < instance);
283285

284-
return countCodePoints(string, 0, index) + 1;
286+
// stringPositionFromStart function returns countCodePoints(string, 0, index) + 1 because it directly works on Slice and use indexOf function of Slice,
287+
// Here we convert Slice to Utf8 string call lastIndexOf function of String to get index, hence directly return index+1
288+
return index + 1;
285289
}
286290

287291
@Description("suffix starting at given index")

presto-main/src/test/java/com/facebook/presto/operator/scalar/TestStringFunctions.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,9 +317,10 @@ public void testStringReversePosition()
317317
assertFunction("STRRPOS('x', '')", BIGINT, 1L);
318318
assertFunction("STRRPOS('', '')", BIGINT, 1L);
319319

320-
assertFunction("STRRPOS('\u4FE1\u5FF5,\u7231,\u5E0C\u671B', '\u7231')", BIGINT, 2L);
321-
assertFunction("STRRPOS('\u4FE1\u5FF5,\u7231,\u5E0C\u671B', '\u5E0C\u671B')", BIGINT, 3L);
320+
assertFunction("STRRPOS('\u4FE1\u5FF5,\u7231,\u5E0C\u671B', '\u7231')", BIGINT, 4L);
321+
assertFunction("STRRPOS('\u4FE1\u5FF5,\u7231,\u5E0C\u671B', '\u5E0C\u671B')", BIGINT, 6L);
322322
assertFunction("STRRPOS('\u4FE1\u5FF5,\u7231,\u5E0C\u671B', 'nice')", BIGINT, 0L);
323+
assertFunction("STRRPOS('Screenshot 2024-12-01 at 12.00.51\u202fPM.png', '.')", BIGINT, 37L);
323324

324325
assertFunction("STRRPOS(NULL, '')", BIGINT, null);
325326
assertFunction("STRRPOS('', NULL)", BIGINT, null);

0 commit comments

Comments
 (0)