Skip to content

Apache Lucene open-source search software

SOLR-11567: Add triangularDistribution Stream Evaluator

Joel Bernstein5633268a450b
diff --git a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java b/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
index 4a5ef331d7a..65e87bd37bb 100644
--- a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
+++ b/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
@@ -271,6 +271,7 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware,
.withFunctionName("matrix", MatrixEvaluator.class)
.withFunctionName("transpose", TransposeEvaluator.class)
.withFunctionName("unit", UnitEvaluator.class)
+ .withFunctionName("triangularDistribution", TriangularDistributionEvaluator.class)
// Boolean Stream Evaluators
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TriangularDistributionEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TriangularDistributionEvaluator.java
new file mode 100644
index 00000000000..4f8d967b8be
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TriangularDistributionEvaluator.java
@@ -0,0 +1,46 @@
+/*
+ * 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 org.apache.commons.math3.distribution.TriangularDistribution;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+
+public class TriangularDistributionEvaluator extends RecursiveNumericEvaluator implements ManyValueWorker {
+
+ private static final long serialVersionUID = 1;
+
+ public TriangularDistributionEvaluator(StreamExpression expression, StreamFactory factory) throws IOException {
+ super(expression, factory);
+ }
+
+ @Override
+ public Object doWork(Object... values) throws IOException {
+
+ if (values.length != 3) {
+ throw new IOException("Triangular distribution requires three numeric parameters low, mode, high");
+ }
+
+ double low = ((Number) values[0]).doubleValue();
+ double mode = ((Number) values[1]).doubleValue();
+ double high = ((Number) values[2]).doubleValue();
+
+ return new TriangularDistribution(low, mode, high);
+ }
+}
\ No newline at end of file
diff --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.java
index 8196cf5edd5..955eafd10a1 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
@@ -7025,6 +7025,40 @@ public class StreamExpressionTest extends SolrCloudTestCase {
}
+ @Test
+ public void testTriangularDistribution() throws Exception {
+ String cexpr = "let(echo=true, " +
+ "a=describe(sample(triangularDistribution(10, 15, 30),10000)), " +
+ "b=describe(sample(triangularDistribution(10, 25, 30),10000)), " +
+ ")";
+
+ 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);
+ Map a = (Map)tuples.get(0).get("a");
+ Map b = (Map)tuples.get(0).get("b");
+
+ Number sa = (Number)a.get("skewness");
+ Number sb = (Number)b.get("skewness");
+
+ Number mina = (Number)a.get("min");
+ Number maxa = (Number)a.get("max");
+
+ assertTrue(sa.doubleValue() > 0);
+ assertTrue(sb.doubleValue() < 0);
+ assertEquals(mina.doubleValue(), 10, .5);
+ assertEquals(maxa.doubleValue(), 30, .5);
+ }
+
+
+
+
@Test
public void testMean() throws Exception {
String cexpr = "mean(array(1,2,3,4,5))";