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

View File

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

View File

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

View File

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

View File

@ -16,21 +16,20 @@ WHERE
FROM
events_table
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
user_id
)
GROUP BY user_id
HAVING count(*) > 66
HAVING count(*) > 2
ORDER BY user_id
LIMIT 5;
user_id
---------
49
55
56
63
(4 rows)
1
5
6
(3 rows)
-- same query with one additional join on non distribution column
SELECT
@ -44,20 +43,20 @@ WHERE
FROM
events_table
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
GROUP BY
user_id
)
GROUP BY user_id
HAVING count(*) > 66
HAVING count(*) > 1
ORDER BY user_id
LIMIT 5;
user_id
---------
55
56
63
1
5
6
(3 rows)
-- the other way around is not supported
@ -72,13 +71,13 @@ WHERE
FROM
events_table
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
GROUP BY
user_id
)
GROUP BY user_id
HAVING count(*) > 66
HAVING count(*) > 1
ORDER BY user_id
LIMIT 5;
ERROR: cannot pushdown the subquery since all relations are not joined using distribution keys
@ -89,7 +88,7 @@ SELECT
FROM
users_table
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)
GROUP BY
1
@ -98,9 +97,9 @@ ORDER BY
LIMIT 3;
user_id
---------
69
52
12
4
3
2
(3 rows)
-- IN operator on non-partition key
@ -115,23 +114,30 @@ WHERE
FROM
events_table as e2
WHERE
value_2 = 15 AND value_3 > 25 AND
value_2 = 1 AND value_3 > 3 AND
e1.user_id = e2.user_id
)
ORDER BY 1;
user_id
---------
8
17
33
47
54
54
56
71
79
86
(10 rows)
2
2
2
2
2
2
2
2
2
2
3
3
3
3
3
3
3
(17 rows)
-- NOT IN on non-partition key
SELECT
@ -145,17 +151,21 @@ WHERE
FROM
events_table as e2
WHERE
value_2 = 15 AND value_3 > 25 AND
value_2 = 1 AND value_3 > 3 AND
e1.user_id = e2.user_id
)
GROUP BY 1
HAVING count(*) > 122
HAVING count(*) > 2
ORDER BY 1;
user_id
---------
23
25
(2 rows)
1
2
3
4
5
6
(6 rows)
-- non-correlated query with =ANY on partition keys
SELECT
@ -163,14 +173,14 @@ ORDER BY 1;
FROM
users_table
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
---------+-------
12 | 121
87 | 117
37 | 115
23 | 115
46 | 115
5 | 26
4 | 23
2 | 18
3 | 17
6 | 10
(5 rows)
-- users that appeared more than 118 times
@ -178,7 +188,7 @@ SELECT
user_id
FROM
users_table
WHERE 118 <=
WHERE 2 <=
(SELECT
count(*)
FROM
@ -193,18 +203,20 @@ ORDER BY
user_id;
user_id
---------
13
17
23
25
(4 rows)
1
2
3
4
5
6
(6 rows)
-- the following query doesn't have a meaningful result
-- but it is a valid query with an arbitrary subquery in
-- WHERE clause
SELECT user_id, value_2 FROM users_table WHERE
value_1 > 101 AND value_1 < 110
AND value_2 >= 5
value_1 > 1 AND value_1 < 3
AND value_2 >= 1
AND user_id IN
(
SELECT
@ -217,7 +229,7 @@ SELECT user_id, value_2 FROM users_table WHERE
min(time) AS view_homepage_time
FROM events_table
WHERE
event_type IN (10, 20, 30, 40, 50, 60, 70, 80, 90)
event_type IN (0, 1)
GROUP BY user_id
) e1 LEFT JOIN LATERAL (
SELECT
@ -227,7 +239,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_table
WHERE
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
) e2 ON true LEFT JOIN LATERAL (
SELECT
@ -237,7 +249,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_table
WHERE
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
) e3 ON true LEFT JOIN LATERAL (
SELECT
@ -247,7 +259,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_table
WHERE
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
) e4 ON true LEFT JOIN LATERAL (
SELECT
@ -255,7 +267,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_table
WHERE
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
) e5 ON true
group by e1.user_id
@ -264,14 +276,22 @@ SELECT user_id, value_2 FROM users_table WHERE
ORDER BY 1, 2;
user_id | value_2
---------+---------
5 | 884
42 | 55
42 | 471
51 | 758
72 | 897
82 | 691
95 | 951
(7 rows)
2 | 2
2 | 2
2 | 4
3 | 2
3 | 2
4 | 1
4 | 3
5 | 1
5 | 2
5 | 2
5 | 5
5 | 5
6 | 3
6 | 4
6 | 4
(15 rows)
-- similar to the above query
-- the following query doesn't have a meaningful result
@ -300,9 +320,9 @@ WHERE
events_table
WHERE
users_table.user_id = events_table.user_id AND
users_table.user_id >= 10 AND
users_table.user_id <= 70 AND
events_table.event_type > 10 AND events_table.event_type < 12
users_table.user_id >= 1 AND
users_table.user_id <= 3 AND
events_table.event_type > 1 AND events_table.event_type < 3
)
UNION
(SELECT
@ -314,9 +334,9 @@ WHERE
events_table
WHERE
users_table.user_id = events_table.user_id AND
users_table.user_id >= 10 AND
users_table.user_id <= 70 AND
events_table.event_type > 12 AND events_table.event_type < 14
users_table.user_id >= 1 AND
users_table.user_id <= 3 AND
events_table.event_type > 2 AND events_table.event_type < 4
)
) AS subquery_1
LEFT JOIN
@ -326,9 +346,9 @@ WHERE
FROM
users_table
WHERE
user_id >= 10 AND
user_id <= 70 AND
users_table.value_1 > 15 AND users_table.value_1 < 17
user_id >= 1 AND
user_id <= 3 AND
users_table.value_1 > 3 AND users_table.value_1 < 5
GROUP BY
user_id
HAVING
@ -342,17 +362,13 @@ WHERE
count_pay, 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;
user_id
---------
18
29
40
49
58
69
(6 rows)
2
3
(2 rows)
-- the following query doesn't have a meaningful result
-- but it is a valid query with an arbitrary subquery in
@ -373,9 +389,9 @@ FROM (
user_id
FROM
users_table
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 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)
WHERE value_2 >= 1
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 > 3 AND event_type <= 4 AND value_3 > 1 AND user_id = users_table.user_id)
)
) t
GROUP BY user_id
@ -383,9 +399,8 @@ FROM (
ORDER BY 2 DESC, 1;
user_id | array_length
---------+--------------
96 | 12204
8 | 8170
(2 rows)
5 | 364
(1 row)
--
-- 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
SELECT user_id, value_2 FROM users_table WHERE
value_1 > 101 AND value_1 < 110
AND value_2 >= 5
value_1 > 1 AND value_1 < 2
AND value_2 >= 1
AND user_id IN
(
SELECT
@ -407,7 +422,7 @@ SELECT user_id, value_2 FROM users_table WHERE
min(time) AS view_homepage_time
FROM events_table
WHERE
event_type IN (10, 20, 30, 40, 50, 60, 70, 80, 90)
event_type IN (0, 1)
GROUP BY user_id
) e1 LEFT JOIN LATERAL (
SELECT
@ -417,7 +432,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_table
WHERE
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
) e2 ON true LEFT JOIN LATERAL (
SELECT
@ -427,7 +442,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_table
WHERE
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
) e3 ON true LEFT JOIN LATERAL (
SELECT
@ -437,7 +452,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_table
WHERE
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
) e4 ON true LEFT JOIN LATERAL (
SELECT
@ -445,7 +460,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_table
WHERE
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
) e5 ON true
group by e1.user_id
@ -477,9 +492,9 @@ WHERE
events_table
WHERE
users_table.user_id = events_table.user_id AND
users_table.user_id >= 10 AND
users_table.user_id <= 70 AND
events_table.event_type > 10 AND events_table.event_type < 12
users_table.user_id >= 1 AND
users_table.user_id <= 3 AND
events_table.event_type > 1 AND events_table.event_type < 3
)
UNION
(SELECT
@ -491,9 +506,9 @@ WHERE
events_table
WHERE
users_table.user_id = events_table.user_id AND
users_table.user_id >= 10 AND
users_table.user_id <= 70 AND
events_table.event_type > 12 AND events_table.event_type < 14
users_table.user_id >= 1 AND
users_table.user_id <= 3 AND
events_table.event_type > 2 AND events_table.event_type < 4
)
) AS subquery_1
LEFT JOIN
@ -503,9 +518,9 @@ WHERE
FROM
users_table
WHERE
user_id >= 10 AND
user_id <= 70 AND
users_table.value_1 > 15 AND users_table.value_1 < 17
user_id >= 1 AND
user_id <= 3 AND
users_table.value_1 > 3 AND users_table.value_1 < 5
GROUP BY
user_id
HAVING
@ -519,7 +534,7 @@ WHERE
count_pay, 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;
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.
@ -541,8 +556,8 @@ FROM (
FROM
users_table
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 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 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 > 3 AND event_type <= 4 AND value_3 > 1 AND user_id != users_table.user_id)
)
) t
GROUP BY user_id
@ -562,8 +577,8 @@ ORDER BY 1 ASC
LIMIT 2;
user_id
---------
0
0
1
1
(2 rows)
-- subquery in where clause has a volatile function and no relation
@ -590,7 +605,7 @@ WHERE
FROM
events_table
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
user_id
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
SELECT user_id
FROM users_table
WHERE user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 10 AND value_1 <= 20)
AND user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 30 AND value_1 <= 40)
AND value_2 IN (SELECT user_id FROM users_table WHERE value_1 >= 50 AND value_1 <= 60);
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 >= 3 AND value_1 <= 4)
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
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
@ -641,9 +656,9 @@ CREATE FUNCTION test_join_function(integer, integer) RETURNS bool
RETURNS NULL ON NULL INPUT;
-- we disallow JOINs via functions
SELECT user_id, value_2 FROM users_table WHERE
value_1 = 101
AND value_2 >= 5
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))
value_1 = 1
AND value_2 >= 2
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
LIMIT 3;
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;
user_id
---------
0
1
2
3
(3 rows)
-- subqueries in WHERE with NOT EXISTS operator, should work since
@ -117,9 +117,9 @@ ORDER BY 2 DESC, 1 DESC
LIMIT 3;
user_id | count
---------+-------
87 | 117
59 | 115
46 | 115
5 | 26
4 | 23
3 | 17
(3 rows)
-- immutable functions are also treated as reference tables
@ -141,9 +141,9 @@ ORDER BY 2 DESC, 1 DESC
LIMIT 3;
user_id | count
---------+-------
12 | 121
87 | 117
59 | 115
5 | 26
4 | 23
2 | 18
(3 rows)
-- immutable functions are also treated as reference tables
@ -165,15 +165,13 @@ ORDER BY 2 DESC, 1 DESC
LIMIT 3;
user_id | count
---------+-------
12 | 121
87 | 117
59 | 115
(3 rows)
6 | 10
(1 row)
-- should error out since reference table exist on the left side
-- of the left lateral join
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 user_id IN
(
@ -187,7 +185,7 @@ SELECT user_id, value_2 FROM users_table WHERE
min(time) AS view_homepage_time
FROM events_reference_table
WHERE
event_type IN (10, 20, 30, 40, 50, 60, 70, 80, 90)
event_type IN (1, 2)
GROUP BY user_id
) e1 LEFT JOIN LATERAL (
SELECT
@ -197,7 +195,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_reference_table
WHERE
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
) e2 ON true LEFT JOIN LATERAL (
SELECT
@ -207,7 +205,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_reference_table
WHERE
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
) e3 ON true LEFT JOIN LATERAL (
SELECT
@ -217,7 +215,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_reference_table
WHERE
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
) e4 ON true LEFT JOIN LATERAL (
SELECT
@ -225,7 +223,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_reference_table
WHERE
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
) e5 ON true
group by e1.user_id
@ -240,15 +238,15 @@ DETAIL: There exist a reference table in the outer part of the outer join
FROM
users_table
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;
user_id | count
---------+-------
48 | 18
26 | 18
15 | 17
54 | 16
35 | 15
5 | 26
4 | 23
2 | 18
3 | 17
6 | 10
(5 rows)
-- non-partition key comparison with reference table
@ -263,8 +261,8 @@ WHERE
FROM
events_reference_table as e2
WHERE
value_2 = 15 AND
value_3 > 25 AND
value_2 = 2 AND
value_3 > 3 AND
e1.value_2 > e2.value_2
)
GROUP BY 1
@ -272,18 +270,18 @@ ORDER BY 2 DESC, 1 DESC
LIMIT 5;
user_id | count
---------+-------
3 | 5
56 | 4
99 | 2
94 | 2
92 | 2
2 | 7
5 | 6
4 | 5
1 | 4
6 | 3
(5 rows)
-- subqueries in both WHERE and FROM clauses
-- should work since reference table is on the
-- inner part of the join
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 user_id IN
(
@ -297,7 +295,7 @@ SELECT user_id, value_2 FROM users_table WHERE
min(time) AS view_homepage_time
FROM events_table
WHERE
event_type IN (10, 20, 30, 40, 50, 60, 70, 80, 90)
event_type IN (1, 2)
GROUP BY user_id
) e1 LEFT JOIN LATERAL (
SELECT
@ -307,7 +305,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_table
WHERE
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
) e2 ON true LEFT JOIN LATERAL (
SELECT
@ -317,7 +315,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_table
WHERE
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
) e3 ON true LEFT JOIN LATERAL (
SELECT
@ -327,7 +325,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_table
WHERE
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
) e4 ON true LEFT JOIN LATERAL (
SELECT
@ -335,7 +333,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_reference_table
WHERE
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
) e5 ON true
group by e1.user_id
@ -344,14 +342,9 @@ SELECT user_id, value_2 FROM users_table WHERE
ORDER BY 1, 2;
user_id | value_2
---------+---------
5 | 884
42 | 55
42 | 471
51 | 758
72 | 897
82 | 691
95 | 951
(7 rows)
5 | 5
5 | 5
(2 rows)
-- reference tables are not allowed if there is sublink
SELECT
@ -381,10 +374,10 @@ FROM users_reference_table
WHERE value_2 > ALL
(SELECT min(value_2)
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
HAVING count(*) > 66
HAVING count(*) > 3
ORDER BY 2 DESC,
1 DESC
LIMIT 5;
@ -415,12 +408,12 @@ WHERE
FROM
events_reference_table
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
users_table.user_id
)
GROUP BY user_id
HAVING count(*) > 66
HAVING count(*) > 3
ORDER BY user_id
LIMIT 5;
ERROR: cannot push down this subquery
@ -438,12 +431,12 @@ WHERE
FROM
events_reference_table
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
(users_table.user_id * 2)
)
GROUP BY user_id
HAVING count(*) > 66
HAVING count(*) > 3
ORDER BY user_id
LIMIT 5;
ERROR: cannot push down this subquery

