--
-- MULTI_AGG_TYPE_CONVERSION
--
-- Test aggregate type conversions using sums of integers and division operator
SELECT sum(l_suppkey) FROM lineitem;
   sum    
----------
 60617976
(1 row)

SELECT sum(l_suppkey) / 2 FROM lineitem;
 ?column? 
----------
 30308988
(1 row)

SELECT sum(l_suppkey) / 2::numeric FROM lineitem;
       ?column?        
-----------------------
 30308988.000000000000
(1 row)

SELECT sum(l_suppkey)::int8 / 2 FROM lineitem;
 ?column? 
----------
 30308988
(1 row)

-- Create a new table to test type conversions on different types, and load
-- data into this table. Then, apply aggregate functions and divide / multiply
-- the results to test type conversions.
CREATE TABLE aggregate_type (
       float_value float(20) not null,
       double_value float(40) not null,
       interval_value interval not null);
SELECT create_distributed_table('aggregate_type', 'float_value', 'append');
 create_distributed_table 
--------------------------
 
(1 row)

\copy aggregate_type FROM '@abs_srcdir@/data/agg_type.data'
-- Test conversions using aggregates on floats and division
SELECT min(float_value), max(float_value),
       sum(float_value), count(float_value), avg(float_value)
FROM aggregate_type;
 min | max | sum  | count |  avg  
-----+-----+------+-------+-------
   1 | 4.5 | 10.5 |     4 | 2.625
(1 row)

SELECT min(float_value) / 2, max(float_value) / 2,
       sum(float_value) / 2, count(float_value) / 2, avg(float_value) / 2
FROM aggregate_type;
 ?column? | ?column? | ?column? | ?column? | ?column? 
----------+----------+----------+----------+----------
      0.5 |     2.25 |     5.25 |        2 |   1.3125
(1 row)

-- Test conversions using aggregates on large floats and multiplication
SELECT min(double_value), max(double_value),
       sum(double_value), count(double_value), avg(double_value)
FROM aggregate_type;
  min  |   max   |   sum    | count |    avg    
-------+---------+----------+-------+-----------
 2.343 | 6.34343 | 15.79703 |     4 | 3.9492575
(1 row)

SELECT min(double_value) * 2, max(double_value) * 2,
       sum(double_value) * 2, count(double_value) * 2, avg(double_value) * 2
FROM aggregate_type;
 ?column? | ?column? | ?column? | ?column? | ?column? 
----------+----------+----------+----------+----------
    4.686 | 12.68686 | 31.59406 |        8 | 7.898515
(1 row)

-- Test conversions using aggregates on intervals and division. We also use the
-- default configuration value for IntervalStyle.
SET IntervalStyle TO 'postgres';
SELECT min(interval_value), max(interval_value),
       sum(interval_value), count(interval_value), avg(interval_value)
FROM aggregate_type;
     min     |    max     |     sum     | count |     avg     
-------------+------------+-------------+-------+-------------
 00:00:23.44 | 00:38:52.9 | 01:23:33.64 |     4 | 00:20:53.41
(1 row)

SELECT min(interval_value) / 2, max(interval_value) / 2,
       sum(interval_value) / 2, count(interval_value) / 2, avg(interval_value) / 2
FROM aggregate_type;
  ?column?   |  ?column?   |  ?column?   | ?column? |   ?column?   
-------------+-------------+-------------+----------+--------------
 00:00:11.72 | 00:19:26.45 | 00:41:46.82 |        2 | 00:10:26.705
(1 row)

