Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

SOLR-16483: Fix IndexOutOfBounds in RecursiveNumericEvaluator (#1054)

Bendegúz Ácsb50367a3a499
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 56ec0477e9c..ed361401313 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -72,6 +72,8 @@ Bug Fixes
* SOLR-16160: UpdateXmlMessages duplicate data when data is removed and then added in the same message (Alex Deparvu via Kevin Risden)
+* SOLR-16483: Fix IndexOutOfBounds in RecursiveNumericEvaluator (Bendegúz Ács via Kevin Risden)
+
Build
---------------------
* Upgrade forbiddenapis to 3.4 (Uwe Schindler)
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/RecursiveNumericEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/RecursiveNumericEvaluator.java
index 3aa4b85b42a..0670fc6cdac 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/RecursiveNumericEvaluator.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/RecursiveNumericEvaluator.java
@@ -51,7 +51,7 @@ public abstract class RecursiveNumericEvaluator extends RecursiveEvaluator {
} else if (value instanceof Number) {
return new BigDecimal(value.toString());
} else if (value instanceof Collection) {
- if (value instanceof List) {
+ if (value instanceof List && !((List) value).isEmpty()) {
if (((List) value).get(0) instanceof Number) {
return value;
}
diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/eval/MovingAverageEvaluatorTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/eval/MovingAverageEvaluatorTest.java
new file mode 100644
index 00000000000..9c7d45a0e9c
--- /dev/null
+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/eval/MovingAverageEvaluatorTest.java
@@ -0,0 +1,52 @@
+/*
+ * 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.stream.eval;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.solr.SolrTestCase;
+import org.apache.solr.client.solrj.io.Tuple;
+import org.apache.solr.client.solrj.io.eval.MovingAverageEvaluator;
+import org.apache.solr.client.solrj.io.eval.StreamEvaluator;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+import org.junit.Test;
+
+public class MovingAverageEvaluatorTest extends SolrTestCase {
+
+ StreamFactory factory;
+ Map<String, Object> values;
+
+ public MovingAverageEvaluatorTest() {
+ super();
+
+ factory = new StreamFactory().withFunctionName("movingAvg", MovingAverageEvaluator.class);
+ values = new HashMap<>();
+ }
+
+ @Test
+ public void doesNotFailWithEmptyList() throws Exception {
+ StreamEvaluator evaluator = factory.constructEvaluator("movingAvg(a,30)");
+ Object result;
+
+ values.clear();
+ values.put("a", new ArrayList<Double>());
+ result = evaluator.evaluate(new Tuple(values));
+ assertEquals(Collections.emptyList(), result);
+ }
+}