|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.sql.expression.parse; |
| 7 | + |
| 8 | +import com.google.common.collect.ImmutableList; |
| 9 | +import java.util.List; |
| 10 | +import java.util.concurrent.ConcurrentHashMap; |
| 11 | +import java.util.regex.Matcher; |
| 12 | +import java.util.regex.Pattern; |
| 13 | +import java.util.regex.PatternSyntaxException; |
| 14 | +import org.opensearch.sql.data.model.ExprValue; |
| 15 | +import org.opensearch.sql.data.type.ExprCoreType; |
| 16 | + |
| 17 | +/** |
| 18 | + * Common utilities for regex operations. Provides pattern caching and consistent matching behavior. |
| 19 | + */ |
| 20 | +public class RegexCommonUtils { |
| 21 | + |
| 22 | + // Pattern to identify named capture groups in regex |
| 23 | + private static final Pattern NAMED_GROUP_PATTERN = |
| 24 | + Pattern.compile("\\(\\?<([a-zA-Z][a-zA-Z0-9]*)>"); |
| 25 | + |
| 26 | + // Pattern cache to avoid recompiling the same patterns |
| 27 | + private static final ConcurrentHashMap<String, Pattern> patternCache = new ConcurrentHashMap<>(); |
| 28 | + |
| 29 | + // Maximum cache size to prevent memory issues |
| 30 | + private static final int MAX_CACHE_SIZE = 1000; |
| 31 | + |
| 32 | + /** |
| 33 | + * Get compiled pattern from cache or compile and cache it. |
| 34 | + * |
| 35 | + * @param regex The regex pattern string |
| 36 | + * @return Compiled Pattern object |
| 37 | + * @throws PatternSyntaxException if the regex is invalid |
| 38 | + */ |
| 39 | + public static Pattern getCompiledPattern(String regex) { |
| 40 | + // Check cache size and clear if needed (simple LRU-like behavior) |
| 41 | + if (patternCache.size() > MAX_CACHE_SIZE) { |
| 42 | + patternCache.clear(); |
| 43 | + } |
| 44 | + |
| 45 | + return patternCache.computeIfAbsent(regex, Pattern::compile); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Extract list of named group candidates from a regex pattern. |
| 50 | + * |
| 51 | + * @param pattern The regex pattern string |
| 52 | + * @return List of named group names found in the pattern |
| 53 | + */ |
| 54 | + public static List<String> getNamedGroupCandidates(String pattern) { |
| 55 | + ImmutableList.Builder<String> namedGroups = ImmutableList.builder(); |
| 56 | + Matcher m = NAMED_GROUP_PATTERN.matcher(pattern); |
| 57 | + while (m.find()) { |
| 58 | + namedGroups.add(m.group(1)); |
| 59 | + } |
| 60 | + return namedGroups.build(); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Match using find() for partial match semantics with string pattern. |
| 65 | + * |
| 66 | + * @param text The text to match against |
| 67 | + * @param patternStr The pattern string |
| 68 | + * @return true if pattern is found anywhere in the text |
| 69 | + * @throws PatternSyntaxException if the regex is invalid |
| 70 | + */ |
| 71 | + public static boolean matchesPartial(String text, String patternStr) { |
| 72 | + if (text == null || patternStr == null) { |
| 73 | + return false; |
| 74 | + } |
| 75 | + Pattern pattern = getCompiledPattern(patternStr); |
| 76 | + return pattern.matcher(text).find(); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Extract a specific named group from text using the pattern. Used by parse command regex method. |
| 81 | + * |
| 82 | + * @param text The text to extract from |
| 83 | + * @param pattern The compiled pattern with named groups |
| 84 | + * @param groupName The name of the group to extract |
| 85 | + * @return The extracted value or null if not found |
| 86 | + */ |
| 87 | + public static String extractNamedGroup(String text, Pattern pattern, String groupName) { |
| 88 | + if (text == null || pattern == null || groupName == null) { |
| 89 | + return null; |
| 90 | + } |
| 91 | + |
| 92 | + Matcher matcher = pattern.matcher(text); |
| 93 | + |
| 94 | + // Use matches() for parse command (full match required) |
| 95 | + if (matcher.matches()) { |
| 96 | + try { |
| 97 | + return matcher.group(groupName); |
| 98 | + } catch (IllegalArgumentException e) { |
| 99 | + // Group name not found |
| 100 | + return null; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return null; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Convert ExprValue to string, handling non-string types. Based on decision to auto-convert |
| 109 | + * non-string fields to strings. |
| 110 | + * |
| 111 | + * @param value The ExprValue to convert |
| 112 | + * @return String representation of the value |
| 113 | + */ |
| 114 | + public static String toStringValue(ExprValue value) { |
| 115 | + if (value == null || value.isNull() || value.isMissing()) { |
| 116 | + return null; |
| 117 | + } |
| 118 | + |
| 119 | + // If already a string, return it directly |
| 120 | + if (value.type() == ExprCoreType.STRING) { |
| 121 | + return value.stringValue(); |
| 122 | + } |
| 123 | + |
| 124 | + // Auto-convert non-string types to string |
| 125 | + return value.value().toString(); |
| 126 | + } |
| 127 | +} |
0 commit comments