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 @@ -25,6 +25,8 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import java.util.zip.CRC32;
import javax.crypto.Cipher;
import javax.crypto.spec.GCMParameterSpec;
Expand Down Expand Up @@ -344,6 +346,21 @@ public static UTF8String quote(UTF8String str) {
return UTF8String.fromString(qtChar + sp + qtChar);
}

/**
* Compiles {@code regex} with the given {@code flags} for the regexp expression
* family, translating a {@link PatternSyntaxException} into the user-facing
* INVALID_PARAMETER_VALUE.PATTERN error. Shared by the regexp eval and codegen
* paths so the generated Java is a single call instead of an inline try/catch
* around {@code Pattern.compile}.
*/
public static Pattern compileRegexPattern(String regex, int flags, String funcName) {
try {
return Pattern.compile(regex, flags);
} catch (PatternSyntaxException e) {
throw QueryExecutionErrors.invalidPatternError(funcName, e.getPattern(), e);
}
}

/**
* Computes the CRC32 checksum of {@code bytes} for the {@code crc32} expression.
* Shared by the eval and codegen paths so the per-stage generated Java is a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1253,17 +1253,15 @@ object RegExpUtils {
val termLastRegex = ctx.addMutableState("UTF8String", "lastRegex")
val termPattern = ctx.addMutableState(classNamePattern, "pattern")
val collationRegexFlags = CollationSupport.collationAwareRegexFlags(collationId)
val utils = classOf[ExpressionImplUtils].getName

s"""
|if (!$regexp.equals($termLastRegex)) {
| // regex value changed
| try {
| UTF8String r = $regexp.clone();
| $termPattern = $classNamePattern.compile(r.toString(), $collationRegexFlags);
| $termLastRegex = r;
| } catch (java.util.regex.PatternSyntaxException e) {
| throw QueryExecutionErrors.invalidPatternError("$prettyName", e.getPattern(), e);
| }
| UTF8String r = $regexp.clone();
| $termPattern =
| $utils.compileRegexPattern(r.toString(), $collationRegexFlags, "$prettyName");
| $termLastRegex = r;
|}
|java.util.regex.Matcher $matcher = $termPattern.matcher($subject.toString());
|""".stripMargin
Expand All @@ -1272,12 +1270,8 @@ object RegExpUtils {
def getPatternAndLastRegex(p: Any, prettyName: String, collationId: Int): (Pattern, UTF8String) =
{
val r = p.asInstanceOf[UTF8String].clone()
val pattern = try {
Pattern.compile(r.toString, CollationSupport.collationAwareRegexFlags(collationId))
} catch {
case e: PatternSyntaxException =>
throw QueryExecutionErrors.invalidPatternError(prettyName, e.getPattern, e)
}
val pattern = ExpressionImplUtils.compileRegexPattern(
r.toString, CollationSupport.collationAwareRegexFlags(collationId), prettyName)
(pattern, r)
}
}