From 99ca13f90fa04fb804e20bbdf51bf6cfc4ea3a34 Mon Sep 17 00:00:00 2001 From: Joel Bernstein Date: Fri, 2 Jun 2017 15:19:38 -0400 Subject: [PATCH] SOLR-10696: Add cumulative probability function --- .../apache/solr/handler/StreamHandler.java | 1 + .../eval/CumulativeProbabilityEvaluator.java | 66 +++++++++++++++++++ .../solrj/io/eval/PercentileEvaluator.java | 24 +++++-- .../solrj/io/stream/StreamExpressionTest.java | 65 ++++++++++++++++-- 4 files changed, 143 insertions(+), 13 deletions(-) create mode 100644 solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/CumulativeProbabilityEvaluator.java diff --git a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java b/solr/core/src/java/org/apache/solr/handler/StreamHandler.java index f79f86ee05e..d4029ffef72 100644 --- a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java +++ b/solr/core/src/java/org/apache/solr/handler/StreamHandler.java @@ -187,6 +187,7 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware, .withFunctionName("copyOfRange", CopyOfRangeEvaluator.class) .withFunctionName("percentile", PercentileEvaluator.class) .withFunctionName("empiricalDistribution", EmpiricalDistributionEvaluator.class) + .withFunctionName("cumulativeProbability", CumulativeProbabilityEvaluator.class) .withFunctionName("describe", DescribeEvaluator.class) .withFunctionName("finddelay", FindDelayEvaluator.class) .withFunctionName("sequence", SequenceEvaluator.class) diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/CumulativeProbabilityEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/CumulativeProbabilityEvaluator.java new file mode 100644 index 00000000000..469e983bd59 --- /dev/null +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/CumulativeProbabilityEvaluator.java @@ -0,0 +1,66 @@ +/* + * 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 java.io.IOException; + +import org.apache.solr.client.solrj.io.Tuple; +import org.apache.solr.client.solrj.io.stream.expr.Explanation; +import org.apache.solr.client.solrj.io.stream.expr.Explanation.ExpressionType; +import org.apache.solr.client.solrj.io.stream.expr.Expressible; +import org.apache.solr.client.solrj.io.stream.expr.StreamExpression; +import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionParameter; +import org.apache.solr.client.solrj.io.stream.expr.StreamFactory; + +public class CumulativeProbabilityEvaluator extends ComplexEvaluator implements Expressible { + + private static final long serialVersionUID = 1; + + public CumulativeProbabilityEvaluator(StreamExpression expression, StreamFactory factory) throws IOException { + super(expression, factory); + } + + public Number evaluate(Tuple tuple) throws IOException { + + if(subEvaluators.size() != 2) { + throw new IOException("Cumulative probability expects 2 parameters: an emperical distribution and a number"); + } + + StreamEvaluator r = subEvaluators.get(0); + StreamEvaluator d = subEvaluators.get(1); + + EmpiricalDistributionEvaluator.EmpiricalDistributionTuple e = (EmpiricalDistributionEvaluator.EmpiricalDistributionTuple)r.evaluate(tuple); + Number n = (Number)d.evaluate(tuple); + return e.percentile(n.doubleValue()); + } + + @Override + public StreamExpressionParameter toExpression(StreamFactory factory) throws IOException { + StreamExpression expression = new StreamExpression(factory.getFunctionName(getClass())); + return expression; + } + + @Override + public Explanation toExplanation(StreamFactory factory) throws IOException { + return new Explanation(nodeId.toString()) + .withExpressionType(ExpressionType.EVALUATOR) + .withFunctionName(factory.getFunctionName(getClass())) + .withImplementingClass(getClass().getName()) + .withExpression(toExpression(factory).toString()); + } +} \ No newline at end of file diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PercentileEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PercentileEvaluator.java index 19d423d22c2..786704841cb 100644 --- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PercentileEvaluator.java +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PercentileEvaluator.java @@ -17,6 +17,9 @@ package org.apache.solr.client.solrj.io.eval; +import java.text.DecimalFormat; +import java.text.NumberFormat; +import java.util.List; import java.io.IOException; import org.apache.solr.client.solrj.io.Tuple; @@ -26,6 +29,7 @@ import org.apache.solr.client.solrj.io.stream.expr.Expressible; import org.apache.solr.client.solrj.io.stream.expr.StreamExpression; import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionParameter; import org.apache.solr.client.solrj.io.stream.expr.StreamFactory; +import org.apache.commons.math3.stat.descriptive.rank.Percentile; public class PercentileEvaluator extends ComplexEvaluator implements Expressible { @@ -36,17 +40,23 @@ public class PercentileEvaluator extends ComplexEvaluator implements Expressible } public Number evaluate(Tuple tuple) throws IOException { - if(subEvaluators.size() != 2) { - throw new IOException("Percentile expects 2 parameters: a regression result and a number"); + throw new IOException("Percentile expects 2 parameters: an array and a number"); } - StreamEvaluator r = subEvaluators.get(0); - StreamEvaluator d = subEvaluators.get(1); + StreamEvaluator colEval = subEvaluators.get(0); + List column = (List)colEval.evaluate(tuple); - EmpiricalDistributionEvaluator.EmpiricalDistributionTuple e = (EmpiricalDistributionEvaluator.EmpiricalDistributionTuple)r.evaluate(tuple); - Number n = (Number)d.evaluate(tuple); - return e.percentile(n.doubleValue()); + double[] data = new double[column.size()]; + for(int i=0; i tuples = getTuples(solrStream); + assertTrue(tuples.size() == 1); + Tuple tuple = tuples.get(0); + double p = tuple.getDouble("return-value"); + assertEquals(p, 6, 0.0); + + + cexpr = "percentile(array(11,10,3,4,5,6,7,8,9,2,1), 50)"; + paramsLoc = new ModifiableSolrParams(); + paramsLoc.set("expr", cexpr); + paramsLoc.set("qt", "/stream"); + + solrStream = new SolrStream(url, paramsLoc); + + context = new StreamContext(); + solrStream.setStreamContext(context); + tuples = getTuples(solrStream); + assertTrue(tuples.size() == 1); + tuple = tuples.get(0); + p = tuple.getDouble("return-value"); + assertEquals(p, 6, 0.0); + + cexpr = "percentile(array(11,10,3,4,5,6,7,8,9,2,1), 20)"; + paramsLoc = new ModifiableSolrParams(); + paramsLoc.set("expr", cexpr); + paramsLoc.set("qt", "/stream"); + + solrStream = new SolrStream(url, paramsLoc); + + context = new StreamContext(); + solrStream.setStreamContext(context); + tuples = getTuples(solrStream); + assertTrue(tuples.size() == 1); + tuple = tuples.get(0); + p = tuple.getDouble("return-value"); + assertEquals(p, 2.4, 0.001); + + } + + + @Test + public void testCumulativeProbability() throws Exception { UpdateRequest updateRequest = new UpdateRequest(); int i=0; @@ -5773,11 +5826,11 @@ public class StreamExpressionTest extends SolrCloudTestCase { String expr = "search("+COLLECTIONORALIAS+", q=\"*:*\", fl=\"price_f\", sort=\"price_f asc\", rows=\"200\")"; String cexpr = "let(a="+expr+", c=col(a, price_f), e=empiricalDistribution(c), " + - "tuple(p1=percentile(e, 88), " + - "p2=percentile(e, 2), " + - "p3=percentile(e, 99), " + - "p4=percentile(e, 77), " + - "p5=percentile(e, 98)))"; + "tuple(p1=cumulativeProbability(e, 88), " + + "p2=cumulativeProbability(e, 2), " + + "p3=cumulativeProbability(e, 99), " + + "p4=cumulativeProbability(e, 77), " + + "p5=cumulativeProbability(e, 98)))"; ModifiableSolrParams paramsLoc = new ModifiableSolrParams(); paramsLoc.set("expr", cexpr);