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.data.model.ExprValueUtils.doubleValue; | |
10 | import static org.opensearch.sql.data.model.ExprValueUtils.floatValue; | |
11 | import static org.opensearch.sql.data.model.ExprValueUtils.getDoubleValue; | |
12 | import static org.opensearch.sql.data.model.ExprValueUtils.getFloatValue; | |
13 | import static org.opensearch.sql.data.model.ExprValueUtils.getIntegerValue; | |
14 | import static org.opensearch.sql.data.model.ExprValueUtils.getLongValue; | |
15 | import static org.opensearch.sql.data.model.ExprValueUtils.integerValue; | |
16 | import static org.opensearch.sql.data.model.ExprValueUtils.longValue; | |
17 | import static org.opensearch.sql.utils.ExpressionUtils.format; | |
18 | ||
19 | import java.util.List; | |
20 | import java.util.Locale; | |
21 | import org.opensearch.sql.data.model.ExprNullValue; | |
22 | import org.opensearch.sql.data.model.ExprValue; | |
23 | import org.opensearch.sql.data.model.ExprValueUtils; | |
24 | import org.opensearch.sql.data.type.ExprCoreType; | |
25 | import org.opensearch.sql.exception.ExpressionEvaluationException; | |
26 | import org.opensearch.sql.expression.Expression; | |
27 | import org.opensearch.sql.expression.aggregation.SumAggregator.SumState; | |
28 | import org.opensearch.sql.expression.function.BuiltinFunctionName; | |
29 | ||
30 | /** | |
31 | * The sum aggregator aggregate the value evaluated by the expression. | |
32 | * If the expression evaluated result is NULL or MISSING, then the result is NULL. | |
33 | */ | |
34 | public class SumAggregator extends Aggregator<SumState> { | |
35 | ||
36 | public SumAggregator(List<Expression> arguments, ExprCoreType returnType) { | |
37 | super(BuiltinFunctionName.SUM.getName(), arguments, returnType); | |
38 | } | |
39 | ||
40 | @Override | |
41 | public SumState create() { | |
42 |
1
1. create : replaced return value with null for org/opensearch/sql/expression/aggregation/SumAggregator::create → KILLED |
return new SumState(returnType); |
43 | } | |
44 | ||
45 | @Override | |
46 | protected SumState iterate(ExprValue value, SumState state) { | |
47 | state.isEmptyCollection = false; | |
48 |
1
1. iterate : removed call to org/opensearch/sql/expression/aggregation/SumAggregator$SumState::add → KILLED |
state.add(value); |
49 |
1
1. iterate : replaced return value with null for org/opensearch/sql/expression/aggregation/SumAggregator::iterate → KILLED |
return state; |
50 | } | |
51 | ||
52 | @Override | |
53 | public String toString() { | |
54 |
1
1. toString : replaced return value with "" for org/opensearch/sql/expression/aggregation/SumAggregator::toString → KILLED |
return String.format(Locale.ROOT, "sum(%s)", format(getArguments())); |
55 | } | |
56 | ||
57 | /** | |
58 | * Sum State. | |
59 | */ | |
60 | protected static class SumState implements AggregationState { | |
61 | ||
62 | private final ExprCoreType type; | |
63 | private ExprValue sumResult; | |
64 | private boolean isEmptyCollection; | |
65 | ||
66 | SumState(ExprCoreType type) { | |
67 | this.type = type; | |
68 | sumResult = ExprValueUtils.integerValue(0); | |
69 | isEmptyCollection = true; | |
70 | } | |
71 | ||
72 | /** | |
73 | * Add value to current sumResult. | |
74 | */ | |
75 | public void add(ExprValue value) { | |
76 | switch (type) { | |
77 | case INTEGER: | |
78 |
1
1. add : Replaced integer addition with subtraction → KILLED |
sumResult = integerValue(getIntegerValue(sumResult) + getIntegerValue(value)); |
79 | break; | |
80 | case LONG: | |
81 |
1
1. add : Replaced long addition with subtraction → KILLED |
sumResult = longValue(getLongValue(sumResult) + getLongValue(value)); |
82 | break; | |
83 | case FLOAT: | |
84 |
1
1. add : Replaced float addition with subtraction → KILLED |
sumResult = floatValue(getFloatValue(sumResult) + getFloatValue(value)); |
85 | break; | |
86 | case DOUBLE: | |
87 |
1
1. add : Replaced double addition with subtraction → KILLED |
sumResult = doubleValue(getDoubleValue(sumResult) + getDoubleValue(value)); |
88 | break; | |
89 | default: | |
90 | throw new ExpressionEvaluationException( | |
91 | String.format("unexpected type [%s] in sum aggregation", type)); | |
92 | } | |
93 | } | |
94 | ||
95 | @Override | |
96 | public ExprValue result() { | |
97 |
2
1. result : negated conditional → KILLED 2. result : replaced return value with null for org/opensearch/sql/expression/aggregation/SumAggregator$SumState::result → KILLED |
return isEmptyCollection ? ExprNullValue.of() : sumResult; |
98 | } | |
99 | } | |
100 | } | |
Mutations | ||
42 |
1.1 |
|
48 |
1.1 |
|
49 |
1.1 |
|
54 |
1.1 |
|
78 |
1.1 |
|
81 |
1.1 |
|
84 |
1.1 |
|
87 |
1.1 |
|
97 |
1.1 2.2 |