TakeAggregator.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.ArrayList;
12
import java.util.List;
13
import java.util.Locale;
14
import org.opensearch.sql.data.model.ExprCollectionValue;
15
import org.opensearch.sql.data.model.ExprValue;
16
import org.opensearch.sql.data.type.ExprCoreType;
17
import org.opensearch.sql.expression.Expression;
18
import org.opensearch.sql.expression.function.BuiltinFunctionName;
19
20
/**
21
 * The take aggregator keeps and returns the original values of a field.
22
 * If the field value is NULL or MISSING, then it is skipped.
23
 */
24
public class TakeAggregator extends Aggregator<TakeAggregator.TakeState> {
25
26
  public TakeAggregator(List<Expression> arguments, ExprCoreType returnType) {
27
    super(BuiltinFunctionName.TAKE.getName(), arguments, returnType);
28
  }
29
30
  @Override
31
  public TakeState create() {
32 1 1. create : replaced return value with null for org/opensearch/sql/expression/aggregation/TakeAggregator::create → KILLED
    return new TakeState(getArguments().get(1).valueOf().integerValue());
33
  }
34
35
  @Override
36
  protected TakeState iterate(ExprValue value, TakeState state) {
37 1 1. iterate : removed call to org/opensearch/sql/expression/aggregation/TakeAggregator$TakeState::take → KILLED
    state.take(value);
38 1 1. iterate : replaced return value with null for org/opensearch/sql/expression/aggregation/TakeAggregator::iterate → SURVIVED
    return state;
39
  }
40
41
  @Override
42
  public String toString() {
43 1 1. toString : replaced return value with "" for org/opensearch/sql/expression/aggregation/TakeAggregator::toString → KILLED
    return String.format(Locale.ROOT, "take(%s)", format(getArguments()));
44
  }
45
46
  /**
47
   * Take State.
48
   */
49
  protected static class TakeState implements AggregationState {
50
    protected int index;
51
    protected int size;
52
    protected List<ExprValue> hits;
53
54
    TakeState(int size) {
55 2 1. <init> : changed conditional boundary → KILLED
2. <init> : negated conditional → KILLED
      if (size <= 0) {
56
        throw new IllegalArgumentException("size must be greater than 0");
57
      }
58
      this.index = 0;
59
      this.size = size;
60
      this.hits = new ArrayList<>();
61
    }
62
63
    public void take(ExprValue value) {
64 2 1. take : changed conditional boundary → KILLED
2. take : negated conditional → KILLED
      if (index < size) {
65
        hits.add(value);
66
      }
67 1 1. take : Replaced integer addition with subtraction → KILLED
      index++;
68
    }
69
70
    @Override
71
    public ExprValue result() {
72 1 1. result : replaced return value with null for org/opensearch/sql/expression/aggregation/TakeAggregator$TakeState::result → KILLED
      return new ExprCollectionValue(hits);
73
    }
74
  }
75
}

Mutations

32

1.1
Location : create
Killed by : org.opensearch.sql.expression.aggregation.TakeAggregatorTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.aggregation.TakeAggregatorTest]/[method:test_take_all_missing_or_null()]
replaced return value with null for org/opensearch/sql/expression/aggregation/TakeAggregator::create → KILLED

37

1.1
Location : iterate
Killed by : org.opensearch.sql.expression.aggregation.TakeAggregatorTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.aggregation.TakeAggregatorTest]/[method:test_take_null()]
removed call to org/opensearch/sql/expression/aggregation/TakeAggregator$TakeState::take → KILLED

38

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

43

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

55

1.1
Location : <init>
Killed by : org.opensearch.sql.expression.aggregation.TakeAggregatorTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.aggregation.TakeAggregatorTest]/[method:test_take_with_invalid_size()]
changed conditional boundary → KILLED

2.2
Location : <init>
Killed by : org.opensearch.sql.expression.aggregation.TakeAggregatorTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.aggregation.TakeAggregatorTest]/[method:test_take_with_invalid_size()]
negated conditional → KILLED

64

1.1
Location : take
Killed by : org.opensearch.sql.expression.aggregation.TakeAggregatorTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.aggregation.TakeAggregatorTest]/[method:take_string_field_expression()]
changed conditional boundary → KILLED

2.2
Location : take
Killed by : org.opensearch.sql.expression.aggregation.TakeAggregatorTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.aggregation.TakeAggregatorTest]/[method:test_take_null()]
negated conditional → KILLED

67

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

72

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

Active mutators

Tests examined


Report generated by PIT 1.9.0