GrokExpression.java

1
/*
2
 * Copyright OpenSearch Contributors
3
 * SPDX-License-Identifier: Apache-2.0
4
 */
5
6
package org.opensearch.sql.expression.parse;
7
8
import java.util.List;
9
import java.util.Map;
10
import java.util.stream.Collectors;
11
import lombok.EqualsAndHashCode;
12
import lombok.ToString;
13
import org.apache.logging.log4j.LogManager;
14
import org.apache.logging.log4j.Logger;
15
import org.opensearch.sql.common.grok.Grok;
16
import org.opensearch.sql.common.grok.GrokCompiler;
17
import org.opensearch.sql.common.grok.Match;
18
import org.opensearch.sql.data.model.ExprStringValue;
19
import org.opensearch.sql.data.model.ExprValue;
20
import org.opensearch.sql.exception.ExpressionEvaluationException;
21
import org.opensearch.sql.expression.Expression;
22
23
/**
24
 * GrokExpression with grok patterns.
25
 */
26
@EqualsAndHashCode(callSuper = true)
27
@ToString
28
public class GrokExpression extends ParseExpression {
29
  private static final Logger log = LogManager.getLogger(GrokExpression.class);
30
  private static final GrokCompiler grokCompiler = GrokCompiler.newInstance();
31
32
  static {
33
    grokCompiler.registerDefaultPatterns();
34
  }
35
36
  @EqualsAndHashCode.Exclude
37
  private final Grok grok;
38
39
  /**
40
   * GrokExpression.
41
   *
42
   * @param sourceField source text field
43
   * @param pattern     pattern used for parsing
44
   * @param identifier  derived field
45
   */
46
  public GrokExpression(Expression sourceField, Expression pattern, Expression identifier) {
47
    super("grok", sourceField, pattern, identifier);
48
    this.grok = grokCompiler.compile(pattern.valueOf().stringValue());
49
  }
50
51
  @Override
52
  ExprValue parseValue(ExprValue value) throws ExpressionEvaluationException {
53
    String rawString = value.stringValue();
54
    Match grokMatch = grok.match(rawString);
55
    Map<String, Object> capture = grokMatch.capture();
56
    Object match = capture.get(identifierStr);
57 1 1. parseValue : negated conditional → KILLED
    if (match != null) {
58 1 1. parseValue : replaced return value with null for org/opensearch/sql/expression/parse/GrokExpression::parseValue → KILLED
      return new ExprStringValue(match.toString());
59
    }
60
    log.debug("failed to extract pattern {} from input ***", grok.getOriginalGrokPattern());
61 1 1. parseValue : replaced return value with null for org/opensearch/sql/expression/parse/GrokExpression::parseValue → KILLED
    return new ExprStringValue("");
62
  }
63
64
  /**
65
   * Get list of derived fields based on parse pattern.
66
   *
67
   * @param pattern pattern used for parsing
68
   * @return list of names of the derived fields
69
   */
70
  public static List<String> getNamedGroupCandidates(String pattern) {
71
    Grok grok = grokCompiler.compile(pattern);
72 1 1. getNamedGroupCandidates : replaced return value with Collections.emptyList for org/opensearch/sql/expression/parse/GrokExpression::getNamedGroupCandidates → KILLED
    return grok.namedGroups.stream().map(grok::getNamedRegexCollectionById)
73 2 1. lambda$getNamedGroupCandidates$0 : negated conditional → KILLED
2. lambda$getNamedGroupCandidates$0 : replaced boolean return with true for org/opensearch/sql/expression/parse/GrokExpression::lambda$getNamedGroupCandidates$0 → KILLED
        .filter(group -> !group.equals("UNWANTED")).collect(Collectors.toUnmodifiableList());
74
  }
75
}

Mutations

57

1.1
Location : parseValue
Killed by : org.opensearch.sql.expression.parse.GrokExpressionTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.parse.GrokExpressionTest]/[method:resolve_null_and_empty_values()]
negated conditional → KILLED

58

1.1
Location : parseValue
Killed by : org.opensearch.sql.expression.parse.GrokExpressionTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.parse.GrokExpressionTest]/[method:resolve_grok_groups_and_parsed_values()]
replaced return value with null for org/opensearch/sql/expression/parse/GrokExpression::parseValue → KILLED

61

1.1
Location : parseValue
Killed by : org.opensearch.sql.expression.parse.GrokExpressionTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.parse.GrokExpressionTest]/[method:resolve_null_and_empty_values()]
replaced return value with null for org/opensearch/sql/expression/parse/GrokExpression::parseValue → KILLED

72

1.1
Location : getNamedGroupCandidates
Killed by : org.opensearch.sql.expression.parse.GrokExpressionTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.parse.GrokExpressionTest]/[method:resolve_grok_groups_and_parsed_values()]
replaced return value with Collections.emptyList for org/opensearch/sql/expression/parse/GrokExpression::getNamedGroupCandidates → KILLED

73

1.1
Location : lambda$getNamedGroupCandidates$0
Killed by : org.opensearch.sql.expression.parse.GrokExpressionTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.parse.GrokExpressionTest]/[method:resolve_grok_groups_and_parsed_values()]
negated conditional → KILLED

2.2
Location : lambda$getNamedGroupCandidates$0
Killed by : org.opensearch.sql.expression.parse.GrokExpressionTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.parse.GrokExpressionTest]/[method:resolve_grok_groups_and_parsed_values()]
replaced boolean return with true for org/opensearch/sql/expression/parse/GrokExpression::lambda$getNamedGroupCandidates$0 → KILLED

Active mutators

Tests examined


Report generated by PIT 1.9.0