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.Variance; | |
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 | * Variance Aggregator. | |
31 | */ | |
32 | public class VarianceAggregator extends Aggregator<VarianceAggregator.VarianceState> { | |
33 | ||
34 | private final boolean isSampleVariance; | |
35 | ||
36 | /** | |
37 | * Build Population Variance {@link VarianceAggregator}. | |
38 | */ | |
39 | public static Aggregator variancePopulation(List<Expression> arguments, | |
40 | ExprCoreType returnType) { | |
41 |
1
1. variancePopulation : replaced return value with null for org/opensearch/sql/expression/aggregation/VarianceAggregator::variancePopulation → KILLED |
return new VarianceAggregator(false, arguments, returnType); |
42 | } | |
43 | ||
44 | /** | |
45 | * Build Sample Variance {@link VarianceAggregator}. | |
46 | */ | |
47 | public static Aggregator varianceSample(List<Expression> arguments, | |
48 | ExprCoreType returnType) { | |
49 |
1
1. varianceSample : replaced return value with null for org/opensearch/sql/expression/aggregation/VarianceAggregator::varianceSample → KILLED |
return new VarianceAggregator(true, arguments, returnType); |
50 | } | |
51 | ||
52 | /** | |
53 | * VarianceAggregator constructor. | |
54 | * | |
55 | * @param isSampleVariance true for sample variance aggregator, false for population variance | |
56 | * aggregator. | |
57 | * @param arguments aggregator arguments. | |
58 | * @param returnType aggregator return types. | |
59 | */ | |
60 | public VarianceAggregator( | |
61 | Boolean isSampleVariance, List<Expression> arguments, ExprCoreType returnType) { | |
62 | super( | |
63 |
1
1. <init> : negated conditional → SURVIVED |
isSampleVariance |
64 | ? BuiltinFunctionName.VARSAMP.getName() | |
65 | : BuiltinFunctionName.VARPOP.getName(), | |
66 | arguments, | |
67 | returnType); | |
68 | this.isSampleVariance = isSampleVariance; | |
69 | } | |
70 | ||
71 | @Override | |
72 | public VarianceState create() { | |
73 |
1
1. create : replaced return value with null for org/opensearch/sql/expression/aggregation/VarianceAggregator::create → KILLED |
return new VarianceState(isSampleVariance); |
74 | } | |
75 | ||
76 | @Override | |
77 | protected VarianceState iterate(ExprValue value, VarianceState state) { | |
78 |
1
1. iterate : removed call to org/opensearch/sql/expression/aggregation/VarianceAggregator$VarianceState::evaluate → KILLED |
state.evaluate(value); |
79 |
1
1. iterate : replaced return value with null for org/opensearch/sql/expression/aggregation/VarianceAggregator::iterate → SURVIVED |
return state; |
80 | } | |
81 | ||
82 | @Override | |
83 | public String toString() { | |
84 |
1
1. toString : replaced return value with "" for org/opensearch/sql/expression/aggregation/VarianceAggregator::toString → KILLED |
return StringUtils.format( |
85 |
1
1. toString : negated conditional → KILLED |
"%s(%s)", isSampleVariance ? "var_samp" : "var_pop", format(getArguments())); |
86 | } | |
87 | ||
88 | protected static class VarianceState implements AggregationState { | |
89 | ||
90 | private final Variance variance; | |
91 | ||
92 | private final List<Double> values = new ArrayList<>(); | |
93 | ||
94 | public VarianceState(boolean isSampleVariance) { | |
95 | this.variance = new Variance(isSampleVariance); | |
96 | } | |
97 | ||
98 | public void evaluate(ExprValue value) { | |
99 | values.add(value.doubleValue()); | |
100 | } | |
101 | ||
102 | @Override | |
103 | public ExprValue result() { | |
104 |
2
1. result : negated conditional → KILLED 2. result : replaced return value with null for org/opensearch/sql/expression/aggregation/VarianceAggregator$VarianceState::result → KILLED |
return values.size() == 0 |
105 | ? ExprNullValue.of() | |
106 |
1
1. lambda$result$0 : replaced double return with 0.0d for org/opensearch/sql/expression/aggregation/VarianceAggregator$VarianceState::lambda$result$0 → KILLED |
: doubleValue(variance.evaluate(values.stream().mapToDouble(d -> d).toArray())); |
107 | } | |
108 | } | |
109 | } | |
Mutations | ||
41 |
1.1 |
|
49 |
1.1 |
|
63 |
1.1 |
|
73 |
1.1 |
|
78 |
1.1 |
|
79 |
1.1 |
|
84 |
1.1 |
|
85 |
1.1 |
|
104 |
1.1 2.2 |
|
106 |
1.1 |