--
-- MULTI_ORDERBY_LIMIT_PUSHDOWN
--
-- order by pushdown with aggregates
-- tests order by when pushing down order by clauses including aggregate
-- aggregate expressions.
SELECT user_id, avg(value_1)
FROM users_table
GROUP BY user_id
ORDER BY avg(value_1) DESC
LIMIT 5;
 user_id |        avg
---------------------------------------------------------------------
       1 | 3.2857142857142857
       4 | 2.7391304347826087
       5 | 2.6538461538461538
       3 | 2.3529411764705882
       2 | 2.3333333333333333
(5 rows)

SELECT user_id, avg(value_1)
FROM users_table
GROUP BY user_id
ORDER BY avg(value_1) DESC
LIMIT 1;
 user_id |        avg
---------------------------------------------------------------------
       1 | 3.2857142857142857
(1 row)

EXPLAIN (COSTS OFF)
SELECT user_id, avg(value_1)
FROM users_table
GROUP BY user_id
ORDER BY avg(value_1) DESC
LIMIT 1;
                                      QUERY PLAN
---------------------------------------------------------------------
 Limit
   ->  Sort
         Sort Key: remote_scan.avg DESC
         ->  Custom Scan (Citus Adaptive)
               Task Count: 4
               Tasks Shown: One of 4
               ->  Task
                     Node: host=localhost port=xxxxx dbname=regression
                     ->  Limit
                           ->  Sort
                                 Sort Key: (avg(value_1)) DESC
                                 ->  HashAggregate
                                       Group Key: user_id
                                       ->  Seq Scan on users_table_1400256 users_table
(14 rows)

SELECT user_id, avg(value_1) + 1
FROM users_table
GROUP BY user_id
ORDER BY avg(value_1) + 1 DESC
LIMIT 1;
 user_id |      ?column?
---------------------------------------------------------------------
       1 | 4.2857142857142857
(1 row)

SELECT user_id, avg(value_1)
FROM users_table
GROUP BY user_id
ORDER BY avg(value_1) + 1 DESC
LIMIT 1;
 user_id |        avg
---------------------------------------------------------------------
       1 | 3.2857142857142857
(1 row)

SELECT user_id, avg(value_1) + sum(value_2)
FROM users_table
GROUP BY user_id
ORDER BY 2  DESC
LIMIT 1;
 user_id |      ?column?
---------------------------------------------------------------------
       5 | 65.6538461538461538
(1 row)

SELECT user_id, avg(value_1) + count(value_2)
FROM users_table
GROUP BY user_id
ORDER BY 2  DESC;
 user_id |      ?column?
---------------------------------------------------------------------
       5 | 28.6538461538461538
       4 | 25.7391304347826087
       2 | 20.3333333333333333
       3 | 19.3529411764705882
       6 | 12.1000000000000000
       1 | 10.2857142857142857
(6 rows)

EXPLAIN (COSTS OFF)
SELECT user_id, avg(value_1) + count(value_2)
FROM users_table
GROUP BY user_id
ORDER BY 2  DESC;
                             QUERY PLAN
---------------------------------------------------------------------
 Sort
   Sort Key: remote_scan."?column?" DESC
   ->  Custom Scan (Citus Adaptive)
         Task Count: 4
         Tasks Shown: One of 4
         ->  Task
               Node: host=localhost port=xxxxx dbname=regression
               ->  HashAggregate
                     Group Key: user_id
                     ->  Seq Scan on users_table_1400256 users_table
(10 rows)

SELECT user_id, avg(value_1) + count(value_2)
FROM users_table
GROUP BY user_id
ORDER BY 2  DESC
LIMIT 1;
 user_id |      ?column?
---------------------------------------------------------------------
       5 | 28.6538461538461538
(1 row)

SELECT user_id, sum(value_1) + sum(value_2)
FROM users_table
GROUP BY user_id
ORDER BY 2  DESC
LIMIT 1;
 user_id | ?column?
---------------------------------------------------------------------
       5 |      132
(1 row)

SELECT user_id, sum(value_1) + sum(value_2)
FROM users_table
GROUP BY user_id
ORDER BY sum(value_2)  DESC
LIMIT 1;
 user_id | ?column?
