<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>question Re: Wrong results dividing decimal by integer in Support Questions</title>
    <link>https://community.cloudera.com/t5/Support-Questions/Wrong-results-dividing-decimal-by-integer/m-p/294447#M217264</link>
    <description>&lt;P&gt;We did a wholesale revamp of decimal behaviour going from CDH5 to CDH6. The default behaviour all changed in CDH6.0: &lt;A href="https://docs.cloudera.com/documentation/enterprise/6/release-notes/topics/rg_cdh_600_new_features.html#decimal_v2" target="_blank"&gt;https://docs.cloudera.com/documentation/enterprise/6/release-notes/topics/rg_cdh_600_new_features.html#decimal_v2&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There's a whole epic JIRA capturing the changes: &lt;A href="https://issues.apache.org/jira/browse/IMPALA-4072" target="_blank"&gt;https://issues.apache.org/jira/browse/IMPALA-4072&lt;/A&gt; . I think &lt;A href="https://issues.apache.org/jira/browse/IMPALA-4370" target="_blank"&gt;https://issues.apache.org/jira/browse/IMPALA-4370&lt;/A&gt; might be the specific fix that you're seeing, based on your analysis. The fix version for that change is Impala 2.9.0, so the code change is in CDH5.15.2, but it was done behind the DECIMAL_V2 query option, which wasn't a supported option until CDH6.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;IN CDH6 you can toggle the behaviour with the DECIMAL_V2 query option (it will eventually be removed, but was kept for backward compatibility).&lt;/P&gt;</description>
    <pubDate>Tue, 21 Apr 2020 20:03:35 GMT</pubDate>
    <dc:creator>Tim Armstrong</dc:creator>
    <dc:date>2020-04-21T20:03:35Z</dc:date>
    <item>
      <title>Wrong results dividing decimal by integer</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Wrong-results-dividing-decimal-by-integer/m-p/294446#M217263</link>
      <description>&lt;H1&gt;Test Case&lt;/H1&gt;&lt;H2&gt;Setup&lt;/H2&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;create table n1 (id STRING)
stored as parquet;

insert into n1 values ('-1234');
insert into n1 values ('7890');&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;H2&gt;Results&lt;/H2&gt;&lt;H3&gt;impalad version 2.12.0-cdh5.15.2&lt;/H3&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;SELECT  `id`
,       CAST(`id` AS DECIMAL(38,0)) / 20 * 20 AS D0 
,       CAST(`id` AS DECIMAL(38,0)) / 20.0 * 20 AS D1
FROM    n1
ORDER BY `id`;

+----------+-------+---------+
| id       | d0    | d1      |
+----------+-------+---------+
| -1234    | -1220 | -1234.0 |
| 7890     | 7880  | 7890.0  |
+----------+-------+---------+
Fetched 4 row(s) in 0.56s&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When we are casting the "id" column to a decimal without scale and dividing by an integer, even though the answer should have a scale (-1234 / 20 = -61.7) we lose the scale and the result is truncated to -61. This is then multiplied by 20 to give -1220 which is column d0. This does not get us back to the same value that we started with which was -1234.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When we divide by a decimal value (20.0) the result of -61.7 is preserved, which gets us back to the correct value of the "id" in column d1.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;H3&gt;impalad version 3.2.0-cdh6.2.0&lt;/H3&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;SELECT  `id`
,       CAST(`id` AS DECIMAL(38,0)) / 20 * 20 AS D0 
,       CAST(`id` AS DECIMAL(38,0)) / 20.0 * 20 AS D1
FROM    n1
ORDER BY `id`;

+-------+--------------+--------------+
| id    | d0           | d1           |
+-------+--------------+--------------+
| -1234 | -1234.000000 | -1234.000000 |
| 7890  | 7890.000000  | 7890.000000  |
+-------+--------------+--------------+&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Notice the change in behaviour with this later version of Impala.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This looks to me like a bug that has been fixed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Could you please confirm and point me to the IMPALA Jira that explains this bug?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Apr 2020 19:56:24 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Wrong-results-dividing-decimal-by-integer/m-p/294446#M217263</guid>
      <dc:creator>ssenior45</dc:creator>
      <dc:date>2020-04-21T19:56:24Z</dc:date>
    </item>
    <item>
      <title>Re: Wrong results dividing decimal by integer</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Wrong-results-dividing-decimal-by-integer/m-p/294447#M217264</link>
      <description>&lt;P&gt;We did a wholesale revamp of decimal behaviour going from CDH5 to CDH6. The default behaviour all changed in CDH6.0: &lt;A href="https://docs.cloudera.com/documentation/enterprise/6/release-notes/topics/rg_cdh_600_new_features.html#decimal_v2" target="_blank"&gt;https://docs.cloudera.com/documentation/enterprise/6/release-notes/topics/rg_cdh_600_new_features.html#decimal_v2&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There's a whole epic JIRA capturing the changes: &lt;A href="https://issues.apache.org/jira/browse/IMPALA-4072" target="_blank"&gt;https://issues.apache.org/jira/browse/IMPALA-4072&lt;/A&gt; . I think &lt;A href="https://issues.apache.org/jira/browse/IMPALA-4370" target="_blank"&gt;https://issues.apache.org/jira/browse/IMPALA-4370&lt;/A&gt; might be the specific fix that you're seeing, based on your analysis. The fix version for that change is Impala 2.9.0, so the code change is in CDH5.15.2, but it was done behind the DECIMAL_V2 query option, which wasn't a supported option until CDH6.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;IN CDH6 you can toggle the behaviour with the DECIMAL_V2 query option (it will eventually be removed, but was kept for backward compatibility).&lt;/P&gt;</description>
      <pubDate>Tue, 21 Apr 2020 20:03:35 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Wrong-results-dividing-decimal-by-integer/m-p/294447#M217264</guid>
      <dc:creator>Tim Armstrong</dc:creator>
      <dc:date>2020-04-21T20:03:35Z</dc:date>
    </item>
  </channel>
</rss>

