<?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: Impala round function does not return expected result in Archives of Support Questions (Read Only)</title>
    <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/Impala-round-function-does-not-return-expected-result/m-p/81852#M75168</link>
    <description>&lt;P&gt;Hi Nitin&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was working on similar problem of dynamic rounding and found&amp;nbsp;a&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;workaround. In my case I do average price statistics and I want to round average prices to 3 significant digits, in order to group similar observations. Now every price has different number of digits, so e.g. I must round 1.495 to 1.50, but also 1099 should be rounded to 1100.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Following expression did the trick for me:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;SELECT ROUND(`price` / POWER(10.0, (FLOOR(LOG10(`price`)) - 2)), 0) * POWER(10.0, (FLOOR(LOG10(`price`)) - 2)) AS `price_rounded`&lt;/PRE&gt;&lt;P&gt;The&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;EM&gt;-2&lt;/EM&gt;&amp;nbsp;constant is related to my specific problem, i.e. 3 significant digits, so that could be adjusted or omitted in other use case.&lt;/P&gt;&lt;P&gt;Conceptually what I do is that I&amp;nbsp;reflect number of digits to round on by transforming the&amp;nbsp;first argument, then I can always round to zero digits no matter what, and finally transform data back.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;May work better than CASE WHEN in situation when you don't know dimensions of your data upfront.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;BR,&lt;/P&gt;&lt;P&gt;Mirek&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 03 Nov 2018 11:27:32 GMT</pubDate>
    <dc:creator>mirekkolar</dc:creator>
    <dc:date>2018-11-03T11:27:32Z</dc:date>
    <item>
      <title>Impala round function does not return expected result</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/Impala-round-function-does-not-return-expected-result/m-p/65029#M75163</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm using impala-2.7.0-cdh5.10.0, and I find execute round() in impala-shell may give unexpected result, while hive can give correct result.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;e.g.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;impala&amp;gt; select round(269586/334026 * 100, 2);
80.70999999999999