---------------------------------------------------------------------
       5 |      132
(1 row)

SELECT user_id, (100 / max(value_1))
FROM users_table
GROUP BY user_id
ORDER BY 2  DESC, 1 DESC
LIMIT 2;
 user_id | ?column?
---------------------------------------------------------------------
       2 |       25
       6 |       20
(2 rows)

SELECT user_id, (100 / (1 + min(value_1)))
FROM users_table
GROUP BY user_id
ORDER BY 2  DESC, 1
LIMIT 2;
 user_id | ?column?
---------------------------------------------------------------------
       2 |      100
       3 |      100
(2 rows)

SELECT user_id, sum(value_1 + value_2)
FROM users_table
GROUP BY user_id
ORDER BY 2  DESC
LIMIT 2;
 user_id | sum
---------------------------------------------------------------------
       5 | 132
       4 | 113
(2 rows)

SELECT user_id, 10000 / (sum(value_1 + value_2))
FROM users_table
GROUP BY user_id
ORDER BY 2  DESC
LIMIT 2;
 user_id | ?column?
---------------------------------------------------------------------
       6 |      238
       1 |      232
(2 rows)

SELECT user_id, sum(value_1 + value_2)
FROM users_table
GROUP BY user_id
ORDER BY (10000 / (sum(value_1 + value_2)))  DESC
LIMIT 2;
 user_id | sum
---------------------------------------------------------------------
       6 |  42
       1 |  43
(2 rows)

SELECT user_id
FROM users_table
GROUP BY user_id
ORDER BY (10000 / (sum(value_1 + value_2)))  DESC
LIMIT 2;
 user_id
---------------------------------------------------------------------
       6
       1
(2 rows)

EXPLAIN (COSTS OFF)
SELECT user_id
FROM users_table
GROUP BY user_id
ORDER BY (10000 / (sum(value_1 + value_2)))  DESC
LIMIT 2;
                                      QUERY PLAN
---------------------------------------------------------------------
 Limit
   ->  Sort
         Sort Key: remote_scan.worker_column_2 DESC
         ->  Custom Scan (Citus Adaptive)
               Task Count: 4
               Tasks Shown: One of 4
               ->  Task
                     Node: host=localhost port=xxxxx dbname=regression
                     ->  Limit
                           ->  Sort
                                 Sort Key: ((10000 / sum((value_1 + value_2)))) DESC
                                 ->  HashAggregate
                                       Group Key: user_id
                                       ->  Seq Scan on users_table_1400256 users_table
(14 rows)

SELECT 10000 / (sum(value_1 + value_2))
FROM users_table
ORDER BY 1 DESC
LIMIT 2;
 ?column?
---------------------------------------------------------------------
       19
(1 row)

SELECT user_id, AVG(value_1)
FROM users_table
GROUP BY user_id
ORDER BY user_id * avg(value_1) DESC
LIMIT 2;
 user_id |        avg
---------------------------------------------------------------------
       5 | 2.6538461538461538
       6 | 2.1000000000000000
(2 rows)

SELECT user_id, AVG(value_1)
FROM users_table
GROUP BY user_id
ORDER BY user_id * avg(value_1 + value_2) DESC
LIMIT 2;
 user_id |        avg
---------------------------------------------------------------------
       5 | 2.6538461538461538
       6 | 2.1000000000000000
(2 rows)

SELECT user_id
FROM users_table
GROUP BY user_id
ORDER BY sum(value_1) DESC
LIMIT 2;
 user_id
---------------------------------------------------------------------
       5
       4
(2 rows)

EXPLAIN (COSTS OFF)
SELECT user_id
FROM users_table
GROUP BY user_id
ORDER BY sum(value_1) DESC
LIMIT 2;
                                      QUERY PLAN
---------------------------------------------------------------------
 Limit
   ->  Sort
         Sort Key: remote_scan.worker_column_2 DESC
         ->  Custom Scan (Citus Adaptive)
               Task Count: 4
               Tasks Shown: One of 4
               ->  Task
                     Node: host=localhost port=xxxxx dbname=regression
                     ->  Limit
                           ->  Sort
                                 Sort Key: (sum(value_1)) DESC
                                 ->  HashAggregate
                                       Group Key: user_id
                                       ->  Seq Scan on users_table_1400256 users_table
