SOLR-12840: Add pairSort Stream Evaluator

This commit is contained in:
Joel Bernstein 2018-10-08 12:53:23 -04:00
parent 49285e8423
commit 6a702ee16b
4 changed files with 127 additions and 1 deletions

View File

@ -270,6 +270,7 @@ public class Lang {
.withFunctionName("getCenter", GetCenterEvaluator.class) .withFunctionName("getCenter", GetCenterEvaluator.class)
.withFunctionName("getRadius", GetRadiusEvaluator.class) .withFunctionName("getRadius", GetRadiusEvaluator.class)
.withFunctionName("getSupportPoints", GetSupportPointsEvaluator.class) .withFunctionName("getSupportPoints", GetSupportPointsEvaluator.class)
.withFunctionName("pairSort", PairSortEvaluator.class)
// Boolean Stream Evaluators // Boolean Stream Evaluators
.withFunctionName("and", AndEvaluator.class) .withFunctionName("and", AndEvaluator.class)

View File

@ -0,0 +1,93 @@
/*
* 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.Comparator;
import java.util.List;
import java.util.Locale;
import java.util.Collections;
import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
public class PairSortEvaluator extends RecursiveNumericEvaluator implements TwoValueWorker {
protected static final long serialVersionUID = 1L;
public PairSortEvaluator(StreamExpression expression, StreamFactory factory) throws IOException{
super(expression, factory);
}
@Override
public Object doWork(Object first, Object second) throws IOException{
if(null == first){
throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - null found for the first value",toExpression(constructingFactory)));
}
if(null == second){
throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - null found for the second value",toExpression(constructingFactory)));
}
if(!(first instanceof List<?>)){
throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - found type %s for the first value, expecting a list of numbers",toExpression(constructingFactory), first.getClass().getSimpleName()));
}
if(!(second instanceof List<?>)){
throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - found type %s for the second value, expecting a list of numbers",toExpression(constructingFactory), first.getClass().getSimpleName()));
}
List<Number> l1 = (List<Number>)first;
List<Number> l2 = (List<Number>)second;
if(l2.size() != l1.size()){
throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - first list (%d) has a different size than the second list (%d)",toExpression(constructingFactory), l1.size(), l2.size()));
}
List<double[]> pairs = new ArrayList();
for(int idx = 0; idx < l1.size(); ++idx){
double[] pair = new double[2];
pair[0]= l1.get(idx).doubleValue();
pair[1] = l2.get(idx).doubleValue();
pairs.add(pair);
}
Collections.sort(pairs, new PairComp());
double[][] data = new double[2][pairs.size()];
for(int i=0; i<pairs.size(); i++) {
data[0][i] = pairs.get(i)[0];
data[1][i] = pairs.get(i)[1];
}
return new Matrix(data);
}
private class PairComp implements Comparator<double[]> {
public int compare(double[] a, double[] b) {
if(a[0] > b[0]) {
return 1;
} else if(a[0] < b[0]) {
return -1;
} else {
if(a[1] > b[1]) {
return 1;
} else if(a[1] < b[1]){
return -1;
} else {
return 0;
}
}
}
}
}

View File

@ -73,7 +73,7 @@ public class TestLang extends LuceneTestCase {
"outliers", "stream", "getCache", "putCache", "listCache", "removeCache", "zscores", "latlonVectors", "outliers", "stream", "getCache", "putCache", "listCache", "removeCache", "zscores", "latlonVectors",
"convexHull", "getVertices", "getBaryCenter", "getArea", "getBoundarySize","oscillate", "convexHull", "getVertices", "getBaryCenter", "getArea", "getBoundarySize","oscillate",
"getAmplitude", "getPhase", "getAngularFrequency", "enclosingDisk", "getCenter", "getRadius", "getAmplitude", "getPhase", "getAngularFrequency", "enclosingDisk", "getCenter", "getRadius",
"getSupportPoints"}; "getSupportPoints", "pairSort"};
@Test @Test
public void testLang() { public void testLang() {

View File

@ -1154,6 +1154,38 @@ public class MathExpressionTest extends SolrCloudTestCase {
assertTrue(out.get(5).doubleValue() == 500.23D); assertTrue(out.get(5).doubleValue() == 500.23D);
} }
@Test
public void testPairSort() throws Exception {
String cexpr = "let(a=array(4.5, 7.7, 2.1, 2.1, 6.3)," +
" b=array(1, 2, 3, 4, 5)," +
" c=pairSort(a, b))";
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<List<Number>> out = (List<List<Number>>)tuples.get(0).get("c");
System.out.println("###### out:"+out);
assertEquals(out.size(), 2);
List<Number> row1 = out.get(0);
assertEquals(row1.get(0).doubleValue(), 2.1, 0);
assertEquals(row1.get(1).doubleValue(), 2.1, 0);
assertEquals(row1.get(2).doubleValue(), 4.5, 0);
assertEquals(row1.get(3).doubleValue(), 6.3, 0);
assertEquals(row1.get(4).doubleValue(), 7.7, 0);
List<Number> row2 = out.get(1);
assertEquals(row2.get(0).doubleValue(), 3, 0);
assertEquals(row2.get(1).doubleValue(), 4, 0);
assertEquals(row2.get(2).doubleValue(), 1, 0);
assertEquals(row2.get(3).doubleValue(), 5, 0);
assertEquals(row2.get(4).doubleValue(), 2, 0);
}
@Test @Test
public void testOnes() throws Exception { public void testOnes() throws Exception {