You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by jb...@apache.org on 2019/04/12 14:02:35 UTC

[lucene-solr] branch branch_8x updated: SOLR-13391: Add variance and standard deviation stream evaluators Squashed commit of the following:

This is an automated email from the ASF dual-hosted git repository.

jbernste pushed a commit to branch branch_8x
in repository https://gitbox.apache.org/repos/asf/lucene-solr.git


The following commit(s) were added to refs/heads/branch_8x by this push:
     new 4f6e782  SOLR-13391: Add variance and standard deviation stream evaluators Squashed commit of the following:
4f6e782 is described below

commit 4f6e78282f797dd327b8b154e742e92b77a2de09
Author: Joel Bernstein <jb...@apache.org>
AuthorDate: Fri Apr 12 09:37:50 2019 -0400

    SOLR-13391: Add variance and standard deviation stream evaluators
    Squashed commit of the following:
    
    commit 406d4b959a42e4830ac1c52836ccbcfc1b614b46
    Author: Nazerke <se...@gmail.com>
    Date:   Fri Apr 12 14:03:34 2019 +0200
    
        added missing package
    
    commit 32c239687c39c5da3e4f2d0f25df73127331fa99
    Author: Nazerke <se...@gmail.com>
    Date:   Fri Apr 12 14:03:14 2019 +0200
    
        added package
    
    commit 7b3f9bd415002969a4ec5d87a9ffbfd6fcff6e92
    Author: Nazerke <se...@gmail.com>
    Date:   Fri Apr 12 14:02:28 2019 +0200
    
        added var and stddev functions
    
    commit 77c4f9fdd9f111862a55b645aad960457291414c
    Author: Nazerke <se...@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 <na...@cloudera.com>
    Date:   Wed Apr 10 19:50:30 2019 +0200
    
        added var and stddev new evaluators
    
    commit d265225747bce9a0eabd713994ddd4990dbbbfa2
    Author: naz <na...@cloudera.com>
    Date:   Wed Apr 10 19:49:23 2019 +0200
    
        variance streaming evaluator
    
    commit a3330064bb62b5723b9125334ef1d61fc3b098d3
    Author: naz <na...@cloudera.com>
    Date:   Wed Apr 10 19:49:02 2019 +0200
    
        standard deviation streaming evaluator
---
 .../java/org/apache/solr/client/solrj/io/Lang.java |  2 +
 .../solrj/io/eval/StandardDeviationEvaluator.java  | 59 ++++++++++++++++++++++
 .../client/solrj/io/eval/VarianceEvaluator.java    | 59 ++++++++++++++++++++++
 .../org/apache/solr/client/solrj/io/TestLang.java  |  2 +-
 .../client/solrj/io/stream/MathExpressionTest.java | 18 +++++++
 5 files changed, 139 insertions(+), 1 deletion(-)

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.java
index ab14062..e00eeef 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.java
new file mode 100644
index 0000000..83132d0
--- /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 file
diff --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.java
new file mode 100644
index 0000000..e7a3f11
--- /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 file
diff --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.java
index b43e963..4df1aca 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"};
 
   @Test
   public 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.java
index 6c7387b..f38b90e 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
@@ -3358,6 +3358,24 @@ public class MathExpressionTest extends SolrCloudTestCase {
   }
 
   @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)))";
     ModifiableSolrParams paramsLoc = new ModifiableSolrParams();