CountAggregator.java

1
/*
2
 * Copyright OpenSearch Contributors
3
 * SPDX-License-Identifier: Apache-2.0
4
 */
5
6
7
package org.opensearch.sql.expression.aggregation;
8
9
import static org.opensearch.sql.utils.ExpressionUtils.format;
10
11
import java.util.HashSet;
12
import java.util.List;
13
import java.util.Locale;
14
import java.util.Set;
15
import org.opensearch.sql.data.model.ExprValue;
16
import org.opensearch.sql.data.model.ExprValueUtils;
17
import org.opensearch.sql.data.type.ExprCoreType;
18
import org.opensearch.sql.expression.Expression;
19
import org.opensearch.sql.expression.aggregation.CountAggregator.CountState;
20
import org.opensearch.sql.expression.function.BuiltinFunctionName;
21
22
public class CountAggregator extends Aggregator<CountState> {
23
24
  public CountAggregator(List<Expression> arguments, ExprCoreType returnType) {
25
    super(BuiltinFunctionName.COUNT.getName(), arguments, returnType);
26
  }
27
28
  @Override
29
  public CountAggregator.CountState create() {
30 2 1. create : negated conditional → KILLED
2. create : replaced return value with null for org/opensearch/sql/expression/aggregation/CountAggregator::create → KILLED
    return distinct ? new DistinctCountState() : new CountState();
31
  }
32
33
  @Override
34
  protected CountState iterate(ExprValue value, CountState state) {
35 1 1. iterate : removed call to org/opensearch/sql/expression/aggregation/CountAggregator$CountState::count → KILLED
    state.count(value);
36 1 1. iterate : replaced return value with null for org/opensearch/sql/expression/aggregation/CountAggregator::iterate → SURVIVED
    return state;
37
  }
38
39
  @Override
40
  public String toString() {
41 2 1. toString : negated conditional → KILLED
2. toString : replaced return value with "" for org/opensearch/sql/expression/aggregation/CountAggregator::toString → KILLED
    return distinct
42
        ? String.format(Locale.ROOT, "count(distinct %s)", format(getArguments()))
43
        : String.format(Locale.ROOT, "count(%s)", format(getArguments()));
44
  }
45
46
  /**
47
   * Count State.
48
   */
49
  protected static class CountState implements AggregationState {
50
    protected int count;
51
52
    CountState() {
53
      this.count = 0;
54
    }
55
56
    public void count(ExprValue value) {
57 1 1. count : Replaced integer addition with subtraction → KILLED
      count++;
58
    }
59
60
    @Override
61
    public ExprValue result() {
62 1 1. result : replaced return value with null for org/opensearch/sql/expression/aggregation/CountAggregator$CountState::result → KILLED
      return ExprValueUtils.integerValue(count);
63
    }
64
  }
65
66
  protected static class DistinctCountState extends CountState {
67
    private final Set<ExprValue> distinctValues = new HashSet<>();
68
69
    @Override
70
    public void count(ExprValue value) {
71 1 1. count : negated conditional → KILLED
      if (!distinctValues.contains(value)) {
72
        distinctValues.add(value);
73 1 1. count : Replaced integer addition with subtraction → KILLED
        count++;
74
      }
75
    }
76
  }
77
}

Mutations

30

1.1
Location : create
Killed by : org.opensearch.sql.expression.aggregation.CountAggregatorTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.aggregation.CountAggregatorTest]/[method:count_literal_with_null_and_missing()]
negated conditional → KILLED

2.2
Location : create
Killed by : org.opensearch.sql.expression.aggregation.CountAggregatorTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.aggregation.CountAggregatorTest]/[method:count_literal_with_null_and_missing()]
replaced return value with null for org/opensearch/sql/expression/aggregation/CountAggregator::create → KILLED

35

1.1
Location : iterate
Killed by : org.opensearch.sql.expression.aggregation.CountAggregatorTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.aggregation.CountAggregatorTest]/[method:count_literal_with_null_and_missing()]
removed call to org/opensearch/sql/expression/aggregation/CountAggregator$CountState::count → KILLED

36

1.1
Location : iterate
Killed by : none
replaced return value with null for org/opensearch/sql/expression/aggregation/CountAggregator::iterate → SURVIVED

41

1.1
Location : toString
Killed by : org.opensearch.sql.expression.aggregation.CountAggregatorTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.aggregation.CountAggregatorTest]/[method:test_to_string()]
negated conditional → KILLED

2.2
Location : toString
Killed by : org.opensearch.sql.expression.aggregation.CountAggregatorTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.aggregation.CountAggregatorTest]/[method:test_to_string()]
replaced return value with "" for org/opensearch/sql/expression/aggregation/CountAggregator::toString → KILLED

57

1.1
Location : count
Killed by : org.opensearch.sql.expression.aggregation.CountAggregatorTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.aggregation.CountAggregatorTest]/[method:count_literal_with_null_and_missing()]
Replaced integer addition with subtraction → KILLED

62

1.1
Location : result
Killed by : org.opensearch.sql.expression.aggregation.CountAggregatorTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.aggregation.CountAggregatorTest]/[method:count_literal_with_null_and_missing()]
replaced return value with null for org/opensearch/sql/expression/aggregation/CountAggregator$CountState::result → KILLED

71

1.1
Location : count
Killed by : org.opensearch.sql.expression.aggregation.CountAggregatorTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.aggregation.CountAggregatorTest]/[method:distinct_count()]
negated conditional → KILLED

73

1.1
Location : count
Killed by : org.opensearch.sql.expression.aggregation.CountAggregatorTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.aggregation.CountAggregatorTest]/[method:distinct_count()]
Replaced integer addition with subtraction → KILLED

Active mutators

Tests examined


Report generated by PIT 1.9.0