hive &amp;gt; select round(269586/334026 * 100, 2);
80.71&lt;/PRE&gt;&lt;P&gt;Is this a known issue?&lt;/P&gt;</description>
      <pubDate>Fri, 16 Sep 2022 12:55:14 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/Impala-round-function-does-not-return-expected-result/m-p/65029#M75163</guid>
      <dc:creator>linhao</dc:creator>
      <dc:date>2022-09-16T12:55:14Z</dc:date>
    </item>
    <item>
      <title>Re: Impala round function does not return expected result</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/Impala-round-function-does-not-return-expected-result/m-p/65050#M75164</link>
      <description>&lt;P&gt;Looks like a client display issue. Both Impala and Hive return a DOUBLE for round(). A DOUBLE cannot&amp;nbsp;precisely represent 80.71 so I'm guessing that technically the results are the same, it's just that the Hive client "pretty prints" the results in a different way than Impala's.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you require precise rouding results it's recommended you use decimal:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;select round(cast(269586 / 334026 * 100 as decimal(17, 15)), 2);&lt;/P&gt;&lt;P&gt;80.71&lt;/P&gt;</description>
      <pubDate>Thu, 01 Mar 2018 17:22:21 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/Impala-round-function-does-not-return-expected-result/m-p/65050#M75164</guid>
      <dc:creator>alex.behm</dc:creator>
      <dc:date>2018-03-01T17:22:21Z</dc:date>
    </item>
    <item>
      <title>Re: Impala round function does not return expected result</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/Impala-round-function-does-not-return-expected-result/m-p/80200#M75165</link>
      <description>&lt;P&gt;Hi Alex,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;it works using the solution you provided, however if i use variable/column to indicate the number of digits after decimal point then it throws an error, which is :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;"AnalysisException: round() must be called with a constant second argument."&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;e.g Below query works perfectly fine&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;select round(cast(test1 as decimal(17,15)),2) as test3&lt;BR /&gt;from (&lt;/P&gt;&lt;P&gt;select round(269586/334026 * 100,2) as test1,&lt;BR /&gt;1 as test2&lt;BR /&gt;)A&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However this query does not work:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;select round(cast(test1 as decimal(17,15)),test2) as test3&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;from (&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;select round(269586/334026 * 100,2) as test1,&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;1 as test2&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;)A&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;the problem lies with the decimal typecast, if i remove it then issue will disappaear however the original issue will appear.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please help, i have been struggling for quite some time with this issue. for my requirement i need to use column name to indicate numbers of digits to round, because the same field(column) may have numbers rounded off to various decimal digits.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;Nitin&lt;/P&gt;</description>
      <pubDate>Fri, 21 Sep 2018 19:25:57 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/Impala-round-function-does-not-return-expected-result/m-p/80200#M75165</guid>
      <dc:creator>nitinagr</dc:creator>
      <dc:date>2018-09-21T19:25:57Z</dc:date>
    </item>
    <item>
      <title>Re: Impala round function does not return expected result</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/Impala-round-function-does-not-return-expected-result/m-p/80257#M75166</link>
      <description>Hi &lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/29446"&gt;@nitinagr&lt;/a&gt;. You pretty much have two options here right now: * Use a constant second argument * Cast the first input to DOUBLE (which means you may lose some precision if the values can't be exactly represented in a DOUBLE type). I think supporting this seems like a valid ask so I went ahead and filed a JIRA: &lt;A href="https://issues.apache.org/jira/browse/IMPALA-7613" target="_blank"&gt;https://issues.apache.org/jira/browse/IMPALA-7613&lt;/A&gt; . I remember a while back debating with someone about whether users were likely to do data-driven rounding, so I think you just settled that argument!</description>
      <pubDate>Mon, 24 Sep 2018 18:11:08 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/Impala-round-function-does-not-return-expected-result/m-p/80257#M75166</guid>
      <dc:creator>Tim Armstrong</dc:creator>
      <dc:date>2018-09-24T18:11:08Z</dc:date>
    </item>
    <item>
      <title>Re: Impala round function does not return expected result</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/Impala-round-function-does-not-return-expected-result/m-p/80300#M75167</link>
      <description>&lt;P&gt;Thank you Tim.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Double is not working in my case, currently i am using case statement to get the desired result (i need maximum 2 digits after decimal, so i have only 3 scenarios in case statement).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Looking forward to the fix.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Nitin&lt;/P&gt;</description>
      <pubDate>Tue, 25 Sep 2018 16:20:27 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/Impala-round-function-does-not-return-expected-result/m-p/80300#M75167</guid>
      <dc:creator>nitinagr</dc:creator>
      <dc:date>2018-09-25T16:20:27Z</dc:date>
    </item>
    <item>
      <title>Re: Impala round function does not return expected result</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/Impala-round-function-does-not-return-expected-result/m-p/81852#M75168</link>
      <description>&lt;P&gt;Hi Nitin&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was working on similar problem of dynamic rounding and found&amp;nbsp;a&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;workaround. In my case I do average price statistics and I want to round average prices to 3 significant digits, in order to group similar observations. Now every price has different number of digits, so e.g. I must round 1.495 to 1.50, but also 1099 should be rounded to 1100.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Following expression did the trick for me:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;SELECT ROUND(`price` / POWER(10.0, (FLOOR(LOG10(`price`)) - 2)), 0) * POWER(10.0, (FLOOR(LOG10(`price`)) - 2)) AS `price_rounded`&lt;/PRE&gt;&lt;P&gt;The&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;EM&gt;-2&lt;/EM&gt;&amp;nbsp;constant is related to my specific problem, i.e. 3 significant digits, so that could be adjusted or omitted in other use case.&lt;/P&gt;&lt;P&gt;Conceptually what I do is that I&amp;nbsp;reflect number of digits to round on by transforming the&amp;nbsp;first argument, then I can always round to zero digits no matter what, and finally transform data back.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;May work better than CASE WHEN in situation when you don't know dimensions of your data upfront.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;BR,&lt;/P&gt;&lt;P&gt;Mirek&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 03 Nov 2018 11:27:32 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/Impala-round-function-does-not-return-expected-result/m-p/81852#M75168</guid>
      <dc:creator>mirekkolar</dc:creator>
      <dc:date>2018-11-03T11:27:32Z</dc:date>
    </item>
    <item>
      <title>Re: Impala round function does not return expected result</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/Impala-round-function-does-not-return-expected-result/m-p/82535#M75169</link>
      <description>impala&amp;gt; select 269586/334026 * 100;&lt;BR /&gt;80.708088591906019&lt;BR /&gt;&lt;BR /&gt;impala&amp;gt; select round(269586/334026 * 100, 2);&lt;BR /&gt;80.70999999999999&lt;BR /&gt;&lt;BR /&gt;I'm curious as to why this isn't 80.71000000000000&lt;BR /&gt;</description>
      <pubDate>Sun, 18 Nov 2018 14:53:17 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/Impala-round-function-does-not-return-expected-result/m-p/82535#M75169</guid>
      <dc:creator>scuffster</dc:creator>
      <dc:date>2018-11-18T14:53:17Z</dc:date>
    </item>
    <item>
      <title>Re: Impala round function does not return expected result</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/Impala-round-function-does-not-return-expected-result/m-p/82588#M75170</link>
      <description>&lt;P&gt;Hi &lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/30673"&gt;@scuffster&lt;/a&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There are some interesting issues here with the different numeric data types here - INT, DOUBLE, DECIMAL, etc. The behaviour you're seeing is because the first input to round() is a DOUBLE expression, which cannot exactly represent all decimal values. Generally the output type of the round() function is the same as the input type.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Impala does support precise decimal arithmetic with the DECIMAL type. If you are operating on DECIMAL columns or you cast the input to a decimal type with the right precision and scale, you may get the behaviour you're hoping for. Here's a query showing the type of your expressions and an alternative version with a cast to DECIMAL:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&amp;gt; select typeof(269586/334026 * 100), typeof(round(269586/334026 * 100, 2)), round(269586/334026 * 100, 2), round(cast(269586/334026 * 100 as DECIMAL(20, 8)), 2);

+-------------------------------+-----------------------------------------+---------------------------------+--------------------------------------------------------+
| typeof(269586 / 334026 * 100) | typeof(round(269586 / 334026 * 100, 2)) | round(269586 / 334026 * 100, 2) | round(cast(269586 / 334026 * 100 as decimal(20,8)), 2) |
+-------------------------------+-----------------------------------------+---------------------------------+--------------------------------------------------------+
| DOUBLE                        | DOUBLE                                  | 80.70999999999999               | 80.71                                                  |
+-------------------------------+-----------------------------------------+---------------------------------+--------------------------------------------------------+&lt;/PRE&gt;</description>
      <pubDate>Mon, 19 Nov 2018 22:24:20 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/Impala-round-function-does-not-return-expected-result/m-p/82588#M75170</guid>
      <dc:creator>Tim Armstrong</dc:creator>
      <dc:date>2018-11-19T22:24:20Z</dc:date>
    </item>
  </channel>
</rss>

