Apache Lucene open-source search software
SOLR-11734: Add ones and zeros Stream Evaluators
Joel Bernstein2c487947e8cd
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 f0862db0e47..941c1a1a9f2 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@@ -242,8 +242,10 @@ public class Lang {.withFunctionName("earthMovers", EarthMoversEvaluator.class).withFunctionName("euclidean", EuclideanEvaluator.class).withFunctionName("chebyshev", ChebyshevEvaluator.class)+ .withFunctionName("ones", OnesEvaluator.class)+ .withFunctionName("zeros", ZerosEvaluator.class)- // Boolean Stream Evaluators+ // Boolean Stream Evaluators.withFunctionName("and", AndEvaluator.class).withFunctionName("eor", ExclusiveOrEvaluator.class)diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/OnesEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/OnesEvaluator.javanew file mode 100644index 00000000000..bef53fd2151--- /dev/null+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/OnesEvaluator.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 OnesEvaluator extends RecursiveNumericEvaluator implements OneValueWorker {+ protected static final long serialVersionUID = 1L;++ public OnesEvaluator(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 size = ((Number)value).intValue();+ List<Number> ones = new ArrayList();+ for(int i=0; i<size; i++) {+ ones.add(1);+ }+ return ones;+ }+}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ZerosEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ZerosEvaluator.javanew file mode 100644index 00000000000..c3791858a5f--- /dev/null+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ZerosEvaluator.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 ZerosEvaluator extends RecursiveNumericEvaluator implements OneValueWorker {+ protected static final long serialVersionUID = 1L;++ public ZerosEvaluator(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 size = ((Number)value).intValue();+ List<Number> ones = new ArrayList();+ for(int i=0; i<size; i++) {+ ones.add(0);+ }+ return ones;+ }+}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 5f1e4f0a0c5..1e824daeb31 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@@ -69,7 +69,7 @@ public class TestLang extends LuceneTestCase {TemporalEvaluatorDayOfQuarter.FUNCTION_NAME, "abs", "add", "div", "mult", "sub", "log", "pow","mod", "ceil", "floor", "sin", "asin", "sinh", "cos", "acos", "cosh", "tan", "atan", "tanh", "round", "sqrt","cbrt", "coalesce", "uuid", "if", "convert", "valueAt", "memset", "fft", "ifft", "euclidean","manhattan",- "earthMovers", "canberra", "chebyshev"};+ "earthMovers", "canberra", "chebyshev", "ones", "zeros"};@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 d63b9221547..94d5334fc98 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@@ -995,6 +995,51 @@ public class MathExpressionTest extends SolrCloudTestCase {assertTrue(out.get(5).doubleValue() == 500.23D);}++ @Test+ public void testOnes() throws Exception {+ String cexpr = "ones(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(), 1);+ assertEquals(out.get(1).intValue(), 1);+ assertEquals(out.get(2).intValue(), 1);+ assertEquals(out.get(3).intValue(), 1);+ assertEquals(out.get(4).intValue(), 1);+ assertEquals(out.get(5).intValue(), 1);+ }++ @Test+ public void testZeros() throws Exception {+ String cexpr = "zeros(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(), 0);+ assertEquals(out.get(2).intValue(), 0);+ assertEquals(out.get(3).intValue(), 0);+ assertEquals(out.get(4).intValue(), 0);+ assertEquals(out.get(5).intValue(), 0);+ }+@Testpublic void testMatrix() throws Exception {String cexpr = "let(echo=true," +