View File

@ -19,14 +19,14 @@ FROM (
SELECT user_id, time
FROM users_table
WHERE
user_id >= 10 AND
user_id <= 70 AND
users_table.value_1 > 10 AND users_table.value_1 < 12
user_id >= 1 AND
user_id <= 3 AND
users_table.value_1 > 1 AND users_table.value_1 < 3
) u LEFT JOIN LATERAL (
SELECT event_type, time
FROM events_table
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
GROUP BY user_id
) AS shard_union
@ -34,13 +34,9 @@ ORDER BY user_lastseen DESC, user_id;
EXECUTE prepared_subquery_1;
user_id | user_lastseen | array_length
---------+---------------------------------+--------------
12 | Sun Jan 19 01:49:20.372688 2014 | 1
20 | Sat Jan 18 14:25:31.817903 2014 | 1
42 | Thu Jan 16 07:08:02.651966 2014 | 1
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)
2 | Thu Nov 23 11:47:26.900284 2017 | 12
3 | Thu Nov 23 11:18:53.114408 2017 | 14
(2 rows)
PREPARE prepared_subquery_2(int, int) AS
SELECT
@ -58,93 +54,65 @@ FROM (
WHERE
user_id >= $1 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 (
SELECT event_type, time
FROM events_table
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
GROUP BY user_id
) AS shard_union
ORDER BY user_lastseen DESC, user_id;
-- 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
---------+---------------------------------+--------------
12 | Sun Jan 19 01:49:20.372688 2014 | 1
20 | Sat Jan 18 14:25:31.817903 2014 | 1
42 | Thu Jan 16 07:08:02.651966 2014 | 1
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)
2 | Thu Nov 23 11:47:26.900284 2017 | 12
3 | Thu Nov 23 11:18:53.114408 2017 | 14
(2 rows)
EXECUTE prepared_subquery_2(10, 70);
EXECUTE prepared_subquery_2(1, 3);
user_id | user_lastseen | array_length
---------+---------------------------------+--------------
12 | Sun Jan 19 01:49:20.372688 2014 | 1
20 | Sat Jan 18 14:25:31.817903 2014 | 1
42 | Thu Jan 16 07:08:02.651966 2014 | 1
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)
2 | Thu Nov 23 11:47:26.900284 2017 | 12
3 | Thu Nov 23 11:18:53.114408 2017 | 14
(2 rows)
EXECUTE prepared_subquery_2(10, 70);
EXECUTE prepared_subquery_2(1, 3);
user_id | user_lastseen | array_length
---------+---------------------------------+--------------
12 | Sun Jan 19 01:49:20.372688 2014 | 1
20 | Sat Jan 18 14:25:31.817903 2014 | 1
42 | Thu Jan 16 07:08:02.651966 2014 | 1
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)
2 | Thu Nov 23 11:47:26.900284 2017 | 12
3 | Thu Nov 23 11:18:53.114408 2017 | 14
(2 rows)
EXECUTE prepared_subquery_2(10, 70);
EXECUTE prepared_subquery_2(1, 3);
user_id | user_lastseen | array_length
---------+---------------------------------+--------------
12 | Sun Jan 19 01:49:20.372688 2014 | 1
20 | Sat Jan 18 14:25:31.817903 2014 | 1
42 | Thu Jan 16 07:08:02.651966 2014 | 1
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)
2 | Thu Nov 23 11:47:26.900284 2017 | 12
3 | Thu Nov 23 11:18:53.114408 2017 | 14
(2 rows)
EXECUTE prepared_subquery_2(10, 70);
EXECUTE prepared_subquery_2(1, 3);
user_id | user_lastseen | array_length
---------+---------------------------------+--------------
12 | Sun Jan 19 01:49:20.372688 2014 | 1
20 | Sat Jan 18 14:25:31.817903 2014 | 1
42 | Thu Jan 16 07:08:02.651966 2014 | 1
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)
2 | Thu Nov 23 11:47:26.900284 2017 | 12
3 | Thu Nov 23 11:18:53.114408 2017 | 14
(2 rows)
EXECUTE prepared_subquery_2(10, 70);
EXECUTE prepared_subquery_2(1, 3);
user_id | user_lastseen | array_length
---------+---------------------------------+--------------
12 | Sun Jan 19 01:49:20.372688 2014 | 1
20 | Sat Jan 18 14:25:31.817903 2014 | 1
42 | Thu Jan 16 07:08:02.651966 2014 | 1
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)
2 | Thu Nov 23 11:47:26.900284 2017 | 12
3 | Thu Nov 23 11:18:53.114408 2017 | 14
(2 rows)
EXECUTE prepared_subquery_2(10, 70);
EXECUTE prepared_subquery_2(1, 3);
user_id | user_lastseen | array_length
---------+---------------------------------+--------------
12 | Sun Jan 19 01:49:20.372688 2014 | 1
20 | Sat Jan 18 14:25:31.817903 2014 | 1
42 | Thu Jan 16 07:08:02.651966 2014 | 1
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)
2 | Thu Nov 23 11:47:26.900284 2017 | 12
3 | Thu Nov 23 11:18:53.114408 2017 | 14
(2 rows)
-- prepared statements with subqueries in WHERE clause
PREPARE prepared_subquery_3(int, int, int, int, int, int) AS
@ -159,64 +127,64 @@ ORDER BY
user_id DESC
LIMIT 5;
-- 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
---------
93
90
88
87
84
6
5
4
3
2
(5 rows)
EXECUTE prepared_subquery_3(50, 60, 20, 10, 30, 40);
EXECUTE prepared_subquery_3(4, 5, 1, 0, 2, 3);
user_id
---------
93
90
88
87
84
6
5
4
3
2
(5 rows)
EXECUTE prepared_subquery_3(50, 60, 20, 10, 30, 40);
EXECUTE prepared_subquery_3(4, 5, 1, 0, 2, 3);
user_id
---------
93
90
88
87
84
6
5
4
3
2
(5 rows)
EXECUTE prepared_subquery_3(50, 60, 20, 10, 30, 40);
EXECUTE prepared_subquery_3(4, 5, 1, 0, 2, 3);
user_id
---------
93
90
88
87
84
6
5
4
3
2
(5 rows)
EXECUTE prepared_subquery_3(50, 60, 20, 10, 30, 40);
EXECUTE prepared_subquery_3(4, 5, 1, 0, 2, 3);
user_id
---------
93
90
88
87
84
6
5
4
3
2
(5 rows)
EXECUTE prepared_subquery_3(50, 60, 20, 10, 30, 40);
EXECUTE prepared_subquery_3(4, 5, 1, 0, 2, 3);
user_id
---------
93
90
88
87
84
6
5
4
3
2
(5 rows)
CREATE FUNCTION plpgsql_subquery_test(int, int) RETURNS TABLE(count bigint) AS $$
@ -233,7 +201,7 @@ BEGIN
FROM
users_table AS ma, events_table as short_list
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
ON users_table.user_id = temp.user_id
WHERE
@ -242,44 +210,44 @@ BEGIN
END;
$$ LANGUAGE plpgsql;
-- enough times (6+) to actually use prepared statements
SELECT plpgsql_subquery_test(10, 20);
SELECT plpgsql_subquery_test(1, 2);
plpgsql_subquery_test
-----------------------
1500
539
(1 row)
SELECT plpgsql_subquery_test(10, 20);
SELECT plpgsql_subquery_test(1, 2);
plpgsql_subquery_test
-----------------------
1500
539
(1 row)
SELECT plpgsql_subquery_test(10, 20);
SELECT plpgsql_subquery_test(1, 2);
plpgsql_subquery_test
-----------------------
1500
539
(1 row)
SELECT plpgsql_subquery_test(10, 20);
SELECT plpgsql_subquery_test(1, 2);
plpgsql_subquery_test
-----------------------
1500
539
(1 row)
SELECT plpgsql_subquery_test(10, 20);
SELECT plpgsql_subquery_test(1, 2);
plpgsql_subquery_test
-----------------------
1500
539
(1 row)
SELECT plpgsql_subquery_test(10, 20);
SELECT plpgsql_subquery_test(1, 2);
plpgsql_subquery_test
-----------------------
1500
539
(1 row)
-- 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
-----------------------
0
@ -296,14 +264,14 @@ CREATE FUNCTION sql_subquery_test(int, int) RETURNS bigint AS $$
FROM
users_table AS ma, events_table as short_list
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
ON users_table.user_id = temp.user_id
WHERE
users_table.value_1 < $2;
$$ LANGUAGE SQL;
-- should error out
SELECT sql_subquery_test(5,5);
SELECT sql_subquery_test(1,1);
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.
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
SELECT user_id, counter
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
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
ORDER BY 2 DESC,1
LIMIT 5;
user_id | counter
---------+---------
7 | 9
8 | 9
15 | 9
16 | 9
20 | 9
2 | 5
3 | 5
4 | 5
1 | 4
2 | 4
(5 rows)
-- a very simple union query with reference table
SELECT user_id, counter
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
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
ORDER BY 2 DESC,1
LIMIT 5;
@ -37,27 +37,27 @@ DETAIL: Reference tables are not allowed with set operations
-- the same query with union all
SELECT user_id, counter
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
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
ORDER BY 2 DESC,1
LIMIT 5;
user_id | counter
---------+---------
7 | 9
7 | 9
8 | 9
15 | 9
15 | 9
2 | 5
2 | 5
3 | 5
3 | 5
4 | 5
(5 rows)
-- the same query with union all and reference table
SELECT user_id, counter
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
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
ORDER BY 2 DESC,1
LIMIT 5;
@ -66,58 +66,58 @@ DETAIL: Reference tables are not allowed with set operations
-- the same query with group by
SELECT user_id, sum(counter)
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
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
GROUP BY 1
ORDER BY 2 DESC,1
LIMIT 5;
user_id | sum
---------+-----
49 | 22
15 | 19
26 | 17
48 | 17
61 | 17
2 | 15
3 | 15
4 | 15
5 | 10
1 | 7
(5 rows)
-- the same query with UNION ALL clause
SELECT user_id, sum(counter)
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
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
GROUP BY 1
ORDER BY 2 DESC,1
LIMIT 5;
user_id | sum
---------+-----
48 | 35
61 | 30
15 | 28
49 | 25
80 | 24
2 | 32
3 | 32
4 | 23
5 | 21
1 | 15
(5 rows)
-- the same query target list entries shuffled
SELECT user_id, sum(counter)
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
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
GROUP BY 1
ORDER BY 2 DESC,1
LIMIT 5;
user_id | sum
---------+-----
49 | 22
15 | 19
26 | 17
48 | 17
61 | 17
2 | 15
3 | 15
4 | 15
5 | 10
1 | 7
(5 rows)
-- same query with GROUP BY
@ -131,13 +131,13 @@ GROUP BY
user_id
--HAVING sum(counter) > 900
ORDER BY 1,2 DESC LIMIT 5;
user_id | sum
---------+------
1 | 518
2 | 637
4 | 343
6 | 354
7 | 1374
user_id | sum
---------+-----
1 | 7
2 | 15
3 | 15
4 | 15
5 | 10
(5 rows)
-- the same query target list entries shuffled but this time the subqueries target list
@ -152,50 +152,49 @@ GROUP BY
user_id
--HAVING sum(counter) > 900
ORDER BY 1,2 DESC LIMIT 5;
user_id | sum
---------+------
1 | 518
2 | 637
4 | 343
6 | 354
7 | 1374
user_id | sum
---------+-----
1 | 7
2 | 15
3 | 15
4 | 15
5 | 10
(5 rows)
-- similar query this time more subqueries and target list contains a resjunk entry
SELECT sum(counter)
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
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
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
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
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
GROUP BY user_id ORDER BY 1 DESC LIMIT 5;
sum
-------
27772
25720
24993
24968
23508
(5 rows)
sum
-----
141
94
87
76
(4 rows)
-- similar query this time more subqueries with reference table and target list contains a resjunk entry
SELECT sum(counter)
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
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
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
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
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
GROUP BY user_id ORDER BY 1 DESC LIMIT 5;
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
SELECT sum(counter)
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
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
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
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
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
GROUP BY user_id ORDER BY 1 DESC LIMIT 5;
sum
-------
27667
25080
24814
24365
23508
(5 rows)
sum
-----
135
87
85
69
(4 rows)
-- unions within unions
SELECT *
@ -265,13 +263,13 @@ FROM (
user_id)) AS ftop
ORDER BY 2 DESC, 1 DESC
LIMIT 5;
user_id | sum
---------+--------
23 | 126017
45 | 117323
25 | 116595
17 | 116520
90 | 115843
user_id | sum
---------+-----
2 | 107
3 | 101
5 | 94
4 | 91
1 | 62
(5 rows)
-- unions within unions with reference table
@ -334,7 +332,7 @@ FROM
FROM
events_table as "events"
WHERE
event_type IN (10, 11, 12, 13, 14, 15)) events_subquery_1)
event_type IN (1, 2)) events_subquery_1)
UNION
(SELECT *
FROM
@ -343,7 +341,7 @@ FROM
FROM
events_table as "events"
WHERE
event_type IN (15, 16, 17, 18, 19) ) events_subquery_2)
event_type IN (2, 3) ) events_subquery_2)
UNION
(SELECT *
FROM
@ -352,7 +350,7 @@ FROM
FROM
events_table as "events"
WHERE
event_type IN (20, 21, 22, 23, 24, 25) ) events_subquery_3)
event_type IN (4, 5) ) events_subquery_3)
UNION
(SELECT *
FROM
@ -361,17 +359,17 @@ FROM
FROM
events_table as "events"
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"
) as final_query
GROUP BY types
ORDER BY types;
types | sumofeventtype
-------+----------------
0 | 55
1 | 38
2 | 70
3 | 58
0 | 43
1 | 42
2 | 28
3 | 25
(4 rows)
-- exactly the same query
@ -391,38 +389,38 @@ FROM
FROM
events_table as "events"
WHERE
event_type IN (10, 11, 12, 13, 14, 15))
event_type IN (1, 2))
UNION
(SELECT
"events"."user_id", "events"."time", 1 AS event
FROM
events_table as "events"
WHERE
event_type IN (15, 16, 17, 18, 19) )
event_type IN (2, 3) )
UNION
(SELECT
"events"."user_id", "events"."time", 2 AS event
FROM
events_table as "events"
WHERE
event_type IN (20, 21, 22, 23, 24, 25) )
event_type IN (4, 5) )
UNION
(SELECT
"events"."user_id", "events"."time", 3 AS event
FROM
events_table as "events"
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"
) as final_query
GROUP BY types
ORDER BY types;
types | sumofeventtype
-------+----------------
0 | 55
1 | 38
2 | 70
3 | 58
0 | 43
1 | 42
2 | 28
3 | 25
(4 rows)
-- again excatly the same query with top level wrapper removed
@ -437,37 +435,37 @@ FROM
FROM
events_table as "events"
WHERE
event_type IN (10, 11, 12, 13, 14, 15))
event_type IN (1, 2))
UNION
(SELECT
"events"."user_id", "events"."time", 1 AS event
FROM
events_table as "events"
WHERE
event_type IN (15, 16, 17, 18, 19) )
event_type IN (2, 3) )
UNION
(SELECT
"events"."user_id", "events"."time", 2 AS event
FROM
events_table as "events"
WHERE
event_type IN (20, 21, 22, 23, 24, 25) )
event_type IN (4, 5) )
UNION
(SELECT
"events"."user_id", "events"."time", 3 AS event
FROM
events_table as "events"
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 types
ORDER BY types;
types | sumofeventtype
-------+----------------
0 | 55
1 | 38
2 | 70
3 | 58
0 | 43
1 | 42
2 | 28
3 | 25
(4 rows)
-- again same query but with only two top level empty queries (i.e., no group bys)
@ -482,36 +480,36 @@ FROM
FROM
events_table as "events"
WHERE
event_type IN (10, 11, 12, 13, 14, 15))
event_type IN (1, 2))
UNION
(SELECT
"events"."user_id", "events"."time", 1 AS event
FROM
events_table as "events"
WHERE
event_type IN (15, 16, 17, 18, 19) )
event_type IN (2, 3) )
UNION
(SELECT
"events"."user_id", "events"."time", 2 AS event
FROM
events_table as "events"
WHERE
event_type IN (20, 21, 22, 23, 24, 25) )
event_type IN (4, 5) )
UNION
(SELECT
"events"."user_id", "events"."time", 3 AS event
FROM
events_table as "events"
WHERE
event_type IN (26, 27, 28, 29, 30, 13))) t1
event_type IN (6, 1))) t1
) AS t) "q"
ORDER BY 1
LIMIT 5;
user_id
---------
0
0
0
1
1
1
1
1
(5 rows)
@ -528,37 +526,37 @@ FROM
FROM
events_table as "events"
WHERE
event_type IN (10, 11, 12, 13, 14, 15))
event_type IN (1, 2))
UNION ALL
(SELECT
"events"."user_id", "events"."time", 1 AS event
FROM
events_table as "events"
WHERE
event_type IN (15, 16, 17, 18, 19) )
event_type IN (2, 3) )
UNION ALL
(SELECT
"events"."user_id", "events"."time", 2 AS event
FROM
events_table as "events"
WHERE
event_type IN (20, 21, 22, 23, 24, 25) )
event_type IN (4, 5) )
UNION ALL
(SELECT
"events"."user_id", "events"."time", 3 AS event
FROM
events_table as "events"
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 types
ORDER BY types;
types | sumofeventtype
-------+----------------
0 | 55
1 | 38
2 | 70
3 | 58
0 | 43
1 | 42
2 | 28
3 | 25
(4 rows)
-- some UNION ALL queries that are going to be pulled up
@ -572,7 +570,7 @@ FROM
) b;
count
-------
20002
202
(1 row)
-- some UNION ALL queries that are going to be pulled up with reference table
@ -599,11 +597,11 @@ ORDER BY 1 DESC
LIMIT 5;
user_id
---------
100
100
100
100
100
6
6
6
6
6
(5 rows)
-- similar query with multiple target list entries
@ -619,11 +617,11 @@ ORDER BY 1 DESC, 2 DESC
LIMIT 5;
user_id | value_3
---------+---------
100 | 999
100 | 997
100 | 991
100 | 989
100 | 988
6 | 5
6 | 5
6 | 5
6 | 5
6 | 4
(5 rows)
-- similar query group by inside the subqueries
@ -639,11 +637,11 @@ ORDER BY 2 DESC, 1 DESC
LIMIT 5;
user_id | value_3_sum
---------+-------------
10 | 64060
10 | 64060
62 | 62445
62 | 62445
26 | 60536
4 | 65
4 | 65
5 | 64
5 | 64
2 | 54
(5 rows)
-- similar query top level group by
@ -658,13 +656,13 @@ FROM
GROUP BY 1
ORDER BY 2 DESC, 1 DESC
LIMIT 5;
user_id | sum
---------+--------
23 | 123923
25 | 118087
69 | 115828
26 | 114705
3 | 113915
user_id | sum
---------+-----
2 | 119
4 | 111
3 | 100
5 | 85
1 | 53
(5 rows)
-- a long set operation list
@ -672,27 +670,27 @@ SELECT
user_id, value_3
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
(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
(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
(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
(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
(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
ORDER BY 1 DESC, 2 DESC
LIMIT 5;
user_id | value_3
---------+---------
100 | 951
99 | 558
99 | 14
98 | 987
98 | 577
6 | 5
6 | 5
6 | 3
6 | 3
6 | 3
(5 rows)
-- no partition key on the top
@ -700,28 +698,28 @@ SELECT
max(value_3)
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
(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
(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
(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
(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
(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
GROUP BY user_id
ORDER BY 1 DESC
LIMIT 5;
max
-----
997
997
996
995
995
5
5
5
5
4
(5 rows)
-- 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
SELECT sum(counter)
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
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
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
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
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
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
@ -877,17 +875,17 @@ SELECT
user_id, value_3
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
(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
(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
(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
(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
(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
ORDER BY 1 DESC, 2 DESC
LIMIT 5;
@ -931,15 +929,15 @@ SELECT
user_id, value_3
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
(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
(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
(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
(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
(SELECT 1, 2)
) b
@ -962,7 +960,7 @@ FROM
FROM
events_table as "events"
WHERE
event_type IN (10, 11, 12, 13, 14, 15)) events_subquery_1)
event_type IN (1, 2)) events_subquery_1)
UNION
(SELECT *
FROM
@ -971,7 +969,7 @@ FROM
FROM
events_table as "events"
WHERE
event_type IN (15, 16, 17, 18, 19) ) events_subquery_2)
event_type IN (2, 3) ) events_subquery_2)
UNION
(SELECT *
FROM
@ -980,7 +978,7 @@ FROM
FROM
events_table as "events"
WHERE
event_type IN (20, 21, 22, 23, 24, 25) ) events_subquery_3)
event_type IN (4, 5) ) events_subquery_3)
UNION
(SELECT *
FROM

View File

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

View File

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

View File

@ -12,9 +12,9 @@ FROM (
FROM users_table AS u,
events_table AS e
WHERE u.user_id = e.user_id
AND u.user_id >= 10
AND u.user_id <= 25
AND e.event_type IN (100, 101, 102)
AND u.user_id >= 1
AND u.user_id <= 2
AND e.event_type IN (2, 3)
) t
GROUP BY user_id
) 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
------------------------------------
------------------------------------
TRUNCATE agg_results;
INSERT INTO agg_results (user_id, value_1_agg, value_2_agg )
SELECT user_id, sum(array_length(events_table, 1)), length(hasdone_event)
FROM (
@ -43,9 +41,9 @@ FROM (
FROM users_table AS u,
events_table AS e
WHERE u.user_id = e.user_id
AND u.user_id >= 10
AND u.user_id <= 25
AND e.event_type IN (100, 101, 102)
AND u.user_id >= 1
AND u.user_id <= 2
AND e.event_type IN (1, 2)
)
UNION
(
@ -53,18 +51,17 @@ FROM (
FROM users_table AS u,
events_table AS e
WHERE u.user_id = e.user_id
AND u.user_id >= 10
AND u.user_id <= 25
AND e.event_type IN (103, 104, 105)
AND u.user_id >= 1
AND u.user_id <= 2
AND e.event_type IN (3, 4)
)
) t1 LEFT JOIN (
SELECT DISTINCT user_id,
'Has done event'::TEXT AS hasdone_event
FROM events_table AS e
WHERE e.user_id >= 10
AND e.user_id <= 25
AND e.event_type IN (106, 107, 108)
WHERE e.user_id >= 1
AND e.user_id <= 2
AND e.event_type IN (5, 6)
) t2 ON (t1.user_id = t2.user_id)
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
------------------------------------
------------------------------------
TRUNCATE agg_results;
INSERT INTO agg_results (user_id, value_1_agg, value_2_agg)
SELECT
user_id,
@ -102,9 +96,9 @@ SELECT
events_table
WHERE
users_table.user_id = events_table.user_id AND
users_table.user_id >= 10 AND
users_table.user_id <= 70 AND
events_table.event_type > 10 AND events_table.event_type < 12
users_table.user_id >= 1 AND
users_table.user_id <= 3 AND
events_table.event_type > 0 AND events_table.event_type < 2
)
UNION
(SELECT
@ -116,9 +110,9 @@ SELECT
events_table
WHERE
users_table.user_id = events_table.user_id AND
users_table.user_id >= 10 AND
users_table.user_id <= 70 AND
events_table.event_type > 12 AND events_table.event_type < 14
users_table.user_id >= 1 AND
users_table.user_id <= 3 AND
events_table.event_type > 1 AND events_table.event_type < 3
)
) AS subquery_1
LEFT JOIN
@ -128,9 +122,9 @@ SELECT
FROM
users_table
WHERE
user_id >= 10 AND
user_id <= 70 AND
users_table.value_1 > 15 AND users_table.value_1 < 17
user_id >= 1 AND
user_id <= 3 AND
users_table.value_1 > 3 AND users_table.value_1 < 5
GROUP BY
user_id
HAVING
@ -174,15 +168,15 @@ FROM (
SELECT user_id, time
FROM users_table
WHERE
user_id >= 10 AND
user_id <= 70 AND
users_table.value_1 > 10 AND users_table.value_1 < 12
user_id >= 1 AND
user_id <= 3 AND
users_table.value_1 > 3 AND users_table.value_1 < 6
) u LEFT JOIN LATERAL (
SELECT event_type, time
FROM events_table
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
GROUP BY user_id
) AS shard_union
@ -202,9 +196,9 @@ TRUNCATE agg_results;
INSERT INTO agg_results (user_id)
SELECT DISTINCT user_id
FROM users_table
WHERE user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 10 AND value_1 <= 20)
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 >= 50 AND value_1 <= 60);
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 >= 3 AND value_1 <= 4)
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
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)
SELECT user_id
FROM users_table
WHERE (value_1 = 10
OR value_1 = 11
OR value_1 = 12)
WHERE (value_1 = 1
OR value_1 = 2
OR value_1 = 3)
GROUP BY user_id
HAVING count(distinct value_1) >= 2;
@ -237,9 +231,9 @@ TRUNCATE agg_results;
INSERT INTO agg_results(user_id, value_2_agg)
SELECT user_id, value_2 FROM users_table WHERE
value_1 > 101 AND value_1 < 110
AND value_2 >= 5
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);
value_1 > 1 AND value_1 < 4
AND value_2 >= 3
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
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)
SELECT user_id, value_2 FROM users_table WHERE
value_1 = 101
AND value_2 >= 5
AND NOT EXISTS (SELECT user_id FROM events_table WHERE event_type=101 AND value_3 > 100 AND user_id=users_table.user_id);
value_1 = 1
AND value_2 >= 2
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
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)
SELECT user_id, value_2 FROM users_table WHERE
value_1 > 100
AND value_2 >= 5
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=101 AND value_3 > 100 AND user_id=users_table.user_id);
value_1 > 1
AND value_2 >= 3
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=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
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)
SELECT user_id, value_2 FROM users_table 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 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);
value_2 >= 3
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 > 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
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,
value_2
FROM users_table
WHERE value_1 > 100
AND value_1 < 124
AND value_2 >= 5
WHERE value_1 > 1
AND value_1 < 3
AND value_2 >= 1
AND EXISTS (SELECT user_id
FROM events_table
WHERE event_type > 100
AND event_type < 124
AND value_3 > 100
WHERE event_type > 1
AND event_type < 3
AND value_3 > 1
AND user_id = users_table.user_id
GROUP BY user_id
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 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;
-- 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)
Select user_id
From events_table
Where event_type = 16
And value_2 > 50
Where event_type = 2
And value_2 > 2
And user_id in
(select user_id
From users_table
Where value_1 = 15
And value_2 > 25);
Where value_1 = 2
And value_2 > 1);
-- 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;
@ -366,7 +360,7 @@ TRUNCATE agg_results;
INSERT INTO agg_results(user_id, value_1_agg)
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;
-- get some statistics from the aggregated results to ensure the results are correct
@ -386,7 +380,7 @@ select user_id from
user_id
from
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;
-- get some statistics from the aggregated results to ensure the results are correct
@ -406,14 +400,14 @@ FROM
users_table
JOIN
(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
users_table AS ma, events_table as short_list
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
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
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results;
@ -432,10 +426,10 @@ FROM
FROM
users_table AS ma, events_table as short_list
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
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
SELECT count(*), count(DISTINCT user_id), avg(user_id) FROM agg_results;
@ -454,10 +448,10 @@ FROM
FROM
users_table AS ma, events_table as short_list
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
ON users_ids.user_id = temp.user_id
WHERE temp.value_1 < 50
WHERE temp.value_1 < 3
ORDER BY 1, 2;
SELECT count(*), count(DISTINCT user_id), avg(user_id), avg(value_1_agg) FROM agg_results;
@ -476,7 +470,7 @@ FROM
FROM
users_table AS ma, events_table as short_list
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
ON users_ids.user_id = temp.user_id
ORDER BY 1, 2;

View File

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

View File

@ -97,7 +97,7 @@ SELECT * FROM
users_table, events_table
WHERE
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)
) as foo;
@ -115,7 +115,7 @@ SELECT * FROM
users_table, events_table
WHERE
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),
w2 AS (PARTITION BY users_table.user_id, (events_table.value_2 % 25) ORDER BY events_table.time)
) 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
WHERE
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),
w2 AS (PARTITION BY users_table.user_id, (events_table.value_2 % 25) ORDER BY events_table.time)
) as sub_1
@ -146,7 +146,7 @@ JOIN
users_table, events_table
WHERE
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),
w2 AS (PARTITION BY users_table.user_id, (events_table.value_2 % 50) ORDER BY events_table.time)
) as sub_2
@ -173,7 +173,7 @@ FROM
WINDOW my_win AS (PARTITION BY user_id ORDER BY count(*) DESC)
) as foo
WHERE
my_rank > 5
my_rank > 1
GROUP BY
my_rank;
@ -267,7 +267,7 @@ SELECT
FROM
users_table
WHERE
value_2 > 545 AND
value_2 > 1 AND
value_2 < ALL (
SELECT
avg(value_3) OVER (PARTITION BY user_id)
@ -704,7 +704,7 @@ SELECT
FROM
users_table
WHERE
value_2 > 545 AND
value_2 > 2 AND
value_2 < ALL (
SELECT
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');
-- 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_events_table_2009 PARTITION OF partitioned_events_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 ('2017-01-01') TO ('2018-01-01');
INSERT INTO partitioned_events_table SELECT * FROM events_table;
INSERT INTO partitioned_users_table_2009 SELECT * FROM users_table;
@ -471,28 +471,28 @@ FROM
FROM
partitioned_events_table as "events"
WHERE
event_type IN (10, 11, 12, 13, 14, 15))
event_type IN (1, 2) )
UNION
(SELECT
"events"."user_id", "events"."time", 1 AS event
FROM
partitioned_events_table as "events"
WHERE
event_type IN (15, 16, 17, 18, 19) )
event_type IN (3, 4) )
UNION
(SELECT
"events"."user_id", "events"."time", 2 AS event
FROM
partitioned_events_table as "events"
WHERE
event_type IN (20, 21, 22, 23, 24, 25) )
event_type IN (5, 6) )
UNION
(SELECT
"events"."user_id", "events"."time", 3 AS event
FROM
partitioned_events_table as "events"
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"
) AS final_query
GROUP BY types
@ -518,7 +518,7 @@ FROM
FROM
partitioned_events_table as "events"
WHERE
event_type IN (10, 11, 12, 13, 14, 15) ) events_subquery_1)
event_type IN (1, 2)) events_subquery_1)
UNION
(SELECT *
FROM
@ -533,7 +533,7 @@ FROM
events_table as "events", users_table as "users"
WHERE
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"
) as events_subquery_5
) events_subquery_2)
@ -545,7 +545,7 @@ FROM
FROM
partitioned_events_table as "events"
WHERE
event_type IN (20, 21, 22, 23, 24, 25) ) events_subquery_3)
event_type IN (3, 4)) events_subquery_3)
UNION
(SELECT *
FROM
@ -554,7 +554,7 @@ FROM
FROM
events_table as "events"
WHERE
event_type IN (26, 27, 28, 29, 30, 13)) events_subquery_4)
event_type IN (5, 6)) events_subquery_4)
) t1
GROUP BY "t1"."user_id") AS t) "q"
INNER JOIN
@ -563,7 +563,7 @@ INNER JOIN
FROM
partitioned_users_table as "users"
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
GROUP BY
types
@ -572,9 +572,9 @@ ORDER BY
-- 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_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_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_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_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 ('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 ('2017-12-01', '2017-12-02', '2017-12-03', '2017-12-04', '2017-12-05');
-- test distributing partitioned table colocated with another partitioned table
SELECT create_distributed_table('list_partitioned_events_table', 'user_id', colocate_with => 'partitioned_events_table');
@ -592,8 +592,8 @@ SELECT
FROM
events_table
WHERE
time >= '2014-01-01' AND
time <= '2014-01-15';
time >= '2017-11-21' AND
time <= '2017-12-01';
-- LEFT JOINs used with INNER JOINs on range partitioned table, list partitioned table and non-partitioned table
SELECT
@ -612,14 +612,14 @@ count(*) AS cnt, "generated_group_field"
FROM
list_partitioned_events_table as "list_partitioned_events_table"
WHERE
user_id > 80) "temp_data_queries"
user_id > 2) "temp_data_queries"
INNER JOIN
(SELECT
"users"."user_id"
FROM
partitioned_users_table as "users"
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"
LEFT JOIN
(SELECT

View File

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

File diff suppressed because it is too large Load Diff

View File

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

View File

@ -17,12 +17,12 @@ WHERE
FROM
events_table
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
user_id
)
GROUP BY user_id
HAVING count(*) > 66
HAVING count(*) > 2
ORDER BY user_id
LIMIT 5;
@ -38,13 +38,13 @@ WHERE
FROM
events_table
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
GROUP BY
user_id
)
GROUP BY user_id
HAVING count(*) > 66
HAVING count(*) > 1
ORDER BY user_id
LIMIT 5;
@ -60,13 +60,13 @@ WHERE
FROM
events_table
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
GROUP BY
user_id
)
GROUP BY user_id
HAVING count(*) > 66
HAVING count(*) > 1
ORDER BY user_id
LIMIT 5;
@ -76,7 +76,7 @@ SELECT
FROM
users_table
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)
GROUP BY
1
@ -96,7 +96,7 @@ WHERE
FROM
events_table as e2
WHERE
value_2 = 15 AND value_3 > 25 AND
value_2 = 1 AND value_3 > 3 AND
e1.user_id = e2.user_id
)
ORDER BY 1;
@ -113,12 +113,12 @@ WHERE
FROM
events_table as e2
WHERE
value_2 = 15 AND value_3 > 25 AND
value_2 = 1 AND value_3 > 3 AND
e1.user_id = e2.user_id
)
GROUP BY 1
HAVING count(*) > 122
HAVING count(*) > 2
ORDER BY 1;
-- non-correlated query with =ANY on partition keys
@ -127,14 +127,14 @@ ORDER BY 1;
FROM
users_table
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
SELECT
user_id
FROM
users_table
WHERE 118 <=
WHERE 2 <=
(SELECT
count(*)
FROM
@ -153,8 +153,8 @@ ORDER BY
-- but it is a valid query with an arbitrary subquery in
-- WHERE clause
SELECT user_id, value_2 FROM users_table WHERE
value_1 > 101 AND value_1 < 110
AND value_2 >= 5
value_1 > 1 AND value_1 < 3
AND value_2 >= 1
AND user_id IN
(
SELECT
@ -167,7 +167,7 @@ SELECT user_id, value_2 FROM users_table WHERE
min(time) AS view_homepage_time
FROM events_table
WHERE
event_type IN (10, 20, 30, 40, 50, 60, 70, 80, 90)
event_type IN (0, 1)
GROUP BY user_id
) e1 LEFT JOIN LATERAL (
SELECT
@ -177,7 +177,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_table
WHERE
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
) e2 ON true LEFT JOIN LATERAL (
SELECT
@ -187,7 +187,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_table
WHERE
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
) e3 ON true LEFT JOIN LATERAL (
SELECT
@ -197,7 +197,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_table
WHERE
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
) e4 ON true LEFT JOIN LATERAL (
SELECT
@ -205,7 +205,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_table
WHERE
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
) e5 ON true
group by e1.user_id
@ -241,9 +241,9 @@ WHERE
events_table
WHERE
users_table.user_id = events_table.user_id AND
users_table.user_id >= 10 AND
users_table.user_id <= 70 AND
events_table.event_type > 10 AND events_table.event_type < 12
users_table.user_id >= 1 AND
users_table.user_id <= 3 AND
events_table.event_type > 1 AND events_table.event_type < 3
)
UNION
(SELECT
@ -255,9 +255,9 @@ WHERE
events_table
WHERE
users_table.user_id = events_table.user_id AND
users_table.user_id >= 10 AND
users_table.user_id <= 70 AND
events_table.event_type > 12 AND events_table.event_type < 14
users_table.user_id >= 1 AND
users_table.user_id <= 3 AND
events_table.event_type > 2 AND events_table.event_type < 4
)
) AS subquery_1
LEFT JOIN
@ -267,9 +267,9 @@ WHERE
FROM
users_table
WHERE
user_id >= 10 AND
user_id <= 70 AND
users_table.value_1 > 15 AND users_table.value_1 < 17
user_id >= 1 AND
user_id <= 3 AND
users_table.value_1 > 3 AND users_table.value_1 < 5
GROUP BY
user_id
HAVING
@ -283,7 +283,7 @@ WHERE
count_pay, 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;
@ -306,9 +306,9 @@ FROM (
user_id
FROM
users_table
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 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)
WHERE value_2 >= 1
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 > 3 AND event_type <= 4 AND value_3 > 1 AND user_id = users_table.user_id)
)
) t
GROUP BY user_id
@ -322,8 +322,8 @@ ORDER BY 2 DESC, 1;
-- e4 is not joined on the partition key
SELECT user_id, value_2 FROM users_table WHERE
value_1 > 101 AND value_1 < 110
AND value_2 >= 5
value_1 > 1 AND value_1 < 2
AND value_2 >= 1
AND user_id IN
(
SELECT
@ -336,7 +336,7 @@ SELECT user_id, value_2 FROM users_table WHERE
min(time) AS view_homepage_time
FROM events_table
WHERE
event_type IN (10, 20, 30, 40, 50, 60, 70, 80, 90)
event_type IN (0, 1)
GROUP BY user_id
) e1 LEFT JOIN LATERAL (
SELECT
@ -346,7 +346,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_table
WHERE
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
) e2 ON true LEFT JOIN LATERAL (
SELECT
@ -356,7 +356,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_table
WHERE
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
) e3 ON true LEFT JOIN LATERAL (
SELECT
@ -366,7 +366,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_table
WHERE
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
) e4 ON true LEFT JOIN LATERAL (
SELECT
@ -374,7 +374,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_table
WHERE
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
) e5 ON true
group by e1.user_id
@ -406,9 +406,9 @@ WHERE
events_table
WHERE
users_table.user_id = events_table.user_id AND
users_table.user_id >= 10 AND
users_table.user_id <= 70 AND
events_table.event_type > 10 AND events_table.event_type < 12
users_table.user_id >= 1 AND
users_table.user_id <= 3 AND
events_table.event_type > 1 AND events_table.event_type < 3
)
UNION
(SELECT
@ -420,9 +420,9 @@ WHERE
events_table
WHERE
users_table.user_id = events_table.user_id AND
users_table.user_id >= 10 AND
users_table.user_id <= 70 AND
events_table.event_type > 12 AND events_table.event_type < 14
users_table.user_id >= 1 AND
users_table.user_id <= 3 AND
events_table.event_type > 2 AND events_table.event_type < 4
)
) AS subquery_1
LEFT JOIN
@ -432,9 +432,9 @@ WHERE
FROM
users_table
WHERE
user_id >= 10 AND
user_id <= 70 AND
users_table.value_1 > 15 AND users_table.value_1 < 17
user_id >= 1 AND
user_id <= 3 AND
users_table.value_1 > 3 AND users_table.value_1 < 5
GROUP BY
user_id
HAVING
@ -448,7 +448,7 @@ WHERE
count_pay, 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;
-- NOT EXISTS query has non-equi join
@ -469,8 +469,8 @@ FROM (
FROM
users_table
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 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 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 > 3 AND event_type <= 4 AND value_3 > 1 AND user_id != users_table.user_id)
)
) t
GROUP BY user_id
@ -511,7 +511,7 @@ WHERE
FROM
events_table
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
user_id
OFFSET 3
@ -548,9 +548,9 @@ WHERE user_id
-- semi join is not on the partition key for the third subquery
SELECT user_id
FROM users_table
WHERE user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 10 AND value_1 <= 20)
AND user_id IN (SELECT user_id FROM users_table WHERE value_1 >= 30 AND value_1 <= 40)
AND value_2 IN (SELECT user_id FROM users_table WHERE value_1 >= 50 AND value_1 <= 60);
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 >= 3 AND value_1 <= 4)
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
AS 'select $1 > $2;'
@ -560,9 +560,9 @@ CREATE FUNCTION test_join_function(integer, integer) RETURNS bool
-- we disallow JOINs via functions
SELECT user_id, value_2 FROM users_table WHERE
value_1 = 101
AND value_2 >= 5
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))
value_1 = 1
AND value_2 >= 2
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
LIMIT 3;

