SOLR-13391: Add variance and standard deviation stream evaluators

Squashed commit of the following:

commit 406d4b959a42e4830ac1c52836ccbcfc1b614b46
Author: Nazerke <seinaz1997@gmail.com>
Date:   Fri Apr 12 14:03:34 2019 +0200

    added missing package

commit 32c239687c39c5da3e4f2d0f25df73127331fa99
Author: Nazerke <seinaz1997@gmail.com>
Date:   Fri Apr 12 14:03:14 2019 +0200

    added package

commit 7b3f9bd415002969a4ec5d87a9ffbfd6fcff6e92
Author: Nazerke <seinaz1997@gmail.com>
Date:   Fri Apr 12 14:02:28 2019 +0200

    added var and stddev functions

commit 77c4f9fdd9f111862a55b645aad960457291414c
Author: Nazerke <seinaz1997@gmail.com>
Date:   Fri Apr 12 14:00:59 2019 +0200

    added test for the variance and standard deviation stream evaluators

commit 2d9692c178590b65e46cfd9e04ca0384c7d39ec5
Author: naz <nazerke.seidan@cloudera.com>
Date:   Wed Apr 10 19:50:30 2019 +0200

    added var and stddev new evaluators

commit d265225747bce9a0eabd713994ddd4990dbbbfa2
Author: naz <nazerke.seidan@cloudera.com>
Date:   Wed Apr 10 19:49:23 2019 +0200

    variance streaming evaluator

commit a3330064bb62b5723b9125334ef1d61fc3b098d3
Author: naz <nazerke.seidan@cloudera.com>
Date:   Wed Apr 10 19:49:02 2019 +0200

    standard deviation streaming evaluator
This commit is contained in:
Joel Bernstein 2019-04-12 09:37:50 -04:00
parent 03f5a5e7a1
commit 58001bfc87
5 changed files with 139 additions and 1 deletions

View File

@ -172,6 +172,8 @@ public class Lang {
.withFunctionName("constantDistribution", ConstantDistributionEvaluator.class)
.withFunctionName("weibullDistribution", WeibullDistributionEvaluator.class)
.withFunctionName("mean", MeanEvaluator.class)
.withFunctionName("var", VarianceEvaluator.class)
.withFunctionName("stddev", StandardDeviationEvaluator.class)
.withFunctionName("mode", ModeEvaluator.class)
.withFunctionName("logNormalDistribution", LogNormalDistributionEvaluator.class)
.withFunctionName("zipFDistribution", ZipFDistributionEvaluator.class)

View File

@ -0,0 +1,59 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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.apache.solr.client.solrj.io.eval;
import org.apache.solr.client.solrj.io.eval.OneValueWorker;
import org.apache.solr.client.solrj.io.eval.RecursiveObjectEvaluator;
import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
import org.apache.commons.math3.stat.StatUtils;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
public class StandardDeviationEvaluator extends RecursiveObjectEvaluator implements OneValueWorker {
protected static final long serialVersionUID = 1L;
public StandardDeviationEvaluator(StreamExpression expression, StreamFactory factory) throws IOException {
super(expression, factory);
if(1 != containedEvaluators.size()){
throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - expecting exactly 1 value but found %d",expression,containedEvaluators.size()));
}
}
@Override
public Object doWork(Object value) throws IOException{
if(null == value){
throw new IOException(String.format(Locale.ROOT, "Unable to find %s(...) because the value is null", constructingFactory.getFunctionName(getClass())));
}
else if(value instanceof List){
List<Number> c = (List<Number>) value;
double[] data = new double[c.size()];
for(int i=0; i< c.size(); i++) {
data[i] = c.get(i).doubleValue();
}
return Math.sqrt(StatUtils.variance(data));
}
else{
throw new IOException(String.format(Locale.ROOT, "Unable to find %s(...) because the value is not a collection, instead a %s was found", constructingFactory.getFunctionName(getClass()), value.getClass().getSimpleName()));
}
}
}

View File

@ -0,0 +1,59 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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.apache.solr.client.solrj.io.eval;
import org.apache.solr.client.solrj.io.eval.OneValueWorker;
import org.apache.solr.client.solrj.io.eval.RecursiveObjectEvaluator;
import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
import org.apache.commons.math3.stat.StatUtils;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
public class VarianceEvaluator extends RecursiveObjectEvaluator implements OneValueWorker {
protected static final long serialVersionUID = 1L;
public VarianceEvaluator(StreamExpression expression, StreamFactory factory) throws IOException {
super(expression, factory);
if(1 != containedEvaluators.size()){
throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - expecting exactly 1 value but found %d",expression,containedEvaluators.size()));
}
}
@Override
public Object doWork(Object value) throws IOException{
if(null == value){
throw new IOException(String.format(Locale.ROOT, "Unable to find %s(...) because the value is null", constructingFactory.getFunctionName(getClass())));
}
else if(value instanceof List){
List<Number> c = (List<Number>) value;
double[] data = new double[c.size()];
for(int i=0; i< c.size(); i++) {
data[i] = c.get(i).doubleValue();
}
return StatUtils.variance(data);
}
else{
throw new IOException(String.format(Locale.ROOT, "Unable to find %s(...) because the value is not a collection, instead a %s was found", constructingFactory.getFunctionName(getClass()), value.getClass().getSimpleName()));
}
}
}

View File

@ -75,7 +75,7 @@ public class TestLang extends SolrTestCase {
"convexHull", "getVertices", "getBaryCenter", "getArea", "getBoundarySize","oscillate",
"getAmplitude", "getPhase", "getAngularFrequency", "enclosingDisk", "getCenter", "getRadius",
"getSupportPoints", "pairSort", "log10", "plist", "recip", "pivot", "ltrim", "rtrim", "export",
"zplot", "natural", "repeat", "movingMAD", "hashRollup", "noop"};
"zplot", "natural", "repeat", "movingMAD", "hashRollup", "noop", "var", "stddev"};
@Test
public void testLang() {

View File

@ -3357,6 +3357,24 @@ public class MathExpressionTest extends SolrCloudTestCase {
assertTrue(dotProduct.doubleValue() == 182);
}
@Test
public void testVarianceAndStandardDeviation() throws Exception {
String cexpr = "let(echo=true,a=var(array(1,2,3,4,5)),b=stddev(array(2,2,2,2)))";
ModifiableSolrParams paramsLoc = new ModifiableSolrParams();
paramsLoc.set("expr", cexpr);
paramsLoc.set("qt", "/stream");
String url = cluster.getJettySolrRunners().get(0).getBaseUrl().toString()+"/"+COLLECTIONORALIAS;
TupleStream solrStream = new SolrStream(url, paramsLoc);
StreamContext context = new StreamContext();
solrStream.setStreamContext(context);
List<Tuple> tuples = getTuples(solrStream);
assertTrue(tuples.size() == 1);
Number variance = (Number)tuples.get(0).get("a");
assertTrue(variance.doubleValue() == 2.5);
Number stddev = (Number)tuples.get(0).get("b");
assertTrue(stddev.doubleValue() == 0);
}
@Test
public void testCache() throws Exception {
String cexpr = "putCache(space1, key1, dotProduct(array(2,4,6,8,10,12),array(1,2,3,4,5,6)))";