SOLR-11867: Add indexOf, rowCount and columnCount StreamEvaluators

This commit is contained in:
Joel Bernstein 2018-01-17 15:42:51 -05:00
parent 42832f8839
commit f491fad955
6 changed files with 157 additions and 2 deletions

View File

@ -307,6 +307,9 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware,
.withFunctionName("setRowLabels", SetRowLabelsEvaluator.class) .withFunctionName("setRowLabels", SetRowLabelsEvaluator.class)
.withFunctionName("knn", KnnEvaluator.class) .withFunctionName("knn", KnnEvaluator.class)
.withFunctionName("getAttributes", GetAttributesEvaluator.class) .withFunctionName("getAttributes", GetAttributesEvaluator.class)
.withFunctionName("indexOf", IndexOfEvaluator.class)
.withFunctionName("columnCount", ColumnCountEvaluator.class)
.withFunctionName("rowCount", RowCountEvaluator.class)
// Boolean Stream Evaluators // Boolean Stream Evaluators

View File

@ -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();
}
}
}

View File

@ -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;
}
}
}

View File

@ -67,6 +67,14 @@ public class Matrix implements Iterable, Attributes {
return this.data; return this.data;
} }
public int getRowCount() {
return data.length;
}
public int getColumnCount() {
return data[0].length;
}
public Iterator iterator() { public Iterator iterator() {
return new MatrixIterator(data); return new MatrixIterator(data);
} }

View File

@ -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();
}
}
}

View File

@ -6178,11 +6178,15 @@ public class StreamExpressionTest extends SolrCloudTestCase {
String cexpr = "let(echo=true," + String cexpr = "let(echo=true," +
" a=setColumnLabels(matrix(array(1, 2, 3), " + " a=setColumnLabels(matrix(array(1, 2, 3), " +
" rev(array(4,5,6)))," + " rev(array(4,5,6)))," +
" array(col1, col2, col3))," + " array(col1, col2, col3))," +
" b=rowAt(a, 1)," + " b=rowAt(a, 1)," +
" c=colAt(a, 2)," + " c=colAt(a, 2)," +
" d=getColumnLabels(a)," + " 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(); ModifiableSolrParams paramsLoc = new ModifiableSolrParams();
paramsLoc.set("expr", cexpr); paramsLoc.set("expr", cexpr);
paramsLoc.set("qt", "/stream"); paramsLoc.set("qt", "/stream");
@ -6230,6 +6234,11 @@ public class StreamExpressionTest extends SolrCloudTestCase {
assertEquals(features.get(1).size(), 1); assertEquals(features.get(1).size(), 1);
assertEquals(features.get(0).get(0), "col3"); assertEquals(features.get(0).get(0), "col3");
assertEquals(features.get(1).get(0), "col1"); 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);
} }