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.List; | |
12 | import java.util.Locale; | |
13 | import org.opensearch.sql.data.model.ExprNullValue; | |
14 | import org.opensearch.sql.data.model.ExprValue; | |
15 | import org.opensearch.sql.data.model.ExprValueUtils; | |
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 average aggregator aggregate the value evaluated by the expression. | |
22 | * If the expression evaluated result is NULL or MISSING, then the result is NULL. | |
23 | */ | |
24 | public class AvgAggregator extends Aggregator<AvgAggregator.AvgState> { | |
25 | ||
26 | public AvgAggregator(List<Expression> arguments, ExprCoreType returnType) { | |
27 | super(BuiltinFunctionName.AVG.getName(), arguments, returnType); | |
28 | } | |
29 | ||
30 | @Override | |
31 | public AvgState create() { | |
32 |
1
1. create : replaced return value with null for org/opensearch/sql/expression/aggregation/AvgAggregator::create → KILLED |
return new AvgState(); |
33 | } | |
34 | ||
35 | @Override | |
36 | protected AvgState iterate(ExprValue value, AvgState state) { | |
37 |
1
1. iterate : Replaced integer addition with subtraction → KILLED |
state.count++; |
38 |
1
1. iterate : Replaced double addition with subtraction → KILLED |
state.total += ExprValueUtils.getDoubleValue(value); |
39 |
1
1. iterate : replaced return value with null for org/opensearch/sql/expression/aggregation/AvgAggregator::iterate → SURVIVED |
return state; |
40 | } | |
41 | ||
42 | @Override | |
43 | public String toString() { | |
44 |
1
1. toString : replaced return value with "" for org/opensearch/sql/expression/aggregation/AvgAggregator::toString → KILLED |
return String.format(Locale.ROOT, "avg(%s)", format(getArguments())); |
45 | } | |
46 | ||
47 | /** | |
48 | * Average State. | |
49 | */ | |
50 | protected static class AvgState implements AggregationState { | |
51 | private int count; | |
52 | private double total; | |
53 | ||
54 | AvgState() { | |
55 | this.count = 0; | |
56 | this.total = 0d; | |
57 | } | |
58 | ||
59 | @Override | |
60 | public ExprValue result() { | |
61 |
3
1. result : Replaced double division with multiplication → KILLED 2. result : negated conditional → KILLED 3. result : replaced return value with null for org/opensearch/sql/expression/aggregation/AvgAggregator$AvgState::result → KILLED |
return count == 0 ? ExprNullValue.of() : ExprValueUtils.doubleValue(total / count); |
62 | } | |
63 | } | |
64 | } | |
Mutations | ||
32 |
1.1 |
|
37 |
1.1 |
|
38 |
1.1 |
|
39 |
1.1 |
|
44 |
1.1 |
|
61 |
1.1 2.2 3.3 |