1 | /* | |
2 | * Licensed under the Apache License, Version 2.0 (the "License"). | |
3 | * You may not use this file except in compliance with the License. | |
4 | * A copy of the License is located at | |
5 | * | |
6 | * http://www.apache.org/licenses/LICENSE-2.0 | |
7 | * | |
8 | * or in the "license" file accompanying this file. This file is distributed | |
9 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | |
10 | * express or implied. See the License for the specific language governing | |
11 | * permissions and limitations under the License. | |
12 | */ | |
13 | ||
14 | package org.opensearch.sql.expression.aggregation; | |
15 | ||
16 | import static org.opensearch.sql.data.model.ExprValueUtils.doubleValue; | |
17 | import static org.opensearch.sql.utils.ExpressionUtils.format; | |
18 | ||
19 | import java.util.ArrayList; | |
20 | import java.util.List; | |
21 | import org.apache.commons.math3.stat.descriptive.moment.StandardDeviation; | |
22 | import org.opensearch.sql.common.utils.StringUtils; | |
23 | import org.opensearch.sql.data.model.ExprNullValue; | |
24 | import org.opensearch.sql.data.model.ExprValue; | |
25 | import org.opensearch.sql.data.type.ExprCoreType; | |
26 | import org.opensearch.sql.expression.Expression; | |
27 | import org.opensearch.sql.expression.function.BuiltinFunctionName; | |
28 | ||
29 | /** | |
30 | * StandardDeviation Aggregator. | |
31 | */ | |
32 | public class StdDevAggregator extends Aggregator<StdDevAggregator.StdDevState> { | |
33 | ||
34 | private final boolean isSampleStdDev; | |
35 | ||
36 | /** | |
37 | * Build Population Variance {@link VarianceAggregator}. | |
38 | */ | |
39 | public static Aggregator stddevPopulation(List<Expression> arguments, | |
40 | ExprCoreType returnType) { | |
41 |
1
1. stddevPopulation : replaced return value with null for org/opensearch/sql/expression/aggregation/StdDevAggregator::stddevPopulation → KILLED |
return new StdDevAggregator(false, arguments, returnType); |
42 | } | |
43 | ||
44 | /** | |
45 | * Build Sample Variance {@link VarianceAggregator}. | |
46 | */ | |
47 | public static Aggregator stddevSample(List<Expression> arguments, | |
48 | ExprCoreType returnType) { | |
49 |
1
1. stddevSample : replaced return value with null for org/opensearch/sql/expression/aggregation/StdDevAggregator::stddevSample → KILLED |
return new StdDevAggregator(true, arguments, returnType); |
50 | } | |
51 | ||
52 | /** | |
53 | * VarianceAggregator constructor. | |
54 | * | |
55 | * @param isSampleStdDev true for sample standard deviation aggregator, false for population | |
56 | * standard deviation aggregator. | |
57 | * @param arguments aggregator arguments. | |
58 | * @param returnType aggregator return types. | |
59 | */ | |
60 | public StdDevAggregator( | |
61 | Boolean isSampleStdDev, List<Expression> arguments, ExprCoreType returnType) { | |
62 | super( | |
63 |
1
1. <init> : negated conditional → SURVIVED |
isSampleStdDev |
64 | ? BuiltinFunctionName.STDDEV_SAMP.getName() | |
65 | : BuiltinFunctionName.STDDEV_POP.getName(), | |
66 | arguments, | |
67 | returnType); | |
68 | this.isSampleStdDev = isSampleStdDev; | |
69 | } | |
70 | ||
71 | @Override | |
72 | public StdDevAggregator.StdDevState create() { | |
73 |
1
1. create : replaced return value with null for org/opensearch/sql/expression/aggregation/StdDevAggregator::create → KILLED |
return new StdDevAggregator.StdDevState(isSampleStdDev); |
74 | } | |
75 | ||
76 | @Override | |
77 | protected StdDevAggregator.StdDevState iterate(ExprValue value, | |
78 | StdDevAggregator.StdDevState state) { | |
79 |
1
1. iterate : removed call to org/opensearch/sql/expression/aggregation/StdDevAggregator$StdDevState::evaluate → KILLED |
state.evaluate(value); |
80 |
1
1. iterate : replaced return value with null for org/opensearch/sql/expression/aggregation/StdDevAggregator::iterate → SURVIVED |
return state; |
81 | } | |
82 | ||
83 | @Override | |
84 | public String toString() { | |
85 |
1
1. toString : replaced return value with "" for org/opensearch/sql/expression/aggregation/StdDevAggregator::toString → KILLED |
return StringUtils.format( |
86 |
1
1. toString : negated conditional → KILLED |
"%s(%s)", isSampleStdDev ? "stddev_samp" : "stddev_pop", format(getArguments())); |
87 | } | |
88 | ||
89 | protected static class StdDevState implements AggregationState { | |
90 | ||
91 | private final StandardDeviation standardDeviation; | |
92 | ||
93 | private final List<Double> values = new ArrayList<>(); | |
94 | ||
95 | public StdDevState(boolean isSampleStdDev) { | |
96 | this.standardDeviation = new StandardDeviation(isSampleStdDev); | |
97 | } | |
98 | ||
99 | public void evaluate(ExprValue value) { | |
100 | values.add(value.doubleValue()); | |
101 | } | |
102 | ||
103 | @Override | |
104 | public ExprValue result() { | |
105 |
2
1. result : negated conditional → KILLED 2. result : replaced return value with null for org/opensearch/sql/expression/aggregation/StdDevAggregator$StdDevState::result → KILLED |
return values.size() == 0 |
106 | ? ExprNullValue.of() | |
107 |
1
1. lambda$result$0 : replaced double return with 0.0d for org/opensearch/sql/expression/aggregation/StdDevAggregator$StdDevState::lambda$result$0 → KILLED |
: doubleValue(standardDeviation.evaluate(values.stream().mapToDouble(d -> d).toArray())); |
108 | } | |
109 | } | |
110 | } | |
Mutations | ||
41 |
1.1 |
|
49 |
1.1 |
|
63 |
1.1 |
|
73 |
1.1 |
|
79 |
1.1 |
|
80 |
1.1 |
|
85 |
1.1 |
|
86 |
1.1 |
|
105 |
1.1 2.2 |
|
107 |
1.1 |