SOLR-11594: Add precision Stream Evaluator

This commit is contained in:
Joel Bernstein 2017-11-01 13:34:56 -04:00
parent 77e6e291bf
commit 6eea7f70a0
3 changed files with 73 additions and 0 deletions

View File

@ -272,6 +272,7 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware,
.withFunctionName("transpose", TransposeEvaluator.class)
.withFunctionName("unit", UnitEvaluator.class)
.withFunctionName("triangularDistribution", TriangularDistributionEvaluator.class)
.withFunctionName("precision", PrecisionEvaluator.class)
// Boolean Stream Evaluators

View File

@ -0,0 +1,51 @@
/*
* 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.List;
import java.util.Locale;
import java.util.stream.Collectors;
import org.apache.commons.math3.util.Precision;
import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
public class PrecisionEvaluator extends RecursiveNumericEvaluator implements TwoValueWorker {
protected static final long serialVersionUID = 1L;
public PrecisionEvaluator(StreamExpression expression, StreamFactory factory) throws IOException{
super(expression, factory);
if(2 != containedEvaluators.size()){
throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - expecting exactly 2 value but found %d",expression,containedEvaluators.size()));
}
}
@Override
public Object doWork(Object value, Object value2) {
if(null == value){
return null;
}
else if(value instanceof List){
return ((List<?>)value).stream().map(innerValue -> doWork(innerValue, ((Number)value2).intValue())).collect(Collectors.toList());
}
else{
return Precision.round(((Number)value).doubleValue(), ((Number)value2).intValue());
}
}
}

View File

@ -7089,7 +7089,28 @@ public class StreamExpressionTest extends SolrCloudTestCase {
assertEquals(row3.get(2).longValue(), 16);
}
@Test
public void testPrecision() throws Exception {
String cexpr = "let(echo=true, a=precision(array(1.44445, 1, 2.00006), 4), b=precision(1.44445, 4))";
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> nums = (List<Number>)tuples.get(0).get("a");
assertTrue(nums.size() == 3);
assertEquals(nums.get(0).doubleValue(), 1.4445, 0.0);
assertEquals(nums.get(1).doubleValue(), 1, 0.0);
assertEquals(nums.get(2).doubleValue(), 2.0001, 0.0);
double num = tuples.get(0).getDouble("b");
assertEquals(num, 1.4445, 0.0);
}
@Test
public void testMean() throws Exception {