-- -- 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 SELECT user_id, avg(value_1) FROM users_table GROUP BY user_id ORDER BY avg(value_1) DESC LIMIT 1; ERROR: bogus varattno for OUTER_VAR var: 3 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 SELECT user_id, avg(value_1) + count(value_2) FROM users_table GROUP BY user_id ORDER BY 2 DESC; ERROR: bogus varattno for OUTER_VAR var: 3 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: (10000 / (pg_catalog.sum(((10000 / (pg_catalog.sum(remote_scan.worker_column_2))::bigint))))::bigint) DESC -> HashAggregate Group Key: remote_scan.user_id -> Custom Scan (Citus Real-Time) Task Count: 4 Tasks Shown: One of 4 -> Task Node: host=localhost port=57637 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 (16 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: (pg_catalog.sum(((pg_catalog.sum(remote_scan.worker_column_2))::bigint)))::bigint DESC -> HashAggregate Group Key: remote_scan.user_id -> Custom Scan (Citus Real-Time) Task Count: 4 Tasks Shown: One of 4 -> Task Node: host=localhost port=57637 dbname=regression -> Limit -> Sort Sort Key: (sum(value_1)) DESC -> HashAggregate Group Key: user_id -> Seq Scan on users_table_1400256 users_table (16 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; ERROR: bogus varattno for OUTER_VAR var: 5 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, 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 5; ERROR: bogus varattno for OUTER_VAR var: 4