Skip to content

Commit

Permalink
SQL: cover the Integer type when extracting values from _source (#42859)
Browse files Browse the repository at this point in the history
* Take into consideration a wider range of Numbers when extracting the
values from source, more specifically - BigInteger and BigDecimal.

(cherry picked from commit 561b8d7)
  • Loading branch information
astefan committed Jun 10, 2019
1 parent 0db64b1 commit 0ed42d6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
Expand Up @@ -143,7 +143,11 @@ private Object unwrapMultiValue(Object values) {
return DateUtils.asDateTime(Long.parseLong(values.toString()), zoneId);
}
}
if (values instanceof Long || values instanceof Double || values instanceof String || values instanceof Boolean) {
// The Jackson json parser can generate for numerics - Integers, Longs, BigIntegers (if Long is not enough)
// and BigDecimal (if Double is not enough)
if (values instanceof Number
|| values instanceof String
|| values instanceof Boolean) {
return values;
}
throw new SqlIllegalArgumentException("Type {} (returned by [{}]) is not supported", values.getClass().getSimpleName(), fieldName);
Expand Down
Expand Up @@ -18,6 +18,8 @@
import org.elasticsearch.xpack.sql.util.DateUtils;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -126,7 +128,7 @@ public void testGetDottedValueWithSource() throws Exception {
BytesReference sourceRef = BytesReference.bytes(source);
hit.sourceRef(sourceRef);
Object extract = extractor.extract(hit);
assertEquals(hasSource ? value : null, extract);
assertFieldHitEquals(hasSource ? value : null, extract);
}
}

Expand Down Expand Up @@ -179,7 +181,7 @@ public void testGetSource() throws IOException {
source.endObject();
BytesReference sourceRef = BytesReference.bytes(source);
hit.sourceRef(sourceRef);
assertEquals(value, extractor.extract(hit));
assertFieldHitEquals(value, extractor.extract(hit));
}
}

Expand Down Expand Up @@ -225,7 +227,7 @@ public void testSingleValueArrayInSource() throws IOException {
source.endObject();
BytesReference sourceRef = BytesReference.bytes(source);
hit.sourceRef(sourceRef);
assertEquals(value, fe.extract(hit));
assertFieldHitEquals(value, fe.extract(hit));
}

public void testExtractSourcePath() {
Expand Down Expand Up @@ -460,6 +462,9 @@ private Object randomValue() {
() -> randomAlphaOfLength(10),
ESTestCase::randomLong,
ESTestCase::randomDouble,
ESTestCase::randomInt,
() -> BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.ONE),
() -> new BigDecimal("20012312345621343256123456254.20012312345621343256123456254"),
() -> null));
return value.get();
}
Expand All @@ -468,7 +473,20 @@ private Object randomNonNullValue() {
Supplier<Object> value = randomFrom(Arrays.asList(
() -> randomAlphaOfLength(10),
ESTestCase::randomLong,
ESTestCase::randomDouble));
ESTestCase::randomDouble,
ESTestCase::randomInt,
() -> BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.ONE),
() -> new BigDecimal("20012312345621343256123456254.20012312345621343256123456254")));
return value.get();
}

private void assertFieldHitEquals(Object expected, Object actual) {
if (expected instanceof BigDecimal) {
// parsing will, by default, build a Double even if the initial value is BigDecimal
// Elasticsearch does this the same when returning the results
assertEquals(((BigDecimal) expected).doubleValue(), actual);
} else {
assertEquals(expected, actual);
}
}
}

0 comments on commit 0ed42d6

Please sign in to comment.