question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Presto is not rounding decimals correctly.

See original GitHub issue

We are currently moving from Hive EMR to Athena Presto. During testing and validation, we noticed penny variances on transactions. Root cause analysis shows that Presto is not rounding correctly beyond 14 digits of decimal precision.

Attempts to resolve these variances only increase the total number of variances by causing more penny variances on other records. So, we solve one record’s value, but create [1 + n] more problems elsewhere. Is there any way to correct for this other than moving these calculations into an ETL engine and handling it there?

Note: the input values to the ROUND() function are, themselves, errors on floating point operations while we are multiplying two decimal columns together on-the-fly in a SELECT statement.

Expected result from below sample calculation: 6.98

-- 15 digits of decimal precision.
SELECT ROUND(6.984999999999999, 2)  -- Hive (EMR):       6.98
SELECT ROUND(6.984999999999999, 2)  -- Presto (Athena):  6.99

-- Remove one level of precision (i.e., 14).
SELECT ROUND(6.98499999999999, 2)   -- Presto (Athena):  6.98

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jharringtonCouponscommented, Jun 16, 2022

A quirk of Athena, which is not the default behavior in most presto clusters- is that literal values in the query string like 1.0 have an inferred type of DOUBLE instead of DECIMAL, so ROUND(6.984999999999999, 2) is rounding using double arithmetic which loses precision. If instead you force the type to be DECIMAL, then the correct result is returned

I did not know that about Athena. Thank you for raising that awareness with me and explaining how Athena is operating under the hood - I greatly appreciate it!

I will go ahead and close the ticket. Take care. Best regards.

1reaction
ClarenceThreepwoodcommented, Jun 15, 2022

I see the following expected behavior on Presto master (0.273). It is possible that this issue has been fixed since version (0.217).

presto> SELECT ROUND(6.984999999999999, 2);
       _col0
-------------------
 6.980000000000000
(1 row)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Decimal Functions and Operators - Presto
The precision of DECIMAL type for literal will be equal to number of digits in literal (including trailing and leading zeros). The scale...
Read more >
Presto query not getting the correct values - Stack Overflow
I have written the below query: SELECT min(s6) AS Mins6, max(s6) AS Maxs6, partition_0, partition_1 FROM wqmram WHERE cast(s6 AS decimal(30 ...
Read more >
ROUND function - Amazon Redshift
Rounds numbers to the nearest integer or decimal. The ROUND function can include a second argument indicating the number of decimal places for...
Read more >
[Analytics] Query big decimal column/scale option not working?
I have a currency entity that has a component attribute amount that is a bigdec. I specified the scale as :currency/amount {:scale {:amount...
Read more >
Presto: The Definitive Guide - Starburst Data
setup so you can start using Presto successfully as quickly as ... you are actually troubleshooting to avoid creating large numbers of.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found