SOLR-13829: RecursiveEvaluator casts Continuous numbers to Discrete Numbers, causing mismatch

This commit is contained in:
Joel Bernstein 2019-10-10 21:18:45 -04:00
parent 5e9ed10657
commit ad1c24e190
17 changed files with 84 additions and 102 deletions

View File

@ -44,7 +44,11 @@ public class DoubleEvaluator extends RecursiveObjectEvaluator implements OneValu
return ((List<?>)value).stream().map(innerValue -> doWork(innerValue)).collect(Collectors.toList());
}
else{
if(value instanceof String) {
return Double.valueOf(value.toString());
} else {
return ((Number) value).doubleValue();
}
}
}
}

View File

@ -44,7 +44,11 @@ public class LongEvaluator extends RecursiveObjectEvaluator implements OneValueW
return ((List<?>)value).stream().map(innerValue -> doWork(innerValue)).collect(Collectors.toList());
}
else{
if(value instanceof String) {
return Long.valueOf(value.toString());
} else {
return ((Number) value).longValue();
}
}
}
}

View File

@ -126,27 +126,16 @@ public abstract class RecursiveEvaluator implements StreamEvaluator, ValueWorker
return value;
} else if(value instanceof BigDecimal){
BigDecimal bd = (BigDecimal)value;
if(bd.signum() == 0 || bd.scale() <= 0 || bd.stripTrailingZeros().scale() <= 0){
try{
return bd.longValueExact();
}
catch(ArithmeticException e){
// value was too big for a long, so use a double which can handle scientific notation
}
}
return bd.doubleValue();
}
else if(value instanceof Long || value instanceof Integer) {
return ((Number) value).longValue();
}
else if(value instanceof Double){
if(Double.isNaN((Double)value)){
return value;
}
// could be a long so recurse back in as a BigDecimal
return normalizeOutputType(new BigDecimal((Double)value));
}
else if(value instanceof Number){
return normalizeOutputType(new BigDecimal(((Number)value).toString()));
return ((Number) value).doubleValue();
}
else if(value instanceof List){
// normalize each value in the list

View File

@ -1203,7 +1203,7 @@ public class MathExpressionTest extends SolrCloudTestCase {
List<Tuple> tuples = getTuples(solrStream);
assertTrue(tuples.size() == 1);
Tuple tuple = tuples.get(0);
long binomialCoefficient = (long) tuple.get("return-value");
long binomialCoefficient = tuple.getLong("return-value");
assertEquals(binomialCoefficient, 56);
}
@ -3522,6 +3522,24 @@ public class MathExpressionTest extends SolrCloudTestCase {
assertEquals(cs.doubleValue(),0.9838197164968291, .00000001);
}
@Test
public void testCosineSimilaritySort() throws Exception {
String cexpr = "sort(select(list(tuple(id=\"1\", f=array(1,2,3,4)), tuple(id=\"2\",f=array(10,2,3,4)))," +
" cosineSimilarity(f, array(1,2,3,4)) as sim, id)," +
" by=\"sim desc\")";
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);
assertEquals(tuples.size(), 2);
assertEquals(tuples.get(0).getString("id"), "1");
}
@Test
public void testPoissonDistribution() throws Exception {
String cexpr = "let(a=poissonDistribution(100)," +
@ -5591,13 +5609,13 @@ public class MathExpressionTest extends SolrCloudTestCase {
assertTrue(tuples.size() == 1);
List<Number> convolution = (List<Number>)(tuples.get(0)).get("conv");
assertTrue(convolution.size() == 7);
assertTrue(convolution.get(0).equals(20000L));
assertTrue(convolution.get(1).equals(20000L));
assertTrue(convolution.get(2).equals(25000L));
assertTrue(convolution.get(3).equals(30000L));
assertTrue(convolution.get(4).equals(15000L));
assertTrue(convolution.get(5).equals(10000L));
assertTrue(convolution.get(6).equals(5000L));
assertTrue(convolution.get(0).equals(20000D));
assertTrue(convolution.get(1).equals(20000D));
assertTrue(convolution.get(2).equals(25000D));
assertTrue(convolution.get(3).equals(30000D));
assertTrue(convolution.get(4).equals(15000D));
assertTrue(convolution.get(5).equals(10000D));
assertTrue(convolution.get(6).equals(5000D));
}
@Test
@ -5648,7 +5666,7 @@ public class MathExpressionTest extends SolrCloudTestCase {
double prediction = tuple.getDouble("p");
assertTrue(prediction == 600.0D);
List<Number> predictions = (List<Number>)tuple.get("pl");
assertList(predictions, 200L, 400L, 600L, 200L, 400L, 800L, 1200L);
assertList(predictions, 200D, 400D, 600D, 200D, 400D, 800D, 1200D);
}
@Test

View File

@ -53,8 +53,7 @@ public class AbsoluteValueEvaluatorTest extends SolrTestCase {
values.clear();
values.put("a", 1);
result = evaluator.evaluate(new Tuple(values));
Assert.assertTrue(result instanceof Long);
Assert.assertEquals(1L, result);
Assert.assertEquals(1D, result);
values.clear();
values.put("a", 1.1);
@ -78,8 +77,7 @@ public class AbsoluteValueEvaluatorTest extends SolrTestCase {
context.getLets().put("a", 1);
result = evaluator.evaluate(new Tuple());
Assert.assertTrue(result instanceof Long);
Assert.assertEquals(1L, result);
Assert.assertEquals(1D, result);
context.getLets().put("a", 1.1);
result = evaluator.evaluate(new Tuple());
@ -93,8 +91,7 @@ public class AbsoluteValueEvaluatorTest extends SolrTestCase {
context.getLets().put("a", factory.constructEvaluator("add(4,-6,34,-56)"));
result = evaluator.evaluate(new Tuple());
Assert.assertTrue(result instanceof Long);
Assert.assertEquals(24L, result);
Assert.assertEquals(24D, result);
}
@Test(expected = IOException.class)

View File

@ -50,8 +50,7 @@ public class AddEvaluatorTest extends SolrTestCase {
values.put("a", 1);
values.put("b", 2);
result = evaluator.evaluate(new Tuple(values));
Assert.assertTrue(result instanceof Long);
Assert.assertEquals(3L, result);
Assert.assertEquals(3D, result);
values.clear();
values.put("a", 1.1);
@ -134,8 +133,7 @@ public class AddEvaluatorTest extends SolrTestCase {
values.put("c", 3);
values.put("d", 4);
result = evaluator.evaluate(new Tuple(values));
Assert.assertTrue(result instanceof Long);
Assert.assertEquals(10L, result);
Assert.assertEquals(10D, result);
values.clear();
values.put("a", 1.1);
@ -167,8 +165,7 @@ public class AddEvaluatorTest extends SolrTestCase {
values.put("c", 3);
values.put("d", 4);
result = evaluator.evaluate(new Tuple(values));
Assert.assertTrue(result instanceof Long);
Assert.assertEquals(10L, result);
Assert.assertEquals(10D, result);
values.clear();
values.put("a", 1.1);
@ -203,8 +200,7 @@ public class AddEvaluatorTest extends SolrTestCase {
values.put("c", 123456789123456789L);
values.put("d", 123456789123456789L);
result = evaluator.evaluate(new Tuple(values));
Assert.assertTrue(result instanceof Long);
Assert.assertEquals(4 * 123456789123456789L, result);
Assert.assertEquals(4 * 123456789123456789D, result);
}
@Test
@ -218,8 +214,7 @@ public class AddEvaluatorTest extends SolrTestCase {
values.put("c", 3);
values.put("d", 4);
result = evaluator.evaluate(new Tuple(values));
Assert.assertTrue(result instanceof Long);
Assert.assertEquals(14L, result);
Assert.assertEquals(14D, result);
values.clear();
values.put("a", 1.1);
@ -254,8 +249,7 @@ public class AddEvaluatorTest extends SolrTestCase {
values.put("c", 123456789123456789L);
values.put("d", 123456789123456789L);
result = evaluator.evaluate(new Tuple(values));
Assert.assertTrue(result instanceof Long);
Assert.assertEquals(6 * 123456789123456789L, result);
Assert.assertEquals(6 * 123456789123456789D, result);
values.clear();
values.put("a", 4.12345678);
@ -278,8 +272,7 @@ public class AddEvaluatorTest extends SolrTestCase {
values.put("c", 3);
values.put("d", 4);
result = evaluator.evaluate(new Tuple(values));
Assert.assertTrue(result instanceof Long);
Assert.assertEquals(10L, result);
Assert.assertEquals(10D, result);
values.clear();
values.put("a", 1.1);
@ -314,8 +307,7 @@ public class AddEvaluatorTest extends SolrTestCase {
values.put("c", 123456789123456789L);
values.put("d", 123456789123456789L);
result = evaluator.evaluate(new Tuple(values));
Assert.assertTrue(result instanceof Long);
Assert.assertEquals(4 * 123456789123456789L, result);
Assert.assertEquals(4 * 123456789123456789D, result);
values.clear();
values.put("a", -4.12345678);

View File

@ -49,11 +49,11 @@ public class AppendEvaluatorTest extends SolrTestCase {
Object result;
values.clear();
values.put("a", 1L);
values.put("a", 1);
values.put("b", Arrays.asList("foo","bar","baz"));
result = evaluator.evaluate(new Tuple(values));
Assert.assertTrue(result instanceof List);
Assert.assertEquals(1L, ((List)result).get(0));
Assert.assertEquals(1D, ((List)result).get(0));
Assert.assertEquals("foo", ((List)result).get(1));
Assert.assertEquals("bar", ((List)result).get(2));
Assert.assertEquals("baz", ((List)result).get(3));

View File

@ -60,9 +60,9 @@ public class ArrayEvaluatorTest extends SolrTestCase {
Assert.assertTrue(result instanceof List<?>);
Assert.assertEquals(3, ((List<?>)result).size());
Assert.assertEquals(1L, ((List<?>)result).get(0));
Assert.assertEquals(2L, ((List<?>)result).get(1));
Assert.assertEquals(3L, ((List<?>)result).get(2));
Assert.assertEquals(1D, ((List<?>)result).get(0));
Assert.assertEquals(2D, ((List<?>)result).get(1));
Assert.assertEquals(3D, ((List<?>)result).get(2));
}
@Test
@ -81,9 +81,9 @@ public class ArrayEvaluatorTest extends SolrTestCase {
Assert.assertTrue(result instanceof List<?>);
Assert.assertEquals(3, ((List<?>)result).size());
Assert.assertEquals(3L, ((List<?>)result).get(0));
Assert.assertEquals(2L, ((List<?>)result).get(1));
Assert.assertEquals(1L, ((List<?>)result).get(2));
Assert.assertEquals(3D, ((List<?>)result).get(0));
Assert.assertEquals(2D, ((List<?>)result).get(1));
Assert.assertEquals(1D, ((List<?>)result).get(2));
}
@Test

View File

@ -53,7 +53,7 @@ public class AscEvaluatorTest extends SolrTestCase {
result = evaluator.evaluate(new Tuple(values));
Assert.assertTrue(result instanceof List<?>);
Assert.assertEquals(7, ((List<?>)result).size());
checkOrder(Arrays.asList(1L,2L,3L,4L,5L,7L,8L), (List<Object>)result);
checkOrder(Arrays.asList(1D,2D,3D,4D,5D,7D,8D), (List<Object>)result);
}
@Test
@ -79,7 +79,7 @@ public class AscEvaluatorTest extends SolrTestCase {
result = evaluator.evaluate(new Tuple(values));
Assert.assertTrue(result instanceof List<?>);
Assert.assertEquals(7, ((List<?>)result).size());
checkOrder(Arrays.asList(2L, 2.1, 2.3, 2.5, 2.6, 2.7, 3L), (List<Object>)result);
checkOrder(Arrays.asList(2D, 2.1, 2.3, 2.5, 2.6, 2.7, 3D), (List<Object>)result);
}
@Test

View File

@ -50,20 +50,17 @@ public class CeilingEvaluatorTest extends SolrTestCase {
values.clear();
values.put("a", 1);
result = evaluator.evaluate(new Tuple(values));
Assert.assertTrue(result instanceof Long);
Assert.assertEquals(1L, result);
Assert.assertEquals(1D, result);
values.clear();
values.put("a", 1.1);
result = evaluator.evaluate(new Tuple(values));
Assert.assertTrue(result instanceof Long);
Assert.assertEquals(2L, result);
Assert.assertEquals(2D, result);
values.clear();
values.put("a", -1.1);
result = evaluator.evaluate(new Tuple(values));
Assert.assertTrue(result instanceof Long);
Assert.assertEquals(-1L, result);
Assert.assertEquals(-1D, result);
}
@Test(expected = IOException.class)

View File

@ -107,6 +107,6 @@ public class CoalesceEvaluatorTest extends SolrTestCase {
values.put("c", null);
values.put("d", 4);
result = evaluator.evaluate(new Tuple(values));
Assert.assertEquals(1L, result);
Assert.assertEquals(1D, result);
}
}

View File

@ -158,7 +158,6 @@ public class DivideEvaluatorTest extends SolrTestCase {
values.put("a", 0);
values.put("b", 2);
result = evaluator.evaluate(new Tuple(values));
Assert.assertTrue(result instanceof Long);
Assert.assertEquals(0L, result);
Assert.assertEquals(0D, result);
}
}

View File

@ -50,20 +50,17 @@ public class FloorEvaluatorTest extends SolrTestCase {
values.clear();
values.put("a", 1);
result = evaluator.evaluate(new Tuple(values));
Assert.assertTrue(result instanceof Long);
Assert.assertEquals(1L, result);
Assert.assertEquals(1D, result);
values.clear();
values.put("a", 1.1);
result = evaluator.evaluate(new Tuple(values));
Assert.assertTrue(result instanceof Long);
Assert.assertEquals(1L, result);
Assert.assertEquals(1D, result);
values.clear();
values.put("a", -1.1);
result = evaluator.evaluate(new Tuple(values));
Assert.assertTrue(result instanceof Long);
Assert.assertEquals(-2L, result);
Assert.assertEquals(-2D, result);
}
@Test(expected = IOException.class)

View File

@ -51,21 +51,18 @@ public class ModuloEvaluatorTest extends SolrTestCase {
values.put("a", 1);
values.put("b", 2);
result = evaluator.evaluate(new Tuple(values));
Assert.assertTrue(result instanceof Long);
Assert.assertEquals(Long.valueOf(1 % 2), result);
Assert.assertEquals(1 % 2, ((Number)result).doubleValue(), 0.0);
values.clear();
values.put("a", 1.1);
values.put("b", 2);
result = evaluator.evaluate(new Tuple(values));
Assert.assertTrue(result instanceof Double);
Assert.assertEquals(1.1 % 2, result);
values.clear();
values.put("a", 1.1);
values.put("b", 2.1);
result = evaluator.evaluate(new Tuple(values));
Assert.assertTrue(result instanceof Double);
Assert.assertEquals(1.1 % 2.1, result);
}
@ -135,8 +132,7 @@ public class ModuloEvaluatorTest extends SolrTestCase {
values.put("b", 2);
values.put("c", 9);
result = evaluator.evaluate(new Tuple(values));
Assert.assertTrue(result instanceof Long);
Assert.assertEquals(Long.valueOf(1 % (2 % 9)), result);
Assert.assertEquals(1 % (2 % 9), ((Number)result).doubleValue(), 0.0);
}
@Test(expected = IOException.class)
@ -158,7 +154,6 @@ public class ModuloEvaluatorTest extends SolrTestCase {
values.put("a", 0);
values.put("b", 2);
result = evaluator.evaluate(new Tuple(values));
Assert.assertTrue(result instanceof Long);
Assert.assertEquals(0L, result);
Assert.assertEquals(0D, result);
}
}

View File

@ -50,8 +50,7 @@ public class MultiplyEvaluatorTest extends SolrTestCase {
values.put("a", 1);
values.put("b", 2);
result = evaluator.evaluate(new Tuple(values));
Assert.assertTrue(result instanceof Long);
Assert.assertEquals(2L, result);
Assert.assertEquals(2D, result);
values.clear();
values.put("a", 1.1);
@ -76,8 +75,7 @@ public class MultiplyEvaluatorTest extends SolrTestCase {
values.clear();
values.put("a", 6);
result = evaluator.evaluate(new Tuple(values));
Assert.assertTrue(result instanceof Long);
Assert.assertEquals(6L, result);
Assert.assertEquals(6D, result);
values.clear();
values.put("a", 6.5);
@ -152,8 +150,7 @@ public class MultiplyEvaluatorTest extends SolrTestCase {
values.put("c", 3);
values.put("d", 4);
result = evaluator.evaluate(new Tuple(values));
Assert.assertTrue(result instanceof Long);
Assert.assertEquals(24L, result);
Assert.assertEquals(24D, result);
values.clear();
values.put("a", 1.1);
@ -185,7 +182,6 @@ public class MultiplyEvaluatorTest extends SolrTestCase {
values.put("c", 3);
values.put("d", 4);
result = evaluator.evaluate(new Tuple(values));
Assert.assertTrue(result instanceof Long);
Assert.assertEquals(24L, result);
Assert.assertEquals(24D, result);
}
}

View File

@ -67,8 +67,7 @@ public class RecursiveEvaluatorTest extends SolrTestCase {
values.put("f", 2);
values.put("g", 5);
result = evaluator.evaluate(new Tuple(values));
Assert.assertTrue(result instanceof Long);
Assert.assertEquals(-16L, result);
Assert.assertEquals(-16D, result);
values.clear();
values.put("a", .1);

View File

@ -51,8 +51,7 @@ public class SubtractEvaluatorTest extends SolrTestCase {
values.put("a", 1);
values.put("b", 2);
result = evaluator.evaluate(new Tuple(values));
Assert.assertTrue(result instanceof Long);
Assert.assertEquals(-1L, result);
Assert.assertEquals(-1D, result);
values.clear();
values.put("a", 1.1);
@ -65,8 +64,7 @@ public class SubtractEvaluatorTest extends SolrTestCase {
values.put("a", 1.1);
values.put("b", 2.1);
result = evaluator.evaluate(new Tuple(values));
Assert.assertTrue(result instanceof Long);
Assert.assertEquals(-1L, result);
Assert.assertEquals(-1D, result);
}
@Test(expected = IOException.class)
@ -140,8 +138,7 @@ public class SubtractEvaluatorTest extends SolrTestCase {
values.put("c", 3);
values.put("d", 4);
result = evaluator.evaluate(new Tuple(values));
Assert.assertTrue(result instanceof Long);
Assert.assertEquals(-8L, result);
Assert.assertEquals(-8D, result);
values.clear();
values.put("a", 1.1);
@ -173,8 +170,7 @@ public class SubtractEvaluatorTest extends SolrTestCase {
values.put("c", 3);
values.put("d", 4);
result = evaluator.evaluate(new Tuple(values));
Assert.assertTrue(result instanceof Long);
Assert.assertEquals(0L, result);
Assert.assertEquals(0D, result);
values.clear();
values.put("a", 123456789123456789L);
@ -182,7 +178,6 @@ public class SubtractEvaluatorTest extends SolrTestCase {
values.put("c", 123456789123456789L);
values.put("d", 123456789123456789L);
result = evaluator.evaluate(new Tuple(values));
Assert.assertTrue(result instanceof Long);
Assert.assertEquals(0L, result);
Assert.assertEquals(0D, result);
}
}