SOLR-11571: Add diff Stream Evaluator to support time series differencing

This commit is contained in:
Joel Bernstein 2017-11-10 13:14:53 -05:00
parent 6e3d082395
commit 9ea9a85339
3 changed files with 155 additions and 0 deletions

View File

@ -282,6 +282,7 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware,
.withFunctionName("scalarDivide", ScalarDivideEvaluator.class)
.withFunctionName("sumRows", SumRowsEvaluator.class)
.withFunctionName("sumColumns", SumColumnsEvaluator.class)
.withFunctionName("diff", TimeDifferencingEvaluator.class)
// Boolean Stream Evaluators

View File

@ -0,0 +1,70 @@
/*
* 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.math.BigDecimal;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
public class TimeDifferencingEvaluator extends RecursiveNumericEvaluator implements ManyValueWorker{
protected static final long serialVersionUID = 1L;
public TimeDifferencingEvaluator(StreamExpression expression, StreamFactory factory) throws IOException {
super(expression, factory);
if (!(1 == containedEvaluators.size() || containedEvaluators.size() == 2)){
throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - expecting one or two values but found %d",expression, containedEvaluators.size()));
}
}
@Override
public Object doWork(Object... values) throws IOException {
if (!(1 == values.length || values.length == 2)){
throw new IOException(String.format(Locale.ROOT,"%s(...) only works with 1 or 2 values but %d were provided", constructingFactory.getFunctionName(getClass()), values.length));
}
List<BigDecimal> timeseriesValues = (List<BigDecimal> )values[0];
Number lagValue = 1;
if(1 == values.length) {
if (!(timeseriesValues instanceof List<?>)) {
throw new IOException(String.format(Locale.ROOT, "Invalid expression %s - found type %s for the first value, expecting a List", toExpression(constructingFactory), values[0].getClass().getSimpleName()));
}
if (!(timeseriesValues.size() > 1)) {
throw new IOException(String.format(Locale.ROOT, "Invalid expression %s - found list size of %s for the first value, expecting a List of size > 0.", toExpression(constructingFactory), timeseriesValues.size()));
}
}
if(2 == values.length) {
lagValue = (Number) values[1];
if(!(lagValue instanceof Number)){
throw new IOException(String.format(Locale.ROOT, "Invalid expression %s - found type %s for the second value, expecting a Number", toExpression(constructingFactory), values[1].getClass().getSimpleName()));
}
if (lagValue.intValue() > timeseriesValues.size()) {
throw new IOException(String.format(Locale.ROOT, "Invalid expression %s - found a lag size of %s for the second value, the first value has a List size of %s, expecting a lag value less than the List size", toExpression(constructingFactory), lagValue.intValue(), timeseriesValues.size()));
}
}
final int lag = lagValue.intValue();
return IntStream.range(lag, timeseriesValues.size())
.mapToObj(n -> (timeseriesValues.get(n).doubleValue()-timeseriesValues.get(n-lag).doubleValue()))
.collect(Collectors.toList());
}
}

View File

@ -6840,6 +6840,90 @@ public class StreamExpressionTest extends SolrCloudTestCase {
assertEquals((double)out.get(20), 22.92, 0.009);
}
@Test
public void testTimeDifferencingDefaultLag() throws Exception {
String cexpr = "diff(array(1709.0, 1621.0, 1973.0, 1812.0, 1975.0, 1862.0, 1940.0, 2013.0, 1596.0, 1725.0, 1676.0, 1814.0, 1615.0, 1557.0, 1891.0, 1956.0, 1885.0, 1623.0, 1903.0, 1997.0))";
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");
assertTrue(out.size() == 19);
assertEquals(out.get(0).doubleValue(),-88.0, 0.01);
assertEquals(out.get(1).doubleValue(),352.0, 0.01);
assertEquals(out.get(2).doubleValue(),-161.0, 0.01);
assertEquals(out.get(3).doubleValue(),163.0, 0.01);
assertEquals(out.get(4).doubleValue(),-113.0, 0.01);
assertEquals(out.get(5).doubleValue(),78.0, 0.01);
assertEquals(out.get(6).doubleValue(),73.0, 0.01);
assertEquals(out.get(7).doubleValue(),-417.0, 0.01);
assertEquals(out.get(8).doubleValue(),129.0, 0.01);
assertEquals(out.get(9).doubleValue(),-49.0, 0.01);
assertEquals(out.get(10).doubleValue(),138.0, 0.01);
assertEquals(out.get(11).doubleValue(),-199.0, 0.01);
assertEquals(out.get(12).doubleValue(),-58.0, 0.01);
assertEquals(out.get(13).doubleValue(),334.0, 0.01);
assertEquals(out.get(14).doubleValue(),65.0, 0.01);
assertEquals(out.get(15).doubleValue(),-71.0, 0.01);
assertEquals(out.get(16).doubleValue(),-262.0, 0.01);
assertEquals(out.get(17).doubleValue(),280.0, 0.01);
assertEquals(out.get(18).doubleValue(),94.0, 0.01);
}
@Test
public void testTimeDifferencingDefinedLag() throws Exception {
String cexpr = "diff(array(1709.0, 1621.0, 1973.0, 1812.0, 1975.0, 1862.0, 1940.0, 2013.0, 1596.0, 1725.0, 1676.0, 1814.0, 1615.0, 1557.0, 1891.0, 1956.0, 1885.0, 1623.0, 1903.0, 1997.0), 12)";
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");
assertTrue(out.size() == 8);
assertEquals(out.get(0).doubleValue(),-94.0, 0.01);
assertEquals(out.get(1).doubleValue(),-64.0, 0.01);
assertEquals(out.get(2).doubleValue(),-82.0, 0.01);
assertEquals(out.get(3).doubleValue(),144.0, 0.01);
assertEquals(out.get(4).doubleValue(),-90.0, 0.01);
assertEquals(out.get(5).doubleValue(),-239.0, 0.01);
assertEquals(out.get(6).doubleValue(),-37.0, 0.01);
assertEquals(out.get(7).doubleValue(),-16.0, 0.01);
}
@Test
public void testNestedDoubleTimeDifference() throws Exception {
String cexpr = "diff(diff(array(1709.0, 1621.0, 1973.0, 1812.0, 1975.0, 1862.0, 1940.0, 2013.0, 1596.0, 1725.0, 1676.0, 1814.0, 1615.0, 1557.0, 1891.0, 1956.0, 1885.0, 1623.0, 1903.0, 1997.0)), 12)";
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");
assertTrue(out.size() == 7);
assertEquals(out.get(0).doubleValue(),30.0,0.01);
assertEquals(out.get(1).doubleValue(),-18.0,0.01);
assertEquals(out.get(2).doubleValue(),226.0,0.01);
assertEquals(out.get(3).doubleValue(),-234.0,0.01);
assertEquals(out.get(4).doubleValue(),-149.0,0.01);
assertEquals(out.get(5).doubleValue(),202.0,0.01);
assertEquals(out.get(6).doubleValue(),21.0,0.01);
}
@Test
public void testResiduals() throws Exception {
String cexpr = "let(a=array(1,2,3,4,5,6), b=array(2,4,6,8,10,12), c=regress(a,b), tuple(res=residuals(c,a,a)))";