Apache Lucene open-source search software
SOLR-12975: Add ltrim and rtrim Stream Evaluators
Joel Bernstein2d0e565c62f0
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 200f07b5fde..13c91ec1b1e 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@@ -274,6 +274,8 @@ public class Lang {.withFunctionName("pairSort", PairSortEvaluator.class).withFunctionName("recip", RecipEvaluator.class).withFunctionName("pivot", PivotEvaluator.class)+ .withFunctionName("ltrim", LeftShiftEvaluator.class)+ .withFunctionName("rtrim", RightShiftEvaluator.class)// Boolean Stream Evaluatorsdiff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/LeftShiftEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/LeftShiftEvaluator.javanew file mode 100644index 00000000000..2e86212d4b6--- /dev/null+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/LeftShiftEvaluator.java@@ -0,0 +1,53 @@+/*+ * 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 LeftShiftEvaluator extends RecursiveObjectEvaluator implements TwoValueWorker {+ protected static final long serialVersionUID = 1L;++ public LeftShiftEvaluator(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 value1, Object value2) throws IOException {+ if (value1 instanceof List && value2 instanceof Number) {+ List<?> actual = (List<?>) value1;+ int val = ((Number) value2).intValue();++ List<Object> shifted = new ArrayList<>();+ for (int i = val; i < actual.size(); i++) {+ shifted.add(actual.get(i));+ }+ return shifted;+ } else {+ throw new IOException("The ltrim function takes two parameters: an array and number to trim it by");+ }+ }+}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/RightShiftEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/RightShiftEvaluator.javanew file mode 100644index 00000000000..1dce38402b0--- /dev/null+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/RightShiftEvaluator.java@@ -0,0 +1,54 @@+/*+ * 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 RightShiftEvaluator extends RecursiveObjectEvaluator implements TwoValueWorker {+ protected static final long serialVersionUID = 1L;++ public RightShiftEvaluator(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 value1, Object value2) throws IOException {+ if (value1 instanceof List && value2 instanceof Number) {+ List<?> actual = (List<?>) value1;+ int val = ((Number) value2).intValue();++ List<Object> shifted = new ArrayList<>();+ for (int i = 0; i < actual.size()-val; i++) {+ shifted.add(actual.get(i));+ }+ return shifted;+ } else {+ throw new IOException("The rtrim function takes two parameters: an array and number to trim it by");+ }+ }++}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 98a6d45f3c3..3adac547157 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@@ -73,7 +73,7 @@ public class TestLang extends LuceneTestCase {"outliers", "stream", "getCache", "putCache", "listCache", "removeCache", "zscores", "latlonVectors","convexHull", "getVertices", "getBaryCenter", "getArea", "getBoundarySize","oscillate","getAmplitude", "getPhase", "getAngularFrequency", "enclosingDisk", "getCenter", "getRadius",- "getSupportPoints", "pairSort", "log10", "plist", "recip", "pivot"};+ "getSupportPoints", "pairSort", "log10", "plist", "recip", "pivot", "ltrim", "rtrim"};@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 9c93cedfeb0..0ccd691c1b8 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,47 @@ public class MathExpressionTest extends SolrCloudTestCase {assertEquals(out.get(5).intValue(), 1);}+ @Test+ public void testLtrim() throws Exception {+ String cexpr = "ltrim(array(1,2,3,4,5,6), 2)";+ 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(), 4);+ assertEquals(out.get(0).intValue(), 3);+ assertEquals(out.get(1).intValue(), 4);+ assertEquals(out.get(2).intValue(), 5);+ assertEquals(out.get(3).intValue(), 6);+ }++ @Test+ public void testRtrim() throws Exception {+ String cexpr = "rtrim(array(1,2,3,4,5,6), 2)";+ 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(), 4);+ assertEquals(out.get(0).intValue(), 1);+ assertEquals(out.get(1).intValue(), 2);+ assertEquals(out.get(2).intValue(), 3);+ assertEquals(out.get(3).intValue(), 4);+ }++@Testpublic void testZeros() throws Exception {String cexpr = "zeros(6)";