mirror of https://github.com/apache/lucene.git
SOLR-11867: Add indexOf, rowCount and columnCount StreamEvaluators
This commit is contained in:
parent
42832f8839
commit
f491fad955
|
@ -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 Evaluators
|
||||
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue