Apache Lucene open-source search software
SOLR-11867: Add indexOf, rowCount and columnCount StreamEvaluators
Joel Bernsteinf491fad955fc
diff --git a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java b/solr/core/src/java/org/apache/solr/handler/StreamHandler.javaindex aa602860189..b9a271a6835 100644--- a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java+++ b/solr/core/src/java/org/apache/solr/handler/StreamHandler.java@@ -307,6 +307,9 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware,.withFunctionName("setRowLabels", SetRowLabelsEvaluator.class).withFunctionName("knn", KnnEvaluator.class).withFunctionName("getAttributes", GetAttributesEvaluator.class)+ .withFunctionName("indexOf", IndexOfEvaluator.class)+ .withFunctionName("columnCount", ColumnCountEvaluator.class)+ .withFunctionName("rowCount", RowCountEvaluator.class)// Boolean Stream Evaluatorsdiff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ColumnCountEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ColumnCountEvaluator.javanew file mode 100644index 00000000000..2949e45a1a9--- /dev/null+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ColumnCountEvaluator.java@@ -0,0 +1,42 @@+/*+ * 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.Locale;++import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;++public class ColumnCountEvaluator extends RecursiveObjectEvaluator implements OneValueWorker {+ private static final long serialVersionUID = 1;++ public ColumnCountEvaluator(StreamExpression expression, StreamFactory factory) throws IOException {+ super(expression, factory);+ }++ @Override+ public Object doWork(Object value) throws IOException {+ if(!(value instanceof Matrix)){+ throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - found type %s for value, expecting a Matrix",toExpression(constructingFactory), value.getClass().getSimpleName()));+ } else {+ Matrix matrix = (Matrix)value;+ return matrix.getColumnCount();+ }+ }+}\ No newline at end of filediff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/IndexOfEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/IndexOfEvaluator.javanew file mode 100644index 00000000000..60136ffd29b--- /dev/null+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/IndexOfEvaluator.java@@ -0,0 +1,51 @@+/*+ * 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.Locale;+import java.util.List;++import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;++public class IndexOfEvaluator extends RecursiveObjectEvaluator implements TwoValueWorker {+ private static final long serialVersionUID = 1;++ public IndexOfEvaluator(StreamExpression expression, StreamFactory factory) throws IOException {+ super(expression, factory);+ }++ @Override+ public Object doWork(Object value1, Object value2) throws IOException {+ if(!(value1 instanceof List)){+ throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - found type %s for value, expecting an array",toExpression(constructingFactory), value1.getClass().getSimpleName()));+ } else {+ List list = (List)value1;+ String find = value2.toString().replace("\"","");+ for(int i=0; i<list.size(); i++) {+ Object o = list.get(i);+ if(o.toString().equals(find)) {+ return i;+ }+ }++ return -1;+ }+ }+}\ No newline at end of filediff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/Matrix.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/Matrix.javaindex 7fcfca27cbc..ed10a8c3e4d 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/Matrix.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/Matrix.java@@ -67,6 +67,14 @@ public class Matrix implements Iterable, Attributes {return this.data;}+ public int getRowCount() {+ return data.length;+ }++ public int getColumnCount() {+ return data[0].length;+ }+public Iterator iterator() {return new MatrixIterator(data);}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/RowCountEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/RowCountEvaluator.javanew file mode 100644index 00000000000..8e92d1ac35a--- /dev/null+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/RowCountEvaluator.java@@ -0,0 +1,42 @@+/*+ * 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.Locale;++import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;++public class RowCountEvaluator extends RecursiveObjectEvaluator implements OneValueWorker {+ private static final long serialVersionUID = 1;++ public RowCountEvaluator(StreamExpression expression, StreamFactory factory) throws IOException {+ super(expression, factory);+ }++ @Override+ public Object doWork(Object value) throws IOException {+ if(!(value instanceof Matrix)){+ throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - found type %s for value, expecting a Matrix",toExpression(constructingFactory), value.getClass().getSimpleName()));+ } else {+ Matrix matrix = (Matrix)value;+ return matrix.getRowCount();+ }+ }+}\ No newline at end of filediff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/StreamExpressionTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/StreamExpressionTest.javaindex 1493562b571..493799299dd 100644--- a/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/StreamExpressionTest.java+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/StreamExpressionTest.java@@ -6178,11 +6178,15 @@ public class StreamExpressionTest extends SolrCloudTestCase {String cexpr = "let(echo=true," +" a=setColumnLabels(matrix(array(1, 2, 3), " +" rev(array(4,5,6)))," +- " array(col1, col2, col3))," ++ " array(col1, col2, col3))," +" b=rowAt(a, 1)," +" c=colAt(a, 2)," +" d=getColumnLabels(a)," +- " e=topFeatures(a, 1))";+ " e=topFeatures(a, 1)," ++ " f=rowCount(a)," ++ " g=columnCount(a)," ++ " h=indexOf(d, \"col2\")," ++ " i=indexOf(d, col3))";ModifiableSolrParams paramsLoc = new ModifiableSolrParams();paramsLoc.set("expr", cexpr);paramsLoc.set("qt", "/stream");@@ -6230,6 +6234,11 @@ public class StreamExpressionTest extends SolrCloudTestCase {assertEquals(features.get(1).size(), 1);assertEquals(features.get(0).get(0), "col3");assertEquals(features.get(1).get(0), "col1");++ assertTrue(tuples.get(0).getLong("f") == 2);+ assertTrue(tuples.get(0).getLong("g")== 3);+ assertTrue(tuples.get(0).getLong("h")== 1);+ assertTrue(tuples.get(0).getLong("i")== 2);}