PatternsExpression.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 com.google.common.collect.ImmutableList;
9
import com.google.common.collect.ImmutableSet;
10
import java.util.List;
11
import java.util.Objects;
12
import java.util.regex.Pattern;
13
import lombok.EqualsAndHashCode;
14
import lombok.ToString;
15
import org.opensearch.sql.data.model.ExprStringValue;
16
import org.opensearch.sql.data.model.ExprValue;
17
import org.opensearch.sql.exception.ExpressionEvaluationException;
18
import org.opensearch.sql.expression.Expression;
19
20
/**
21
 * PatternsExpression with regex filter.
22
 */
23
@EqualsAndHashCode(callSuper = true)
24
@ToString
25
public class PatternsExpression extends ParseExpression {
26
  /**
27
   * Default name of the derived field.
28
   */
29
  public static final String DEFAULT_NEW_FIELD = "patterns_field";
30
31
  private static final ImmutableSet<Character> DEFAULT_IGNORED_CHARS = ImmutableSet.copyOf(
32
      "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".chars()
33 2 1. lambda$static$0 : replaced Character return value with 0 for org/opensearch/sql/expression/parse/PatternsExpression::lambda$static$0 → SURVIVED
2. lambda$static$1 : replaced return value with null for org/opensearch/sql/expression/parse/PatternsExpression::lambda$static$1 → SURVIVED
          .mapToObj(c -> (char) c).toArray(Character[]::new));
34
  private final boolean useCustomPattern;
35
  @EqualsAndHashCode.Exclude
36
  private Pattern pattern;
37
38
  /**
39
   * PatternsExpression.
40
   *
41
   * @param sourceField source text field
42
   * @param pattern     pattern used for parsing
43
   * @param identifier  derived field
44
   */
45
  public PatternsExpression(Expression sourceField, Expression pattern, Expression identifier) {
46
    super("patterns", sourceField, pattern, identifier);
47
    String patternStr = pattern.valueOf().stringValue();
48 1 1. <init> : negated conditional → KILLED
    useCustomPattern = !patternStr.isEmpty();
49 1 1. <init> : negated conditional → KILLED
    if (useCustomPattern) {
50
      this.pattern = Pattern.compile(patternStr);
51
    }
52
  }
53
54
  @Override
55
  ExprValue parseValue(ExprValue value) throws ExpressionEvaluationException {
56
    String rawString = value.stringValue();
57 1 1. parseValue : negated conditional → KILLED
    if (useCustomPattern) {
58 1 1. parseValue : replaced return value with null for org/opensearch/sql/expression/parse/PatternsExpression::parseValue → KILLED
      return new ExprStringValue(pattern.matcher(rawString).replaceAll(""));
59
    }
60
61
    char[] chars = rawString.toCharArray();
62
    int pos = 0;
63 2 1. parseValue : changed conditional boundary → KILLED
2. parseValue : negated conditional → KILLED
    for (int i = 0; i < chars.length; i++) {
64 1 1. parseValue : negated conditional → KILLED
      if (!DEFAULT_IGNORED_CHARS.contains(chars[i])) {
65 1 1. parseValue : Changed increment from 1 to -1 → KILLED
        chars[pos++] = chars[i];
66
      }
67
    }
68 1 1. parseValue : replaced return value with null for org/opensearch/sql/expression/parse/PatternsExpression::parseValue → KILLED
    return new ExprStringValue(new String(chars, 0, pos));
69
  }
70
71
  /**
72
   * Get list of derived fields.
73
   *
74
   * @param identifier identifier used to generate the field name
75
   * @return list of names of the derived fields
76
   */
77
  public static List<String> getNamedGroupCandidates(String identifier) {
78 1 1. getNamedGroupCandidates : replaced return value with Collections.emptyList for org/opensearch/sql/expression/parse/PatternsExpression::getNamedGroupCandidates → KILLED
    return ImmutableList.of(Objects.requireNonNullElse(identifier, DEFAULT_NEW_FIELD));
79
  }
80
}

Mutations

33

1.1
Location : lambda$static$0
Killed by : none
replaced Character return value with 0 for org/opensearch/sql/expression/parse/PatternsExpression::lambda$static$0 → SURVIVED

2.2
Location : lambda$static$1
Killed by : none
replaced return value with null for org/opensearch/sql/expression/parse/PatternsExpression::lambda$static$1 → SURVIVED

48

1.1
Location : <init>
Killed by : org.opensearch.sql.expression.parse.PatternsExpressionTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.parse.PatternsExpressionTest]/[method:resolve_value()]
negated conditional → KILLED

49

1.1
Location : <init>
Killed by : org.opensearch.sql.expression.parse.PatternsExpressionTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.parse.PatternsExpressionTest]/[method:resolve_value()]
negated conditional → KILLED

57

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

58

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

63

1.1
Location : parseValue
Killed by : org.opensearch.sql.expression.parse.PatternsExpressionTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.parse.PatternsExpressionTest]/[method:resolve_value()]
changed conditional boundary → KILLED

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

64

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

65

1.1
Location : parseValue
Killed by : org.opensearch.sql.expression.parse.PatternsExpressionTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.parse.PatternsExpressionTest]/[method:resolve_value()]
Changed increment from 1 to -1 → KILLED

68

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

78

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

Active mutators

Tests examined


Report generated by PIT 1.9.0