Skip to content

Commit 4a3b03a

Browse files
authored
[PPL] Patterns command add UUID regex into log template parsing (#3989)
* add UUID regex into template Signed-off-by: Hailong Cui <ihailong@amazon.com> * fix spotless Signed-off-by: Hailong Cui <ihailong@amazon.com> * add integration test Signed-off-by: Hailong Cui <ihailong@amazon.com> --------- Signed-off-by: Hailong Cui <ihailong@amazon.com>
1 parent 5c0ed0d commit 4a3b03a

3 files changed

Lines changed: 38 additions & 0 deletions

File tree

common/src/main/java/org/opensearch/sql/common/patterns/BrainLogParser.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ public class BrainLogParser {
3939
"(\\d{4}-\\d{2}-\\d{2})[T"
4040
+ " ]?(\\d{2}:\\d{2}:\\d{2})(\\.\\d{3})?(Z|([+-]\\d{2}:?\\d{2}))?"),
4141
"<*DATETIME*>");
42+
// UUID
43+
DEFAULT_FILTER_PATTERN_VARIABLE_MAP.put(
44+
Pattern.compile(
45+
"\\b[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\\b"),
46+
"<*UUID*>");
4247
// Hex Decimal, letters followed by digits, float numbers
4348
DEFAULT_FILTER_PATTERN_VARIABLE_MAP.put(
4449
Pattern.compile(

common/src/test/java/org/opensearch/sql/common/patterns/BrainLogParserTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,22 @@ public void testPreprocess() {
104104
assertEquals(expectedResult, result);
105105
}
106106

107+
@Test
108+
public void testPreprocessWithUUID() {
109+
String logMessage = "127.0.0.1 - 1234 something, user_id:c78ac970-f0c3-4954-8cf8-352a8458d01c";
110+
String logId = "log1";
111+
List<String> expectedResult =
112+
Arrays.asList("<*IP*>", "-", "<*>", "something", "user_id:<*UUID*>", "log1");
113+
List<String> result = parser.preprocess(logMessage, logId);
114+
assertEquals(expectedResult, result);
115+
// Test with different delimiter
116+
logMessage = "127.0.0.1=1234 something, user_id:c78ac970-f0c3-4954-8cf8-352a8458d01c";
117+
logId = "log2";
118+
expectedResult = Arrays.asList("<*IP*>=<*>", "something", "user_id:<*UUID*>", "log2");
119+
result = parser.preprocess(logMessage, logId);
120+
assertEquals(expectedResult, result);
121+
}
122+
107123
@Test
108124
public void testPreprocessWithIllegalInput() {
109125
String logMessage = "127.0.0.1 - 1234 something";

integ-test/src/test/java/org/opensearch/sql/calcite/remote/CalcitePPLPatternsIT.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,4 +281,21 @@ public void testBrainAggregationModeWithGroupByClause() throws IOException {
281281
ImmutableMap.of(
282282
"<token1>", ImmutableList.of("6996194389878584395", "-1547954353065580372"))));
283283
}
284+
285+
@Test
286+
public void testBrainParseWithUUID() throws IOException {
287+
JSONObject result =
288+
executeQuery(
289+
String.format(
290+
"source=%s | eval body = '[PlaceOrder] user_id=d664d7be-77d8-11f0-8880-0242f00b101d"
291+
+ " user_currency=USD' | head 1 | patterns body method=BRAIN mode=label |"
292+
+ " fields patterns_field, tokens",
293+
Index.WEBLOG.getName()));
294+
verifySchema(result, schema("patterns_field", "string"), schema("tokens", "struct"));
295+
verifyDataRows(
296+
result,
297+
rows(
298+
"[PlaceOrder] user_id=<token1> user_currency=USD",
299+
ImmutableMap.of("<token1>", ImmutableList.of("d664d7be-77d8-11f0-8880-0242f00b101d"))));
300+
}
284301
}

0 commit comments

Comments
 (0)