SOLR-11401: Add zipFDistribution Stream Evaluator

This commit is contained in:
Joel Bernstein 2017-09-27 21:40:27 -04:00
parent 26677ab2b0
commit 790533da12
3 changed files with 76 additions and 0 deletions

View File

@ -305,6 +305,7 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware,
.withFunctionName("mean", MeanEvaluator.class)
.withFunctionName("mode", ModeEvaluator.class)
.withFunctionName("logNormalDistribution", LogNormalDistributionEvaluator.class)
.withFunctionName("zipFDistribution", ZipFDistributionEvaluator.class)
// Boolean Stream Evaluators

View File

@ -0,0 +1,48 @@
/*
* 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 java.util.Locale;
import org.apache.commons.math3.distribution.ZipfDistribution;
import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
public class ZipFDistributionEvaluator extends RecursiveNumericEvaluator implements TwoValueWorker {
private static final long serialVersionUID = 1;
public ZipFDistributionEvaluator(StreamExpression expression, StreamFactory factory) throws IOException {
super(expression, factory);
}
@Override
public Object doWork(Object first, Object second) throws IOException{
if(null == first){
throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - null found for the first value",toExpression(constructingFactory)));
}
if(null == second){
throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - null found for the second value",toExpression(constructingFactory)));
}
Number size = (Number)first;
Number exp = (Number)second;
return new ZipfDistribution(size.intValue(), exp.doubleValue());
}
}

View File

@ -6371,6 +6371,33 @@ public class StreamExpressionTest extends SolrCloudTestCase {
assertEquals(cprob.doubleValue(), 0.5, 0.0);
}
@Test
public void testZipFDistribution() throws Exception {
String cexpr = "let(a=sample(zipFDistribution(10, 1), 50000), b=freqTable(a), c=col(b, count))";
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);
List<Number> counts = (List<Number>)tuples.get(0).get("c");
assertTrue(counts.size() == 10);
int lastCount = Integer.MAX_VALUE;
for(Number number : counts) {
int current = number.intValue();
if(current > lastCount) {
throw new Exception("Zipf distribution not descending!!!");
} else {
lastCount = current;
}
}
}
@Test
public void testEnumeratedDistribution() throws Exception {
String cexpr = "let(a=uniformIntegerDistribution(1, 10)," +