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 |
|
37 |
1.1 |
|
38 |
1.1 |
|
43 |
1.1 |
|
55 |
1.1 2.2 |
|
64 |
1.1 2.2 |
|
67 |
1.1 |
|
72 |
1.1 |