SOLR-13104: Add natural and repeat Stream Evaluators

This commit is contained in:
Joel Bernstein 2019-01-13 13:13:33 -05:00
parent 292e26bc2d
commit 570d573c00
5 changed files with 144 additions and 1 deletions

View File

@ -279,6 +279,8 @@ public class Lang {
.withFunctionName("pivot", PivotEvaluator.class)
.withFunctionName("ltrim", LeftShiftEvaluator.class)
.withFunctionName("rtrim", RightShiftEvaluator.class)
.withFunctionName("repeat", RepeatEvaluator.class)
.withFunctionName("natural", NaturalEvaluator.class)
// Boolean Stream Evaluators

View File

@ -0,0 +1,47 @@
/*
* 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 java.util.Locale;
import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
public class NaturalEvaluator extends RecursiveNumericEvaluator implements OneValueWorker {
protected static final long serialVersionUID = 1L;
public NaturalEvaluator(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){
int natural = ((Number)value).intValue();
List<Number> naturals = new ArrayList();
for(int i=0; i<natural; i++) {
naturals.add(i);
}
return naturals;
}
}

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.ArrayList;
import java.util.List;
import java.util.Locale;
import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
public class RepeatEvaluator extends RecursiveNumericEvaluator implements TwoValueWorker {
protected static final long serialVersionUID = 1L;
public RepeatEvaluator(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 values but found %d",expression,containedEvaluators.size()));
}
}
@Override
public Object doWork(Object value1, Object value2){
double d = ((Number)value1).doubleValue();
int size = ((Number)value2).intValue();
List<Number> repeated = new ArrayList();
for(int i=0; i<size; i++) {
repeated.add(d);
}
return repeated;
}
}

View File

@ -74,7 +74,7 @@ public class TestLang extends LuceneTestCase {
"convexHull", "getVertices", "getBaryCenter", "getArea", "getBoundarySize","oscillate",
"getAmplitude", "getPhase", "getAngularFrequency", "enclosingDisk", "getCenter", "getRadius",
"getSupportPoints", "pairSort", "log10", "plist", "recip", "pivot", "ltrim", "rtrim", "export",
"zplot"};
"zplot", "natural", "repeat"};
@Test
public void testLang() {

View File

@ -1225,6 +1225,52 @@ public class MathExpressionTest extends SolrCloudTestCase {
assertEquals(out.get(5).intValue(), 1);
}
@Test
public void testNatural() throws Exception {
String cexpr = "natural(6)";
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");
assertEquals(out.size(), 6);
assertEquals(out.get(0).intValue(), 0);
assertEquals(out.get(1).intValue(), 1);
assertEquals(out.get(2).intValue(), 2);
assertEquals(out.get(3).intValue(), 3);
assertEquals(out.get(4).intValue(), 4);
assertEquals(out.get(5).intValue(), 5);
}
@Test
public void testRepeat() throws Exception {
String cexpr = "repeat(6.5, 6)";
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");
assertEquals(out.size(), 6);
assertEquals(out.get(0).doubleValue(), 6.5, 0);
assertEquals(out.get(1).doubleValue(), 6.5, 0);
assertEquals(out.get(2).doubleValue(), 6.5, 0);
assertEquals(out.get(3).doubleValue(), 6.5, 0);
assertEquals(out.get(4).doubleValue(), 6.5, 0);
assertEquals(out.get(5).doubleValue(), 6.5, 0);
}
@Test
public void testLtrim() throws Exception {
String cexpr = "ltrim(array(1,2,3,4,5,6), 2)";