Skip to content
Merged
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 @@ -37,6 +37,7 @@
import java.nio.charset.StandardCharsets;

import static com.facebook.presto.common.type.VarcharType.VARCHAR;
import static com.facebook.presto.spi.StandardErrorCode.GENERIC_USER_ERROR;
import static com.facebook.presto.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT;
import static java.lang.Math.toIntExact;
import static java.lang.String.format;
Expand All @@ -63,7 +64,8 @@ public static boolean regexpLike(@SqlType("varchar(x)") Slice source, @SqlType(J
offset = 0;
matcher = pattern.matcher(source.getBytes());
}
return matcher.search(offset, offset + source.length(), Option.DEFAULT) != -1;

return getMatchingOffset(matcher, offset, offset + source.length()) != -1;
}

@Description("removes substrings matching a regular expression")
Expand Down Expand Up @@ -93,7 +95,7 @@ public static Slice regexpReplace(@SqlType("varchar(x)") Slice source, @SqlType(
int lastEnd = 0;
int nextStart = 0; // nextStart is the same as lastEnd, unless the last match was zero-width. In such case, nextStart is lastEnd + 1.
while (true) {
int offset = matcher.search(nextStart, source.length(), Option.DEFAULT);
int offset = getMatchingOffset(matcher, nextStart, source.length());
if (offset == -1) {
break;
}
Expand Down Expand Up @@ -215,7 +217,7 @@ public static Block regexpExtractAll(@SqlType("varchar(x)") Slice source, @SqlTy

int nextStart = 0;
while (true) {
int offset = matcher.search(nextStart, source.length(), Option.DEFAULT);
int offset = getMatchingOffset(matcher, nextStart, source.length());
if (offset == -1) {
break;
}
Expand Down Expand Up @@ -260,7 +262,7 @@ public static Slice regexpExtract(@SqlType("varchar(x)") Slice source, @SqlType(
validateGroup(groupIndex, matcher.getEagerRegion());
int group = toIntExact(groupIndex);

int offset = matcher.search(0, source.length(), Option.DEFAULT);
int offset = getMatchingOffset(matcher, 0, source.length());
if (offset == -1) {
return null;
}
Expand Down Expand Up @@ -288,7 +290,7 @@ public static Block regexpSplit(@SqlType("varchar(x)") Slice source, @SqlType(Jo
int lastEnd = 0;
int nextStart = 0;
while (true) {
int offset = matcher.search(nextStart, source.length(), Option.DEFAULT);
int offset = getMatchingOffset(matcher, nextStart, source.length());
if (offset == -1) {
break;
}
Expand All @@ -307,6 +309,16 @@ public static Block regexpSplit(@SqlType("varchar(x)") Slice source, @SqlType(Jo
return blockBuilder.build();
}

private static int getMatchingOffset(Matcher matcher, int at, int range)
{
try {
return matcher.searchInterruptible(at, range, Option.DEFAULT);
}
catch (InterruptedException interruptedException) {
throw new PrestoException(GENERIC_USER_ERROR, "Regexp matching interrupted");
}
}

private static void validateGroup(long group, Region region)
{
if (group < 0) {
Expand Down