From 036f9c4a557b8c32b1a5f609dc74065ff3a3ea1e Mon Sep 17 00:00:00 2001 From: Andrei Stefan Date: Mon, 10 Jun 2019 09:01:42 +0300 Subject: [PATCH] SQL: cover the Integer type when extracting values from _source (#42859) * Take into consideration a wider range of Numbers when extracting the values from source, more specifically - BigInteger and BigDecimal. (cherry picked from commit 561b8d73dd7b03c50242e4e3f0128b2142959176) --- .../search/extractor/FieldHitExtractor.java | 6 ++++- .../extractor/FieldHitExtractorTests.java | 26 ++++++++++++++++--- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/extractor/FieldHitExtractor.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/extractor/FieldHitExtractor.java index 13294fbca22..5446ff2493e 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/extractor/FieldHitExtractor.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/extractor/FieldHitExtractor.java @@ -165,7 +165,11 @@ public class FieldHitExtractor implements HitExtractor { 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); diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/FieldHitExtractorTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/FieldHitExtractorTests.java index 50a3b185dba..d4ea80485f4 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/FieldHitExtractorTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/FieldHitExtractorTests.java @@ -19,6 +19,8 @@ import org.elasticsearch.xpack.sql.type.DataType; 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; @@ -127,7 +129,7 @@ public class FieldHitExtractorTests extends AbstractWireSerializingTestCase randomAlphaOfLength(10), ESTestCase::randomLong, ESTestCase::randomDouble, + ESTestCase::randomInt, + () -> BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.ONE), + () -> new BigDecimal("20012312345621343256123456254.20012312345621343256123456254"), () -> null)); return value.get(); } @@ -588,9 +593,22 @@ public class FieldHitExtractorTests extends AbstractWireSerializingTestCase 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); + } + } private Object randomPoint(double lat, double lon) { Supplier value = randomFrom(Arrays.asList(