View File

@ -145,7 +145,7 @@ LIMIT 3;
-- should error out since reference table exist on the left side
-- of the left lateral join
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 user_id IN
(
@ -159,7 +159,7 @@ SELECT user_id, value_2 FROM users_table WHERE
min(time) AS view_homepage_time
FROM events_reference_table
WHERE
event_type IN (10, 20, 30, 40, 50, 60, 70, 80, 90)
event_type IN (1, 2)
GROUP BY user_id
) e1 LEFT JOIN LATERAL (
SELECT
@ -169,7 +169,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_reference_table
WHERE
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
) e2 ON true LEFT JOIN LATERAL (
SELECT
@ -179,7 +179,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_reference_table
WHERE
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
) e3 ON true LEFT JOIN LATERAL (
SELECT
@ -189,7 +189,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_reference_table
WHERE
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
) e4 ON true LEFT JOIN LATERAL (
SELECT
@ -197,7 +197,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_reference_table
WHERE
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
) e5 ON true
group by e1.user_id
@ -211,7 +211,7 @@ ORDER BY 1, 2;
FROM
users_table
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;
@ -227,8 +227,8 @@ WHERE
FROM
events_reference_table as e2
WHERE
value_2 = 15 AND
value_3 > 25 AND
value_2 = 2 AND
value_3 > 3 AND
e1.value_2 > e2.value_2
)
GROUP BY 1
@ -239,7 +239,7 @@ LIMIT 5;
-- should work since reference table is on the
-- inner part of the join
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 user_id IN
(
@ -253,7 +253,7 @@ SELECT user_id, value_2 FROM users_table WHERE
min(time) AS view_homepage_time
FROM events_table
WHERE
event_type IN (10, 20, 30, 40, 50, 60, 70, 80, 90)
event_type IN (1, 2)
GROUP BY user_id
) e1 LEFT JOIN LATERAL (
SELECT
@ -263,7 +263,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_table
WHERE
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
) e2 ON true LEFT JOIN LATERAL (
SELECT
@ -273,7 +273,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_table
WHERE
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
) e3 ON true LEFT JOIN LATERAL (
SELECT
@ -283,7 +283,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_table
WHERE
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
) e4 ON true LEFT JOIN LATERAL (
SELECT
@ -291,7 +291,7 @@ SELECT user_id, value_2 FROM users_table WHERE
FROM events_reference_table
WHERE
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
) e5 ON true
group by e1.user_id
@ -326,10 +326,10 @@ FROM users_reference_table
WHERE value_2 > ALL
(SELECT min(value_2)
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
HAVING count(*) > 66
HAVING count(*) > 3
ORDER BY 2 DESC,
1 DESC
LIMIT 5;
@ -358,12 +358,12 @@ WHERE
FROM
events_reference_table
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
users_table.user_id
)
GROUP BY user_id
HAVING count(*) > 66
HAVING count(*) > 3
ORDER BY user_id
LIMIT 5;
@ -380,11 +380,11 @@ WHERE
FROM
events_reference_table
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
(users_table.user_id * 2)
)
GROUP BY user_id
HAVING count(*) > 66
HAVING count(*) > 3
ORDER BY user_id
LIMIT 5;

