Apache Lucene open-source search software
SOLR-13391: Add variance and standard deviation stream evaluators Squashed commit of the following:
Joel Bernstein58001bfc870a
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/Lang.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/Lang.javaindex ab140626e0a..e00eeef70af 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/Lang.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/Lang.java@@ -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)diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/StandardDeviationEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/StandardDeviationEvaluator.javanew file mode 100644index 00000000000..83132d09987--- /dev/null+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/StandardDeviationEvaluator.java@@ -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()));+ }+ }+}\ No newline at end of filediff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/VarianceEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/VarianceEvaluator.javanew file mode 100644index 00000000000..e7a3f11b7d6--- /dev/null+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/VarianceEvaluator.java@@ -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()));+ }+ }+}\ No newline at end of filediff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/io/TestLang.java b/solr/solrj/src/test/org/apache/solr/client/solrj/io/TestLang.javaindex b43e9630da0..4df1aca32d2 100644--- a/solr/solrj/src/test/org/apache/solr/client/solrj/io/TestLang.java+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/io/TestLang.java@@ -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"};@Testpublic void testLang() {diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/MathExpressionTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/MathExpressionTest.javaindex 6c7387b84e9..f38b90e4b80 100644--- a/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/MathExpressionTest.java+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/MathExpressionTest.java@@ -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);+ }+@Testpublic void testCache() throws Exception {String cexpr = "putCache(space1, key1, dotProduct(array(2,4,6,8,10,12),array(1,2,3,4,5,6)))";