Merge pull request #1826 from citusdata/regression_data_ax

Regression data is reduced from 10K to 100 for 
events_table and users_table
pull/1835/head
Mehmet Furkan ŞAHİN 2017-11-28 15:16:03 +03:00 committed by GitHub
commit 198438978e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
29 changed files with 3126 additions and 23260 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -12,17 +12,17 @@ FROM (
FROM users_table AS u, FROM users_table AS u,
events_table AS e events_table AS e
WHERE u.user_id = e.user_id WHERE u.user_id = e.user_id
AND u.user_id >= 10 AND u.user_id >= 1
AND u.user_id <= 25 AND u.user_id <= 2
AND e.event_type IN (100, 101, 102) AND e.event_type IN (2, 3)
) t ) t
GROUP BY user_id GROUP BY user_id
) q; ) q;
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
5 | 5 | 15.6000000000000000 2 | 2 | 1.5000000000000000
(1 row) (1 row)
------------------------------------ ------------------------------------
@ -30,7 +30,6 @@ SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results;
-- Funnel grouped by whether or not a user has done an event -- Funnel grouped by whether or not a user has done an event
------------------------------------ ------------------------------------
------------------------------------ ------------------------------------
TRUNCATE agg_results;
INSERT INTO agg_results (user_id, value_1_agg, value_2_agg ) INSERT INTO agg_results (user_id, value_1_agg, value_2_agg )
SELECT user_id, sum(array_length(events_table, 1)), length(hasdone_event) SELECT user_id, sum(array_length(events_table, 1)), length(hasdone_event)
FROM ( FROM (
@ -44,9 +43,9 @@ FROM (
FROM users_table AS u, FROM users_table AS u,
events_table AS e events_table AS e
WHERE u.user_id = e.user_id WHERE u.user_id = e.user_id
AND u.user_id >= 10 AND u.user_id >= 1
AND u.user_id <= 25 AND u.user_id <= 2
AND e.event_type IN (100, 101, 102) AND e.event_type IN (1, 2)
) )
UNION UNION
( (
@ -54,26 +53,25 @@ FROM (
FROM users_table AS u, FROM users_table AS u,
events_table AS e events_table AS e
WHERE u.user_id = e.user_id WHERE u.user_id = e.user_id
AND u.user_id >= 10 AND u.user_id >= 1
AND u.user_id <= 25 AND u.user_id <= 2
AND e.event_type IN (103, 104, 105) AND e.event_type IN (3, 4)
) )
) t1 LEFT JOIN ( ) t1 LEFT JOIN (
SELECT DISTINCT user_id, SELECT DISTINCT user_id,
'Has done event'::TEXT AS hasdone_event 'Has done event'::TEXT AS hasdone_event
FROM events_table AS e FROM events_table AS e
WHERE e.user_id >= 1
WHERE e.user_id >= 10 AND e.user_id <= 2
AND e.user_id <= 25 AND e.event_type IN (5, 6)
AND e.event_type IN (106, 107, 108)
) t2 ON (t1.user_id = t2.user_id) ) t2 ON (t1.user_id = t2.user_id)
GROUP BY t1.user_id, hasdone_event GROUP BY t1.user_id, hasdone_event
) t GROUP BY user_id, hasdone_event; ) t GROUP BY user_id, hasdone_event;
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
8 | 8 | 16.1250000000000000 4 | 2 | 1.5000000000000000
(1 row) (1 row)
------------------------------------ ------------------------------------
@ -81,7 +79,6 @@ SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results;
-- Funnel, grouped by the number of times a user has done an event -- Funnel, grouped by the number of times a user has done an event
------------------------------------ ------------------------------------
------------------------------------ ------------------------------------
TRUNCATE agg_results;
INSERT INTO agg_results (user_id, value_1_agg, value_2_agg) INSERT INTO agg_results (user_id, value_1_agg, value_2_agg)
SELECT SELECT
user_id, user_id,
@ -103,9 +100,9 @@ SELECT
events_table events_table
WHERE WHERE
users_table.user_id = events_table.user_id AND users_table.user_id = events_table.user_id AND
users_table.user_id >= 10 AND users_table.user_id >= 1 AND
users_table.user_id <= 70 AND users_table.user_id <= 3 AND
events_table.event_type > 10 AND events_table.event_type < 12 events_table.event_type > 0 AND events_table.event_type < 2
) )
UNION UNION
(SELECT (SELECT
@ -117,9 +114,9 @@ SELECT
events_table events_table
WHERE WHERE
users_table.user_id = events_table.user_id AND users_table.user_id = events_table.user_id AND
users_table.user_id >= 10 AND users_table.user_id >= 1 AND
users_table.user_id <= 70 AND users_table.user_id <= 3 AND
events_table.event_type > 12 AND events_table.event_type < 14 events_table.event_type > 1 AND events_table.event_type < 3
) )
) AS subquery_1 ) AS subquery_1
LEFT JOIN LEFT JOIN
@ -129,9 +126,9 @@ SELECT
FROM FROM
users_table users_table
WHERE WHERE
user_id >= 10 AND user_id >= 1 AND
user_id <= 70 AND user_id <= 3 AND
users_table.value_1 > 15 AND users_table.value_1 < 17 users_table.value_1 > 3 AND users_table.value_1 < 5
GROUP BY GROUP BY
user_id user_id
HAVING HAVING
@ -149,9 +146,9 @@ ORDER BY
count_pay; count_pay;
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
8 | 8 | 45.0000000000000000 7 | 3 | 1.7142857142857143
(1 row) (1 row)
------------------------------------ ------------------------------------
@ -177,23 +174,23 @@ FROM (
SELECT user_id, time SELECT user_id, time
FROM users_table FROM users_table
WHERE WHERE
user_id >= 10 AND user_id >= 1 AND
user_id <= 70 AND user_id <= 3 AND
users_table.value_1 > 10 AND users_table.value_1 < 12 users_table.value_1 > 3 AND users_table.value_1 < 6
) u LEFT JOIN LATERAL ( ) u LEFT JOIN LATERAL (
SELECT event_type, time SELECT event_type, time
FROM events_table FROM events_table
WHERE user_id = u.user_id AND WHERE user_id = u.user_id AND
events_table.event_type > 10 AND events_table.event_type < 12 events_table.event_type > 3 AND events_table.event_type < 6
) t ON true ) t ON true
GROUP BY user_id GROUP BY user_id
) AS shard_union ) AS shard_union
ORDER BY user_lastseen DESC; ORDER BY user_lastseen DESC;
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
6 | 6 | 42.0000000000000000 3 | 3 | 2.0000000000000000
(1 row) (1 row)
------------------------------------ ------------------------------------
@ -205,15 +202,15 @@ TRUNCATE agg_results;
INSERT INTO agg_results (user_id) INSERT INTO agg_results (user_id)
SELECT DISTINCT user_id SELECT DISTINCT user_id
FROM users_table FROM users_table
WHERE user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 10 AND value_1 <= 20) WHERE user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 1 AND value_1 <= 2)
AND user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 30 AND value_1 <= 40) AND user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 3 AND value_1 <= 4)
AND user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 50 AND value_1 <= 60); AND user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 5 AND value_1 <= 6);
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
33 | 33 | 50.3939393939393939 5 | 5 | 3.8000000000000000
(1 row) (1 row)
------------------------------------ ------------------------------------
@ -225,16 +222,16 @@ TRUNCATE agg_results;
INSERT INTO agg_results(user_id) INSERT INTO agg_results(user_id)
SELECT user_id SELECT user_id
FROM users_table FROM users_table
WHERE (value_1 = 10 WHERE (value_1 = 1
OR value_1 = 11 OR value_1 = 2
OR value_1 = 12) OR value_1 = 3)
GROUP BY user_id GROUP BY user_id
HAVING count(distinct value_1) >= 2; HAVING count(distinct value_1) >= 2;
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
4 | 4 | 51.0000000000000000 6 | 6 | 3.5000000000000000
(1 row) (1 row)
------------------------------------ ------------------------------------
@ -245,14 +242,14 @@ SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results;
TRUNCATE agg_results; TRUNCATE agg_results;
INSERT INTO agg_results(user_id, value_2_agg) INSERT INTO agg_results(user_id, value_2_agg)
SELECT user_id, value_2 FROM users_table WHERE SELECT user_id, value_2 FROM users_table WHERE
value_1 > 101 AND value_1 < 110 value_1 > 1 AND value_1 < 4
AND value_2 >= 5 AND value_2 >= 3
AND EXISTS (SELECT user_id FROM events_table WHERE event_type>101 AND event_type < 110 AND value_3 > 100 AND user_id=users_table.user_id); AND EXISTS (SELECT user_id FROM events_table WHERE event_type>1 AND event_type < 5 AND value_3 > 2 AND user_id=users_table.user_id);
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
34 | 27 | 40.5588235294117647 20 | 6 | 3.7500000000000000
(1 row) (1 row)
------------------------------------ ------------------------------------
@ -263,14 +260,14 @@ SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results;
TRUNCATE agg_results; TRUNCATE agg_results;
INSERT INTO agg_results(user_id, value_2_agg) INSERT INTO agg_results(user_id, value_2_agg)
SELECT user_id, value_2 FROM users_table WHERE SELECT user_id, value_2 FROM users_table WHERE
value_1 = 101 value_1 = 1
AND value_2 >= 5 AND value_2 >= 2
AND NOT EXISTS (SELECT user_id FROM events_table WHERE event_type=101 AND value_3 > 100 AND user_id=users_table.user_id); AND NOT EXISTS (SELECT user_id FROM events_table WHERE event_type=1 AND value_3 > 4 AND user_id=users_table.user_id);
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
8 | 7 | 39.7500000000000000 4 | 2 | 4.2500000000000000
(1 row) (1 row)
------------------------------------ ------------------------------------
@ -281,15 +278,15 @@ SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results;
TRUNCATE agg_results; TRUNCATE agg_results;
INSERT INTO agg_results(user_id, value_2_agg) INSERT INTO agg_results(user_id, value_2_agg)
SELECT user_id, value_2 FROM users_table WHERE SELECT user_id, value_2 FROM users_table WHERE
value_1 > 100 value_1 > 1
AND value_2 >= 5 AND value_2 >= 3
AND EXISTS (SELECT user_id FROM events_table WHERE event_type!=100 AND value_3 > 100 AND user_id=users_table.user_id) AND EXISTS (SELECT user_id FROM events_table WHERE event_type!=1 AND value_3 > 1 AND user_id=users_table.user_id)
AND EXISTS (SELECT user_id FROM events_table WHERE event_type=101 AND value_3 > 100 AND user_id=users_table.user_id); AND EXISTS (SELECT user_id FROM events_table WHERE event_type=2 AND value_3 > 1 AND user_id=users_table.user_id);
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
1202 | 14 | 47.7462562396006656 29 | 5 | 3.1034482758620690
(1 row) (1 row)
------------------------------------ ------------------------------------
@ -300,15 +297,15 @@ SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results;
TRUNCATE agg_results; TRUNCATE agg_results;
INSERT INTO agg_results(user_id, value_2_agg) INSERT INTO agg_results(user_id, value_2_agg)
SELECT user_id, value_2 FROM users_table WHERE SELECT user_id, value_2 FROM users_table WHERE
value_2 >= 5 value_2 >= 3
AND EXISTS (SELECT user_id FROM events_table WHERE event_type > 100 AND event_type <= 300 AND value_3 > 100 AND user_id=users_table.user_id) AND EXISTS (SELECT user_id FROM events_table WHERE event_type > 1 AND event_type <= 3 AND value_3 > 1 AND user_id=users_table.user_id)
AND NOT EXISTS (SELECT user_id FROM events_table WHERE event_type > 300 AND event_type <= 350 AND value_3 > 100 AND user_id=users_table.user_id); AND NOT EXISTS (SELECT user_id FROM events_table WHERE event_type > 3 AND event_type <= 4 AND value_3 > 1 AND user_id=users_table.user_id);
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
205 | 2 | 55.2195121951219512 11 | 1 | 5.0000000000000000
(1 row) (1 row)
------------------------------------ ------------------------------------
@ -321,23 +318,23 @@ INSERT INTO agg_results(user_id, value_2_agg)
SELECT user_id, SELECT user_id,
value_2 value_2
FROM users_table FROM users_table
WHERE value_1 > 100 WHERE value_1 > 1
AND value_1 < 124 AND value_1 < 3
AND value_2 >= 5 AND value_2 >= 1
AND EXISTS (SELECT user_id AND EXISTS (SELECT user_id
FROM events_table FROM events_table
WHERE event_type > 100 WHERE event_type > 1
AND event_type < 124 AND event_type < 3
AND value_3 > 100 AND value_3 > 1
AND user_id = users_table.user_id AND user_id = users_table.user_id
GROUP BY user_id GROUP BY user_id
HAVING Count(*) > 2); HAVING Count(*) > 2);
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
78 | 34 | 52.4230769230769231 4 | 2 | 3.5000000000000000
(1 row) (1 row)
@ -351,13 +348,13 @@ INSERT INTO agg_results(user_id, value_1_agg)
SELECT user_id, value_1 from SELECT user_id, value_1 from
( (
SELECT user_id, value_1 From users_table SELECT user_id, value_1 From users_table
WHERE value_2 > 100 and user_id = 15 GROUP BY value_1, user_id HAVING count(*) > 1 WHERE value_2 > 1 and user_id = 1 GROUP BY value_1, user_id HAVING count(*) > 1
) as a; ) as a;
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+------------------------
6 | 1 | 15.0000000000000000 1 | 1 | 1.00000000000000000000
(1 row) (1 row)
------------------------------------ ------------------------------------
@ -369,19 +366,19 @@ TRUNCATE agg_results;
INSERT INTO agg_results(user_id) INSERT INTO agg_results(user_id)
Select user_id Select user_id
From events_table From events_table
Where event_type = 16 Where event_type = 2
And value_2 > 50 And value_2 > 2
And user_id in And user_id in
(select user_id (select user_id
From users_table From users_table
Where value_1 = 15 Where value_1 = 2
And value_2 > 25); And value_2 > 1);
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
2 | 2 | 30.0000000000000000 11 | 4 | 3.1818181818181818
(1 row) (1 row)
------------------------------------ ------------------------------------
@ -392,13 +389,13 @@ SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results;
TRUNCATE agg_results; TRUNCATE agg_results;
INSERT INTO agg_results(user_id, value_1_agg) INSERT INTO agg_results(user_id, value_1_agg)
SELECT user_id, event_type FROM events_table SELECT user_id, event_type FROM events_table
WHERE user_id in (SELECT user_id from events_table WHERE event_type > 500 and event_type < 505) WHERE user_id in (SELECT user_id from events_table WHERE event_type > 3 and event_type < 5)
GROUP BY user_id, event_type; GROUP BY user_id, event_type;
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
3084 | 32 | 44.1498054474708171 34 | 6 | 3.4411764705882353
(1 row) (1 row)
@ -415,14 +412,14 @@ select user_id from
user_id user_id
from from
events_table events_table
where event_type = 901 group by user_id having count(*) > 3 where event_type = 4 group by user_id having count(*) > 3
) as a; ) as a;
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
1 | 1 | 57.0000000000000000 4 | 4 | 2.5000000000000000
(1 row) (1 row)
@ -439,19 +436,19 @@ FROM
users_table users_table
JOIN JOIN
(SELECT (SELECT
ma.user_id, (GREATEST(coalesce(ma.value_4 / 250, 0.0) + GREATEST(1.0))) / 2 AS prob ma.user_id, (GREATEST(coalesce(ma.value_4, 0.0) / 250 + GREATEST(1.0))) / 2 AS prob
FROM FROM
users_table AS ma, events_table as short_list users_table AS ma, events_table as short_list
WHERE WHERE
short_list.user_id = ma.user_id and ma.value_1 < 50 and short_list.event_type < 50 short_list.user_id = ma.user_id and ma.value_1 < 3 and short_list.event_type < 3
) temp ) temp
ON users_table.user_id = temp.user_id ON users_table.user_id = temp.user_id
WHERE users_table.value_1 < 50; WHERE users_table.value_1 < 3;
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
14371 | 101 | 50.5232064574490293 3488 | 6 | 3.5372706422018349
(1 row) (1 row)
-- DISTINCT in the outer query and DISTINCT in the subquery -- DISTINCT in the outer query and DISTINCT in the subquery
@ -467,15 +464,15 @@ FROM
FROM FROM
users_table AS ma, events_table as short_list users_table AS ma, events_table as short_list
WHERE WHERE
short_list.user_id = ma.user_id and ma.value_1 < 50 and short_list.event_type < 3 short_list.user_id = ma.user_id and ma.value_1 < 3 and short_list.event_type < 2
) temp ) temp
ON users_ids.user_id = temp.user_id ON users_ids.user_id = temp.user_id
WHERE temp.value_1 < 50; WHERE temp.value_1 < 3;
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
27 | 27 | 54.0000000000000000 6 | 6 | 3.5000000000000000
(1 row) (1 row)
-- DISTINCT ON in the outer query and DISTINCT in the subquery -- DISTINCT ON in the outer query and DISTINCT in the subquery
@ -491,15 +488,15 @@ FROM
FROM FROM
users_table AS ma, events_table as short_list users_table AS ma, events_table as short_list
WHERE WHERE
short_list.user_id = ma.user_id and ma.value_1 < 50 and short_list.event_type < 15 short_list.user_id = ma.user_id and ma.value_1 < 3 and short_list.event_type < 2
) temp ) temp
ON users_ids.user_id = temp.user_id ON users_ids.user_id = temp.user_id
WHERE temp.value_1 < 50 WHERE temp.value_1 < 3
ORDER BY 1, 2; ORDER BY 1, 2;
SELECT count(*), count(DISTINCT user_id), avg(user_id), avg(value_1_agg) FROM agg_results; SELECT count(*), count(DISTINCT user_id), avg(user_id), avg(value_1_agg) FROM agg_results;
count | count | avg | avg count | count | avg | avg
-------+-------+---------------------+--------------------- -------+-------+--------------------+------------------------
80 | 80 | 50.7875000000000000 | 10.0125000000000000 6 | 6 | 3.5000000000000000 | 0.16666666666666666667
(1 row) (1 row)
-- DISTINCT ON in the outer query and DISTINCT ON in the subquery -- DISTINCT ON in the outer query and DISTINCT ON in the subquery
@ -515,13 +512,13 @@ FROM
FROM FROM
users_table AS ma, events_table as short_list users_table AS ma, events_table as short_list
WHERE WHERE
short_list.user_id = ma.user_id and ma.value_1 < 5000 and short_list.event_type < 3 short_list.user_id = ma.user_id and ma.value_1 < 10 and short_list.event_type < 2
) temp ) temp
ON users_ids.user_id = temp.user_id ON users_ids.user_id = temp.user_id
ORDER BY 1, 2; ORDER BY 1, 2;
SELECT count(*), count(DISTINCT user_id), avg(user_id), avg(value_1_agg) FROM agg_results; SELECT count(*), count(DISTINCT user_id), avg(user_id), avg(value_1_agg) FROM agg_results;
count | count | avg | avg count | count | avg | avg
-------+-------+---------------------+-------------------- -------+-------+--------------------+------------------------
27 | 27 | 54.0000000000000000 | 9.8518518518518519 6 | 6 | 3.5000000000000000 | 0.16666666666666666667
(1 row) (1 row)

View File

@ -13,18 +13,18 @@ FROM (
FROM users_table AS u, FROM users_table AS u,
events_table AS e events_table AS e
WHERE u.user_id = e.user_id WHERE u.user_id = e.user_id
AND u.user_id >= 10 AND u.user_id >= 1
AND u.user_id <= 25 AND u.user_id <= 2
AND e.event_type IN (100, 101, 102) AND e.event_type IN (2,3)
) t ) t
GROUP BY user_id GROUP BY user_id
) q ) q
WHERE user_id = 20; WHERE user_id = 2;
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_second; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_second;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
1 | 1 | 20.0000000000000000 1 | 1 | 2.0000000000000000
(1 row) (1 row)
------------------------------------ ------------------------------------
@ -42,18 +42,18 @@ FROM (
FROM users_table AS u, FROM users_table AS u,
events_table AS e events_table AS e
WHERE u.user_id = e.user_id AND WHERE u.user_id = e.user_id AND
(u.user_id = 13 OR u.user_id = 20) AND (u.user_id = 1 OR u.user_id = 2) AND
(e.user_id = 13 OR e.user_id = 20) (e.user_id = 1 OR e.user_id = 2)
AND e.event_type IN (100, 101, 102) AND e.event_type IN (1, 2)
) t ) t
GROUP BY user_id GROUP BY user_id
) q ) q
WHERE (user_id = 13 OR user_id = 20); WHERE (user_id = 1 OR user_id = 2);
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_second; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_second;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
2 | 2 | 16.5000000000000000 2 | 2 | 1.5000000000000000
(1 row) (1 row)
------------------------------------ ------------------------------------
@ -75,9 +75,9 @@ FROM (
FROM users_table AS u, FROM users_table AS u,
events_table AS e events_table AS e
WHERE u.user_id = e.user_id WHERE u.user_id = e.user_id
AND u.user_id >= 10 AND u.user_id >= 1
AND u.user_id <= 25 AND u.user_id <= 2
AND e.event_type IN (100, 101, 102) AND e.event_type IN (1, 2)
) )
UNION UNION
( (
@ -85,20 +85,20 @@ FROM (
FROM users_table AS u, FROM users_table AS u,
events_table AS e events_table AS e
WHERE u.user_id = e.user_id WHERE u.user_id = e.user_id
AND u.user_id >= 10 AND u.user_id >= 1
AND u.user_id <= 25 AND u.user_id <= 2
AND e.event_type IN (103, 104, 105) AND e.event_type IN (3, 4)
) )
) t1 LEFT JOIN ( ) t1 LEFT JOIN (
SELECT DISTINCT user_id, SELECT DISTINCT user_id,
'Has done event'::TEXT AS hasdone_event 'Has done event'::TEXT AS hasdone_event
FROM events_table AS e FROM events_table AS e
WHERE e.user_id >= 10 WHERE e.user_id >= 1
AND e.user_id <= 25 AND e.user_id <= 2
AND e.event_type IN (106, 107, 108) AND e.event_type IN (5, 6)
) t2 ON (t1.user_id = t2.user_id) ) t2 ON (t1.user_id = t2.user_id)
WHERE t1.user_id = 20 WHERE t1.user_id = 2
GROUP BY t1.user_id, hasdone_event GROUP BY t1.user_id, hasdone_event
) t GROUP BY user_id, hasdone_event; ) t GROUP BY user_id, hasdone_event;
------------------------------------ ------------------------------------
@ -120,8 +120,8 @@ FROM (
FROM users_table AS u, FROM users_table AS u,
events_table AS e events_table AS e
WHERE u.user_id = e.user_id WHERE u.user_id = e.user_id
AND (e.user_id = 20 OR e.user_id = 17) AND (e.user_id = 2 OR e.user_id = 3)
AND e.event_type IN (100, 101, 102) AND e.event_type IN (1, 2)
) )
UNION UNION
( (
@ -129,8 +129,8 @@ FROM (
FROM users_table AS u, FROM users_table AS u,
events_table AS e events_table AS e
WHERE u.user_id = e.user_id WHERE u.user_id = e.user_id
AND (e.user_id = 20 OR e.user_id = 17) AND (e.user_id = 2 OR e.user_id = 3)
AND e.event_type IN (103, 104, 105) AND e.event_type IN (3, 4)
) )
) t1 LEFT JOIN ( ) t1 LEFT JOIN (
SELECT DISTINCT user_id, SELECT DISTINCT user_id,
@ -138,17 +138,17 @@ FROM (
FROM events_table AS e FROM events_table AS e
WHERE WHERE
(e.user_id = 20 OR e.user_id = 17) (e.user_id = 2 OR e.user_id = 3)
AND e.event_type IN (106, 107, 108) AND e.event_type IN (4, 5)
) t2 ON (t1.user_id = t2.user_id) ) t2 ON (t1.user_id = t2.user_id)
WHERE (t1.user_id = 20 OR t1.user_id = 17) WHERE (t1.user_id = 2 OR t1.user_id = 1)
GROUP BY t1.user_id, hasdone_event GROUP BY t1.user_id, hasdone_event
) t GROUP BY user_id, hasdone_event; ) t GROUP BY user_id, hasdone_event;
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_second; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_second;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
2 | 2 | 18.5000000000000000 1 | 1 | 2.0000000000000000
(1 row) (1 row)
------------------------------------ ------------------------------------
@ -174,24 +174,24 @@ FROM (
SELECT user_id, time SELECT user_id, time
FROM users_table FROM users_table
WHERE WHERE
user_id >= 10 AND user_id >= 1 AND
user_id <= 70 AND user_id <= 5 AND
users_table.value_1 > 10 AND users_table.value_1 < 12 users_table.value_1 > 1 AND users_table.value_1 < 4
) u LEFT JOIN LATERAL ( ) u LEFT JOIN LATERAL (
SELECT event_type, time SELECT event_type, time
FROM events_table FROM events_table
WHERE user_id = u.user_id AND WHERE user_id = u.user_id AND
events_table.event_type > 10 AND events_table.event_type < 12 events_table.event_type > 1 AND events_table.event_type < 4
) t ON true ) t ON true
WHERE user_id = 65 WHERE user_id = 5
GROUP BY user_id GROUP BY user_id
) AS shard_union ) AS shard_union
ORDER BY user_lastseen DESC; ORDER BY user_lastseen DESC;
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_second; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_second;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
1 | 1 | 65.0000000000000000 1 | 1 | 5.0000000000000000
(1 row) (1 row)
------------------------------------ ------------------------------------
@ -217,25 +217,25 @@ FROM (
SELECT user_id, time SELECT user_id, time
FROM users_table FROM users_table
WHERE WHERE
user_id >= 10 AND user_id >= 1 AND
user_id <= 70 AND user_id <= 5 AND
(user_id = 65 OR user_id = 12) AND (user_id = 5 OR user_id = 1) AND
users_table.value_1 > 10 AND users_table.value_1 < 12 users_table.value_1 > 1 AND users_table.value_1 < 4
) u LEFT JOIN LATERAL ( ) u LEFT JOIN LATERAL (
SELECT event_type, time SELECT event_type, time
FROM events_table FROM events_table
WHERE user_id = u.user_id AND (user_id = 65 OR user_id = 12) AND WHERE user_id = u.user_id AND (user_id = 5 OR user_id = 1) AND
events_table.event_type > 10 AND events_table.event_type < 12 events_table.event_type > 1 AND events_table.event_type < 4
) t ON true ) t ON true
WHERE (user_id = 65 OR user_id = 12) WHERE (user_id = 5 OR user_id = 1)
GROUP BY user_id GROUP BY user_id
) AS shard_union ) AS shard_union
ORDER BY user_lastseen DESC; ORDER BY user_lastseen DESC;
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_second; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_second;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
2 | 2 | 38.5000000000000000 2 | 2 | 3.0000000000000000
(1 row) (1 row)
------------------------------------ ------------------------------------
@ -247,16 +247,16 @@ TRUNCATE agg_results_second;
INSERT INTO agg_results_second (user_id) INSERT INTO agg_results_second (user_id)
SELECT DISTINCT user_id SELECT DISTINCT user_id
FROM users_table FROM users_table
WHERE user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 10 AND value_1 <= 20) WHERE user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 1 AND value_1 <= 2)
AND user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 30 AND value_1 <= 40) AND user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 3 AND value_1 <= 4)
AND user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 50 AND value_1 <= 60) AND user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 5 AND value_1 <= 6)
AND user_id = 7; AND user_id = 1;
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_second; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_second;
count | count | avg count | count | avg
-------+-------+-------------------- -------+-------+------------------------
1 | 1 | 7.0000000000000000 1 | 1 | 1.00000000000000000000
(1 row) (1 row)
------------------------------------ ------------------------------------
@ -268,16 +268,16 @@ TRUNCATE agg_results_second;
INSERT INTO agg_results_second (user_id) INSERT INTO agg_results_second (user_id)
SELECT DISTINCT user_id SELECT DISTINCT user_id
FROM users_table FROM users_table
WHERE user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 10 AND value_1 <= 20 AND (user_id = 7 OR user_id = 20)) WHERE user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 1 AND value_1 <= 2 AND (user_id = 1 OR user_id = 2))
AND user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 30 AND value_1 <= 40 AND (user_id = 7 OR user_id = 20)) AND user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 3 AND value_1 <= 4 AND (user_id = 1 OR user_id = 2))
AND user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 50 AND value_1 <= 60 AND (user_id = 7 OR user_id = 20)) AND user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 5 AND value_1 <= 6 AND (user_id = 1 OR user_id = 2))
AND (user_id = 7 OR user_id = 20); AND (user_id = 1 OR user_id = 2);
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_second; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_second;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+------------------------
2 | 2 | 13.5000000000000000 1 | 1 | 1.00000000000000000000
(1 row) (1 row)
------------------------------------ ------------------------------------
@ -288,15 +288,15 @@ SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_second;
TRUNCATE agg_results_second; TRUNCATE agg_results_second;
INSERT INTO agg_results_second(user_id, value_2_agg) INSERT INTO agg_results_second(user_id, value_2_agg)
SELECT user_id, value_2 FROM users_table WHERE SELECT user_id, value_2 FROM users_table WHERE
value_1 > 101 AND value_1 < 110 value_1 > 1 AND value_1 < 4
AND value_2 >= 5 AND value_2 >= 1
AND EXISTS (SELECT user_id FROM events_table WHERE event_type>101 AND event_type < 110 AND value_3 > 100 AND user_id=users_table.user_id) AND EXISTS (SELECT user_id FROM events_table WHERE event_type>1 AND event_type < 3 AND value_3 > 1 AND user_id=users_table.user_id)
AND user_id = 61; AND user_id = 2;
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_second; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_second;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
1 | 1 | 61.0000000000000000 7 | 1 | 2.0000000000000000
(1 row) (1 row)
------------------------------------ ------------------------------------
@ -307,15 +307,15 @@ SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_second;
TRUNCATE agg_results_second; TRUNCATE agg_results_second;
INSERT INTO agg_results_second(user_id, value_2_agg) INSERT INTO agg_results_second(user_id, value_2_agg)
SELECT user_id, value_2 FROM users_table WHERE SELECT user_id, value_2 FROM users_table WHERE
value_1 > 101 AND value_1 < 110 value_1 > 1 AND value_1 < 4
AND value_2 >= 5 AND value_2 >= 1
AND EXISTS (SELECT user_id FROM events_table WHERE event_type>101 AND event_type < 110 AND value_3 > 100 AND (user_id = 61 OR user_id = 51) AND user_id=users_table.user_id) AND EXISTS (SELECT user_id FROM events_table WHERE event_type>0 AND event_type < 2 AND value_3 > 1 AND (user_id = 2 OR user_id = 1) AND user_id=users_table.user_id)
AND (user_id = 61 OR user_id = 51); AND (user_id = 2 OR user_id = 1);
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_second; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_second;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
2 | 2 | 56.0000000000000000 10 | 2 | 1.7000000000000000
(1 row) (1 row)
------------------------------------ ------------------------------------
@ -326,16 +326,16 @@ SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_second;
TRUNCATE agg_results_second; TRUNCATE agg_results_second;
INSERT INTO agg_results_second(user_id, value_2_agg) INSERT INTO agg_results_second(user_id, value_2_agg)
SELECT user_id, value_2 FROM users_table WHERE SELECT user_id, value_2 FROM users_table WHERE
value_2 >= 5 value_2 >= 2
AND user_id = 96 AND user_id = 1
AND EXISTS (SELECT user_id FROM events_table WHERE event_type > 100 AND event_type <= 300 AND value_3 > 100 AND user_id=users_table.user_id) AND EXISTS (SELECT user_id FROM events_table WHERE event_type > 1 AND event_type <= 3 AND value_3 > 1 AND user_id=users_table.user_id)
AND NOT EXISTS (SELECT user_id FROM events_table WHERE event_type > 300 AND event_type <= 350 AND value_3 > 100 AND user_id=users_table.user_id); AND NOT EXISTS (SELECT user_id FROM events_table WHERE event_type > 4 AND event_type <= 5 AND value_3 > 4 AND user_id=users_table.user_id);
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_second; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_second;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+------------------------
110 | 1 | 96.0000000000000000 6 | 1 | 1.00000000000000000000
(1 row) (1 row)
------------------------------------ ------------------------------------
@ -346,16 +346,16 @@ SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_second;
TRUNCATE agg_results_second; TRUNCATE agg_results_second;
INSERT INTO agg_results_second(user_id, value_2_agg) INSERT INTO agg_results_second(user_id, value_2_agg)
SELECT user_id, value_2 FROM users_table WHERE SELECT user_id, value_2 FROM users_table WHERE
value_2 >= 5 value_2 >= 2
AND (user_id = 96 OR user_id = 8) AND (user_id = 1 OR user_id = 2)
AND EXISTS (SELECT user_id FROM events_table WHERE event_type > 100 AND event_type <= 300 AND value_3 > 100 AND user_id=users_table.user_id AND (user_id = 96 OR user_id = 8)) AND EXISTS (SELECT user_id FROM events_table WHERE event_type > 1 AND event_type <= 3 AND value_3 > 1 AND user_id=users_table.user_id AND (user_id = 1 OR user_id = 2))
AND NOT EXISTS (SELECT user_id FROM events_table WHERE event_type > 300 AND event_type <= 350 AND value_3 > 100 AND user_id=users_table.user_id AND (user_id = 96 OR user_id = 8)); AND NOT EXISTS (SELECT user_id FROM events_table WHERE event_type > 4 AND event_type <= 5 AND value_3 > 4 AND user_id=users_table.user_id AND (user_id = 1 OR user_id = 2));
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_second; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_second;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
205 | 2 | 55.2195121951219512 20 | 2 | 1.7000000000000000
(1 row) (1 row)
------------------------------------ ------------------------------------
@ -368,25 +368,25 @@ INSERT INTO agg_results_second(user_id, value_2_agg)
SELECT user_id, SELECT user_id,
value_2 value_2
FROM users_table FROM users_table
WHERE value_1 > 100 WHERE value_1 > 1
AND value_1 < 124 AND value_1 < 3
AND value_2 >= 5 AND value_2 >= 1
AND user_id = 47 AND user_id = 3
AND EXISTS (SELECT user_id AND EXISTS (SELECT user_id
FROM events_table FROM events_table
WHERE event_type > 100 WHERE event_type > 1
AND event_type < 124 AND event_type < 3
AND value_3 > 100 AND value_3 > 1
AND user_id = users_table.user_id AND user_id = users_table.user_id
AND user_id = 47 AND user_id = 3
GROUP BY user_id GROUP BY user_id
HAVING Count(*) > 2); HAVING Count(*) > 2);
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_second; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_second;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
6 | 1 | 47.0000000000000000 2 | 1 | 3.0000000000000000
(1 row) (1 row)
------------------------------------ ------------------------------------
@ -399,25 +399,24 @@ INSERT INTO agg_results_second(user_id, value_2_agg)
SELECT user_id, SELECT user_id,
value_2 value_2
FROM users_table FROM users_table
WHERE value_1 > 100 WHERE value_1 > 1
AND value_1 < 124 AND value_1 < 3
AND value_2 >= 5 AND value_2 >= 1
AND (user_id = 47 or user_id = 81) AND (user_id = 3 or user_id = 4)
AND EXISTS (SELECT user_id AND EXISTS (SELECT user_id
FROM events_table FROM events_table
WHERE event_type > 100 WHERE event_type = 2
AND event_type < 124 AND value_3 > 1
AND value_3 > 100
AND user_id = users_table.user_id AND user_id = users_table.user_id
AND (user_id = 47 or user_id = 81) AND (user_id = 3 or user_id = 4)
GROUP BY user_id GROUP BY user_id
HAVING Count(*) > 2); HAVING Count(*) > 2);
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_second; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_second;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
7 | 2 | 51.8571428571428571 4 | 2 | 3.5000000000000000
(1 row) (1 row)

View File

@ -14,9 +14,9 @@ FROM
) as foo; ) as foo;
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_window; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_window;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
10001 | 101 | 49.5810418958104190 101 | 6 | 3.2079207920792079
(1 row) (1 row)
TRUNCATE agg_results_window; TRUNCATE agg_results_window;
@ -33,9 +33,9 @@ FROM
) as foo; ) as foo;
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_window; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_window;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
10001 | 101 | 49.5810418958104190 101 | 6 | 3.2079207920792079
(1 row) (1 row)
TRUNCATE agg_results_window; TRUNCATE agg_results_window;
@ -52,9 +52,9 @@ FROM
) as foo; ) as foo;
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_window; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_window;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
10001 | 101 | 49.5810418958104190 101 | 6 | 3.2079207920792079
(1 row) (1 row)
TRUNCATE agg_results_window; TRUNCATE agg_results_window;
@ -74,9 +74,9 @@ FROM
) as foo; ) as foo;
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_window; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_window;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
1188 | 101 | 49.7895622895622896 12 | 6 | 3.5000000000000000
(1 row) (1 row)
TRUNCATE agg_results_window; TRUNCATE agg_results_window;
@ -97,7 +97,7 @@ GROUP BY
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_window; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_window;
count | count | avg count | count | avg
-------+-------+-------------------- -------+-------+--------------------
1002 | 50 | 9.7844311377245509 8 | 2 | 1.1250000000000000
(1 row) (1 row)
TRUNCATE agg_results_window; TRUNCATE agg_results_window;
@ -111,14 +111,14 @@ SELECT * FROM
users_table, events_table users_table, events_table
WHERE WHERE
users_table.user_id = events_table.user_id and users_table.user_id = events_table.user_id and
event_type < 25 event_type < 2
WINDOW w1 AS (PARTITION BY users_table.user_id, events_table.event_type ORDER BY events_table.time) WINDOW w1 AS (PARTITION BY users_table.user_id, events_table.event_type ORDER BY events_table.time)
) as foo; ) as foo;
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_window; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_window;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
195 | 91 | 51.0205128205128205 30 | 6 | 3.4000000000000000
(1 row) (1 row)
TRUNCATE agg_results_window; TRUNCATE agg_results_window;
@ -132,15 +132,15 @@ SELECT * FROM
users_table, events_table users_table, events_table
WHERE WHERE
users_table.user_id = events_table.user_id and users_table.user_id = events_table.user_id and
event_type < 25 event_type < 2
WINDOW w1 AS (PARTITION BY users_table.user_id, events_table.event_type ORDER BY events_table.time), WINDOW w1 AS (PARTITION BY users_table.user_id, events_table.event_type ORDER BY events_table.time),
w2 AS (PARTITION BY users_table.user_id, (events_table.value_2 % 25) ORDER BY events_table.time) w2 AS (PARTITION BY users_table.user_id, (events_table.value_2 % 25) ORDER BY events_table.time)
) as foo; ) as foo;
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_window; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_window;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
202 | 91 | 50.2970297029702970 20 | 6 | 3.3500000000000000
(1 row) (1 row)
TRUNCATE agg_results_window; TRUNCATE agg_results_window;
@ -154,7 +154,7 @@ SELECT sub_1.user_id, max(lag_1), max(rank_1), max(rank_2) FROM
users_table, events_table users_table, events_table
WHERE WHERE
users_table.user_id = events_table.user_id and users_table.user_id = events_table.user_id and
event_type < 25 event_type < 2
WINDOW w1 AS (PARTITION BY users_table.user_id, events_table.event_type ORDER BY events_table.time), WINDOW w1 AS (PARTITION BY users_table.user_id, events_table.event_type ORDER BY events_table.time),
w2 AS (PARTITION BY users_table.user_id, (events_table.value_2 % 25) ORDER BY events_table.time) w2 AS (PARTITION BY users_table.user_id, (events_table.value_2 % 25) ORDER BY events_table.time)
) as sub_1 ) as sub_1
@ -166,7 +166,7 @@ JOIN
users_table, events_table users_table, events_table
WHERE WHERE
users_table.user_id = events_table.user_id and users_table.user_id = events_table.user_id and
event_type < 25 event_type < 2
WINDOW w1 AS (PARTITION BY users_table.user_id, events_table.value_2 ORDER BY events_table.time), WINDOW w1 AS (PARTITION BY users_table.user_id, events_table.value_2 ORDER BY events_table.time),
w2 AS (PARTITION BY users_table.user_id, (events_table.value_2 % 50) ORDER BY events_table.time) w2 AS (PARTITION BY users_table.user_id, (events_table.value_2 % 50) ORDER BY events_table.time)
) as sub_2 ) as sub_2
@ -175,9 +175,9 @@ JOIN
sub_1.user_id; sub_1.user_id;
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_window; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_window;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
91 | 91 | 50.2637362637362637 6 | 6 | 3.5000000000000000
(1 row) (1 row)
TRUNCATE agg_results_window; TRUNCATE agg_results_window;
@ -196,14 +196,14 @@ FROM
WINDOW my_win AS (PARTITION BY user_id ORDER BY count(*) DESC) WINDOW my_win AS (PARTITION BY user_id ORDER BY count(*) DESC)
) as foo ) as foo
WHERE WHERE
my_rank > 5 my_rank > 1
GROUP BY GROUP BY
my_rank; my_rank;
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_window; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_window;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
7 | 6 | 50.0000000000000000 1 | 1 | 4.0000000000000000
(1 row) (1 row)
TRUNCATE agg_results_window; TRUNCATE agg_results_window;
@ -227,9 +227,9 @@ GROUP BY
my_rank; my_rank;
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_window; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_window;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
8 | 7 | 48.8750000000000000 2 | 2 | 3.5000000000000000
(1 row) (1 row)
TRUNCATE agg_results_window; TRUNCATE agg_results_window;
@ -252,9 +252,9 @@ GROUP BY
my_rank; my_rank;
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_window; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_window;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
1 | 1 | 50.0000000000000000 1 | 1 | 4.0000000000000000
(1 row) (1 row)
TRUNCATE agg_results_window; TRUNCATE agg_results_window;
@ -274,9 +274,9 @@ LIMIT
10; 10;
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_window; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_window;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
10 | 10 | 49.1000000000000000 6 | 6 | 3.5000000000000000
(1 row) (1 row)
TRUNCATE agg_results_window; TRUNCATE agg_results_window;
@ -292,9 +292,9 @@ SELECT user_id, max(sum) FROM (
GROUP BY user_id; GROUP BY user_id;
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_window; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_window;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
101 | 101 | 50.0000000000000000 6 | 6 | 3.5000000000000000
(1 row) (1 row)
TRUNCATE agg_results_window; TRUNCATE agg_results_window;
@ -305,7 +305,7 @@ SELECT
FROM FROM
users_table users_table
WHERE WHERE
value_2 > 545 AND value_2 > 1 AND
value_2 < ALL ( value_2 < ALL (
SELECT SELECT
avg(value_3) OVER (PARTITION BY user_id) avg(value_3) OVER (PARTITION BY user_id)
@ -318,9 +318,9 @@ GROUP BY
user_id; user_id;
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_window; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_window;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
4 | 4 | 35.2500000000000000 4 | 4 | 2.5000000000000000
(1 row) (1 row)
TRUNCATE agg_results_window; TRUNCATE agg_results_window;
@ -338,9 +338,9 @@ SELECT * FROM (
) a; ) a;
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_window; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_window;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
101 | 101 | 50.0000000000000000 6 | 6 | 3.5000000000000000
(1 row) (1 row)
TRUNCATE agg_results_window; TRUNCATE agg_results_window;
@ -361,9 +361,9 @@ GROUP BY
user_id, rank; user_id, rank;
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_window; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_window;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
9501 | 101 | 49.8461214608988528 32 | 6 | 3.5937500000000000
(1 row) (1 row)
TRUNCATE agg_results_window; TRUNCATE agg_results_window;
@ -392,9 +392,9 @@ WHERE
) a; ) a;
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_window; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_window;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
101 | 101 | 50.0000000000000000 6 | 6 | 3.5000000000000000
(1 row) (1 row)
TRUNCATE agg_results_window; TRUNCATE agg_results_window;
@ -410,9 +410,9 @@ FROM
) a; ) a;
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_window; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_window;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
101 | 101 | 50.0000000000000000 6 | 6 | 3.5000000000000000
(1 row) (1 row)
TRUNCATE agg_results_window; TRUNCATE agg_results_window;
@ -428,9 +428,9 @@ SELECT * FROM (
) a; ) a;
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_window; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_window;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
437 | 100 | 49.9496567505720824 26 | 6 | 3.7692307692307692
(1 row) (1 row)
TRUNCATE agg_results_window; TRUNCATE agg_results_window;
@ -450,9 +450,9 @@ LIMIT
10; 10;
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_window; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_window;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
10 | 5 | 32.4000000000000000 10 | 5 | 3.8000000000000000
(1 row) (1 row)
TRUNCATE agg_results_window; TRUNCATE agg_results_window;
@ -471,9 +471,9 @@ FROM
view_with_window_func; view_with_window_func;
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_window; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_window;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
437 | 100 | 49.9496567505720824 26 | 6 | 3.7692307692307692
(1 row) (1 row)
TRUNCATE agg_results_window; TRUNCATE agg_results_window;
@ -544,9 +544,9 @@ GROUP BY
user_id; user_id;
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_window; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_window;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
94 | 94 | 50.4787234042553191 6 | 6 | 3.5000000000000000
(1 row) (1 row)
TRUNCATE agg_results_window; TRUNCATE agg_results_window;
@ -631,9 +631,9 @@ FROM (
) AS ftop; ) AS ftop;
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_window; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_window;
count | count | avg count | count | avg
-------+-------+--------------------- -------+-------+--------------------
101 | 101 | 50.0000000000000000 6 | 6 | 3.5000000000000000
(1 row) (1 row)
TRUNCATE agg_results_window; TRUNCATE agg_results_window;
@ -788,7 +788,7 @@ SELECT
FROM FROM
users_table users_table
WHERE WHERE
value_2 > 545 AND value_2 > 2 AND
value_2 < ALL ( value_2 < ALL (
SELECT SELECT
avg(value_3) OVER () avg(value_3) OVER ()

View File

@ -677,8 +677,8 @@ SELECT create_distributed_table('partitioned_events_table', 'user_id', colocate_
(1 row) (1 row)
-- INSERT/SELECT from regular table to partitioned table -- INSERT/SELECT from regular table to partitioned table
CREATE TABLE partitioned_users_table_2009 PARTITION OF partitioned_users_table FOR VALUES FROM ('2014-01-01') TO ('2015-01-01'); CREATE TABLE partitioned_users_table_2009 PARTITION OF partitioned_users_table FOR VALUES FROM ('2017-01-01') TO ('2018-01-01');
CREATE TABLE partitioned_events_table_2009 PARTITION OF partitioned_events_table FOR VALUES FROM ('2014-01-01') TO ('2015-01-01'); CREATE TABLE partitioned_events_table_2009 PARTITION OF partitioned_events_table FOR VALUES FROM ('2017-01-01') TO ('2018-01-01');
INSERT INTO partitioned_events_table SELECT * FROM events_table; INSERT INTO partitioned_events_table SELECT * FROM events_table;
INSERT INTO partitioned_users_table_2009 SELECT * FROM users_table; INSERT INTO partitioned_users_table_2009 SELECT * FROM users_table;
-- --
@ -700,38 +700,38 @@ FROM
FROM FROM
partitioned_events_table as "events" partitioned_events_table as "events"
WHERE WHERE
event_type IN (10, 11, 12, 13, 14, 15)) event_type IN (1, 2) )
UNION UNION
(SELECT (SELECT
"events"."user_id", "events"."time", 1 AS event "events"."user_id", "events"."time", 1 AS event
FROM FROM
partitioned_events_table as "events" partitioned_events_table as "events"
WHERE WHERE
event_type IN (15, 16, 17, 18, 19) ) event_type IN (3, 4) )
UNION UNION
(SELECT (SELECT
"events"."user_id", "events"."time", 2 AS event "events"."user_id", "events"."time", 2 AS event
FROM FROM
partitioned_events_table as "events" partitioned_events_table as "events"
WHERE WHERE
event_type IN (20, 21, 22, 23, 24, 25) ) event_type IN (5, 6) )
UNION UNION
(SELECT (SELECT
"events"."user_id", "events"."time", 3 AS event "events"."user_id", "events"."time", 3 AS event
FROM FROM
partitioned_events_table as "events" partitioned_events_table as "events"
WHERE WHERE
event_type IN (26, 27, 28, 29, 30, 13))) t1 event_type IN (1, 6))) t1
GROUP BY "t1"."user_id") AS t) "q" GROUP BY "t1"."user_id") AS t) "q"
) AS final_query ) AS final_query
GROUP BY types GROUP BY types
ORDER BY types; ORDER BY types;
types | sumofeventtype types | sumofeventtype
-------+---------------- -------+----------------
0 | 55 0 | 43
1 | 38 1 | 44
2 | 70 2 | 8
3 | 58 3 | 25
(4 rows) (4 rows)
-- UNION and JOIN on both partitioned and regular tables -- UNION and JOIN on both partitioned and regular tables
@ -754,7 +754,7 @@ FROM
FROM FROM
partitioned_events_table as "events" partitioned_events_table as "events"
WHERE WHERE
event_type IN (10, 11, 12, 13, 14, 15) ) events_subquery_1) event_type IN (1, 2)) events_subquery_1)
UNION UNION
(SELECT * (SELECT *
FROM FROM
@ -769,7 +769,7 @@ FROM
events_table as "events", users_table as "users" events_table as "events", users_table as "users"
WHERE WHERE
events.user_id = users.user_id AND events.user_id = users.user_id AND
event_type IN (10, 11, 12, 13, 14, 15) event_type IN (1, 2)
GROUP BY "events"."user_id" GROUP BY "events"."user_id"
) as events_subquery_5 ) as events_subquery_5
) events_subquery_2) ) events_subquery_2)
@ -781,7 +781,7 @@ FROM
FROM FROM
partitioned_events_table as "events" partitioned_events_table as "events"
WHERE WHERE
event_type IN (20, 21, 22, 23, 24, 25) ) events_subquery_3) event_type IN (3, 4)) events_subquery_3)
UNION UNION
(SELECT * (SELECT *
FROM FROM
@ -790,7 +790,7 @@ FROM
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (26, 27, 28, 29, 30, 13)) events_subquery_4) event_type IN (5, 6)) events_subquery_4)
) t1 ) t1
GROUP BY "t1"."user_id") AS t) "q" GROUP BY "t1"."user_id") AS t) "q"
INNER JOIN INNER JOIN
@ -799,7 +799,7 @@ INNER JOIN
FROM FROM
partitioned_users_table as "users" partitioned_users_table as "users"
WHERE WHERE
value_1 > 50 and value_1 < 70) AS t value_1 > 2 and value_1 < 5) AS t
ON (t.user_id = q.user_id)) as final_query ON (t.user_id = q.user_id)) as final_query
GROUP BY GROUP BY
types types
@ -807,16 +807,16 @@ ORDER BY
types; types;
types | sumofeventtype types | sumofeventtype
-------+---------------- -------+----------------
0 | 115 0 | 367
2 | 160 2 | 360
3 | 158 3 | 57
(3 rows) (3 rows)
-- test LIST partitioning -- test LIST partitioning
CREATE TABLE list_partitioned_events_table (user_id int, time date, event_type int, value_2 int, value_3 float, value_4 bigint) PARTITION BY LIST (time); CREATE TABLE list_partitioned_events_table (user_id int, time date, event_type int, value_2 int, value_3 float, value_4 bigint) PARTITION BY LIST (time);
CREATE TABLE list_partitioned_events_table_2014_01_01_05 PARTITION OF list_partitioned_events_table FOR VALUES IN ('2014-01-01', '2014-01-02', '2014-01-03', '2014-01-04', '2014-01-05'); CREATE TABLE list_partitioned_events_table_2014_01_01_05 PARTITION OF list_partitioned_events_table FOR VALUES IN ('2017-11-21', '2017-11-22', '2017-11-23', '2017-11-24', '2017-11-25');
CREATE TABLE list_partitioned_events_table_2014_01_06_10 PARTITION OF list_partitioned_events_table FOR VALUES IN ('2014-01-06', '2014-01-07', '2014-01-08', '2014-01-09', '2014-01-10'); CREATE TABLE list_partitioned_events_table_2014_01_06_10 PARTITION OF list_partitioned_events_table FOR VALUES IN ('2017-11-26', '2017-11-27', '2017-11-28', '2017-11-29', '2017-11-30');
CREATE TABLE list_partitioned_events_table_2014_01_11_15 PARTITION OF list_partitioned_events_table FOR VALUES IN ('2014-01-11', '2014-01-12', '2014-01-13', '2014-01-14', '2014-01-15'); CREATE TABLE list_partitioned_events_table_2014_01_11_15 PARTITION OF list_partitioned_events_table FOR VALUES IN ('2017-12-01', '2017-12-02', '2017-12-03', '2017-12-04', '2017-12-05');
-- test distributing partitioned table colocated with another partitioned table -- test distributing partitioned table colocated with another partitioned table
SELECT create_distributed_table('list_partitioned_events_table', 'user_id', colocate_with => 'partitioned_events_table'); SELECT create_distributed_table('list_partitioned_events_table', 'user_id', colocate_with => 'partitioned_events_table');
create_distributed_table create_distributed_table
@ -837,8 +837,8 @@ SELECT
FROM FROM
events_table events_table
WHERE WHERE
time >= '2014-01-01' AND time >= '2017-11-21' AND
time <= '2014-01-15'; time <= '2017-12-01';
-- LEFT JOINs used with INNER JOINs on range partitioned table, list partitioned table and non-partitioned table -- LEFT JOINs used with INNER JOINs on range partitioned table, list partitioned table and non-partitioned table
SELECT SELECT
count(*) AS cnt, "generated_group_field" count(*) AS cnt, "generated_group_field"
@ -856,14 +856,14 @@ count(*) AS cnt, "generated_group_field"
FROM FROM
list_partitioned_events_table as "list_partitioned_events_table" list_partitioned_events_table as "list_partitioned_events_table"
WHERE WHERE
user_id > 80) "temp_data_queries" user_id > 2) "temp_data_queries"
INNER JOIN INNER JOIN
(SELECT (SELECT
"users"."user_id" "users"."user_id"
FROM FROM
partitioned_users_table as "users" partitioned_users_table as "users"
WHERE WHERE
user_id > 80 and value_2 = 5) "user_filters_1" user_id > 2 and value_2 = 1) "user_filters_1"
ON ("temp_data_queries".event_user_id = "user_filters_1".user_id)) AS "multi_group_wrapper_1" ON ("temp_data_queries".event_user_id = "user_filters_1".user_id)) AS "multi_group_wrapper_1"
LEFT JOIN LEFT JOIN
(SELECT (SELECT
@ -876,19 +876,15 @@ count(*) AS cnt, "generated_group_field"
ORDER BY ORDER BY
cnt DESC, generated_group_field ASC cnt DESC, generated_group_field ASC
LIMIT 10; LIMIT 10;
cnt | generated_group_field cnt | generated_group_field
-----+----------------------- ------+-----------------------
68 | 551 1851 | 1
68 | 569 1077 | 4
68 | 645 963 | 2
68 | 713 955 | 3
68 | 734 768 | 5
34 | 3 639 | 0
34 | 5 (6 rows)
34 | 15
34 | 32
34 | 68
(10 rows)
-- --
-- Additional partitioning features -- Additional partitioning features

View File

@ -655,11 +655,11 @@ ERROR: relation "partitioned_events_table" does not exist
LINE 1: SELECT create_distributed_table('partitioned_events_table', ... LINE 1: SELECT create_distributed_table('partitioned_events_table', ...
^ ^
-- INSERT/SELECT from regular table to partitioned table -- INSERT/SELECT from regular table to partitioned table
CREATE TABLE partitioned_users_table_2009 PARTITION OF partitioned_users_table FOR VALUES FROM ('2014-01-01') TO ('2015-01-01'); CREATE TABLE partitioned_users_table_2009 PARTITION OF partitioned_users_table FOR VALUES FROM ('2017-01-01') TO ('2018-01-01');
ERROR: syntax error at or near "PARTITION" ERROR: syntax error at or near "PARTITION"
LINE 1: CREATE TABLE partitioned_users_table_2009 PARTITION OF parti... LINE 1: CREATE TABLE partitioned_users_table_2009 PARTITION OF parti...
^ ^
CREATE TABLE partitioned_events_table_2009 PARTITION OF partitioned_events_table FOR VALUES FROM ('2014-01-01') TO ('2015-01-01'); CREATE TABLE partitioned_events_table_2009 PARTITION OF partitioned_events_table FOR VALUES FROM ('2017-01-01') TO ('2018-01-01');
ERROR: syntax error at or near "PARTITION" ERROR: syntax error at or near "PARTITION"
LINE 1: CREATE TABLE partitioned_events_table_2009 PARTITION OF part... LINE 1: CREATE TABLE partitioned_events_table_2009 PARTITION OF part...
^ ^
@ -690,28 +690,28 @@ FROM
FROM FROM
partitioned_events_table as "events" partitioned_events_table as "events"
WHERE WHERE
event_type IN (10, 11, 12, 13, 14, 15)) event_type IN (1, 2) )
UNION UNION
(SELECT (SELECT
"events"."user_id", "events"."time", 1 AS event "events"."user_id", "events"."time", 1 AS event
FROM FROM
partitioned_events_table as "events" partitioned_events_table as "events"
WHERE WHERE
event_type IN (15, 16, 17, 18, 19) ) event_type IN (3, 4) )
UNION UNION
(SELECT (SELECT
"events"."user_id", "events"."time", 2 AS event "events"."user_id", "events"."time", 2 AS event
FROM FROM
partitioned_events_table as "events" partitioned_events_table as "events"
WHERE WHERE
event_type IN (20, 21, 22, 23, 24, 25) ) event_type IN (5, 6) )
UNION UNION
(SELECT (SELECT
"events"."user_id", "events"."time", 3 AS event "events"."user_id", "events"."time", 3 AS event
FROM FROM
partitioned_events_table as "events" partitioned_events_table as "events"
WHERE WHERE
event_type IN (26, 27, 28, 29, 30, 13))) t1 event_type IN (1, 6))) t1
GROUP BY "t1"."user_id") AS t) "q" GROUP BY "t1"."user_id") AS t) "q"
) AS final_query ) AS final_query
GROUP BY types GROUP BY types
@ -739,7 +739,7 @@ FROM
FROM FROM
partitioned_events_table as "events" partitioned_events_table as "events"
WHERE WHERE
event_type IN (10, 11, 12, 13, 14, 15) ) events_subquery_1) event_type IN (1, 2)) events_subquery_1)
UNION UNION
(SELECT * (SELECT *
FROM FROM
@ -754,7 +754,7 @@ FROM
events_table as "events", users_table as "users" events_table as "events", users_table as "users"
WHERE WHERE
events.user_id = users.user_id AND events.user_id = users.user_id AND
event_type IN (10, 11, 12, 13, 14, 15) event_type IN (1, 2)
GROUP BY "events"."user_id" GROUP BY "events"."user_id"
) as events_subquery_5 ) as events_subquery_5
) events_subquery_2) ) events_subquery_2)
@ -766,7 +766,7 @@ FROM
FROM FROM
partitioned_events_table as "events" partitioned_events_table as "events"
WHERE WHERE
event_type IN (20, 21, 22, 23, 24, 25) ) events_subquery_3) event_type IN (3, 4)) events_subquery_3)
UNION UNION
(SELECT * (SELECT *
FROM FROM
@ -775,7 +775,7 @@ FROM
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (26, 27, 28, 29, 30, 13)) events_subquery_4) event_type IN (5, 6)) events_subquery_4)
) t1 ) t1
GROUP BY "t1"."user_id") AS t) "q" GROUP BY "t1"."user_id") AS t) "q"
INNER JOIN INNER JOIN
@ -784,7 +784,7 @@ INNER JOIN
FROM FROM
partitioned_users_table as "users" partitioned_users_table as "users"
WHERE WHERE
value_1 > 50 and value_1 < 70) AS t value_1 > 2 and value_1 < 5) AS t
ON (t.user_id = q.user_id)) as final_query ON (t.user_id = q.user_id)) as final_query
GROUP BY GROUP BY
types types
@ -798,15 +798,15 @@ CREATE TABLE list_partitioned_events_table (user_id int, time date, event_type i
ERROR: syntax error at or near "PARTITION" ERROR: syntax error at or near "PARTITION"
LINE 1: ... int, value_2 int, value_3 float, value_4 bigint) PARTITION ... LINE 1: ... int, value_2 int, value_3 float, value_4 bigint) PARTITION ...
^ ^
CREATE TABLE list_partitioned_events_table_2014_01_01_05 PARTITION OF list_partitioned_events_table FOR VALUES IN ('2014-01-01', '2014-01-02', '2014-01-03', '2014-01-04', '2014-01-05'); CREATE TABLE list_partitioned_events_table_2014_01_01_05 PARTITION OF list_partitioned_events_table FOR VALUES IN ('2017-11-21', '2017-11-22', '2017-11-23', '2017-11-24', '2017-11-25');
ERROR: syntax error at or near "PARTITION" ERROR: syntax error at or near "PARTITION"
LINE 1: ...TABLE list_partitioned_events_table_2014_01_01_05 PARTITION ... LINE 1: ...TABLE list_partitioned_events_table_2014_01_01_05 PARTITION ...
^ ^
CREATE TABLE list_partitioned_events_table_2014_01_06_10 PARTITION OF list_partitioned_events_table FOR VALUES IN ('2014-01-06', '2014-01-07', '2014-01-08', '2014-01-09', '2014-01-10'); CREATE TABLE list_partitioned_events_table_2014_01_06_10 PARTITION OF list_partitioned_events_table FOR VALUES IN ('2017-11-26', '2017-11-27', '2017-11-28', '2017-11-29', '2017-11-30');
ERROR: syntax error at or near "PARTITION" ERROR: syntax error at or near "PARTITION"
LINE 1: ...TABLE list_partitioned_events_table_2014_01_06_10 PARTITION ... LINE 1: ...TABLE list_partitioned_events_table_2014_01_06_10 PARTITION ...
^ ^
CREATE TABLE list_partitioned_events_table_2014_01_11_15 PARTITION OF list_partitioned_events_table FOR VALUES IN ('2014-01-11', '2014-01-12', '2014-01-13', '2014-01-14', '2014-01-15'); CREATE TABLE list_partitioned_events_table_2014_01_11_15 PARTITION OF list_partitioned_events_table FOR VALUES IN ('2017-12-01', '2017-12-02', '2017-12-03', '2017-12-04', '2017-12-05');
ERROR: syntax error at or near "PARTITION" ERROR: syntax error at or near "PARTITION"
LINE 1: ...TABLE list_partitioned_events_table_2014_01_11_15 PARTITION ... LINE 1: ...TABLE list_partitioned_events_table_2014_01_11_15 PARTITION ...
^ ^
@ -828,8 +828,8 @@ SELECT
FROM FROM
events_table events_table
WHERE WHERE
time >= '2014-01-01' AND time >= '2017-11-21' AND
time <= '2014-01-15'; time <= '2017-12-01';
ERROR: relation "list_partitioned_events_table" does not exist ERROR: relation "list_partitioned_events_table" does not exist
LINE 2: list_partitioned_events_table LINE 2: list_partitioned_events_table
^ ^
@ -850,14 +850,14 @@ count(*) AS cnt, "generated_group_field"
FROM FROM
list_partitioned_events_table as "list_partitioned_events_table" list_partitioned_events_table as "list_partitioned_events_table"
WHERE WHERE
user_id > 80) "temp_data_queries" user_id > 2) "temp_data_queries"
INNER JOIN INNER JOIN
(SELECT (SELECT
"users"."user_id" "users"."user_id"
FROM FROM
partitioned_users_table as "users" partitioned_users_table as "users"
WHERE WHERE
user_id > 80 and value_2 = 5) "user_filters_1" user_id > 2 and value_2 = 1) "user_filters_1"
ON ("temp_data_queries".event_user_id = "user_filters_1".user_id)) AS "multi_group_wrapper_1" ON ("temp_data_queries".event_user_id = "user_filters_1".user_id)) AS "multi_group_wrapper_1"
LEFT JOIN LEFT JOIN
(SELECT (SELECT

File diff suppressed because it is too large Load Diff

View File

@ -189,19 +189,14 @@ FROM
INNER JOIN events_reference_table ON (events_reference_table.value_2 = users_table.user_id) INNER JOIN events_reference_table ON (events_reference_table.value_2 = users_table.user_id)
) as foo ) as foo
GROUP BY user_id ORDER BY 2 DESC LIMIT 10; GROUP BY user_id ORDER BY 2 DESC LIMIT 10;
user_id | sum user_id | sum
---------+---------- ---------+-------
12 | 92221920 2 | 31248
17 | 89192642 3 | 15120
96 | 85143744 4 | 14994
45 | 84267456 5 | 8694
90 | 84157047 1 | 7590
43 | 82110240 (5 rows)
1 | 81735612
72 | 78992640
67 | 72385516
97 | 71002659
(10 rows)
-- same query as above, reference table is wrapped into a subquery -- same query as above, reference table is wrapped into a subquery
SELECT SELECT
@ -214,19 +209,14 @@ FROM
INNER JOIN (SELECT *, random() FROM events_reference_table) as ref_all ON (ref_all.value_2 = users_table.user_id) INNER JOIN (SELECT *, random() FROM events_reference_table) as ref_all ON (ref_all.value_2 = users_table.user_id)
) as foo ) as foo
GROUP BY user_id ORDER BY 2 DESC LIMIT 10; GROUP BY user_id ORDER BY 2 DESC LIMIT 10;
user_id | sum user_id | sum
---------+---------- ---------+-------
12 | 92221920 2 | 31248
17 | 89192642 3 | 15120
96 | 85143744 4 | 14994
45 | 84267456 5 | 8694
90 | 84157047 1 | 7590
43 | 82110240 (5 rows)
1 | 81735612
72 | 78992640
67 | 72385516
97 | 71002659
(10 rows)
-- table function can be the inner relationship in a join -- table function can be the inner relationship in a join
SELECT count(*) FROM SELECT count(*) FROM
@ -336,19 +326,15 @@ FROM
LEFT JOIN events_reference_table ON (events_reference_table.value_2 = users_table.user_id) LEFT JOIN events_reference_table ON (events_reference_table.value_2 = users_table.user_id)
) as foo ) as foo
GROUP BY user_id ORDER BY 2 DESC LIMIT 10; GROUP BY user_id ORDER BY 2 DESC LIMIT 10;
user_id | sum user_id | sum
---------+---------- ---------+-------
12 | 92221920 2 | 31248
17 | 89192642 3 | 15120
96 | 85143744 4 | 14994
45 | 84267456 5 | 8694
90 | 84157047 1 | 7590
43 | 82110240 6 | 210
1 | 81735612 (6 rows)
72 | 78992640
67 | 72385516
97 | 71002659
(10 rows)
-- should not be able to pushdown since reference table is in the -- should not be able to pushdown since reference table is in the
-- direct outer part of the left join -- direct outer part of the left join
@ -398,19 +384,21 @@ SELECT * FROM
FROM FROM
events_reference_table as "events" events_reference_table as "events"
WHERE WHERE
event_type > 80) as "temp_data_queries" event_type > 2) as "temp_data_queries"
INNER JOIN INNER JOIN
(SELECT (SELECT
"users"."user_id" "users"."user_id"
FROM FROM
users_table as "users" users_table as "users"
WHERE WHERE
user_id > 80 and value_2 = 5) as foo_in ON (event_val_2 = user_id)) as foo LEFT JOIN user_id > 2 and value_2 = 1) as foo_in ON (event_val_2 = user_id)) as foo LEFT JOIN
(SELECT user_id as user_user_id FROM users_table) as fooo ON (user_id = user_user_id)) as bar; (SELECT user_id as user_user_id FROM users_table) as fooo ON (user_id = user_user_id)) as bar;
user_id user_id
--------- ---------
89 5
(1 row) 3
4
(3 rows)
-- the same query but this time reference table is in the outer part of the query -- the same query but this time reference table is in the outer part of the query
SELECT * FROM SELECT * FROM
@ -422,14 +410,14 @@ SELECT * FROM
FROM FROM
events_reference_table as "events" events_reference_table as "events"
WHERE WHERE
event_type > 80) as "temp_data_queries" event_type > 2) as "temp_data_queries"
LEFT JOIN LEFT JOIN
(SELECT (SELECT
"users"."user_id" "users"."user_id"
FROM FROM
users_table as "users" users_table as "users"
WHERE WHERE
user_id > 80 and value_2 = 5) as foo_in ON (event_val_2 = user_id)) as foo LEFT JOIN user_id > 2 and value_2 = 1) as foo_in ON (event_val_2 = user_id)) as foo LEFT JOIN
(SELECT user_id as user_user_id FROM users_table) as fooo ON (user_id = user_user_id)) as bar; (SELECT user_id as user_user_id FROM users_table) as fooo ON (user_id = user_user_id)) as bar;
ERROR: cannot pushdown the subquery ERROR: cannot pushdown the subquery
DETAIL: There exist a reference table in the outer part of the outer join DETAIL: There exist a reference table in the outer part of the outer join
@ -442,13 +430,13 @@ FROM
FROM events_reference_table FROM events_reference_table
INNER JOIN users_table ON (users_table.user_id = events_reference_table.user_id) GROUP BY users_table.user_id) AS events_all INNER JOIN users_table ON (users_table.user_id = events_reference_table.user_id) GROUP BY users_table.user_id) AS events_all
LEFT JOIN events_table ON (events_all.usr_id = events_table.user_id) GROUP BY 2 ORDER BY 1 DESC, 2 DESC LIMIT 5; LEFT JOIN events_table ON (events_all.usr_id = events_table.user_id) GROUP BY 2 ORDER BY 1 DESC, 2 DESC LIMIT 5;
max | usr_id max | usr_id
-------+-------- -----+--------
14605 | 23 432 | 2
13090 | 17 391 | 4
12915 | 25 364 | 5
12317 | 90 357 | 3
12285 | 87 105 | 1
(5 rows) (5 rows)
-- but, we fail to pushdown the following query where join that reference table appears -- but, we fail to pushdown the following query where join that reference table appears
@ -486,14 +474,14 @@ FROM
FROM FROM
users_reference_table as "users" users_reference_table as "users"
WHERE WHERE
user_id > 12 and user_id < 16 and value_1 > 20) user_where_1_1 user_id > 0 and user_id < 5 and value_1 > 1) user_where_1_1
INNER JOIN INNER JOIN
(SELECT (SELECT
"users"."user_id" "users"."user_id"
FROM FROM
users_reference_table as "users" users_reference_table as "users"
WHERE WHERE
user_id > 12 and user_id < 16 and value_2 > 60) user_where_1_join_1 user_id > 0 and user_id < 5 and value_2 > 2) user_where_1_join_1
ON ("user_where_1_1".user_id = "user_where_1_join_1".user_id)) ON ("user_where_1_1".user_id = "user_where_1_join_1".user_id))
filter_users_1 filter_users_1
JOIN LATERAL JOIN LATERAL
@ -502,7 +490,7 @@ FROM
FROM FROM
events_reference_table as "events" events_reference_table as "events"
WHERE WHERE
user_id > 12 and user_id < 16 AND user_id > 0 and user_id < 5 AND
user_id = filter_users_1.user_id user_id = filter_users_1.user_id
ORDER BY ORDER BY
time DESC time DESC
@ -518,7 +506,7 @@ FROM
users_reference_table as "users" users_reference_table as "users"
WHERE WHERE
"users"."user_id" = "some_recent_users"."user_id" AND "users"."user_id" = "some_recent_users"."user_id" AND
"users"."value_2" > 70 "users"."value_2" > 2
LIMIT 1) "some_users_data" LIMIT 1) "some_users_data"
ON TRUE ON TRUE
ORDER BY ORDER BY
@ -529,16 +517,16 @@ ORDER BY
LIMIT 10; LIMIT 10;
user_id | lastseen user_id | lastseen
---------+--------------------------------- ---------+---------------------------------
14 | Tue Jan 21 05:46:51.286381 2014 1 | Thu Nov 23 21:54:46.924477 2017
14 | Tue Jan 21 05:46:51.286381 2014 1 | Thu Nov 23 21:54:46.924477 2017
14 | Tue Jan 21 05:46:51.286381 2014 1 | Thu Nov 23 21:54:46.924477 2017
14 | Tue Jan 21 05:46:51.286381 2014 1 | Thu Nov 23 21:54:46.924477 2017
14 | Tue Jan 21 05:46:51.286381 2014 1 | Thu Nov 23 21:54:46.924477 2017
14 | Tue Jan 21 05:46:51.286381 2014 1 | Thu Nov 23 21:54:46.924477 2017
14 | Tue Jan 21 05:46:51.286381 2014 1 | Thu Nov 23 21:54:46.924477 2017
14 | Tue Jan 21 05:46:51.286381 2014 1 | Thu Nov 23 21:54:46.924477 2017
14 | Tue Jan 21 05:46:51.286381 2014 1 | Thu Nov 23 21:54:46.924477 2017
14 | Tue Jan 21 05:46:51.286381 2014 1 | Thu Nov 23 21:54:46.924477 2017
(10 rows) (10 rows)
SET citus.subquery_pushdown to OFF; SET citus.subquery_pushdown to OFF;
@ -560,7 +548,7 @@ SELECT
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
user_id > 10 and user_id < 40 AND event_type IN (40, 41, 42, 43, 44, 45) ) "temp_data_queries" user_id > 0 and user_id < 4 AND event_type IN (4, 5) ) "temp_data_queries"
INNER JOIN INNER JOIN
(SELECT (SELECT
user_where_1_1.real_user_id user_where_1_1.real_user_id
@ -570,14 +558,14 @@ SELECT
FROM FROM
users_reference_table as "users" users_reference_table as "users"
WHERE WHERE
user_id > 10 and user_id < 40 and value_2 > 50 ) user_where_1_1 user_id > 0 and user_id < 4 and value_2 > 3 ) user_where_1_1
INNER JOIN INNER JOIN
(SELECT (SELECT
"users"."user_id" "users"."user_id"
FROM FROM
users_reference_table as "users" users_reference_table as "users"
WHERE WHERE
user_id > 10 and user_id < 40 and value_3 > 50 ) user_where_1_join_1 user_id > 0 and user_id < 4 and value_3 > 3 ) user_where_1_join_1
ON ("user_where_1_1".real_user_id = "user_where_1_join_1".user_id)) "user_filters_1" ON ("user_where_1_1".real_user_id = "user_where_1_join_1".user_id)) "user_filters_1"
ON ("temp_data_queries".user_id = "user_filters_1".real_user_id)) "eventQuery") "pushedDownQuery") "pushedDownQuery" ON ("temp_data_queries".user_id = "user_filters_1".real_user_id)) "eventQuery") "pushedDownQuery") "pushedDownQuery"
GROUP BY GROUP BY
@ -586,23 +574,12 @@ ORDER BY
generated_group_field DESC, value DESC; generated_group_field DESC, value DESC;
value | generated_group_field value | generated_group_field
-------+----------------------- -------+-----------------------
1 | 966 2 | 5
1 | 917 1 | 3
1 | 905 3 | 2
1 | 868 3 | 1
1 | 836 1 | 0
1 | 791 (5 rows)
1 | 671
1 | 642
1 | 358
1 | 317
1 | 307
1 | 302
1 | 214
1 | 166
1 | 116
1 | 1
(16 rows)
-- single level inner joins with reference tables -- single level inner joins with reference tables
SELECT SELECT
@ -625,7 +602,7 @@ FROM
FROM FROM
users_reference_table as "users" users_reference_table as "users"
WHERE WHERE
user_id > 10 and user_id < 40 and value_2 > 30 user_id > 1 and user_id < 4 and value_2 > 2
) simple_user_where_1 ) simple_user_where_1
) all_buckets_1 ) all_buckets_1
) users_in_segment_1 ) users_in_segment_1
@ -635,7 +612,7 @@ FROM
FROM FROM
users_reference_table as "users" users_reference_table as "users"
WHERE WHERE
user_id > 10 and user_id < 40 and value_2 > 60 user_id > 1 and user_id < 4 and value_2 > 3
) some_users_data ) some_users_data
ON ("users_in_segment_1".user_id = "some_users_data".user_id) ON ("users_in_segment_1".user_id = "some_users_data".user_id)
) segmentalias_1) "tempQuery" ) segmentalias_1) "tempQuery"
@ -643,17 +620,13 @@ GROUP BY "value_3"
ORDER BY cnt, value_3 DESC LIMIT 10; ORDER BY cnt, value_3 DESC LIMIT 10;
value_3 | cnt value_3 | cnt
---------+----- ---------+-----
556 | 75 0 | 7
228 | 75 10 | 21
146 | 75 4 | 21
70 | 75 8 | 28
1442 | 79 6 | 28
1232 | 79 2 | 35
1090 | 79 (6 rows)
1012 | 79
886 | 79
674 | 79
(10 rows)
-- nested LATERAL JOINs with reference tables -- nested LATERAL JOINs with reference tables
SET citus.subquery_pushdown to ON; SET citus.subquery_pushdown to ON;
@ -669,14 +642,14 @@ FROM
FROM FROM
users_reference_table as "users" users_reference_table as "users"
WHERE WHERE
user_id > 20 and user_id < 70 and users.value_2 = 200) filter_users_1 user_id > 2 and user_id < 5 and users.value_2 = 3) filter_users_1
JOIN LATERAL JOIN LATERAL
(SELECT (SELECT
user_id, value_3 user_id, value_3
FROM FROM
events_reference_table as "events" events_reference_table as "events"
WHERE WHERE
user_id > 20 and user_id < 70 AND user_id > 2 and user_id < 5 AND
("events".user_id = "filter_users_1".user_id) ("events".user_id = "filter_users_1".user_id)
ORDER BY ORDER BY
value_3 DESC value_3 DESC
@ -690,7 +663,7 @@ FROM
users_reference_table as "users" users_reference_table as "users"
WHERE WHERE
"users"."user_id" = "some_recent_users"."user_id" AND "users"."user_id" = "some_recent_users"."user_id" AND
users.value_2 > 200 users.value_2 > 3
LIMIT 1) "some_users_data" ON true LIMIT 1) "some_users_data" ON true
ORDER BY ORDER BY
value_3 DESC value_3 DESC
@ -700,12 +673,12 @@ ORDER BY
LIMIT 10; LIMIT 10;
user_id | value_3 user_id | value_3
---------+--------- ---------+---------
44 | 998 3 | 5
65 | 996 3 | 5
66 | 996 3 | 5
37 | 995 4 | 4
57 | 989 4 | 4
21 | 985 4 | 4
(6 rows) (6 rows)
SET citus.subquery_pushdown to OFF; SET citus.subquery_pushdown to OFF;
@ -727,14 +700,14 @@ count(*) AS cnt, "generated_group_field"
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
user_id > 80) "temp_data_queries" user_id > 4) "temp_data_queries"
INNER JOIN INNER JOIN
(SELECT (SELECT
"users"."user_id" "users"."user_id"
FROM FROM
users_reference_table as "users" users_reference_table as "users"
WHERE WHERE
user_id > 80 and value_2 = 5) "user_filters_1" user_id > 2 and value_2 = 5) "user_filters_1"
ON ("temp_data_queries".event_user_id = "user_filters_1".user_id)) AS "multi_group_wrapper_1" ON ("temp_data_queries".event_user_id = "user_filters_1".user_id)) AS "multi_group_wrapper_1"
LEFT JOIN LEFT JOIN
(SELECT (SELECT
@ -749,17 +722,13 @@ count(*) AS cnt, "generated_group_field"
LIMIT 10; LIMIT 10;
cnt | generated_group_field cnt | generated_group_field
-----+----------------------- -----+-----------------------
176 | 551 336 | 2
176 | 569 210 | 1
176 | 645 210 | 3
176 | 713 126 | 4
176 | 734 126 | 5
88 | 3 84 | 0
88 | 5 (6 rows)
88 | 15
88 | 32
88 | 68
(10 rows)
-- RIGHT JOINs used with INNER JOINs should error out since reference table exist in the -- RIGHT JOINs used with INNER JOINs should error out since reference table exist in the
-- right side of the RIGHT JOIN. -- right side of the RIGHT JOIN.
@ -779,14 +748,14 @@ count(*) AS cnt, "generated_group_field"
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
user_id > 80) "temp_data_queries" user_id > 2) "temp_data_queries"
INNER JOIN INNER JOIN
(SELECT (SELECT
"users"."user_id" "users"."user_id"
FROM FROM
users_table as "users" users_table as "users"
WHERE WHERE
user_id > 80 and value_2 = 5) "user_filters_1" user_id > 2 and value_2 = 5) "user_filters_1"
ON ("temp_data_queries".event_user_id = "user_filters_1".user_id)) AS "multi_group_wrapper_1" ON ("temp_data_queries".event_user_id = "user_filters_1".user_id)) AS "multi_group_wrapper_1"
RIGHT JOIN RIGHT JOIN
(SELECT (SELECT
@ -815,29 +784,27 @@ FROM (
FROM users_table AS u, FROM users_table AS u,
events_reference_table AS e events_reference_table AS e
WHERE u.user_id > e.user_id WHERE u.user_id > e.user_id
AND u.user_id >= 10 AND u.user_id >= 1
AND u.user_id <= 25 AND u.user_id <= 3
AND e.event_type IN (100, 101, 102) AND e.event_type IN (1, 2)
) )
) t1 RIGHT JOIN ( ) t1 RIGHT JOIN (
SELECT DISTINCT user_id, SELECT DISTINCT user_id,
'Has done event'::TEXT AS hasdone_event 'Has done event'::TEXT AS hasdone_event
FROM events_table AS e FROM events_table AS e
WHERE e.user_id >= 10 WHERE e.user_id >= 1
AND e.user_id <= 25 AND e.user_id <= 3
AND e.event_type IN (106, 107, 108) AND e.event_type IN (3, 4)
) t2 ON (t1.user_id = t2.user_id) ) t2 ON (t1.user_id = t2.user_id)
GROUP BY t1.user_id, hasdone_event GROUP BY t1.user_id, hasdone_event
) t GROUP BY user_id, hasdone_event ) t GROUP BY user_id, hasdone_event
ORDER BY user_id; ORDER BY user_id;
user_id | sum | length | hasdone_event user_id | sum | length | hasdone_event
---------+-----+--------+---------------- ---------+-----+--------+----------------
11 | 306 | 14 | Has done event 2 | 72 | 14 | Has done event
12 | 363 | 14 | Has done event 3 | 238 | 14 | Has done event
14 | 510 | 14 | Has done event | 1 | 14 | Has done event
18 | 600 | 14 | Has done event (3 rows)
19 | 618 | 14 | Has done event
(5 rows)
-- a similar query as the above, with non-partition key comparison -- a similar query as the above, with non-partition key comparison
SELECT user_id, sum(array_length(events_table, 1)), length(hasdone_event), hasdone_event SELECT user_id, sum(array_length(events_table, 1)), length(hasdone_event), hasdone_event
@ -852,28 +819,27 @@ FROM (
FROM users_table AS u, FROM users_table AS u,
events_reference_table AS e events_reference_table AS e
WHERE u.value_1 > e.user_id WHERE u.value_1 > e.user_id
AND u.user_id >= 10 AND u.user_id >= 1
AND u.user_id <= 25 AND u.user_id <= 3
AND e.event_type >= 125 AND e.event_type < 130 AND e.event_type >= 2 AND e.event_type < 3
) )
) t1 RIGHT JOIN ( ) t1 RIGHT JOIN (
SELECT DISTINCT user_id, SELECT DISTINCT user_id,
'Has done event'::TEXT AS hasdone_event 'Has done event'::TEXT AS hasdone_event
FROM events_table AS e FROM events_table AS e
WHERE e.user_id >= 10 WHERE e.user_id >= 1
AND e.user_id <= 25 AND e.user_id <= 3
AND e.event_type >= 130 AND e.event_type < 135 AND e.event_type >= 3 AND e.event_type < 4
) t2 ON (t1.user_id = t2.user_id) ) t2 ON (t1.user_id = t2.user_id)
GROUP BY t1.user_id, hasdone_event GROUP BY t1.user_id, hasdone_event
) t GROUP BY user_id, hasdone_event ) t GROUP BY user_id, hasdone_event
ORDER BY user_id; ORDER BY user_id;
user_id | sum | length | hasdone_event user_id | sum | length | hasdone_event
---------+------+--------+---------------- ---------+-----+--------+----------------
10 | 6018 | 14 | Has done event 1 | 55 | 14 | Has done event
16 | 5373 | 14 | Has done event 2 | 88 | 14 | Has done event
17 | 5683 | 14 | Has done event 3 | 83 | 14 | Has done event
18 | 5321 | 14 | Has done event (3 rows)
(4 rows)
-- LEFT JOINs used with INNER JOINs -- LEFT JOINs used with INNER JOINs
-- events_table and users_reference_table joined -- events_table and users_reference_table joined
@ -894,14 +860,14 @@ count(*) AS cnt, "generated_group_field"
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
user_id > 80) "temp_data_queries" user_id > 2) "temp_data_queries"
INNER JOIN INNER JOIN
(SELECT (SELECT
"users"."user_id" "users"."user_id"
FROM FROM
users_reference_table as "users" users_reference_table as "users"
WHERE WHERE
user_id > 80 and value_2 = 5) "user_filters_1" user_id > 2 and value_2 = 5) "user_filters_1"
ON ("temp_data_queries".event_user_id < "user_filters_1".user_id)) AS "multi_group_wrapper_1" ON ("temp_data_queries".event_user_id < "user_filters_1".user_id)) AS "multi_group_wrapper_1"
RIGHT JOIN RIGHT JOIN
(SELECT (SELECT
@ -916,17 +882,13 @@ count(*) AS cnt, "generated_group_field"
LIMIT 10; LIMIT 10;
cnt | generated_group_field cnt | generated_group_field
-----+----------------------- -----+-----------------------
540 | 814 737 | 5
533 | 746 679 | 1
473 | 914 591 | 2
449 | 684 479 | 3
445 | 715 374 | 4
423 | 191 159 | 0
419 | 39 (6 rows)
415 | 108
414 | 819
411 | 642
(10 rows)
-- Outer subquery with reference table -- Outer subquery with reference table
SELECT "some_users_data".user_id, lastseen SELECT "some_users_data".user_id, lastseen
@ -940,7 +902,7 @@ FROM
FROM FROM
events_reference_table as "events" events_reference_table as "events"
WHERE WHERE
user_id > 10 and user_id < 40) "events_1" user_id > 1 and user_id < 4) "events_1"
ORDER BY ORDER BY
time DESC) "recent_events_1" time DESC) "recent_events_1"
GROUP BY GROUP BY
@ -953,7 +915,7 @@ FROM
FROM FROM
users_table as "users" users_table as "users"
WHERE WHERE
users.value_2 > 50 and users.value_2 < 55) "some_users_data" users.value_2 > 2 and users.value_2 < 4) "some_users_data"
ON "some_users_data"."user_id" = "some_recent_users"."user_id" ON "some_users_data"."user_id" = "some_recent_users"."user_id"
ORDER BY ORDER BY
user_id user_id
@ -979,7 +941,7 @@ FROM
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (10, 11, 12, 13, 14, 15) ) events_subquery_1) event_type IN (1, 2) ) events_subquery_1)
UNION UNION
(SELECT (SELECT
* *
@ -989,7 +951,7 @@ FROM
FROM FROM
events_reference_table as "events" events_reference_table as "events"
WHERE WHERE
event_type IN (15, 16, 17, 18, 19) ) events_subquery_2) event_type IN (3, 4) ) events_subquery_2)
UNION UNION
(SELECT (SELECT
* *
@ -999,7 +961,7 @@ FROM
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (20, 21, 22, 23, 24, 25) ) events_subquery_3) event_type IN (5, 6) ) events_subquery_3)
UNION UNION
(SELECT (SELECT
* *
@ -1009,7 +971,7 @@ FROM
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (26, 27, 28, 29, 30, 13)) events_subquery_4)) t1 event_type IN (1, 6)) events_subquery_4)) t1
GROUP BY "t1"."user_id") AS t) "q" GROUP BY "t1"."user_id") AS t) "q"
INNER JOIN INNER JOIN
(SELECT (SELECT
@ -1017,7 +979,7 @@ INNER JOIN
FROM FROM
users_table as "users" users_table as "users"
WHERE WHERE
value_1 > 50 and value_1 < 70) AS t value_1 > 2 and value_1 < 4) AS t
ON (t.user_id = q.user_id)) as final_query ON (t.user_id = q.user_id)) as final_query
ORDER BY ORDER BY
types; types;
@ -1043,7 +1005,7 @@ FROM
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (10, 11, 12, 13, 14, 15) ) events_subquery_1) event_type IN (1, 2) ) events_subquery_1)
UNION UNION
(SELECT * (SELECT *
FROM FROM
@ -1058,7 +1020,7 @@ FROM
events_reference_table as "events", users_table as "users" events_reference_table as "events", users_table as "users"
WHERE WHERE
events.user_id = users.user_id AND events.user_id = users.user_id AND
event_type IN (10, 11, 12, 13, 14, 15) event_type IN (1, 2)
GROUP BY "users"."user_id" GROUP BY "users"."user_id"
) as events_subquery_5 ) as events_subquery_5
) events_subquery_2) ) events_subquery_2)
@ -1070,7 +1032,7 @@ FROM
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (20, 21, 22, 23, 24, 25) ) events_subquery_3) event_type IN (3, 4) ) events_subquery_3)
UNION UNION
(SELECT * (SELECT *
FROM FROM
@ -1079,7 +1041,7 @@ FROM
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (26, 27, 28, 29, 30, 13)) events_subquery_4) event_type IN (5, 6)) events_subquery_4)
) t1 ) t1
GROUP BY "t1"."user_id") AS t) "q" GROUP BY "t1"."user_id") AS t) "q"
INNER JOIN INNER JOIN
@ -1088,7 +1050,7 @@ INNER JOIN
FROM FROM
users_table as "users" users_table as "users"
WHERE WHERE
value_1 > 50 and value_1 < 70) AS t value_1 > 2 and value_1 < 4) AS t
ON (t.user_id = q.user_id)) as final_query ON (t.user_id = q.user_id)) as final_query
GROUP BY GROUP BY
types types
@ -1114,7 +1076,7 @@ FROM
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (10, 11, 12, 13, 14, 15) ) events_subquery_1) event_type IN (1, 2) ) events_subquery_1)
UNION ALL UNION ALL
(SELECT * (SELECT *
FROM FROM
@ -1123,7 +1085,7 @@ FROM
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (15, 16, 17, 18, 19) ) events_subquery_2) event_type IN (3, 4) ) events_subquery_2)
UNION ALL UNION ALL
(SELECT * (SELECT *
FROM FROM
@ -1132,7 +1094,7 @@ FROM
FROM FROM
events_reference_table as "events" events_reference_table as "events"
WHERE WHERE
event_type IN (20, 21, 22, 23, 24, 25) ) events_subquery_3) event_type IN (5, 6) ) events_subquery_3)
UNION ALL UNION ALL
(SELECT * (SELECT *
FROM FROM
@ -1141,12 +1103,12 @@ FROM
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (26, 27, 28, 29, 30, 13)) events_subquery_4)) t1 event_type IN (1, 6)) events_subquery_4)) t1
GROUP BY "t1"."user_id") AS t) "q" GROUP BY "t1"."user_id") AS t) "q"
INNER JOIN INNER JOIN
(SELECT "users"."user_id" (SELECT "users"."user_id"
FROM users_table as "users" FROM users_table as "users"
WHERE value_1 > 50 and value_1 < 70) AS t ON (t.user_id = q.user_id)) as final_query WHERE value_1 > 2 and value_1 < 4) AS t ON (t.user_id = q.user_id)) as final_query
GROUP BY types GROUP BY types
ORDER BY types; ORDER BY types;
ERROR: cannot push down this subquery ERROR: cannot push down this subquery
@ -1185,14 +1147,14 @@ count(*) AS cnt, "generated_group_field"
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
user_id > 80) "temp_data_queries" user_id > 2) "temp_data_queries"
INNER JOIN INNER JOIN
(SELECT (SELECT
"users"."user_id" "users"."user_id"
FROM FROM
users_reference_table as "users" users_reference_table as "users"
WHERE WHERE
user_id > 80 and value_2 = 5) "user_filters_1" user_id > 2 and value_2 = 5) "user_filters_1"
ON ("temp_data_queries".event_user_id < "user_filters_1".user_id)) AS "multi_group_wrapper_1" ON ("temp_data_queries".event_user_id < "user_filters_1".user_id)) AS "multi_group_wrapper_1"
RIGHT JOIN RIGHT JOIN
(SELECT (SELECT
@ -1221,8 +1183,8 @@ WHERE
FROM FROM
events_reference_table as e2 events_reference_table as e2
WHERE WHERE
value_2 = 15 AND value_2 = 1 AND
value_3 > 25 AND value_3 > 3 AND
e1.value_2 > e2.value_2 e1.value_2 > e2.value_2
) )
AND u1.user_id > e1.user_id AND u1.user_id > e1.user_id
@ -1234,7 +1196,7 @@ DETAIL: Each relation should be joined with at least one another relation using
SELECT foo.user_id FROM SELECT foo.user_id FROM
( (
SELECT m.user_id, random() FROM users_table m JOIN events_reference_table r ON int4eq(m.user_id, r.user_id) SELECT m.user_id, random() FROM users_table m JOIN events_reference_table r ON int4eq(m.user_id, r.user_id)
WHERE event_type > 100000 WHERE event_type > 100
) as foo; ) as foo;
user_id user_id
--------- ---------
@ -1257,9 +1219,9 @@ SELECT foo.user_id FROM
ORDER BY 1 LIMIT 3; ORDER BY 1 LIMIT 3;
user_id user_id
--------- ---------
0
1 1
2 2
3
(3 rows) (3 rows)
-- not supported since distinct is on the reference table column -- not supported since distinct is on the reference table column
@ -1284,9 +1246,9 @@ SELECT foo.user_id FROM
ORDER BY 1 LIMIT 3; ORDER BY 1 LIMIT 3;
user_id user_id
--------- ---------
0
1 1
2 2
3
(3 rows) (3 rows)
-- should be able to pushdown since one of the subqueries has distinct on reference tables -- should be able to pushdown since one of the subqueries has distinct on reference tables
@ -1301,11 +1263,11 @@ LIMIT 5
OFFSET 0; OFFSET 0;
distinct_users | event_type | time distinct_users | event_type | time
----------------+------------+--------------------------------- ----------------+------------+---------------------------------
78 | 815 | Tue Jan 21 05:59:54.833395 2014 1 | 6 | Thu Nov 23 21:54:46.924477 2017
92 | 826 | Tue Jan 21 05:57:26.643861 2014 4 | 1 | Thu Nov 23 18:10:21.338399 2017
65 | 241 | Tue Jan 21 05:56:52.624231 2014 3 | 2 | Thu Nov 23 18:08:26.550729 2017
23 | 573 | Tue Jan 21 05:55:28.796818 2014 2 | 1 | Thu Nov 23 17:26:14.563216 2017
98 | 23 | Tue Jan 21 05:54:57.987456 2014 3 | 4 | Thu Nov 23 16:44:41.903713 2017
(5 rows) (5 rows)
-- the same query wuth multiple reference tables in the subquery -- the same query wuth multiple reference tables in the subquery
@ -1321,11 +1283,11 @@ LIMIT 5
OFFSET 0; OFFSET 0;
distinct_users | event_type | time distinct_users | event_type | time
----------------+------------+--------------------------------- ----------------+------------+---------------------------------
65 | 241 | Tue Jan 21 05:56:52.624231 2014 1 | 6 | Thu Nov 23 21:54:46.924477 2017
98 | 23 | Tue Jan 21 05:54:57.987456 2014 4 | 1 | Thu Nov 23 18:10:21.338399 2017
26 | 957 | Tue Jan 21 05:43:16.99674 2014 3 | 2 | Thu Nov 23 18:08:26.550729 2017
44 | 682 | Tue Jan 21 05:43:00.838945 2014 2 | 1 | Thu Nov 23 17:26:14.563216 2017
81 | 852 | Tue Jan 21 05:34:56.310878 2014 3 | 4 | Thu Nov 23 16:44:41.903713 2017
(5 rows) (5 rows)
-- similar query as the above, but with group bys -- similar query as the above, but with group bys
@ -1339,11 +1301,11 @@ LIMIT 5
OFFSET 0; OFFSET 0;
distinct_users | event_type | time distinct_users | event_type | time
----------------+------------+--------------------------------- ----------------+------------+---------------------------------
78 | 815 | Tue Jan 21 05:59:54.833395 2014 1 | 6 | Thu Nov 23 21:54:46.924477 2017
92 | 826 | Tue Jan 21 05:57:26.643861 2014 4 | 1 | Thu Nov 23 18:10:21.338399 2017
65 | 241 | Tue Jan 21 05:56:52.624231 2014 3 | 2 | Thu Nov 23 18:08:26.550729 2017
23 | 573 | Tue Jan 21 05:55:28.796818 2014 2 | 1 | Thu Nov 23 17:26:14.563216 2017
98 | 23 | Tue Jan 21 05:54:57.987456 2014 3 | 4 | Thu Nov 23 16:44:41.903713 2017
(5 rows) (5 rows)
-- should not push down this query since there is a distributed table (i.e., events_table) -- should not push down this query since there is a distributed table (i.e., events_table)
@ -1378,10 +1340,10 @@ ORDER BY 1 DESC
LIMIT 4; LIMIT 4;
user_id | user_id user_id | user_id
---------+--------- ---------+---------
98 | 98 6 | 6
96 | 96 5 | 5
90 | 90 4 | 4
81 | 81 3 | 3
(4 rows) (4 rows)
-- should not pushdown since there is a non partition column on the DISTINCT clause -- should not pushdown since there is a non partition column on the DISTINCT clause

View File

@ -16,21 +16,20 @@ WHERE
FROM FROM
events_table events_table
WHERE WHERE
users_table.user_id = events_table.user_id AND event_type = 50 users_table.user_id = events_table.user_id AND event_type = 1
GROUP BY GROUP BY
user_id user_id
) )
GROUP BY user_id GROUP BY user_id
HAVING count(*) > 66 HAVING count(*) > 2
ORDER BY user_id ORDER BY user_id
LIMIT 5; LIMIT 5;
user_id user_id
--------- ---------
49 1
55 5
56 6
63 (3 rows)
(4 rows)
-- same query with one additional join on non distribution column -- same query with one additional join on non distribution column
SELECT SELECT
@ -44,20 +43,20 @@ WHERE
FROM FROM
events_table events_table
WHERE WHERE
users_table.user_id = events_table.user_id AND event_type = 50 AND users_table.user_id = events_table.user_id AND event_type = 1 AND
users_table.time > events_table.time users_table.time > events_table.time
GROUP BY GROUP BY
user_id user_id
) )
GROUP BY user_id GROUP BY user_id
HAVING count(*) > 66 HAVING count(*) > 1
ORDER BY user_id ORDER BY user_id
LIMIT 5; LIMIT 5;
user_id user_id
--------- ---------
55 1
56 5
63 6
(3 rows) (3 rows)
-- the other way around is not supported -- the other way around is not supported
@ -72,13 +71,13 @@ WHERE
FROM FROM
events_table events_table
WHERE WHERE
users_table.user_id > events_table.user_id AND event_type = 50 AND users_table.user_id > events_table.user_id AND event_type = 1 AND
users_table.time = events_table.time users_table.time = events_table.time
GROUP BY GROUP BY
user_id user_id
) )
GROUP BY user_id GROUP BY user_id
HAVING count(*) > 66 HAVING count(*) > 1
ORDER BY user_id ORDER BY user_id
LIMIT 5; LIMIT 5;
ERROR: cannot pushdown the subquery since all relations are not joined using distribution keys ERROR: cannot pushdown the subquery since all relations are not joined using distribution keys
@ -89,7 +88,7 @@ SELECT
FROM FROM
users_table users_table
WHERE WHERE
value_2 > 545 AND value_2 > 1 AND
value_2 < ALL (SELECT avg(value_3) FROM events_table WHERE users_table.user_id = events_table.user_id GROUP BY user_id) value_2 < ALL (SELECT avg(value_3) FROM events_table WHERE users_table.user_id = events_table.user_id GROUP BY user_id)
GROUP BY GROUP BY
1 1
@ -98,9 +97,9 @@ ORDER BY
LIMIT 3; LIMIT 3;
user_id user_id
--------- ---------
69 4
52 3
12 2
(3 rows) (3 rows)
-- IN operator on non-partition key -- IN operator on non-partition key
@ -115,23 +114,30 @@ WHERE
FROM FROM
events_table as e2 events_table as e2
WHERE WHERE
value_2 = 15 AND value_3 > 25 AND value_2 = 1 AND value_3 > 3 AND
e1.user_id = e2.user_id e1.user_id = e2.user_id
) )
ORDER BY 1; ORDER BY 1;
user_id user_id
--------- ---------
8 2
17 2
33 2
47 2
54 2
54 2
56 2
71 2
79 2
86 2
(10 rows) 3
3
3
3
3
3
3
(17 rows)
-- NOT IN on non-partition key -- NOT IN on non-partition key
SELECT SELECT
@ -145,17 +151,21 @@ WHERE
FROM FROM
events_table as e2 events_table as e2
WHERE WHERE
value_2 = 15 AND value_3 > 25 AND value_2 = 1 AND value_3 > 3 AND
e1.user_id = e2.user_id e1.user_id = e2.user_id
) )
GROUP BY 1 GROUP BY 1
HAVING count(*) > 122 HAVING count(*) > 2
ORDER BY 1; ORDER BY 1;
user_id user_id
--------- ---------
23 1
25 2
(2 rows) 3
4
5
6
(6 rows)
-- non-correlated query with =ANY on partition keys -- non-correlated query with =ANY on partition keys
SELECT SELECT
@ -163,14 +173,14 @@ ORDER BY 1;
FROM FROM
users_table users_table
WHERE WHERE
user_id =ANY(SELECT user_id FROM users_table WHERE value_1 >= 10 AND value_1 <= 20) GROUP BY 1 ORDER BY 2 DESC LIMIT 5; user_id =ANY(SELECT user_id FROM users_table WHERE value_1 >= 1 AND value_1 <= 2) GROUP BY 1 ORDER BY 2 DESC LIMIT 5;
user_id | count user_id | count
---------+------- ---------+-------
12 | 121 5 | 26
87 | 117 4 | 23
37 | 115 2 | 18
23 | 115 3 | 17
46 | 115 6 | 10
(5 rows) (5 rows)
-- users that appeared more than 118 times -- users that appeared more than 118 times
@ -178,7 +188,7 @@ SELECT
user_id user_id
FROM FROM
users_table users_table
WHERE 118 <= WHERE 2 <=
(SELECT (SELECT
count(*) count(*)
FROM FROM
@ -193,18 +203,20 @@ ORDER BY
user_id; user_id;
user_id user_id
--------- ---------
13 1
17 2
23 3
25 4
(4 rows) 5
6
(6 rows)
-- the following query doesn't have a meaningful result -- the following query doesn't have a meaningful result
-- but it is a valid query with an arbitrary subquery in -- but it is a valid query with an arbitrary subquery in
-- WHERE clause -- WHERE clause
SELECT user_id, value_2 FROM users_table WHERE SELECT user_id, value_2 FROM users_table WHERE
value_1 > 101 AND value_1 < 110 value_1 > 1 AND value_1 < 3
AND value_2 >= 5 AND value_2 >= 1
AND user_id IN AND user_id IN
( (
SELECT SELECT
@ -217,7 +229,7 @@ SELECT user_id, value_2 FROM users_table WHERE
min(time) AS view_homepage_time min(time) AS view_homepage_time
FROM events_table FROM events_table
WHERE WHERE
event_type IN (10, 20, 30, 40, 50, 60, 70, 80, 90) event_type IN (0, 1)
GROUP BY user_id GROUP BY user_id
) e1 LEFT JOIN LATERAL ( ) e1 LEFT JOIN LATERAL (
SELECT SELECT
@ -227,7 +239,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_table FROM events_table
WHERE WHERE
user_id = e1.user_id AND user_id = e1.user_id AND
event_type IN (11, 21, 31, 41, 51, 61, 71, 81, 91) event_type IN (1, 2)
ORDER BY time ORDER BY time
) e2 ON true LEFT JOIN LATERAL ( ) e2 ON true LEFT JOIN LATERAL (
SELECT SELECT
@ -237,7 +249,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_table FROM events_table
WHERE WHERE
user_id = e2.user_id AND user_id = e2.user_id AND
event_type IN (12, 22, 32, 42, 52, 62, 72, 82, 92) event_type IN (2, 3)
ORDER BY time ORDER BY time
) e3 ON true LEFT JOIN LATERAL ( ) e3 ON true LEFT JOIN LATERAL (
SELECT SELECT
@ -247,7 +259,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_table FROM events_table
WHERE WHERE
user_id = e3.user_id AND user_id = e3.user_id AND
event_type IN (13, 23, 33, 43, 53, 63, 73, 83, 93) event_type IN (3, 4)
ORDER BY time ORDER BY time
) e4 ON true LEFT JOIN LATERAL ( ) e4 ON true LEFT JOIN LATERAL (
SELECT SELECT
@ -255,7 +267,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_table FROM events_table
WHERE WHERE
user_id = e4.user_id AND user_id = e4.user_id AND
event_type IN (14, 24, 34, 44, 54, 64, 74, 84, 94) event_type IN (5, 6)
ORDER BY time ORDER BY time
) e5 ON true ) e5 ON true
group by e1.user_id group by e1.user_id
@ -264,14 +276,22 @@ SELECT user_id, value_2 FROM users_table WHERE
ORDER BY 1, 2; ORDER BY 1, 2;
user_id | value_2 user_id | value_2
---------+--------- ---------+---------
5 | 884 2 | 2
42 | 55 2 | 2
42 | 471 2 | 4
51 | 758 3 | 2
72 | 897 3 | 2
82 | 691 4 | 1
95 | 951 4 | 3
(7 rows) 5 | 1
5 | 2
5 | 2
5 | 5
5 | 5
6 | 3
6 | 4
6 | 4
(15 rows)
-- similar to the above query -- similar to the above query
-- the following query doesn't have a meaningful result -- the following query doesn't have a meaningful result
@ -300,9 +320,9 @@ WHERE
events_table events_table
WHERE WHERE
users_table.user_id = events_table.user_id AND users_table.user_id = events_table.user_id AND
users_table.user_id >= 10 AND users_table.user_id >= 1 AND
users_table.user_id <= 70 AND users_table.user_id <= 3 AND
events_table.event_type > 10 AND events_table.event_type < 12 events_table.event_type > 1 AND events_table.event_type < 3
) )
UNION UNION
(SELECT (SELECT
@ -314,9 +334,9 @@ WHERE
events_table events_table
WHERE WHERE
users_table.user_id = events_table.user_id AND users_table.user_id = events_table.user_id AND
users_table.user_id >= 10 AND users_table.user_id >= 1 AND
users_table.user_id <= 70 AND users_table.user_id <= 3 AND
events_table.event_type > 12 AND events_table.event_type < 14 events_table.event_type > 2 AND events_table.event_type < 4
) )
) AS subquery_1 ) AS subquery_1
LEFT JOIN LEFT JOIN
@ -326,9 +346,9 @@ WHERE
FROM FROM
users_table users_table
WHERE WHERE
user_id >= 10 AND user_id >= 1 AND
user_id <= 70 AND user_id <= 3 AND
users_table.value_1 > 15 AND users_table.value_1 < 17 users_table.value_1 > 3 AND users_table.value_1 < 5
GROUP BY GROUP BY
user_id user_id
HAVING HAVING
@ -342,17 +362,13 @@ WHERE
count_pay, user_id count_pay, user_id
) )
GROUP BY user_id GROUP BY user_id
HAVING count(*) > 3 AND sum(value_2) > 49000 HAVING count(*) > 1 AND sum(value_2) > 29
ORDER BY 1; ORDER BY 1;
user_id user_id
--------- ---------
18 2
29 3
40 (2 rows)
49
58
69
(6 rows)
-- the following query doesn't have a meaningful result -- the following query doesn't have a meaningful result
-- but it is a valid query with an arbitrary subquery in -- but it is a valid query with an arbitrary subquery in
@ -373,9 +389,9 @@ FROM (
user_id user_id
FROM FROM
users_table users_table
WHERE value_2 >= 5 WHERE value_2 >= 1
AND EXISTS (SELECT user_id FROM events_table WHERE event_type > 100 AND event_type <= 300 AND value_3 > 100 AND user_id = users_table.user_id) AND EXISTS (SELECT user_id FROM events_table WHERE event_type > 1 AND event_type <= 3 AND value_3 > 1 AND user_id = users_table.user_id)
AND NOT EXISTS (SELECT user_id FROM events_table WHERE event_type > 300 AND event_type <= 350 AND value_3 > 100 AND user_id = users_table.user_id) AND NOT EXISTS (SELECT user_id FROM events_table WHERE event_type > 3 AND event_type <= 4 AND value_3 > 1 AND user_id = users_table.user_id)
) )
) t ) t
GROUP BY user_id GROUP BY user_id
@ -383,9 +399,8 @@ FROM (
ORDER BY 2 DESC, 1; ORDER BY 2 DESC, 1;
user_id | array_length user_id | array_length
---------+-------------- ---------+--------------
96 | 12204 5 | 364
8 | 8170 (1 row)
(2 rows)
-- --
-- below tests only aims for cases where all relations -- below tests only aims for cases where all relations
@ -393,8 +408,8 @@ ORDER BY 2 DESC, 1;
-- --
-- e4 is not joined on the partition key -- e4 is not joined on the partition key
SELECT user_id, value_2 FROM users_table WHERE SELECT user_id, value_2 FROM users_table WHERE
value_1 > 101 AND value_1 < 110 value_1 > 1 AND value_1 < 2
AND value_2 >= 5 AND value_2 >= 1
AND user_id IN AND user_id IN
( (
SELECT SELECT
@ -407,7 +422,7 @@ SELECT user_id, value_2 FROM users_table WHERE
min(time) AS view_homepage_time min(time) AS view_homepage_time
FROM events_table FROM events_table
WHERE WHERE
event_type IN (10, 20, 30, 40, 50, 60, 70, 80, 90) event_type IN (0, 1)
GROUP BY user_id GROUP BY user_id
) e1 LEFT JOIN LATERAL ( ) e1 LEFT JOIN LATERAL (
SELECT SELECT
@ -417,7 +432,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_table FROM events_table
WHERE WHERE
user_id = e1.user_id AND user_id = e1.user_id AND
event_type IN (11, 21, 31, 41, 51, 61, 71, 81, 91) event_type IN (1, 2)
ORDER BY time ORDER BY time
) e2 ON true LEFT JOIN LATERAL ( ) e2 ON true LEFT JOIN LATERAL (
SELECT SELECT
@ -427,7 +442,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_table FROM events_table
WHERE WHERE
user_id = e2.user_id AND user_id = e2.user_id AND
event_type IN (12, 22, 32, 42, 52, 62, 72, 82, 92) event_type IN (2, 3)
ORDER BY time ORDER BY time
) e3 ON true LEFT JOIN LATERAL ( ) e3 ON true LEFT JOIN LATERAL (
SELECT SELECT
@ -437,7 +452,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_table FROM events_table
WHERE WHERE
value_2 = e3.user_id AND value_2 = e3.user_id AND
event_type IN (13, 23, 33, 43, 53, 63, 73, 83, 93) event_type IN (3, 4)
ORDER BY time ORDER BY time
) e4 ON true LEFT JOIN LATERAL ( ) e4 ON true LEFT JOIN LATERAL (
SELECT SELECT
@ -445,7 +460,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_table FROM events_table
WHERE WHERE
user_id = e4.user_id AND user_id = e4.user_id AND
event_type IN (14, 24, 34, 44, 54, 64, 74, 84, 94) event_type IN (5, 6)
ORDER BY time ORDER BY time
) e5 ON true ) e5 ON true
group by e1.user_id group by e1.user_id
@ -477,9 +492,9 @@ WHERE
events_table events_table
WHERE WHERE
users_table.user_id = events_table.user_id AND users_table.user_id = events_table.user_id AND
users_table.user_id >= 10 AND users_table.user_id >= 1 AND
users_table.user_id <= 70 AND users_table.user_id <= 3 AND
events_table.event_type > 10 AND events_table.event_type < 12 events_table.event_type > 1 AND events_table.event_type < 3
) )
UNION UNION
(SELECT (SELECT
@ -491,9 +506,9 @@ WHERE
events_table events_table
WHERE WHERE
users_table.user_id = events_table.user_id AND users_table.user_id = events_table.user_id AND
users_table.user_id >= 10 AND users_table.user_id >= 1 AND
users_table.user_id <= 70 AND users_table.user_id <= 3 AND
events_table.event_type > 12 AND events_table.event_type < 14 events_table.event_type > 2 AND events_table.event_type < 4
) )
) AS subquery_1 ) AS subquery_1
LEFT JOIN LEFT JOIN
@ -503,9 +518,9 @@ WHERE
FROM FROM
users_table users_table
WHERE WHERE
user_id >= 10 AND user_id >= 1 AND
user_id <= 70 AND user_id <= 3 AND
users_table.value_1 > 15 AND users_table.value_1 < 17 users_table.value_1 > 3 AND users_table.value_1 < 5
GROUP BY GROUP BY
user_id user_id
HAVING HAVING
@ -519,7 +534,7 @@ WHERE
count_pay, user_id count_pay, user_id
) )
GROUP BY user_id GROUP BY user_id
HAVING count(*) > 3 AND sum(value_2) > 49000 HAVING count(*) > 1 AND sum(value_2) > 29
ORDER BY 1; ORDER BY 1;
ERROR: cannot pushdown the subquery since all relations are not joined using distribution keys ERROR: cannot pushdown the subquery since all relations are not joined using distribution keys
DETAIL: Each relation should be joined with at least one another relation using distribution keys and equality operator. DETAIL: Each relation should be joined with at least one another relation using distribution keys and equality operator.
@ -541,8 +556,8 @@ FROM (
FROM FROM
users_table users_table
WHERE value_2 >= 5 WHERE value_2 >= 5
AND EXISTS (SELECT user_id FROM events_table WHERE event_type > 100 AND event_type <= 300 AND value_3 > 100 AND user_id = users_table.user_id) AND EXISTS (SELECT user_id FROM events_table WHERE event_type > 1 AND event_type <= 3 AND value_3 > 1 AND user_id = users_table.user_id)
AND NOT EXISTS (SELECT user_id FROM events_table WHERE event_type > 300 AND event_type <= 350 AND value_3 > 100 AND user_id != users_table.user_id) AND NOT EXISTS (SELECT user_id FROM events_table WHERE event_type > 3 AND event_type <= 4 AND value_3 > 1 AND user_id != users_table.user_id)
) )
) t ) t
GROUP BY user_id GROUP BY user_id
@ -562,8 +577,8 @@ ORDER BY 1 ASC
LIMIT 2; LIMIT 2;
user_id user_id
--------- ---------
0 1
0 1
(2 rows) (2 rows)
-- subquery in where clause has a volatile function and no relation -- subquery in where clause has a volatile function and no relation
@ -590,7 +605,7 @@ WHERE
FROM FROM
events_table events_table
WHERE WHERE
users_table.user_id = events_table.user_id AND event_type = 50 users_table.user_id = events_table.user_id AND event_type = 2
GROUP BY GROUP BY
user_id user_id
OFFSET 3 OFFSET 3
@ -629,9 +644,9 @@ DETAIL: Limit in subquery is currently unsupported
-- semi join is not on the partition key for the third subquery -- semi join is not on the partition key for the third subquery
SELECT user_id SELECT user_id
FROM users_table FROM users_table
WHERE user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 10 AND value_1 <= 20) WHERE user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 1 AND value_1 <= 2)
AND user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 30 AND value_1 <= 40) AND user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 3 AND value_1 <= 4)
AND value_2 IN (SELECT user_id FROM users_table WHERE value_1 >= 50 AND value_1 <= 60); AND value_2 IN (SELECT user_id FROM users_table WHERE value_1 >= 5 AND value_1 <= 6);
ERROR: cannot pushdown the subquery since all relations are not joined using distribution keys ERROR: cannot pushdown the subquery since all relations are not joined using distribution keys
DETAIL: Each relation should be joined with at least one another relation using distribution keys and equality operator. DETAIL: Each relation should be joined with at least one another relation using distribution keys and equality operator.
CREATE FUNCTION test_join_function(integer, integer) RETURNS bool CREATE FUNCTION test_join_function(integer, integer) RETURNS bool
@ -641,9 +656,9 @@ CREATE FUNCTION test_join_function(integer, integer) RETURNS bool
RETURNS NULL ON NULL INPUT; RETURNS NULL ON NULL INPUT;
-- we disallow JOINs via functions -- we disallow JOINs via functions
SELECT user_id, value_2 FROM users_table WHERE SELECT user_id, value_2 FROM users_table WHERE
value_1 = 101 value_1 = 1
AND value_2 >= 5 AND value_2 >= 2
AND NOT EXISTS (SELECT user_id FROM events_table WHERE event_type=101 AND value_3 > 100 AND test_join_function(events_table.user_id, users_table.user_id)) AND NOT EXISTS (SELECT user_id FROM events_table WHERE event_type=1 AND value_3 > 1 AND test_join_function(events_table.user_id, users_table.user_id))
ORDER BY 1 DESC, 2 DESC ORDER BY 1 DESC, 2 DESC
LIMIT 3; LIMIT 3;
ERROR: cannot pushdown the subquery since all relations are not joined using distribution keys ERROR: cannot pushdown the subquery since all relations are not joined using distribution keys

View File

@ -19,9 +19,9 @@ ORDER BY user_id
LIMIT 3; LIMIT 3;
user_id user_id
--------- ---------
0
1 1
2 2
3
(3 rows) (3 rows)
-- subqueries in WHERE with NOT EXISTS operator, should work since -- subqueries in WHERE with NOT EXISTS operator, should work since
@ -117,9 +117,9 @@ ORDER BY 2 DESC, 1 DESC
LIMIT 3; LIMIT 3;
user_id | count user_id | count
---------+------- ---------+-------
87 | 117 5 | 26
59 | 115 4 | 23
46 | 115 3 | 17
(3 rows) (3 rows)
-- immutable functions are also treated as reference tables -- immutable functions are also treated as reference tables
@ -141,9 +141,9 @@ ORDER BY 2 DESC, 1 DESC
LIMIT 3; LIMIT 3;
user_id | count user_id | count
---------+------- ---------+-------
12 | 121 5 | 26
87 | 117 4 | 23
59 | 115 2 | 18
(3 rows) (3 rows)
-- immutable functions are also treated as reference tables -- immutable functions are also treated as reference tables
@ -165,15 +165,13 @@ ORDER BY 2 DESC, 1 DESC
LIMIT 3; LIMIT 3;
user_id | count user_id | count
---------+------- ---------+-------
12 | 121 6 | 10
87 | 117 (1 row)
59 | 115
(3 rows)
-- should error out since reference table exist on the left side -- should error out since reference table exist on the left side
-- of the left lateral join -- of the left lateral join
SELECT user_id, value_2 FROM users_table WHERE SELECT user_id, value_2 FROM users_table WHERE
value_1 > 101 AND value_1 < 110 value_1 > 1 AND value_1 < 3
AND value_2 >= 5 AND value_2 >= 5
AND user_id IN AND user_id IN
( (
@ -187,7 +185,7 @@ SELECT user_id, value_2 FROM users_table WHERE
min(time) AS view_homepage_time min(time) AS view_homepage_time
FROM events_reference_table FROM events_reference_table
WHERE WHERE
event_type IN (10, 20, 30, 40, 50, 60, 70, 80, 90) event_type IN (1, 2)
GROUP BY user_id GROUP BY user_id
) e1 LEFT JOIN LATERAL ( ) e1 LEFT JOIN LATERAL (
SELECT SELECT
@ -197,7 +195,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_reference_table FROM events_reference_table
WHERE WHERE
user_id = e1.user_id AND user_id = e1.user_id AND
event_type IN (11, 21, 31, 41, 51, 61, 71, 81, 91) event_type IN (2, 3)
ORDER BY time ORDER BY time
) e2 ON true LEFT JOIN LATERAL ( ) e2 ON true LEFT JOIN LATERAL (
SELECT SELECT
@ -207,7 +205,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_reference_table FROM events_reference_table
WHERE WHERE
user_id = e2.user_id AND user_id = e2.user_id AND
event_type IN (12, 22, 32, 42, 52, 62, 72, 82, 92) event_type IN (3, 4)
ORDER BY time ORDER BY time
) e3 ON true LEFT JOIN LATERAL ( ) e3 ON true LEFT JOIN LATERAL (
SELECT SELECT
@ -217,7 +215,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_reference_table FROM events_reference_table
WHERE WHERE
user_id = e3.user_id AND user_id = e3.user_id AND
event_type IN (13, 23, 33, 43, 53, 63, 73, 83, 93) event_type IN (4, 5)
ORDER BY time ORDER BY time
) e4 ON true LEFT JOIN LATERAL ( ) e4 ON true LEFT JOIN LATERAL (
SELECT SELECT
@ -225,7 +223,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_reference_table FROM events_reference_table
WHERE WHERE
user_id = e4.user_id AND user_id = e4.user_id AND
event_type IN (14, 24, 34, 44, 54, 64, 74, 84, 94) event_type IN (5, 6)
ORDER BY time ORDER BY time
) e5 ON true ) e5 ON true
group by e1.user_id group by e1.user_id
@ -240,15 +238,15 @@ DETAIL: There exist a reference table in the outer part of the outer join
FROM FROM
users_table users_table
WHERE WHERE
value_3 =ANY(SELECT value_2 FROM users_reference_table WHERE value_1 >= 10 AND value_1 <= 20) value_3 =ANY(SELECT value_2 FROM users_reference_table WHERE value_1 >= 1 AND value_1 <= 2)
GROUP BY 1 ORDER BY 2 DESC, 1 DESC LIMIT 5; GROUP BY 1 ORDER BY 2 DESC, 1 DESC LIMIT 5;
user_id | count user_id | count
---------+------- ---------+-------
48 | 18 5 | 26
26 | 18 4 | 23
15 | 17 2 | 18
54 | 16 3 | 17
35 | 15 6 | 10
(5 rows) (5 rows)
-- non-partition key comparison with reference table -- non-partition key comparison with reference table
@ -263,8 +261,8 @@ WHERE
FROM FROM
events_reference_table as e2 events_reference_table as e2
WHERE WHERE
value_2 = 15 AND value_2 = 2 AND
value_3 > 25 AND value_3 > 3 AND
e1.value_2 > e2.value_2 e1.value_2 > e2.value_2
) )
GROUP BY 1 GROUP BY 1
@ -272,18 +270,18 @@ ORDER BY 2 DESC, 1 DESC
LIMIT 5; LIMIT 5;
user_id | count user_id | count
---------+------- ---------+-------
3 | 5 2 | 7
56 | 4 5 | 6
99 | 2 4 | 5
94 | 2 1 | 4
92 | 2 6 | 3
(5 rows) (5 rows)
-- subqueries in both WHERE and FROM clauses -- subqueries in both WHERE and FROM clauses
-- should work since reference table is on the -- should work since reference table is on the
-- inner part of the join -- inner part of the join
SELECT user_id, value_2 FROM users_table WHERE SELECT user_id, value_2 FROM users_table WHERE
value_1 > 101 AND value_1 < 110 value_1 > 1 AND value_1 < 3
AND value_2 >= 5 AND value_2 >= 5
AND user_id IN AND user_id IN
( (
@ -297,7 +295,7 @@ SELECT user_id, value_2 FROM users_table WHERE
min(time) AS view_homepage_time min(time) AS view_homepage_time
FROM events_table FROM events_table
WHERE WHERE
event_type IN (10, 20, 30, 40, 50, 60, 70, 80, 90) event_type IN (1, 2)
GROUP BY user_id GROUP BY user_id
) e1 LEFT JOIN LATERAL ( ) e1 LEFT JOIN LATERAL (
SELECT SELECT
@ -307,7 +305,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_table FROM events_table
WHERE WHERE
user_id = e1.user_id AND user_id = e1.user_id AND
event_type IN (11, 21, 31, 41, 51, 61, 71, 81, 91) event_type IN (2, 3)
ORDER BY time ORDER BY time
) e2 ON true LEFT JOIN LATERAL ( ) e2 ON true LEFT JOIN LATERAL (
SELECT SELECT
@ -317,7 +315,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_table FROM events_table
WHERE WHERE
user_id = e2.user_id AND user_id = e2.user_id AND
event_type IN (12, 22, 32, 42, 52, 62, 72, 82, 92) event_type IN (3, 4)
ORDER BY time ORDER BY time
) e3 ON true LEFT JOIN LATERAL ( ) e3 ON true LEFT JOIN LATERAL (
SELECT SELECT
@ -327,7 +325,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_table FROM events_table
WHERE WHERE
user_id = e3.user_id AND user_id = e3.user_id AND
event_type IN (13, 23, 33, 43, 53, 63, 73, 83, 93) event_type IN (4, 5)
ORDER BY time ORDER BY time
) e4 ON true LEFT JOIN LATERAL ( ) e4 ON true LEFT JOIN LATERAL (
SELECT SELECT
@ -335,7 +333,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_reference_table FROM events_reference_table
WHERE WHERE
user_id = e4.user_id AND user_id = e4.user_id AND
event_type IN (14, 24, 34, 44, 54, 64, 74, 84, 94) event_type IN (5, 6)
ORDER BY time ORDER BY time
) e5 ON true ) e5 ON true
group by e1.user_id group by e1.user_id
@ -344,14 +342,9 @@ SELECT user_id, value_2 FROM users_table WHERE
ORDER BY 1, 2; ORDER BY 1, 2;
user_id | value_2 user_id | value_2
---------+--------- ---------+---------
5 | 884 5 | 5
42 | 55 5 | 5
42 | 471 (2 rows)
51 | 758
72 | 897
82 | 691
95 | 951
(7 rows)
-- reference tables are not allowed if there is sublink -- reference tables are not allowed if there is sublink
SELECT SELECT
@ -381,10 +374,10 @@ FROM users_reference_table
WHERE value_2 > ALL WHERE value_2 > ALL
(SELECT min(value_2) (SELECT min(value_2)
FROM events_table FROM events_table
WHERE event_type > 50 AND users_reference_table.user_id = events_table.user_id WHERE event_type > 2 AND users_reference_table.user_id = events_table.user_id
GROUP BY user_id) GROUP BY user_id)
GROUP BY user_id GROUP BY user_id
HAVING count(*) > 66 HAVING count(*) > 3
ORDER BY 2 DESC, ORDER BY 2 DESC,
1 DESC 1 DESC
LIMIT 5; LIMIT 5;
@ -415,12 +408,12 @@ WHERE
FROM FROM
events_reference_table events_reference_table
WHERE WHERE
users_table.user_id = events_reference_table.user_id AND event_type = 50 users_table.user_id = events_reference_table.user_id AND event_type = 2
GROUP BY GROUP BY
users_table.user_id users_table.user_id
) )
GROUP BY user_id GROUP BY user_id
HAVING count(*) > 66 HAVING count(*) > 3
ORDER BY user_id ORDER BY user_id
LIMIT 5; LIMIT 5;
ERROR: cannot push down this subquery ERROR: cannot push down this subquery
@ -438,12 +431,12 @@ WHERE
FROM FROM
events_reference_table events_reference_table
WHERE WHERE
users_table.user_id = events_reference_table.user_id AND event_type = 50 users_table.user_id = events_reference_table.user_id AND event_type = 2
GROUP BY GROUP BY
(users_table.user_id * 2) (users_table.user_id * 2)
) )
GROUP BY user_id GROUP BY user_id
HAVING count(*) > 66 HAVING count(*) > 3
ORDER BY user_id ORDER BY user_id
LIMIT 5; LIMIT 5;
ERROR: cannot push down this subquery ERROR: cannot push down this subquery

View File

@ -19,14 +19,14 @@ FROM (
SELECT user_id, time SELECT user_id, time
FROM users_table FROM users_table
WHERE WHERE
user_id >= 10 AND user_id >= 1 AND
user_id <= 70 AND user_id <= 3 AND
users_table.value_1 > 10 AND users_table.value_1 < 12 users_table.value_1 > 1 AND users_table.value_1 < 3
) u LEFT JOIN LATERAL ( ) u LEFT JOIN LATERAL (
SELECT event_type, time SELECT event_type, time
FROM events_table FROM events_table
WHERE user_id = u.user_id AND WHERE user_id = u.user_id AND
events_table.event_type > 10 AND events_table.event_type < 12 events_table.event_type > 1 AND events_table.event_type < 3
) t ON true ) t ON true
GROUP BY user_id GROUP BY user_id
) AS shard_union ) AS shard_union
@ -34,13 +34,9 @@ ORDER BY user_lastseen DESC, user_id;
EXECUTE prepared_subquery_1; EXECUTE prepared_subquery_1;
user_id | user_lastseen | array_length user_id | user_lastseen | array_length
---------+---------------------------------+-------------- ---------+---------------------------------+--------------
12 | Sun Jan 19 01:49:20.372688 2014 | 1 2 | Thu Nov 23 11:47:26.900284 2017 | 12
20 | Sat Jan 18 14:25:31.817903 2014 | 1 3 | Thu Nov 23 11:18:53.114408 2017 | 14
42 | Thu Jan 16 07:08:02.651966 2014 | 1 (2 rows)
56 | Tue Jan 14 12:11:47.27375 2014 | 1
57 | Mon Jan 13 14:53:50.494836 2014 | 1
65 | Sun Jan 12 03:14:26.810597 2014 | 1
(6 rows)
PREPARE prepared_subquery_2(int, int) AS PREPARE prepared_subquery_2(int, int) AS
SELECT SELECT
@ -58,93 +54,65 @@ FROM (
WHERE WHERE
user_id >= $1 AND user_id >= $1 AND
user_id <= $2 AND user_id <= $2 AND
users_table.value_1 > 10 AND users_table.value_1 < 12 users_table.value_1 > 1 AND users_table.value_1 < 3
) u LEFT JOIN LATERAL ( ) u LEFT JOIN LATERAL (
SELECT event_type, time SELECT event_type, time
FROM events_table FROM events_table
WHERE user_id = u.user_id AND WHERE user_id = u.user_id AND
events_table.event_type > 10 AND events_table.event_type < 12 events_table.event_type > 1 AND events_table.event_type < 3
) t ON true ) t ON true
GROUP BY user_id GROUP BY user_id
) AS shard_union ) AS shard_union
ORDER BY user_lastseen DESC, user_id; ORDER BY user_lastseen DESC, user_id;
-- should be fine with more than five executions -- should be fine with more than five executions
EXECUTE prepared_subquery_2(10, 70); EXECUTE prepared_subquery_2(1, 3);
user_id | user_lastseen | array_length user_id | user_lastseen | array_length
---------+---------------------------------+-------------- ---------+---------------------------------+--------------
12 | Sun Jan 19 01:49:20.372688 2014 | 1 2 | Thu Nov 23 11:47:26.900284 2017 | 12
20 | Sat Jan 18 14:25:31.817903 2014 | 1 3 | Thu Nov 23 11:18:53.114408 2017 | 14
42 | Thu Jan 16 07:08:02.651966 2014 | 1 (2 rows)
56 | Tue Jan 14 12:11:47.27375 2014 | 1
57 | Mon Jan 13 14:53:50.494836 2014 | 1
65 | Sun Jan 12 03:14:26.810597 2014 | 1
(6 rows)
EXECUTE prepared_subquery_2(10, 70); EXECUTE prepared_subquery_2(1, 3);
user_id | user_lastseen | array_length user_id | user_lastseen | array_length
---------+---------------------------------+-------------- ---------+---------------------------------+--------------
12 | Sun Jan 19 01:49:20.372688 2014 | 1 2 | Thu Nov 23 11:47:26.900284 2017 | 12
20 | Sat Jan 18 14:25:31.817903 2014 | 1 3 | Thu Nov 23 11:18:53.114408 2017 | 14
42 | Thu Jan 16 07:08:02.651966 2014 | 1 (2 rows)
56 | Tue Jan 14 12:11:47.27375 2014 | 1
57 | Mon Jan 13 14:53:50.494836 2014 | 1
65 | Sun Jan 12 03:14:26.810597 2014 | 1
(6 rows)
EXECUTE prepared_subquery_2(10, 70); EXECUTE prepared_subquery_2(1, 3);
user_id | user_lastseen | array_length user_id | user_lastseen | array_length
---------+---------------------------------+-------------- ---------+---------------------------------+--------------
12 | Sun Jan 19 01:49:20.372688 2014 | 1 2 | Thu Nov 23 11:47:26.900284 2017 | 12
20 | Sat Jan 18 14:25:31.817903 2014 | 1 3 | Thu Nov 23 11:18:53.114408 2017 | 14
42 | Thu Jan 16 07:08:02.651966 2014 | 1 (2 rows)
56 | Tue Jan 14 12:11:47.27375 2014 | 1
57 | Mon Jan 13 14:53:50.494836 2014 | 1
65 | Sun Jan 12 03:14:26.810597 2014 | 1
(6 rows)
EXECUTE prepared_subquery_2(10, 70); EXECUTE prepared_subquery_2(1, 3);
user_id | user_lastseen | array_length user_id | user_lastseen | array_length
---------+---------------------------------+-------------- ---------+---------------------------------+--------------
12 | Sun Jan 19 01:49:20.372688 2014 | 1 2 | Thu Nov 23 11:47:26.900284 2017 | 12
20 | Sat Jan 18 14:25:31.817903 2014 | 1 3 | Thu Nov 23 11:18:53.114408 2017 | 14
42 | Thu Jan 16 07:08:02.651966 2014 | 1 (2 rows)
56 | Tue Jan 14 12:11:47.27375 2014 | 1
57 | Mon Jan 13 14:53:50.494836 2014 | 1
65 | Sun Jan 12 03:14:26.810597 2014 | 1
(6 rows)
EXECUTE prepared_subquery_2(10, 70); EXECUTE prepared_subquery_2(1, 3);
user_id | user_lastseen | array_length user_id | user_lastseen | array_length
---------+---------------------------------+-------------- ---------+---------------------------------+--------------
12 | Sun Jan 19 01:49:20.372688 2014 | 1 2 | Thu Nov 23 11:47:26.900284 2017 | 12
20 | Sat Jan 18 14:25:31.817903 2014 | 1 3 | Thu Nov 23 11:18:53.114408 2017 | 14
42 | Thu Jan 16 07:08:02.651966 2014 | 1 (2 rows)
56 | Tue Jan 14 12:11:47.27375 2014 | 1
57 | Mon Jan 13 14:53:50.494836 2014 | 1
65 | Sun Jan 12 03:14:26.810597 2014 | 1
(6 rows)
EXECUTE prepared_subquery_2(10, 70); EXECUTE prepared_subquery_2(1, 3);
user_id | user_lastseen | array_length user_id | user_lastseen | array_length
---------+---------------------------------+-------------- ---------+---------------------------------+--------------
12 | Sun Jan 19 01:49:20.372688 2014 | 1 2 | Thu Nov 23 11:47:26.900284 2017 | 12
20 | Sat Jan 18 14:25:31.817903 2014 | 1 3 | Thu Nov 23 11:18:53.114408 2017 | 14
42 | Thu Jan 16 07:08:02.651966 2014 | 1 (2 rows)
56 | Tue Jan 14 12:11:47.27375 2014 | 1
57 | Mon Jan 13 14:53:50.494836 2014 | 1
65 | Sun Jan 12 03:14:26.810597 2014 | 1
(6 rows)
EXECUTE prepared_subquery_2(10, 70); EXECUTE prepared_subquery_2(1, 3);
user_id | user_lastseen | array_length user_id | user_lastseen | array_length
---------+---------------------------------+-------------- ---------+---------------------------------+--------------
12 | Sun Jan 19 01:49:20.372688 2014 | 1 2 | Thu Nov 23 11:47:26.900284 2017 | 12
20 | Sat Jan 18 14:25:31.817903 2014 | 1 3 | Thu Nov 23 11:18:53.114408 2017 | 14
42 | Thu Jan 16 07:08:02.651966 2014 | 1 (2 rows)
56 | Tue Jan 14 12:11:47.27375 2014 | 1
57 | Mon Jan 13 14:53:50.494836 2014 | 1
65 | Sun Jan 12 03:14:26.810597 2014 | 1
(6 rows)
-- prepared statements with subqueries in WHERE clause -- prepared statements with subqueries in WHERE clause
PREPARE prepared_subquery_3(int, int, int, int, int, int) AS PREPARE prepared_subquery_3(int, int, int, int, int, int) AS
@ -159,64 +127,64 @@ ORDER BY
user_id DESC user_id DESC
LIMIT 5; LIMIT 5;
-- enough times (6+) to actually use prepared statements -- enough times (6+) to actually use prepared statements
EXECUTE prepared_subquery_3(50, 60, 20, 10, 30, 40); EXECUTE prepared_subquery_3(4, 5, 1, 0, 2, 3);
user_id user_id
--------- ---------
93 6
90 5
88 4
87 3
84 2
(5 rows) (5 rows)
EXECUTE prepared_subquery_3(50, 60, 20, 10, 30, 40); EXECUTE prepared_subquery_3(4, 5, 1, 0, 2, 3);
user_id user_id
--------- ---------
93 6
90 5
88 4
87 3
84 2
(5 rows) (5 rows)
EXECUTE prepared_subquery_3(50, 60, 20, 10, 30, 40); EXECUTE prepared_subquery_3(4, 5, 1, 0, 2, 3);
user_id user_id
--------- ---------
93 6
90 5
88 4
87 3
84 2
(5 rows) (5 rows)
EXECUTE prepared_subquery_3(50, 60, 20, 10, 30, 40); EXECUTE prepared_subquery_3(4, 5, 1, 0, 2, 3);
user_id user_id
--------- ---------
93 6
90 5
88 4
87 3
84 2
(5 rows) (5 rows)
EXECUTE prepared_subquery_3(50, 60, 20, 10, 30, 40); EXECUTE prepared_subquery_3(4, 5, 1, 0, 2, 3);
user_id user_id
--------- ---------
93 6
90 5
88 4
87 3
84 2
(5 rows) (5 rows)
EXECUTE prepared_subquery_3(50, 60, 20, 10, 30, 40); EXECUTE prepared_subquery_3(4, 5, 1, 0, 2, 3);
user_id user_id
--------- ---------
93 6
90 5
88 4
87 3
84 2
(5 rows) (5 rows)
CREATE FUNCTION plpgsql_subquery_test(int, int) RETURNS TABLE(count bigint) AS $$ CREATE FUNCTION plpgsql_subquery_test(int, int) RETURNS TABLE(count bigint) AS $$
@ -233,7 +201,7 @@ BEGIN
FROM FROM
users_table AS ma, events_table as short_list users_table AS ma, events_table as short_list
WHERE WHERE
short_list.user_id = ma.user_id and ma.value_1 < $1 and short_list.event_type < 50 short_list.user_id = ma.user_id and ma.value_1 < $1 and short_list.event_type < 3
) temp ) temp
ON users_table.user_id = temp.user_id ON users_table.user_id = temp.user_id
WHERE WHERE
@ -242,44 +210,44 @@ BEGIN
END; END;
$$ LANGUAGE plpgsql; $$ LANGUAGE plpgsql;
-- enough times (6+) to actually use prepared statements -- enough times (6+) to actually use prepared statements
SELECT plpgsql_subquery_test(10, 20); SELECT plpgsql_subquery_test(1, 2);
plpgsql_subquery_test plpgsql_subquery_test
----------------------- -----------------------
1500 539
(1 row) (1 row)
SELECT plpgsql_subquery_test(10, 20); SELECT plpgsql_subquery_test(1, 2);
plpgsql_subquery_test plpgsql_subquery_test
----------------------- -----------------------
1500 539
(1 row) (1 row)
SELECT plpgsql_subquery_test(10, 20); SELECT plpgsql_subquery_test(1, 2);
plpgsql_subquery_test plpgsql_subquery_test
----------------------- -----------------------
1500 539
(1 row) (1 row)
SELECT plpgsql_subquery_test(10, 20); SELECT plpgsql_subquery_test(1, 2);
plpgsql_subquery_test plpgsql_subquery_test
----------------------- -----------------------
1500 539
(1 row) (1 row)
SELECT plpgsql_subquery_test(10, 20); SELECT plpgsql_subquery_test(1, 2);
plpgsql_subquery_test plpgsql_subquery_test
----------------------- -----------------------
1500 539
(1 row) (1 row)
SELECT plpgsql_subquery_test(10, 20); SELECT plpgsql_subquery_test(1, 2);
plpgsql_subquery_test plpgsql_subquery_test
----------------------- -----------------------
1500 539
(1 row) (1 row)
-- this should also work, but should return 0 given that int = NULL is always returns false -- this should also work, but should return 0 given that int = NULL is always returns false
SELECT plpgsql_subquery_test(10, NULL); SELECT plpgsql_subquery_test(1, NULL);
plpgsql_subquery_test plpgsql_subquery_test
----------------------- -----------------------
0 0
@ -296,14 +264,14 @@ CREATE FUNCTION sql_subquery_test(int, int) RETURNS bigint AS $$
FROM FROM
users_table AS ma, events_table as short_list users_table AS ma, events_table as short_list
WHERE WHERE
short_list.user_id = ma.user_id and ma.value_1 < $1 and short_list.event_type < 50 short_list.user_id = ma.user_id and ma.value_1 < $1 and short_list.event_type < 3
) temp ) temp
ON users_table.user_id = temp.user_id ON users_table.user_id = temp.user_id
WHERE WHERE
users_table.value_1 < $2; users_table.value_1 < $2;
$$ LANGUAGE SQL; $$ LANGUAGE SQL;
-- should error out -- should error out
SELECT sql_subquery_test(5,5); SELECT sql_subquery_test(1,1);
ERROR: could not create distributed plan ERROR: could not create distributed plan
DETAIL: Possibly this is caused by the use of parameters in SQL functions, which is not supported in Citus. DETAIL: Possibly this is caused by the use of parameters in SQL functions, which is not supported in Citus.
HINT: Consider using PL/pgSQL functions instead. HINT: Consider using PL/pgSQL functions instead.

View File

@ -8,27 +8,27 @@ SET citus.enable_router_execution TO false;
-- a very simple union query -- a very simple union query
SELECT user_id, counter SELECT user_id, counter
FROM ( FROM (
SELECT user_id, value_2 % 10 AS counter FROM events_table WHERE event_type IN (1, 2, 3, 4, 5) SELECT user_id, value_2 % 10 AS counter FROM events_table WHERE event_type IN (1, 2)
UNION UNION
SELECT user_id, value_2 % 10 AS counter FROM events_table WHERE event_type IN (5, 6, 7, 8, 9, 10) SELECT user_id, value_2 % 10 AS counter FROM events_table WHERE event_type IN (5, 6)
) user_id ) user_id
ORDER BY 2 DESC,1 ORDER BY 2 DESC,1
LIMIT 5; LIMIT 5;
user_id | counter user_id | counter
---------+--------- ---------+---------
7 | 9 2 | 5
8 | 9 3 | 5
15 | 9 4 | 5
16 | 9 1 | 4
20 | 9 2 | 4
(5 rows) (5 rows)
-- a very simple union query with reference table -- a very simple union query with reference table
SELECT user_id, counter SELECT user_id, counter
FROM ( FROM (
SELECT user_id, value_2 % 10 AS counter FROM events_table WHERE event_type IN (1, 2, 3, 4, 5) SELECT user_id, value_2 % 10 AS counter FROM events_table WHERE event_type IN (1, 2)
UNION UNION
SELECT user_id, value_2 % 10 AS counter FROM events_reference_table WHERE event_type IN (5, 6, 7, 8, 9, 10) SELECT user_id, value_2 % 10 AS counter FROM events_reference_table WHERE event_type IN (5, 6)
) user_id ) user_id
ORDER BY 2 DESC,1 ORDER BY 2 DESC,1
LIMIT 5; LIMIT 5;
@ -37,27 +37,27 @@ DETAIL: Reference tables are not allowed with set operations
-- the same query with union all -- the same query with union all
SELECT user_id, counter SELECT user_id, counter
FROM ( FROM (
SELECT user_id, value_2 % 10 AS counter FROM events_table WHERE event_type IN (1, 2, 3, 4, 5) SELECT user_id, value_2 % 10 AS counter FROM events_table WHERE event_type IN (1, 2)
UNION ALL UNION ALL
SELECT user_id, value_2 % 10 AS counter FROM events_table WHERE event_type IN (5, 6, 7, 8, 9, 10) SELECT user_id, value_2 % 10 AS counter FROM events_table WHERE event_type IN (5, 6)
) user_id ) user_id
ORDER BY 2 DESC,1 ORDER BY 2 DESC,1
LIMIT 5; LIMIT 5;
user_id | counter user_id | counter
---------+--------- ---------+---------
7 | 9 2 | 5
7 | 9 2 | 5
8 | 9 3 | 5
15 | 9 3 | 5
15 | 9 4 | 5
(5 rows) (5 rows)
-- the same query with union all and reference table -- the same query with union all and reference table
SELECT user_id, counter SELECT user_id, counter
FROM ( FROM (
SELECT user_id, value_2 % 10 AS counter FROM events_table WHERE event_type IN (1, 2, 3, 4, 5) SELECT user_id, value_2 % 10 AS counter FROM events_table WHERE event_type IN (1, 2)
UNION ALL UNION ALL
SELECT user_id, value_2 % 10 AS counter FROM events_reference_table WHERE event_type IN (5, 6, 7, 8, 9, 10) SELECT user_id, value_2 % 10 AS counter FROM events_reference_table WHERE event_type IN (5, 6)
) user_id ) user_id
ORDER BY 2 DESC,1 ORDER BY 2 DESC,1
LIMIT 5; LIMIT 5;
@ -66,58 +66,58 @@ DETAIL: Reference tables are not allowed with set operations
-- the same query with group by -- the same query with group by
SELECT user_id, sum(counter) SELECT user_id, sum(counter)
FROM ( FROM (
SELECT user_id, value_2 % 10 AS counter FROM events_table WHERE event_type IN (1, 2, 3, 4, 5) SELECT user_id, value_2 % 10 AS counter FROM events_table WHERE event_type IN (1, 2)
UNION UNION
SELECT user_id, value_2 % 10 AS counter FROM events_table WHERE event_type IN (5, 6, 7, 8, 9, 10) SELECT user_id, value_2 % 10 AS counter FROM events_table WHERE event_type IN (5, 6)
) user_id ) user_id
GROUP BY 1 GROUP BY 1
ORDER BY 2 DESC,1 ORDER BY 2 DESC,1
LIMIT 5; LIMIT 5;
user_id | sum user_id | sum
---------+----- ---------+-----
49 | 22 2 | 15
15 | 19 3 | 15
26 | 17 4 | 15
48 | 17 5 | 10
61 | 17 1 | 7
(5 rows) (5 rows)
-- the same query with UNION ALL clause -- the same query with UNION ALL clause
SELECT user_id, sum(counter) SELECT user_id, sum(counter)
FROM ( FROM (
SELECT user_id, value_2 % 10 AS counter FROM events_table WHERE event_type IN (1, 2, 3, 4, 5) SELECT user_id, value_2 % 10 AS counter FROM events_table WHERE event_type IN (1, 2)
UNION ALL UNION ALL
SELECT user_id, value_2 % 10 AS counter FROM events_table WHERE event_type IN (5, 6, 7, 8, 9, 10) SELECT user_id, value_2 % 10 AS counter FROM events_table WHERE event_type IN (5, 6)
) user_id ) user_id
GROUP BY 1 GROUP BY 1
ORDER BY 2 DESC,1 ORDER BY 2 DESC,1
LIMIT 5; LIMIT 5;
user_id | sum user_id | sum
---------+----- ---------+-----
48 | 35 2 | 32
61 | 30 3 | 32
15 | 28 4 | 23
49 | 25 5 | 21
80 | 24 1 | 15
(5 rows) (5 rows)
-- the same query target list entries shuffled -- the same query target list entries shuffled
SELECT user_id, sum(counter) SELECT user_id, sum(counter)
FROM ( FROM (
SELECT value_2 % 10 AS counter, user_id FROM events_table WHERE event_type IN (1, 2, 3, 4, 5) SELECT value_2 % 10 AS counter, user_id FROM events_table WHERE event_type IN (1, 2)
UNION UNION
SELECT value_2 % 10 AS counter, user_id FROM events_table WHERE event_type IN (5, 6, 7, 8, 9, 10) SELECT value_2 % 10 AS counter, user_id FROM events_table WHERE event_type IN (5, 6)
) user_id ) user_id
GROUP BY 1 GROUP BY 1
ORDER BY 2 DESC,1 ORDER BY 2 DESC,1
LIMIT 5; LIMIT 5;
user_id | sum user_id | sum
---------+----- ---------+-----
49 | 22 2 | 15
15 | 19 3 | 15
26 | 17 4 | 15
48 | 17 5 | 10
61 | 17 1 | 7
(5 rows) (5 rows)
-- same query with GROUP BY -- same query with GROUP BY
@ -131,13 +131,13 @@ GROUP BY
user_id user_id
--HAVING sum(counter) > 900 --HAVING sum(counter) > 900
ORDER BY 1,2 DESC LIMIT 5; ORDER BY 1,2 DESC LIMIT 5;
user_id | sum user_id | sum
---------+------ ---------+-----
1 | 518 1 | 7
2 | 637 2 | 15
4 | 343 3 | 15
6 | 354 4 | 15
7 | 1374 5 | 10
(5 rows) (5 rows)
-- the same query target list entries shuffled but this time the subqueries target list -- the same query target list entries shuffled but this time the subqueries target list
@ -152,50 +152,49 @@ GROUP BY
user_id user_id
--HAVING sum(counter) > 900 --HAVING sum(counter) > 900
ORDER BY 1,2 DESC LIMIT 5; ORDER BY 1,2 DESC LIMIT 5;
user_id | sum user_id | sum
---------+------ ---------+-----
1 | 518 1 | 7
2 | 637 2 | 15
4 | 343 3 | 15
6 | 354 4 | 15
7 | 1374 5 | 10
(5 rows) (5 rows)
-- similar query this time more subqueries and target list contains a resjunk entry -- similar query this time more subqueries and target list contains a resjunk entry
SELECT sum(counter) SELECT sum(counter)
FROM ( FROM (
SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 20 GROUP BY user_id HAVING sum(value_2) > 500 SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 1 GROUP BY user_id HAVING sum(value_2) > 5
UNION UNION
SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 40 and value_1 < 60 GROUP BY user_id HAVING sum(value_2) > 500 SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 2 and value_1 < 3 GROUP BY user_id HAVING sum(value_2) > 25
UNION UNION
SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 60 and value_1 < 80 GROUP BY user_id HAVING sum(value_2) > 500 SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 3 and value_1 < 4 GROUP BY user_id HAVING sum(value_2) > 25
UNION UNION
SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 80 and value_1 < 100 GROUP BY user_id HAVING sum(value_2) > 500 SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 4 and value_1 < 5 GROUP BY user_id HAVING sum(value_2) > 25
UNION UNION
SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 100 and value_1 < 120 GROUP BY user_id HAVING sum(value_2) > 500 SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 5 and value_1 < 6 GROUP BY user_id HAVING sum(value_2) > 25
) user_id ) user_id
GROUP BY user_id ORDER BY 1 DESC LIMIT 5; GROUP BY user_id ORDER BY 1 DESC LIMIT 5;
sum sum
------- -----
27772 141
25720 94
24993 87
24968 76
23508 (4 rows)
(5 rows)
-- similar query this time more subqueries with reference table and target list contains a resjunk entry -- similar query this time more subqueries with reference table and target list contains a resjunk entry
SELECT sum(counter) SELECT sum(counter)
FROM ( FROM (
SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 20 GROUP BY user_id HAVING sum(value_2) > 500 SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 1 GROUP BY user_id HAVING sum(value_2) > 25
UNION UNION
SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 40 and value_1 < 60 GROUP BY user_id HAVING sum(value_2) > 500 SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 2 and value_1 < 3 GROUP BY user_id HAVING sum(value_2) > 25
UNION UNION
SELECT user_id, sum(value_2) AS counter FROM users_reference_table where value_1 < 60 and value_1 < 80 GROUP BY user_id HAVING sum(value_2) > 500 SELECT user_id, sum(value_2) AS counter FROM users_reference_table where value_1 < 3 and value_1 < 4 GROUP BY user_id HAVING sum(value_2) > 25
UNION UNION
SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 80 and value_1 < 100 GROUP BY user_id HAVING sum(value_2) > 500 SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 4 and value_1 < 5 GROUP BY user_id HAVING sum(value_2) > 25
UNION UNION
SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 100 and value_1 < 120 GROUP BY user_id HAVING sum(value_2) > 500 SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 5 and value_1 < 6 GROUP BY user_id HAVING sum(value_2) > 25
) user_id ) user_id
GROUP BY user_id ORDER BY 1 DESC LIMIT 5; GROUP BY user_id ORDER BY 1 DESC LIMIT 5;
ERROR: cannot pushdown this query ERROR: cannot pushdown this query
@ -203,25 +202,24 @@ DETAIL: Reference tables are not allowed with set operations
-- similar query as above, with UNION ALL -- similar query as above, with UNION ALL
SELECT sum(counter) SELECT sum(counter)
FROM ( FROM (
SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 20 GROUP BY user_id HAVING sum(value_2) > 5000 SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 1 GROUP BY user_id HAVING sum(value_2) > 250
UNION ALL UNION ALL
SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 40 and value_1 < 60 GROUP BY user_id HAVING sum(value_2) > 500 SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 2 and value_1 < 3 GROUP BY user_id HAVING sum(value_2) > 25
UNION ALL UNION ALL
SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 60 and value_1 < 80 GROUP BY user_id HAVING sum(value_2) > 500 SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 3 and value_1 < 4 GROUP BY user_id HAVING sum(value_2) > 25
UNION ALL UNION ALL
SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 80 and value_1 < 100 GROUP BY user_id HAVING sum(value_2) > 500 SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 4 and value_1 < 5 GROUP BY user_id HAVING sum(value_2) > 25
UNION ALL UNION ALL
SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 100 and value_1 < 120 GROUP BY user_id HAVING sum(value_2) > 500 SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 5 and value_1 < 6 GROUP BY user_id HAVING sum(value_2) > 25
) user_id ) user_id
GROUP BY user_id ORDER BY 1 DESC LIMIT 5; GROUP BY user_id ORDER BY 1 DESC LIMIT 5;
sum sum
------- -----
27667 135
25080 87
24814 85
24365 69
23508 (4 rows)
(5 rows)
-- unions within unions -- unions within unions
SELECT * SELECT *
@ -265,13 +263,13 @@ FROM (
user_id)) AS ftop user_id)) AS ftop
ORDER BY 2 DESC, 1 DESC ORDER BY 2 DESC, 1 DESC
LIMIT 5; LIMIT 5;
user_id | sum user_id | sum
---------+-------- ---------+-----
23 | 126017 2 | 107
45 | 117323 3 | 101
25 | 116595 5 | 94
17 | 116520 4 | 91
90 | 115843 1 | 62
(5 rows) (5 rows)
-- unions within unions with reference table -- unions within unions with reference table
@ -334,7 +332,7 @@ FROM
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (10, 11, 12, 13, 14, 15)) events_subquery_1) event_type IN (1, 2)) events_subquery_1)
UNION UNION
(SELECT * (SELECT *
FROM FROM
@ -343,7 +341,7 @@ FROM
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (15, 16, 17, 18, 19) ) events_subquery_2) event_type IN (2, 3) ) events_subquery_2)
UNION UNION
(SELECT * (SELECT *
FROM FROM
@ -352,7 +350,7 @@ FROM
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (20, 21, 22, 23, 24, 25) ) events_subquery_3) event_type IN (4, 5) ) events_subquery_3)
UNION UNION
(SELECT * (SELECT *
FROM FROM
@ -361,17 +359,17 @@ FROM
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (26, 27, 28, 29, 30, 13)) events_subquery_4)) t1 event_type IN (6, 1)) events_subquery_4)) t1
GROUP BY "t1"."user_id") AS t) "q" GROUP BY "t1"."user_id") AS t) "q"
) as final_query ) as final_query
GROUP BY types GROUP BY types
ORDER BY types; ORDER BY types;
types | sumofeventtype types | sumofeventtype
-------+---------------- -------+----------------
0 | 55 0 | 43
1 | 38 1 | 42
2 | 70 2 | 28
3 | 58 3 | 25
(4 rows) (4 rows)
-- exactly the same query -- exactly the same query
@ -391,38 +389,38 @@ FROM
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (10, 11, 12, 13, 14, 15)) event_type IN (1, 2))
UNION UNION
(SELECT (SELECT
"events"."user_id", "events"."time", 1 AS event "events"."user_id", "events"."time", 1 AS event
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (15, 16, 17, 18, 19) ) event_type IN (2, 3) )
UNION UNION
(SELECT (SELECT
"events"."user_id", "events"."time", 2 AS event "events"."user_id", "events"."time", 2 AS event
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (20, 21, 22, 23, 24, 25) ) event_type IN (4, 5) )
UNION UNION
(SELECT (SELECT
"events"."user_id", "events"."time", 3 AS event "events"."user_id", "events"."time", 3 AS event
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (26, 27, 28, 29, 30, 13))) t1 event_type IN (6, 1))) t1
GROUP BY "t1"."user_id") AS t) "q" GROUP BY "t1"."user_id") AS t) "q"
) as final_query ) as final_query
GROUP BY types GROUP BY types
ORDER BY types; ORDER BY types;
types | sumofeventtype types | sumofeventtype
-------+---------------- -------+----------------
0 | 55 0 | 43
1 | 38 1 | 42
2 | 70 2 | 28
3 | 58 3 | 25
(4 rows) (4 rows)
-- again excatly the same query with top level wrapper removed -- again excatly the same query with top level wrapper removed
@ -437,37 +435,37 @@ FROM
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (10, 11, 12, 13, 14, 15)) event_type IN (1, 2))
UNION UNION
(SELECT (SELECT
"events"."user_id", "events"."time", 1 AS event "events"."user_id", "events"."time", 1 AS event
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (15, 16, 17, 18, 19) ) event_type IN (2, 3) )
UNION UNION
(SELECT (SELECT
"events"."user_id", "events"."time", 2 AS event "events"."user_id", "events"."time", 2 AS event
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (20, 21, 22, 23, 24, 25) ) event_type IN (4, 5) )
UNION UNION
(SELECT (SELECT
"events"."user_id", "events"."time", 3 AS event "events"."user_id", "events"."time", 3 AS event
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (26, 27, 28, 29, 30, 13))) t1 event_type IN (6, 1))) t1
GROUP BY "t1"."user_id") AS t) "q" GROUP BY "t1"."user_id") AS t) "q"
GROUP BY types GROUP BY types
ORDER BY types; ORDER BY types;
types | sumofeventtype types | sumofeventtype
-------+---------------- -------+----------------
0 | 55 0 | 43
1 | 38 1 | 42
2 | 70 2 | 28
3 | 58 3 | 25
(4 rows) (4 rows)
-- again same query but with only two top level empty queries (i.e., no group bys) -- again same query but with only two top level empty queries (i.e., no group bys)
@ -482,36 +480,36 @@ FROM
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (10, 11, 12, 13, 14, 15)) event_type IN (1, 2))
UNION UNION
(SELECT (SELECT
"events"."user_id", "events"."time", 1 AS event "events"."user_id", "events"."time", 1 AS event
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (15, 16, 17, 18, 19) ) event_type IN (2, 3) )
UNION UNION
(SELECT (SELECT
"events"."user_id", "events"."time", 2 AS event "events"."user_id", "events"."time", 2 AS event
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (20, 21, 22, 23, 24, 25) ) event_type IN (4, 5) )
UNION UNION
(SELECT (SELECT
"events"."user_id", "events"."time", 3 AS event "events"."user_id", "events"."time", 3 AS event
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (26, 27, 28, 29, 30, 13))) t1 event_type IN (6, 1))) t1
) AS t) "q" ) AS t) "q"
ORDER BY 1 ORDER BY 1
LIMIT 5; LIMIT 5;
user_id user_id
--------- ---------
0 1
0 1
0 1
1 1
1 1
(5 rows) (5 rows)
@ -528,37 +526,37 @@ FROM
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (10, 11, 12, 13, 14, 15)) event_type IN (1, 2))
UNION ALL UNION ALL
(SELECT (SELECT
"events"."user_id", "events"."time", 1 AS event "events"."user_id", "events"."time", 1 AS event
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (15, 16, 17, 18, 19) ) event_type IN (2, 3) )
UNION ALL UNION ALL
(SELECT (SELECT
"events"."user_id", "events"."time", 2 AS event "events"."user_id", "events"."time", 2 AS event
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (20, 21, 22, 23, 24, 25) ) event_type IN (4, 5) )
UNION ALL UNION ALL
(SELECT (SELECT
"events"."user_id", "events"."time", 3 AS event "events"."user_id", "events"."time", 3 AS event
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (26, 27, 28, 29, 30, 13))) t1 event_type IN (6, 1))) t1
GROUP BY "t1"."user_id") AS t) "q" GROUP BY "t1"."user_id") AS t) "q"
GROUP BY types GROUP BY types
ORDER BY types; ORDER BY types;
types | sumofeventtype types | sumofeventtype
-------+---------------- -------+----------------
0 | 55 0 | 43
1 | 38 1 | 42
2 | 70 2 | 28
3 | 58 3 | 25
(4 rows) (4 rows)
-- some UNION ALL queries that are going to be pulled up -- some UNION ALL queries that are going to be pulled up
@ -572,7 +570,7 @@ FROM
) b; ) b;
count count
------- -------
20002 202
(1 row) (1 row)
-- some UNION ALL queries that are going to be pulled up with reference table -- some UNION ALL queries that are going to be pulled up with reference table
@ -599,11 +597,11 @@ ORDER BY 1 DESC
LIMIT 5; LIMIT 5;
user_id user_id
--------- ---------
100 6
100 6
100 6
100 6
100 6
(5 rows) (5 rows)
-- similar query with multiple target list entries -- similar query with multiple target list entries
@ -619,11 +617,11 @@ ORDER BY 1 DESC, 2 DESC
LIMIT 5; LIMIT 5;
user_id | value_3 user_id | value_3
---------+--------- ---------+---------
100 | 999 6 | 5
100 | 997 6 | 5
100 | 991 6 | 5
100 | 989 6 | 5
100 | 988 6 | 4
(5 rows) (5 rows)
-- similar query group by inside the subqueries -- similar query group by inside the subqueries
@ -639,11 +637,11 @@ ORDER BY 2 DESC, 1 DESC
LIMIT 5; LIMIT 5;
user_id | value_3_sum user_id | value_3_sum
---------+------------- ---------+-------------
10 | 64060 4 | 65
10 | 64060 4 | 65
62 | 62445 5 | 64
62 | 62445 5 | 64
26 | 60536 2 | 54
(5 rows) (5 rows)
-- similar query top level group by -- similar query top level group by
@ -658,13 +656,13 @@ FROM
GROUP BY 1 GROUP BY 1
ORDER BY 2 DESC, 1 DESC ORDER BY 2 DESC, 1 DESC
LIMIT 5; LIMIT 5;
user_id | sum user_id | sum
---------+-------- ---------+-----
23 | 123923 2 | 119
25 | 118087 4 | 111
69 | 115828 3 | 100
26 | 114705 5 | 85
3 | 113915 1 | 53
(5 rows) (5 rows)
-- a long set operation list -- a long set operation list
@ -672,27 +670,27 @@ SELECT
user_id, value_3 user_id, value_3
FROM FROM
( (
(SELECT value_3, user_id FROM events_table where event_type IN (1, 2, 3, 4, 5)) (SELECT value_3, user_id FROM events_table where event_type IN (1, 2))
UNION ALL UNION ALL
(SELECT value_3, user_id FROM events_table where event_type IN (6, 7, 8, 9, 10)) (SELECT value_3, user_id FROM events_table where event_type IN (2, 3))
UNION ALL UNION ALL
(SELECT value_3, user_id FROM events_table where event_type IN (11, 12, 13, 14, 15)) (SELECT value_3, user_id FROM events_table where event_type IN (3, 4))
UNION ALL UNION ALL
(SELECT value_3, user_id FROM events_table where event_type IN (16, 17, 18, 19, 20)) (SELECT value_3, user_id FROM events_table where event_type IN (4, 5))
UNION ALL UNION ALL
(SELECT value_3, user_id FROM events_table where event_type IN (21, 22, 23, 24, 25)) (SELECT value_3, user_id FROM events_table where event_type IN (5, 6))
UNION ALL UNION ALL
(SELECT value_3, user_id FROM events_table where event_type IN (26, 27, 28, 29, 30)) (SELECT value_3, user_id FROM events_table where event_type IN (1, 6))
) b ) b
ORDER BY 1 DESC, 2 DESC ORDER BY 1 DESC, 2 DESC
LIMIT 5; LIMIT 5;
user_id | value_3 user_id | value_3
---------+--------- ---------+---------
100 | 951 6 | 5
99 | 558 6 | 5
99 | 14 6 | 3
98 | 987 6 | 3
98 | 577 6 | 3
(5 rows) (5 rows)
-- no partition key on the top -- no partition key on the top
@ -700,28 +698,28 @@ SELECT
max(value_3) max(value_3)
FROM FROM
( (
(SELECT value_3, user_id FROM events_table where event_type IN (1, 2, 3, 4, 5)) (SELECT value_3, user_id FROM events_table where event_type IN (1, 2))
UNION ALL UNION ALL
(SELECT value_3, user_id FROM events_table where event_type IN (6, 7, 8, 9, 10)) (SELECT value_3, user_id FROM events_table where event_type IN (2, 3))
UNION ALL UNION ALL
(SELECT value_3, user_id FROM events_table where event_type IN (11, 12, 13, 14, 15)) (SELECT value_3, user_id FROM events_table where event_type IN (3, 4))
UNION ALL UNION ALL
(SELECT value_3, user_id FROM events_table where event_type IN (16, 17, 18, 19, 20)) (SELECT value_3, user_id FROM events_table where event_type IN (4, 5))
UNION ALL UNION ALL
(SELECT value_3, user_id FROM events_table where event_type IN (21, 22, 23, 24, 25)) (SELECT value_3, user_id FROM events_table where event_type IN (5, 6))
UNION ALL UNION ALL
(SELECT value_3, user_id FROM events_table where event_type IN (26, 27, 28, 29, 30)) (SELECT value_3, user_id FROM events_table where event_type IN (1, 6))
) b ) b
GROUP BY user_id GROUP BY user_id
ORDER BY 1 DESC ORDER BY 1 DESC
LIMIT 5; LIMIT 5;
max max
----- -----
997 5
997 5
996 5
995 5
995 4
(5 rows) (5 rows)
-- now lets also have some unsupported queries -- now lets also have some unsupported queries
@ -738,15 +736,15 @@ DETAIL: Each leaf query of the UNION should return partition key at the same po
-- partition key is not selected -- partition key is not selected
SELECT sum(counter) SELECT sum(counter)
FROM ( FROM (
SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 20 GROUP BY user_id HAVING sum(value_2) > 500 SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 1 GROUP BY user_id HAVING sum(value_2) > 25
UNION UNION
SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 40 and value_1 < 60 GROUP BY user_id HAVING sum(value_2) > 500 SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 2 and value_1 < 3 GROUP BY user_id HAVING sum(value_2) > 25
UNION UNION
SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 60 and value_1 < 80 GROUP BY user_id HAVING sum(value_2) > 500 SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 3 and value_1 < 4 GROUP BY user_id HAVING sum(value_2) > 25
UNION UNION
SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 80 and value_1 < 100 GROUP BY user_id HAVING sum(value_2) > 500 SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 4 and value_1 < 5 GROUP BY user_id HAVING sum(value_2) > 25
UNION UNION
SELECT 2 * user_id, sum(value_2) AS counter FROM users_table where value_1 < 100 and value_1 < 120 GROUP BY user_id HAVING sum(value_2) > 500 SELECT 2 * user_id, sum(value_2) AS counter FROM users_table where value_1 < 5 and value_1 < 6 GROUP BY user_id HAVING sum(value_2) > 25
) user_id ) user_id
GROUP BY user_id ORDER BY 1 DESC LIMIT 5; GROUP BY user_id ORDER BY 1 DESC LIMIT 5;
ERROR: cannot pushdown the subquery since all leaves of the UNION does not include partition key at the same position ERROR: cannot pushdown the subquery since all leaves of the UNION does not include partition key at the same position
@ -877,17 +875,17 @@ SELECT
user_id, value_3 user_id, value_3
FROM FROM
( (
(SELECT value_3, user_id FROM events_table where event_type IN (1, 2, 3, 4, 5)) (SELECT value_3, user_id FROM events_table where event_type IN (1, 2))
UNION ALL UNION ALL
(SELECT value_3, user_id FROM events_table where event_type IN (6, 7, 8, 9, 10)) (SELECT value_3, user_id FROM events_table where event_type IN (2, 3))
UNION ALL UNION ALL
(SELECT value_3, user_id FROM events_table where event_type IN (11, 12, 13, 14, 15)) (SELECT value_3, user_id FROM events_table where event_type IN (3, 4))
UNION ALL UNION ALL
(SELECT value_3, user_id FROM events_table where event_type IN (16, 17, 18, 19, 20)) (SELECT value_3, user_id FROM events_table where event_type IN (4, 5))
UNION ALL UNION ALL
(SELECT value_3, user_id FROM events_table where event_type IN (21, 22, 23, 24, 25)) (SELECT value_3, user_id FROM events_table where event_type IN (5, 6))
UNION ALL UNION ALL
(SELECT value_3, value_2 FROM events_table where event_type IN (26, 27, 28, 29, 30)) (SELECT value_3, value_2 FROM events_table where event_type IN (1, 6))
) b ) b
ORDER BY 1 DESC, 2 DESC ORDER BY 1 DESC, 2 DESC
LIMIT 5; LIMIT 5;
@ -931,15 +929,15 @@ SELECT
user_id, value_3 user_id, value_3
FROM FROM
( (
(SELECT value_3, user_id FROM events_table where event_type IN (1, 2, 3, 4, 5)) (SELECT value_3, user_id FROM events_table where event_type IN (1, 2))
UNION ALL UNION ALL
(SELECT value_3, user_id FROM events_table where event_type IN (6, 7, 8, 9, 10)) (SELECT value_3, user_id FROM events_table where event_type IN (2, 3))
UNION ALL UNION ALL
(SELECT value_3, user_id FROM events_table where event_type IN (11, 12, 13, 14, 15)) (SELECT value_3, user_id FROM events_table where event_type IN (3, 4))
UNION ALL UNION ALL
(SELECT value_3, user_id FROM events_table where event_type IN (16, 17, 18, 19, 20)) (SELECT value_3, user_id FROM events_table where event_type IN (4, 5))
UNION ALL UNION ALL
(SELECT value_3, user_id FROM events_table where event_type IN (21, 22, 23, 24, 25)) (SELECT value_3, user_id FROM events_table where event_type IN (5, 6))
UNION ALL UNION ALL
(SELECT 1, 2) (SELECT 1, 2)
) b ) b
@ -962,7 +960,7 @@ FROM
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (10, 11, 12, 13, 14, 15)) events_subquery_1) event_type IN (1, 2)) events_subquery_1)
UNION UNION
(SELECT * (SELECT *
FROM FROM
@ -971,7 +969,7 @@ FROM
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (15, 16, 17, 18, 19) ) events_subquery_2) event_type IN (2, 3) ) events_subquery_2)
UNION UNION
(SELECT * (SELECT *
FROM FROM
@ -980,7 +978,7 @@ FROM
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (20, 21, 22, 23, 24, 25) ) events_subquery_3) event_type IN (4, 5) ) events_subquery_3)
UNION UNION
(SELECT * (SELECT *
FROM FROM

View File

@ -25,16 +25,16 @@ LIMIT
10; 10;
user_id | time | rnk user_id | time | rnk
---------+---------------------------------+----- ---------+---------------------------------+-----
23 | Fri Jan 10 20:11:40.439606 2014 | 127 2 | Wed Nov 22 20:16:16.614779 2017 | 24
23 | Fri Jan 10 20:15:35.594738 2014 | 126 2 | Wed Nov 22 22:06:12.107108 2017 | 23
23 | Fri Jan 10 23:14:59.348548 2014 | 125 2 | Wed Nov 22 22:23:25.40611 2017 | 22
23 | Fri Jan 10 23:38:35.800498 2014 | 124 3 | Wed Nov 22 18:36:16.372893 2017 | 21
25 | Fri Jan 10 21:50:55.465393 2014 | 123 2 | Wed Nov 22 22:50:33.855696 2017 | 21
23 | Sat Jan 11 00:40:59.383928 2014 | 123 3 | Wed Nov 22 20:23:46.906523 2017 | 20
25 | Fri Jan 10 22:43:09.881855 2014 | 122 2 | Wed Nov 22 22:56:47.673504 2017 | 20
23 | Sat Jan 11 00:42:46.148 2014 | 122 3 | Wed Nov 22 21:12:24.542921 2017 | 19
25 | Fri Jan 10 23:08:28.963923 2014 | 121 2 | Thu Nov 23 01:08:57.24208 2017 | 19
23 | Sat Jan 11 01:23:01.126017 2014 | 121 3 | Wed Nov 22 21:26:21.185134 2017 | 18
(10 rows) (10 rows)
-- the same test with different syntax -- the same test with different syntax
@ -53,16 +53,16 @@ LIMIT
10; 10;
user_id | time | rnk user_id | time | rnk
---------+---------------------------------+----- ---------+---------------------------------+-----
23 | Fri Jan 10 20:11:40.439606 2014 | 127 2 | Wed Nov 22 20:16:16.614779 2017 | 24
23 | Fri Jan 10 20:15:35.594738 2014 | 126 2 | Wed Nov 22 22:06:12.107108 2017 | 23
23 | Fri Jan 10 23:14:59.348548 2014 | 125 2 | Wed Nov 22 22:23:25.40611 2017 | 22
23 | Fri Jan 10 23:38:35.800498 2014 | 124 3 | Wed Nov 22 18:36:16.372893 2017 | 21
25 | Fri Jan 10 21:50:55.465393 2014 | 123 2 | Wed Nov 22 22:50:33.855696 2017 | 21
23 | Sat Jan 11 00:40:59.383928 2014 | 123 3 | Wed Nov 22 20:23:46.906523 2017 | 20
25 | Fri Jan 10 22:43:09.881855 2014 | 122 2 | Wed Nov 22 22:56:47.673504 2017 | 20
23 | Sat Jan 11 00:42:46.148 2014 | 122 3 | Wed Nov 22 21:12:24.542921 2017 | 19
25 | Fri Jan 10 23:08:28.963923 2014 | 121 2 | Thu Nov 23 01:08:57.24208 2017 | 19
23 | Sat Jan 11 01:23:01.126017 2014 | 121 3 | Wed Nov 22 21:26:21.185134 2017 | 18
(10 rows) (10 rows)
-- similar test with lag -- similar test with lag
@ -81,16 +81,16 @@ LIMIT
10; 10;
user_id | time | lag_event_type | row_no user_id | time | lag_event_type | row_no
---------+---------------------------------+----------------+-------- ---------+---------------------------------+----------------+--------
23 | Fri Jan 10 20:11:40.439606 2014 | 338 | 127 2 | Wed Nov 22 20:16:16.614779 2017 | 0 | 24
23 | Fri Jan 10 20:15:35.594738 2014 | 999 | 126 2 | Wed Nov 22 22:06:12.107108 2017 | 3 | 23
23 | Fri Jan 10 23:14:59.348548 2014 | 783 | 125 2 | Wed Nov 22 22:23:25.40611 2017 | 4 | 22
23 | Fri Jan 10 23:38:35.800498 2014 | 802 | 124 2 | Wed Nov 22 22:50:33.855696 2017 | 4 | 21
25 | Fri Jan 10 21:50:55.465393 2014 | 517 | 123 3 | Wed Nov 22 18:36:16.372893 2017 | 3 | 21
23 | Sat Jan 11 00:40:59.383928 2014 | 359 | 123 2 | Wed Nov 22 22:56:47.673504 2017 | 2 | 20
25 | Fri Jan 10 22:43:09.881855 2014 | 918 | 122 3 | Wed Nov 22 20:23:46.906523 2017 | 1 | 20
23 | Sat Jan 11 00:42:46.148 2014 | 68 | 122 2 | Thu Nov 23 01:08:57.24208 2017 | 3 | 19
25 | Fri Jan 10 23:08:28.963923 2014 | 757 | 121 3 | Wed Nov 22 21:12:24.542921 2017 | 1 | 19
23 | Sat Jan 11 01:23:01.126017 2014 | 251 | 121 3 | Wed Nov 22 21:26:21.185134 2017 | 3 | 18
(10 rows) (10 rows)
-- simple window function, partitioned and grouped by on the distribution key -- simple window function, partitioned and grouped by on the distribution key
@ -110,18 +110,18 @@ ORDER BY
2 DESC, 1 DESC, 3 DESC 2 DESC, 1 DESC, 3 DESC
LIMIT LIMIT
10; 10;
user_id | rnk | avg_val_2 user_id | rnk | avg_val_2
---------+-----+---------------------- ---------+-----+--------------------
98 | 12 | 647.5000000000000000 6 | 2 | 2.0000000000000000
95 | 12 | 428.5000000000000000 5 | 2 | 2.0909090909090909
94 | 12 | 608.6666666666666667 4 | 2 | 2.4000000000000000
92 | 12 | 724.0000000000000000 3 | 2 | 3.1666666666666667
91 | 12 | 549.0000000000000000 2 | 2 | 2.0000000000000000
90 | 12 | 525.1000000000000000 1 | 2 | 2.1428571428571429
89 | 12 | 531.0000000000000000 6 | 1 | 2.5000000000000000
87 | 12 | 740.0000000000000000 5 | 1 | 2.6666666666666667
84 | 12 | 487.7500000000000000 4 | 1 | 2.5000000000000000
83 | 12 | 629.5000000000000000 3 | 1 | 1.8000000000000000
(10 rows) (10 rows)
-- top level query has a group by on the result of the window function -- top level query has a group by on the result of the window function
@ -142,17 +142,15 @@ LIMIT
10; 10;
min | min | lag_event_type | count min | min | lag_event_type | count
-----+---------------------------------+----------------+------- -----+---------------------------------+----------------+-------
45 | Sat Jan 11 12:47:09.502744 2014 | 1000 | 2 1 | Thu Nov 23 11:09:38.074595 2017 | 6 | 1
18 | Fri Jan 10 20:15:35.594738 2014 | 999 | 9 2 | Wed Nov 22 19:00:10.396739 2017 | 5 | 7
1 | Sat Jan 11 21:08:41.737933 2014 | 998 | 10 1 | Wed Nov 22 18:49:42.327403 2017 | 4 | 21
0 | Sat Jan 11 16:32:40.662168 2014 | 997 | 9 1 | Wed Nov 22 18:36:16.372893 2017 | 3 | 21
3 | Fri Jan 10 23:30:18.011423 2014 | 996 | 13 1 | Wed Nov 22 19:07:03.846437 2017 | 2 | 17
17 | Sun Jan 12 03:54:06.464758 2014 | 995 | 9 1 | Wed Nov 22 19:03:01.772353 2017 | 1 | 23
23 | Tue Jan 14 22:04:23.44321 2014 | 994 | 7 1 | Wed Nov 22 20:16:16.614779 2017 | 0 | 5
7 | Sat Jan 11 04:59:48.119353 2014 | 993 | 9 1 | Thu Nov 23 14:00:13.20013 2017 | | 6
8 | Sat Jan 11 05:14:45.845071 2014 | 992 | 14 (8 rows)
0 | Sun Jan 12 03:24:01.449152 2014 | 991 | 10
(10 rows)
-- window functions should work along with joins as well -- window functions should work along with joins as well
SELECT * FROM SELECT * FROM
@ -163,23 +161,23 @@ SELECT * FROM
users_table, events_table users_table, events_table
WHERE WHERE
users_table.user_id = events_table.user_id and users_table.user_id = events_table.user_id and
event_type < 25 event_type < 4
WINDOW w1 AS (PARTITION BY users_table.user_id, events_table.event_type ORDER BY events_table.time) WINDOW w1 AS (PARTITION BY users_table.user_id, events_table.event_type ORDER BY events_table.time)
) as foo ) as foo
ORDER BY 3 DESC, 1 DESC, 2 DESC NULLS LAST ORDER BY 3 DESC, 1 DESC, 2 DESC NULLS LAST
LIMIT 10; LIMIT 10;
user_id | lag | rank user_id | lag | rank
---------+-----+------ ---------+-----+------
90 | 90 | 114 2 | 2 | 109
72 | 72 | 109 5 | 5 | 105
26 | 26 | 109 3 | 3 | 103
91 | 91 | 108 2 | 2 | 91
55 | 55 | 107 3 | 3 | 86
27 | 27 | 106 5 | 5 | 79
60 | 60 | 101 2 | 2 | 73
98 | 98 | 97 4 | 4 | 70
39 | 39 | 95 3 | 3 | 69
61 | 61 | 93 2 | 2 | 55
(10 rows) (10 rows)
-- two window functions in a single subquery should work fine as well -- two window functions in a single subquery should work fine as well
@ -191,7 +189,7 @@ SELECT * FROM
users_table, events_table users_table, events_table
WHERE WHERE
users_table.user_id = events_table.user_id and users_table.user_id = events_table.user_id and
event_type < 25 event_type < 4
WINDOW w1 AS (PARTITION BY users_table.user_id, events_table.event_type ORDER BY events_table.time), WINDOW w1 AS (PARTITION BY users_table.user_id, events_table.event_type ORDER BY events_table.time),
w2 AS (PARTITION BY users_table.user_id, (events_table.value_2 % 25) ORDER BY events_table.time) w2 AS (PARTITION BY users_table.user_id, (events_table.value_2 % 25) ORDER BY events_table.time)
) as foo ) as foo
@ -199,16 +197,16 @@ ORDER BY 3 DESC, 1 DESC, 2 DESC NULLS LAST
LIMIT 10; LIMIT 10;
user_id | lag | rank user_id | lag | rank
---------+-----+------ ---------+-----+------
73 | 73 | 112 2 | 2 | 73
73 | | 112 4 | 4 | 70
48 | 48 | 111 3 | 3 | 69
48 | | 111 2 | 2 | 55
43 | 43 | 105 5 | 5 | 53
43 | | 105 5 | | 53
77 | 77 | 104 3 | 3 | 52
77 | | 104 4 | 4 | 47
30 | 30 | 104 2 | 2 | 37
30 | | 104 3 | 3 | 35
(10 rows) (10 rows)
-- window functions should be fine within subquery joins -- window functions should be fine within subquery joins
@ -220,7 +218,7 @@ SELECT sub_1.user_id, max(lag_1), max(rank_1), max(rank_2) FROM
users_table, events_table users_table, events_table
WHERE WHERE
users_table.user_id = events_table.user_id and users_table.user_id = events_table.user_id and
event_type < 25 event_type < 4
WINDOW w1 AS (PARTITION BY users_table.user_id, events_table.event_type ORDER BY events_table.time), WINDOW w1 AS (PARTITION BY users_table.user_id, events_table.event_type ORDER BY events_table.time),
w2 AS (PARTITION BY users_table.user_id, (events_table.value_2 % 25) ORDER BY events_table.time) w2 AS (PARTITION BY users_table.user_id, (events_table.value_2 % 25) ORDER BY events_table.time)
) as sub_1 ) as sub_1
@ -232,7 +230,7 @@ JOIN
users_table, events_table users_table, events_table
WHERE WHERE
users_table.user_id = events_table.user_id and users_table.user_id = events_table.user_id and
event_type < 25 event_type < 4
WINDOW w1 AS (PARTITION BY users_table.user_id, events_table.value_2 ORDER BY events_table.time), WINDOW w1 AS (PARTITION BY users_table.user_id, events_table.value_2 ORDER BY events_table.time),
w2 AS (PARTITION BY users_table.user_id, (events_table.value_2 % 50) ORDER BY events_table.time) w2 AS (PARTITION BY users_table.user_id, (events_table.value_2 % 50) ORDER BY events_table.time)
) as sub_2 ) as sub_2
@ -243,17 +241,13 @@ JOIN
LIMIT 10; LIMIT 10;
user_id | max | max | max user_id | max | max | max
---------+-----+-----+----- ---------+-----+-----+-----
73 | 73 | 112 | 112 2 | 2 | 73 | 73
48 | 48 | 111 | 111 4 | 4 | 70 | 70
43 | 43 | 105 | 1 3 | 3 | 69 | 69
77 | 77 | 104 | 104 5 | 5 | 53 | 53
30 | 30 | 104 | 104 6 | 6 | 21 | 21
50 | 50 | 101 | 1 1 | 1 | 15 | 15
79 | 79 | 97 | 1 (6 rows)
49 | 49 | 96 | 96
44 | 44 | 93 | 1
13 | 13 | 87 | 1
(10 rows)
-- GROUP BYs and PARTITION BYs should work fine together -- GROUP BYs and PARTITION BYs should work fine together
SELECT SELECT
@ -269,23 +263,17 @@ FROM
WINDOW my_win AS (PARTITION BY user_id ORDER BY count(*) DESC) WINDOW my_win AS (PARTITION BY user_id ORDER BY count(*) DESC)
) as foo ) as foo
WHERE WHERE
my_rank > 5 my_rank > 1
GROUP BY GROUP BY
my_rank my_rank
ORDER BY ORDER BY
3 DESC, 1 DESC,2 DESC 3 DESC, 1 DESC,2 DESC
LIMIT LIMIT
10; 10;
avg | max | my_rank avg | max | my_rank
---------------------+--------------------------+--------- --------------------+--------------------------+---------
48.6250000000000000 | Tue Jan 21 00:00:00 2014 | 12 3.5000000000000000 | Wed Nov 22 00:00:00 2017 | 2
48.4786324786324786 | Tue Jan 21 00:00:00 2014 | 11 (1 row)
50.2083333333333333 | Tue Jan 21 00:00:00 2014 | 10
51.8247422680412371 | Tue Jan 21 00:00:00 2014 | 9
46.2061855670103093 | Mon Jan 20 00:00:00 2014 | 8
52.5945945945945946 | Mon Jan 20 00:00:00 2014 | 7
52.2589285714285714 | Tue Jan 21 00:00:00 2014 | 6
(7 rows)
-- aggregates in the PARTITION BY is also allows -- aggregates in the PARTITION BY is also allows
SELECT SELECT
@ -298,7 +286,7 @@ FROM
events_table events_table
GROUP BY GROUP BY
user_id, date_trunc('day', time) user_id, date_trunc('day', time)
WINDOW my_win AS (PARTITION BY user_id, avg(event_type%10)::int ORDER BY count(*) DESC) WINDOW my_win AS (PARTITION BY user_id, avg(event_type%3)::int ORDER BY count(*) DESC)
) as foo ) as foo
WHERE WHERE
my_rank > 0 my_rank > 0
@ -308,17 +296,11 @@ ORDER BY
3 DESC, 1 DESC,2 DESC 3 DESC, 1 DESC,2 DESC
LIMIT LIMIT
10; 10;
avg | max | my_rank avg | max | my_rank
---------------------+--------------------------+--------- --------------------+--------------------------+---------
22.0000000000000000 | Fri Jan 10 00:00:00 2014 | 8 3.7500000000000000 | Wed Nov 22 00:00:00 2017 | 2
60.4000000000000000 | Mon Jan 20 00:00:00 2014 | 7 3.3750000000000000 | Thu Nov 23 00:00:00 2017 | 1
55.5500000000000000 | Tue Jan 21 00:00:00 2014 | 6 (2 rows)
50.6142857142857143 | Tue Jan 21 00:00:00 2014 | 5
53.6697247706422018 | Tue Jan 21 00:00:00 2014 | 4
49.7604166666666667 | Tue Jan 21 00:00:00 2014 | 3
47.9569892473118280 | Tue Jan 21 00:00:00 2014 | 2
49.5859375000000000 | Tue Jan 21 00:00:00 2014 | 1
(8 rows)
-- GROUP BY should not necessarly be inclusive of partitioning -- GROUP BY should not necessarly be inclusive of partitioning
-- but this query doesn't make much sense -- but this query doesn't make much sense
@ -340,9 +322,9 @@ ORDER BY
2 DESC, 1 DESC 2 DESC, 1 DESC
LIMIT LIMIT
10; 10;
avg | my_rank avg | my_rank
---------------------+--------- --------------------+---------
50.0000000000000000 | 1 3.5000000000000000 | 1
(1 row) (1 row)
-- Using previously defined supported window function on distribution key -- Using previously defined supported window function on distribution key
@ -366,16 +348,16 @@ LIMIT
10; 10;
user_id | time | sum user_id | time | sum
---------+--------------------------+----- ---------+--------------------------+-----
0 | Fri Jan 10 00:00:00 2014 | 32 1 | Wed Nov 22 00:00:00 2017 | 1
0 | Sat Jan 11 00:00:00 2014 | 40 1 | Thu Nov 23 00:00:00 2017 | 7
0 | Sat Jan 11 00:00:00 2014 | 38 1 | Thu Nov 23 00:00:00 2017 | 6
0 | Sat Jan 11 00:00:00 2014 | 30 1 | Thu Nov 23 00:00:00 2017 | 5
0 | Sun Jan 12 00:00:00 2014 | 49 1 | Thu Nov 23 00:00:00 2017 | 4
0 | Sun Jan 12 00:00:00 2014 | 47 1 | Thu Nov 23 00:00:00 2017 | 3
0 | Sun Jan 12 00:00:00 2014 | 34 1 | Thu Nov 23 00:00:00 2017 | 2
0 | Sun Jan 12 00:00:00 2014 | 29 2 | Wed Nov 22 00:00:00 2017 | 17
0 | Sun Jan 12 00:00:00 2014 | 24 2 | Thu Nov 23 00:00:00 2017 | 18
0 | Sun Jan 12 00:00:00 2014 | 20 2 | Thu Nov 23 00:00:00 2017 | 16
(10 rows) (10 rows)
-- test with reference table partitioned on columns from both -- test with reference table partitioned on columns from both
@ -386,7 +368,7 @@ FROM
DISTINCT user_id, it_name, count(id) OVER (PARTITION BY user_id, id) DISTINCT user_id, it_name, count(id) OVER (PARTITION BY user_id, id)
FROM FROM
users_table, users_ref_test_table users_table, users_ref_test_table
WHERE users_table.value_2=users_ref_test_table.k_no WHERE users_table.value_2 + 40 = users_ref_test_table.k_no
) a ) a
ORDER BY ORDER BY
1, 2, 3 1, 2, 3
@ -394,27 +376,11 @@ LIMIT
20; 20;
user_id | it_name | count user_id | it_name | count
---------+---------+------- ---------+---------+-------
6 | User_4 | 1 2 | User_1 | 2
8 | User_4 | 1 3 | User_1 | 6
9 | User_3 | 1 4 | User_1 | 2
11 | User_3 | 1 5 | User_1 | 3
15 | User_6 | 1 (4 rows)
16 | User_2 | 1
16 | User_3 | 1
17 | User_3 | 1
17 | User_4 | 1
20 | User_3 | 1
21 | User_6 | 1
22 | User_1 | 1
24 | User_4 | 1
26 | User_2 | 2
27 | User_4 | 1
28 | User_2 | 1
28 | User_3 | 1
31 | User_1 | 1
31 | User_4 | 1
34 | User_3 | 1
(20 rows)
-- Group by has more columns than partition by -- Group by has more columns than partition by
SELECT * FROM ( SELECT * FROM (
@ -429,19 +395,15 @@ ORDER BY
2 DESC, 1 2 DESC, 1
LIMIT LIMIT
10; 10;
user_id | sum user_id | sum
---------+------- ---------+-----
46 | 63666 3 | 44
23 | 62524 5 | 43
56 | 61350 4 | 41
12 | 61317 2 | 38
48 | 60144 1 | 16
71 | 60095 6 | 16
45 | 59904 (6 rows)
94 | 59773
3 | 59141
93 | 58365
(10 rows)
SELECT user_id, max(sum) FROM ( SELECT user_id, max(sum) FROM (
SELECT SELECT
@ -455,19 +417,15 @@ GROUP BY user_id ORDER BY
2 DESC,1 2 DESC,1
LIMIT LIMIT
10; 10;
user_id | max user_id | max
---------+------ ---------+-----
1 | 2469 3 | 15
87 | 2089 4 | 13
81 | 1952 2 | 10
23 | 1891 5 | 10
58 | 1888 6 | 7
97 | 1868 1 | 6
94 | 1849 (6 rows)
17 | 1844
22 | 1844
43 | 1843
(10 rows)
-- Window functions with HAVING clause -- Window functions with HAVING clause
SELECT * FROM ( SELECT * FROM (
@ -484,16 +442,16 @@ LIMIT
10; 10;
user_id | rank user_id | rank
---------+------ ---------+------
12 | 10 5 | 6
55 | 10 2 | 5
12 | 9 4 | 5
25 | 9 5 | 5
46 | 9 2 | 4
55 | 9 3 | 4
12 | 8 4 | 4
25 | 8 5 | 4
36 | 8 6 | 4
46 | 8 2 | 3
(10 rows) (10 rows)
-- Window function in View works -- Window function in View works
@ -506,16 +464,16 @@ LIMIT
10; 10;
user_id | rank user_id | rank
---------+------ ---------+------
12 | 10 5 | 6
55 | 10 2 | 5
12 | 9 4 | 5
25 | 9 5 | 5
46 | 9 2 | 4
55 | 9 3 | 4
12 | 8 4 | 4
25 | 8 5 | 4
36 | 8 6 | 4
46 | 8 2 | 3
(10 rows) (10 rows)
-- Window functions with UNION/UNION ALL works -- Window functions with UNION/UNION ALL works
@ -523,28 +481,28 @@ SELECT
max(avg) max(avg)
FROM FROM
( (
(SELECT avg(value_3) over (partition by user_id), user_id FROM events_table where event_type IN (1, 2, 3, 4, 5)) (SELECT avg(value_3) over (partition by user_id), user_id FROM events_table where event_type IN (1, 2))
UNION ALL UNION ALL
(SELECT avg(value_3) over (partition by user_id), user_id FROM events_table where event_type IN (6, 7, 8, 9, 10)) (SELECT avg(value_3) over (partition by user_id), user_id FROM events_table where event_type IN (2, 3))
UNION ALL UNION ALL
(SELECT avg(value_3) over (partition by user_id), user_id FROM events_table where event_type IN (11, 12, 13, 14, 15)) (SELECT avg(value_3) over (partition by user_id), user_id FROM events_table where event_type IN (3, 4))
UNION ALL UNION ALL
(SELECT avg(value_3) over (partition by user_id), user_id FROM events_table where event_type IN (16, 17, 18, 19, 20)) (SELECT avg(value_3) over (partition by user_id), user_id FROM events_table where event_type IN (4, 5))
UNION ALL UNION ALL
(SELECT avg(value_3) over (partition by user_id), user_id FROM events_table where event_type IN (21, 22, 23, 24, 25)) (SELECT avg(value_3) over (partition by user_id), user_id FROM events_table where event_type IN (5, 6))
UNION ALL UNION ALL
(SELECT avg(value_3) over (partition by user_id), user_id FROM events_table where event_type IN (26, 27, 28, 29, 30)) (SELECT avg(value_3) over (partition by user_id), user_id FROM events_table where event_type IN (1, 6))
) b ) b
GROUP BY user_id GROUP BY user_id
ORDER BY 1 DESC ORDER BY 1 DESC
LIMIT 5; LIMIT 5;
max max
----- ------
996 5
995 3.5
987 3.25
978 3
974 3
(5 rows) (5 rows)
SELECT * SELECT *
@ -580,13 +538,13 @@ FROM (
user_id)) AS ftop user_id)) AS ftop
ORDER BY 2 DESC, 1 DESC ORDER BY 2 DESC, 1 DESC
LIMIT 5; LIMIT 5;
user_id | sum user_id | sum
---------+-------- ---------+-----
23 | 126017 2 | 107
45 | 117323 3 | 101
25 | 116595 5 | 94
17 | 116520 4 | 91
90 | 115843 1 | 62
(5 rows) (5 rows)
-- Subquery in where with window function -- Subquery in where with window function
@ -595,7 +553,7 @@ SELECT
FROM FROM
users_table users_table
WHERE WHERE
value_2 > 545 AND value_2 > 1 AND
value_2 < ALL ( value_2 < ALL (
SELECT SELECT
avg(value_3) OVER (PARTITION BY user_id) avg(value_3) OVER (PARTITION BY user_id)
@ -612,9 +570,9 @@ LIMIT
3; 3;
user_id user_id
--------- ---------
69 4
52 3
12 2
(3 rows) (3 rows)
-- Some more nested queries -- Some more nested queries
@ -632,30 +590,30 @@ FROM (
GROUP BY GROUP BY
user_id, rank user_id, rank
ORDER BY ORDER BY
difference DESC, rank DESC difference DESC, rank DESC, user_id
LIMIT 20; LIMIT 20;
user_id | rank | difference | distinct_users user_id | rank | difference | distinct_users
---------+------+------------+---------------- ---------+------+------------+----------------
2 | 101 | 3696 | 2 4 | 12 | 306 | 9
73 | 98 | 3526 | 2 5 | 12 | 136 | 8
73 | 103 | 3522 | 3 3 | 1 | 84 | 6
97 | 73 | 3440 | 2 5 | 20 | 70 | 5
43 | 92 | 3418 | 2 3 | 11 | 55 | 5
57 | 75 | 3286 | 2 2 | 11 | 44 | 4
66 | 64 | 3249 | 3 2 | 3 | 40 | 5
42 | 97 | 3218 | 2 5 | 7 | 30 | 5
19 | 101 | 3110 | 2 2 | 8 | 24 | 3
91 | 94 | 3064 | 2 4 | 21 | 21 | 3
71 | 100 | 3026 | 2 2 | 15 | 21 | 3
59 | 69 | 3016 | 2 5 | 4 | 21 | 3
46 | 83 | 2858 | 2 6 | 9 | 20 | 2
16 | 86 | 2848 | 2 4 | 3 | 15 | 5
23 | 63 | 2734 | 2 3 | 16 | 14 | 2
62 | 96 | 2668 | 2 4 | 8 | 9 | 3
81 | 84 | 2666 | 2 1 | 1 | 9 | 3
7 | 74 | 2648 | 2 5 | 1 | 9 | 3
27 | 97 | 2640 | 2 6 | 7 | 8 | 2
55 | 76 | 2630 | 2 1 | 4 | 8 | 2
(20 rows) (20 rows)
SELECT * FROM ( SELECT * FROM (
@ -681,21 +639,17 @@ WHERE
f3.user_id=f2.user_id f3.user_id=f2.user_id
) a ) a
ORDER BY ORDER BY
abs DESC abs DESC, user_id
LIMIT 10; LIMIT 10;
user_id | abs user_id | abs
---------+------- ---------+-----
64 | 10669 6 | 2
74 | 10037 1 | 1
26 | 9571 2 | 0
76 | 9376 3 | 0
81 | 8330 4 | 0
16 | 7746 5 | 0
9 | 7100 (6 rows)
98 | 6922
94 | 6895
93 | 6653
(10 rows)
-- Partition by with aggregate functions. This query does not make much sense since the -- Partition by with aggregate functions. This query does not make much sense since the
-- result of aggregate function will be the same for every row in a partition and it is -- result of aggregate function will be the same for every row in a partition and it is
@ -714,11 +668,11 @@ LIMIT
5; 5;
user_id | count user_id | count
---------+------- ---------+-------
100 | 1 6 | 1
99 | 1 5 | 1
98 | 1 4 | 1
97 | 1 3 | 1
96 | 1 2 | 1
(5 rows) (5 rows)
EXPLAIN (COSTS FALSE, VERBOSE TRUE) EXPLAIN (COSTS FALSE, VERBOSE TRUE)
@ -755,8 +709,8 @@ EXPLAIN (COSTS FALSE, VERBOSE TRUE)
user_id)) AS ftop user_id)) AS ftop
ORDER BY 2 DESC, 1 DESC ORDER BY 2 DESC, 1 DESC
LIMIT 5; LIMIT 5;
QUERY PLAN QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------------------------------------------------
Limit Limit
Output: remote_scan.user_id, remote_scan.sum Output: remote_scan.user_id, remote_scan.sum
-> Sort -> Sort
@ -786,12 +740,18 @@ EXPLAIN (COSTS FALSE, VERBOSE TRUE)
-> Append -> Append
-> WindowAgg -> WindowAgg
Output: users_table.user_id, sum(users_table.value_2) OVER (?) Output: users_table.user_id, sum(users_table.value_2) OVER (?)
-> Index Scan using is_index1_1400000 on public.users_table_1400000 users_table -> Sort
Output: users_table.user_id, users_table."time", users_table.value_1, users_table.value_2, users_table.value_3, users_table.value_4 Output: users_table.user_id, users_table.value_2
Sort Key: users_table.user_id
-> Seq Scan on public.users_table_1400000 users_table
Output: users_table.user_id, users_table.value_2
-> WindowAgg -> WindowAgg
Output: events_table.user_id, sum(events_table.value_2) OVER (?) Output: events_table.user_id, sum(events_table.value_2) OVER (?)
-> Index Scan using is_index2_1400004 on public.events_table_1400004 events_table -> Sort
Output: events_table.user_id, events_table."time", events_table.event_type, events_table.value_2, events_table.value_3, events_table.value_4 Output: events_table.user_id, events_table.value_2
Sort Key: events_table.user_id
-> Seq Scan on public.events_table_1400004 events_table
Output: events_table.user_id, events_table.value_2
-> HashAggregate -> HashAggregate
Output: users_table_1.user_id, sum((sum(users_table_1.value_2) OVER (?))) Output: users_table_1.user_id, sum((sum(users_table_1.value_2) OVER (?)))
Group Key: users_table_1.user_id Group Key: users_table_1.user_id
@ -801,13 +761,19 @@ EXPLAIN (COSTS FALSE, VERBOSE TRUE)
-> Append -> Append
-> WindowAgg -> WindowAgg
Output: users_table_1.user_id, sum(users_table_1.value_2) OVER (?) Output: users_table_1.user_id, sum(users_table_1.value_2) OVER (?)
-> Index Scan using is_index1_1400000 on public.users_table_1400000 users_table_1 -> Sort
Output: users_table_1.user_id, users_table_1."time", users_table_1.value_1, users_table_1.value_2, users_table_1.value_3, users_table_1.value_4 Output: users_table_1.user_id, users_table_1.value_2
Sort Key: users_table_1.user_id
-> Seq Scan on public.users_table_1400000 users_table_1
Output: users_table_1.user_id, users_table_1.value_2
-> WindowAgg -> WindowAgg
Output: events_table_1.user_id, sum(events_table_1.value_2) OVER (?) Output: events_table_1.user_id, sum(events_table_1.value_2) OVER (?)
-> Index Scan using is_index2_1400004 on public.events_table_1400004 events_table_1 -> Sort
Output: events_table_1.user_id, events_table_1."time", events_table_1.event_type, events_table_1.value_2, events_table_1.value_3, events_table_1.value_4 Output: events_table_1.user_id, events_table_1.value_2
(50 rows) Sort Key: events_table_1.user_id
-> Seq Scan on public.events_table_1400004 events_table_1
Output: events_table_1.user_id, events_table_1.value_2
(62 rows)
-- lets have some queries that Citus shouldn't push down -- lets have some queries that Citus shouldn't push down
SELECT SELECT
@ -871,7 +837,7 @@ SELECT * FROM
users_table, events_table users_table, events_table
WHERE WHERE
users_table.user_id = events_table.user_id and users_table.user_id = events_table.user_id and
event_type < 25 event_type < 4
WINDOW w1 AS (PARTITION BY users_table.user_id, events_table.event_type ORDER BY events_table.time), WINDOW w1 AS (PARTITION BY users_table.user_id, events_table.event_type ORDER BY events_table.time),
w2 AS (PARTITION BY users_table.user_id+1, (events_table.value_2 % 25) ORDER BY events_table.time) w2 AS (PARTITION BY users_table.user_id+1, (events_table.value_2 % 25) ORDER BY events_table.time)
) as foo ) as foo
@ -888,7 +854,7 @@ SELECT * FROM
users_table, events_table users_table, events_table
WHERE WHERE
users_table.user_id = events_table.user_id and users_table.user_id = events_table.user_id and
event_type < 25 event_type < 4
WINDOW w1 AS (PARTITION BY users_table.user_id, events_table.event_type ORDER BY events_table.time), WINDOW w1 AS (PARTITION BY users_table.user_id, events_table.event_type ORDER BY events_table.time),
w2 AS (ORDER BY events_table.time) w2 AS (ORDER BY events_table.time)
) as foo ) as foo
@ -912,7 +878,7 @@ FROM
WINDOW my_win AS (ORDER BY avg(event_type)) WINDOW my_win AS (ORDER BY avg(event_type))
) as foo ) as foo
WHERE WHERE
my_rank > 125 my_rank > 1
ORDER BY ORDER BY
3 DESC, 1 DESC,2 DESC 3 DESC, 1 DESC,2 DESC
LIMIT LIMIT
@ -933,7 +899,7 @@ FROM
WINDOW my_win AS (PARTITION BY date_trunc('day', time) ORDER BY avg(event_type)) WINDOW my_win AS (PARTITION BY date_trunc('day', time) ORDER BY avg(event_type))
) as foo ) as foo
WHERE WHERE
my_rank > 125 my_rank > 1
ORDER BY ORDER BY
3 DESC, 1 DESC,2 DESC 3 DESC, 1 DESC,2 DESC
LIMIT LIMIT
@ -993,17 +959,17 @@ SELECT
max(avg) max(avg)
FROM FROM
( (
(SELECT avg(value_3) over (partition by user_id), user_id FROM events_table where event_type IN (1, 2, 3, 4, 5)) (SELECT avg(value_3) over (partition by user_id), user_id FROM events_table where event_type IN (1, 2))
UNION ALL UNION ALL
(SELECT avg(value_3) over (partition by user_id), user_id FROM events_table where event_type IN (6, 7, 8, 9, 10)) (SELECT avg(value_3) over (partition by user_id), user_id FROM events_table where event_type IN (2, 3))
UNION ALL UNION ALL
(SELECT avg(value_3) over (partition by user_id), user_id FROM events_table where event_type IN (11, 12, 13, 14, 15)) (SELECT avg(value_3) over (partition by user_id), user_id FROM events_table where event_type IN (3, 4))
UNION ALL UNION ALL
(SELECT avg(value_3) over (partition by user_id), user_id FROM events_table where event_type IN (16, 17, 18, 19, 20)) (SELECT avg(value_3) over (partition by user_id), user_id FROM events_table where event_type IN (4, 5))
UNION ALL UNION ALL
(SELECT avg(value_3) over (partition by user_id), user_id FROM events_table where event_type IN (21, 22, 23, 24, 25)) (SELECT avg(value_3) over (partition by user_id), user_id FROM events_table where event_type IN (5, 6))
UNION ALL UNION ALL
(SELECT avg(value_3) over (partition by event_type), user_id FROM events_table where event_type IN (26, 27, 28, 29, 30)) (SELECT avg(value_3) over (partition by event_type), user_id FROM events_table where event_type IN (1, 6))
) b ) b
GROUP BY user_id GROUP BY user_id
ORDER BY 1 DESC ORDER BY 1 DESC

View File

@ -302,41 +302,30 @@ DROP VIEW priority_orders;
CREATE VIEW recent_users AS CREATE VIEW recent_users AS
SELECT user_id, max(time) as lastseen FROM users_table SELECT user_id, max(time) as lastseen FROM users_table
GROUP BY user_id GROUP BY user_id
HAVING max(time) > '2014-01-21 05:45:49.978738'::timestamp order by 2 DESC; HAVING max(time) > '2017-11-23 16:20:33.264457'::timestamp order by 2 DESC;
SELECT * FROM recent_users; SELECT * FROM recent_users;
user_id | lastseen user_id | lastseen
---------+--------------------------------- ---------+---------------------------------
87 | Tue Jan 21 05:53:51.866813 2014 1 | Thu Nov 23 17:30:34.635085 2017
50 | Tue Jan 21 05:53:44.251016 2014 5 | Thu Nov 23 16:48:32.08896 2017
74 | Tue Jan 21 05:54:04.837808 2014 3 | Thu Nov 23 17:18:51.048758 2017
6 | Tue Jan 21 05:57:47.118755 2014 (3 rows)
71 | Tue Jan 21 05:55:52.018461 2014
39 | Tue Jan 21 05:55:18.875997 2014
66 | Tue Jan 21 05:51:31.681997 2014
100 | Tue Jan 21 05:49:04.953009 2014
46 | Tue Jan 21 05:49:00.229807 2014
86 | Tue Jan 21 05:48:54.381334 2014
13 | Tue Jan 21 05:48:45.418146 2014
90 | Tue Jan 21 05:48:25.027491 2014
58 | Tue Jan 21 05:47:30.418553 2014
44 | Tue Jan 21 05:47:01.104523 2014
(14 rows)
-- create a view for recent_events -- create a view for recent_events
CREATE VIEW recent_events AS CREATE VIEW recent_events AS
SELECT user_id, time FROM events_table SELECT user_id, time FROM events_table
WHERE time > '2014-01-20 01:45:49.978738'::timestamp; WHERE time > '2017-11-23 16:20:33.264457'::timestamp;
SELECT count(*) FROM recent_events; SELECT count(*) FROM recent_events;
count count
------- -------
1105 6
(1 row) (1 row)
-- count number of events of recent_users -- count number of events of recent_users
SELECT count(*) FROM recent_users ru JOIN events_table et ON (ru.user_id = et.user_id); SELECT count(*) FROM recent_users ru JOIN events_table et ON (ru.user_id = et.user_id);
count count
------- -------
1336 50
(1 row) (1 row)
-- count number of events of per recent users order by count -- count number of events of per recent users order by count
@ -348,21 +337,10 @@ SELECT ru.user_id, count(*)
ORDER BY 2 DESC, 1; ORDER BY 2 DESC, 1;
user_id | count user_id | count
---------+------- ---------+-------
13 | 118 3 | 21
44 | 109 1 | 15
90 | 109 5 | 14
87 | 105 (3 rows)
46 | 103
86 | 100
66 | 98
39 | 96
71 | 95
74 | 93
6 | 89
58 | 87
50 | 79
100 | 55
(14 rows)
-- the same query with a left join however, it would still generate the same result -- the same query with a left join however, it would still generate the same result
SELECT ru.user_id, count(*) SELECT ru.user_id, count(*)
@ -373,21 +351,10 @@ SELECT ru.user_id, count(*)
ORDER BY 2 DESC, 1; ORDER BY 2 DESC, 1;
user_id | count user_id | count
---------+------- ---------+-------
13 | 118 3 | 21
44 | 109 1 | 15
90 | 109 5 | 14
87 | 105 (3 rows)
46 | 103
86 | 100
66 | 98
39 | 96
71 | 95
74 | 93
6 | 89
58 | 87
50 | 79
100 | 55
(14 rows)
-- query wrapped inside a subquery, it needs another top level order by -- query wrapped inside a subquery, it needs another top level order by
SELECT * FROM SELECT * FROM
@ -400,21 +367,10 @@ SELECT * FROM
ORDER BY 2 DESC, 1; ORDER BY 2 DESC, 1;
user_id | count user_id | count
---------+------- ---------+-------
13 | 118 3 | 21
44 | 109 1 | 15
90 | 109 5 | 14
87 | 105 (3 rows)
46 | 103
86 | 100
66 | 98
39 | 96
71 | 95
74 | 93
6 | 89
58 | 87
50 | 79
100 | 55
(14 rows)
-- non-partition key joins are not supported inside subquery -- non-partition key joins are not supported inside subquery
SELECT * FROM SELECT * FROM
@ -432,21 +388,9 @@ DETAIL: Each relation should be joined with at least one another relation using
SELECT ru.user_id FROM recent_users ru JOIN recent_events re USING(user_id) GROUP BY ru.user_id ORDER BY ru.user_id; SELECT ru.user_id FROM recent_users ru JOIN recent_events re USING(user_id) GROUP BY ru.user_id ORDER BY ru.user_id;
user_id user_id
--------- ---------
6 1
13 3
39 (2 rows)
44
46
50
58
66
71
74
86
87
90
100
(14 rows)
-- outer join inside a subquery -- outer join inside a subquery
-- recent_events who are not done by recent users -- recent_events who are not done by recent users
@ -456,7 +400,7 @@ SELECT count(*) FROM (
WHERE recent_user IS NULL; WHERE recent_user IS NULL;
count count
------- -------
957 2
(1 row) (1 row)
-- same query with anti-join -- same query with anti-join
@ -465,83 +409,64 @@ SELECT count(*)
WHERE ru.user_id IS NULL; WHERE ru.user_id IS NULL;
count count
------- -------
957 2
(1 row) (1 row)
-- join between view and table -- join between view and table
-- users who has recent activity and they have an entry with value_1 is less than 15 -- users who has recent activity and they have an entry with value_1 is less than 3
SELECT ut.* FROM recent_users ru JOIN users_table ut USING (user_id) WHERE ut.value_1 < 15 ORDER BY 1,2; SELECT ut.* FROM recent_users ru JOIN users_table ut USING (user_id) WHERE ut.value_1 < 3 ORDER BY 1,2;
user_id | time | value_1 | value_2 | value_3 | value_4 user_id | time | value_1 | value_2 | value_3 | value_4
---------+---------------------------------+---------+---------+---------+--------- ---------+---------------------------------+---------+---------+---------+---------
6 | Mon Jan 13 05:30:08.289267 2014 | 12 | 140 | 618 | 1 | Thu Nov 23 09:26:42.145043 2017 | 1 | 3 | 3 |
6 | Thu Jan 16 15:17:16.779695 2014 | 6 | 978 | 430 | 3 | Wed Nov 22 18:43:51.450263 2017 | 1 | 1 | 4 |
6 | Sun Jan 19 06:09:39.900888 2014 | 3 | 908 | 688 | 3 | Wed Nov 22 20:43:31.008625 2017 | 1 | 3 | 2 |
13 | Sun Jan 19 22:09:26.256209 2014 | 2 | 755 | 584 | 3 | Thu Nov 23 00:15:45.610845 2017 | 1 | 1 | 4 |
39 | Wed Jan 15 05:46:51.48765 2014 | 14 | 657 | 137 | 3 | Thu Nov 23 03:23:24.702501 2017 | 1 | 2 | 5 |
39 | Sun Jan 19 11:26:47.45937 2014 | 12 | 118 | 165 | 3 | Thu Nov 23 06:20:05.854857 2017 | 1 | 4 | 2 |
44 | Wed Jan 15 14:23:52.532426 2014 | 8 | 204 | 735 | 3 | Thu Nov 23 09:57:41.540228 2017 | 2 | 2 | 3 |
44 | Sun Jan 19 05:53:34.829093 2014 | 4 | 758 | 205 | 3 | Thu Nov 23 11:18:53.114408 2017 | 2 | 2 | 0 |
46 | Mon Jan 13 20:39:11.211169 2014 | 0 | 235 | 475 | 3 | Thu Nov 23 12:56:49.29191 2017 | 0 | 5 | 1 |
46 | Wed Jan 15 09:14:57.471944 2014 | 2 | 407 | 664 | 3 | Thu Nov 23 17:18:51.048758 2017 | 1 | 5 | 5 |
50 | Sat Jan 11 11:07:13.089216 2014 | 6 | 292 | 425 | 5 | Wed Nov 22 20:43:18.667473 2017 | 0 | 3 | 2 |
58 | Sun Jan 19 22:36:14.795396 2014 | 2 | 86 | 311 | 5 | Wed Nov 22 21:02:07.575129 2017 | 2 | 0 | 2 |
66 | Tue Jan 14 20:16:31.219213 2014 | 14 | 347 | 655 | 5 | Wed Nov 22 22:10:24.315371 2017 | 1 | 2 | 1 |
74 | Tue Jan 21 01:38:39.570986 2014 | 9 | 334 | 642 | 5 | Thu Nov 23 00:54:44.192608 2017 | 1 | 3 | 2 |
86 | Sun Jan 19 06:18:51.466578 2014 | 14 | 712 | 490 | 5 | Thu Nov 23 07:47:09.542999 2017 | 1 | 4 | 3 |
87 | Sat Jan 11 20:46:28.439073 2014 | 2 | 528 | 311 | 5 | Thu Nov 23 09:05:08.53142 2017 | 2 | 2 | 2 |
90 | Sun Jan 12 21:37:30.778206 2014 | 11 | 458 | 377 | 5 | Thu Nov 23 09:17:47.706703 2017 | 2 | 5 | 3 |
100 | Sun Jan 19 22:32:08.284043 2014 | 2 | 384 | 149 | 5 | Thu Nov 23 10:15:31.764558 2017 | 2 | 2 | 2 |
(18 rows) 5 | Thu Nov 23 14:29:02.557934 2017 | 2 | 1 | 2 |
5 | Thu Nov 23 15:55:08.493462 2017 | 0 | 3 | 3 |
5 | Thu Nov 23 16:28:38.455322 2017 | 2 | 5 | 4 |
(21 rows)
-- determine if a recent user has done a given event type or not -- determine if a recent user has done a given event type or not
SELECT ru.user_id, CASE WHEN et.user_id IS NULL THEN 'NO' ELSE 'YES' END as done_event SELECT ru.user_id, CASE WHEN et.user_id IS NULL THEN 'NO' ELSE 'YES' END as done_event
FROM recent_users ru FROM recent_users ru
LEFT JOIN events_table et LEFT JOIN events_table et
ON(ru.user_id = et.user_id AND et.event_type = 625) ON(ru.user_id = et.user_id AND et.event_type = 6)
ORDER BY 2 DESC, 1; ORDER BY 2 DESC, 1;
user_id | done_event user_id | done_event
---------+------------ ---------+------------
6 | YES 1 | YES
13 | NO 3 | NO
39 | NO 5 | NO
44 | NO (3 rows)
46 | NO
50 | NO
58 | NO
66 | NO
71 | NO
74 | NO
86 | NO
87 | NO
90 | NO
100 | NO
(14 rows)
-- view vs table join wrapped inside a subquery -- view vs table join wrapped inside a subquery
SELECT * FROM SELECT * FROM
(SELECT ru.user_id, CASE WHEN et.user_id IS NULL THEN 'NO' ELSE 'YES' END as done_event (SELECT ru.user_id, CASE WHEN et.user_id IS NULL THEN 'NO' ELSE 'YES' END as done_event
FROM recent_users ru FROM recent_users ru
LEFT JOIN events_table et LEFT JOIN events_table et
ON(ru.user_id = et.user_id AND et.event_type = 625) ON(ru.user_id = et.user_id AND et.event_type = 6)
) s1 ) s1
ORDER BY 2 DESC, 1; ORDER BY 2 DESC, 1;
user_id | done_event user_id | done_event
---------+------------ ---------+------------
6 | YES 1 | YES
13 | NO 3 | NO
39 | NO 5 | NO
44 | NO (3 rows)
46 | NO
50 | NO
58 | NO
66 | NO
71 | NO
74 | NO
86 | NO
87 | NO
90 | NO
100 | NO
(14 rows)
-- event vs table non-partition-key join is not supported -- event vs table non-partition-key join is not supported
SELECT * FROM SELECT * FROM
@ -554,41 +479,32 @@ ORDER BY 2 DESC, 1;
ERROR: cannot pushdown the subquery since all relations are not joined using distribution keys ERROR: cannot pushdown the subquery since all relations are not joined using distribution keys
DETAIL: Each relation should be joined with at least one another relation using distribution keys and equality operator. DETAIL: Each relation should be joined with at least one another relation using distribution keys and equality operator.
-- create a select only view -- create a select only view
CREATE VIEW selected_users AS SELECT * FROM users_table WHERE value_1 >= 120 and value_1 <150; CREATE VIEW selected_users AS SELECT * FROM users_table WHERE value_1 >= 1 and value_1 <3;
CREATE VIEW recent_selected_users AS SELECT su.* FROM selected_users su JOIN recent_users ru USING(user_id); CREATE VIEW recent_selected_users AS SELECT su.* FROM selected_users su JOIN recent_users ru USING(user_id);
SELECT user_id FROM recent_selected_users GROUP BY 1 ORDER BY 1; SELECT user_id FROM recent_selected_users GROUP BY 1 ORDER BY 1;
user_id user_id
--------- ---------
6 1
13 3
39 5
44 (3 rows)
46
50
58
66
71
74
86
90
(12 rows)
-- this would be supported when we implement where partition_key in (subquery) support -- this would be supported when we implement where partition_key in (subquery) support
SELECT et.user_id, et.time FROM events_table et WHERE et.user_id IN (SELECT user_id FROM recent_selected_users) GROUP BY 1,2 ORDER BY 1 DESC,2 DESC LIMIT 5; SELECT et.user_id, et.time FROM events_table et WHERE et.user_id IN (SELECT user_id FROM recent_selected_users) GROUP BY 1,2 ORDER BY 1 DESC,2 DESC LIMIT 5;
user_id | time user_id | time
---------+--------------------------------- ---------+---------------------------------
90 | Tue Jan 21 02:50:05.379732 2014 5 | Thu Nov 23 16:11:02.929469 2017
90 | Tue Jan 21 00:08:33.911898 2014 5 | Thu Nov 23 14:40:40.467511 2017
90 | Mon Jan 20 22:25:39.21906 2014 5 | Thu Nov 23 14:28:51.833214 2017
90 | Mon Jan 20 21:11:10.814326 2014 5 | Thu Nov 23 14:23:09.889786 2017
90 | Mon Jan 20 19:16:33.359257 2014 5 | Thu Nov 23 13:26:45.571108 2017
(5 rows) (5 rows)
-- it is supported when it is a router query -- it is supported when it is a router query
SELECT count(*) FROM events_table et WHERE et.user_id IN (SELECT user_id FROM recent_selected_users WHERE user_id = 90); SELECT count(*) FROM events_table et WHERE et.user_id IN (SELECT user_id FROM recent_selected_users WHERE user_id = 1);
count count
------- -------
109 15
(1 row) (1 row)
-- expected this to work but it did not -- expected this to work but it did not
@ -603,15 +519,12 @@ SELECT *
(SELECT user_id FROM recent_users) (SELECT user_id FROM recent_users)
UNION UNION
(SELECT user_id FROM selected_users) ) u (SELECT user_id FROM selected_users) ) u
WHERE user_id < 15 AND user_id > 10 WHERE user_id < 2 AND user_id > 0
ORDER BY user_id; ORDER BY user_id;
user_id user_id
--------- ---------
11 1
12 (1 row)
13
14
(4 rows)
-- union all also works for views -- union all also works for views
SELECT * SELECT *
@ -619,36 +532,23 @@ SELECT *
(SELECT user_id FROM recent_users) (SELECT user_id FROM recent_users)
UNION ALL UNION ALL
(SELECT user_id FROM selected_users) ) u (SELECT user_id FROM selected_users) ) u
WHERE user_id < 15 AND user_id > 10 WHERE user_id < 2 AND user_id > 0
ORDER BY user_id; ORDER BY user_id;
user_id user_id
--------- ---------
11 1
11 1
11 (2 rows)
12
12
12
12
12
12
13
13
13
13
13
14
(15 rows)
SELECT count(*) SELECT count(*)
FROM ( FROM (
(SELECT user_id FROM recent_users) (SELECT user_id FROM recent_users)
UNION UNION
(SELECT user_id FROM selected_users) ) u (SELECT user_id FROM selected_users) ) u
WHERE user_id < 15 AND user_id > 10; WHERE user_id < 2 AND user_id > 0;
count count
------- -------
4 1
(1 row) (1 row)
-- expected this to work but it does not -- expected this to work but it does not
@ -657,7 +557,7 @@ SELECT count(*)
(SELECT user_id FROM recent_users) (SELECT user_id FROM recent_users)
UNION ALL UNION ALL
(SELECT user_id FROM selected_users) ) u (SELECT user_id FROM selected_users) ) u
WHERE user_id < 15 AND user_id > 10; WHERE user_id < 2 AND user_id > 0;
ERROR: cannot pushdown the subquery since all leaves of the UNION does not include partition key at the same position ERROR: cannot pushdown the subquery since all leaves of the UNION does not include partition key at the same position
DETAIL: Each leaf query of the UNION should return partition key at the same position on its target list. DETAIL: Each leaf query of the UNION should return partition key at the same position on its target list.
-- expand view definitions and re-run last 2 queries -- expand view definitions and re-run last 2 queries
@ -665,70 +565,68 @@ SELECT count(*)
FROM ( FROM (
(SELECT user_id FROM (SELECT user_id, max(time) as lastseen FROM users_table (SELECT user_id FROM (SELECT user_id, max(time) as lastseen FROM users_table
GROUP BY user_id GROUP BY user_id
HAVING max(time) > '2014-01-21 05:45:49.978738'::timestamp order by 2 DESC) aa HAVING max(time) > '2017-11-22 05:45:49.978738'::timestamp order by 2 DESC) aa
) )
UNION UNION
(SELECT user_id FROM (SELECT * FROM users_table WHERE value_1 >= 120 and value_1 <150) bb) ) u (SELECT user_id FROM (SELECT * FROM users_table WHERE value_1 >= 1 and value_1 < 3) bb) ) u
WHERE user_id < 15 AND user_id > 10; WHERE user_id < 2 AND user_id > 0;
count count
------- -------
4 1
(1 row) (1 row)
SELECT count(*) SELECT count(*)
FROM ( FROM (
(SELECT user_id FROM (SELECT user_id, max(time) as lastseen FROM users_table (SELECT user_id FROM (SELECT user_id, max(time) as lastseen FROM users_table
GROUP BY user_id GROUP BY user_id
HAVING max(time) > '2014-01-21 05:45:49.978738'::timestamp order by 2 DESC) aa HAVING max(time) > '2017-11-22 05:45:49.978738'::timestamp order by 2 DESC) aa
) )
UNION ALL UNION ALL
(SELECT user_id FROM (SELECT * FROM users_table WHERE value_1 >= 120 and value_1 <150) bb) ) u (SELECT user_id FROM (SELECT * FROM users_table WHERE value_1 >= 1 and value_1 < 3) bb) ) u
WHERE user_id < 15 AND user_id > 10; WHERE user_id < 2 AND user_id > 0;
ERROR: cannot pushdown the subquery since all leaves of the UNION does not include partition key at the same position ERROR: cannot pushdown the subquery since all leaves of the UNION does not include partition key at the same position
DETAIL: Each leaf query of the UNION should return partition key at the same position on its target list. DETAIL: Each leaf query of the UNION should return partition key at the same position on its target list.
-- test distinct -- test distinct
-- distinct is supported if it is on a partition key -- distinct is supported if it is on a partition key
CREATE VIEW distinct_user_with_value_1_15 AS SELECT DISTINCT user_id FROM users_table WHERE value_1 = 15; CREATE VIEW distinct_user_with_value_1_3 AS SELECT DISTINCT user_id FROM users_table WHERE value_1 = 3;
SELECT * FROM distinct_user_with_value_1_15 ORDER BY user_id; SELECT * FROM distinct_user_with_value_1_3 ORDER BY user_id;
user_id user_id
--------- ---------
7 1
8 2
35 3
42 4
46 5
53 6
70 (6 rows)
82
87
88
96
(11 rows)
-- distinct is not supported if it is on a non-partition key -- distinct is not supported if it is on a non-partition key
CREATE VIEW distinct_value_1 AS SELECT DISTINCT value_1 FROM users_table WHERE value_2 = 15; CREATE VIEW distinct_value_1 AS SELECT DISTINCT value_1 FROM users_table WHERE value_2 = 3;
SELECT * FROM distinct_value_1; SELECT * FROM distinct_value_1;
ERROR: cannot perform distributed planning on this query ERROR: cannot perform distributed planning on this query
DETAIL: Subqueries without group by clause are not supported yet DETAIL: Subqueries without group by clause are not supported yet
-- CTEs are not supported even if they are on views -- CTEs are not supported even if they are on views
CREATE VIEW cte_view_1 AS CREATE VIEW cte_view_1 AS
WITH c1 AS (SELECT * FROM users_table WHERE value_1 = 15) SELECT * FROM c1 WHERE value_2 < 500; WITH c1 AS (SELECT * FROM users_table WHERE value_1 = 3) SELECT * FROM c1 WHERE value_2 < 4;
SELECT * FROM cte_view_1; SELECT * FROM cte_view_1;
ERROR: cannot push down this subquery ERROR: cannot push down this subquery
DETAIL: CTEs in multi-shard queries are currently unsupported DETAIL: CTEs in multi-shard queries are currently unsupported
-- this is single shard query but still not supported since it has view + cte -- this is single shard query but still not supported since it has view + cte
-- router planner can't detect it -- router planner can't detect it
SELECT * FROM cte_view_1 WHERE user_id = 8; SELECT * FROM cte_view_1 WHERE user_id = 2;
ERROR: cannot push down this subquery ERROR: cannot push down this subquery
DETAIL: CTEs in multi-shard queries are currently unsupported DETAIL: CTEs in multi-shard queries are currently unsupported
-- if CTE itself prunes down to a single shard than the view is supported (router plannable) -- if CTE itself prunes down to a single shard than the view is supported (router plannable)
CREATE VIEW cte_view_2 AS CREATE VIEW cte_view_2 AS
WITH c1 AS (SELECT * FROM users_table WHERE user_id = 8) SELECT * FROM c1 WHERE value_1 = 15; WITH c1 AS (SELECT * FROM users_table WHERE user_id = 2) SELECT * FROM c1 WHERE value_1 = 3;
SELECT * FROM cte_view_2; SELECT * FROM cte_view_2;
user_id | time | value_1 | value_2 | value_3 | value_4 user_id | time | value_1 | value_2 | value_3 | value_4
---------+---------------------------------+---------+---------+---------+--------- ---------+---------------------------------+---------+---------+---------+---------
8 | Tue Jan 21 00:52:36.967785 2014 | 15 | 10 | 868 | 2 | Thu Nov 23 00:19:14.138058 2017 | 3 | 4 | 0 |
(1 row) 2 | Thu Nov 23 13:52:54.83829 2017 | 3 | 1 | 4 |
2 | Wed Nov 22 18:19:49.944985 2017 | 3 | 5 | 1 |
2 | Thu Nov 23 11:41:04.042936 2017 | 3 | 4 | 1 |
(4 rows)
CREATE VIEW router_view AS SELECT * FROM users_table WHERE user_id = 2; CREATE VIEW router_view AS SELECT * FROM users_table WHERE user_id = 2;
-- router plannable -- router plannable
@ -742,18 +640,14 @@ SELECT user_id FROM router_view GROUP BY 1;
SELECT * FROM (SELECT user_id FROM router_view GROUP BY 1) rv JOIN recent_events USING (user_id) ORDER BY 2 LIMIT 3; SELECT * FROM (SELECT user_id FROM router_view GROUP BY 1) rv JOIN recent_events USING (user_id) ORDER BY 2 LIMIT 3;
user_id | time user_id | time
---------+--------------------------------- ---------+---------------------------------
2 | Mon Jan 20 02:02:03.208351 2014 2 | Thu Nov 23 17:26:14.563216 2017
2 | Mon Jan 20 02:34:14.54301 2014 (1 row)
2 | Mon Jan 20 03:16:38.418772 2014
(3 rows)
SELECT * FROM (SELECT user_id FROM router_view GROUP BY 1) rv JOIN (SELECT * FROM recent_events) re USING (user_id) ORDER BY 2 LIMIT 3; SELECT * FROM (SELECT user_id FROM router_view GROUP BY 1) rv JOIN (SELECT * FROM recent_events) re USING (user_id) ORDER BY 2 LIMIT 3;
user_id | time user_id | time
---------+--------------------------------- ---------+---------------------------------
2 | Mon Jan 20 02:02:03.208351 2014 2 | Thu Nov 23 17:26:14.563216 2017
2 | Mon Jan 20 02:34:14.54301 2014 (1 row)
2 | Mon Jan 20 03:16:38.418772 2014
(3 rows)
-- views with limits -- views with limits
CREATE VIEW recent_10_users AS CREATE VIEW recent_10_users AS
@ -779,31 +673,27 @@ DETAIL: Limit in subquery without limit in the outermost query is unsupported
SELECT * FROM recent_10_users ORDER BY lastseen DESC LIMIT 10; SELECT * FROM recent_10_users ORDER BY lastseen DESC LIMIT 10;
user_id | lastseen user_id | lastseen
---------+--------------------------------- ---------+---------------------------------
6 | Tue Jan 21 05:57:47.118755 2014 1 | Thu Nov 23 17:30:34.635085 2017
71 | Tue Jan 21 05:55:52.018461 2014 3 | Thu Nov 23 17:18:51.048758 2017
39 | Tue Jan 21 05:55:18.875997 2014 5 | Thu Nov 23 16:48:32.08896 2017
74 | Tue Jan 21 05:54:04.837808 2014 4 | Thu Nov 23 15:32:02.360969 2017
87 | Tue Jan 21 05:53:51.866813 2014 6 | Thu Nov 23 14:43:18.024104 2017
50 | Tue Jan 21 05:53:44.251016 2014 2 | Thu Nov 23 13:52:54.83829 2017
66 | Tue Jan 21 05:51:31.681997 2014 (6 rows)
100 | Tue Jan 21 05:49:04.953009 2014
46 | Tue Jan 21 05:49:00.229807 2014
86 | Tue Jan 21 05:48:54.381334 2014
(10 rows)
SELECT et.* FROM recent_10_users JOIN events_table et USING(user_id) ORDER BY et.time DESC LIMIT 10; SELECT et.* FROM recent_10_users JOIN events_table et USING(user_id) ORDER BY et.time DESC LIMIT 10;
user_id | time | event_type | value_2 | value_3 | value_4 user_id | time | event_type | value_2 | value_3 | value_4
---------+---------------------------------+------------+---------+---------+--------- ---------+---------------------------------+------------+---------+---------+---------
65 | Tue Jan 21 05:56:52.624231 2014 | 241 | 30 | 543 | 1 | Thu Nov 23 21:54:46.924477 2017 | 6 | 4 | 5 |
42 | Tue Jan 21 05:46:35.158342 2014 | 761 | 877 | 335 | 4 | Thu Nov 23 18:10:21.338399 2017 | 1 | 2 | 4 |
54 | Tue Jan 21 05:46:19.103645 2014 | 595 | 477 | 996 | 3 | Thu Nov 23 18:08:26.550729 2017 | 2 | 4 | 3 |
44 | Tue Jan 21 05:43:00.838945 2014 | 682 | 641 | 448 | 2 | Thu Nov 23 17:26:14.563216 2017 | 1 | 5 | 3 |
27 | Tue Jan 21 05:34:10.935865 2014 | 912 | 605 | 989 | 3 | Thu Nov 23 16:44:41.903713 2017 | 4 | 2 | 2 |
61 | Tue Jan 21 05:25:27.452065 2014 | 392 | 472 | 925 | 3 | Thu Nov 23 16:31:56.219594 2017 | 5 | 1 | 2 |
19 | Tue Jan 21 05:23:09.26298 2014 | 202 | 888 | 640 | 4 | Thu Nov 23 16:20:33.264457 2017 | 0 | 0 | 3 |
65 | Tue Jan 21 05:22:56.725329 2014 | 519 | 457 | 259 | 5 | Thu Nov 23 16:11:02.929469 2017 | 4 | 2 | 0 |
27 | Tue Jan 21 05:19:14.38026 2014 | 19 | 19 | 205 | 2 | Thu Nov 23 15:58:49.273421 2017 | 5 | 1 | 2 |
11 | Tue Jan 21 05:15:14.879531 2014 | 459 | 545 | 80 | 5 | Thu Nov 23 14:40:40.467511 2017 | 1 | 4 | 1 |
(10 rows) (10 rows)
RESET citus.subquery_pushdown; RESET citus.subquery_pushdown;
@ -825,26 +715,24 @@ EXPLAIN (COSTS FALSE) SELECT user_id FROM recent_selected_users GROUP BY 1 ORDER
Group Key: users_table.user_id Group Key: users_table.user_id
-> Hash Join -> Hash Join
Hash Cond: (users_table.user_id = ru.user_id) Hash Cond: (users_table.user_id = ru.user_id)
-> Bitmap Heap Scan on users_table_1400000 users_table -> Seq Scan on users_table_1400000 users_table
Recheck Cond: ((value_1 >= 120) AND (value_1 < 150)) Filter: ((value_1 >= 1) AND (value_1 < 3))
-> Bitmap Index Scan on is_index3_1400000
Index Cond: ((value_1 >= 120) AND (value_1 < 150))
-> Hash -> Hash
-> Subquery Scan on ru -> Subquery Scan on ru
-> Sort -> Sort
Sort Key: (max(users_table_1."time")) DESC Sort Key: (max(users_table_1."time")) DESC
-> HashAggregate -> HashAggregate
Group Key: users_table_1.user_id Group Key: users_table_1.user_id
Filter: (max(users_table_1."time") > '2014-01-21 05:45:49.978738'::timestamp without time zone) Filter: (max(users_table_1."time") > '2017-11-23 16:20:33.264457'::timestamp without time zone)
-> Seq Scan on users_table_1400000 users_table_1 -> Seq Scan on users_table_1400000 users_table_1
(25 rows) (23 rows)
EXPLAIN (COSTS FALSE) SELECT * EXPLAIN (COSTS FALSE) SELECT *
FROM ( FROM (
(SELECT user_id FROM recent_users) (SELECT user_id FROM recent_users)
UNION UNION
(SELECT user_id FROM selected_users) ) u (SELECT user_id FROM selected_users) ) u
WHERE user_id < 15 AND user_id > 10 WHERE user_id < 4 AND user_id > 1
ORDER BY user_id; ORDER BY user_id;
QUERY PLAN QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------------------------
@ -864,13 +752,14 @@ EXPLAIN (COSTS FALSE) SELECT *
Sort Key: (max(users_table."time")) DESC Sort Key: (max(users_table."time")) DESC
-> GroupAggregate -> GroupAggregate
Group Key: users_table.user_id Group Key: users_table.user_id
Filter: (max(users_table."time") > '2014-01-21 05:45:49.978738'::timestamp without time zone) Filter: (max(users_table."time") > '2017-11-23 16:20:33.264457'::timestamp without time zone)
-> Index Scan using is_index1_1400000 on users_table_1400000 users_table -> Sort
Index Cond: ((user_id < 15) AND (user_id > 10)) Sort Key: users_table.user_id
-> Index Scan using is_index1_1400000 on users_table_1400000 users_table_1 -> Seq Scan on users_table_1400000 users_table
Index Cond: ((user_id < 15) AND (user_id > 10)) Filter: ((user_id < 4) AND (user_id > 1))
Filter: ((value_1 >= 120) AND (value_1 < 150)) -> Seq Scan on users_table_1400000 users_table_1
(22 rows) Filter: ((value_1 >= 1) AND (value_1 < 3) AND (user_id < 4) AND (user_id > 1))
(23 rows)
EXPLAIN (COSTS FALSE) SELECT et.* FROM recent_10_users JOIN events_table et USING(user_id) ORDER BY et.time DESC LIMIT 10; EXPLAIN (COSTS FALSE) SELECT et.* FROM recent_10_users JOIN events_table et USING(user_id) ORDER BY et.time DESC LIMIT 10;
ERROR: cannot push down this subquery ERROR: cannot push down this subquery
@ -909,7 +798,7 @@ DROP VIEW router_view;
DROP VIEW cte_view_2; DROP VIEW cte_view_2;
DROP VIEW cte_view_1; DROP VIEW cte_view_1;
DROP VIEW distinct_value_1; DROP VIEW distinct_value_1;
DROP VIEW distinct_user_with_value_1_15; DROP VIEW distinct_user_with_value_1_3;
DROP VIEW recent_selected_users; DROP VIEW recent_selected_users;
DROP VIEW selected_users; DROP VIEW selected_users;
DROP VIEW recent_events; DROP VIEW recent_events;

View File

@ -12,9 +12,9 @@ FROM (
FROM users_table AS u, FROM users_table AS u,
events_table AS e events_table AS e
WHERE u.user_id = e.user_id WHERE u.user_id = e.user_id
AND u.user_id >= 10 AND u.user_id >= 1
AND u.user_id <= 25 AND u.user_id <= 2
AND e.event_type IN (100, 101, 102) AND e.event_type IN (2, 3)
) t ) t
GROUP BY user_id GROUP BY user_id
) q; ) q;
@ -28,8 +28,6 @@ SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results;
-- Funnel grouped by whether or not a user has done an event -- Funnel grouped by whether or not a user has done an event
------------------------------------ ------------------------------------
------------------------------------ ------------------------------------
TRUNCATE agg_results;
INSERT INTO agg_results (user_id, value_1_agg, value_2_agg ) INSERT INTO agg_results (user_id, value_1_agg, value_2_agg )
SELECT user_id, sum(array_length(events_table, 1)), length(hasdone_event) SELECT user_id, sum(array_length(events_table, 1)), length(hasdone_event)
FROM ( FROM (
@ -43,9 +41,9 @@ FROM (
FROM users_table AS u, FROM users_table AS u,
events_table AS e events_table AS e
WHERE u.user_id = e.user_id WHERE u.user_id = e.user_id
AND u.user_id >= 10 AND u.user_id >= 1
AND u.user_id <= 25 AND u.user_id <= 2
AND e.event_type IN (100, 101, 102) AND e.event_type IN (1, 2)
) )
UNION UNION
( (
@ -53,18 +51,17 @@ FROM (
FROM users_table AS u, FROM users_table AS u,
events_table AS e events_table AS e
WHERE u.user_id = e.user_id WHERE u.user_id = e.user_id
AND u.user_id >= 10 AND u.user_id >= 1
AND u.user_id <= 25 AND u.user_id <= 2
AND e.event_type IN (103, 104, 105) AND e.event_type IN (3, 4)
) )
) t1 LEFT JOIN ( ) t1 LEFT JOIN (
SELECT DISTINCT user_id, SELECT DISTINCT user_id,
'Has done event'::TEXT AS hasdone_event 'Has done event'::TEXT AS hasdone_event
FROM events_table AS e FROM events_table AS e
WHERE e.user_id >= 1
WHERE e.user_id >= 10 AND e.user_id <= 2
AND e.user_id <= 25 AND e.event_type IN (5, 6)
AND e.event_type IN (106, 107, 108)
) t2 ON (t1.user_id = t2.user_id) ) t2 ON (t1.user_id = t2.user_id)
GROUP BY t1.user_id, hasdone_event GROUP BY t1.user_id, hasdone_event
@ -78,9 +75,6 @@ SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results;
-- Funnel, grouped by the number of times a user has done an event -- Funnel, grouped by the number of times a user has done an event
------------------------------------ ------------------------------------
------------------------------------ ------------------------------------
TRUNCATE agg_results;
INSERT INTO agg_results (user_id, value_1_agg, value_2_agg) INSERT INTO agg_results (user_id, value_1_agg, value_2_agg)
SELECT SELECT
user_id, user_id,
@ -102,9 +96,9 @@ SELECT
events_table events_table
WHERE WHERE
users_table.user_id = events_table.user_id AND users_table.user_id = events_table.user_id AND
users_table.user_id >= 10 AND users_table.user_id >= 1 AND
users_table.user_id <= 70 AND users_table.user_id <= 3 AND
events_table.event_type > 10 AND events_table.event_type < 12 events_table.event_type > 0 AND events_table.event_type < 2
) )
UNION UNION
(SELECT (SELECT
@ -116,9 +110,9 @@ SELECT
events_table events_table
WHERE WHERE
users_table.user_id = events_table.user_id AND users_table.user_id = events_table.user_id AND
users_table.user_id >= 10 AND users_table.user_id >= 1 AND
users_table.user_id <= 70 AND users_table.user_id <= 3 AND
events_table.event_type > 12 AND events_table.event_type < 14 events_table.event_type > 1 AND events_table.event_type < 3
) )
) AS subquery_1 ) AS subquery_1
LEFT JOIN LEFT JOIN
@ -128,9 +122,9 @@ SELECT
FROM FROM
users_table users_table
WHERE WHERE
user_id >= 10 AND user_id >= 1 AND
user_id <= 70 AND user_id <= 3 AND
users_table.value_1 > 15 AND users_table.value_1 < 17 users_table.value_1 > 3 AND users_table.value_1 < 5
GROUP BY GROUP BY
user_id user_id
HAVING HAVING
@ -174,15 +168,15 @@ FROM (
SELECT user_id, time SELECT user_id, time
FROM users_table FROM users_table
WHERE WHERE
user_id >= 10 AND user_id >= 1 AND
user_id <= 70 AND user_id <= 3 AND
users_table.value_1 > 10 AND users_table.value_1 < 12 users_table.value_1 > 3 AND users_table.value_1 < 6
) u LEFT JOIN LATERAL ( ) u LEFT JOIN LATERAL (
SELECT event_type, time SELECT event_type, time
FROM events_table FROM events_table
WHERE user_id = u.user_id AND WHERE user_id = u.user_id AND
events_table.event_type > 10 AND events_table.event_type < 12 events_table.event_type > 3 AND events_table.event_type < 6
) t ON true ) t ON true
GROUP BY user_id GROUP BY user_id
) AS shard_union ) AS shard_union
@ -202,9 +196,9 @@ TRUNCATE agg_results;
INSERT INTO agg_results (user_id) INSERT INTO agg_results (user_id)
SELECT DISTINCT user_id SELECT DISTINCT user_id
FROM users_table FROM users_table
WHERE user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 10 AND value_1 <= 20) WHERE user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 1 AND value_1 <= 2)
AND user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 30 AND value_1 <= 40) AND user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 3 AND value_1 <= 4)
AND user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 50 AND value_1 <= 60); AND user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 5 AND value_1 <= 6);
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results;
@ -219,9 +213,9 @@ TRUNCATE agg_results;
INSERT INTO agg_results(user_id) INSERT INTO agg_results(user_id)
SELECT user_id SELECT user_id
FROM users_table FROM users_table
WHERE (value_1 = 10 WHERE (value_1 = 1
OR value_1 = 11 OR value_1 = 2
OR value_1 = 12) OR value_1 = 3)
GROUP BY user_id GROUP BY user_id
HAVING count(distinct value_1) >= 2; HAVING count(distinct value_1) >= 2;
@ -237,9 +231,9 @@ TRUNCATE agg_results;
INSERT INTO agg_results(user_id, value_2_agg) INSERT INTO agg_results(user_id, value_2_agg)
SELECT user_id, value_2 FROM users_table WHERE SELECT user_id, value_2 FROM users_table WHERE
value_1 > 101 AND value_1 < 110 value_1 > 1 AND value_1 < 4
AND value_2 >= 5 AND value_2 >= 3
AND EXISTS (SELECT user_id FROM events_table WHERE event_type>101 AND event_type < 110 AND value_3 > 100 AND user_id=users_table.user_id); AND EXISTS (SELECT user_id FROM events_table WHERE event_type>1 AND event_type < 5 AND value_3 > 2 AND user_id=users_table.user_id);
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results;
@ -253,9 +247,9 @@ TRUNCATE agg_results;
INSERT INTO agg_results(user_id, value_2_agg) INSERT INTO agg_results(user_id, value_2_agg)
SELECT user_id, value_2 FROM users_table WHERE SELECT user_id, value_2 FROM users_table WHERE
value_1 = 101 value_1 = 1
AND value_2 >= 5 AND value_2 >= 2
AND NOT EXISTS (SELECT user_id FROM events_table WHERE event_type=101 AND value_3 > 100 AND user_id=users_table.user_id); AND NOT EXISTS (SELECT user_id FROM events_table WHERE event_type=1 AND value_3 > 4 AND user_id=users_table.user_id);
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results;
@ -269,10 +263,10 @@ TRUNCATE agg_results;
INSERT INTO agg_results(user_id, value_2_agg) INSERT INTO agg_results(user_id, value_2_agg)
SELECT user_id, value_2 FROM users_table WHERE SELECT user_id, value_2 FROM users_table WHERE
value_1 > 100 value_1 > 1
AND value_2 >= 5 AND value_2 >= 3
AND EXISTS (SELECT user_id FROM events_table WHERE event_type!=100 AND value_3 > 100 AND user_id=users_table.user_id) AND EXISTS (SELECT user_id FROM events_table WHERE event_type!=1 AND value_3 > 1 AND user_id=users_table.user_id)
AND EXISTS (SELECT user_id FROM events_table WHERE event_type=101 AND value_3 > 100 AND user_id=users_table.user_id); AND EXISTS (SELECT user_id FROM events_table WHERE event_type=2 AND value_3 > 1 AND user_id=users_table.user_id);
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results;
@ -286,9 +280,9 @@ TRUNCATE agg_results;
INSERT INTO agg_results(user_id, value_2_agg) INSERT INTO agg_results(user_id, value_2_agg)
SELECT user_id, value_2 FROM users_table WHERE SELECT user_id, value_2 FROM users_table WHERE
value_2 >= 5 value_2 >= 3
AND EXISTS (SELECT user_id FROM events_table WHERE event_type > 100 AND event_type <= 300 AND value_3 > 100 AND user_id=users_table.user_id) AND EXISTS (SELECT user_id FROM events_table WHERE event_type > 1 AND event_type <= 3 AND value_3 > 1 AND user_id=users_table.user_id)
AND NOT EXISTS (SELECT user_id FROM events_table WHERE event_type > 300 AND event_type <= 350 AND value_3 > 100 AND user_id=users_table.user_id); AND NOT EXISTS (SELECT user_id FROM events_table WHERE event_type > 3 AND event_type <= 4 AND value_3 > 1 AND user_id=users_table.user_id);
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results;
@ -304,14 +298,14 @@ INSERT INTO agg_results(user_id, value_2_agg)
SELECT user_id, SELECT user_id,
value_2 value_2
FROM users_table FROM users_table
WHERE value_1 > 100 WHERE value_1 > 1
AND value_1 < 124 AND value_1 < 3
AND value_2 >= 5 AND value_2 >= 1
AND EXISTS (SELECT user_id AND EXISTS (SELECT user_id
FROM events_table FROM events_table
WHERE event_type > 100 WHERE event_type > 1
AND event_type < 124 AND event_type < 3
AND value_3 > 100 AND value_3 > 1
AND user_id = users_table.user_id AND user_id = users_table.user_id
GROUP BY user_id GROUP BY user_id
HAVING Count(*) > 2); HAVING Count(*) > 2);
@ -330,7 +324,7 @@ INSERT INTO agg_results(user_id, value_1_agg)
SELECT user_id, value_1 from SELECT user_id, value_1 from
( (
SELECT user_id, value_1 From users_table SELECT user_id, value_1 From users_table
WHERE value_2 > 100 and user_id = 15 GROUP BY value_1, user_id HAVING count(*) > 1 WHERE value_2 > 1 and user_id = 1 GROUP BY value_1, user_id HAVING count(*) > 1
) as a; ) as a;
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
@ -346,13 +340,13 @@ TRUNCATE agg_results;
INSERT INTO agg_results(user_id) INSERT INTO agg_results(user_id)
Select user_id Select user_id
From events_table From events_table
Where event_type = 16 Where event_type = 2
And value_2 > 50 And value_2 > 2
And user_id in And user_id in
(select user_id (select user_id
From users_table From users_table
Where value_1 = 15 Where value_1 = 2
And value_2 > 25); And value_2 > 1);
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results;
@ -366,7 +360,7 @@ TRUNCATE agg_results;
INSERT INTO agg_results(user_id, value_1_agg) INSERT INTO agg_results(user_id, value_1_agg)
SELECT user_id, event_type FROM events_table SELECT user_id, event_type FROM events_table
WHERE user_id in (SELECT user_id from events_table WHERE event_type > 500 and event_type < 505) WHERE user_id in (SELECT user_id from events_table WHERE event_type > 3 and event_type < 5)
GROUP BY user_id, event_type; GROUP BY user_id, event_type;
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
@ -386,7 +380,7 @@ select user_id from
user_id user_id
from from
events_table events_table
where event_type = 901 group by user_id having count(*) > 3 where event_type = 4 group by user_id having count(*) > 3
) as a; ) as a;
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
@ -406,14 +400,14 @@ FROM
users_table users_table
JOIN JOIN
(SELECT (SELECT
ma.user_id, (GREATEST(coalesce(ma.value_4 / 250, 0.0) + GREATEST(1.0))) / 2 AS prob ma.user_id, (GREATEST(coalesce(ma.value_4, 0.0) / 250 + GREATEST(1.0))) / 2 AS prob
FROM FROM
users_table AS ma, events_table as short_list users_table AS ma, events_table as short_list
WHERE WHERE
short_list.user_id = ma.user_id and ma.value_1 < 50 and short_list.event_type < 50 short_list.user_id = ma.user_id and ma.value_1 < 3 and short_list.event_type < 3
) temp ) temp
ON users_table.user_id = temp.user_id ON users_table.user_id = temp.user_id
WHERE users_table.value_1 < 50; WHERE users_table.value_1 < 3;
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results;
@ -432,10 +426,10 @@ FROM
FROM FROM
users_table AS ma, events_table as short_list users_table AS ma, events_table as short_list
WHERE WHERE
short_list.user_id = ma.user_id and ma.value_1 < 50 and short_list.event_type < 3 short_list.user_id = ma.user_id and ma.value_1 < 3 and short_list.event_type < 2
) temp ) temp
ON users_ids.user_id = temp.user_id ON users_ids.user_id = temp.user_id
WHERE temp.value_1 < 50; WHERE temp.value_1 < 3;
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results;
@ -454,10 +448,10 @@ FROM
FROM FROM
users_table AS ma, events_table as short_list users_table AS ma, events_table as short_list
WHERE WHERE
short_list.user_id = ma.user_id and ma.value_1 < 50 and short_list.event_type < 15 short_list.user_id = ma.user_id and ma.value_1 < 3 and short_list.event_type < 2
) temp ) temp
ON users_ids.user_id = temp.user_id ON users_ids.user_id = temp.user_id
WHERE temp.value_1 < 50 WHERE temp.value_1 < 3
ORDER BY 1, 2; ORDER BY 1, 2;
SELECT count(*), count(DISTINCT user_id), avg(user_id), avg(value_1_agg) FROM agg_results; SELECT count(*), count(DISTINCT user_id), avg(user_id), avg(value_1_agg) FROM agg_results;
@ -476,7 +470,7 @@ FROM
FROM FROM
users_table AS ma, events_table as short_list users_table AS ma, events_table as short_list
WHERE WHERE
short_list.user_id = ma.user_id and ma.value_1 < 5000 and short_list.event_type < 3 short_list.user_id = ma.user_id and ma.value_1 < 10 and short_list.event_type < 2
) temp ) temp
ON users_ids.user_id = temp.user_id ON users_ids.user_id = temp.user_id
ORDER BY 1, 2; ORDER BY 1, 2;

View File

@ -14,13 +14,13 @@ FROM (
FROM users_table AS u, FROM users_table AS u,
events_table AS e events_table AS e
WHERE u.user_id = e.user_id WHERE u.user_id = e.user_id
AND u.user_id >= 10 AND u.user_id >= 1
AND u.user_id <= 25 AND u.user_id <= 2
AND e.event_type IN (100, 101, 102) AND e.event_type IN (2,3)
) t ) t
GROUP BY user_id GROUP BY user_id
) q ) q
WHERE user_id = 20; WHERE user_id = 2;
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_second; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_second;
@ -41,13 +41,13 @@ FROM (
FROM users_table AS u, FROM users_table AS u,
events_table AS e events_table AS e
WHERE u.user_id = e.user_id AND WHERE u.user_id = e.user_id AND
(u.user_id = 13 OR u.user_id = 20) AND (u.user_id = 1 OR u.user_id = 2) AND
(e.user_id = 13 OR e.user_id = 20) (e.user_id = 1 OR e.user_id = 2)
AND e.event_type IN (100, 101, 102) AND e.event_type IN (1, 2)
) t ) t
GROUP BY user_id GROUP BY user_id
) q ) q
WHERE (user_id = 13 OR user_id = 20); WHERE (user_id = 1 OR user_id = 2);
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_second; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_second;
@ -72,9 +72,9 @@ FROM (
FROM users_table AS u, FROM users_table AS u,
events_table AS e events_table AS e
WHERE u.user_id = e.user_id WHERE u.user_id = e.user_id
AND u.user_id >= 10 AND u.user_id >= 1
AND u.user_id <= 25 AND u.user_id <= 2
AND e.event_type IN (100, 101, 102) AND e.event_type IN (1, 2)
) )
UNION UNION
( (
@ -82,20 +82,20 @@ FROM (
FROM users_table AS u, FROM users_table AS u,
events_table AS e events_table AS e
WHERE u.user_id = e.user_id WHERE u.user_id = e.user_id
AND u.user_id >= 10 AND u.user_id >= 1
AND u.user_id <= 25 AND u.user_id <= 2
AND e.event_type IN (103, 104, 105) AND e.event_type IN (3, 4)
) )
) t1 LEFT JOIN ( ) t1 LEFT JOIN (
SELECT DISTINCT user_id, SELECT DISTINCT user_id,
'Has done event'::TEXT AS hasdone_event 'Has done event'::TEXT AS hasdone_event
FROM events_table AS e FROM events_table AS e
WHERE e.user_id >= 10 WHERE e.user_id >= 1
AND e.user_id <= 25 AND e.user_id <= 2
AND e.event_type IN (106, 107, 108) AND e.event_type IN (5, 6)
) t2 ON (t1.user_id = t2.user_id) ) t2 ON (t1.user_id = t2.user_id)
WHERE t1.user_id = 20 WHERE t1.user_id = 2
GROUP BY t1.user_id, hasdone_event GROUP BY t1.user_id, hasdone_event
) t GROUP BY user_id, hasdone_event; ) t GROUP BY user_id, hasdone_event;
@ -120,8 +120,8 @@ FROM (
FROM users_table AS u, FROM users_table AS u,
events_table AS e events_table AS e
WHERE u.user_id = e.user_id WHERE u.user_id = e.user_id
AND (e.user_id = 20 OR e.user_id = 17) AND (e.user_id = 2 OR e.user_id = 3)
AND e.event_type IN (100, 101, 102) AND e.event_type IN (1, 2)
) )
UNION UNION
( (
@ -129,8 +129,8 @@ FROM (
FROM users_table AS u, FROM users_table AS u,
events_table AS e events_table AS e
WHERE u.user_id = e.user_id WHERE u.user_id = e.user_id
AND (e.user_id = 20 OR e.user_id = 17) AND (e.user_id = 2 OR e.user_id = 3)
AND e.event_type IN (103, 104, 105) AND e.event_type IN (3, 4)
) )
) t1 LEFT JOIN ( ) t1 LEFT JOIN (
SELECT DISTINCT user_id, SELECT DISTINCT user_id,
@ -138,10 +138,10 @@ FROM (
FROM events_table AS e FROM events_table AS e
WHERE WHERE
(e.user_id = 20 OR e.user_id = 17) (e.user_id = 2 OR e.user_id = 3)
AND e.event_type IN (106, 107, 108) AND e.event_type IN (4, 5)
) t2 ON (t1.user_id = t2.user_id) ) t2 ON (t1.user_id = t2.user_id)
WHERE (t1.user_id = 20 OR t1.user_id = 17) WHERE (t1.user_id = 2 OR t1.user_id = 1)
GROUP BY t1.user_id, hasdone_event GROUP BY t1.user_id, hasdone_event
) t GROUP BY user_id, hasdone_event; ) t GROUP BY user_id, hasdone_event;
@ -173,17 +173,17 @@ FROM (
SELECT user_id, time SELECT user_id, time
FROM users_table FROM users_table
WHERE WHERE
user_id >= 10 AND user_id >= 1 AND
user_id <= 70 AND user_id <= 5 AND
users_table.value_1 > 10 AND users_table.value_1 < 12 users_table.value_1 > 1 AND users_table.value_1 < 4
) u LEFT JOIN LATERAL ( ) u LEFT JOIN LATERAL (
SELECT event_type, time SELECT event_type, time
FROM events_table FROM events_table
WHERE user_id = u.user_id AND WHERE user_id = u.user_id AND
events_table.event_type > 10 AND events_table.event_type < 12 events_table.event_type > 1 AND events_table.event_type < 4
) t ON true ) t ON true
WHERE user_id = 65 WHERE user_id = 5
GROUP BY user_id GROUP BY user_id
) AS shard_union ) AS shard_union
ORDER BY user_lastseen DESC; ORDER BY user_lastseen DESC;
@ -215,18 +215,18 @@ FROM (
SELECT user_id, time SELECT user_id, time
FROM users_table FROM users_table
WHERE WHERE
user_id >= 10 AND user_id >= 1 AND
user_id <= 70 AND user_id <= 5 AND
(user_id = 65 OR user_id = 12) AND (user_id = 5 OR user_id = 1) AND
users_table.value_1 > 10 AND users_table.value_1 < 12 users_table.value_1 > 1 AND users_table.value_1 < 4
) u LEFT JOIN LATERAL ( ) u LEFT JOIN LATERAL (
SELECT event_type, time SELECT event_type, time
FROM events_table FROM events_table
WHERE user_id = u.user_id AND (user_id = 65 OR user_id = 12) AND WHERE user_id = u.user_id AND (user_id = 5 OR user_id = 1) AND
events_table.event_type > 10 AND events_table.event_type < 12 events_table.event_type > 1 AND events_table.event_type < 4
) t ON true ) t ON true
WHERE (user_id = 65 OR user_id = 12) WHERE (user_id = 5 OR user_id = 1)
GROUP BY user_id GROUP BY user_id
) AS shard_union ) AS shard_union
ORDER BY user_lastseen DESC; ORDER BY user_lastseen DESC;
@ -246,10 +246,10 @@ TRUNCATE agg_results_second;
INSERT INTO agg_results_second (user_id) INSERT INTO agg_results_second (user_id)
SELECT DISTINCT user_id SELECT DISTINCT user_id
FROM users_table FROM users_table
WHERE user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 10 AND value_1 <= 20) WHERE user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 1 AND value_1 <= 2)
AND user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 30 AND value_1 <= 40) AND user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 3 AND value_1 <= 4)
AND user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 50 AND value_1 <= 60) AND user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 5 AND value_1 <= 6)
AND user_id = 7; AND user_id = 1;
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_second; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_second;
@ -265,10 +265,10 @@ TRUNCATE agg_results_second;
INSERT INTO agg_results_second (user_id) INSERT INTO agg_results_second (user_id)
SELECT DISTINCT user_id SELECT DISTINCT user_id
FROM users_table FROM users_table
WHERE user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 10 AND value_1 <= 20 AND (user_id = 7 OR user_id = 20)) WHERE user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 1 AND value_1 <= 2 AND (user_id = 1 OR user_id = 2))
AND user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 30 AND value_1 <= 40 AND (user_id = 7 OR user_id = 20)) AND user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 3 AND value_1 <= 4 AND (user_id = 1 OR user_id = 2))
AND user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 50 AND value_1 <= 60 AND (user_id = 7 OR user_id = 20)) AND user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 5 AND value_1 <= 6 AND (user_id = 1 OR user_id = 2))
AND (user_id = 7 OR user_id = 20); AND (user_id = 1 OR user_id = 2);
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_second; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_second;
@ -282,10 +282,10 @@ TRUNCATE agg_results_second;
INSERT INTO agg_results_second(user_id, value_2_agg) INSERT INTO agg_results_second(user_id, value_2_agg)
SELECT user_id, value_2 FROM users_table WHERE SELECT user_id, value_2 FROM users_table WHERE
value_1 > 101 AND value_1 < 110 value_1 > 1 AND value_1 < 4
AND value_2 >= 5 AND value_2 >= 1
AND EXISTS (SELECT user_id FROM events_table WHERE event_type>101 AND event_type < 110 AND value_3 > 100 AND user_id=users_table.user_id) AND EXISTS (SELECT user_id FROM events_table WHERE event_type>1 AND event_type < 3 AND value_3 > 1 AND user_id=users_table.user_id)
AND user_id = 61; AND user_id = 2;
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_second; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_second;
@ -300,10 +300,10 @@ TRUNCATE agg_results_second;
INSERT INTO agg_results_second(user_id, value_2_agg) INSERT INTO agg_results_second(user_id, value_2_agg)
SELECT user_id, value_2 FROM users_table WHERE SELECT user_id, value_2 FROM users_table WHERE
value_1 > 101 AND value_1 < 110 value_1 > 1 AND value_1 < 4
AND value_2 >= 5 AND value_2 >= 1
AND EXISTS (SELECT user_id FROM events_table WHERE event_type>101 AND event_type < 110 AND value_3 > 100 AND (user_id = 61 OR user_id = 51) AND user_id=users_table.user_id) AND EXISTS (SELECT user_id FROM events_table WHERE event_type>0 AND event_type < 2 AND value_3 > 1 AND (user_id = 2 OR user_id = 1) AND user_id=users_table.user_id)
AND (user_id = 61 OR user_id = 51); AND (user_id = 2 OR user_id = 1);
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_second; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_second;
@ -317,10 +317,10 @@ TRUNCATE agg_results_second;
INSERT INTO agg_results_second(user_id, value_2_agg) INSERT INTO agg_results_second(user_id, value_2_agg)
SELECT user_id, value_2 FROM users_table WHERE SELECT user_id, value_2 FROM users_table WHERE
value_2 >= 5 value_2 >= 2
AND user_id = 96 AND user_id = 1
AND EXISTS (SELECT user_id FROM events_table WHERE event_type > 100 AND event_type <= 300 AND value_3 > 100 AND user_id=users_table.user_id) AND EXISTS (SELECT user_id FROM events_table WHERE event_type > 1 AND event_type <= 3 AND value_3 > 1 AND user_id=users_table.user_id)
AND NOT EXISTS (SELECT user_id FROM events_table WHERE event_type > 300 AND event_type <= 350 AND value_3 > 100 AND user_id=users_table.user_id); AND NOT EXISTS (SELECT user_id FROM events_table WHERE event_type > 4 AND event_type <= 5 AND value_3 > 4 AND user_id=users_table.user_id);
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_second; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_second;
@ -334,10 +334,10 @@ TRUNCATE agg_results_second;
INSERT INTO agg_results_second(user_id, value_2_agg) INSERT INTO agg_results_second(user_id, value_2_agg)
SELECT user_id, value_2 FROM users_table WHERE SELECT user_id, value_2 FROM users_table WHERE
value_2 >= 5 value_2 >= 2
AND (user_id = 96 OR user_id = 8) AND (user_id = 1 OR user_id = 2)
AND EXISTS (SELECT user_id FROM events_table WHERE event_type > 100 AND event_type <= 300 AND value_3 > 100 AND user_id=users_table.user_id AND (user_id = 96 OR user_id = 8)) AND EXISTS (SELECT user_id FROM events_table WHERE event_type > 1 AND event_type <= 3 AND value_3 > 1 AND user_id=users_table.user_id AND (user_id = 1 OR user_id = 2))
AND NOT EXISTS (SELECT user_id FROM events_table WHERE event_type > 300 AND event_type <= 350 AND value_3 > 100 AND user_id=users_table.user_id AND (user_id = 96 OR user_id = 8)); AND NOT EXISTS (SELECT user_id FROM events_table WHERE event_type > 4 AND event_type <= 5 AND value_3 > 4 AND user_id=users_table.user_id AND (user_id = 1 OR user_id = 2));
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_second; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results_second;
@ -354,17 +354,17 @@ INSERT INTO agg_results_second(user_id, value_2_agg)
SELECT user_id, SELECT user_id,
value_2 value_2
FROM users_table FROM users_table
WHERE value_1 > 100 WHERE value_1 > 1
AND value_1 < 124 AND value_1 < 3
AND value_2 >= 5 AND value_2 >= 1
AND user_id = 47 AND user_id = 3
AND EXISTS (SELECT user_id AND EXISTS (SELECT user_id
FROM events_table FROM events_table
WHERE event_type > 100 WHERE event_type > 1
AND event_type < 124 AND event_type < 3
AND value_3 > 100 AND value_3 > 1
AND user_id = users_table.user_id AND user_id = users_table.user_id
AND user_id = 47 AND user_id = 3
GROUP BY user_id GROUP BY user_id
HAVING Count(*) > 2); HAVING Count(*) > 2);
@ -382,17 +382,16 @@ INSERT INTO agg_results_second(user_id, value_2_agg)
SELECT user_id, SELECT user_id,
value_2 value_2
FROM users_table FROM users_table
WHERE value_1 > 100 WHERE value_1 > 1
AND value_1 < 124 AND value_1 < 3
AND value_2 >= 5 AND value_2 >= 1
AND (user_id = 47 or user_id = 81) AND (user_id = 3 or user_id = 4)
AND EXISTS (SELECT user_id AND EXISTS (SELECT user_id
FROM events_table FROM events_table
WHERE event_type > 100 WHERE event_type = 2
AND event_type < 124 AND value_3 > 1
AND value_3 > 100
AND user_id = users_table.user_id AND user_id = users_table.user_id
AND (user_id = 47 or user_id = 81) AND (user_id = 3 or user_id = 4)
GROUP BY user_id GROUP BY user_id
HAVING Count(*) > 2); HAVING Count(*) > 2);

View File

@ -97,7 +97,7 @@ SELECT * FROM
users_table, events_table users_table, events_table
WHERE WHERE
users_table.user_id = events_table.user_id and users_table.user_id = events_table.user_id and
event_type < 25 event_type < 2
WINDOW w1 AS (PARTITION BY users_table.user_id, events_table.event_type ORDER BY events_table.time) WINDOW w1 AS (PARTITION BY users_table.user_id, events_table.event_type ORDER BY events_table.time)
) as foo; ) as foo;
@ -115,7 +115,7 @@ SELECT * FROM
users_table, events_table users_table, events_table
WHERE WHERE
users_table.user_id = events_table.user_id and users_table.user_id = events_table.user_id and
event_type < 25 event_type < 2
WINDOW w1 AS (PARTITION BY users_table.user_id, events_table.event_type ORDER BY events_table.time), WINDOW w1 AS (PARTITION BY users_table.user_id, events_table.event_type ORDER BY events_table.time),
w2 AS (PARTITION BY users_table.user_id, (events_table.value_2 % 25) ORDER BY events_table.time) w2 AS (PARTITION BY users_table.user_id, (events_table.value_2 % 25) ORDER BY events_table.time)
) as foo; ) as foo;
@ -134,7 +134,7 @@ SELECT sub_1.user_id, max(lag_1), max(rank_1), max(rank_2) FROM
users_table, events_table users_table, events_table
WHERE WHERE
users_table.user_id = events_table.user_id and users_table.user_id = events_table.user_id and
event_type < 25 event_type < 2
WINDOW w1 AS (PARTITION BY users_table.user_id, events_table.event_type ORDER BY events_table.time), WINDOW w1 AS (PARTITION BY users_table.user_id, events_table.event_type ORDER BY events_table.time),
w2 AS (PARTITION BY users_table.user_id, (events_table.value_2 % 25) ORDER BY events_table.time) w2 AS (PARTITION BY users_table.user_id, (events_table.value_2 % 25) ORDER BY events_table.time)
) as sub_1 ) as sub_1
@ -146,7 +146,7 @@ JOIN
users_table, events_table users_table, events_table
WHERE WHERE
users_table.user_id = events_table.user_id and users_table.user_id = events_table.user_id and
event_type < 25 event_type < 2
WINDOW w1 AS (PARTITION BY users_table.user_id, events_table.value_2 ORDER BY events_table.time), WINDOW w1 AS (PARTITION BY users_table.user_id, events_table.value_2 ORDER BY events_table.time),
w2 AS (PARTITION BY users_table.user_id, (events_table.value_2 % 50) ORDER BY events_table.time) w2 AS (PARTITION BY users_table.user_id, (events_table.value_2 % 50) ORDER BY events_table.time)
) as sub_2 ) as sub_2
@ -173,7 +173,7 @@ FROM
WINDOW my_win AS (PARTITION BY user_id ORDER BY count(*) DESC) WINDOW my_win AS (PARTITION BY user_id ORDER BY count(*) DESC)
) as foo ) as foo
WHERE WHERE
my_rank > 5 my_rank > 1
GROUP BY GROUP BY
my_rank; my_rank;
@ -267,7 +267,7 @@ SELECT
FROM FROM
users_table users_table
WHERE WHERE
value_2 > 545 AND value_2 > 1 AND
value_2 < ALL ( value_2 < ALL (
SELECT SELECT
avg(value_3) OVER (PARTITION BY user_id) avg(value_3) OVER (PARTITION BY user_id)
@ -704,7 +704,7 @@ SELECT
FROM FROM
users_table users_table
WHERE WHERE
value_2 > 545 AND value_2 > 2 AND
value_2 < ALL ( value_2 < ALL (
SELECT SELECT
avg(value_3) OVER () avg(value_3) OVER ()

View File

@ -445,8 +445,8 @@ SELECT create_distributed_table('partitioned_users_table', 'user_id', colocate_w
SELECT create_distributed_table('partitioned_events_table', 'user_id', colocate_with => 'events_table'); SELECT create_distributed_table('partitioned_events_table', 'user_id', colocate_with => 'events_table');
-- INSERT/SELECT from regular table to partitioned table -- INSERT/SELECT from regular table to partitioned table
CREATE TABLE partitioned_users_table_2009 PARTITION OF partitioned_users_table FOR VALUES FROM ('2014-01-01') TO ('2015-01-01'); CREATE TABLE partitioned_users_table_2009 PARTITION OF partitioned_users_table FOR VALUES FROM ('2017-01-01') TO ('2018-01-01');
CREATE TABLE partitioned_events_table_2009 PARTITION OF partitioned_events_table FOR VALUES FROM ('2014-01-01') TO ('2015-01-01'); CREATE TABLE partitioned_events_table_2009 PARTITION OF partitioned_events_table FOR VALUES FROM ('2017-01-01') TO ('2018-01-01');
INSERT INTO partitioned_events_table SELECT * FROM events_table; INSERT INTO partitioned_events_table SELECT * FROM events_table;
INSERT INTO partitioned_users_table_2009 SELECT * FROM users_table; INSERT INTO partitioned_users_table_2009 SELECT * FROM users_table;
@ -471,28 +471,28 @@ FROM
FROM FROM
partitioned_events_table as "events" partitioned_events_table as "events"
WHERE WHERE
event_type IN (10, 11, 12, 13, 14, 15)) event_type IN (1, 2) )
UNION UNION
(SELECT (SELECT
"events"."user_id", "events"."time", 1 AS event "events"."user_id", "events"."time", 1 AS event
FROM FROM
partitioned_events_table as "events" partitioned_events_table as "events"
WHERE WHERE
event_type IN (15, 16, 17, 18, 19) ) event_type IN (3, 4) )
UNION UNION
(SELECT (SELECT
"events"."user_id", "events"."time", 2 AS event "events"."user_id", "events"."time", 2 AS event
FROM FROM
partitioned_events_table as "events" partitioned_events_table as "events"
WHERE WHERE
event_type IN (20, 21, 22, 23, 24, 25) ) event_type IN (5, 6) )
UNION UNION
(SELECT (SELECT
"events"."user_id", "events"."time", 3 AS event "events"."user_id", "events"."time", 3 AS event
FROM FROM
partitioned_events_table as "events" partitioned_events_table as "events"
WHERE WHERE
event_type IN (26, 27, 28, 29, 30, 13))) t1 event_type IN (1, 6))) t1
GROUP BY "t1"."user_id") AS t) "q" GROUP BY "t1"."user_id") AS t) "q"
) AS final_query ) AS final_query
GROUP BY types GROUP BY types
@ -518,7 +518,7 @@ FROM
FROM FROM
partitioned_events_table as "events" partitioned_events_table as "events"
WHERE WHERE
event_type IN (10, 11, 12, 13, 14, 15) ) events_subquery_1) event_type IN (1, 2)) events_subquery_1)
UNION UNION
(SELECT * (SELECT *
FROM FROM
@ -533,7 +533,7 @@ FROM
events_table as "events", users_table as "users" events_table as "events", users_table as "users"
WHERE WHERE
events.user_id = users.user_id AND events.user_id = users.user_id AND
event_type IN (10, 11, 12, 13, 14, 15) event_type IN (1, 2)
GROUP BY "events"."user_id" GROUP BY "events"."user_id"
) as events_subquery_5 ) as events_subquery_5
) events_subquery_2) ) events_subquery_2)
@ -545,7 +545,7 @@ FROM
FROM FROM
partitioned_events_table as "events" partitioned_events_table as "events"
WHERE WHERE
event_type IN (20, 21, 22, 23, 24, 25) ) events_subquery_3) event_type IN (3, 4)) events_subquery_3)
UNION UNION
(SELECT * (SELECT *
FROM FROM
@ -554,7 +554,7 @@ FROM
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (26, 27, 28, 29, 30, 13)) events_subquery_4) event_type IN (5, 6)) events_subquery_4)
) t1 ) t1
GROUP BY "t1"."user_id") AS t) "q" GROUP BY "t1"."user_id") AS t) "q"
INNER JOIN INNER JOIN
@ -563,7 +563,7 @@ INNER JOIN
FROM FROM
partitioned_users_table as "users" partitioned_users_table as "users"
WHERE WHERE
value_1 > 50 and value_1 < 70) AS t value_1 > 2 and value_1 < 5) AS t
ON (t.user_id = q.user_id)) as final_query ON (t.user_id = q.user_id)) as final_query
GROUP BY GROUP BY
types types
@ -572,9 +572,9 @@ ORDER BY
-- test LIST partitioning -- test LIST partitioning
CREATE TABLE list_partitioned_events_table (user_id int, time date, event_type int, value_2 int, value_3 float, value_4 bigint) PARTITION BY LIST (time); CREATE TABLE list_partitioned_events_table (user_id int, time date, event_type int, value_2 int, value_3 float, value_4 bigint) PARTITION BY LIST (time);
CREATE TABLE list_partitioned_events_table_2014_01_01_05 PARTITION OF list_partitioned_events_table FOR VALUES IN ('2014-01-01', '2014-01-02', '2014-01-03', '2014-01-04', '2014-01-05'); CREATE TABLE list_partitioned_events_table_2014_01_01_05 PARTITION OF list_partitioned_events_table FOR VALUES IN ('2017-11-21', '2017-11-22', '2017-11-23', '2017-11-24', '2017-11-25');
CREATE TABLE list_partitioned_events_table_2014_01_06_10 PARTITION OF list_partitioned_events_table FOR VALUES IN ('2014-01-06', '2014-01-07', '2014-01-08', '2014-01-09', '2014-01-10'); CREATE TABLE list_partitioned_events_table_2014_01_06_10 PARTITION OF list_partitioned_events_table FOR VALUES IN ('2017-11-26', '2017-11-27', '2017-11-28', '2017-11-29', '2017-11-30');
CREATE TABLE list_partitioned_events_table_2014_01_11_15 PARTITION OF list_partitioned_events_table FOR VALUES IN ('2014-01-11', '2014-01-12', '2014-01-13', '2014-01-14', '2014-01-15'); CREATE TABLE list_partitioned_events_table_2014_01_11_15 PARTITION OF list_partitioned_events_table FOR VALUES IN ('2017-12-01', '2017-12-02', '2017-12-03', '2017-12-04', '2017-12-05');
-- test distributing partitioned table colocated with another partitioned table -- test distributing partitioned table colocated with another partitioned table
SELECT create_distributed_table('list_partitioned_events_table', 'user_id', colocate_with => 'partitioned_events_table'); SELECT create_distributed_table('list_partitioned_events_table', 'user_id', colocate_with => 'partitioned_events_table');
@ -592,8 +592,8 @@ SELECT
FROM FROM
events_table events_table
WHERE WHERE
time >= '2014-01-01' AND time >= '2017-11-21' AND
time <= '2014-01-15'; time <= '2017-12-01';
-- LEFT JOINs used with INNER JOINs on range partitioned table, list partitioned table and non-partitioned table -- LEFT JOINs used with INNER JOINs on range partitioned table, list partitioned table and non-partitioned table
SELECT SELECT
@ -612,14 +612,14 @@ count(*) AS cnt, "generated_group_field"
FROM FROM
list_partitioned_events_table as "list_partitioned_events_table" list_partitioned_events_table as "list_partitioned_events_table"
WHERE WHERE
user_id > 80) "temp_data_queries" user_id > 2) "temp_data_queries"
INNER JOIN INNER JOIN
(SELECT (SELECT
"users"."user_id" "users"."user_id"
FROM FROM
partitioned_users_table as "users" partitioned_users_table as "users"
WHERE WHERE
user_id > 80 and value_2 = 5) "user_filters_1" user_id > 2 and value_2 = 1) "user_filters_1"
ON ("temp_data_queries".event_user_id = "user_filters_1".user_id)) AS "multi_group_wrapper_1" ON ("temp_data_queries".event_user_id = "user_filters_1".user_id)) AS "multi_group_wrapper_1"
LEFT JOIN LEFT JOIN
(SELECT (SELECT

View File

@ -23,9 +23,9 @@ FROM (
FROM users_table AS u, FROM users_table AS u,
events_table AS e events_table AS e
WHERE u.user_id = e.user_id WHERE u.user_id = e.user_id
AND u.user_id >= 10 AND u.user_id >= 1
AND u.user_id <= 25 AND u.user_id <= 3
AND e.event_type IN (100, 101, 102) AND e.event_type IN (1, 2)
) t ) t
GROUP BY user_id GROUP BY user_id
) q ) q
@ -47,9 +47,9 @@ FROM (
FROM users_table AS u, FROM users_table AS u,
events_table AS e events_table AS e
WHERE u.user_id = e.user_id WHERE u.user_id = e.user_id
AND u.user_id >= 10 AND u.user_id >= 1
AND u.user_id <= 25 AND u.user_id <= 3
AND e.event_type IN (100, 101, 102) AND e.event_type IN (1, 2)
) )
UNION UNION
( (
@ -57,17 +57,17 @@ FROM (
FROM users_table AS u, FROM users_table AS u,
events_table AS e events_table AS e
WHERE u.user_id = e.user_id WHERE u.user_id = e.user_id
AND u.user_id >= 10 AND u.user_id >= 1
AND u.user_id <= 25 AND u.user_id <= 3
AND e.event_type IN (103, 104, 105) AND e.event_type IN (3, 4)
) )
) t1 LEFT JOIN ( ) t1 LEFT JOIN (
SELECT DISTINCT user_id, SELECT DISTINCT user_id,
'Has done event'::TEXT AS hasdone_event 'Has done event'::TEXT AS hasdone_event
FROM events_table AS e FROM events_table AS e
WHERE e.user_id >= 10 WHERE e.user_id >= 1
AND e.user_id <= 25 AND e.user_id <= 3
AND e.event_type IN (106, 107, 108) AND e.event_type IN (5, 6)
) t2 ON (t1.user_id = t2.user_id) ) t2 ON (t1.user_id = t2.user_id)
GROUP BY t1.user_id, hasdone_event GROUP BY t1.user_id, hasdone_event
) t GROUP BY user_id, hasdone_event ) t GROUP BY user_id, hasdone_event
@ -88,9 +88,9 @@ FROM (
FROM users_table AS u, FROM users_table AS u,
events_table AS e events_table AS e
WHERE u.user_id = e.user_id WHERE u.user_id = e.user_id
AND u.user_id >= 10 AND u.user_id >= 1
AND u.user_id <= 25 AND u.user_id <= 3
AND e.event_type IN (100, 101, 102) AND e.event_type IN (1, 2)
) )
UNION UNION
( (
@ -98,17 +98,17 @@ FROM (
FROM users_table AS u, FROM users_table AS u,
events_table AS e events_table AS e
WHERE u.user_id = e.user_id WHERE u.user_id = e.user_id
AND u.user_id >= 10 AND u.user_id >= 1
AND u.user_id <= 25 AND u.user_id <= 3
AND e.event_type IN (103, 104, 105) AND e.event_type IN (3, 4)
) )
) t1 LEFT JOIN ( ) t1 LEFT JOIN (
SELECT DISTINCT user_id, SELECT DISTINCT user_id,
'Has done event'::TEXT AS hasdone_event 'Has done event'::TEXT AS hasdone_event
FROM events_table AS e FROM events_table AS e
WHERE e.user_id >= 10 WHERE e.user_id >= 1
AND e.user_id <= 25 AND e.user_id <= 3
AND e.event_type IN (106, 107, 108) AND e.event_type IN (5, 6)
) t2 ON (t1.user_id = t2.user_id) ) t2 ON (t1.user_id = t2.user_id)
GROUP BY t1.user_id, hasdone_event GROUP BY t1.user_id, hasdone_event
) t GROUP BY user_id, hasdone_event ) t GROUP BY user_id, hasdone_event
@ -124,22 +124,22 @@ FROM (
FROM ( FROM (
SELECT SELECT
u.user_id, u.user_id,
CASE WHEN e.event_type IN (100, 101, 102) THEN 'step=>1'::text else 'step==>2'::text END AS event, CASE WHEN e.event_type IN (1, 2) THEN 'step=>1'::text else 'step==>2'::text END AS event,
e.time e.time
FROM users_table AS u, FROM users_table AS u,
events_table AS e events_table AS e
WHERE u.user_id = e.user_id WHERE u.user_id = e.user_id
AND u.user_id >= 10 AND u.user_id >= 1
AND u.user_id <= 25 AND u.user_id <= 3
AND e.event_type IN (100, 101, 102, 103, 104, 105) AND e.event_type IN (1, 2, 3, 4)
GROUP BY 1,2,3 GROUP BY 1,2,3
) t1 LEFT JOIN ( ) t1 LEFT JOIN (
SELECT DISTINCT user_id, SELECT DISTINCT user_id,
'Has done event'::TEXT AS hasdone_event 'Has done event'::TEXT AS hasdone_event
FROM events_table AS e FROM events_table AS e
WHERE e.user_id >= 10 WHERE e.user_id >= 1
AND e.user_id <= 25 AND e.user_id <= 3
AND e.event_type IN (106, 107, 108) AND e.event_type IN (5, 6)
) t2 ON (t1.user_id = t2.user_id) ) t2 ON (t1.user_id = t2.user_id)
GROUP BY t1.user_id, hasdone_event GROUP BY t1.user_id, hasdone_event
) t GROUP BY user_id, hasdone_event ) t GROUP BY user_id, hasdone_event
@ -157,22 +157,22 @@ FROM (
FROM ( FROM (
SELECT SELECT
u.user_id, u.user_id,
CASE WHEN e.event_type in (100, 101, 102) then 'step=>1'::text else 'step==>2'::text END AS event, CASE WHEN e.event_type in (1, 2) then 'step=>1'::text else 'step==>2'::text END AS event,
e.time e.time
FROM users_table AS u, FROM users_table AS u,
events_table AS e events_table AS e
WHERE u.user_id = e.user_id WHERE u.user_id = e.user_id
AND u.user_id >= 10 AND u.user_id >= 1
AND u.user_id <= 25 AND u.user_id <= 3
AND e.event_type IN (100, 101, 102, 103, 104, 105) AND e.event_type IN (1, 2, 3, 4)
GROUP BY 1,2,3 GROUP BY 1,2,3
) t1 LEFT JOIN ( ) t1 LEFT JOIN (
SELECT DISTINCT user_id, SELECT DISTINCT user_id,
'Has done event'::TEXT AS hasdone_event 'Has done event'::TEXT AS hasdone_event
FROM events_table AS e FROM events_table AS e
WHERE e.user_id >= 10 WHERE e.user_id >= 1
AND e.user_id <= 25 AND e.user_id <= 3
AND e.event_type IN (106, 107, 108) AND e.event_type IN (5, 6)
) t2 ON (t1.user_id = t2.user_id) ) t2 ON (t1.user_id = t2.user_id)
GROUP BY t1.user_id, hasdone_event GROUP BY t1.user_id, hasdone_event
) t GROUP BY user_id, hasdone_event ) t GROUP BY user_id, hasdone_event
@ -201,9 +201,9 @@ SELECT
events_table events_table
WHERE WHERE
users_table.user_id = events_table.user_id AND users_table.user_id = events_table.user_id AND
users_table.user_id >= 10 AND users_table.user_id >= 1 AND
users_table.user_id <= 70 AND users_table.user_id <= 3 AND
events_table.event_type > 10 AND events_table.event_type < 12 events_table.event_type > 1 AND events_table.event_type < 3
) )
UNION UNION
(SELECT (SELECT
@ -215,9 +215,9 @@ SELECT
events_table events_table
WHERE WHERE
users_table.user_id = events_table.user_id AND users_table.user_id = events_table.user_id AND
users_table.user_id >= 10 AND users_table.user_id >= 1 AND
users_table.user_id <= 70 AND users_table.user_id <= 3 AND
events_table.event_type > 12 AND events_table.event_type < 14 events_table.event_type > 1 AND events_table.event_type < 4
) )
) AS subquery_1 ) AS subquery_1
LEFT JOIN LEFT JOIN
@ -227,9 +227,9 @@ SELECT
FROM FROM
users_table users_table
WHERE WHERE
user_id >= 10 AND user_id >= 1 AND
user_id <= 70 AND user_id <= 3 AND
users_table.value_1 > 15 AND users_table.value_1 < 17 users_table.value_1 > 2 AND users_table.value_1 < 5
GROUP BY GROUP BY
user_id user_id
HAVING HAVING
@ -266,9 +266,9 @@ SELECT
events_table events_table
WHERE WHERE
users_table.user_id = events_table.user_id AND users_table.user_id = events_table.user_id AND
users_table.user_id >= 10 AND users_table.user_id >= 1 AND
users_table.user_id <= 70 AND users_table.user_id <= 3 AND
events_table.event_type > 10 AND events_table.event_type < 12 events_table.event_type > 1 AND events_table.event_type < 3
) )
UNION UNION
(SELECT (SELECT
@ -280,9 +280,9 @@ SELECT
events_table events_table
WHERE WHERE
users_table.user_id = events_table.user_id AND users_table.user_id = events_table.user_id AND
users_table.user_id >= 10 AND users_table.user_id >= 1 AND
users_table.user_id <= 70 AND users_table.user_id <= 3 AND
events_table.event_type > 12 AND events_table.event_type < 14 events_table.event_type > 1 AND events_table.event_type < 4
) )
) AS subquery_1 ) AS subquery_1
LEFT JOIN LEFT JOIN
@ -292,9 +292,9 @@ SELECT
FROM FROM
users_table users_table
WHERE WHERE
user_id >= 10 AND user_id >= 1 AND
user_id <= 70 AND user_id <= 3 AND
users_table.value_1 > 15 AND users_table.value_1 < 17 users_table.value_1 > 2 AND users_table.value_1 < 4
GROUP BY GROUP BY
user_id user_id
HAVING HAVING
@ -329,7 +329,7 @@ ORDER BY
users_table.user_id, users_table.user_id,
CASE CASE
WHEN WHEN
events_table.event_type > 10 AND events_table.event_type < 12 events_table.event_type > 1 AND events_table.event_type < 3
THEN 'action=>1' THEN 'action=>1'
ELSE 'action=>2' ELSE 'action=>2'
END AS event, END AS event,
@ -339,11 +339,11 @@ ORDER BY
events_table events_table
WHERE WHERE
users_table.user_id = events_table.user_id AND users_table.user_id = events_table.user_id AND
users_table.user_id >= 10 AND users_table.user_id >= 1 AND
users_table.user_id <= 70 AND users_table.user_id <= 3 AND
(events_table.event_type > 10 AND events_table.event_type < 12 (events_table.event_type > 1 AND events_table.event_type < 3
OR OR
events_table.event_type > 12 AND events_table.event_type < 14) events_table.event_type > 2 AND events_table.event_type < 4)
GROUP BY 1, 2, 3 GROUP BY 1, 2, 3
) AS subquery_1 ) AS subquery_1
LEFT JOIN LEFT JOIN
@ -353,9 +353,9 @@ ORDER BY
FROM FROM
users_table users_table
WHERE WHERE
user_id >= 10 AND user_id >= 1 AND
user_id <= 70 AND user_id <= 3 AND
users_table.value_1 > 15 AND users_table.value_1 < 17 users_table.value_1 > 3 AND users_table.value_1 < 5
GROUP BY GROUP BY
user_id user_id
HAVING HAVING
@ -385,18 +385,18 @@ SELECT
( (
SELECT SELECT
users_table.user_id, users_table.user_id,
CASE WHEN events_table.event_type > 10 AND events_table.event_type < 12 THEN 'action=>1' ELSE 'action=>2' END AS event, CASE WHEN events_table.event_type > 1 AND events_table.event_type < 3 THEN 'action=>1' ELSE 'action=>2' END AS event,
events_table.time events_table.time
FROM FROM
users_table, users_table,
events_table events_table
WHERE WHERE
users_table.user_id = events_table.user_id AND users_table.user_id = events_table.user_id AND
users_table.user_id >= 10 AND users_table.user_id >= 1 AND
users_table.user_id <= 70 AND users_table.user_id <= 3 AND
(events_table.event_type > 10 AND events_table.event_type < 12 (events_table.event_type > 1 AND events_table.event_type < 3
OR OR
events_table.event_type > 12 AND events_table.event_type < 14) events_table.event_type > 2 AND events_table.event_type < 4)
GROUP BY 1, 2, 3 GROUP BY 1, 2, 3
) AS subquery_1 ) AS subquery_1
LEFT JOIN LEFT JOIN
@ -406,9 +406,9 @@ SELECT
FROM FROM
users_table users_table
WHERE WHERE
user_id >= 10 AND user_id >= 1 AND
user_id <= 70 AND user_id <= 3 AND
users_table.value_1 > 15 AND users_table.value_1 < 17 users_table.value_1 > 3 AND users_table.value_1 < 5
GROUP BY GROUP BY
user_id user_id
HAVING HAVING
@ -445,14 +445,14 @@ FROM (
SELECT user_id, time SELECT user_id, time
FROM users_table FROM users_table
WHERE WHERE
user_id >= 10 AND user_id >= 1 AND
user_id <= 70 AND user_id <= 3 AND
users_table.value_1 > 10 AND users_table.value_1 < 12 users_table.value_1 > 1 AND users_table.value_1 < 3
) u LEFT JOIN LATERAL ( ) u LEFT JOIN LATERAL (
SELECT event_type, time SELECT event_type, time
FROM events_table FROM events_table
WHERE user_id = u.user_id AND WHERE user_id = u.user_id AND
events_table.event_type > 10 AND events_table.event_type < 12 events_table.event_type > 1 AND events_table.event_type < 3
) t ON true ) t ON true
GROUP BY user_id GROUP BY user_id
) AS shard_union ) AS shard_union
@ -463,9 +463,9 @@ ORDER BY user_lastseen DESC, user_id;
------------------------------------ ------------------------------------
SELECT user_id SELECT user_id
FROM users_table FROM users_table
WHERE user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 10 AND value_1 <= 20) WHERE user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 1 AND value_1 <= 2)
AND user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 30 AND value_1 <= 40) AND user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 3 AND value_1 <= 4)
AND user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 50 AND value_1 <= 60) AND user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 5 AND value_1 <= 6)
GROUP BY GROUP BY
user_id user_id
ORDER BY ORDER BY
@ -476,9 +476,9 @@ ORDER BY
-- Find customers who have done X, and satisfy other customer specific criteria -- Find customers who have done X, and satisfy other customer specific criteria
------------------------------------ ------------------------------------
SELECT user_id, value_2 FROM users_table WHERE SELECT user_id, value_2 FROM users_table WHERE
value_1 > 101 AND value_1 < 110 value_1 > 1 AND value_1 < 3
AND value_2 >= 5 AND value_2 >= 1
AND EXISTS (SELECT user_id FROM events_table WHERE event_type > 101 AND event_type < 110 AND value_3 > 100 AND user_id = users_table.user_id) AND EXISTS (SELECT user_id FROM events_table WHERE event_type > 1 AND event_type < 3 AND value_3 > 1 AND user_id = users_table.user_id)
ORDER BY 2 DESC, 1 DESC ORDER BY 2 DESC, 1 DESC
LIMIT 5; LIMIT 5;
@ -486,9 +486,9 @@ LIMIT 5;
-- Customers who havent done X, and satisfy other customer specific criteria -- Customers who havent done X, and satisfy other customer specific criteria
------------------------------------ ------------------------------------
SELECT user_id, value_2 FROM users_table WHERE SELECT user_id, value_2 FROM users_table WHERE
value_1 = 101 value_1 = 2
AND value_2 >= 5 AND value_2 >= 1
AND NOT EXISTS (SELECT user_id FROM events_table WHERE event_type=101 AND value_3 > 100 AND user_id = users_table.user_id) AND NOT EXISTS (SELECT user_id FROM events_table WHERE event_type=2 AND value_3 > 1 AND user_id = users_table.user_id)
ORDER BY 1 DESC, 2 DESC ORDER BY 1 DESC, 2 DESC
LIMIT 3; LIMIT 3;
@ -496,10 +496,10 @@ LIMIT 3;
-- Customers who have done X and Y, and satisfy other customer specific criteria -- Customers who have done X and Y, and satisfy other customer specific criteria
------------------------------------ ------------------------------------
SELECT user_id, sum(value_2) as cnt FROM users_table WHERE SELECT user_id, sum(value_2) as cnt FROM users_table WHERE
value_1 > 100 value_1 > 1
AND value_2 >= 5 AND value_2 >= 1
AND EXISTS (SELECT user_id FROM events_table WHERE event_type != 100 AND value_3 > 100 AND user_id = users_table.user_id) AND EXISTS (SELECT user_id FROM events_table WHERE event_type != 1 AND value_3 > 1 AND user_id = users_table.user_id)
AND EXISTS (SELECT user_id FROM events_table WHERE event_type = 101 AND value_3 > 100 AND user_id = users_table.user_id) AND EXISTS (SELECT user_id FROM events_table WHERE event_type = 2 AND value_3 > 1 AND user_id = users_table.user_id)
GROUP BY GROUP BY
user_id user_id
ORDER BY cnt DESC, user_id DESC ORDER BY cnt DESC, user_id DESC
@ -509,9 +509,9 @@ LIMIT 5;
-- Customers who have done X and havent done Y, and satisfy other customer specific criteria -- Customers who have done X and havent done Y, and satisfy other customer specific criteria
------------------------------------ ------------------------------------
SELECT user_id, value_2 FROM users_table WHERE SELECT user_id, value_2 FROM users_table WHERE
value_2 >= 5 value_2 >= 1
AND EXISTS (SELECT user_id FROM events_table WHERE event_type > 100 AND event_type <= 300 AND value_3 > 100 AND user_id = users_table.user_id) AND EXISTS (SELECT user_id FROM events_table WHERE event_type > 1 AND event_type <= 3 AND value_3 > 1 AND user_id = users_table.user_id)
AND NOT EXISTS (SELECT user_id FROM events_table WHERE event_type > 300 AND event_type <= 350 AND value_3 > 100 AND user_id = users_table.user_id) AND NOT EXISTS (SELECT user_id FROM events_table WHERE event_type > 3 AND event_type <= 4 AND value_3 > 1 AND user_id = users_table.user_id)
ORDER BY 2 DESC, 1 DESC ORDER BY 2 DESC, 1 DESC
LIMIT 4; LIMIT 4;
@ -521,14 +521,14 @@ LIMIT 4;
SELECT user_id, SELECT user_id,
avg(value_2) avg(value_2)
FROM users_table FROM users_table
WHERE value_1 > 100 WHERE value_1 > 1
AND value_1 < 124 AND value_1 < 3
AND value_2 >= 5 AND value_2 >= 1
AND EXISTS (SELECT user_id AND EXISTS (SELECT user_id
FROM events_table FROM events_table
WHERE event_type > 100 WHERE event_type > 1
AND event_type < 124 AND event_type < 3
AND value_3 > 100 AND value_3 > 1
AND user_id = users_table.user_id AND user_id = users_table.user_id
GROUP BY user_id GROUP BY user_id
HAVING Count(*) > 2) HAVING Count(*) > 2)
@ -546,7 +546,7 @@ SELECT user_id, value_1 from
SELECT SELECT
user_id, value_1 From users_table user_id, value_1 From users_table
WHERE WHERE
value_2 > 100 and user_id = 15 value_2 > 1 and user_id = 2
GROUP BY GROUP BY
value_1, user_id value_1, user_id
HAVING HAVING
@ -561,7 +561,7 @@ SELECT user_id, value_1 from
SELECT SELECT
user_id, value_1 From users_table user_id, value_1 From users_table
WHERE WHERE
value_2 > 100 and (user_id = 15 OR user_id = 16) value_2 > 1 and (user_id = 2 OR user_id = 3)
GROUP BY GROUP BY
value_1, user_id value_1, user_id
HAVING count(*) > 1 HAVING count(*) > 1
@ -575,14 +575,14 @@ ORDER BY
SELECT user_id SELECT user_id
FROM events_table FROM events_table
WHERE WHERE
event_type = 16 AND value_2 > 50 AND event_type = 3 AND value_2 > 2 AND
user_id IN user_id IN
(SELECT (SELECT
user_id user_id
FROM FROM
users_table users_table
WHERE WHERE
value_1 = 15 AND value_2 > 25 value_1 = 1 AND value_2 > 2
) )
ORDER BY 1; ORDER BY 1;
@ -592,7 +592,7 @@ ORDER BY 1;
SELECT SELECT
user_id, event_type FROM events_table user_id, event_type FROM events_table
WHERE WHERE
user_id in (SELECT user_id from events_table WHERE event_type > 500 and event_type < 505) user_id in (SELECT user_id from events_table WHERE event_type > 3 and event_type < 5)
GROUP BY GROUP BY
user_id, event_type user_id, event_type
ORDER BY 2 DESC, 1 ORDER BY 2 DESC, 1
@ -608,11 +608,11 @@ SELECT user_id FROM
FROM FROM
events_table events_table
WHERE WHERE
event_type = 901 event_type = 2
GROUP BY GROUP BY
user_id user_id
HAVING HAVING
count(*) > 3 count(*) > 1
) AS a ) AS a
ORDER BY ORDER BY
user_id; user_id;
@ -631,11 +631,11 @@ FROM
FROM FROM
users_table AS ma, events_table as short_list users_table AS ma, events_table as short_list
WHERE WHERE
short_list.user_id = ma.user_id and ma.value_1 < 50 and short_list.event_type < 50 short_list.user_id = ma.user_id and ma.value_1 < 2 and short_list.event_type < 2
) temp ) temp
ON users_table.user_id = temp.user_id ON users_table.user_id = temp.user_id
WHERE WHERE
users_table.value_1 < 50; users_table.value_1 < 2;
-- get some statistics from the aggregated results to ensure the results are correct -- get some statistics from the aggregated results to ensure the results are correct
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM assets; SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM assets;
@ -651,8 +651,8 @@ SELECT count(*) FROM
FROM FROM
users_table users_table
WHERE WHERE
(value_1 = '5' OR value_1 = '13') AND (value_1 = '1' OR value_1 = '3') AND
user_id NOT IN (select user_id from users_table where value_1 = '3') user_id NOT IN (select user_id from users_table where value_1 = '4')
GROUP BY GROUP BY
user_id user_id
HAVING HAVING
@ -667,7 +667,7 @@ SELECT subquery_count FROM
FROM FROM
users_table users_table
WHERE WHERE
(value_1 = '5' OR value_1 = '13') (value_1 = '1' OR value_1 = '3')
GROUP BY GROUP BY
user_id user_id
HAVING HAVING
@ -678,7 +678,7 @@ SELECT subquery_count FROM
FROM FROM
users_table users_table
WHERE WHERE
(value_1 = '3') (value_1 = '2')
GROUP BY GROUP BY
user_id) as b user_id) as b
ON a.user_id = b.user_id ON a.user_id = b.user_id
@ -696,7 +696,7 @@ FROM (
FROM FROM
users_table users_table
WHERE WHERE
(value_1 = '5' OR value_1 = '13') (value_1 = '1' OR value_1 = '3')
GROUP BY GROUP BY
user_id user_id
HAVING HAVING
@ -708,7 +708,7 @@ FROM (
FROM FROM
users_table users_table
WHERE WHERE
(value_1 = '3') (value_1 = '2')
GROUP BY GROUP BY
user_id) AS b user_id) AS b
ON a.user_id = b.user_id ON a.user_id = b.user_id
@ -739,7 +739,7 @@ FROM (
min(time) AS view_homepage_time min(time) AS view_homepage_time
FROM events_table FROM events_table
WHERE user_id = 1 and WHERE user_id = 1 and
event_type IN (10, 20, 30, 40, 50, 60, 70, 80, 90) event_type IN (1, 2)
GROUP BY user_id GROUP BY user_id
) e1 LEFT JOIN LATERAL ( ) e1 LEFT JOIN LATERAL (
SELECT SELECT
@ -749,7 +749,7 @@ FROM (
FROM events_table FROM events_table
WHERE WHERE
user_id = e1.user_id AND user_id = 1 and user_id = e1.user_id AND user_id = 1 and
event_type IN (11, 21, 31, 41, 51, 61, 71, 81, 91) event_type IN (2, 3)
ORDER BY time ORDER BY time
LIMIT 1 LIMIT 1
) e2 ON true LEFT JOIN LATERAL ( ) e2 ON true LEFT JOIN LATERAL (
@ -760,7 +760,7 @@ FROM (
FROM events_table FROM events_table
WHERE WHERE
user_id = e2.user_id AND user_id = 1 and user_id = e2.user_id AND user_id = 1 and
event_type IN (12, 22, 32, 42, 52, 62, 72, 82, 92) event_type IN (3, 4)
ORDER BY time ORDER BY time
LIMIT 1 LIMIT 1
) e3 ON true LEFT JOIN LATERAL ( ) e3 ON true LEFT JOIN LATERAL (
@ -771,7 +771,7 @@ FROM (
FROM events_table FROM events_table
WHERE WHERE
user_id = e3.user_id AND user_id = 1 and user_id = e3.user_id AND user_id = 1 and
event_type IN (13, 23, 33, 43, 53, 63, 73, 83, 93) event_type IN (4, 5)
ORDER BY time ORDER BY time
LIMIT 1 LIMIT 1
) e4 ON true LEFT JOIN LATERAL ( ) e4 ON true LEFT JOIN LATERAL (
@ -780,7 +780,7 @@ FROM (
FROM events_table FROM events_table
WHERE WHERE
user_id = e4.user_id AND user_id = 1 and user_id = e4.user_id AND user_id = 1 and
event_type IN (14, 24, 34, 44, 54, 64, 74, 84, 94) event_type IN (5, 6)
ORDER BY time ORDER BY time
LIMIT 1 LIMIT 1
) e5 ON true ) e5 ON true
@ -806,7 +806,7 @@ FROM (
min(time) AS view_homepage_time min(time) AS view_homepage_time
FROM events_table FROM events_table
WHERE WHERE
event_type IN (10, 20, 30, 40, 50, 60, 70, 80, 90) event_type IN (1, 2)
GROUP BY user_id GROUP BY user_id
) e1 LEFT JOIN LATERAL ( ) e1 LEFT JOIN LATERAL (
SELECT SELECT
@ -816,7 +816,7 @@ FROM (
FROM events_table FROM events_table
WHERE WHERE
user_id = e1.user_id AND user_id = e1.user_id AND
event_type IN (11, 21, 31, 41, 51, 61, 71, 81, 91) event_type IN (2, 3)
ORDER BY time ORDER BY time
) e2 ON true LEFT JOIN LATERAL ( ) e2 ON true LEFT JOIN LATERAL (
SELECT SELECT
@ -826,7 +826,7 @@ FROM (
FROM events_table FROM events_table
WHERE WHERE
user_id = e2.user_id AND user_id = e2.user_id AND
event_type IN (12, 22, 32, 42, 52, 62, 72, 82, 92) event_type IN (3, 4)
ORDER BY time ORDER BY time
) e3 ON true LEFT JOIN LATERAL ( ) e3 ON true LEFT JOIN LATERAL (
SELECT SELECT
@ -836,7 +836,7 @@ FROM (
FROM events_table FROM events_table
WHERE WHERE
user_id = e3.user_id AND user_id = e3.user_id AND
event_type IN (13, 23, 33, 43, 53, 63, 73, 83, 93) event_type IN (4, 5)
ORDER BY time ORDER BY time
) e4 ON true LEFT JOIN LATERAL ( ) e4 ON true LEFT JOIN LATERAL (
SELECT SELECT
@ -844,7 +844,7 @@ FROM (
FROM events_table FROM events_table
WHERE WHERE
user_id = e4.user_id AND user_id = e4.user_id AND
event_type IN (14, 24, 34, 44, 54, 64, 74, 84, 94) event_type IN (5, 6)
ORDER BY time ORDER BY time
) e5 ON true ) e5 ON true
GROUP BY e1.user_id GROUP BY e1.user_id
@ -867,7 +867,7 @@ FROM (
min(time) AS view_homepage_time min(time) AS view_homepage_time
FROM events_table FROM events_table
WHERE WHERE
event_type IN (10, 20, 30, 40, 50, 60, 70, 80, 90) event_type IN (1, 2)
GROUP BY user_id GROUP BY user_id
) e1 LEFT JOIN LATERAL ( ) e1 LEFT JOIN LATERAL (
SELECT SELECT
@ -877,7 +877,7 @@ FROM (
FROM events_table FROM events_table
WHERE WHERE
user_id = e1.user_id AND user_id = e1.user_id AND
event_type IN (11, 21, 31, 41, 51, 61, 71, 81, 91) event_type IN (2, 3)
ORDER BY time ORDER BY time
) e2 ON true LEFT JOIN LATERAL ( ) e2 ON true LEFT JOIN LATERAL (
SELECT SELECT
@ -887,7 +887,7 @@ FROM (
FROM events_table FROM events_table
WHERE WHERE
user_id = e2.user_id AND user_id = e2.user_id AND
event_type IN (12, 22, 32, 42, 52, 62, 72, 82, 92) event_type IN (3, 4)
ORDER BY time ORDER BY time
) e3 ON true LEFT JOIN LATERAL ( ) e3 ON true LEFT JOIN LATERAL (
SELECT SELECT
@ -897,7 +897,7 @@ FROM (
FROM events_table FROM events_table
WHERE WHERE
user_id = e3.user_id AND user_id = e3.user_id AND
event_type IN (13, 23, 33, 43, 53, 63, 73, 83, 93) event_type IN (4, 5)
ORDER BY time ORDER BY time
) e4 ON true LEFT JOIN LATERAL ( ) e4 ON true LEFT JOIN LATERAL (
SELECT SELECT
@ -905,7 +905,7 @@ FROM (
FROM events_table FROM events_table
WHERE WHERE
user_id = e4.user_id AND user_id = e4.user_id AND
event_type IN (14, 24, 34, 44, 54, 64, 74, 84, 94) event_type IN (5, 6)
ORDER BY time ORDER BY time
) e5 ON true ) e5 ON true
group by e1.user_id group by e1.user_id
@ -922,11 +922,11 @@ FROM (
FROM FROM
users_table users_table
WHERE WHERE
(value_1 > 5) (value_1 > 2)
GROUP BY GROUP BY
user_id user_id
HAVING HAVING
count(distinct value_1) > 88 count(distinct value_1) > 2
) as a ) as a
LEFT JOIN ( LEFT JOIN (
SELECT SELECT
@ -952,9 +952,9 @@ FROM (
FROM FROM
users_table users_table
WHERE WHERE
(value_1 > 5) (value_1 > 2)
GROUP BY user_id GROUP BY user_id
HAVING count(distinct value_1) > 88 HAVING count(distinct value_1) > 2
) as a ) as a
LEFT JOIN ( LEFT JOIN (
SELECT SELECT
@ -969,7 +969,7 @@ WHERE
GROUP BY GROUP BY
a.user_id a.user_id
HAVING HAVING
sum(b.value_3) > 50000 sum(b.value_3) > 5
ORDER BY ORDER BY
avg(b.value_3), 2, 1 avg(b.value_3), 2, 1
LIMIT 5; LIMIT 5;
@ -982,11 +982,11 @@ FROM
FROM FROM
users_table users_table
WHERE WHERE
(value_1 > 5) (value_1 > 2)
GROUP BY GROUP BY
user_id user_id
HAVING HAVING
count(distinct value_1) > 88 count(distinct value_1) > 2
) as a ) as a
LEFT JOIN LEFT JOIN
( (
@ -1026,7 +1026,7 @@ FROM
LEFT OUTER JOIN events_table e2 LEFT OUTER JOIN events_table e2
ON e2.user_id = sub.user_id ON e2.user_id = sub.user_id
WHERE WHERE
e2.value_2 > 10 AND e2.value_2 < 50 AND u.value_2 > 10 AND u.value_2 < 50 e2.value_2 > 1 AND e2.value_2 < 5 AND u.value_2 > 1 AND u.value_2 < 5
GROUP BY GROUP BY
u.user_id, sub.value_2, sub.value_3 u.user_id, sub.value_2, sub.value_3
ORDER BY ORDER BY
@ -1103,11 +1103,11 @@ FROM
FROM FROM
users_table users_table
WHERE WHERE
(value_1 > 5) (value_1 > 2)
GROUP BY GROUP BY
user_id user_id
HAVING HAVING
count(distinct value_1) > 88 count(distinct value_1) > 2
) as a ) as a
LEFT JOIN LEFT JOIN
(SELECT (SELECT
@ -1134,11 +1134,11 @@ FROM
FROM FROM
users_table users_table
WHERE WHERE
(value_1 > 5) (value_1 > 2)
GROUP BY GROUP BY
user_id user_id
HAVING HAVING
count(distinct value_1) > 88 count(distinct value_1) > 2
) as a ) as a
LEFT JOIN LEFT JOIN
(SELECT (SELECT
@ -1160,11 +1160,11 @@ FROM
FROM FROM
users_table users_table
WHERE WHERE
(value_1 > 5) (value_1 > 2)
GROUP BY GROUP BY
user_id user_id
HAVING HAVING
count(distinct value_1) > 88 count(distinct value_1) > 2
) as a ) as a
LEFT JOIN LEFT JOIN
(SELECT (SELECT
@ -1201,19 +1201,19 @@ FROM
FROM FROM
users_table AS ma users_table AS ma
WHERE WHERE
(ma.value_2 > 100) (ma.value_2 > 1)
ORDER BY ORDER BY
prob DESC, user_id DESC prob DESC, value_2 DESC, user_id DESC
LIMIT 10 LIMIT 10
) AS ma ) AS ma
ON (a.a_user_id = ma.user_id) ON (a.a_user_id = ma.user_id)
) AS inner_sub ) AS inner_sub
ORDER BY ORDER BY
prob DESC, user_id DESC prob DESC, value_2 DESC, user_id DESC, event_type DESC
LIMIT 10 LIMIT 10
) AS outer_sub ) AS outer_sub
ORDER BY ORDER BY
prob DESC, event_type DESC, user_id DESC prob DESC, value_2 DESC, user_id DESC, event_type DESC
LIMIT 10; LIMIT 10;
-- very similar query but produces different result due to -- very similar query but produces different result due to
@ -1231,7 +1231,7 @@ FROM
FROM FROM
users_table AS ma users_table AS ma
WHERE WHERE
(ma.value_2 > 100) (ma.value_2 > 1)
ORDER BY ORDER BY
prob DESC, user_id DESC prob DESC, user_id DESC
LIMIT 10 LIMIT 10
@ -1259,7 +1259,7 @@ FROM
FROM FROM
users_table AS ma users_table AS ma
WHERE WHERE
(ma.value_2 > 100) (ma.value_2 > 1)
ORDER BY ORDER BY
prob DESC, user_id DESC prob DESC, user_id DESC
LIMIT 10 LIMIT 10
@ -1332,7 +1332,7 @@ FROM
FROM FROM
events_table events_table
WHERE WHERE
event_type = ANY(ARRAY [10, 11, 12]) event_type = ANY(ARRAY [4, 5, 6])
ORDER BY ORDER BY
value_3 ASC, user_id_ck DESC, array_index(ARRAY [1, 2, 3], (value_2 % 3)) ASC value_3 ASC, user_id_ck DESC, array_index(ARRAY [1, 2, 3], (value_2 % 3)) ASC
LIMIT 10 ) LIMIT 10 )
@ -1406,7 +1406,7 @@ FROM
FROM FROM
events_table events_table
WHERE WHERE
event_type = ANY(ARRAY [10, 11, 12]) event_type = ANY(ARRAY [4, 5, 6])
ORDER BY ORDER BY
value_3 ASC, user_id_ck DESC, array_index(ARRAY [1, 2, 3], (value_2 % 3)) ASC value_3 ASC, user_id_ck DESC, array_index(ARRAY [1, 2, 3], (value_2 % 3)) ASC
LIMIT 10 LIMIT 10
@ -1440,7 +1440,7 @@ FROM (
FROM FROM
users_table users_table
WHERE WHERE
(value_1 = '5' OR value_1 = '13') (value_1 = '1' OR value_1 = '3')
GROUP BY user_id GROUP BY user_id
HAVING count(distinct value_1) = 2 HAVING count(distinct value_1) = 2
) as a ) as a
@ -1460,7 +1460,7 @@ FROM (
FROM FROM
users_table users_table
WHERE WHERE
(value_1 = '5' OR value_1 = '13') (value_1 = '1' OR value_1 = '3')
GROUP BY user_id GROUP BY user_id
HAVING count(distinct value_1) = 2 HAVING count(distinct value_1) = 2
) as a ) as a
@ -1479,7 +1479,7 @@ FROM (SELECT
FROM FROM
users_table users_table
WHERE WHERE
(value_1 = '5' OR value_1 = '13' ) (value_1 = '1' OR value_1 = '3' )
GROUP BY GROUP BY
user_id user_id
HAVING HAVING
@ -1508,12 +1508,12 @@ FROM (
users_table AS u, users_table AS u,
events_table AS e events_table AS e
WHERE WHERE
u.user_id = e.user_id AND e.event_type IN (100, 101, 102) u.user_id = e.user_id AND e.event_type IN (1, 2)
) t ) t
GROUP BY user_id GROUP BY user_id
) q ) q
ORDER BY 2 DESC, 1 ORDER BY 2 DESC, 1
LIMIT 3+3 OFFSET 5::smallint; LIMIT 1+1 OFFSET 1::smallint;
-- now, lets use implicit coersion in LIMIT and a simple expressions on OFFSET -- now, lets use implicit coersion in LIMIT and a simple expressions on OFFSET
SELECT user_id, array_length(events_table, 1) SELECT user_id, array_length(events_table, 1)
@ -1526,17 +1526,17 @@ FROM (
users_table AS u, users_table AS u,
events_table AS e events_table AS e
WHERE WHERE
u.user_id = e.user_id AND e.event_type IN (100, 101, 102) u.user_id = e.user_id AND e.event_type IN (1, 2)
) t ) t
GROUP BY user_id GROUP BY user_id
) q ) q
ORDER BY 2 DESC, 1 ORDER BY 2 DESC, 1
LIMIT '3' OFFSET 27+2; LIMIT '3' OFFSET 2+1;
-- create a test function which is marked as volatile -- create a test function which is marked as volatile
CREATE OR REPLACE FUNCTION volatile_func_test() CREATE OR REPLACE FUNCTION volatile_func_test()
RETURNS INT AS $$ RETURNS INT AS $$
SELECT 5; SELECT 1;
$$ LANGUAGE sql VOLATILE; $$ LANGUAGE sql VOLATILE;
-- Citus should be able to evalute functions/row comparisons on the LIMIT/OFFSET -- Citus should be able to evalute functions/row comparisons on the LIMIT/OFFSET
@ -1550,7 +1550,7 @@ FROM (
users_table AS u, users_table AS u,
events_table AS e events_table AS e
WHERE WHERE
u.user_id = e.user_id AND e.event_type IN (100, 101, 102) u.user_id = e.user_id AND e.event_type IN (1, 2, 3, 4)
) t ) t
GROUP BY user_id GROUP BY user_id
) q ) q
@ -1568,7 +1568,7 @@ FROM (
users_table AS u, users_table AS u,
events_table AS e events_table AS e
WHERE WHERE
u.user_id = e.user_id AND e.event_type IN (100, 101, 102) u.user_id = e.user_id AND e.event_type IN (1, 2)
) t ) t
GROUP BY user_id GROUP BY user_id
) q ) q
@ -1577,7 +1577,7 @@ LIMIT (5 > 4)::int OFFSET
CASE CASE
WHEN 5 != 5 THEN 27 WHEN 5 != 5 THEN 27
WHEN 1 > 5 THEN 28 WHEN 1 > 5 THEN 28
ELSE 29 ELSE 2
END; END;
-- we don't allow parameters on the LIMIT/OFFSET clauses -- we don't allow parameters on the LIMIT/OFFSET clauses
@ -1590,14 +1590,14 @@ FROM (
FROM users_table AS u, FROM users_table AS u,
events_table AS e events_table AS e
WHERE u.user_id = e.user_id WHERE u.user_id = e.user_id
AND e.event_type IN (100, 101, 102) AND e.event_type IN (1, 2)
) t ) t
GROUP BY user_id GROUP BY user_id
) q ) q
ORDER BY 2 DESC, 1 ORDER BY 2 DESC, 1
LIMIT $1 OFFSET $2; LIMIT $1 OFFSET $2;
EXECUTE parametrized_limit(3,3); EXECUTE parametrized_limit(1,1);
PREPARE parametrized_offset AS PREPARE parametrized_offset AS
SELECT user_id, array_length(events_table, 1) SELECT user_id, array_length(events_table, 1)
@ -1608,14 +1608,14 @@ FROM (
FROM users_table AS u, FROM users_table AS u,
events_table AS e events_table AS e
WHERE u.user_id = e.user_id WHERE u.user_id = e.user_id
AND e.event_type IN (100, 101, 102) AND e.event_type IN (1, 2)
) t ) t
GROUP BY user_id GROUP BY user_id
) q ) q
ORDER BY 2 DESC, 1 ORDER BY 2 DESC, 1
LIMIT 3 OFFSET $1; LIMIT 1 OFFSET $1;
EXECUTE parametrized_offset(3); EXECUTE parametrized_offset(1);
SET client_min_messages TO DEFAULT; SET client_min_messages TO DEFAULT;
DROP FUNCTION volatile_func_test(); DROP FUNCTION volatile_func_test();
@ -1663,11 +1663,11 @@ FROM
FROM FROM
users_table AS ma, events_table as short_list users_table AS ma, events_table as short_list
WHERE WHERE
short_list.user_id = ma.user_id and ma.value_1 < 50 and short_list.event_type < 50 short_list.user_id = ma.user_id and ma.value_1 < 3 and short_list.event_type < 3
) temp ) temp
ON users_table.user_id = temp.user_id ON users_table.user_id = temp.user_id
WHERE WHERE
users_table.value_1 < 50 AND test_join_function_2(users_table.user_id, temp.user_id); users_table.value_1 < 3 AND test_join_function_2(users_table.user_id, temp.user_id);
-- we do support the following since there is already an equality on the partition -- we do support the following since there is already an equality on the partition
-- key and we have an additional join via a function -- key and we have an additional join via a function
@ -1681,12 +1681,12 @@ FROM
FROM FROM
users_table AS ma, events_table as short_list users_table AS ma, events_table as short_list
WHERE WHERE
short_list.user_id = ma.user_id and ma.value_1 < 50 and short_list.event_type < 100 AND short_list.user_id = ma.user_id and ma.value_1 < 3 and short_list.event_type < 4 AND
test_join_function_2(ma.value_1, short_list.value_2) test_join_function_2(ma.value_1, short_list.value_2)
) temp ) temp
ON users_table.user_id = temp.user_id ON users_table.user_id = temp.user_id
WHERE WHERE
users_table.value_1 < 50 users_table.value_1 < 3
ORDER BY 2 DESC, 1 DESC ORDER BY 2 DESC, 1 DESC
LIMIT 10; LIMIT 10;
@ -1702,7 +1702,7 @@ FROM
WHERE WHERE
events_table.user_id = users_table.user_id AND events_table.user_id = users_table.user_id AND
events_table.time > users_table.time AND events_table.time > users_table.time AND
events_table.value_2 IN (10, 100) events_table.value_2 IN (0, 4)
) as foo; ) as foo;
-- the other way around is not supported -- the other way around is not supported
@ -1716,7 +1716,7 @@ FROM
WHERE WHERE
events_table.user_id > users_table.user_id AND events_table.user_id > users_table.user_id AND
events_table.time = users_table.time AND events_table.time = users_table.time AND
events_table.value_2 IN (10, 100) events_table.value_2 IN (0, 4)
) as foo; ) as foo;
-- we can even allow that on top level joins -- we can even allow that on top level joins
@ -1729,7 +1729,7 @@ FROM
events_table, users_table events_table, users_table
WHERE WHERE
events_table.user_id = users_table.user_id AND events_table.user_id = users_table.user_id AND
events_table.value_2 IN (10, 100) events_table.value_2 IN (0, 4)
) as foo, ) as foo,
(SELECT (SELECT
event_type, random(), events_table.user_id event_type, random(), events_table.user_id
@ -1737,7 +1737,7 @@ FROM
events_table, users_table events_table, users_table
WHERE WHERE
events_table.user_id = users_table.user_id AND events_table.user_id = users_table.user_id AND
events_table.value_2 IN (20, 200) events_table.value_2 IN (1, 5)
) as bar ) as bar
WHERE foo.event_type > bar.event_type WHERE foo.event_type > bar.event_type
AND foo.user_id = bar.user_id; AND foo.user_id = bar.user_id;
@ -1753,7 +1753,7 @@ FROM
events_table, users_table events_table, users_table
WHERE WHERE
events_table.user_id = users_table.user_id AND events_table.user_id = users_table.user_id AND
events_table.value_2 IN (10, 100) events_table.value_2 IN (0, 4)
) as foo, ) as foo,
(SELECT (SELECT
event_type, random() event_type, random()
@ -1761,7 +1761,7 @@ FROM
events_table, users_table events_table, users_table
WHERE WHERE
events_table.user_id = users_table.user_id AND events_table.user_id = users_table.user_id AND
events_table.value_2 IN (20, 200) events_table.value_2 IN (1, 5)
) as bar ) as bar
WHERE foo.event_type = bar.event_type; WHERE foo.event_type = bar.event_type;
@ -1776,10 +1776,10 @@ FROM
FROM FROM
users_table AS ma, events_table as short_list users_table AS ma, events_table as short_list
WHERE WHERE
short_list.user_id = ma.user_id and ma.value_1 < 50 and short_list.event_type < 3 short_list.user_id = ma.user_id and ma.value_1 < 3 and short_list.event_type < 3
) temp ) temp
ON users_ids.user_id = temp.user_id ON users_ids.user_id = temp.user_id
WHERE temp.value_1 < 50 WHERE temp.value_1 < 3
ORDER BY 1 ORDER BY 1
LIMIT 5; LIMIT 5;
@ -1794,10 +1794,10 @@ FROM
FROM FROM
users_table AS ma, events_table as short_list users_table AS ma, events_table as short_list
WHERE WHERE
short_list.user_id = ma.user_id and ma.value_1 < 50 and short_list.event_type < 15 short_list.user_id = ma.user_id and ma.value_1 < 3 and short_list.event_type < 2
) temp ) temp
ON users_ids.user_id = temp.user_id ON users_ids.user_id = temp.user_id
WHERE temp.value_1 < 50 WHERE temp.value_1 < 3
ORDER BY 1, 2 ORDER BY 1, 2
LIMIT 5; LIMIT 5;
@ -1812,7 +1812,7 @@ FROM
FROM FROM
users_table AS ma, events_table as short_list users_table AS ma, events_table as short_list
WHERE WHERE
short_list.user_id = ma.user_id and ma.value_1 < 25 and short_list.event_type < 3 short_list.user_id = ma.user_id and ma.value_1 < 2 and short_list.event_type < 3
) temp ) temp
ON users_ids.user_id = temp.user_id ON users_ids.user_id = temp.user_id
ORDER BY 1,2 ORDER BY 1,2

File diff suppressed because it is too large Load Diff

View File

@ -258,14 +258,14 @@ SELECT * FROM
FROM FROM
events_reference_table as "events" events_reference_table as "events"
WHERE WHERE
event_type > 80) as "temp_data_queries" event_type > 2) as "temp_data_queries"
INNER JOIN INNER JOIN
(SELECT (SELECT
"users"."user_id" "users"."user_id"
FROM FROM
users_table as "users" users_table as "users"
WHERE WHERE
user_id > 80 and value_2 = 5) as foo_in ON (event_val_2 = user_id)) as foo LEFT JOIN user_id > 2 and value_2 = 1) as foo_in ON (event_val_2 = user_id)) as foo LEFT JOIN
(SELECT user_id as user_user_id FROM users_table) as fooo ON (user_id = user_user_id)) as bar; (SELECT user_id as user_user_id FROM users_table) as fooo ON (user_id = user_user_id)) as bar;
-- the same query but this time reference table is in the outer part of the query -- the same query but this time reference table is in the outer part of the query
@ -278,14 +278,14 @@ SELECT * FROM
FROM FROM
events_reference_table as "events" events_reference_table as "events"
WHERE WHERE
event_type > 80) as "temp_data_queries" event_type > 2) as "temp_data_queries"
LEFT JOIN LEFT JOIN
(SELECT (SELECT
"users"."user_id" "users"."user_id"
FROM FROM
users_table as "users" users_table as "users"
WHERE WHERE
user_id > 80 and value_2 = 5) as foo_in ON (event_val_2 = user_id)) as foo LEFT JOIN user_id > 2 and value_2 = 1) as foo_in ON (event_val_2 = user_id)) as foo LEFT JOIN
(SELECT user_id as user_user_id FROM users_table) as fooo ON (user_id = user_user_id)) as bar; (SELECT user_id as user_user_id FROM users_table) as fooo ON (user_id = user_user_id)) as bar;
-- we could even suuport the following where the subquery -- we could even suuport the following where the subquery
@ -332,14 +332,14 @@ FROM
FROM FROM
users_reference_table as "users" users_reference_table as "users"
WHERE WHERE
user_id > 12 and user_id < 16 and value_1 > 20) user_where_1_1 user_id > 0 and user_id < 5 and value_1 > 1) user_where_1_1
INNER JOIN INNER JOIN
(SELECT (SELECT
"users"."user_id" "users"."user_id"
FROM FROM
users_reference_table as "users" users_reference_table as "users"
WHERE WHERE
user_id > 12 and user_id < 16 and value_2 > 60) user_where_1_join_1 user_id > 0 and user_id < 5 and value_2 > 2) user_where_1_join_1
ON ("user_where_1_1".user_id = "user_where_1_join_1".user_id)) ON ("user_where_1_1".user_id = "user_where_1_join_1".user_id))
filter_users_1 filter_users_1
JOIN LATERAL JOIN LATERAL
@ -348,7 +348,7 @@ FROM
FROM FROM
events_reference_table as "events" events_reference_table as "events"
WHERE WHERE
user_id > 12 and user_id < 16 AND user_id > 0 and user_id < 5 AND
user_id = filter_users_1.user_id user_id = filter_users_1.user_id
ORDER BY ORDER BY
time DESC time DESC
@ -364,7 +364,7 @@ FROM
users_reference_table as "users" users_reference_table as "users"
WHERE WHERE
"users"."user_id" = "some_recent_users"."user_id" AND "users"."user_id" = "some_recent_users"."user_id" AND
"users"."value_2" > 70 "users"."value_2" > 2
LIMIT 1) "some_users_data" LIMIT 1) "some_users_data"
ON TRUE ON TRUE
ORDER BY ORDER BY
@ -393,7 +393,7 @@ SELECT
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
user_id > 10 and user_id < 40 AND event_type IN (40, 41, 42, 43, 44, 45) ) "temp_data_queries" user_id > 0 and user_id < 4 AND event_type IN (4, 5) ) "temp_data_queries"
INNER JOIN INNER JOIN
(SELECT (SELECT
user_where_1_1.real_user_id user_where_1_1.real_user_id
@ -403,14 +403,14 @@ SELECT
FROM FROM
users_reference_table as "users" users_reference_table as "users"
WHERE WHERE
user_id > 10 and user_id < 40 and value_2 > 50 ) user_where_1_1 user_id > 0 and user_id < 4 and value_2 > 3 ) user_where_1_1
INNER JOIN INNER JOIN
(SELECT (SELECT
"users"."user_id" "users"."user_id"
FROM FROM
users_reference_table as "users" users_reference_table as "users"
WHERE WHERE
user_id > 10 and user_id < 40 and value_3 > 50 ) user_where_1_join_1 user_id > 0 and user_id < 4 and value_3 > 3 ) user_where_1_join_1
ON ("user_where_1_1".real_user_id = "user_where_1_join_1".user_id)) "user_filters_1" ON ("user_where_1_1".real_user_id = "user_where_1_join_1".user_id)) "user_filters_1"
ON ("temp_data_queries".user_id = "user_filters_1".real_user_id)) "eventQuery") "pushedDownQuery") "pushedDownQuery" ON ("temp_data_queries".user_id = "user_filters_1".real_user_id)) "eventQuery") "pushedDownQuery") "pushedDownQuery"
GROUP BY GROUP BY
@ -439,7 +439,7 @@ FROM
FROM FROM
users_reference_table as "users" users_reference_table as "users"
WHERE WHERE
user_id > 10 and user_id < 40 and value_2 > 30 user_id > 1 and user_id < 4 and value_2 > 2
) simple_user_where_1 ) simple_user_where_1
) all_buckets_1 ) all_buckets_1
) users_in_segment_1 ) users_in_segment_1
@ -449,7 +449,7 @@ FROM
FROM FROM
users_reference_table as "users" users_reference_table as "users"
WHERE WHERE
user_id > 10 and user_id < 40 and value_2 > 60 user_id > 1 and user_id < 4 and value_2 > 3
) some_users_data ) some_users_data
ON ("users_in_segment_1".user_id = "some_users_data".user_id) ON ("users_in_segment_1".user_id = "some_users_data".user_id)
) segmentalias_1) "tempQuery" ) segmentalias_1) "tempQuery"
@ -470,14 +470,14 @@ FROM
FROM FROM
users_reference_table as "users" users_reference_table as "users"
WHERE WHERE
user_id > 20 and user_id < 70 and users.value_2 = 200) filter_users_1 user_id > 2 and user_id < 5 and users.value_2 = 3) filter_users_1
JOIN LATERAL JOIN LATERAL
(SELECT (SELECT
user_id, value_3 user_id, value_3
FROM FROM
events_reference_table as "events" events_reference_table as "events"
WHERE WHERE
user_id > 20 and user_id < 70 AND user_id > 2 and user_id < 5 AND
("events".user_id = "filter_users_1".user_id) ("events".user_id = "filter_users_1".user_id)
ORDER BY ORDER BY
value_3 DESC value_3 DESC
@ -491,7 +491,7 @@ FROM
users_reference_table as "users" users_reference_table as "users"
WHERE WHERE
"users"."user_id" = "some_recent_users"."user_id" AND "users"."user_id" = "some_recent_users"."user_id" AND
users.value_2 > 200 users.value_2 > 3
LIMIT 1) "some_users_data" ON true LIMIT 1) "some_users_data" ON true
ORDER BY ORDER BY
value_3 DESC value_3 DESC
@ -519,14 +519,14 @@ count(*) AS cnt, "generated_group_field"
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
user_id > 80) "temp_data_queries" user_id > 4) "temp_data_queries"
INNER JOIN INNER JOIN
(SELECT (SELECT
"users"."user_id" "users"."user_id"
FROM FROM
users_reference_table as "users" users_reference_table as "users"
WHERE WHERE
user_id > 80 and value_2 = 5) "user_filters_1" user_id > 2 and value_2 = 5) "user_filters_1"
ON ("temp_data_queries".event_user_id = "user_filters_1".user_id)) AS "multi_group_wrapper_1" ON ("temp_data_queries".event_user_id = "user_filters_1".user_id)) AS "multi_group_wrapper_1"
LEFT JOIN LEFT JOIN
(SELECT (SELECT
@ -558,14 +558,14 @@ count(*) AS cnt, "generated_group_field"
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
user_id > 80) "temp_data_queries" user_id > 2) "temp_data_queries"
INNER JOIN INNER JOIN
(SELECT (SELECT
"users"."user_id" "users"."user_id"
FROM FROM
users_table as "users" users_table as "users"
WHERE WHERE
user_id > 80 and value_2 = 5) "user_filters_1" user_id > 2 and value_2 = 5) "user_filters_1"
ON ("temp_data_queries".event_user_id = "user_filters_1".user_id)) AS "multi_group_wrapper_1" ON ("temp_data_queries".event_user_id = "user_filters_1".user_id)) AS "multi_group_wrapper_1"
RIGHT JOIN RIGHT JOIN
(SELECT (SELECT
@ -593,17 +593,17 @@ FROM (
FROM users_table AS u, FROM users_table AS u,
events_reference_table AS e events_reference_table AS e
WHERE u.user_id > e.user_id WHERE u.user_id > e.user_id
AND u.user_id >= 10 AND u.user_id >= 1
AND u.user_id <= 25 AND u.user_id <= 3
AND e.event_type IN (100, 101, 102) AND e.event_type IN (1, 2)
) )
) t1 RIGHT JOIN ( ) t1 RIGHT JOIN (
SELECT DISTINCT user_id, SELECT DISTINCT user_id,
'Has done event'::TEXT AS hasdone_event 'Has done event'::TEXT AS hasdone_event
FROM events_table AS e FROM events_table AS e
WHERE e.user_id >= 10 WHERE e.user_id >= 1
AND e.user_id <= 25 AND e.user_id <= 3
AND e.event_type IN (106, 107, 108) AND e.event_type IN (3, 4)
) t2 ON (t1.user_id = t2.user_id) ) t2 ON (t1.user_id = t2.user_id)
GROUP BY t1.user_id, hasdone_event GROUP BY t1.user_id, hasdone_event
) t GROUP BY user_id, hasdone_event ) t GROUP BY user_id, hasdone_event
@ -622,17 +622,17 @@ FROM (
FROM users_table AS u, FROM users_table AS u,
events_reference_table AS e events_reference_table AS e
WHERE u.value_1 > e.user_id WHERE u.value_1 > e.user_id
AND u.user_id >= 10 AND u.user_id >= 1
AND u.user_id <= 25 AND u.user_id <= 3
AND e.event_type >= 125 AND e.event_type < 130 AND e.event_type >= 2 AND e.event_type < 3
) )
) t1 RIGHT JOIN ( ) t1 RIGHT JOIN (
SELECT DISTINCT user_id, SELECT DISTINCT user_id,
'Has done event'::TEXT AS hasdone_event 'Has done event'::TEXT AS hasdone_event
FROM events_table AS e FROM events_table AS e
WHERE e.user_id >= 10 WHERE e.user_id >= 1
AND e.user_id <= 25 AND e.user_id <= 3
AND e.event_type >= 130 AND e.event_type < 135 AND e.event_type >= 3 AND e.event_type < 4
) t2 ON (t1.user_id = t2.user_id) ) t2 ON (t1.user_id = t2.user_id)
GROUP BY t1.user_id, hasdone_event GROUP BY t1.user_id, hasdone_event
) t GROUP BY user_id, hasdone_event ) t GROUP BY user_id, hasdone_event
@ -658,14 +658,14 @@ count(*) AS cnt, "generated_group_field"
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
user_id > 80) "temp_data_queries" user_id > 2) "temp_data_queries"
INNER JOIN INNER JOIN
(SELECT (SELECT
"users"."user_id" "users"."user_id"
FROM FROM
users_reference_table as "users" users_reference_table as "users"
WHERE WHERE
user_id > 80 and value_2 = 5) "user_filters_1" user_id > 2 and value_2 = 5) "user_filters_1"
ON ("temp_data_queries".event_user_id < "user_filters_1".user_id)) AS "multi_group_wrapper_1" ON ("temp_data_queries".event_user_id < "user_filters_1".user_id)) AS "multi_group_wrapper_1"
RIGHT JOIN RIGHT JOIN
(SELECT (SELECT
@ -691,7 +691,7 @@ FROM
FROM FROM
events_reference_table as "events" events_reference_table as "events"
WHERE WHERE
user_id > 10 and user_id < 40) "events_1" user_id > 1 and user_id < 4) "events_1"
ORDER BY ORDER BY
time DESC) "recent_events_1" time DESC) "recent_events_1"
GROUP BY GROUP BY
@ -704,7 +704,7 @@ FROM
FROM FROM
users_table as "users" users_table as "users"
WHERE WHERE
users.value_2 > 50 and users.value_2 < 55) "some_users_data" users.value_2 > 2 and users.value_2 < 4) "some_users_data"
ON "some_users_data"."user_id" = "some_recent_users"."user_id" ON "some_users_data"."user_id" = "some_recent_users"."user_id"
ORDER BY ORDER BY
user_id user_id
@ -729,7 +729,7 @@ FROM
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (10, 11, 12, 13, 14, 15) ) events_subquery_1) event_type IN (1, 2) ) events_subquery_1)
UNION UNION
(SELECT (SELECT
* *
@ -739,7 +739,7 @@ FROM
FROM FROM
events_reference_table as "events" events_reference_table as "events"
WHERE WHERE
event_type IN (15, 16, 17, 18, 19) ) events_subquery_2) event_type IN (3, 4) ) events_subquery_2)
UNION UNION
(SELECT (SELECT
* *
@ -749,7 +749,7 @@ FROM
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (20, 21, 22, 23, 24, 25) ) events_subquery_3) event_type IN (5, 6) ) events_subquery_3)
UNION UNION
(SELECT (SELECT
* *
@ -759,7 +759,7 @@ FROM
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (26, 27, 28, 29, 30, 13)) events_subquery_4)) t1 event_type IN (1, 6)) events_subquery_4)) t1
GROUP BY "t1"."user_id") AS t) "q" GROUP BY "t1"."user_id") AS t) "q"
INNER JOIN INNER JOIN
(SELECT (SELECT
@ -767,7 +767,7 @@ INNER JOIN
FROM FROM
users_table as "users" users_table as "users"
WHERE WHERE
value_1 > 50 and value_1 < 70) AS t value_1 > 2 and value_1 < 4) AS t
ON (t.user_id = q.user_id)) as final_query ON (t.user_id = q.user_id)) as final_query
ORDER BY ORDER BY
types; types;
@ -792,7 +792,7 @@ FROM
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (10, 11, 12, 13, 14, 15) ) events_subquery_1) event_type IN (1, 2) ) events_subquery_1)
UNION UNION
(SELECT * (SELECT *
FROM FROM
@ -807,7 +807,7 @@ FROM
events_reference_table as "events", users_table as "users" events_reference_table as "events", users_table as "users"
WHERE WHERE
events.user_id = users.user_id AND events.user_id = users.user_id AND
event_type IN (10, 11, 12, 13, 14, 15) event_type IN (1, 2)
GROUP BY "users"."user_id" GROUP BY "users"."user_id"
) as events_subquery_5 ) as events_subquery_5
) events_subquery_2) ) events_subquery_2)
@ -819,7 +819,7 @@ FROM
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (20, 21, 22, 23, 24, 25) ) events_subquery_3) event_type IN (3, 4) ) events_subquery_3)
UNION UNION
(SELECT * (SELECT *
FROM FROM
@ -828,7 +828,7 @@ FROM
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (26, 27, 28, 29, 30, 13)) events_subquery_4) event_type IN (5, 6)) events_subquery_4)
) t1 ) t1
GROUP BY "t1"."user_id") AS t) "q" GROUP BY "t1"."user_id") AS t) "q"
INNER JOIN INNER JOIN
@ -837,7 +837,7 @@ INNER JOIN
FROM FROM
users_table as "users" users_table as "users"
WHERE WHERE
value_1 > 50 and value_1 < 70) AS t value_1 > 2 and value_1 < 4) AS t
ON (t.user_id = q.user_id)) as final_query ON (t.user_id = q.user_id)) as final_query
GROUP BY GROUP BY
types types
@ -862,7 +862,7 @@ FROM
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (10, 11, 12, 13, 14, 15) ) events_subquery_1) event_type IN (1, 2) ) events_subquery_1)
UNION ALL UNION ALL
(SELECT * (SELECT *
FROM FROM
@ -871,7 +871,7 @@ FROM
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (15, 16, 17, 18, 19) ) events_subquery_2) event_type IN (3, 4) ) events_subquery_2)
UNION ALL UNION ALL
(SELECT * (SELECT *
FROM FROM
@ -880,7 +880,7 @@ FROM
FROM FROM
events_reference_table as "events" events_reference_table as "events"
WHERE WHERE
event_type IN (20, 21, 22, 23, 24, 25) ) events_subquery_3) event_type IN (5, 6) ) events_subquery_3)
UNION ALL UNION ALL
(SELECT * (SELECT *
FROM FROM
@ -889,12 +889,12 @@ FROM
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (26, 27, 28, 29, 30, 13)) events_subquery_4)) t1 event_type IN (1, 6)) events_subquery_4)) t1
GROUP BY "t1"."user_id") AS t) "q" GROUP BY "t1"."user_id") AS t) "q"
INNER JOIN INNER JOIN
(SELECT "users"."user_id" (SELECT "users"."user_id"
FROM users_table as "users" FROM users_table as "users"
WHERE value_1 > 50 and value_1 < 70) AS t ON (t.user_id = q.user_id)) as final_query WHERE value_1 > 2 and value_1 < 4) AS t ON (t.user_id = q.user_id)) as final_query
GROUP BY types GROUP BY types
ORDER BY types; ORDER BY types;
@ -930,14 +930,14 @@ count(*) AS cnt, "generated_group_field"
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
user_id > 80) "temp_data_queries" user_id > 2) "temp_data_queries"
INNER JOIN INNER JOIN
(SELECT (SELECT
"users"."user_id" "users"."user_id"
FROM FROM
users_reference_table as "users" users_reference_table as "users"
WHERE WHERE
user_id > 80 and value_2 = 5) "user_filters_1" user_id > 2 and value_2 = 5) "user_filters_1"
ON ("temp_data_queries".event_user_id < "user_filters_1".user_id)) AS "multi_group_wrapper_1" ON ("temp_data_queries".event_user_id < "user_filters_1".user_id)) AS "multi_group_wrapper_1"
RIGHT JOIN RIGHT JOIN
(SELECT (SELECT
@ -965,8 +965,8 @@ WHERE
FROM FROM
events_reference_table as e2 events_reference_table as e2
WHERE WHERE
value_2 = 15 AND value_2 = 1 AND
value_3 > 25 AND value_3 > 3 AND
e1.value_2 > e2.value_2 e1.value_2 > e2.value_2
) )
AND u1.user_id > e1.user_id AND u1.user_id > e1.user_id
@ -977,7 +977,7 @@ LIMIT 5;
SELECT foo.user_id FROM SELECT foo.user_id FROM
( (
SELECT m.user_id, random() FROM users_table m JOIN events_reference_table r ON int4eq(m.user_id, r.user_id) SELECT m.user_id, random() FROM users_table m JOIN events_reference_table r ON int4eq(m.user_id, r.user_id)
WHERE event_type > 100000 WHERE event_type > 100
) as foo; ) as foo;
-- not supported since group by is on the reference table column -- not supported since group by is on the reference table column

View File

@ -17,12 +17,12 @@ WHERE
FROM FROM
events_table events_table
WHERE WHERE
users_table.user_id = events_table.user_id AND event_type = 50 users_table.user_id = events_table.user_id AND event_type = 1
GROUP BY GROUP BY
user_id user_id
) )
GROUP BY user_id GROUP BY user_id
HAVING count(*) > 66 HAVING count(*) > 2
ORDER BY user_id ORDER BY user_id
LIMIT 5; LIMIT 5;
@ -38,13 +38,13 @@ WHERE
FROM FROM
events_table events_table
WHERE WHERE
users_table.user_id = events_table.user_id AND event_type = 50 AND users_table.user_id = events_table.user_id AND event_type = 1 AND
users_table.time > events_table.time users_table.time > events_table.time
GROUP BY GROUP BY
user_id user_id
) )
GROUP BY user_id GROUP BY user_id
HAVING count(*) > 66 HAVING count(*) > 1
ORDER BY user_id ORDER BY user_id
LIMIT 5; LIMIT 5;
@ -60,13 +60,13 @@ WHERE
FROM FROM
events_table events_table
WHERE WHERE
users_table.user_id > events_table.user_id AND event_type = 50 AND users_table.user_id > events_table.user_id AND event_type = 1 AND
users_table.time = events_table.time users_table.time = events_table.time
GROUP BY GROUP BY
user_id user_id
) )
GROUP BY user_id GROUP BY user_id
HAVING count(*) > 66 HAVING count(*) > 1
ORDER BY user_id ORDER BY user_id
LIMIT 5; LIMIT 5;
@ -76,7 +76,7 @@ SELECT
FROM FROM
users_table users_table
WHERE WHERE
value_2 > 545 AND value_2 > 1 AND
value_2 < ALL (SELECT avg(value_3) FROM events_table WHERE users_table.user_id = events_table.user_id GROUP BY user_id) value_2 < ALL (SELECT avg(value_3) FROM events_table WHERE users_table.user_id = events_table.user_id GROUP BY user_id)
GROUP BY GROUP BY
1 1
@ -96,7 +96,7 @@ WHERE
FROM FROM
events_table as e2 events_table as e2
WHERE WHERE
value_2 = 15 AND value_3 > 25 AND value_2 = 1 AND value_3 > 3 AND
e1.user_id = e2.user_id e1.user_id = e2.user_id
) )
ORDER BY 1; ORDER BY 1;
@ -113,12 +113,12 @@ WHERE
FROM FROM
events_table as e2 events_table as e2
WHERE WHERE
value_2 = 15 AND value_3 > 25 AND value_2 = 1 AND value_3 > 3 AND
e1.user_id = e2.user_id e1.user_id = e2.user_id
) )
GROUP BY 1 GROUP BY 1
HAVING count(*) > 122 HAVING count(*) > 2
ORDER BY 1; ORDER BY 1;
-- non-correlated query with =ANY on partition keys -- non-correlated query with =ANY on partition keys
@ -127,14 +127,14 @@ ORDER BY 1;
FROM FROM
users_table users_table
WHERE WHERE
user_id =ANY(SELECT user_id FROM users_table WHERE value_1 >= 10 AND value_1 <= 20) GROUP BY 1 ORDER BY 2 DESC LIMIT 5; user_id =ANY(SELECT user_id FROM users_table WHERE value_1 >= 1 AND value_1 <= 2) GROUP BY 1 ORDER BY 2 DESC LIMIT 5;
-- users that appeared more than 118 times -- users that appeared more than 118 times
SELECT SELECT
user_id user_id
FROM FROM
users_table users_table
WHERE 118 <= WHERE 2 <=
(SELECT (SELECT
count(*) count(*)
FROM FROM
@ -153,8 +153,8 @@ ORDER BY
-- but it is a valid query with an arbitrary subquery in -- but it is a valid query with an arbitrary subquery in
-- WHERE clause -- WHERE clause
SELECT user_id, value_2 FROM users_table WHERE SELECT user_id, value_2 FROM users_table WHERE
value_1 > 101 AND value_1 < 110 value_1 > 1 AND value_1 < 3
AND value_2 >= 5 AND value_2 >= 1
AND user_id IN AND user_id IN
( (
SELECT SELECT
@ -167,7 +167,7 @@ SELECT user_id, value_2 FROM users_table WHERE
min(time) AS view_homepage_time min(time) AS view_homepage_time
FROM events_table FROM events_table
WHERE WHERE
event_type IN (10, 20, 30, 40, 50, 60, 70, 80, 90) event_type IN (0, 1)
GROUP BY user_id GROUP BY user_id
) e1 LEFT JOIN LATERAL ( ) e1 LEFT JOIN LATERAL (
SELECT SELECT
@ -177,7 +177,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_table FROM events_table
WHERE WHERE
user_id = e1.user_id AND user_id = e1.user_id AND
event_type IN (11, 21, 31, 41, 51, 61, 71, 81, 91) event_type IN (1, 2)
ORDER BY time ORDER BY time
) e2 ON true LEFT JOIN LATERAL ( ) e2 ON true LEFT JOIN LATERAL (
SELECT SELECT
@ -187,7 +187,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_table FROM events_table
WHERE WHERE
user_id = e2.user_id AND user_id = e2.user_id AND
event_type IN (12, 22, 32, 42, 52, 62, 72, 82, 92) event_type IN (2, 3)
ORDER BY time ORDER BY time
) e3 ON true LEFT JOIN LATERAL ( ) e3 ON true LEFT JOIN LATERAL (
SELECT SELECT
@ -197,7 +197,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_table FROM events_table
WHERE WHERE
user_id = e3.user_id AND user_id = e3.user_id AND
event_type IN (13, 23, 33, 43, 53, 63, 73, 83, 93) event_type IN (3, 4)
ORDER BY time ORDER BY time
) e4 ON true LEFT JOIN LATERAL ( ) e4 ON true LEFT JOIN LATERAL (
SELECT SELECT
@ -205,7 +205,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_table FROM events_table
WHERE WHERE
user_id = e4.user_id AND user_id = e4.user_id AND
event_type IN (14, 24, 34, 44, 54, 64, 74, 84, 94) event_type IN (5, 6)
ORDER BY time ORDER BY time
) e5 ON true ) e5 ON true
group by e1.user_id group by e1.user_id
@ -241,9 +241,9 @@ WHERE
events_table events_table
WHERE WHERE
users_table.user_id = events_table.user_id AND users_table.user_id = events_table.user_id AND
users_table.user_id >= 10 AND users_table.user_id >= 1 AND
users_table.user_id <= 70 AND users_table.user_id <= 3 AND
events_table.event_type > 10 AND events_table.event_type < 12 events_table.event_type > 1 AND events_table.event_type < 3
) )
UNION UNION
(SELECT (SELECT
@ -255,9 +255,9 @@ WHERE
events_table events_table
WHERE WHERE
users_table.user_id = events_table.user_id AND users_table.user_id = events_table.user_id AND
users_table.user_id >= 10 AND users_table.user_id >= 1 AND
users_table.user_id <= 70 AND users_table.user_id <= 3 AND
events_table.event_type > 12 AND events_table.event_type < 14 events_table.event_type > 2 AND events_table.event_type < 4
) )
) AS subquery_1 ) AS subquery_1
LEFT JOIN LEFT JOIN
@ -267,9 +267,9 @@ WHERE
FROM FROM
users_table users_table
WHERE WHERE
user_id >= 10 AND user_id >= 1 AND
user_id <= 70 AND user_id <= 3 AND
users_table.value_1 > 15 AND users_table.value_1 < 17 users_table.value_1 > 3 AND users_table.value_1 < 5
GROUP BY GROUP BY
user_id user_id
HAVING HAVING
@ -283,7 +283,7 @@ WHERE
count_pay, user_id count_pay, user_id
) )
GROUP BY user_id GROUP BY user_id
HAVING count(*) > 3 AND sum(value_2) > 49000 HAVING count(*) > 1 AND sum(value_2) > 29
ORDER BY 1; ORDER BY 1;
@ -306,9 +306,9 @@ FROM (
user_id user_id
FROM FROM
users_table users_table
WHERE value_2 >= 5 WHERE value_2 >= 1
AND EXISTS (SELECT user_id FROM events_table WHERE event_type > 100 AND event_type <= 300 AND value_3 > 100 AND user_id = users_table.user_id) AND EXISTS (SELECT user_id FROM events_table WHERE event_type > 1 AND event_type <= 3 AND value_3 > 1 AND user_id = users_table.user_id)
AND NOT EXISTS (SELECT user_id FROM events_table WHERE event_type > 300 AND event_type <= 350 AND value_3 > 100 AND user_id = users_table.user_id) AND NOT EXISTS (SELECT user_id FROM events_table WHERE event_type > 3 AND event_type <= 4 AND value_3 > 1 AND user_id = users_table.user_id)
) )
) t ) t
GROUP BY user_id GROUP BY user_id
@ -322,8 +322,8 @@ ORDER BY 2 DESC, 1;
-- e4 is not joined on the partition key -- e4 is not joined on the partition key
SELECT user_id, value_2 FROM users_table WHERE SELECT user_id, value_2 FROM users_table WHERE
value_1 > 101 AND value_1 < 110 value_1 > 1 AND value_1 < 2
AND value_2 >= 5 AND value_2 >= 1
AND user_id IN AND user_id IN
( (
SELECT SELECT
@ -336,7 +336,7 @@ SELECT user_id, value_2 FROM users_table WHERE
min(time) AS view_homepage_time min(time) AS view_homepage_time
FROM events_table FROM events_table
WHERE WHERE
event_type IN (10, 20, 30, 40, 50, 60, 70, 80, 90) event_type IN (0, 1)
GROUP BY user_id GROUP BY user_id
) e1 LEFT JOIN LATERAL ( ) e1 LEFT JOIN LATERAL (
SELECT SELECT
@ -346,7 +346,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_table FROM events_table
WHERE WHERE
user_id = e1.user_id AND user_id = e1.user_id AND
event_type IN (11, 21, 31, 41, 51, 61, 71, 81, 91) event_type IN (1, 2)
ORDER BY time ORDER BY time
) e2 ON true LEFT JOIN LATERAL ( ) e2 ON true LEFT JOIN LATERAL (
SELECT SELECT
@ -356,7 +356,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_table FROM events_table
WHERE WHERE
user_id = e2.user_id AND user_id = e2.user_id AND
event_type IN (12, 22, 32, 42, 52, 62, 72, 82, 92) event_type IN (2, 3)
ORDER BY time ORDER BY time
) e3 ON true LEFT JOIN LATERAL ( ) e3 ON true LEFT JOIN LATERAL (
SELECT SELECT
@ -366,7 +366,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_table FROM events_table
WHERE WHERE
value_2 = e3.user_id AND value_2 = e3.user_id AND
event_type IN (13, 23, 33, 43, 53, 63, 73, 83, 93) event_type IN (3, 4)
ORDER BY time ORDER BY time
) e4 ON true LEFT JOIN LATERAL ( ) e4 ON true LEFT JOIN LATERAL (
SELECT SELECT
@ -374,7 +374,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_table FROM events_table
WHERE WHERE
user_id = e4.user_id AND user_id = e4.user_id AND
event_type IN (14, 24, 34, 44, 54, 64, 74, 84, 94) event_type IN (5, 6)
ORDER BY time ORDER BY time
) e5 ON true ) e5 ON true
group by e1.user_id group by e1.user_id
@ -406,9 +406,9 @@ WHERE
events_table events_table
WHERE WHERE
users_table.user_id = events_table.user_id AND users_table.user_id = events_table.user_id AND
users_table.user_id >= 10 AND users_table.user_id >= 1 AND
users_table.user_id <= 70 AND users_table.user_id <= 3 AND
events_table.event_type > 10 AND events_table.event_type < 12 events_table.event_type > 1 AND events_table.event_type < 3
) )
UNION UNION
(SELECT (SELECT
@ -420,9 +420,9 @@ WHERE
events_table events_table
WHERE WHERE
users_table.user_id = events_table.user_id AND users_table.user_id = events_table.user_id AND
users_table.user_id >= 10 AND users_table.user_id >= 1 AND
users_table.user_id <= 70 AND users_table.user_id <= 3 AND
events_table.event_type > 12 AND events_table.event_type < 14 events_table.event_type > 2 AND events_table.event_type < 4
) )
) AS subquery_1 ) AS subquery_1
LEFT JOIN LEFT JOIN
@ -432,9 +432,9 @@ WHERE
FROM FROM
users_table users_table
WHERE WHERE
user_id >= 10 AND user_id >= 1 AND
user_id <= 70 AND user_id <= 3 AND
users_table.value_1 > 15 AND users_table.value_1 < 17 users_table.value_1 > 3 AND users_table.value_1 < 5
GROUP BY GROUP BY
user_id user_id
HAVING HAVING
@ -448,7 +448,7 @@ WHERE
count_pay, user_id count_pay, user_id
) )
GROUP BY user_id GROUP BY user_id
HAVING count(*) > 3 AND sum(value_2) > 49000 HAVING count(*) > 1 AND sum(value_2) > 29
ORDER BY 1; ORDER BY 1;
-- NOT EXISTS query has non-equi join -- NOT EXISTS query has non-equi join
@ -469,8 +469,8 @@ FROM (
FROM FROM
users_table users_table
WHERE value_2 >= 5 WHERE value_2 >= 5
AND EXISTS (SELECT user_id FROM events_table WHERE event_type > 100 AND event_type <= 300 AND value_3 > 100 AND user_id = users_table.user_id) AND EXISTS (SELECT user_id FROM events_table WHERE event_type > 1 AND event_type <= 3 AND value_3 > 1 AND user_id = users_table.user_id)
AND NOT EXISTS (SELECT user_id FROM events_table WHERE event_type > 300 AND event_type <= 350 AND value_3 > 100 AND user_id != users_table.user_id) AND NOT EXISTS (SELECT user_id FROM events_table WHERE event_type > 3 AND event_type <= 4 AND value_3 > 1 AND user_id != users_table.user_id)
) )
) t ) t
GROUP BY user_id GROUP BY user_id
@ -511,7 +511,7 @@ WHERE
FROM FROM
events_table events_table
WHERE WHERE
users_table.user_id = events_table.user_id AND event_type = 50 users_table.user_id = events_table.user_id AND event_type = 2
GROUP BY GROUP BY
user_id user_id
OFFSET 3 OFFSET 3
@ -548,9 +548,9 @@ WHERE user_id
-- semi join is not on the partition key for the third subquery -- semi join is not on the partition key for the third subquery
SELECT user_id SELECT user_id
FROM users_table FROM users_table
WHERE user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 10 AND value_1 <= 20) WHERE user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 1 AND value_1 <= 2)
AND user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 30 AND value_1 <= 40) AND user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 3 AND value_1 <= 4)
AND value_2 IN (SELECT user_id FROM users_table WHERE value_1 >= 50 AND value_1 <= 60); AND value_2 IN (SELECT user_id FROM users_table WHERE value_1 >= 5 AND value_1 <= 6);
CREATE FUNCTION test_join_function(integer, integer) RETURNS bool CREATE FUNCTION test_join_function(integer, integer) RETURNS bool
AS 'select $1 > $2;' AS 'select $1 > $2;'
@ -560,9 +560,9 @@ CREATE FUNCTION test_join_function(integer, integer) RETURNS bool
-- we disallow JOINs via functions -- we disallow JOINs via functions
SELECT user_id, value_2 FROM users_table WHERE SELECT user_id, value_2 FROM users_table WHERE
value_1 = 101 value_1 = 1
AND value_2 >= 5 AND value_2 >= 2
AND NOT EXISTS (SELECT user_id FROM events_table WHERE event_type=101 AND value_3 > 100 AND test_join_function(events_table.user_id, users_table.user_id)) AND NOT EXISTS (SELECT user_id FROM events_table WHERE event_type=1 AND value_3 > 1 AND test_join_function(events_table.user_id, users_table.user_id))
ORDER BY 1 DESC, 2 DESC ORDER BY 1 DESC, 2 DESC
LIMIT 3; LIMIT 3;

View File

@ -145,7 +145,7 @@ LIMIT 3;
-- should error out since reference table exist on the left side -- should error out since reference table exist on the left side
-- of the left lateral join -- of the left lateral join
SELECT user_id, value_2 FROM users_table WHERE SELECT user_id, value_2 FROM users_table WHERE
value_1 > 101 AND value_1 < 110 value_1 > 1 AND value_1 < 3
AND value_2 >= 5 AND value_2 >= 5
AND user_id IN AND user_id IN
( (
@ -159,7 +159,7 @@ SELECT user_id, value_2 FROM users_table WHERE
min(time) AS view_homepage_time min(time) AS view_homepage_time
FROM events_reference_table FROM events_reference_table
WHERE WHERE
event_type IN (10, 20, 30, 40, 50, 60, 70, 80, 90) event_type IN (1, 2)
GROUP BY user_id GROUP BY user_id
) e1 LEFT JOIN LATERAL ( ) e1 LEFT JOIN LATERAL (
SELECT SELECT
@ -169,7 +169,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_reference_table FROM events_reference_table
WHERE WHERE
user_id = e1.user_id AND user_id = e1.user_id AND
event_type IN (11, 21, 31, 41, 51, 61, 71, 81, 91) event_type IN (2, 3)
ORDER BY time ORDER BY time
) e2 ON true LEFT JOIN LATERAL ( ) e2 ON true LEFT JOIN LATERAL (
SELECT SELECT
@ -179,7 +179,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_reference_table FROM events_reference_table
WHERE WHERE
user_id = e2.user_id AND user_id = e2.user_id AND
event_type IN (12, 22, 32, 42, 52, 62, 72, 82, 92) event_type IN (3, 4)
ORDER BY time ORDER BY time
) e3 ON true LEFT JOIN LATERAL ( ) e3 ON true LEFT JOIN LATERAL (
SELECT SELECT
@ -189,7 +189,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_reference_table FROM events_reference_table
WHERE WHERE
user_id = e3.user_id AND user_id = e3.user_id AND
event_type IN (13, 23, 33, 43, 53, 63, 73, 83, 93) event_type IN (4, 5)
ORDER BY time ORDER BY time
) e4 ON true LEFT JOIN LATERAL ( ) e4 ON true LEFT JOIN LATERAL (
SELECT SELECT
@ -197,7 +197,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_reference_table FROM events_reference_table
WHERE WHERE
user_id = e4.user_id AND user_id = e4.user_id AND
event_type IN (14, 24, 34, 44, 54, 64, 74, 84, 94) event_type IN (5, 6)
ORDER BY time ORDER BY time
) e5 ON true ) e5 ON true
group by e1.user_id group by e1.user_id
@ -211,7 +211,7 @@ ORDER BY 1, 2;
FROM FROM
users_table users_table
WHERE WHERE
value_3 =ANY(SELECT value_2 FROM users_reference_table WHERE value_1 >= 10 AND value_1 <= 20) value_3 =ANY(SELECT value_2 FROM users_reference_table WHERE value_1 >= 1 AND value_1 <= 2)
GROUP BY 1 ORDER BY 2 DESC, 1 DESC LIMIT 5; GROUP BY 1 ORDER BY 2 DESC, 1 DESC LIMIT 5;
@ -227,8 +227,8 @@ WHERE
FROM FROM
events_reference_table as e2 events_reference_table as e2
WHERE WHERE
value_2 = 15 AND value_2 = 2 AND
value_3 > 25 AND value_3 > 3 AND
e1.value_2 > e2.value_2 e1.value_2 > e2.value_2
) )
GROUP BY 1 GROUP BY 1
@ -239,7 +239,7 @@ LIMIT 5;
-- should work since reference table is on the -- should work since reference table is on the
-- inner part of the join -- inner part of the join
SELECT user_id, value_2 FROM users_table WHERE SELECT user_id, value_2 FROM users_table WHERE
value_1 > 101 AND value_1 < 110 value_1 > 1 AND value_1 < 3
AND value_2 >= 5 AND value_2 >= 5
AND user_id IN AND user_id IN
( (
@ -253,7 +253,7 @@ SELECT user_id, value_2 FROM users_table WHERE
min(time) AS view_homepage_time min(time) AS view_homepage_time
FROM events_table FROM events_table
WHERE WHERE
event_type IN (10, 20, 30, 40, 50, 60, 70, 80, 90) event_type IN (1, 2)
GROUP BY user_id GROUP BY user_id
) e1 LEFT JOIN LATERAL ( ) e1 LEFT JOIN LATERAL (
SELECT SELECT
@ -263,7 +263,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_table FROM events_table
WHERE WHERE
user_id = e1.user_id AND user_id = e1.user_id AND
event_type IN (11, 21, 31, 41, 51, 61, 71, 81, 91) event_type IN (2, 3)
ORDER BY time ORDER BY time
) e2 ON true LEFT JOIN LATERAL ( ) e2 ON true LEFT JOIN LATERAL (
SELECT SELECT
@ -273,7 +273,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_table FROM events_table
WHERE WHERE
user_id = e2.user_id AND user_id = e2.user_id AND
event_type IN (12, 22, 32, 42, 52, 62, 72, 82, 92) event_type IN (3, 4)
ORDER BY time ORDER BY time
) e3 ON true LEFT JOIN LATERAL ( ) e3 ON true LEFT JOIN LATERAL (
SELECT SELECT
@ -283,7 +283,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_table FROM events_table
WHERE WHERE
user_id = e3.user_id AND user_id = e3.user_id AND
event_type IN (13, 23, 33, 43, 53, 63, 73, 83, 93) event_type IN (4, 5)
ORDER BY time ORDER BY time
) e4 ON true LEFT JOIN LATERAL ( ) e4 ON true LEFT JOIN LATERAL (
SELECT SELECT
@ -291,7 +291,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_reference_table FROM events_reference_table
WHERE WHERE
user_id = e4.user_id AND user_id = e4.user_id AND
event_type IN (14, 24, 34, 44, 54, 64, 74, 84, 94) event_type IN (5, 6)
ORDER BY time ORDER BY time
) e5 ON true ) e5 ON true
group by e1.user_id group by e1.user_id
@ -326,10 +326,10 @@ FROM users_reference_table
WHERE value_2 > ALL WHERE value_2 > ALL
(SELECT min(value_2) (SELECT min(value_2)
FROM events_table FROM events_table
WHERE event_type > 50 AND users_reference_table.user_id = events_table.user_id WHERE event_type > 2 AND users_reference_table.user_id = events_table.user_id
GROUP BY user_id) GROUP BY user_id)
GROUP BY user_id GROUP BY user_id
HAVING count(*) > 66 HAVING count(*) > 3
ORDER BY 2 DESC, ORDER BY 2 DESC,
1 DESC 1 DESC
LIMIT 5; LIMIT 5;
@ -358,12 +358,12 @@ WHERE
FROM FROM
events_reference_table events_reference_table
WHERE WHERE
users_table.user_id = events_reference_table.user_id AND event_type = 50 users_table.user_id = events_reference_table.user_id AND event_type = 2
GROUP BY GROUP BY
users_table.user_id users_table.user_id
) )
GROUP BY user_id GROUP BY user_id
HAVING count(*) > 66 HAVING count(*) > 3
ORDER BY user_id ORDER BY user_id
LIMIT 5; LIMIT 5;
@ -380,11 +380,11 @@ WHERE
FROM FROM
events_reference_table events_reference_table
WHERE WHERE
users_table.user_id = events_reference_table.user_id AND event_type = 50 users_table.user_id = events_reference_table.user_id AND event_type = 2
GROUP BY GROUP BY
(users_table.user_id * 2) (users_table.user_id * 2)
) )
GROUP BY user_id GROUP BY user_id
HAVING count(*) > 66 HAVING count(*) > 3
ORDER BY user_id ORDER BY user_id
LIMIT 5; LIMIT 5;

View File

@ -22,14 +22,14 @@ FROM (
SELECT user_id, time SELECT user_id, time
FROM users_table FROM users_table
WHERE WHERE
user_id >= 10 AND user_id >= 1 AND
user_id <= 70 AND user_id <= 3 AND
users_table.value_1 > 10 AND users_table.value_1 < 12 users_table.value_1 > 1 AND users_table.value_1 < 3
) u LEFT JOIN LATERAL ( ) u LEFT JOIN LATERAL (
SELECT event_type, time SELECT event_type, time
FROM events_table FROM events_table
WHERE user_id = u.user_id AND WHERE user_id = u.user_id AND
events_table.event_type > 10 AND events_table.event_type < 12 events_table.event_type > 1 AND events_table.event_type < 3
) t ON true ) t ON true
GROUP BY user_id GROUP BY user_id
) AS shard_union ) AS shard_union
@ -54,25 +54,25 @@ FROM (
WHERE WHERE
user_id >= $1 AND user_id >= $1 AND
user_id <= $2 AND user_id <= $2 AND
users_table.value_1 > 10 AND users_table.value_1 < 12 users_table.value_1 > 1 AND users_table.value_1 < 3
) u LEFT JOIN LATERAL ( ) u LEFT JOIN LATERAL (
SELECT event_type, time SELECT event_type, time
FROM events_table FROM events_table
WHERE user_id = u.user_id AND WHERE user_id = u.user_id AND
events_table.event_type > 10 AND events_table.event_type < 12 events_table.event_type > 1 AND events_table.event_type < 3
) t ON true ) t ON true
GROUP BY user_id GROUP BY user_id
) AS shard_union ) AS shard_union
ORDER BY user_lastseen DESC, user_id; ORDER BY user_lastseen DESC, user_id;
-- should be fine with more than five executions -- should be fine with more than five executions
EXECUTE prepared_subquery_2(10, 70); EXECUTE prepared_subquery_2(1, 3);
EXECUTE prepared_subquery_2(10, 70); EXECUTE prepared_subquery_2(1, 3);
EXECUTE prepared_subquery_2(10, 70); EXECUTE prepared_subquery_2(1, 3);
EXECUTE prepared_subquery_2(10, 70); EXECUTE prepared_subquery_2(1, 3);
EXECUTE prepared_subquery_2(10, 70); EXECUTE prepared_subquery_2(1, 3);
EXECUTE prepared_subquery_2(10, 70); EXECUTE prepared_subquery_2(1, 3);
EXECUTE prepared_subquery_2(10, 70); EXECUTE prepared_subquery_2(1, 3);
-- prepared statements with subqueries in WHERE clause -- prepared statements with subqueries in WHERE clause
PREPARE prepared_subquery_3(int, int, int, int, int, int) AS PREPARE prepared_subquery_3(int, int, int, int, int, int) AS
@ -88,12 +88,12 @@ ORDER BY
LIMIT 5; LIMIT 5;
-- enough times (6+) to actually use prepared statements -- enough times (6+) to actually use prepared statements
EXECUTE prepared_subquery_3(50, 60, 20, 10, 30, 40); EXECUTE prepared_subquery_3(4, 5, 1, 0, 2, 3);
EXECUTE prepared_subquery_3(50, 60, 20, 10, 30, 40); EXECUTE prepared_subquery_3(4, 5, 1, 0, 2, 3);
EXECUTE prepared_subquery_3(50, 60, 20, 10, 30, 40); EXECUTE prepared_subquery_3(4, 5, 1, 0, 2, 3);
EXECUTE prepared_subquery_3(50, 60, 20, 10, 30, 40); EXECUTE prepared_subquery_3(4, 5, 1, 0, 2, 3);
EXECUTE prepared_subquery_3(50, 60, 20, 10, 30, 40); EXECUTE prepared_subquery_3(4, 5, 1, 0, 2, 3);
EXECUTE prepared_subquery_3(50, 60, 20, 10, 30, 40); EXECUTE prepared_subquery_3(4, 5, 1, 0, 2, 3);
CREATE FUNCTION plpgsql_subquery_test(int, int) RETURNS TABLE(count bigint) AS $$ CREATE FUNCTION plpgsql_subquery_test(int, int) RETURNS TABLE(count bigint) AS $$
@ -110,7 +110,7 @@ BEGIN
FROM FROM
users_table AS ma, events_table as short_list users_table AS ma, events_table as short_list
WHERE WHERE
short_list.user_id = ma.user_id and ma.value_1 < $1 and short_list.event_type < 50 short_list.user_id = ma.user_id and ma.value_1 < $1 and short_list.event_type < 3
) temp ) temp
ON users_table.user_id = temp.user_id ON users_table.user_id = temp.user_id
WHERE WHERE
@ -120,15 +120,15 @@ END;
$$ LANGUAGE plpgsql; $$ LANGUAGE plpgsql;
-- enough times (6+) to actually use prepared statements -- enough times (6+) to actually use prepared statements
SELECT plpgsql_subquery_test(10, 20); SELECT plpgsql_subquery_test(1, 2);
SELECT plpgsql_subquery_test(10, 20); SELECT plpgsql_subquery_test(1, 2);
SELECT plpgsql_subquery_test(10, 20); SELECT plpgsql_subquery_test(1, 2);
SELECT plpgsql_subquery_test(10, 20); SELECT plpgsql_subquery_test(1, 2);
SELECT plpgsql_subquery_test(10, 20); SELECT plpgsql_subquery_test(1, 2);
SELECT plpgsql_subquery_test(10, 20); SELECT plpgsql_subquery_test(1, 2);
-- this should also work, but should return 0 given that int = NULL is always returns false -- this should also work, but should return 0 given that int = NULL is always returns false
SELECT plpgsql_subquery_test(10, NULL); SELECT plpgsql_subquery_test(1, NULL);
CREATE FUNCTION sql_subquery_test(int, int) RETURNS bigint AS $$ CREATE FUNCTION sql_subquery_test(int, int) RETURNS bigint AS $$
SELECT SELECT
@ -141,7 +141,7 @@ CREATE FUNCTION sql_subquery_test(int, int) RETURNS bigint AS $$
FROM FROM
users_table AS ma, events_table as short_list users_table AS ma, events_table as short_list
WHERE WHERE
short_list.user_id = ma.user_id and ma.value_1 < $1 and short_list.event_type < 50 short_list.user_id = ma.user_id and ma.value_1 < $1 and short_list.event_type < 3
) temp ) temp
ON users_table.user_id = temp.user_id ON users_table.user_id = temp.user_id
WHERE WHERE
@ -149,7 +149,7 @@ CREATE FUNCTION sql_subquery_test(int, int) RETURNS bigint AS $$
$$ LANGUAGE SQL; $$ LANGUAGE SQL;
-- should error out -- should error out
SELECT sql_subquery_test(5,5); SELECT sql_subquery_test(1,1);
DROP FUNCTION plpgsql_subquery_test(int, int); DROP FUNCTION plpgsql_subquery_test(int, int);
DROP FUNCTION sql_subquery_test(int, int); DROP FUNCTION sql_subquery_test(int, int);

View File

@ -10,9 +10,9 @@ SET citus.enable_router_execution TO false;
-- a very simple union query -- a very simple union query
SELECT user_id, counter SELECT user_id, counter
FROM ( FROM (
SELECT user_id, value_2 % 10 AS counter FROM events_table WHERE event_type IN (1, 2, 3, 4, 5) SELECT user_id, value_2 % 10 AS counter FROM events_table WHERE event_type IN (1, 2)
UNION UNION
SELECT user_id, value_2 % 10 AS counter FROM events_table WHERE event_type IN (5, 6, 7, 8, 9, 10) SELECT user_id, value_2 % 10 AS counter FROM events_table WHERE event_type IN (5, 6)
) user_id ) user_id
ORDER BY 2 DESC,1 ORDER BY 2 DESC,1
LIMIT 5; LIMIT 5;
@ -20,9 +20,9 @@ LIMIT 5;
-- a very simple union query with reference table -- a very simple union query with reference table
SELECT user_id, counter SELECT user_id, counter
FROM ( FROM (
SELECT user_id, value_2 % 10 AS counter FROM events_table WHERE event_type IN (1, 2, 3, 4, 5) SELECT user_id, value_2 % 10 AS counter FROM events_table WHERE event_type IN (1, 2)
UNION UNION
SELECT user_id, value_2 % 10 AS counter FROM events_reference_table WHERE event_type IN (5, 6, 7, 8, 9, 10) SELECT user_id, value_2 % 10 AS counter FROM events_reference_table WHERE event_type IN (5, 6)
) user_id ) user_id
ORDER BY 2 DESC,1 ORDER BY 2 DESC,1
LIMIT 5; LIMIT 5;
@ -30,9 +30,9 @@ LIMIT 5;
-- the same query with union all -- the same query with union all
SELECT user_id, counter SELECT user_id, counter
FROM ( FROM (
SELECT user_id, value_2 % 10 AS counter FROM events_table WHERE event_type IN (1, 2, 3, 4, 5) SELECT user_id, value_2 % 10 AS counter FROM events_table WHERE event_type IN (1, 2)
UNION ALL UNION ALL
SELECT user_id, value_2 % 10 AS counter FROM events_table WHERE event_type IN (5, 6, 7, 8, 9, 10) SELECT user_id, value_2 % 10 AS counter FROM events_table WHERE event_type IN (5, 6)
) user_id ) user_id
ORDER BY 2 DESC,1 ORDER BY 2 DESC,1
LIMIT 5; LIMIT 5;
@ -40,9 +40,9 @@ LIMIT 5;
-- the same query with union all and reference table -- the same query with union all and reference table
SELECT user_id, counter SELECT user_id, counter
FROM ( FROM (
SELECT user_id, value_2 % 10 AS counter FROM events_table WHERE event_type IN (1, 2, 3, 4, 5) SELECT user_id, value_2 % 10 AS counter FROM events_table WHERE event_type IN (1, 2)
UNION ALL UNION ALL
SELECT user_id, value_2 % 10 AS counter FROM events_reference_table WHERE event_type IN (5, 6, 7, 8, 9, 10) SELECT user_id, value_2 % 10 AS counter FROM events_reference_table WHERE event_type IN (5, 6)
) user_id ) user_id
ORDER BY 2 DESC,1 ORDER BY 2 DESC,1
LIMIT 5; LIMIT 5;
@ -50,9 +50,9 @@ LIMIT 5;
-- the same query with group by -- the same query with group by
SELECT user_id, sum(counter) SELECT user_id, sum(counter)
FROM ( FROM (
SELECT user_id, value_2 % 10 AS counter FROM events_table WHERE event_type IN (1, 2, 3, 4, 5) SELECT user_id, value_2 % 10 AS counter FROM events_table WHERE event_type IN (1, 2)
UNION UNION
SELECT user_id, value_2 % 10 AS counter FROM events_table WHERE event_type IN (5, 6, 7, 8, 9, 10) SELECT user_id, value_2 % 10 AS counter FROM events_table WHERE event_type IN (5, 6)
) user_id ) user_id
GROUP BY 1 GROUP BY 1
ORDER BY 2 DESC,1 ORDER BY 2 DESC,1
@ -61,9 +61,9 @@ LIMIT 5;
-- the same query with UNION ALL clause -- the same query with UNION ALL clause
SELECT user_id, sum(counter) SELECT user_id, sum(counter)
FROM ( FROM (
SELECT user_id, value_2 % 10 AS counter FROM events_table WHERE event_type IN (1, 2, 3, 4, 5) SELECT user_id, value_2 % 10 AS counter FROM events_table WHERE event_type IN (1, 2)
UNION ALL UNION ALL
SELECT user_id, value_2 % 10 AS counter FROM events_table WHERE event_type IN (5, 6, 7, 8, 9, 10) SELECT user_id, value_2 % 10 AS counter FROM events_table WHERE event_type IN (5, 6)
) user_id ) user_id
GROUP BY 1 GROUP BY 1
ORDER BY 2 DESC,1 ORDER BY 2 DESC,1
@ -72,9 +72,9 @@ LIMIT 5;
-- the same query target list entries shuffled -- the same query target list entries shuffled
SELECT user_id, sum(counter) SELECT user_id, sum(counter)
FROM ( FROM (
SELECT value_2 % 10 AS counter, user_id FROM events_table WHERE event_type IN (1, 2, 3, 4, 5) SELECT value_2 % 10 AS counter, user_id FROM events_table WHERE event_type IN (1, 2)
UNION UNION
SELECT value_2 % 10 AS counter, user_id FROM events_table WHERE event_type IN (5, 6, 7, 8, 9, 10) SELECT value_2 % 10 AS counter, user_id FROM events_table WHERE event_type IN (5, 6)
) user_id ) user_id
GROUP BY 1 GROUP BY 1
ORDER BY 2 DESC,1 ORDER BY 2 DESC,1
@ -110,45 +110,45 @@ ORDER BY 1,2 DESC LIMIT 5;
-- similar query this time more subqueries and target list contains a resjunk entry -- similar query this time more subqueries and target list contains a resjunk entry
SELECT sum(counter) SELECT sum(counter)
FROM ( FROM (
SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 20 GROUP BY user_id HAVING sum(value_2) > 500 SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 1 GROUP BY user_id HAVING sum(value_2) > 5
UNION UNION
SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 40 and value_1 < 60 GROUP BY user_id HAVING sum(value_2) > 500 SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 2 and value_1 < 3 GROUP BY user_id HAVING sum(value_2) > 25
UNION UNION
SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 60 and value_1 < 80 GROUP BY user_id HAVING sum(value_2) > 500 SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 3 and value_1 < 4 GROUP BY user_id HAVING sum(value_2) > 25
UNION UNION
SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 80 and value_1 < 100 GROUP BY user_id HAVING sum(value_2) > 500 SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 4 and value_1 < 5 GROUP BY user_id HAVING sum(value_2) > 25
UNION UNION
SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 100 and value_1 < 120 GROUP BY user_id HAVING sum(value_2) > 500 SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 5 and value_1 < 6 GROUP BY user_id HAVING sum(value_2) > 25
) user_id ) user_id
GROUP BY user_id ORDER BY 1 DESC LIMIT 5; GROUP BY user_id ORDER BY 1 DESC LIMIT 5;
-- similar query this time more subqueries with reference table and target list contains a resjunk entry -- similar query this time more subqueries with reference table and target list contains a resjunk entry
SELECT sum(counter) SELECT sum(counter)
FROM ( FROM (
SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 20 GROUP BY user_id HAVING sum(value_2) > 500 SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 1 GROUP BY user_id HAVING sum(value_2) > 25
UNION UNION
SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 40 and value_1 < 60 GROUP BY user_id HAVING sum(value_2) > 500 SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 2 and value_1 < 3 GROUP BY user_id HAVING sum(value_2) > 25
UNION UNION
SELECT user_id, sum(value_2) AS counter FROM users_reference_table where value_1 < 60 and value_1 < 80 GROUP BY user_id HAVING sum(value_2) > 500 SELECT user_id, sum(value_2) AS counter FROM users_reference_table where value_1 < 3 and value_1 < 4 GROUP BY user_id HAVING sum(value_2) > 25
UNION UNION
SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 80 and value_1 < 100 GROUP BY user_id HAVING sum(value_2) > 500 SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 4 and value_1 < 5 GROUP BY user_id HAVING sum(value_2) > 25
UNION UNION
SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 100 and value_1 < 120 GROUP BY user_id HAVING sum(value_2) > 500 SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 5 and value_1 < 6 GROUP BY user_id HAVING sum(value_2) > 25
) user_id ) user_id
GROUP BY user_id ORDER BY 1 DESC LIMIT 5; GROUP BY user_id ORDER BY 1 DESC LIMIT 5;
-- similar query as above, with UNION ALL -- similar query as above, with UNION ALL
SELECT sum(counter) SELECT sum(counter)
FROM ( FROM (
SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 20 GROUP BY user_id HAVING sum(value_2) > 5000 SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 1 GROUP BY user_id HAVING sum(value_2) > 250
UNION ALL UNION ALL
SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 40 and value_1 < 60 GROUP BY user_id HAVING sum(value_2) > 500 SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 2 and value_1 < 3 GROUP BY user_id HAVING sum(value_2) > 25
UNION ALL UNION ALL
SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 60 and value_1 < 80 GROUP BY user_id HAVING sum(value_2) > 500 SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 3 and value_1 < 4 GROUP BY user_id HAVING sum(value_2) > 25
UNION ALL UNION ALL
SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 80 and value_1 < 100 GROUP BY user_id HAVING sum(value_2) > 500 SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 4 and value_1 < 5 GROUP BY user_id HAVING sum(value_2) > 25
UNION ALL UNION ALL
SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 100 and value_1 < 120 GROUP BY user_id HAVING sum(value_2) > 500 SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 5 and value_1 < 6 GROUP BY user_id HAVING sum(value_2) > 25
) user_id ) user_id
GROUP BY user_id ORDER BY 1 DESC LIMIT 5; GROUP BY user_id ORDER BY 1 DESC LIMIT 5;
@ -254,7 +254,7 @@ FROM
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (10, 11, 12, 13, 14, 15)) events_subquery_1) event_type IN (1, 2)) events_subquery_1)
UNION UNION
(SELECT * (SELECT *
FROM FROM
@ -263,7 +263,7 @@ FROM
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (15, 16, 17, 18, 19) ) events_subquery_2) event_type IN (2, 3) ) events_subquery_2)
UNION UNION
(SELECT * (SELECT *
FROM FROM
@ -272,7 +272,7 @@ FROM
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (20, 21, 22, 23, 24, 25) ) events_subquery_3) event_type IN (4, 5) ) events_subquery_3)
UNION UNION
(SELECT * (SELECT *
FROM FROM
@ -281,7 +281,7 @@ FROM
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (26, 27, 28, 29, 30, 13)) events_subquery_4)) t1 event_type IN (6, 1)) events_subquery_4)) t1
GROUP BY "t1"."user_id") AS t) "q" GROUP BY "t1"."user_id") AS t) "q"
) as final_query ) as final_query
GROUP BY types GROUP BY types
@ -304,28 +304,28 @@ FROM
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (10, 11, 12, 13, 14, 15)) event_type IN (1, 2))
UNION UNION
(SELECT (SELECT
"events"."user_id", "events"."time", 1 AS event "events"."user_id", "events"."time", 1 AS event
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (15, 16, 17, 18, 19) ) event_type IN (2, 3) )
UNION UNION
(SELECT (SELECT
"events"."user_id", "events"."time", 2 AS event "events"."user_id", "events"."time", 2 AS event
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (20, 21, 22, 23, 24, 25) ) event_type IN (4, 5) )
UNION UNION
(SELECT (SELECT
"events"."user_id", "events"."time", 3 AS event "events"."user_id", "events"."time", 3 AS event
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (26, 27, 28, 29, 30, 13))) t1 event_type IN (6, 1))) t1
GROUP BY "t1"."user_id") AS t) "q" GROUP BY "t1"."user_id") AS t) "q"
) as final_query ) as final_query
GROUP BY types GROUP BY types
@ -343,28 +343,28 @@ FROM
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (10, 11, 12, 13, 14, 15)) event_type IN (1, 2))
UNION UNION
(SELECT (SELECT
"events"."user_id", "events"."time", 1 AS event "events"."user_id", "events"."time", 1 AS event
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (15, 16, 17, 18, 19) ) event_type IN (2, 3) )
UNION UNION
(SELECT (SELECT
"events"."user_id", "events"."time", 2 AS event "events"."user_id", "events"."time", 2 AS event
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (20, 21, 22, 23, 24, 25) ) event_type IN (4, 5) )
UNION UNION
(SELECT (SELECT
"events"."user_id", "events"."time", 3 AS event "events"."user_id", "events"."time", 3 AS event
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (26, 27, 28, 29, 30, 13))) t1 event_type IN (6, 1))) t1
GROUP BY "t1"."user_id") AS t) "q" GROUP BY "t1"."user_id") AS t) "q"
GROUP BY types GROUP BY types
ORDER BY types; ORDER BY types;
@ -381,28 +381,28 @@ FROM
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (10, 11, 12, 13, 14, 15)) event_type IN (1, 2))
UNION UNION
(SELECT (SELECT
"events"."user_id", "events"."time", 1 AS event "events"."user_id", "events"."time", 1 AS event
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (15, 16, 17, 18, 19) ) event_type IN (2, 3) )
UNION UNION
(SELECT (SELECT
"events"."user_id", "events"."time", 2 AS event "events"."user_id", "events"."time", 2 AS event
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (20, 21, 22, 23, 24, 25) ) event_type IN (4, 5) )
UNION UNION
(SELECT (SELECT
"events"."user_id", "events"."time", 3 AS event "events"."user_id", "events"."time", 3 AS event
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (26, 27, 28, 29, 30, 13))) t1 event_type IN (6, 1))) t1
) AS t) "q" ) AS t) "q"
ORDER BY 1 ORDER BY 1
LIMIT 5; LIMIT 5;
@ -419,28 +419,28 @@ FROM
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (10, 11, 12, 13, 14, 15)) event_type IN (1, 2))
UNION ALL UNION ALL
(SELECT (SELECT
"events"."user_id", "events"."time", 1 AS event "events"."user_id", "events"."time", 1 AS event
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (15, 16, 17, 18, 19) ) event_type IN (2, 3) )
UNION ALL UNION ALL
(SELECT (SELECT
"events"."user_id", "events"."time", 2 AS event "events"."user_id", "events"."time", 2 AS event
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (20, 21, 22, 23, 24, 25) ) event_type IN (4, 5) )
UNION ALL UNION ALL
(SELECT (SELECT
"events"."user_id", "events"."time", 3 AS event "events"."user_id", "events"."time", 3 AS event
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (26, 27, 28, 29, 30, 13))) t1 event_type IN (6, 1))) t1
GROUP BY "t1"."user_id") AS t) "q" GROUP BY "t1"."user_id") AS t) "q"
GROUP BY types GROUP BY types
ORDER BY types; ORDER BY types;
@ -519,17 +519,17 @@ SELECT
user_id, value_3 user_id, value_3
FROM FROM
( (
(SELECT value_3, user_id FROM events_table where event_type IN (1, 2, 3, 4, 5)) (SELECT value_3, user_id FROM events_table where event_type IN (1, 2))
UNION ALL UNION ALL
(SELECT value_3, user_id FROM events_table where event_type IN (6, 7, 8, 9, 10)) (SELECT value_3, user_id FROM events_table where event_type IN (2, 3))
UNION ALL UNION ALL
(SELECT value_3, user_id FROM events_table where event_type IN (11, 12, 13, 14, 15)) (SELECT value_3, user_id FROM events_table where event_type IN (3, 4))
UNION ALL UNION ALL
(SELECT value_3, user_id FROM events_table where event_type IN (16, 17, 18, 19, 20)) (SELECT value_3, user_id FROM events_table where event_type IN (4, 5))
UNION ALL UNION ALL
(SELECT value_3, user_id FROM events_table where event_type IN (21, 22, 23, 24, 25)) (SELECT value_3, user_id FROM events_table where event_type IN (5, 6))
UNION ALL UNION ALL
(SELECT value_3, user_id FROM events_table where event_type IN (26, 27, 28, 29, 30)) (SELECT value_3, user_id FROM events_table where event_type IN (1, 6))
) b ) b
ORDER BY 1 DESC, 2 DESC ORDER BY 1 DESC, 2 DESC
LIMIT 5; LIMIT 5;
@ -539,17 +539,17 @@ SELECT
max(value_3) max(value_3)
FROM FROM
( (
(SELECT value_3, user_id FROM events_table where event_type IN (1, 2, 3, 4, 5)) (SELECT value_3, user_id FROM events_table where event_type IN (1, 2))
UNION ALL UNION ALL
(SELECT value_3, user_id FROM events_table where event_type IN (6, 7, 8, 9, 10)) (SELECT value_3, user_id FROM events_table where event_type IN (2, 3))
UNION ALL UNION ALL
(SELECT value_3, user_id FROM events_table where event_type IN (11, 12, 13, 14, 15)) (SELECT value_3, user_id FROM events_table where event_type IN (3, 4))
UNION ALL UNION ALL
(SELECT value_3, user_id FROM events_table where event_type IN (16, 17, 18, 19, 20)) (SELECT value_3, user_id FROM events_table where event_type IN (4, 5))
UNION ALL UNION ALL
(SELECT value_3, user_id FROM events_table where event_type IN (21, 22, 23, 24, 25)) (SELECT value_3, user_id FROM events_table where event_type IN (5, 6))
UNION ALL UNION ALL
(SELECT value_3, user_id FROM events_table where event_type IN (26, 27, 28, 29, 30)) (SELECT value_3, user_id FROM events_table where event_type IN (1, 6))
) b ) b
GROUP BY user_id GROUP BY user_id
ORDER BY 1 DESC ORDER BY 1 DESC
@ -570,15 +570,15 @@ GROUP BY user_id;
-- partition key is not selected -- partition key is not selected
SELECT sum(counter) SELECT sum(counter)
FROM ( FROM (
SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 20 GROUP BY user_id HAVING sum(value_2) > 500 SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 1 GROUP BY user_id HAVING sum(value_2) > 25
UNION UNION
SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 40 and value_1 < 60 GROUP BY user_id HAVING sum(value_2) > 500 SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 2 and value_1 < 3 GROUP BY user_id HAVING sum(value_2) > 25
UNION UNION
SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 60 and value_1 < 80 GROUP BY user_id HAVING sum(value_2) > 500 SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 3 and value_1 < 4 GROUP BY user_id HAVING sum(value_2) > 25
UNION UNION
SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 80 and value_1 < 100 GROUP BY user_id HAVING sum(value_2) > 500 SELECT user_id, sum(value_2) AS counter FROM users_table where value_1 < 4 and value_1 < 5 GROUP BY user_id HAVING sum(value_2) > 25
UNION UNION
SELECT 2 * user_id, sum(value_2) AS counter FROM users_table where value_1 < 100 and value_1 < 120 GROUP BY user_id HAVING sum(value_2) > 500 SELECT 2 * user_id, sum(value_2) AS counter FROM users_table where value_1 < 5 and value_1 < 6 GROUP BY user_id HAVING sum(value_2) > 25
) user_id ) user_id
GROUP BY user_id ORDER BY 1 DESC LIMIT 5; GROUP BY user_id ORDER BY 1 DESC LIMIT 5;
@ -703,17 +703,17 @@ SELECT
user_id, value_3 user_id, value_3
FROM FROM
( (
(SELECT value_3, user_id FROM events_table where event_type IN (1, 2, 3, 4, 5)) (SELECT value_3, user_id FROM events_table where event_type IN (1, 2))
UNION ALL UNION ALL
(SELECT value_3, user_id FROM events_table where event_type IN (6, 7, 8, 9, 10)) (SELECT value_3, user_id FROM events_table where event_type IN (2, 3))
UNION ALL UNION ALL
(SELECT value_3, user_id FROM events_table where event_type IN (11, 12, 13, 14, 15)) (SELECT value_3, user_id FROM events_table where event_type IN (3, 4))
UNION ALL UNION ALL
(SELECT value_3, user_id FROM events_table where event_type IN (16, 17, 18, 19, 20)) (SELECT value_3, user_id FROM events_table where event_type IN (4, 5))
UNION ALL UNION ALL
(SELECT value_3, user_id FROM events_table where event_type IN (21, 22, 23, 24, 25)) (SELECT value_3, user_id FROM events_table where event_type IN (5, 6))
UNION ALL UNION ALL
(SELECT value_3, value_2 FROM events_table where event_type IN (26, 27, 28, 29, 30)) (SELECT value_3, value_2 FROM events_table where event_type IN (1, 6))
) b ) b
ORDER BY 1 DESC, 2 DESC ORDER BY 1 DESC, 2 DESC
LIMIT 5; LIMIT 5;
@ -753,15 +753,15 @@ SELECT
user_id, value_3 user_id, value_3
FROM FROM
( (
(SELECT value_3, user_id FROM events_table where event_type IN (1, 2, 3, 4, 5)) (SELECT value_3, user_id FROM events_table where event_type IN (1, 2))
UNION ALL UNION ALL
(SELECT value_3, user_id FROM events_table where event_type IN (6, 7, 8, 9, 10)) (SELECT value_3, user_id FROM events_table where event_type IN (2, 3))
UNION ALL UNION ALL
(SELECT value_3, user_id FROM events_table where event_type IN (11, 12, 13, 14, 15)) (SELECT value_3, user_id FROM events_table where event_type IN (3, 4))
UNION ALL UNION ALL
(SELECT value_3, user_id FROM events_table where event_type IN (16, 17, 18, 19, 20)) (SELECT value_3, user_id FROM events_table where event_type IN (4, 5))
UNION ALL UNION ALL
(SELECT value_3, user_id FROM events_table where event_type IN (21, 22, 23, 24, 25)) (SELECT value_3, user_id FROM events_table where event_type IN (5, 6))
UNION ALL UNION ALL
(SELECT 1, 2) (SELECT 1, 2)
) b ) b
@ -783,7 +783,7 @@ FROM
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (10, 11, 12, 13, 14, 15)) events_subquery_1) event_type IN (1, 2)) events_subquery_1)
UNION UNION
(SELECT * (SELECT *
FROM FROM
@ -792,7 +792,7 @@ FROM
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (15, 16, 17, 18, 19) ) events_subquery_2) event_type IN (2, 3) ) events_subquery_2)
UNION UNION
(SELECT * (SELECT *
FROM FROM
@ -801,7 +801,7 @@ FROM
FROM FROM
events_table as "events" events_table as "events"
WHERE WHERE
event_type IN (20, 21, 22, 23, 24, 25) ) events_subquery_3) event_type IN (4, 5) ) events_subquery_3)
UNION UNION
(SELECT * (SELECT *
FROM FROM

View File

@ -100,7 +100,7 @@ SELECT * FROM
users_table, events_table users_table, events_table
WHERE WHERE
users_table.user_id = events_table.user_id and users_table.user_id = events_table.user_id and
event_type < 25 event_type < 4
WINDOW w1 AS (PARTITION BY users_table.user_id, events_table.event_type ORDER BY events_table.time) WINDOW w1 AS (PARTITION BY users_table.user_id, events_table.event_type ORDER BY events_table.time)
) as foo ) as foo
ORDER BY 3 DESC, 1 DESC, 2 DESC NULLS LAST ORDER BY 3 DESC, 1 DESC, 2 DESC NULLS LAST
@ -115,7 +115,7 @@ SELECT * FROM
users_table, events_table users_table, events_table
WHERE WHERE
users_table.user_id = events_table.user_id and users_table.user_id = events_table.user_id and
event_type < 25 event_type < 4
WINDOW w1 AS (PARTITION BY users_table.user_id, events_table.event_type ORDER BY events_table.time), WINDOW w1 AS (PARTITION BY users_table.user_id, events_table.event_type ORDER BY events_table.time),
w2 AS (PARTITION BY users_table.user_id, (events_table.value_2 % 25) ORDER BY events_table.time) w2 AS (PARTITION BY users_table.user_id, (events_table.value_2 % 25) ORDER BY events_table.time)
) as foo ) as foo
@ -131,7 +131,7 @@ SELECT sub_1.user_id, max(lag_1), max(rank_1), max(rank_2) FROM
users_table, events_table users_table, events_table
WHERE WHERE
users_table.user_id = events_table.user_id and users_table.user_id = events_table.user_id and
event_type < 25 event_type < 4
WINDOW w1 AS (PARTITION BY users_table.user_id, events_table.event_type ORDER BY events_table.time), WINDOW w1 AS (PARTITION BY users_table.user_id, events_table.event_type ORDER BY events_table.time),
w2 AS (PARTITION BY users_table.user_id, (events_table.value_2 % 25) ORDER BY events_table.time) w2 AS (PARTITION BY users_table.user_id, (events_table.value_2 % 25) ORDER BY events_table.time)
) as sub_1 ) as sub_1
@ -143,7 +143,7 @@ JOIN
users_table, events_table users_table, events_table
WHERE WHERE
users_table.user_id = events_table.user_id and users_table.user_id = events_table.user_id and
event_type < 25 event_type < 4
WINDOW w1 AS (PARTITION BY users_table.user_id, events_table.value_2 ORDER BY events_table.time), WINDOW w1 AS (PARTITION BY users_table.user_id, events_table.value_2 ORDER BY events_table.time),
w2 AS (PARTITION BY users_table.user_id, (events_table.value_2 % 50) ORDER BY events_table.time) w2 AS (PARTITION BY users_table.user_id, (events_table.value_2 % 50) ORDER BY events_table.time)
) as sub_2 ) as sub_2
@ -167,7 +167,7 @@ FROM
WINDOW my_win AS (PARTITION BY user_id ORDER BY count(*) DESC) WINDOW my_win AS (PARTITION BY user_id ORDER BY count(*) DESC)
) as foo ) as foo
WHERE WHERE
my_rank > 5 my_rank > 1
GROUP BY GROUP BY
my_rank my_rank
ORDER BY ORDER BY
@ -186,7 +186,7 @@ FROM
events_table events_table
GROUP BY GROUP BY
user_id, date_trunc('day', time) user_id, date_trunc('day', time)
WINDOW my_win AS (PARTITION BY user_id, avg(event_type%10)::int ORDER BY count(*) DESC) WINDOW my_win AS (PARTITION BY user_id, avg(event_type%3)::int ORDER BY count(*) DESC)
) as foo ) as foo
WHERE WHERE
my_rank > 0 my_rank > 0
@ -246,7 +246,7 @@ FROM
DISTINCT user_id, it_name, count(id) OVER (PARTITION BY user_id, id) DISTINCT user_id, it_name, count(id) OVER (PARTITION BY user_id, id)
FROM FROM
users_table, users_ref_test_table users_table, users_ref_test_table
WHERE users_table.value_2=users_ref_test_table.k_no WHERE users_table.value_2 + 40 = users_ref_test_table.k_no
) a ) a
ORDER BY ORDER BY
1, 2, 3 1, 2, 3
@ -308,17 +308,17 @@ SELECT
max(avg) max(avg)
FROM FROM
( (
(SELECT avg(value_3) over (partition by user_id), user_id FROM events_table where event_type IN (1, 2, 3, 4, 5)) (SELECT avg(value_3) over (partition by user_id), user_id FROM events_table where event_type IN (1, 2))
UNION ALL UNION ALL
(SELECT avg(value_3) over (partition by user_id), user_id FROM events_table where event_type IN (6, 7, 8, 9, 10)) (SELECT avg(value_3) over (partition by user_id), user_id FROM events_table where event_type IN (2, 3))
UNION ALL UNION ALL
(SELECT avg(value_3) over (partition by user_id), user_id FROM events_table where event_type IN (11, 12, 13, 14, 15)) (SELECT avg(value_3) over (partition by user_id), user_id FROM events_table where event_type IN (3, 4))
UNION ALL UNION ALL
(SELECT avg(value_3) over (partition by user_id), user_id FROM events_table where event_type IN (16, 17, 18, 19, 20)) (SELECT avg(value_3) over (partition by user_id), user_id FROM events_table where event_type IN (4, 5))
UNION ALL UNION ALL
(SELECT avg(value_3) over (partition by user_id), user_id FROM events_table where event_type IN (21, 22, 23, 24, 25)) (SELECT avg(value_3) over (partition by user_id), user_id FROM events_table where event_type IN (5, 6))
UNION ALL UNION ALL
(SELECT avg(value_3) over (partition by user_id), user_id FROM events_table where event_type IN (26, 27, 28, 29, 30)) (SELECT avg(value_3) over (partition by user_id), user_id FROM events_table where event_type IN (1, 6))
) b ) b
GROUP BY user_id GROUP BY user_id
ORDER BY 1 DESC ORDER BY 1 DESC
@ -364,7 +364,7 @@ SELECT
FROM FROM
users_table users_table
WHERE WHERE
value_2 > 545 AND value_2 > 1 AND
value_2 < ALL ( value_2 < ALL (
SELECT SELECT
avg(value_3) OVER (PARTITION BY user_id) avg(value_3) OVER (PARTITION BY user_id)
@ -395,7 +395,7 @@ FROM (
GROUP BY GROUP BY
user_id, rank user_id, rank
ORDER BY ORDER BY
difference DESC, rank DESC difference DESC, rank DESC, user_id
LIMIT 20; LIMIT 20;
SELECT * FROM ( SELECT * FROM (
@ -421,7 +421,7 @@ WHERE
f3.user_id=f2.user_id f3.user_id=f2.user_id
) a ) a
ORDER BY ORDER BY
abs DESC abs DESC, user_id
LIMIT 10; LIMIT 10;
@ -535,7 +535,7 @@ SELECT * FROM
users_table, events_table users_table, events_table
WHERE WHERE
users_table.user_id = events_table.user_id and users_table.user_id = events_table.user_id and
event_type < 25 event_type < 4
WINDOW w1 AS (PARTITION BY users_table.user_id, events_table.event_type ORDER BY events_table.time), WINDOW w1 AS (PARTITION BY users_table.user_id, events_table.event_type ORDER BY events_table.time),
w2 AS (PARTITION BY users_table.user_id+1, (events_table.value_2 % 25) ORDER BY events_table.time) w2 AS (PARTITION BY users_table.user_id+1, (events_table.value_2 % 25) ORDER BY events_table.time)
) as foo ) as foo
@ -551,7 +551,7 @@ SELECT * FROM
users_table, events_table users_table, events_table
WHERE WHERE
users_table.user_id = events_table.user_id and users_table.user_id = events_table.user_id and
event_type < 25 event_type < 4
WINDOW w1 AS (PARTITION BY users_table.user_id, events_table.event_type ORDER BY events_table.time), WINDOW w1 AS (PARTITION BY users_table.user_id, events_table.event_type ORDER BY events_table.time),
w2 AS (ORDER BY events_table.time) w2 AS (ORDER BY events_table.time)
) as foo ) as foo
@ -574,7 +574,7 @@ FROM
WINDOW my_win AS (ORDER BY avg(event_type)) WINDOW my_win AS (ORDER BY avg(event_type))
) as foo ) as foo
WHERE WHERE
my_rank > 125 my_rank > 1
ORDER BY ORDER BY
3 DESC, 1 DESC,2 DESC 3 DESC, 1 DESC,2 DESC
LIMIT LIMIT
@ -594,7 +594,7 @@ FROM
WINDOW my_win AS (PARTITION BY date_trunc('day', time) ORDER BY avg(event_type)) WINDOW my_win AS (PARTITION BY date_trunc('day', time) ORDER BY avg(event_type))
) as foo ) as foo
WHERE WHERE
my_rank > 125 my_rank > 1
ORDER BY ORDER BY
3 DESC, 1 DESC,2 DESC 3 DESC, 1 DESC,2 DESC
LIMIT LIMIT
@ -651,17 +651,17 @@ SELECT
max(avg) max(avg)
FROM FROM
( (
(SELECT avg(value_3) over (partition by user_id), user_id FROM events_table where event_type IN (1, 2, 3, 4, 5)) (SELECT avg(value_3) over (partition by user_id), user_id FROM events_table where event_type IN (1, 2))
UNION ALL UNION ALL
(SELECT avg(value_3) over (partition by user_id), user_id FROM events_table where event_type IN (6, 7, 8, 9, 10)) (SELECT avg(value_3) over (partition by user_id), user_id FROM events_table where event_type IN (2, 3))
UNION ALL UNION ALL
(SELECT avg(value_3) over (partition by user_id), user_id FROM events_table where event_type IN (11, 12, 13, 14, 15)) (SELECT avg(value_3) over (partition by user_id), user_id FROM events_table where event_type IN (3, 4))
UNION ALL UNION ALL
(SELECT avg(value_3) over (partition by user_id), user_id FROM events_table where event_type IN (16, 17, 18, 19, 20)) (SELECT avg(value_3) over (partition by user_id), user_id FROM events_table where event_type IN (4, 5))
UNION ALL UNION ALL
(SELECT avg(value_3) over (partition by user_id), user_id FROM events_table where event_type IN (21, 22, 23, 24, 25)) (SELECT avg(value_3) over (partition by user_id), user_id FROM events_table where event_type IN (5, 6))
UNION ALL UNION ALL
(SELECT avg(value_3) over (partition by event_type), user_id FROM events_table where event_type IN (26, 27, 28, 29, 30)) (SELECT avg(value_3) over (partition by event_type), user_id FROM events_table where event_type IN (1, 6))
) b ) b
GROUP BY user_id GROUP BY user_id
ORDER BY 1 DESC ORDER BY 1 DESC

View File

@ -158,13 +158,13 @@ DROP VIEW priority_orders;
CREATE VIEW recent_users AS CREATE VIEW recent_users AS
SELECT user_id, max(time) as lastseen FROM users_table SELECT user_id, max(time) as lastseen FROM users_table
GROUP BY user_id GROUP BY user_id
HAVING max(time) > '2014-01-21 05:45:49.978738'::timestamp order by 2 DESC; HAVING max(time) > '2017-11-23 16:20:33.264457'::timestamp order by 2 DESC;
SELECT * FROM recent_users; SELECT * FROM recent_users;
-- create a view for recent_events -- create a view for recent_events
CREATE VIEW recent_events AS CREATE VIEW recent_events AS
SELECT user_id, time FROM events_table SELECT user_id, time FROM events_table
WHERE time > '2014-01-20 01:45:49.978738'::timestamp; WHERE time > '2017-11-23 16:20:33.264457'::timestamp;
SELECT count(*) FROM recent_events; SELECT count(*) FROM recent_events;
@ -223,14 +223,14 @@ SELECT count(*)
WHERE ru.user_id IS NULL; WHERE ru.user_id IS NULL;
-- join between view and table -- join between view and table
-- users who has recent activity and they have an entry with value_1 is less than 15 -- users who has recent activity and they have an entry with value_1 is less than 3
SELECT ut.* FROM recent_users ru JOIN users_table ut USING (user_id) WHERE ut.value_1 < 15 ORDER BY 1,2; SELECT ut.* FROM recent_users ru JOIN users_table ut USING (user_id) WHERE ut.value_1 < 3 ORDER BY 1,2;
-- determine if a recent user has done a given event type or not -- determine if a recent user has done a given event type or not
SELECT ru.user_id, CASE WHEN et.user_id IS NULL THEN 'NO' ELSE 'YES' END as done_event SELECT ru.user_id, CASE WHEN et.user_id IS NULL THEN 'NO' ELSE 'YES' END as done_event
FROM recent_users ru FROM recent_users ru
LEFT JOIN events_table et LEFT JOIN events_table et
ON(ru.user_id = et.user_id AND et.event_type = 625) ON(ru.user_id = et.user_id AND et.event_type = 6)
ORDER BY 2 DESC, 1; ORDER BY 2 DESC, 1;
-- view vs table join wrapped inside a subquery -- view vs table join wrapped inside a subquery
@ -238,7 +238,7 @@ SELECT * FROM
(SELECT ru.user_id, CASE WHEN et.user_id IS NULL THEN 'NO' ELSE 'YES' END as done_event (SELECT ru.user_id, CASE WHEN et.user_id IS NULL THEN 'NO' ELSE 'YES' END as done_event
FROM recent_users ru FROM recent_users ru
LEFT JOIN events_table et LEFT JOIN events_table et
ON(ru.user_id = et.user_id AND et.event_type = 625) ON(ru.user_id = et.user_id AND et.event_type = 6)
) s1 ) s1
ORDER BY 2 DESC, 1; ORDER BY 2 DESC, 1;
@ -252,7 +252,7 @@ SELECT * FROM
ORDER BY 2 DESC, 1; ORDER BY 2 DESC, 1;
-- create a select only view -- create a select only view
CREATE VIEW selected_users AS SELECT * FROM users_table WHERE value_1 >= 120 and value_1 <150; CREATE VIEW selected_users AS SELECT * FROM users_table WHERE value_1 >= 1 and value_1 <3;
CREATE VIEW recent_selected_users AS SELECT su.* FROM selected_users su JOIN recent_users ru USING(user_id); CREATE VIEW recent_selected_users AS SELECT su.* FROM selected_users su JOIN recent_users ru USING(user_id);
SELECT user_id FROM recent_selected_users GROUP BY 1 ORDER BY 1; SELECT user_id FROM recent_selected_users GROUP BY 1 ORDER BY 1;
@ -261,7 +261,7 @@ SELECT user_id FROM recent_selected_users GROUP BY 1 ORDER BY 1;
SELECT et.user_id, et.time FROM events_table et WHERE et.user_id IN (SELECT user_id FROM recent_selected_users) GROUP BY 1,2 ORDER BY 1 DESC,2 DESC LIMIT 5; SELECT et.user_id, et.time FROM events_table et WHERE et.user_id IN (SELECT user_id FROM recent_selected_users) GROUP BY 1,2 ORDER BY 1 DESC,2 DESC LIMIT 5;
-- it is supported when it is a router query -- it is supported when it is a router query
SELECT count(*) FROM events_table et WHERE et.user_id IN (SELECT user_id FROM recent_selected_users WHERE user_id = 90); SELECT count(*) FROM events_table et WHERE et.user_id IN (SELECT user_id FROM recent_selected_users WHERE user_id = 1);
-- expected this to work but it did not -- expected this to work but it did not
(SELECT user_id FROM recent_users) (SELECT user_id FROM recent_users)
@ -274,7 +274,7 @@ SELECT *
(SELECT user_id FROM recent_users) (SELECT user_id FROM recent_users)
UNION UNION
(SELECT user_id FROM selected_users) ) u (SELECT user_id FROM selected_users) ) u
WHERE user_id < 15 AND user_id > 10 WHERE user_id < 2 AND user_id > 0
ORDER BY user_id; ORDER BY user_id;
-- union all also works for views -- union all also works for views
@ -283,7 +283,7 @@ SELECT *
(SELECT user_id FROM recent_users) (SELECT user_id FROM recent_users)
UNION ALL UNION ALL
(SELECT user_id FROM selected_users) ) u (SELECT user_id FROM selected_users) ) u
WHERE user_id < 15 AND user_id > 10 WHERE user_id < 2 AND user_id > 0
ORDER BY user_id; ORDER BY user_id;
SELECT count(*) SELECT count(*)
@ -291,7 +291,7 @@ SELECT count(*)
(SELECT user_id FROM recent_users) (SELECT user_id FROM recent_users)
UNION UNION
(SELECT user_id FROM selected_users) ) u (SELECT user_id FROM selected_users) ) u
WHERE user_id < 15 AND user_id > 10; WHERE user_id < 2 AND user_id > 0;
-- expected this to work but it does not -- expected this to work but it does not
SELECT count(*) SELECT count(*)
@ -299,51 +299,51 @@ SELECT count(*)
(SELECT user_id FROM recent_users) (SELECT user_id FROM recent_users)
UNION ALL UNION ALL
(SELECT user_id FROM selected_users) ) u (SELECT user_id FROM selected_users) ) u
WHERE user_id < 15 AND user_id > 10; WHERE user_id < 2 AND user_id > 0;
-- expand view definitions and re-run last 2 queries -- expand view definitions and re-run last 2 queries
SELECT count(*) SELECT count(*)
FROM ( FROM (
(SELECT user_id FROM (SELECT user_id, max(time) as lastseen FROM users_table (SELECT user_id FROM (SELECT user_id, max(time) as lastseen FROM users_table
GROUP BY user_id GROUP BY user_id
HAVING max(time) > '2014-01-21 05:45:49.978738'::timestamp order by 2 DESC) aa HAVING max(time) > '2017-11-22 05:45:49.978738'::timestamp order by 2 DESC) aa
) )
UNION UNION
(SELECT user_id FROM (SELECT * FROM users_table WHERE value_1 >= 120 and value_1 <150) bb) ) u (SELECT user_id FROM (SELECT * FROM users_table WHERE value_1 >= 1 and value_1 < 3) bb) ) u
WHERE user_id < 15 AND user_id > 10; WHERE user_id < 2 AND user_id > 0;
SELECT count(*) SELECT count(*)
FROM ( FROM (
(SELECT user_id FROM (SELECT user_id, max(time) as lastseen FROM users_table (SELECT user_id FROM (SELECT user_id, max(time) as lastseen FROM users_table
GROUP BY user_id GROUP BY user_id
HAVING max(time) > '2014-01-21 05:45:49.978738'::timestamp order by 2 DESC) aa HAVING max(time) > '2017-11-22 05:45:49.978738'::timestamp order by 2 DESC) aa
) )
UNION ALL UNION ALL
(SELECT user_id FROM (SELECT * FROM users_table WHERE value_1 >= 120 and value_1 <150) bb) ) u (SELECT user_id FROM (SELECT * FROM users_table WHERE value_1 >= 1 and value_1 < 3) bb) ) u
WHERE user_id < 15 AND user_id > 10; WHERE user_id < 2 AND user_id > 0;
-- test distinct -- test distinct
-- distinct is supported if it is on a partition key -- distinct is supported if it is on a partition key
CREATE VIEW distinct_user_with_value_1_15 AS SELECT DISTINCT user_id FROM users_table WHERE value_1 = 15; CREATE VIEW distinct_user_with_value_1_3 AS SELECT DISTINCT user_id FROM users_table WHERE value_1 = 3;
SELECT * FROM distinct_user_with_value_1_15 ORDER BY user_id; SELECT * FROM distinct_user_with_value_1_3 ORDER BY user_id;
-- distinct is not supported if it is on a non-partition key -- distinct is not supported if it is on a non-partition key
CREATE VIEW distinct_value_1 AS SELECT DISTINCT value_1 FROM users_table WHERE value_2 = 15; CREATE VIEW distinct_value_1 AS SELECT DISTINCT value_1 FROM users_table WHERE value_2 = 3;
SELECT * FROM distinct_value_1; SELECT * FROM distinct_value_1;
-- CTEs are not supported even if they are on views -- CTEs are not supported even if they are on views
CREATE VIEW cte_view_1 AS CREATE VIEW cte_view_1 AS
WITH c1 AS (SELECT * FROM users_table WHERE value_1 = 15) SELECT * FROM c1 WHERE value_2 < 500; WITH c1 AS (SELECT * FROM users_table WHERE value_1 = 3) SELECT * FROM c1 WHERE value_2 < 4;
SELECT * FROM cte_view_1; SELECT * FROM cte_view_1;
-- this is single shard query but still not supported since it has view + cte -- this is single shard query but still not supported since it has view + cte
-- router planner can't detect it -- router planner can't detect it
SELECT * FROM cte_view_1 WHERE user_id = 8; SELECT * FROM cte_view_1 WHERE user_id = 2;
-- if CTE itself prunes down to a single shard than the view is supported (router plannable) -- if CTE itself prunes down to a single shard than the view is supported (router plannable)
CREATE VIEW cte_view_2 AS CREATE VIEW cte_view_2 AS
WITH c1 AS (SELECT * FROM users_table WHERE user_id = 8) SELECT * FROM c1 WHERE value_1 = 15; WITH c1 AS (SELECT * FROM users_table WHERE user_id = 2) SELECT * FROM c1 WHERE value_1 = 3;
SELECT * FROM cte_view_2; SELECT * FROM cte_view_2;
CREATE VIEW router_view AS SELECT * FROM users_table WHERE user_id = 2; CREATE VIEW router_view AS SELECT * FROM users_table WHERE user_id = 2;
@ -387,7 +387,7 @@ EXPLAIN (COSTS FALSE) SELECT *
(SELECT user_id FROM recent_users) (SELECT user_id FROM recent_users)
UNION UNION
(SELECT user_id FROM selected_users) ) u (SELECT user_id FROM selected_users) ) u
WHERE user_id < 15 AND user_id > 10 WHERE user_id < 4 AND user_id > 1
ORDER BY user_id; ORDER BY user_id;
EXPLAIN (COSTS FALSE) SELECT et.* FROM recent_10_users JOIN events_table et USING(user_id) ORDER BY et.time DESC LIMIT 10; EXPLAIN (COSTS FALSE) SELECT et.* FROM recent_10_users JOIN events_table et USING(user_id) ORDER BY et.time DESC LIMIT 10;
@ -401,7 +401,7 @@ DROP VIEW router_view;
DROP VIEW cte_view_2; DROP VIEW cte_view_2;
DROP VIEW cte_view_1; DROP VIEW cte_view_1;
DROP VIEW distinct_value_1; DROP VIEW distinct_value_1;
DROP VIEW distinct_user_with_value_1_15; DROP VIEW distinct_user_with_value_1_3;
DROP VIEW recent_selected_users; DROP VIEW recent_selected_users;
DROP VIEW selected_users; DROP VIEW selected_users;
DROP VIEW recent_events; DROP VIEW recent_events;