Apache Lucene open-source search software
SOLR-13104: Add natural and repeat Stream Evaluators
Joel Bernstein570d573c0049
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/Lang.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/Lang.javaindex a1a796d1ecf..9229dcf9370 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/Lang.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/Lang.java@@ -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 Evaluatorsdiff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/NaturalEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/NaturalEvaluator.javanew file mode 100644index 00000000000..f8469eea2a6--- /dev/null+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/NaturalEvaluator.java@@ -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;+ }+}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/RepeatEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/RepeatEvaluator.javanew file mode 100644index 00000000000..c2f8aecc99c--- /dev/null+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/RepeatEvaluator.java@@ -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;+ }+}diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/io/TestLang.java b/solr/solrj/src/test/org/apache/solr/client/solrj/io/TestLang.javaindex b5b731717e5..2632e1482b0 100644--- a/solr/solrj/src/test/org/apache/solr/client/solrj/io/TestLang.java+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/io/TestLang.java@@ -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"};@Testpublic void testLang() {diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/MathExpressionTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/MathExpressionTest.javaindex 6c61ad7452d..912138782c2 100644--- a/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/MathExpressionTest.java+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/MathExpressionTest.java@@ -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);+ }++@Testpublic void testLtrim() throws Exception {String cexpr = "ltrim(array(1,2,3,4,5,6), 2)";