# \[bug\] big decimal value losed precision

**URL:** <https://discuss.elastic.co/t/bug-big-decimal-value-losed-precision/253105>\
**Category:** Elasticsearch\
**Created:** [October 23, 2020, 9:13am UTC](https://discuss.elastic.co/t/bug-big-decimal-value-losed-precision/253105 "2020-10-23T09:13:54Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![vpzlin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vpzlin/32/77843_2.png) [@vpzlin](https://discuss.elastic.co/u/vpzlin)\
**Post date:** [October 23, 2020, 9:13am UTC](https://discuss.elastic.co/t/bug-big-decimal-value-losed-precision/253105/1 "2020-10-23T09:13:54Z")

</div>

Hello, ES team!  
There is a precision losing problem when I'm using java client of ElasticSearch.  
Will you fix this bug recently? We do need a big decimal in developing financial system.  
Thanks very much!

[Development Environment]  
JDK: 1.8  
ElasticSearch Server Version: 6.4.2 / 6.8.9 / 7.9.0  
ElasticSearch Client Version: 6.4.2 / 6.8.9 / 7.9.0

[Mapping]  
PUT test\_idx/\_doc/\_mapping  
{  
"properties": {  
"balance":{"type": "double"}  
}  
}

[Main Codes & Results -- version of java client and ES server: 6.4.2 / 6.8.9]  
BigDecimal val = new BigDecimal("12345678901234567890.12345");  
JSONObject jsonObject = new JSONObject();  
jsonObject.put("balance", val);

```
/* Single Put */
// The field 'balance' in curl result by shell showed the correct value '12345678901234567890.12345'
client.index(new IndexRequest("test_idx", "_doc", "1").source(jsonObject.toJSONString(), XContentType.JSON)
        ,RequestOptions.DEFAULT);

/* Bulk Load */
// The field 'balance' in curl result by shell showed the precision losing value '1.2345678901234567E19'
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.add(new IndexRequest("test_idx", "_doc", "2").source(jsonObject.toJSONString(), XContentType.JSON));
client.bulk(bulkRequest, RequestOptions.DEFAULT);

```

[Main Codes & Results -- version of java client and ES server: 7.9.0]  
BigDecimal val = new BigDecimal("12345678901234567890.12345");  
JSONObject jsonObject = new JSONObject();  
jsonObject.put("balance", val);

```
/* Single Put */
// The field 'balance' in curl result by shell showed the correct value '12345678901234567890.12345'
client.index(new IndexRequest("test_idx").source(jsonObject.toJSONString(), XContentType.JSON)
        ,RequestOptions.DEFAULT);

/* Bulk Load */
// The field 'balance' in curl result by shell showed the precision losing value '1.2345678901234567E19'
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.add(new IndexRequest("test_idx").source(jsonObject.toJSONString(), XContentType.JSON));
client.bulk(bulkRequest, RequestOptions.DEFAULT);

```

[Debug and Modify code in java client of ES 7.9.0]  
From the results above in different version we can see that it'll losing precision when bulk loading data into ES.  
So I debugged code, and I found this bug occured in function '\_parseSlowFloat(int expType)' of class 'jackon-core-2.10.4.jar/com.fasterxml.jackson.core.base.ParserBase', the value of variable 'expType' is always 0(int value) from begging, it havn't changged, so the non-int numeric value will be regarded as type 'double' always.  
And then, I done these:  
[1] downloaded the source code 'jackson-core-2.10.4-sources.jar' and decompressed it into my project.  
[2] altered code  
/\*\*\*\*\*\*\*\*\* before _**/  
if (expType == NR\_BIGDECIMAL) {  
\_numberBigDecimal = \_textBuffer.contentsAsDecimal();  
\_numTypesValid = NR\_BIGDECIMAL;  
} else {  
// Otherwise double has to do  
\_numberDouble = \_textBuffer.contentsAsDouble();  
\_numTypesValid = NR\_DOUBLE;  
}  
/**_ after \*\*\*\*\*\*\*\*\*/  
\_numberBigDecimal = \_textBuffer.contentsAsDecimal();  
\_numTypesValid = NR\_BIGDECIMAL;  
[3] rerun bulk load code to ES, field 'balance' in curl result by shell showed the correct value '12345678901234567890.12345'

---

<div class="post-metadata">

**Author:** ![vpzlin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vpzlin/32/77843_2.png) [@vpzlin](https://discuss.elastic.co/u/vpzlin)\
**Post date:** [October 26, 2020, 12:53am UTC](https://discuss.elastic.co/t/bug-big-decimal-value-losed-precision/253105/2 "2020-10-26T00:53:11Z")

</div>

Hello, is there anybody who would reply to me? 🤔

---

<div class="post-metadata">

**Author:** ![Wolfram\_Haussig](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wolfram_haussig/32/70528_2.png) [@Wolfram\_Haussig](https://discuss.elastic.co/u/Wolfram_Haussig)\
**Post date:** [October 26, 2020, 7:01am UTC](https://discuss.elastic.co/t/bug-big-decimal-value-losed-precision/253105/3 "2020-10-26T07:01:33Z")

</div>

Hi,

Please be patient with the users in this forum - you opened the topic on friday and today is just monday. If you need faster responses to your questions you might want to order a paid license from Elastic which gives you a guaranteed SLA tickets.

Also, I saw that you already created an issue in Github [here](https://github.com/elastic/elasticsearch/issues/64094). As this seems to be a bug you might get a better answer there as this forum is more about discussions and questions.

Best regards  
Wolfram

---

<div class="post-metadata">

**Author:** ![vpzlin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vpzlin/32/77843_2.png) [@vpzlin](https://discuss.elastic.co/u/vpzlin)\
**Post date:** [October 26, 2020, 7:13am UTC](https://discuss.elastic.co/t/bug-big-decimal-value-losed-precision/253105/4 "2020-10-26T07:13:20Z")

</div>

Got it, thanks.  
I will be patient to wait for other guys' replys. 😊

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [October 26, 2020, 7:37am UTC](https://discuss.elastic.co/t/bug-big-decimal-value-losed-precision/253105/5 "2020-10-26T07:37:51Z")

</div>

If I recall correctly I believe there is a limit to the precision JSON supports.

---

<div class="post-metadata">

**Author:** ![vpzlin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vpzlin/32/77843_2.png) [@vpzlin](https://discuss.elastic.co/u/vpzlin)\
**Post date:** [October 26, 2020, 7:51am UTC](https://discuss.elastic.co/t/bug-big-decimal-value-losed-precision/253105/6 "2020-10-26T07:51:24Z")

</div>

Actually sourcing map data to bulk load ES meets the same result: losing pricesion, but putting one by one record of sourcing map data works fine.

---

<div class="post-metadata">

**Author:** ![Ignacio\_Vera](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ignacio_vera/32/36674_2.png) [@Ignacio\_Vera](https://discuss.elastic.co/u/Ignacio_Vera)\
**Post date:** [October 26, 2020, 8:17am UTC](https://discuss.elastic.co/t/bug-big-decimal-value-losed-precision/253105/7 "2020-10-26T08:17:11Z")

</div>

I don't think this is a bug but a limitation, please check this issue:

> <https://github.com/elastic/elasticsearch/issues/46934>
>
> BigDecimal data type:
> For a financial application, we need maximum accuracy and arbitrary precision, even at the expense of performance. scaled\_float doesn't...

---

<div class="post-metadata">

**Author:** ![vpzlin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vpzlin/32/77843_2.png) [@vpzlin](https://discuss.elastic.co/u/vpzlin)\
**Post date:** [October 26, 2020, 9:59am UTC](https://discuss.elastic.co/t/bug-big-decimal-value-losed-precision/253105/8 "2020-10-26T09:59:51Z")

</div>

Thanks for your reply. But I'm still confused why putting single record with bigdecimal to ES works fine, while bulk loading multis loses precision...It's unfair to them 🤣

---

<div class="post-metadata">

**Author:** ![vpzlin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vpzlin/32/77843_2.png) [@vpzlin](https://discuss.elastic.co/u/vpzlin)\
**Post date:** [October 27, 2020, 1:30am UTC](https://discuss.elastic.co/t/bug-big-decimal-value-losed-precision/253105/9 "2020-10-27T01:30:25Z")

</div>

Hi, Ignacio\_Vera.  
I'm still considering it as a bug. Did you see my description above?  
When I downloaded the source code 'jackson-core-2.10.4-sources.jar' and decompressed it into my project, and then altered codes in java class 'ParserBase.java' as below:  
====================================================================  
/\*\*\*\*\*\*\*\*\* codes before \*\*\*\*\*\*\*\*\*/  
if (expType == NR\_BIGDECIMAL) {  
\_numberBigDecimal = \_textBuffer.contentsAsDecimal();  
\_numTypesValid = NR\_BIGDECIMAL;  
} else {  
// Otherwise double has to do  
\_numberDouble = \_textBuffer.contentsAsDouble();  
\_numTypesValid = NR\_DOUBLE;  
}

/\*\*\*\*\*\*\*\*\* codes after \*\*\*\*\*\*\*\*\*/  
\_numberBigDecimal = \_textBuffer.contentsAsDecimal();  
\_numTypesValid = NR\_BIGDECIMAL;  
====================================================================

After finished altering codes, I reran codes of putting single big-decimal record and bulk loaded multi big-decimal records, both of them worked fine, big-decimal value stored correctly into ES(should be queryed by curl shell, not in kibana or web browser).

So, don't you think this is a bug?

---

<div class="post-metadata">

**Author:** ![Ignacio\_Vera](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ignacio_vera/32/36674_2.png) [@Ignacio\_Vera](https://discuss.elastic.co/u/Ignacio_Vera)\
**Post date:** [October 27, 2020, 8:02am UTC](https://discuss.elastic.co/t/bug-big-decimal-value-losed-precision/253105/10 "2020-10-27T08:02:00Z")

</div>

That sounds to me an issue in the Jackson library not in Elasticsearch?

---

<div class="post-metadata">

**Author:** ![vpzlin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vpzlin/32/77843_2.png) [@vpzlin](https://discuss.elastic.co/u/vpzlin)\
**Post date:** [October 28, 2020, 2:09am UTC](https://discuss.elastic.co/t/bug-big-decimal-value-losed-precision/253105/11 "2020-10-28T02:09:56Z")

</div>

I'm not sure it's an issue in the Jackson library, but Elasticsearch stored the precision losing value finally, and it can be revised after altering Jackson library codes. So I used 'bug' in the topic name. 🤣  
I think you can make a try, and then draw a conclusion. Will you?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 25, 2020, 2:10am UTC](https://discuss.elastic.co/t/bug-big-decimal-value-losed-precision/253105/12 "2020-11-25T02:10:10Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
