WideningTypeRule.java

1
/*
2
 * Copyright OpenSearch Contributors
3
 * SPDX-License-Identifier: Apache-2.0
4
 */
5
6
7
package org.opensearch.sql.data.type;
8
9
import static org.opensearch.sql.data.type.ExprCoreType.UNKNOWN;
10
11
import lombok.experimental.UtilityClass;
12
import org.opensearch.sql.exception.ExpressionEvaluationException;
13
14
/**
15
 * The definition of widening type rule for expression value.
16
 * ExprType     Widens to data types
17
 * INTEGER      LONG, FLOAT, DOUBLE
18
 * LONG         FLOAT, DOUBLE
19
 * FLOAT        DOUBLE
20
 * DOUBLE       DOUBLE
21
 * STRING       STRING
22
 * BOOLEAN      BOOLEAN
23
 * ARRAY        ARRAY
24
 * STRUCT       STRUCT
25
 */
26
@UtilityClass
27
public class WideningTypeRule {
28
  public static final int IMPOSSIBLE_WIDENING = Integer.MAX_VALUE;
29
  public static final int TYPE_EQUAL = 0;
30
31
  /**
32
   * The widening distance is calculated from the leaf to root.
33
   * e.g. distance(INTEGER, FLOAT) = 2, but distance(FLOAT, INTEGER) = IMPOSSIBLE_WIDENING
34
   *
35
   * @param type1 widen from type
36
   * @param type2 widen to type
37
   * @return The widening distance when widen one type to another type.
38
   */
39
  public static int distance(ExprType type1, ExprType type2) {
40 1 1. distance : replaced int return with 0 for org/opensearch/sql/data/type/WideningTypeRule::distance → KILLED
    return distance(type1, type2, TYPE_EQUAL);
41
  }
42
43
  private static int distance(ExprType type1, ExprType type2, int distance) {
44 1 1. distance : negated conditional → KILLED
    if (type1 == type2) {
45 1 1. distance : replaced int return with 0 for org/opensearch/sql/data/type/WideningTypeRule::distance → KILLED
      return distance;
46 1 1. distance : negated conditional → KILLED
    } else if (type1 == UNKNOWN) {
47 1 1. distance : replaced int return with 0 for org/opensearch/sql/data/type/WideningTypeRule::distance → KILLED
      return IMPOSSIBLE_WIDENING;
48
    } else {
49 1 1. distance : replaced int return with 0 for org/opensearch/sql/data/type/WideningTypeRule::distance → KILLED
      return type1.getParent().stream()
50 2 1. lambda$distance$0 : Replaced integer addition with subtraction → KILLED
2. lambda$distance$0 : replaced Integer return value with 0 for org/opensearch/sql/data/type/WideningTypeRule::lambda$distance$0 → KILLED
          .map(parentOfType1 -> distance(parentOfType1, type2, distance + 1))
51
          .reduce(Math::min).get();
52
    }
53
  }
54
55
  /**
56
   * The max type among two types. The max is defined as follow
57
   * if type1 could widen to type2, then max is type2, vice versa
58
   * if type1 could't widen to type2 and type2 could't widen to type1,
59
   * then throw {@link ExpressionEvaluationException}.
60
   *
61
   * @param type1 type1
62
   * @param type2 type2
63
   * @return the max type among two types.
64
   */
65
  public static ExprType max(ExprType type1, ExprType type2) {
66
    int type1To2 = distance(type1, type2);
67
    int type2To1 = distance(type2, type1);
68
69 2 1. max : negated conditional → KILLED
2. max : negated conditional → KILLED
    if (type1To2 == Integer.MAX_VALUE && type2To1 == Integer.MAX_VALUE) {
70
      throw new ExpressionEvaluationException(
71
          String.format("no max type of %s and %s ", type1, type2));
72
    } else {
73 2 1. max : negated conditional → KILLED
2. max : replaced return value with null for org/opensearch/sql/data/type/WideningTypeRule::max → KILLED
      return type1To2 == Integer.MAX_VALUE ? type1 : type2;
74
    }
75
  }
76
}

Mutations

40

