SOLR-11019: Add addAll Stream Evaluator

This commit is contained in:
Joel Bernstein 2017-07-06 15:21:07 -04:00
parent d81daf54b4
commit 01d0ce3258
3 changed files with 96 additions and 0 deletions

View File

@ -216,6 +216,7 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware,
.withFunctionName("rev", ReverseEvaluator.class)
.withFunctionName("scale", ScaleEvaluator.class)
.withFunctionName("sequence", SequenceEvaluator.class)
.withFunctionName("addAll", AddAllEvaluator.class)
// Boolean Stream Evaluators

View File

@ -0,0 +1,69 @@
/*
* 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.ArrayList;
import java.util.List;
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 AddAllEvaluator extends ComplexEvaluator implements Expressible {
private static final long serialVersionUID = 1;
public AddAllEvaluator(StreamExpression expression, StreamFactory factory) throws IOException {
super(expression, factory);
if(subEvaluators.size() == 0) {
throw new IOException("addAll evaluator expects atleast one array parameter");
}
}
public List<Number> evaluate(Tuple tuple) throws IOException {
List<Number> all = new ArrayList();
for(StreamEvaluator subEvaluator : subEvaluators) {
List<Number> numbers = (List<Number>)subEvaluator.evaluate(tuple);
all.addAll(numbers);
}
return all;
}
@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());
}
}

View File

@ -5995,6 +5995,32 @@ public class StreamExpressionTest extends SolrCloudTestCase {
assertTrue(out.get(5).doubleValue() == 500.23D);
}
@Test
public void testAddAll() throws Exception {
String cexpr = "addAll(array(1, 2, 3), array(4.5, 5.5, 6.5), array(7,8,9))";
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> out = (List<Number>)tuples.get(0).get("return-value");
assertTrue(out.size() == 9);
assertTrue(out.get(0).intValue() == 1);
assertTrue(out.get(1).intValue() == 2);
assertTrue(out.get(2).intValue() == 3);
assertTrue(out.get(3).doubleValue() == 4.5D);
assertTrue(out.get(4).doubleValue() == 5.5D);
assertTrue(out.get(5).doubleValue() == 6.5D);
assertTrue(out.get(6).intValue() == 7);
assertTrue(out.get(7).intValue() == 8);
assertTrue(out.get(8).intValue() == 9);
}
@Test
public void testAnova() throws Exception {
String cexpr = "anova(array(1,2,3,5,4,6), array(5,2,3,5,4,6), array(1,2,7,5,4,6))";