-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Impl stddev and variance function in SQL and PPL (#115)
* impl variance frontend and backend * Support construct AggregationResponseParser during Aggregator build stage * add var and varp for PPL Signed-off-by: penghuo <penghuo@gmail.com> * add UT Signed-off-by: penghuo <penghuo@gmail.com> * fix UT Signed-off-by: penghuo <penghuo@gmail.com> * fix doc format Signed-off-by: penghuo <penghuo@gmail.com> * fix doc format Signed-off-by: penghuo <penghuo@gmail.com> * fix the doc Signed-off-by: penghuo <penghuo@gmail.com> * add stddev_samp and stddev_pop Signed-off-by: penghuo <penghuo@gmail.com> * fix UT coverage * address comments Signed-off-by: penghuo <penghuo@gmail.com>
- Loading branch information
Showing
24 changed files
with
1,414 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
core/src/main/java/org/opensearch/sql/expression/aggregation/StdDevAggregator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package org.opensearch.sql.expression.aggregation; | ||
|
||
import static org.opensearch.sql.data.model.ExprValueUtils.doubleValue; | ||
import static org.opensearch.sql.utils.ExpressionUtils.format; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.apache.commons.math3.stat.descriptive.moment.StandardDeviation; | ||
import org.opensearch.sql.common.utils.StringUtils; | ||
import org.opensearch.sql.data.model.ExprNullValue; | ||
import org.opensearch.sql.data.model.ExprValue; | ||
import org.opensearch.sql.data.type.ExprCoreType; | ||
import org.opensearch.sql.expression.Expression; | ||
import org.opensearch.sql.expression.function.BuiltinFunctionName; | ||
|
||
/** | ||
* StandardDeviation Aggregator. | ||
*/ | ||
public class StdDevAggregator extends Aggregator<StdDevAggregator.StdDevState> { | ||
|
||
private final boolean isSampleStdDev; | ||
|
||
/** | ||
* Build Population Variance {@link VarianceAggregator}. | ||
*/ | ||
public static Aggregator stddevPopulation(List<Expression> arguments, | ||
ExprCoreType returnType) { | ||
return new StdDevAggregator(false, arguments, returnType); | ||
} | ||
|
||
/** | ||
* Build Sample Variance {@link VarianceAggregator}. | ||
*/ | ||
public static Aggregator stddevSample(List<Expression> arguments, | ||
ExprCoreType returnType) { | ||
return new StdDevAggregator(true, arguments, returnType); | ||
} | ||
|
||
/** | ||
* VarianceAggregator constructor. | ||
* | ||
* @param isSampleStdDev true for sample standard deviation aggregator, false for population | ||
* standard deviation aggregator. | ||
* @param arguments aggregator arguments. | ||
* @param returnType aggregator return types. | ||
*/ | ||
public StdDevAggregator( | ||
Boolean isSampleStdDev, List<Expression> arguments, ExprCoreType returnType) { | ||
super( | ||
isSampleStdDev | ||
? BuiltinFunctionName.STDDEV_SAMP.getName() | ||
: BuiltinFunctionName.STDDEV_POP.getName(), | ||
arguments, | ||
returnType); | ||
this.isSampleStdDev = isSampleStdDev; | ||
} | ||
|
||
@Override | ||
public StdDevAggregator.StdDevState create() { | ||
return new StdDevAggregator.StdDevState(isSampleStdDev); | ||
} | ||
|
||
@Override | ||
protected StdDevAggregator.StdDevState iterate(ExprValue value, | ||
StdDevAggregator.StdDevState state) { | ||
state.evaluate(value); | ||
return state; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return StringUtils.format( | ||
"%s(%s)", isSampleStdDev ? "stddev_samp" : "stddev_pop", format(getArguments())); | ||
} | ||
|
||
protected static class StdDevState implements AggregationState { | ||
|
||
private final StandardDeviation standardDeviation; | ||
|
||
private final List<Double> values = new ArrayList<>(); | ||
|
||
public StdDevState(boolean isSampleStdDev) { | ||
this.standardDeviation = new StandardDeviation(isSampleStdDev); | ||
} | ||
|
||
public void evaluate(ExprValue value) { | ||
values.add(value.doubleValue()); | ||
} | ||
|
||
@Override | ||
public ExprValue result() { | ||
return values.size() == 0 | ||
? ExprNullValue.of() | ||
: doubleValue(standardDeviation.evaluate(values.stream().mapToDouble(d -> d).toArray())); | ||
} | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
core/src/main/java/org/opensearch/sql/expression/aggregation/VarianceAggregator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package org.opensearch.sql.expression.aggregation; | ||
|
||
import static org.opensearch.sql.data.model.ExprValueUtils.doubleValue; | ||
import static org.opensearch.sql.utils.ExpressionUtils.format; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.apache.commons.math3.stat.descriptive.moment.Variance; | ||
import org.opensearch.sql.common.utils.StringUtils; | ||
import org.opensearch.sql.data.model.ExprNullValue; | ||
import org.opensearch.sql.data.model.ExprValue; | ||
import org.opensearch.sql.data.type.ExprCoreType; | ||
import org.opensearch.sql.expression.Expression; | ||
import org.opensearch.sql.expression.function.BuiltinFunctionName; | ||
|
||
/** | ||
* Variance Aggregator. | ||
*/ | ||
public class VarianceAggregator extends Aggregator<VarianceAggregator.VarianceState> { | ||
|
||
private final boolean isSampleVariance; | ||
|
||
/** | ||
* Build Population Variance {@link VarianceAggregator}. | ||
*/ | ||
public static Aggregator variancePopulation(List<Expression> arguments, | ||
ExprCoreType returnType) { | ||
return new VarianceAggregator(false, arguments, returnType); | ||
} | ||
|
||
/** | ||
* Build Sample Variance {@link VarianceAggregator}. | ||
*/ | ||
public static Aggregator varianceSample(List<Expression> arguments, | ||
ExprCoreType returnType) { | ||
return new VarianceAggregator(true, arguments, returnType); | ||
} | ||
|
||
/** | ||
* VarianceAggregator constructor. | ||
* | ||
* @param isSampleVariance true for sample variance aggregator, false for population variance | ||
* aggregator. | ||
* @param arguments aggregator arguments. | ||
* @param returnType aggregator return types. | ||
*/ | ||
public VarianceAggregator( | ||
Boolean isSampleVariance, List<Expression> arguments, ExprCoreType returnType) { | ||
super( | ||
isSampleVariance | ||
? BuiltinFunctionName.VARSAMP.getName() | ||
: BuiltinFunctionName.VARPOP.getName(), | ||
arguments, | ||
returnType); | ||
this.isSampleVariance = isSampleVariance; | ||
} | ||
|
||
@Override | ||
public VarianceState create() { | ||
return new VarianceState(isSampleVariance); | ||
} | ||
|
||
@Override | ||
protected VarianceState iterate(ExprValue value, VarianceState state) { | ||
state.evaluate(value); | ||
return state; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return StringUtils.format( | ||
"%s(%s)", isSampleVariance ? "var_samp" : "var_pop", format(getArguments())); | ||
} | ||
|
||
protected static class VarianceState implements AggregationState { | ||
|
||
private final Variance variance; | ||
|
||
private final List<Double> values = new ArrayList<>(); | ||
|
||
public VarianceState(boolean isSampleVariance) { | ||
this.variance = new Variance(isSampleVariance); | ||
} | ||
|
||
public void evaluate(ExprValue value) { | ||
values.add(value.doubleValue()); | ||
} | ||
|
||
@Override | ||
public ExprValue result() { | ||
return values.size() == 0 | ||
? ExprNullValue.of() | ||
: doubleValue(variance.evaluate(values.stream().mapToDouble(d -> d).toArray())); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.