(14 rows)

SELECT ut.user_id, avg(ut.value_2)
FROM users_table ut, events_table et
WHERE ut.user_id = et.user_id and et.value_2 < 5
GROUP BY ut.user_id
ORDER BY MAX(et.time), AVG(ut.value_1)
LIMIT 5;
 user_id |        avg
---------------------------------------------------------------------
       6 | 2.1000000000000000
       2 | 2.7777777777777778
       5 | 2.4230769230769231
       3 | 3.2352941176470588
       4 | 2.1739130434782609
(5 rows)

EXPLAIN (COSTS OFF)
SELECT ut.user_id, avg(ut.value_2)
FROM users_table ut, events_table et
WHERE ut.user_id = et.user_id and et.value_2 < 5
GROUP BY ut.user_id
ORDER BY MAX(et.time), AVG(ut.value_1)
LIMIT 5;
                                        QUERY PLAN
---------------------------------------------------------------------
 Limit
   ->  Sort
         Sort Key: remote_scan.worker_column_3, remote_scan.worker_column_4
         ->  Custom Scan (Citus Adaptive)
               Task Count: 4
               Tasks Shown: One of 4
               ->  Task
                     Node: host=localhost port=xxxxx dbname=regression
                     ->  Limit
                           ->  Sort
                                 Sort Key: (max(et."time")), (avg(ut.value_1))
                                 ->  HashAggregate
                                       Group Key: ut.user_id
                                       ->  Hash Join
                                             Hash Cond: (ut.user_id = et.user_id)
                                             ->  Seq Scan on users_table_1400256 ut
                                             ->  Hash
                                                   ->  Seq Scan on events_table_1400260 et
                                                         Filter: (value_2 < 5)
(19 rows)

SELECT ut.user_id, avg(et.value_2)
FROM users_table ut, events_table et
WHERE ut.user_id = et.user_id and et.value_2 < 5
GROUP BY ut.user_id
ORDER BY avg(ut.value_2) DESC, AVG(et.value_2)
LIMIT 5;
 user_id |        avg
---------------------------------------------------------------------
       3 | 1.8947368421052632
       1 | 2.4615384615384615
       2 | 2.0000000000000000
       5 | 2.2142857142857143
       4 | 2.0666666666666667
(5 rows)

SELECT ut.user_id, count(DISTINCT ut.value_2)
FROM users_table ut, events_table et
WHERE ut.user_id = et.user_id and et.value_2 < 5
GROUP BY ut.user_id
ORDER BY 2, AVG(ut.value_1), 1 DESC
LIMIT 2;
 user_id | count
---------------------------------------------------------------------
       1 |     4
       6 |     5
(2 rows)

EXPLAIN (COSTS OFF)
SELECT ut.user_id, avg(ut.value_2)
FROM users_table ut, events_table et
WHERE ut.user_id = et.user_id and et.value_2 < 5
GROUP BY ut.user_id
ORDER BY 2, AVG(ut.value_1), 1 DESC
LIMIT 5;
                                           QUERY PLAN
---------------------------------------------------------------------
 Limit
   ->  Sort
         Sort Key: remote_scan.avg, remote_scan.worker_column_3, remote_scan.user_id DESC
         ->  Custom Scan (Citus Adaptive)
               Task Count: 4
               Tasks Shown: One of 4
               ->  Task
                     Node: host=localhost port=xxxxx dbname=regression
                     ->  Limit
                           ->  Sort
                                 Sort Key: (avg(ut.value_2)), (avg(ut.value_1)), ut.user_id DESC
                                 ->  HashAggregate
                                       Group Key: ut.user_id
                                       ->  Hash Join
                                             Hash Cond: (ut.user_id = et.user_id)
                                             ->  Seq Scan on users_table_1400256 ut
                                             ->  Hash
                                                   ->  Seq Scan on events_table_1400260 et
                                                         Filter: (value_2 < 5)
(19 rows)