View File

@ -22,14 +22,14 @@ FROM (
SELECT user_id, time
FROM users_table
WHERE
user_id >= 10 AND
user_id <= 70 AND
users_table.value_1 > 10 AND users_table.value_1 < 12
user_id >= 1 AND
user_id <= 3 AND
users_table.value_1 > 1 AND users_table.value_1 < 3
) u LEFT JOIN LATERAL (
SELECT event_type, time
FROM events_table
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
GROUP BY user_id
) AS shard_union
@ -54,25 +54,25 @@ FROM (
WHERE
user_id >= $1 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 (
SELECT event_type, time
FROM events_table
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
GROUP BY user_id
) AS shard_union
ORDER BY user_lastseen DESC, user_id;
-- should be fine with more than five executions
EXECUTE prepared_subquery_2(10, 70);
EXECUTE prepared_subquery_2(10, 70);
EXECUTE prepared_subquery_2(10, 70);
EXECUTE prepared_subquery_2(10, 70);
EXECUTE prepared_subquery_2(10, 70);
EXECUTE prepared_subquery_2(10, 70);
EXECUTE prepared_subquery_2(10, 70);
EXECUTE prepared_subquery_2(1, 3);
EXECUTE prepared_subquery_2(1, 3);
EXECUTE prepared_subquery_2(1, 3);
EXECUTE prepared_subquery_2(1, 3);
EXECUTE prepared_subquery_2(1, 3);
EXECUTE prepared_subquery_2(1, 3);
EXECUTE prepared_subquery_2(1, 3);
-- prepared statements with subqueries in WHERE clause
PREPARE prepared_subquery_3(int, int, int, int, int, int) AS
@ -88,12 +88,12 @@ ORDER BY
LIMIT 5;
-- enough times (6+) to actually use prepared statements
EXECUTE prepared_subquery_3(50, 60, 20, 10, 30, 40);
EXECUTE prepared_subquery_3(50, 60, 20, 10, 30, 40);
EXECUTE prepared_subquery_3(50, 60, 20, 10, 30, 40);
EXECUTE prepared_subquery_3(50, 60, 20, 10, 30, 40);
EXECUTE prepared_subquery_3(50, 60, 20, 10, 30, 40);
EXECUTE prepared_subquery_3(50, 60, 20, 10, 30, 40);
EXECUTE prepared_subquery_3(4, 5, 1, 0, 2, 3);
EXECUTE prepared_subquery_3(4, 5, 1, 0, 2, 3);
EXECUTE prepared_subquery_3(4, 5, 1, 0, 2, 3);
EXECUTE prepared_subquery_3(4, 5, 1, 0, 2, 3);
EXECUTE prepared_subquery_3(4, 5, 1, 0, 2, 3);
EXECUTE prepared_subquery_3(4, 5, 1, 0, 2, 3);
CREATE FUNCTION plpgsql_subquery_test(int, int) RETURNS TABLE(count bigint) AS $$
@ -110,7 +110,7 @@ BEGIN
FROM
users_table AS ma, events_table as short_list
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
ON users_table.user_id = temp.user_id
WHERE
@ -120,15 +120,15 @@ END;
$$ LANGUAGE plpgsql;
-- enough times (6+) to actually use prepared statements
SELECT plpgsql_subquery_test(10, 20);
SELECT plpgsql_subquery_test(10, 20);
SELECT plpgsql_subquery_test(10, 20);
SELECT plpgsql_subquery_test(10, 20);
SELECT plpgsql_subquery_test(10, 20);
SELECT plpgsql_subquery_test(10, 20);
SELECT plpgsql_subquery_test(1, 2);
SELECT plpgsql_subquery_test(1, 2);
SELECT plpgsql_subquery_test(1, 2);
SELECT plpgsql_subquery_test(1, 2);
SELECT plpgsql_subquery_test(1, 2);
SELECT plpgsql_subquery_test(1, 2);
-- 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 $$
SELECT
@ -141,7 +141,7 @@ CREATE FUNCTION sql_subquery_test(int, int) RETURNS bigint AS $$
FROM
users_table AS ma, events_table as short_list
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
ON users_table.user_id = temp.user_id
WHERE
@ -149,7 +149,7 @@ CREATE FUNCTION sql_subquery_test(int, int) RETURNS bigint AS $$
$$ LANGUAGE SQL;
-- should error out
SELECT sql_subquery_test(5,5);
SELECT sql_subquery_test(1,1);
DROP FUNCTION plpgsql_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
SELECT user_id, counter
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
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
ORDER BY 2 DESC,1
LIMIT 5;
@ -20,9 +20,9 @@ LIMIT 5;
-- a very simple union query with reference table
SELECT user_id, counter
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
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
ORDER BY 2 DESC,1
LIMIT 5;
@ -30,9 +30,9 @@ LIMIT 5;
-- the same query with union all
SELECT user_id, counter
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
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
ORDER BY 2 DESC,1
LIMIT 5;
@ -40,9 +40,9 @@ LIMIT 5;
-- the same query with union all and reference table
SELECT user_id, counter
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
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
ORDER BY 2 DESC,1
LIMIT 5;
@ -50,9 +50,9 @@ LIMIT 5;
-- the same query with group by
SELECT user_id, sum(counter)
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
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
GROUP BY 1
ORDER BY 2 DESC,1
@ -61,9 +61,9 @@ LIMIT 5;
-- the same query with UNION ALL clause
SELECT user_id, sum(counter)
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
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
GROUP BY 1
ORDER BY 2 DESC,1
@ -72,9 +72,9 @@ LIMIT 5;
-- the same query target list entries shuffled
SELECT user_id, sum(counter)
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
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
GROUP BY 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
SELECT sum(counter)
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
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
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
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
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
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
SELECT sum(counter)
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
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
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
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
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
GROUP BY user_id ORDER BY 1 DESC LIMIT 5;
-- similar query as above, with UNION ALL
SELECT sum(counter)
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
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
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
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
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
GROUP BY user_id ORDER BY 1 DESC LIMIT 5;
@ -254,7 +254,7 @@ FROM
FROM
events_table as "events"
WHERE
event_type IN (10, 11, 12, 13, 14, 15)) events_subquery_1)
event_type IN (1, 2)) events_subquery_1)
UNION
(SELECT *
FROM
@ -263,7 +263,7 @@ FROM
FROM
events_table as "events"
WHERE
event_type IN (15, 16, 17, 18, 19) ) events_subquery_2)
event_type IN (2, 3) ) events_subquery_2)
UNION
(SELECT *
FROM
@ -272,7 +272,7 @@ FROM
FROM
events_table as "events"
WHERE
event_type IN (20, 21, 22, 23, 24, 25) ) events_subquery_3)
event_type IN (4, 5) ) events_subquery_3)
UNION
(SELECT *
FROM
@ -281,7 +281,7 @@ FROM
FROM
events_table as "events"
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"
) as final_query
GROUP BY types
@ -304,28 +304,28 @@ FROM
FROM
events_table as "events"
WHERE
event_type IN (10, 11, 12, 13, 14, 15))
event_type IN (1, 2))
UNION
(SELECT
"events"."user_id", "events"."time", 1 AS event
FROM
events_table as "events"
WHERE
event_type IN (15, 16, 17, 18, 19) )
event_type IN (2, 3) )
UNION
(SELECT
"events"."user_id", "events"."time", 2 AS event
FROM
events_table as "events"
WHERE
event_type IN (20, 21, 22, 23, 24, 25) )
event_type IN (4, 5) )
UNION
(SELECT
"events"."user_id", "events"."time", 3 AS event
FROM
events_table as "events"
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"
) as final_query
GROUP BY types
@ -343,28 +343,28 @@ FROM
FROM
events_table as "events"
WHERE
event_type IN (10, 11, 12, 13, 14, 15))
event_type IN (1, 2))
UNION
(SELECT
"events"."user_id", "events"."time", 1 AS event
FROM
events_table as "events"
WHERE
event_type IN (15, 16, 17, 18, 19) )
event_type IN (2, 3) )
UNION
(SELECT
"events"."user_id", "events"."time", 2 AS event
FROM
events_table as "events"
WHERE
event_type IN (20, 21, 22, 23, 24, 25) )
event_type IN (4, 5) )
UNION
(SELECT
"events"."user_id", "events"."time", 3 AS event
FROM
events_table as "events"
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 types
ORDER BY types;
@ -381,28 +381,28 @@ FROM
FROM
events_table as "events"
WHERE
event_type IN (10, 11, 12, 13, 14, 15))
event_type IN (1, 2))
UNION
(SELECT
"events"."user_id", "events"."time", 1 AS event
FROM
events_table as "events"
WHERE
event_type IN (15, 16, 17, 18, 19) )
event_type IN (2, 3) )
UNION
(SELECT
"events"."user_id", "events"."time", 2 AS event
FROM
events_table as "events"
WHERE
event_type IN (20, 21, 22, 23, 24, 25) )
event_type IN (4, 5) )
UNION
(SELECT
"events"."user_id", "events"."time", 3 AS event
FROM
events_table as "events"
WHERE
event_type IN (26, 27, 28, 29, 30, 13))) t1
event_type IN (6, 1))) t1
) AS t) "q"
ORDER BY 1
LIMIT 5;
@ -419,28 +419,28 @@ FROM
FROM
events_table as "events"
WHERE
event_type IN (10, 11, 12, 13, 14, 15))
event_type IN (1, 2))
UNION ALL
(SELECT
"events"."user_id", "events"."time", 1 AS event
FROM
events_table as "events"
WHERE
event_type IN (15, 16, 17, 18, 19) )
event_type IN (2, 3) )
UNION ALL
(SELECT
"events"."user_id", "events"."time", 2 AS event
FROM
events_table as "events"
WHERE
event_type IN (20, 21, 22, 23, 24, 25) )
event_type IN (4, 5) )
UNION ALL
(SELECT
"events"."user_id", "events"."time", 3 AS event
FROM
events_table as "events"
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 types
ORDER BY types;
@ -519,17 +519,17 @@ SELECT
user_id, value_3
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
(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
(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
(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
(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
(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
ORDER BY 1 DESC, 2 DESC
LIMIT 5;
@ -539,17 +539,17 @@ SELECT
max(value_3)
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
(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
(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
(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
(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
(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
GROUP BY user_id
ORDER BY 1 DESC
@ -570,15 +570,15 @@ GROUP BY user_id;
-- partition key is not selected
SELECT sum(counter)
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
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
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
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
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
GROUP BY user_id ORDER BY 1 DESC LIMIT 5;
@ -703,17 +703,17 @@ SELECT
user_id, value_3
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
(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
(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
(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
(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
(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
ORDER BY 1 DESC, 2 DESC
LIMIT 5;
@ -753,15 +753,15 @@ SELECT
user_id, value_3
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
(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
(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
(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
(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
(SELECT 1, 2)
) b
@ -783,7 +783,7 @@ FROM
FROM
events_table as "events"
WHERE
event_type IN (10, 11, 12, 13, 14, 15)) events_subquery_1)
event_type IN (1, 2)) events_subquery_1)
UNION
(SELECT *
FROM
@ -792,7 +792,7 @@ FROM
FROM
events_table as "events"
WHERE
event_type IN (15, 16, 17, 18, 19) ) events_subquery_2)
event_type IN (2, 3) ) events_subquery_2)
UNION
(SELECT *
FROM
@ -801,7 +801,7 @@ FROM
FROM
events_table as "events"
WHERE
event_type IN (20, 21, 22, 23, 24, 25) ) events_subquery_3)
event_type IN (4, 5) ) events_subquery_3)
UNION
(SELECT *
FROM

View File

@ -100,7 +100,7 @@ SELECT * FROM
users_table, events_table
WHERE
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)
) as foo
ORDER BY 3 DESC, 1 DESC, 2 DESC NULLS LAST
@ -115,7 +115,7 @@ SELECT * FROM
users_table, events_table
WHERE
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),
w2 AS (PARTITION BY users_table.user_id, (events_table.value_2 % 25) ORDER BY events_table.time)
) 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
WHERE
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),
w2 AS (PARTITION BY users_table.user_id, (events_table.value_2 % 25) ORDER BY events_table.time)
) as sub_1
@ -143,7 +143,7 @@ JOIN
users_table, events_table
WHERE
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),
w2 AS (PARTITION BY users_table.user_id, (events_table.value_2 % 50) ORDER BY events_table.time)
) as sub_2
@ -167,7 +167,7 @@ FROM
WINDOW my_win AS (PARTITION BY user_id ORDER BY count(*) DESC)
) as foo
WHERE
my_rank > 5
my_rank > 1
GROUP BY
my_rank
ORDER BY
@ -186,7 +186,7 @@ FROM
events_table
GROUP BY
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
WHERE
my_rank > 0
@ -246,7 +246,7 @@ FROM
DISTINCT user_id, it_name, count(id) OVER (PARTITION BY user_id, id)
FROM
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
ORDER BY
1, 2, 3
@ -308,17 +308,17 @@ SELECT
max(avg)
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
(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
(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
(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
(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
(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
GROUP BY user_id
ORDER BY 1 DESC
@ -364,7 +364,7 @@ SELECT
FROM
users_table
WHERE
value_2 > 545 AND
value_2 > 1 AND
value_2 < ALL (
SELECT
avg(value_3) OVER (PARTITION BY user_id)
@ -395,7 +395,7 @@ FROM (
GROUP BY
user_id, rank
ORDER BY
difference DESC, rank DESC
difference DESC, rank DESC, user_id
LIMIT 20;
SELECT * FROM (
@ -421,7 +421,7 @@ WHERE
f3.user_id=f2.user_id
) a
ORDER BY
abs DESC
abs DESC, user_id
LIMIT 10;
@ -535,7 +535,7 @@ SELECT * FROM
users_table, events_table
WHERE
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),
w2 AS (PARTITION BY users_table.user_id+1, (events_table.value_2 % 25) ORDER BY events_table.time)
) as foo
@ -551,7 +551,7 @@ SELECT * FROM
users_table, events_table
WHERE
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),
w2 AS (ORDER BY events_table.time)
) as foo
@ -574,7 +574,7 @@ FROM
WINDOW my_win AS (ORDER BY avg(event_type))
) as foo
WHERE
my_rank > 125
my_rank > 1
ORDER BY
3 DESC, 1 DESC,2 DESC
LIMIT
@ -594,7 +594,7 @@ FROM
WINDOW my_win AS (PARTITION BY date_trunc('day', time) ORDER BY avg(event_type))
) as foo
WHERE
my_rank > 125
my_rank > 1
ORDER BY
3 DESC, 1 DESC,2 DESC
LIMIT
@ -651,17 +651,17 @@ SELECT
max(avg)
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
(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
(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
(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
(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
(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
GROUP BY user_id
ORDER BY 1 DESC

View File

@ -158,13 +158,13 @@ DROP VIEW priority_orders;
CREATE VIEW recent_users AS
SELECT user_id, max(time) as lastseen FROM users_table
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;
-- create a view for recent_events
CREATE VIEW recent_events AS
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;
@ -223,14 +223,14 @@ SELECT count(*)
WHERE ru.user_id IS NULL;
-- join between view and table
-- users who has recent activity and they have an entry with value_1 is less than 15
SELECT ut.* FROM recent_users ru JOIN users_table ut USING (user_id) WHERE ut.value_1 < 15 ORDER BY 1,2;
-- 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 < 3 ORDER BY 1,2;
-- 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
FROM recent_users ru
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;
-- 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
FROM recent_users ru
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
ORDER BY 2 DESC, 1;
@ -252,7 +252,7 @@ SELECT * FROM
ORDER BY 2 DESC, 1;
-- 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);
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;
-- 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
(SELECT user_id FROM recent_users)
@ -274,7 +274,7 @@ SELECT *
(SELECT user_id FROM recent_users)
UNION
(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;
-- union all also works for views
@ -283,7 +283,7 @@ SELECT *
(SELECT user_id FROM recent_users)
UNION ALL
(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;
SELECT count(*)
@ -291,7 +291,7 @@ SELECT count(*)
(SELECT user_id FROM recent_users)
UNION
(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
SELECT count(*)
@ -299,51 +299,51 @@ SELECT count(*)
(SELECT user_id FROM recent_users)
UNION ALL
(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
SELECT count(*)
FROM (
(SELECT user_id FROM (SELECT user_id, max(time) as lastseen FROM users_table
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
(SELECT user_id FROM (SELECT * FROM users_table WHERE value_1 >= 120 and value_1 <150) bb) ) u
WHERE user_id < 15 AND user_id > 10;
(SELECT user_id FROM (SELECT * FROM users_table WHERE value_1 >= 1 and value_1 < 3) bb) ) u
WHERE user_id < 2 AND user_id > 0;
SELECT count(*)
FROM (
(SELECT user_id FROM (SELECT user_id, max(time) as lastseen FROM users_table
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
(SELECT user_id FROM (SELECT * FROM users_table WHERE value_1 >= 120 and value_1 <150) bb) ) u
WHERE user_id < 15 AND user_id > 10;
(SELECT user_id FROM (SELECT * FROM users_table WHERE value_1 >= 1 and value_1 < 3) bb) ) u
WHERE user_id < 2 AND user_id > 0;
-- test distinct
-- 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;
SELECT * FROM distinct_user_with_value_1_15 ORDER BY user_id;
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_3 ORDER BY user_id;
-- 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;
-- CTEs are not supported even if they are on views
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;
-- this is single shard query but still not supported since it has view + cte
-- 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)
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;
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)
UNION
(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;
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_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 selected_users;
DROP VIEW recent_events;