mirror of https://github.com/citusdata/citus.git
692 lines
32 KiB
Plaintext
692 lines
32 KiB
Plaintext
-- ===================================================================
|
|
-- test recursive planning functionality with subqueries in WHERE
|
|
-- ===================================================================
|
|
CREATE SCHEMA subquery_in_where;
|
|
SET search_path TO subquery_in_where, public;
|
|
SET client_min_messages TO DEBUG1;
|
|
--CTEs can be used as a recurring tuple with subqueries in WHERE
|
|
WITH event_id
|
|
AS (SELECT user_id AS events_user_id,
|
|
time AS events_time,
|
|
event_type
|
|
FROM events_table)
|
|
SELECT Count(*)
|
|
FROM event_id
|
|
WHERE events_user_id IN (SELECT user_id
|
|
FROM users_table);
|
|
DEBUG: generating subplan 1_1 for CTE event_id: SELECT user_id AS events_user_id, "time" AS events_time, event_type FROM public.events_table
|
|
DEBUG: generating subplan 1_2 for subquery SELECT user_id FROM public.users_table
|
|
DEBUG: Plan 1 query after replacing subqueries and CTEs: SELECT count(*) AS count FROM (SELECT intermediate_result.events_user_id, intermediate_result.events_time, intermediate_result.event_type FROM read_intermediate_result('1_1'::text, 'binary'::citus_copy_format) intermediate_result(events_user_id integer, events_time timestamp without time zone, event_type integer)) event_id WHERE (events_user_id OPERATOR(pg_catalog.=) ANY (SELECT intermediate_result.user_id FROM read_intermediate_result('1_2'::text, 'binary'::citus_copy_format) intermediate_result(user_id integer)))
|
|
count
|
|
-------
|
|
101
|
|
(1 row)
|
|
|
|
--Correlated subqueries can not be used in WHERE clause
|
|
WITH event_id
|
|
AS (SELECT user_id AS events_user_id,
|
|
time AS events_time,
|
|
event_type
|
|
FROM events_table)
|
|
SELECT Count(*)
|
|
FROM event_id
|
|
WHERE events_user_id IN (SELECT user_id
|
|
FROM users_table
|
|
WHERE users_table.time = events_time);
|
|
DEBUG: generating subplan 4_1 for CTE event_id: SELECT user_id AS events_user_id, "time" AS events_time, event_type FROM public.events_table
|
|
DEBUG: Plan 4 query after replacing subqueries and CTEs: SELECT count(*) AS count FROM (SELECT intermediate_result.events_user_id, intermediate_result.events_time, intermediate_result.event_type FROM read_intermediate_result('4_1'::text, 'binary'::citus_copy_format) intermediate_result(events_user_id integer, events_time timestamp without time zone, event_type integer)) event_id WHERE (events_user_id OPERATOR(pg_catalog.=) ANY (SELECT users_table.user_id FROM public.users_table WHERE (users_table."time" OPERATOR(pg_catalog.=) event_id.events_time)))
|
|
ERROR: cannot pushdown the subquery
|
|
DETAIL: Complex subqueries and CTEs are not allowed in the FROM clause when the query has subqueries in the WHERE clause and it references a column from another query
|
|
-- Recurring tuples as empty join tree
|
|
SELECT *
|
|
FROM (SELECT 1 AS id,
|
|
2 AS value_1,
|
|
3 AS value_3) AS tt1
|
|
WHERE id IN (SELECT user_id
|
|
FROM events_table);
|
|
DEBUG: generating subplan 6_1 for subquery SELECT user_id, NULL::timestamp without time zone AS "time", NULL::integer AS event_type, NULL::integer AS value_2, NULL::double precision AS value_3, NULL::bigint AS value_4 FROM public.events_table WHERE true
|
|
DEBUG: Plan 6 query after replacing subqueries and CTEs: SELECT id, value_1, value_3 FROM (SELECT 1 AS id, 2 AS value_1, 3 AS value_3) tt1 WHERE (id OPERATOR(pg_catalog.=) ANY (SELECT events_table.user_id FROM (SELECT intermediate_result.user_id, intermediate_result."time", intermediate_result.event_type, intermediate_result.value_2, intermediate_result.value_3, intermediate_result.value_4 FROM read_intermediate_result('6_1'::text, 'binary'::citus_copy_format) intermediate_result(user_id integer, "time" timestamp without time zone, event_type integer, value_2 integer, value_3 double precision, value_4 bigint)) events_table(user_id, "time", event_type, value_2, value_3, value_4)))
|
|
id | value_1 | value_3
|
|
----+---------+---------
|
|
1 | 2 | 3
|
|
(1 row)
|
|
|
|
-- Recurring tuples in from clause as CTE and SET operation in WHERE clause
|
|
SELECT Count(*)
|
|
FROM (WITH event_id AS
|
|
(SELECT user_id AS events_user_id, time AS events_time, event_type
|
|
FROM events_table)
|
|
SELECT events_user_id, events_time, event_type
|
|
FROM event_id
|
|
ORDER BY 1,2,3
|
|
LIMIT 10) AS sub_table
|
|
WHERE events_user_id IN (
|
|
(SELECT user_id
|
|
FROM users_table
|
|
ORDER BY 1
|
|
LIMIT 10)
|
|
UNION ALL
|
|
(SELECT value_1
|
|
FROM users_table
|
|
ORDER BY 1
|
|
limit 10));
|
|
DEBUG: push down of limit count: 10
|
|
DEBUG: generating subplan 8_1 for subquery SELECT user_id FROM public.users_table ORDER BY user_id LIMIT 10
|
|
DEBUG: push down of limit count: 10
|
|
DEBUG: generating subplan 8_2 for subquery SELECT value_1 FROM public.users_table ORDER BY value_1 LIMIT 10
|
|
DEBUG: generating subplan 8_3 for subquery SELECT intermediate_result.user_id FROM read_intermediate_result('8_1'::text, 'binary'::citus_copy_format) intermediate_result(user_id integer) UNION ALL SELECT intermediate_result.value_1 FROM read_intermediate_result('8_2'::text, 'binary'::citus_copy_format) intermediate_result(value_1 integer)
|
|
DEBUG: generating subplan 8_4 for CTE event_id: SELECT user_id AS events_user_id, "time" AS events_time, event_type FROM public.events_table
|
|
DEBUG: generating subplan 8_5 for subquery SELECT events_user_id, events_time, event_type FROM (SELECT intermediate_result.events_user_id, intermediate_result.events_time, intermediate_result.event_type FROM read_intermediate_result('8_4'::text, 'binary'::citus_copy_format) intermediate_result(events_user_id integer, events_time timestamp without time zone, event_type integer)) event_id ORDER BY events_user_id, events_time, event_type LIMIT 10
|
|
DEBUG: Plan 8 query after replacing subqueries and CTEs: SELECT count(*) AS count FROM (SELECT intermediate_result.events_user_id, intermediate_result.events_time, intermediate_result.event_type FROM read_intermediate_result('8_5'::text, 'binary'::citus_copy_format) intermediate_result(events_user_id integer, events_time timestamp without time zone, event_type integer)) sub_table WHERE (events_user_id OPERATOR(pg_catalog.=) ANY (SELECT intermediate_result.user_id FROM read_intermediate_result('8_3'::text, 'binary'::citus_copy_format) intermediate_result(user_id integer)))
|
|
count
|
|
-------
|
|
10
|
|
(1 row)
|
|
|
|
-- Recurring tuples in from clause as SET operation on recursively plannable
|
|
-- queries and CTE in WHERE clause
|
|
SELECT
|
|
*
|
|
FROM
|
|
(
|
|
(SELECT
|
|
user_id
|
|
FROM
|
|
users_table
|
|
ORDER BY
|
|
user_id ASC
|
|
LIMIT
|
|
10
|
|
)
|
|
UNION ALL
|
|
(SELECT
|
|
value_1
|
|
FROM
|
|
users_table
|
|
ORDER BY
|
|
value_1 ASC
|
|
LIMIT
|
|
10
|
|
)
|
|
) as SUB_TABLE
|
|
WHERE
|
|
user_id
|
|
IN
|
|
(
|
|
WITH event_id AS (
|
|
SELECT
|
|
user_id as events_user_id, time as events_time, event_type
|
|
FROM
|
|
events_table
|
|
)
|
|
SELECT
|
|
events_user_id
|
|
FROM
|
|
event_id
|
|
ORDER BY
|
|
events_user_id
|
|
LIMIT
|
|
10
|
|
);
|
|
DEBUG: generating subplan 14_1 for CTE event_id: SELECT user_id AS events_user_id, "time" AS events_time, event_type FROM public.events_table
|
|
DEBUG: generating subplan 14_2 for subquery SELECT events_user_id FROM (SELECT intermediate_result.events_user_id, intermediate_result.events_time, intermediate_result.event_type FROM read_intermediate_result('14_1'::text, 'binary'::citus_copy_format) intermediate_result(events_user_id integer, events_time timestamp without time zone, event_type integer)) event_id ORDER BY events_user_id LIMIT 10
|
|
DEBUG: push down of limit count: 10
|
|
DEBUG: generating subplan 14_3 for subquery SELECT user_id FROM public.users_table ORDER BY user_id LIMIT 10
|
|
DEBUG: push down of limit count: 10
|
|
DEBUG: generating subplan 14_4 for subquery SELECT value_1 FROM public.users_table ORDER BY value_1 LIMIT 10
|
|
DEBUG: generating subplan 14_5 for subquery SELECT intermediate_result.user_id FROM read_intermediate_result('14_3'::text, 'binary'::citus_copy_format) intermediate_result(user_id integer) UNION ALL SELECT intermediate_result.value_1 FROM read_intermediate_result('14_4'::text, 'binary'::citus_copy_format) intermediate_result(value_1 integer)
|
|
DEBUG: Plan 14 query after replacing subqueries and CTEs: SELECT user_id FROM (SELECT intermediate_result.user_id FROM read_intermediate_result('14_5'::text, 'binary'::citus_copy_format) intermediate_result(user_id integer)) sub_table WHERE (user_id OPERATOR(pg_catalog.=) ANY (SELECT intermediate_result.events_user_id FROM read_intermediate_result('14_2'::text, 'binary'::citus_copy_format) intermediate_result(events_user_id integer)))
|
|
user_id
|
|
---------
|
|
1
|
|
1
|
|
1
|
|
1
|
|
1
|
|
1
|
|
1
|
|
(7 rows)
|
|
|
|
-- Complex target list in WHERE clause
|
|
SELECT
|
|
COUNT(*)
|
|
FROM
|
|
(SELECT
|
|
user_id as events_user_id, time as events_time, event_type
|
|
FROM
|
|
events_table
|
|
ORDER BY
|
|
1,2
|
|
LIMIT
|
|
10
|
|
) as SUB_TABLE
|
|
WHERE
|
|
events_user_id
|
|
<=ANY (
|
|
SELECT
|
|
max(abs(user_id * 1) + mod(user_id, 3)) as val_1
|
|
FROM
|
|
users_table
|
|
GROUP BY
|
|
user_id
|
|
);
|
|
DEBUG: push down of limit count: 10
|
|
DEBUG: generating subplan 20_1 for subquery SELECT user_id AS events_user_id, "time" AS events_time, event_type FROM public.events_table ORDER BY user_id, "time" LIMIT 10
|
|
DEBUG: generating subplan 20_2 for subquery SELECT max((abs((user_id OPERATOR(pg_catalog.*) 1)) OPERATOR(pg_catalog.+) mod(user_id, 3))) AS val_1 FROM public.users_table GROUP BY user_id
|
|
DEBUG: Plan 20 query after replacing subqueries and CTEs: SELECT count(*) AS count FROM (SELECT intermediate_result.events_user_id, intermediate_result.events_time, intermediate_result.event_type FROM read_intermediate_result('20_1'::text, 'binary'::citus_copy_format) intermediate_result(events_user_id integer, events_time timestamp without time zone, event_type integer)) sub_table WHERE (events_user_id OPERATOR(pg_catalog.<=) ANY (SELECT intermediate_result.val_1 FROM read_intermediate_result('20_2'::text, 'binary'::citus_copy_format) intermediate_result(val_1 integer)))
|
|
count
|
|
-------
|
|
10
|
|
(1 row)
|
|
|
|
-- DISTINCT clause in WHERE
|
|
SELECT
|
|
COUNT(*)
|
|
FROM
|
|
(SELECT
|
|
user_id as events_user_id, time as events_time, event_type
|
|
FROM
|
|
events_table
|
|
LIMIT
|
|
10
|
|
) as SUB_TABLE
|
|
WHERE
|
|
events_user_id
|
|
IN (
|
|
SELECT
|
|
distinct user_id
|
|
FROM
|
|
users_table
|
|
GROUP BY
|
|
user_id
|
|
);
|
|
DEBUG: push down of limit count: 10
|
|
DEBUG: generating subplan 23_1 for subquery SELECT user_id AS events_user_id, "time" AS events_time, event_type FROM public.events_table LIMIT 10
|
|
DEBUG: generating subplan 23_2 for subquery SELECT DISTINCT user_id FROM public.users_table GROUP BY user_id
|
|
DEBUG: Plan 23 query after replacing subqueries and CTEs: SELECT count(*) AS count FROM (SELECT intermediate_result.events_user_id, intermediate_result.events_time, intermediate_result.event_type FROM read_intermediate_result('23_1'::text, 'binary'::citus_copy_format) intermediate_result(events_user_id integer, events_time timestamp without time zone, event_type integer)) sub_table WHERE (events_user_id OPERATOR(pg_catalog.=) ANY (SELECT intermediate_result.user_id FROM read_intermediate_result('23_2'::text, 'binary'::citus_copy_format) intermediate_result(user_id integer)))
|
|
count
|
|
-------
|
|
10
|
|
(1 row)
|
|
|
|
-- AND in WHERE clause
|
|
SELECT
|
|
COUNT(*)
|
|
FROM
|
|
(SELECT
|
|
user_id as events_user_id, time as events_time, event_type
|
|
FROM
|
|
events_table
|
|
ORDER BY
|
|
1,2,3
|
|
LIMIT
|
|
10
|
|
) as SUB_TABLE
|
|
WHERE
|
|
events_user_id
|
|
>=ANY (
|
|
SELECT
|
|
min(user_id)
|
|
FROM
|
|
users_table
|
|
GROUP BY
|
|
user_id
|
|
)
|
|
AND
|
|
events_user_id
|
|
<=ANY (
|
|
SELECT
|
|
max(user_id)
|
|
FROM
|
|
users_table
|
|
GROUP BY
|
|
user_id
|
|
);
|
|
DEBUG: push down of limit count: 10
|
|
DEBUG: generating subplan 26_1 for subquery SELECT user_id AS events_user_id, "time" AS events_time, event_type FROM public.events_table ORDER BY user_id, "time", event_type LIMIT 10
|
|
DEBUG: generating subplan 26_2 for subquery SELECT min(user_id) AS min FROM public.users_table GROUP BY user_id
|
|
DEBUG: generating subplan 26_3 for subquery SELECT max(user_id) AS max FROM public.users_table GROUP BY user_id
|
|
DEBUG: Plan 26 query after replacing subqueries and CTEs: SELECT count(*) AS count FROM (SELECT intermediate_result.events_user_id, intermediate_result.events_time, intermediate_result.event_type FROM read_intermediate_result('26_1'::text, 'binary'::citus_copy_format) intermediate_result(events_user_id integer, events_time timestamp without time zone, event_type integer)) sub_table WHERE ((events_user_id OPERATOR(pg_catalog.>=) ANY (SELECT intermediate_result.min FROM read_intermediate_result('26_2'::text, 'binary'::citus_copy_format) intermediate_result(min integer))) AND (events_user_id OPERATOR(pg_catalog.<=) ANY (SELECT intermediate_result.max FROM read_intermediate_result('26_3'::text, 'binary'::citus_copy_format) intermediate_result(max integer))))
|
|
count
|
|
-------
|
|
10
|
|
(1 row)
|
|
|
|
-- AND in WHERE clause, part of the AND is pushdownable other is not
|
|
SELECT
|
|
COUNT(*)
|
|
FROM
|
|
(SELECT
|
|
user_id as events_user_id, time as events_time, event_type
|
|
FROM
|
|
events_table
|
|
ORDER BY
|
|
1,2,3
|
|
LIMIT
|
|
10
|
|
) as SUB_TABLE
|
|
WHERE
|
|
events_user_id
|
|
>=ANY (
|
|
SELECT
|
|
min(user_id)
|
|
FROM
|
|
users_table
|
|
GROUP BY
|
|
user_id
|
|
)
|
|
AND
|
|
events_user_id
|
|
<=ANY (
|
|
SELECT
|
|
max(value_2)
|
|
FROM
|
|
users_table
|
|
GROUP BY
|
|
user_id
|
|
);
|
|
DEBUG: push down of limit count: 10
|
|
DEBUG: generating subplan 30_1 for subquery SELECT user_id AS events_user_id, "time" AS events_time, event_type FROM public.events_table ORDER BY user_id, "time", event_type LIMIT 10
|
|
DEBUG: generating subplan 30_2 for subquery SELECT min(user_id) AS min FROM public.users_table GROUP BY user_id
|
|
DEBUG: generating subplan 30_3 for subquery SELECT max(value_2) AS max FROM public.users_table GROUP BY user_id
|
|
DEBUG: Plan 30 query after replacing subqueries and CTEs: SELECT count(*) AS count FROM (SELECT intermediate_result.events_user_id, intermediate_result.events_time, intermediate_result.event_type FROM read_intermediate_result('30_1'::text, 'binary'::citus_copy_format) intermediate_result(events_user_id integer, events_time timestamp without time zone, event_type integer)) sub_table WHERE ((events_user_id OPERATOR(pg_catalog.>=) ANY (SELECT intermediate_result.min FROM read_intermediate_result('30_2'::text, 'binary'::citus_copy_format) intermediate_result(min integer))) AND (events_user_id OPERATOR(pg_catalog.<=) ANY (SELECT intermediate_result.max FROM read_intermediate_result('30_3'::text, 'binary'::citus_copy_format) intermediate_result(max integer))))
|
|
count
|
|
-------
|
|
10
|
|
(1 row)
|
|
|
|
-- Planning subqueries in WHERE clause in CTE recursively
|
|
WITH cte AS (
|
|
SELECT
|
|
*
|
|
FROM
|
|
(SELECT
|
|
*
|
|
FROM
|
|
users_table
|
|
ORDER BY
|
|
user_id ASC,
|
|
value_2 DESC
|
|
LIMIT
|
|
10
|
|
) as sub_table
|
|
WHERE
|
|
user_id
|
|
IN
|
|
(SELECT
|
|
value_2
|
|
FROM
|
|
events_table
|
|
)
|
|
)
|
|
SELECT
|
|
COUNT(*)
|
|
FROM
|
|
cte;
|
|
DEBUG: generating subplan 34_1 for CTE cte: SELECT user_id, "time", value_1, value_2, value_3, value_4 FROM (SELECT users_table.user_id, users_table."time", users_table.value_1, users_table.value_2, users_table.value_3, users_table.value_4 FROM public.users_table ORDER BY users_table.user_id, users_table.value_2 DESC LIMIT 10) sub_table WHERE (user_id OPERATOR(pg_catalog.=) ANY (SELECT events_table.value_2 FROM public.events_table))
|
|
DEBUG: push down of limit count: 10
|
|
DEBUG: generating subplan 35_1 for subquery SELECT user_id, "time", value_1, value_2, value_3, value_4 FROM public.users_table ORDER BY user_id, value_2 DESC LIMIT 10
|
|
DEBUG: generating subplan 35_2 for subquery SELECT value_2 FROM public.events_table
|
|
DEBUG: Plan 35 query after replacing subqueries and CTEs: SELECT user_id, "time", value_1, value_2, value_3, value_4 FROM (SELECT intermediate_result.user_id, intermediate_result."time", intermediate_result.value_1, intermediate_result.value_2, intermediate_result.value_3, intermediate_result.value_4 FROM read_intermediate_result('35_1'::text, 'binary'::citus_copy_format) intermediate_result(user_id integer, "time" timestamp without time zone, value_1 integer, value_2 integer, value_3 double precision, value_4 bigint)) sub_table WHERE (user_id OPERATOR(pg_catalog.=) ANY (SELECT intermediate_result.value_2 FROM read_intermediate_result('35_2'::text, 'binary'::citus_copy_format) intermediate_result(value_2 integer)))
|
|
DEBUG: Plan 34 query after replacing subqueries and CTEs: SELECT count(*) AS count FROM (SELECT intermediate_result.user_id, intermediate_result."time", intermediate_result.value_1, intermediate_result.value_2, intermediate_result.value_3, intermediate_result.value_4 FROM read_intermediate_result('34_1'::text, 'binary'::citus_copy_format) intermediate_result(user_id integer, "time" timestamp without time zone, value_1 integer, value_2 integer, value_3 double precision, value_4 bigint)) cte
|
|
count
|
|
-------
|
|
10
|
|
(1 row)
|
|
|
|
-- Planing subquery in WHERE clause in FROM clause of a subquery recursively
|
|
SELECT
|
|
COUNT(*)
|
|
FROM
|
|
(SELECT
|
|
*
|
|
FROM
|
|
(SELECT
|
|
*
|
|
FROM
|
|
users_table
|
|
ORDER BY
|
|
user_id ASC,
|
|
value_2 DESC
|
|
LIMIT
|
|
10
|
|
) as sub_table_1
|
|
WHERE
|
|
user_id
|
|
IN
|
|
(SELECT
|
|
value_2
|
|
FROM
|
|
events_table
|
|
)
|
|
) as sub_table_2;
|
|
DEBUG: push down of limit count: 10
|
|
DEBUG: generating subplan 38_1 for subquery SELECT user_id, "time", value_1, value_2, value_3, value_4 FROM public.users_table ORDER BY user_id, value_2 DESC LIMIT 10
|
|
DEBUG: generating subplan 38_2 for subquery SELECT value_2 FROM public.events_table
|
|
DEBUG: generating subplan 38_3 for subquery SELECT user_id, "time", value_1, value_2, value_3, value_4 FROM (SELECT intermediate_result.user_id, intermediate_result."time", intermediate_result.value_1, intermediate_result.value_2, intermediate_result.value_3, intermediate_result.value_4 FROM read_intermediate_result('38_1'::text, 'binary'::citus_copy_format) intermediate_result(user_id integer, "time" timestamp without time zone, value_1 integer, value_2 integer, value_3 double precision, value_4 bigint)) sub_table_1 WHERE (user_id OPERATOR(pg_catalog.=) ANY (SELECT intermediate_result.value_2 FROM read_intermediate_result('38_2'::text, 'binary'::citus_copy_format) intermediate_result(value_2 integer)))
|
|
DEBUG: Plan 38 query after replacing subqueries and CTEs: SELECT count(*) AS count FROM (SELECT intermediate_result.user_id, intermediate_result."time", intermediate_result.value_1, intermediate_result.value_2, intermediate_result.value_3, intermediate_result.value_4 FROM read_intermediate_result('38_3'::text, 'binary'::citus_copy_format) intermediate_result(user_id integer, "time" timestamp without time zone, value_1 integer, value_2 integer, value_3 double precision, value_4 bigint)) sub_table_2
|
|
count
|
|
-------
|
|
10
|
|
(1 row)
|
|
|
|
-- Recurring table in the FROM clause of a subquery in the FROM clause
|
|
-- Recurring table is created by joining a two recurrign table
|
|
SELECT
|
|
SUM(user_id)
|
|
FROM
|
|
(SELECT
|
|
*
|
|
FROM
|
|
(SELECT
|
|
user_id
|
|
FROM
|
|
users_table
|
|
ORDER BY
|
|
user_id
|
|
LIMIT 10) as t1
|
|
INNER JOIN
|
|
(SELECT
|
|
user_id as user_id_2
|
|
FROM
|
|
users_table
|
|
ORDER BY
|
|
user_id
|
|
LIMIT
|
|
10) as t2
|
|
ON
|
|
t1.user_id = t2.user_id_2
|
|
WHERE
|
|
t1.user_id
|
|
IN
|
|
(SELECT
|
|
value_2
|
|
FROM
|
|
events_table)
|
|
) as t3
|
|
WHERE
|
|
user_id
|
|
>ANY
|
|
(SELECT
|
|
min(user_id)
|
|
FROM
|
|
events_table
|
|
GROUP BY
|
|
user_id);
|
|
DEBUG: push down of limit count: 10
|
|
DEBUG: generating subplan 42_1 for subquery SELECT user_id FROM public.users_table ORDER BY user_id LIMIT 10
|
|
DEBUG: push down of limit count: 10
|
|
DEBUG: generating subplan 42_2 for subquery SELECT user_id AS user_id_2 FROM public.users_table ORDER BY user_id LIMIT 10
|
|
DEBUG: generating subplan 42_3 for subquery SELECT value_2 FROM public.events_table
|
|
DEBUG: generating subplan 42_4 for subquery SELECT t1.user_id, t2.user_id_2 FROM ((SELECT intermediate_result.user_id FROM read_intermediate_result('42_1'::text, 'binary'::citus_copy_format) intermediate_result(user_id integer)) t1 JOIN (SELECT intermediate_result.user_id_2 FROM read_intermediate_result('42_2'::text, 'binary'::citus_copy_format) intermediate_result(user_id_2 integer)) t2 ON ((t1.user_id OPERATOR(pg_catalog.=) t2.user_id_2))) WHERE (t1.user_id OPERATOR(pg_catalog.=) ANY (SELECT intermediate_result.value_2 FROM read_intermediate_result('42_3'::text, 'binary'::citus_copy_format) intermediate_result(value_2 integer)))
|
|
DEBUG: generating subplan 42_5 for subquery SELECT min(user_id) AS min FROM public.events_table GROUP BY user_id
|
|
DEBUG: Plan 42 query after replacing subqueries and CTEs: SELECT sum(user_id) AS sum FROM (SELECT intermediate_result.user_id, intermediate_result.user_id_2 FROM read_intermediate_result('42_4'::text, 'binary'::citus_copy_format) intermediate_result(user_id integer, user_id_2 integer)) t3 WHERE (user_id OPERATOR(pg_catalog.>) ANY (SELECT intermediate_result.min FROM read_intermediate_result('42_5'::text, 'binary'::citus_copy_format) intermediate_result(min integer)))
|
|
sum
|
|
-----
|
|
18
|
|
(1 row)
|
|
|
|
-- Same example with the above query, but now check the rows with EXISTS
|
|
SELECT
|
|
SUM(user_id)
|
|
FROM
|
|
(SELECT
|
|
*
|
|
FROM
|
|
(SELECT
|
|
user_id
|
|
FROM
|
|
users_table
|
|
ORDER BY
|
|
user_id
|
|
LIMIT 10) as t1
|
|
INNER JOIN
|
|
(SELECT
|
|
user_id as user_id_2
|
|
FROM
|
|
users_table
|
|
ORDER BY
|
|
user_id
|
|
LIMIT
|
|
10) as t2
|
|
ON
|
|
t1.user_id = t2.user_id_2
|
|
WHERE
|
|
t1.user_id
|
|
IN
|
|
(SELECT
|
|
value_2
|
|
FROM
|
|
events_table)
|
|
) as t3
|
|
WHERE EXISTS
|
|
(SELECT
|
|
1,2
|
|
FROM
|
|
events_table
|
|
WHERE
|
|
events_table.value_2 = events_table.user_id);
|
|
DEBUG: push down of limit count: 10
|
|
DEBUG: generating subplan 48_1 for subquery SELECT user_id FROM public.users_table ORDER BY user_id LIMIT 10
|
|
DEBUG: push down of limit count: 10
|
|
DEBUG: generating subplan 48_2 for subquery SELECT user_id AS user_id_2 FROM public.users_table ORDER BY user_id LIMIT 10
|
|
DEBUG: generating subplan 48_3 for subquery SELECT value_2 FROM public.events_table
|
|
DEBUG: generating subplan 48_4 for subquery SELECT t1.user_id, t2.user_id_2 FROM ((SELECT intermediate_result.user_id FROM read_intermediate_result('48_1'::text, 'binary'::citus_copy_format) intermediate_result(user_id integer)) t1 JOIN (SELECT intermediate_result.user_id_2 FROM read_intermediate_result('48_2'::text, 'binary'::citus_copy_format) intermediate_result(user_id_2 integer)) t2 ON ((t1.user_id OPERATOR(pg_catalog.=) t2.user_id_2))) WHERE (t1.user_id OPERATOR(pg_catalog.=) ANY (SELECT intermediate_result.value_2 FROM read_intermediate_result('48_3'::text, 'binary'::citus_copy_format) intermediate_result(value_2 integer)))
|
|
DEBUG: generating subplan 48_5 for subquery SELECT 1, 2 FROM public.events_table WHERE (value_2 OPERATOR(pg_catalog.=) user_id)
|
|
DEBUG: Plan 48 query after replacing subqueries and CTEs: SELECT sum(user_id) AS sum FROM (SELECT intermediate_result.user_id, intermediate_result.user_id_2 FROM read_intermediate_result('48_4'::text, 'binary'::citus_copy_format) intermediate_result(user_id integer, user_id_2 integer)) t3 WHERE (EXISTS (SELECT intermediate_result."?column?", intermediate_result."?column?_1" AS "?column?" FROM read_intermediate_result('48_5'::text, 'binary'::citus_copy_format) intermediate_result("?column?" integer, "?column?_1" integer)))
|
|
sum
|
|
-----
|
|
67
|
|
(1 row)
|
|
|
|
-- Same query with the above one, yet now we check the row's NON-existence
|
|
-- by NOT EXISTS. Note that, max value_2 of events_table is 5
|
|
SELECT
|
|
SUM(user_id)
|
|
FROM
|
|
(SELECT
|
|
*
|
|
FROM
|
|
(SELECT
|
|
user_id
|
|
FROM
|
|
users_table
|
|
ORDER BY
|
|
user_id
|
|
LIMIT 10) as t1
|
|
INNER JOIN
|
|
(SELECT
|
|
user_id as user_id_2
|
|
FROM
|
|
users_table
|
|
ORDER BY
|
|
user_id
|
|
LIMIT
|
|
10) as t2
|
|
ON
|
|
t1.user_id = t2.user_id_2
|
|
WHERE
|
|
t1.user_id
|
|
IN
|
|
(SELECT
|
|
value_2
|
|
FROM
|
|
events_table)
|
|
) as t3
|
|
WHERE NOT EXISTS
|
|
(SELECT
|
|
1,2
|
|
FROM
|
|
events_table
|
|
WHERE
|
|
events_table.value_2 = events_table.user_id + 6);
|
|
DEBUG: push down of limit count: 10
|
|
DEBUG: generating subplan 54_1 for subquery SELECT user_id FROM public.users_table ORDER BY user_id LIMIT 10
|
|
DEBUG: push down of limit count: 10
|
|
DEBUG: generating subplan 54_2 for subquery SELECT user_id AS user_id_2 FROM public.users_table ORDER BY user_id LIMIT 10
|
|
DEBUG: generating subplan 54_3 for subquery SELECT value_2 FROM public.events_table
|
|
DEBUG: generating subplan 54_4 for subquery SELECT t1.user_id, t2.user_id_2 FROM ((SELECT intermediate_result.user_id FROM read_intermediate_result('54_1'::text, 'binary'::citus_copy_format) intermediate_result(user_id integer)) t1 JOIN (SELECT intermediate_result.user_id_2 FROM read_intermediate_result('54_2'::text, 'binary'::citus_copy_format) intermediate_result(user_id_2 integer)) t2 ON ((t1.user_id OPERATOR(pg_catalog.=) t2.user_id_2))) WHERE (t1.user_id OPERATOR(pg_catalog.=) ANY (SELECT intermediate_result.value_2 FROM read_intermediate_result('54_3'::text, 'binary'::citus_copy_format) intermediate_result(value_2 integer)))
|
|
DEBUG: generating subplan 54_5 for subquery SELECT 1, 2 FROM public.events_table WHERE (value_2 OPERATOR(pg_catalog.=) (user_id OPERATOR(pg_catalog.+) 6))
|
|
DEBUG: Plan 54 query after replacing subqueries and CTEs: SELECT sum(user_id) AS sum FROM (SELECT intermediate_result.user_id, intermediate_result.user_id_2 FROM read_intermediate_result('54_4'::text, 'binary'::citus_copy_format) intermediate_result(user_id integer, user_id_2 integer)) t3 WHERE (NOT (EXISTS (SELECT intermediate_result."?column?", intermediate_result."?column?_1" AS "?column?" FROM read_intermediate_result('54_5'::text, 'binary'::citus_copy_format) intermediate_result("?column?" integer, "?column?_1" integer))))
|
|
sum
|
|
-----
|
|
67
|
|
(1 row)
|
|
|
|
-- Check the existence of row by comparing it with the result of subquery in
|
|
-- WHERE clause. Note that subquery is planned recursively since there is no
|
|
-- distributed table in the from
|
|
SELECT
|
|
*
|
|
FROM
|
|
(SELECT
|
|
user_id, value_1
|
|
FROM
|
|
users_table
|
|
ORDER BY
|
|
user_id ASC,
|
|
value_1 ASC
|
|
LIMIT 10) as t3
|
|
WHERE row(user_id, value_1) =
|
|
(SELECT
|
|
min(user_id) + 1, min(user_id) + 1
|
|
FROM
|
|
events_table);
|
|
DEBUG: generating subplan 60_1 for subquery SELECT (min(user_id) OPERATOR(pg_catalog.+) 1), (min(user_id) OPERATOR(pg_catalog.+) 1) FROM public.events_table
|
|
DEBUG: push down of limit count: 10
|
|
DEBUG: generating subplan 60_2 for subquery SELECT user_id, value_1 FROM public.users_table ORDER BY user_id, value_1 LIMIT 10
|
|
DEBUG: Plan 60 query after replacing subqueries and CTEs: SELECT user_id, value_1 FROM (SELECT intermediate_result.user_id, intermediate_result.value_1 FROM read_intermediate_result('60_2'::text, 'binary'::citus_copy_format) intermediate_result(user_id integer, value_1 integer)) t3 WHERE ((user_id, value_1) OPERATOR(pg_catalog.=) (SELECT intermediate_result."?column?", intermediate_result."?column?_1" AS "?column?" FROM read_intermediate_result('60_1'::text, 'binary'::citus_copy_format) intermediate_result("?column?" integer, "?column?_1" integer)))
|
|
user_id | value_1
|
|
---------+---------
|
|
(0 rows)
|
|
|
|
-- Recursively plan subquery in WHERE clause when the FROM clause has a subquery
|
|
-- generated by generate_series function
|
|
SELECT
|
|
*
|
|
FROM
|
|
(SELECT
|
|
*
|
|
FROM
|
|
generate_series(1,10)
|
|
) as gst
|
|
WHERE
|
|
generate_series
|
|
IN
|
|
(SELECT
|
|
value_2
|
|
FROM
|
|
events_table
|
|
)
|
|
ORDER BY
|
|
generate_series ASC;
|
|
DEBUG: generating subplan 63_1 for subquery SELECT NULL::integer AS user_id, NULL::timestamp without time zone AS "time", NULL::integer AS event_type, value_2, NULL::double precision AS value_3, NULL::bigint AS value_4 FROM public.events_table WHERE true
|
|
DEBUG: Plan 63 query after replacing subqueries and CTEs: SELECT generate_series FROM (SELECT generate_series.generate_series FROM generate_series(1, 10) generate_series(generate_series)) gst WHERE (generate_series OPERATOR(pg_catalog.=) ANY (SELECT events_table.value_2 FROM (SELECT intermediate_result.user_id, intermediate_result."time", intermediate_result.event_type, intermediate_result.value_2, intermediate_result.value_3, intermediate_result.value_4 FROM read_intermediate_result('63_1'::text, 'binary'::citus_copy_format) intermediate_result(user_id integer, "time" timestamp without time zone, event_type integer, value_2 integer, value_3 double precision, value_4 bigint)) events_table(user_id, "time", event_type, value_2, value_3, value_4))) ORDER BY generate_series
|
|
generate_series
|
|
-----------------
|
|
1
|
|
2
|
|
3
|
|
4
|
|
5
|
|
(5 rows)
|
|
|
|
-- Similar to the test above, now we also have a generate_series in WHERE clause
|
|
SELECT
|
|
*
|
|
FROM
|
|
(SELECT
|
|
*
|
|
FROM
|
|
generate_series(1,10)
|
|
) as gst
|
|
WHERE
|
|
generate_series
|
|
IN
|
|
(SELECT
|
|
user_id
|
|
FROM
|
|
users_table
|
|
WHERE
|
|
user_id
|
|
IN
|
|
(SELECT
|
|
*
|
|
FROM
|
|
generate_series(1,3)
|
|
)
|
|
)
|
|
ORDER BY
|
|
generate_series ASC;
|
|
DEBUG: generating subplan 65_1 for subquery SELECT user_id, NULL::timestamp without time zone AS "time", NULL::integer AS value_1, NULL::integer AS value_2, NULL::double precision AS value_3, NULL::bigint AS value_4 FROM public.users_table WHERE true
|
|
DEBUG: generating subplan 65_2 for subquery SELECT user_id FROM (SELECT intermediate_result.user_id, intermediate_result."time", intermediate_result.value_1, intermediate_result.value_2, intermediate_result.value_3, intermediate_result.value_4 FROM read_intermediate_result('65_1'::text, 'binary'::citus_copy_format) intermediate_result(user_id integer, "time" timestamp without time zone, value_1 integer, value_2 integer, value_3 double precision, value_4 bigint)) users_table(user_id, "time", value_1, value_2, value_3, value_4) WHERE (user_id OPERATOR(pg_catalog.=) ANY (SELECT generate_series.generate_series FROM generate_series(1, 3) generate_series(generate_series)))
|
|
DEBUG: Plan 65 query after replacing subqueries and CTEs: SELECT generate_series FROM (SELECT generate_series.generate_series FROM generate_series(1, 10) generate_series(generate_series)) gst WHERE (generate_series OPERATOR(pg_catalog.=) ANY (SELECT intermediate_result.user_id FROM read_intermediate_result('65_2'::text, 'binary'::citus_copy_format) intermediate_result(user_id integer))) ORDER BY generate_series
|
|
generate_series
|
|
-----------------
|
|
1
|
|
2
|
|
3
|
|
(3 rows)
|
|
|
|
|
|
-- Local tables also planned recursively, so using it as part of the FROM clause
|
|
-- make the clause recurring
|
|
CREATE TABLE local_table(id int, value_1 int);
|
|
INSERT INTO local_table VALUES(1,1), (2,2);
|
|
SELECT
|
|
*
|
|
FROM
|
|
(SELECT
|
|
*
|
|
FROM
|
|
local_table) as sub_table
|
|
WHERE
|
|
id
|
|
IN
|
|
(SELECT
|
|
user_id
|
|
FROM
|
|
users_table);
|
|
DEBUG: generating subplan 68_1 for subquery SELECT id, value_1 FROM subquery_in_where.local_table
|
|
DEBUG: generating subplan 68_2 for subquery SELECT user_id FROM public.users_table
|
|
DEBUG: Plan 68 query after replacing subqueries and CTEs: SELECT id, value_1 FROM (SELECT intermediate_result.id, intermediate_result.value_1 FROM read_intermediate_result('68_1'::text, 'binary'::citus_copy_format) intermediate_result(id integer, value_1 integer)) sub_table WHERE (id OPERATOR(pg_catalog.=) ANY (SELECT intermediate_result.user_id FROM read_intermediate_result('68_2'::text, 'binary'::citus_copy_format) intermediate_result(user_id integer)))
|
|
id | value_1
|
|
----+---------
|
|
1 | 1
|
|
2 | 2
|
|
(2 rows)
|
|
|
|
|
|
-- Use local table in WHERE clause
|
|
SELECT
|
|
COUNT(*)
|
|
FROM
|
|
(SELECT
|
|
*
|
|
FROM
|
|
users_table
|
|
ORDER BY
|
|
user_id
|
|
LIMIT
|
|
10) as sub_table
|
|
WHERE
|
|
user_id
|
|
IN
|
|
(SELECT
|
|
id
|
|
FROM
|
|
local_table);
|
|
DEBUG: generating subplan 70_1 for subquery SELECT id FROM subquery_in_where.local_table
|
|
DEBUG: push down of limit count: 10
|
|
DEBUG: generating subplan 70_2 for subquery SELECT user_id, "time", value_1, value_2, value_3, value_4 FROM public.users_table ORDER BY user_id LIMIT 10
|
|
DEBUG: Plan 70 query after replacing subqueries and CTEs: SELECT count(*) AS count FROM (SELECT intermediate_result.user_id, intermediate_result."time", intermediate_result.value_1, intermediate_result.value_2, intermediate_result.value_3, intermediate_result.value_4 FROM read_intermediate_result('70_2'::text, 'binary'::citus_copy_format) intermediate_result(user_id integer, "time" timestamp without time zone, value_1 integer, value_2 integer, value_3 double precision, value_4 bigint)) sub_table WHERE (user_id OPERATOR(pg_catalog.=) ANY (SELECT intermediate_result.id FROM read_intermediate_result('70_1'::text, 'binary'::citus_copy_format) intermediate_result(id integer)))
|
|
count
|
|
-------
|
|
10
|
|
(1 row)
|
|
|
|
SET client_min_messages TO DEFAULT;
|
|
DROP TABLE local_table;
|
|
DROP SCHEMA subquery_in_where CASCADE;
|
|
SET search_path TO public;
|