jonkeane commented on pull request #11754:
URL: https://github.com/apache/arrow/pull/11754#issuecomment-975940747


   You mention this in the Jira, but could we add in the cast back to decimal 
proactively? What I'm seeing in postgres (haven't tried others yet) is it 
"just" blows up the precision on the decimal but does still return a decimal 
with `AVG`.
   
   If someone has a decimal already it seems odd that it would return a double 
when a decimal result is possible (it's not quite like division of integers 
where you have to return something with more precision than an integer there). 
Even of (for now? until someone complains?) we cast back to the same decimal 
precision as the input, I think that would be more natural for what operations 
on decimals like this behave like elsewhere.
   
   ```
   postgres=# create table df (dec decimal(3, 2));
                   insert into df (dec) values (0.01);
                   insert into df (dec) values (0.02);
                   insert into df (dec) values (0.02);
                   insert into df (dec) values (0.02);
   CREATE TABLE
   INSERT 0 1
   INSERT 0 1
   INSERT 0 1
   INSERT 0 1
   postgres=# alter table df add column flt float;
                   insert into df (flt) values (0.01);
                   insert into df (flt) values (0.02);
                   insert into df (flt) values (0.02);
                   insert into df (flt) values (0.02);
   ALTER TABLE
   INSERT 0 1
   INSERT 0 1
   INSERT 0 1
   INSERT 0 1
   postgres=# select pg_typeof(dec), pg_typeof(flt) from df;
    pg_typeof |    pg_typeof     
   -----------+------------------
    numeric   | double precision
    numeric   | double precision
    numeric   | double precision
    numeric   | double precision
    numeric   | double precision
    numeric   | double precision
    numeric   | double precision
    numeric   | double precision
   (8 rows)
   
   postgres=# select AVG(dec), pg_typeof(AVG(dec)), AVG(flt), 
pg_typeof(AVG(flt)) from df;
             avg           | pg_typeof |  avg   |    pg_typeof     
   ------------------------+-----------+--------+------------------
    0.01750000000000000000 | numeric   | 0.0175 | double precision
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to