1.1
Location : distance
Killed by : org.opensearch.sql.expression.function.WideningTypeRuleTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.function.WideningTypeRuleTest]/[test-template:distance(org.opensearch.sql.data.type.ExprCoreType, org.opensearch.sql.data.type.ExprCoreType, java.lang.Integer)]/[test-template-invocation:#126]
replaced int return with 0 for org/opensearch/sql/data/type/WideningTypeRule::distance → KILLED

44

1.1
Location : distance
Killed by : org.opensearch.sql.expression.function.WideningTypeRuleTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.function.WideningTypeRuleTest]/[test-template:distance(org.opensearch.sql.data.type.ExprCoreType, org.opensearch.sql.data.type.ExprCoreType, java.lang.Integer)]/[test-template-invocation:#113]
negated conditional → KILLED

45

1.1
Location : distance
Killed by : org.opensearch.sql.expression.function.WideningTypeRuleTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.function.WideningTypeRuleTest]/[test-template:distance(org.opensearch.sql.data.type.ExprCoreType, org.opensearch.sql.data.type.ExprCoreType, java.lang.Integer)]/[test-template-invocation:#102]
replaced int return with 0 for org/opensearch/sql/data/type/WideningTypeRule::distance → KILLED

46

1.1
Location : distance
Killed by : org.opensearch.sql.expression.function.WideningTypeRuleTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.function.WideningTypeRuleTest]/[test-template:distance(org.opensearch.sql.data.type.ExprCoreType, org.opensearch.sql.data.type.ExprCoreType, java.lang.Integer)]/[test-template-invocation:#102]
negated conditional → KILLED

47

1.1
Location : distance
Killed by : org.opensearch.sql.expression.function.WideningTypeRuleTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.function.WideningTypeRuleTest]/[test-template:distance(org.opensearch.sql.data.type.ExprCoreType, org.opensearch.sql.data.type.ExprCoreType, java.lang.Integer)]/[test-template-invocation:#200]
replaced int return with 0 for org/opensearch/sql/data/type/WideningTypeRule::distance → KILLED

49

1.1
Location : distance
Killed by : org.opensearch.sql.expression.function.WideningTypeRuleTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.function.WideningTypeRuleTest]/[test-template:distance(org.opensearch.sql.data.type.ExprCoreType, org.opensearch.sql.data.type.ExprCoreType, java.lang.Integer)]/[test-template-invocation:#200]
replaced int return with 0 for org/opensearch/sql/data/type/WideningTypeRule::distance → KILLED

50

1.1
Location : lambda$distance$0
Killed by : org.opensearch.sql.expression.function.WideningTypeRuleTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.function.WideningTypeRuleTest]/[test-template:distance(org.opensearch.sql.data.type.ExprCoreType, org.opensearch.sql.data.type.ExprCoreType, java.lang.Integer)]/[test-template-invocation:#102]
Replaced integer addition with subtraction → KILLED

2.2
Location : lambda$distance$0
Killed by : org.opensearch.sql.expression.function.WideningTypeRuleTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.function.WideningTypeRuleTest]/[test-template:distance(org.opensearch.sql.data.type.ExprCoreType, org.opensearch.sql.data.type.ExprCoreType, java.lang.Integer)]/[test-template-invocation:#200]
replaced Integer return value with 0 for org/opensearch/sql/data/type/WideningTypeRule::lambda$distance$0 → KILLED

69

1.1
Location : max
Killed by : org.opensearch.sql.expression.function.WideningTypeRuleTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.function.WideningTypeRuleTest]/[test-template:max(org.opensearch.sql.data.type.ExprCoreType, org.opensearch.sql.data.type.ExprCoreType, org.opensearch.sql.data.type.ExprCoreType)]/[test-template-invocation:#5]
negated conditional → KILLED

2.2
Location : max
Killed by : org.opensearch.sql.expression.function.WideningTypeRuleTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.function.WideningTypeRuleTest]/[test-template:max(org.opensearch.sql.data.type.ExprCoreType, org.opensearch.sql.data.type.ExprCoreType, org.opensearch.sql.data.type.ExprCoreType)]/[test-template-invocation:#61]
negated conditional → KILLED

73

1.1
Location : max
Killed by : org.opensearch.sql.expression.function.WideningTypeRuleTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.function.WideningTypeRuleTest]/[test-template:max(org.opensearch.sql.data.type.ExprCoreType, org.opensearch.sql.data.type.ExprCoreType, org.opensearch.sql.data.type.ExprCoreType)]/[test-template-invocation:#35]
negated conditional → KILLED

2.2
Location : max
Killed by : org.opensearch.sql.expression.function.WideningTypeRuleTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.function.WideningTypeRuleTest]/[test-template:max(org.opensearch.sql.data.type.ExprCoreType, org.opensearch.sql.data.type.ExprCoreType, org.opensearch.sql.data.type.ExprCoreType)]/[test-template-invocation:#113]
replaced return value with null for org/opensearch/sql/data/type/WideningTypeRule::max → KILLED

Active mutators

Tests examined


Report generated by PIT 1.9.0