--
-- MULTI_REPARTITION_JOIN_PLANNING
--
-- Tests that cover repartition join planning. Note that we explicitly start a
-- transaction block here so that we don't emit debug messages with changing
-- transaction ids in them. Also, we set the executor type to task tracker
-- executor here, as we cannot run repartition jobs with real time executor.
SET citus.next_shard_id TO 690000;
SET citus.enable_unique_job_ids TO off;
create schema repartition_join;
DROP TABLE IF EXISTS repartition_join.order_line;
NOTICE:  table "order_line" does not exist, skipping
CREATE TABLE order_line (
  ol_w_id int NOT NULL,
  ol_d_id int NOT NULL,
  ol_o_id int NOT NULL,
  ol_number int NOT NULL,
  ol_i_id int NOT NULL,
  ol_quantity decimal(2,0) NOT NULL,
  PRIMARY KEY (ol_w_id,ol_d_id,ol_o_id,ol_number)
);
DROP TABLE IF EXISTS repartition_join.stock;
NOTICE:  table "stock" does not exist, skipping
CREATE TABLE stock (
  s_w_id int NOT NULL,
  s_i_id int NOT NULL,
  s_quantity decimal(4,0) NOT NULL,
  PRIMARY KEY (s_w_id,s_i_id)
);
SELECT create_distributed_table('order_line','ol_w_id');
 create_distributed_table
---------------------------------------------------------------------

(1 row)

SELECT create_distributed_table('stock','s_w_id');
 create_distributed_table
---------------------------------------------------------------------

(1 row)

BEGIN;
SET client_min_messages TO DEBUG4;
SET citus.task_executor_type TO 'task-tracker';
-- Debug4 log messages display jobIds within them. We explicitly set the jobId
-- sequence here so that the regression output becomes independent of the number
-- of jobs executed prior to running this test.
-- Multi-level repartition join to verify our projection columns are correctly
-- referenced and propagated across multiple repartition jobs. The test also
-- validates that only the minimal necessary projection columns are transferred
-- between jobs.
SELECT
	l_partkey, o_orderkey, count(*)
FROM
	lineitem, part_append, orders, customer_append
WHERE
	l_orderkey = o_orderkey AND
	l_partkey = p_partkey AND
	c_custkey = o_custkey AND
        (l_quantity > 5.0 OR l_extendedprice > 1200.0) AND
        p_size > 8 AND o_totalprice > 10.0 AND
        c_acctbal < 5000.0 AND l_partkey < 1000
GROUP BY
	l_partkey, o_orderkey
ORDER BY
	l_partkey, o_orderkey;
DEBUG:  Router planner does not support append-partitioned tables.
DEBUG:  join prunable for intervals [1,5986] and [8997,14947]
DEBUG:  join prunable for intervals [8997,14947] and [1,5986]
DEBUG:  generated sql query for task 1
DETAIL:  query string: "SELECT lineitem.l_partkey, orders.o_orderkey, lineitem.l_quantity, lineitem.l_extendedprice, orders.o_custkey FROM (lineitem_290000 lineitem JOIN orders_290002 orders ON ((lineitem.l_orderkey OPERATOR(pg_catalog.=) orders.o_orderkey))) WHERE ((lineitem.l_partkey OPERATOR(pg_catalog.<) 1000) AND (orders.o_totalprice OPERATOR(pg_catalog.>) 10.0))"
DEBUG:  generated sql query for task 2
DETAIL:  query string: "SELECT lineitem.l_partkey, orders.o_orderkey, lineitem.l_quantity, lineitem.l_extendedprice, orders.o_custkey FROM (lineitem_290001 lineitem JOIN orders_290003 orders ON ((lineitem.l_orderkey OPERATOR(pg_catalog.=) orders.o_orderkey))) WHERE ((lineitem.l_partkey OPERATOR(pg_catalog.<) 1000) AND (orders.o_totalprice OPERATOR(pg_catalog.>) 10.0))"
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  join prunable for intervals [1,1000] and [6001,7000]
DEBUG:  join prunable for intervals [6001,7000] and [1,1000]
DEBUG:  generated sql query for task 2
DETAIL:  query string: "SELECT "pg_merge_job_0001.task_000003".intermediate_column_1_0, "pg_merge_job_0001.task_000003".intermediate_column_1_1, "pg_merge_job_0001.task_000003".intermediate_column_1_2, "pg_merge_job_0001.task_000003".intermediate_column_1_3, "pg_merge_job_0001.task_000003".intermediate_column_1_4 FROM (pg_merge_job_0001.task_000003 "pg_merge_job_0001.task_000003" JOIN part_append_290005 part_append ON (("pg_merge_job_0001.task_000003".intermediate_column_1_0 OPERATOR(pg_catalog.=) part_append.p_partkey))) WHERE (part_append.p_size OPERATOR(pg_catalog.>) 8)"
DEBUG:  generated sql query for task 4
DETAIL:  query string: "SELECT "pg_merge_job_0001.task_000006".intermediate_column_1_0, "pg_merge_job_0001.task_000006".intermediate_column_1_1, "pg_merge_job_0001.task_000006".intermediate_column_1_2, "pg_merge_job_0001.task_000006".intermediate_column_1_3, "pg_merge_job_0001.task_000006".intermediate_column_1_4 FROM (pg_merge_job_0001.task_000006 "pg_merge_job_0001.task_000006" JOIN part_append_280002 part_append ON (("pg_merge_job_0001.task_000006".intermediate_column_1_0 OPERATOR(pg_catalog.=) part_append.p_partkey))) WHERE (part_append.p_size OPERATOR(pg_catalog.>) 8)"
DEBUG:  pruning merge fetch taskId 1
DETAIL:  Creating dependency on merge taskId 3
DEBUG:  pruning merge fetch taskId 3
DETAIL:  Creating dependency on merge taskId 6
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  join prunable for intervals [1,1000] and [1001,2000]
DEBUG:  join prunable for intervals [1,1000] and [6001,7000]
DEBUG:  join prunable for intervals [1001,2000] and [1,1000]
DEBUG:  join prunable for intervals [1001,2000] and [6001,7000]
DEBUG:  join prunable for intervals [6001,7000] and [1,1000]
DEBUG:  join prunable for intervals [6001,7000] and [1001,2000]
DEBUG:  generated sql query for task 2
DETAIL:  query string: "SELECT "pg_merge_job_0002.task_000005".intermediate_column_2_0 AS l_partkey, "pg_merge_job_0002.task_000005".intermediate_column_2_1 AS o_orderkey, count(*) AS count FROM (pg_merge_job_0002.task_000005 "pg_merge_job_0002.task_000005" JOIN customer_append_290004 customer_append ON ((customer_append.c_custkey OPERATOR(pg_catalog.=) "pg_merge_job_0002.task_000005".intermediate_column_2_4))) WHERE ((("pg_merge_job_0002.task_000005".intermediate_column_2_2 OPERATOR(pg_catalog.>) 5.0) OR ("pg_merge_job_0002.task_000005".intermediate_column_2_3 OPERATOR(pg_catalog.>) 1200.0)) AND (customer_append.c_acctbal OPERATOR(pg_catalog.<) 5000.0)) GROUP BY "pg_merge_job_0002.task_000005".intermediate_column_2_0, "pg_merge_job_0002.task_000005".intermediate_column_2_1"
DEBUG:  generated sql query for task 4
DETAIL:  query string: "SELECT "pg_merge_job_0002.task_000008".intermediate_column_2_0 AS l_partkey, "pg_merge_job_0002.task_000008".intermediate_column_2_1 AS o_orderkey, count(*) AS count FROM (pg_merge_job_0002.task_000008 "pg_merge_job_0002.task_000008" JOIN customer_append_280001 customer_append ON ((customer_append.c_custkey OPERATOR(pg_catalog.=) "pg_merge_job_0002.task_000008".intermediate_column_2_4))) WHERE ((("pg_merge_job_0002.task_000008".intermediate_column_2_2 OPERATOR(pg_catalog.>) 5.0) OR ("pg_merge_job_0002.task_000008".intermediate_column_2_3 OPERATOR(pg_catalog.>) 1200.0)) AND (customer_append.c_acctbal OPERATOR(pg_catalog.<) 5000.0)) GROUP BY "pg_merge_job_0002.task_000008".intermediate_column_2_0, "pg_merge_job_0002.task_000008".intermediate_column_2_1"
DEBUG:  generated sql query for task 6
DETAIL:  query string: "SELECT "pg_merge_job_0002.task_000011".intermediate_column_2_0 AS l_partkey, "pg_merge_job_0002.task_000011".intermediate_column_2_1 AS o_orderkey, count(*) AS count FROM (pg_merge_job_0002.task_000011 "pg_merge_job_0002.task_000011" JOIN customer_append_280000 customer_append ON ((customer_append.c_custkey OPERATOR(pg_catalog.=) "pg_merge_job_0002.task_000011".intermediate_column_2_4))) WHERE ((("pg_merge_job_0002.task_000011".intermediate_column_2_2 OPERATOR(pg_catalog.>) 5.0) OR ("pg_merge_job_0002.task_000011".intermediate_column_2_3 OPERATOR(pg_catalog.>) 1200.0)) AND (customer_append.c_acctbal OPERATOR(pg_catalog.<) 5000.0)) GROUP BY "pg_merge_job_0002.task_000011".intermediate_column_2_0, "pg_merge_job_0002.task_000011".intermediate_column_2_1"
DEBUG:  pruning merge fetch taskId 1
DETAIL:  Creating dependency on merge taskId 5
DEBUG:  pruning merge fetch taskId 3
DETAIL:  Creating dependency on merge taskId 8
DEBUG:  pruning merge fetch taskId 5
DETAIL:  Creating dependency on merge taskId 11
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  master query: SELECT l_partkey, o_orderkey, COALESCE((pg_catalog.sum(count))::bigint, '0'::bigint) AS count FROM pg_catalog.citus_extradata_container(XXX, NULL::cstring(0), NULL::cstring(0), '(i 1 3 2 4)'::cstring(0)) remote_scan(l_partkey integer, o_orderkey bigint, count bigint) GROUP BY l_partkey, o_orderkey ORDER BY l_partkey, o_orderkey
DEBUG:  completed cleanup query for job 3
DEBUG:  completed cleanup query for job 3
DEBUG:  completed cleanup query for job 2
DEBUG:  completed cleanup query for job 2
DEBUG:  completed cleanup query for job 1
DEBUG:  completed cleanup query for job 1
 l_partkey | o_orderkey | count
---------------------------------------------------------------------
        18 |      12005 |     1
        79 |       5121 |     1
        91 |       2883 |     1
       222 |       9413 |     1
       278 |       1287 |     1
       309 |       2374 |     1
       318 |        321 |     1
       321 |       5984 |     1
       337 |      10403 |     1
       350 |      13698 |     1
       358 |       4323 |     1
       364 |       9347 |     1
       416 |        640 |     1
       426 |      10855 |     1
       450 |         35 |     1
       484 |       3843 |     1
       504 |      14566 |     1
       510 |      13569 |     1
       532 |       3175 |     1
       641 |        134 |     1
       669 |      10944 |     1
       716 |       2885 |     1
       738 |       4355 |     1
       802 |       2534 |     1
       824 |       9287 |     1
       864 |       3175 |     1
       957 |       4293 |     1
       960 |      10980 |     1
       963 |       4580 |     1
(29 rows)

SELECT
	l_partkey, o_orderkey, count(*)
FROM
	lineitem, orders
WHERE
	l_suppkey = o_shippriority AND
        l_quantity < 5.0 AND o_totalprice <> 4.0
GROUP BY
	l_partkey, o_orderkey
ORDER BY
	l_partkey, o_orderkey;
DEBUG:  Router planner does not support append-partitioned tables.
DEBUG:  generated sql query for task 1
DETAIL:  query string: "SELECT l_partkey, l_suppkey FROM lineitem_290000 lineitem WHERE (l_quantity OPERATOR(pg_catalog.<) 5.0)"
DEBUG:  generated sql query for task 2
DETAIL:  query string: "SELECT l_partkey, l_suppkey FROM lineitem_290001 lineitem WHERE (l_quantity OPERATOR(pg_catalog.<) 5.0)"
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  generated sql query for task 1
DETAIL:  query string: "SELECT o_orderkey, o_shippriority FROM orders_290002 orders WHERE (o_totalprice OPERATOR(pg_catalog.<>) 4.0)"
DEBUG:  generated sql query for task 2
DETAIL:  query string: "SELECT o_orderkey, o_shippriority FROM orders_290003 orders WHERE (o_totalprice OPERATOR(pg_catalog.<>) 4.0)"
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  join prunable for task partitionId 0 and 1
DEBUG:  join prunable for task partitionId 0 and 2
DEBUG:  join prunable for task partitionId 0 and 3
DEBUG:  join prunable for task partitionId 1 and 0
DEBUG:  join prunable for task partitionId 1 and 2
DEBUG:  join prunable for task partitionId 1 and 3
DEBUG:  join prunable for task partitionId 2 and 0
DEBUG:  join prunable for task partitionId 2 and 1
DEBUG:  join prunable for task partitionId 2 and 3
DEBUG:  join prunable for task partitionId 3 and 0
DEBUG:  join prunable for task partitionId 3 and 1
DEBUG:  join prunable for task partitionId 3 and 2
DEBUG:  generated sql query for task 3
DETAIL:  query string: "SELECT "pg_merge_job_0004.task_000003".intermediate_column_4_0 AS l_partkey, "pg_merge_job_0005.task_000003".intermediate_column_5_0 AS o_orderkey, count(*) AS count FROM (pg_merge_job_0004.task_000003 "pg_merge_job_0004.task_000003" JOIN pg_merge_job_0005.task_000003 "pg_merge_job_0005.task_000003" ON (("pg_merge_job_0004.task_000003".intermediate_column_4_1 OPERATOR(pg_catalog.=) "pg_merge_job_0005.task_000003".intermediate_column_5_1))) WHERE true GROUP BY "pg_merge_job_0004.task_000003".intermediate_column_4_0, "pg_merge_job_0005.task_000003".intermediate_column_5_0"
DEBUG:  generated sql query for task 6
DETAIL:  query string: "SELECT "pg_merge_job_0004.task_000006".intermediate_column_4_0 AS l_partkey, "pg_merge_job_0005.task_000006".intermediate_column_5_0 AS o_orderkey, count(*) AS count FROM (pg_merge_job_0004.task_000006 "pg_merge_job_0004.task_000006" JOIN pg_merge_job_0005.task_000006 "pg_merge_job_0005.task_000006" ON (("pg_merge_job_0004.task_000006".intermediate_column_4_1 OPERATOR(pg_catalog.=) "pg_merge_job_0005.task_000006".intermediate_column_5_1))) WHERE true GROUP BY "pg_merge_job_0004.task_000006".intermediate_column_4_0, "pg_merge_job_0005.task_000006".intermediate_column_5_0"
DEBUG:  generated sql query for task 9
DETAIL:  query string: "SELECT "pg_merge_job_0004.task_000009".intermediate_column_4_0 AS l_partkey, "pg_merge_job_0005.task_000009".intermediate_column_5_0 AS o_orderkey, count(*) AS count FROM (pg_merge_job_0004.task_000009 "pg_merge_job_0004.task_000009" JOIN pg_merge_job_0005.task_000009 "pg_merge_job_0005.task_000009" ON (("pg_merge_job_0004.task_000009".intermediate_column_4_1 OPERATOR(pg_catalog.=) "pg_merge_job_0005.task_000009".intermediate_column_5_1))) WHERE true GROUP BY "pg_merge_job_0004.task_000009".intermediate_column_4_0, "pg_merge_job_0005.task_000009".intermediate_column_5_0"
DEBUG:  generated sql query for task 12
DETAIL:  query string: "SELECT "pg_merge_job_0004.task_000012".intermediate_column_4_0 AS l_partkey, "pg_merge_job_0005.task_000012".intermediate_column_5_0 AS o_orderkey, count(*) AS count FROM (pg_merge_job_0004.task_000012 "pg_merge_job_0004.task_000012" JOIN pg_merge_job_0005.task_000012 "pg_merge_job_0005.task_000012" ON (("pg_merge_job_0004.task_000012".intermediate_column_4_1 OPERATOR(pg_catalog.=) "pg_merge_job_0005.task_000012".intermediate_column_5_1))) WHERE true GROUP BY "pg_merge_job_0004.task_000012".intermediate_column_4_0, "pg_merge_job_0005.task_000012".intermediate_column_5_0"
DEBUG:  pruning merge fetch taskId 1
DETAIL:  Creating dependency on merge taskId 3
DEBUG:  pruning merge fetch taskId 2
DETAIL:  Creating dependency on merge taskId 3
DEBUG:  pruning merge fetch taskId 4
DETAIL:  Creating dependency on merge taskId 6
DEBUG:  pruning merge fetch taskId 5
DETAIL:  Creating dependency on merge taskId 6
DEBUG:  pruning merge fetch taskId 7
DETAIL:  Creating dependency on merge taskId 9
DEBUG:  pruning merge fetch taskId 8
DETAIL:  Creating dependency on merge taskId 9
DEBUG:  pruning merge fetch taskId 10
DETAIL:  Creating dependency on merge taskId 12
DEBUG:  pruning merge fetch taskId 11
DETAIL:  Creating dependency on merge taskId 12
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  master query: SELECT l_partkey, o_orderkey, COALESCE((pg_catalog.sum(count))::bigint, '0'::bigint) AS count FROM pg_catalog.citus_extradata_container(XXX, NULL::cstring(0), NULL::cstring(0), '(i 1 2)'::cstring(0)) remote_scan(l_partkey integer, o_orderkey bigint, count bigint) GROUP BY l_partkey, o_orderkey ORDER BY l_partkey, o_orderkey
DEBUG:  completed cleanup query for job 6
DEBUG:  completed cleanup query for job 6
DEBUG:  completed cleanup query for job 4
DEBUG:  completed cleanup query for job 4
DEBUG:  completed cleanup query for job 5
DEBUG:  completed cleanup query for job 5
 l_partkey | o_orderkey | count
---------------------------------------------------------------------
(0 rows)

-- Check that grouping by primary key allows o_shippriority to be in the target list
SELECT
	o_orderkey, o_shippriority, count(*)
FROM
	lineitem, orders
WHERE
	l_suppkey = o_shippriority
GROUP BY
	o_orderkey
ORDER BY
	o_orderkey;
DEBUG:  Router planner does not support append-partitioned tables.
DEBUG:  generated sql query for task 1
DETAIL:  query string: "SELECT l_suppkey FROM lineitem_290000 lineitem WHERE true"
DEBUG:  generated sql query for task 2
DETAIL:  query string: "SELECT l_suppkey FROM lineitem_290001 lineitem WHERE true"
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  generated sql query for task 1
DETAIL:  query string: "SELECT o_orderkey, o_shippriority FROM orders_290002 orders WHERE true"
DEBUG:  generated sql query for task 2
DETAIL:  query string: "SELECT o_orderkey, o_shippriority FROM orders_290003 orders WHERE true"
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  join prunable for task partitionId 0 and 1
DEBUG:  join prunable for task partitionId 0 and 2
DEBUG:  join prunable for task partitionId 0 and 3
DEBUG:  join prunable for task partitionId 1 and 0
DEBUG:  join prunable for task partitionId 1 and 2
DEBUG:  join prunable for task partitionId 1 and 3
DEBUG:  join prunable for task partitionId 2 and 0
DEBUG:  join prunable for task partitionId 2 and 1
DEBUG:  join prunable for task partitionId 2 and 3
DEBUG:  join prunable for task partitionId 3 and 0
DEBUG:  join prunable for task partitionId 3 and 1
DEBUG:  join prunable for task partitionId 3 and 2
DEBUG:  generated sql query for task 3
DETAIL:  query string: "SELECT "pg_merge_job_0008.task_000003".intermediate_column_8_0 AS o_orderkey, any_value("pg_merge_job_0008.task_000003".intermediate_column_8_1) AS o_shippriority, count(*) AS count FROM (pg_merge_job_0007.task_000003 "pg_merge_job_0007.task_000003" JOIN pg_merge_job_0008.task_000003 "pg_merge_job_0008.task_000003" ON (("pg_merge_job_0007.task_000003".intermediate_column_7_0 OPERATOR(pg_catalog.=) "pg_merge_job_0008.task_000003".intermediate_column_8_1))) WHERE true GROUP BY "pg_merge_job_0008.task_000003".intermediate_column_8_0"
DEBUG:  generated sql query for task 6
DETAIL:  query string: "SELECT "pg_merge_job_0008.task_000006".intermediate_column_8_0 AS o_orderkey, any_value("pg_merge_job_0008.task_000006".intermediate_column_8_1) AS o_shippriority, count(*) AS count FROM (pg_merge_job_0007.task_000006 "pg_merge_job_0007.task_000006" JOIN pg_merge_job_0008.task_000006 "pg_merge_job_0008.task_000006" ON (("pg_merge_job_0007.task_000006".intermediate_column_7_0 OPERATOR(pg_catalog.=) "pg_merge_job_0008.task_000006".intermediate_column_8_1))) WHERE true GROUP BY "pg_merge_job_0008.task_000006".intermediate_column_8_0"
DEBUG:  generated sql query for task 9
DETAIL:  query string: "SELECT "pg_merge_job_0008.task_000009".intermediate_column_8_0 AS o_orderkey, any_value("pg_merge_job_0008.task_000009".intermediate_column_8_1) AS o_shippriority, count(*) AS count FROM (pg_merge_job_0007.task_000009 "pg_merge_job_0007.task_000009" JOIN pg_merge_job_0008.task_000009 "pg_merge_job_0008.task_000009" ON (("pg_merge_job_0007.task_000009".intermediate_column_7_0 OPERATOR(pg_catalog.=) "pg_merge_job_0008.task_000009".intermediate_column_8_1))) WHERE true GROUP BY "pg_merge_job_0008.task_000009".intermediate_column_8_0"
DEBUG:  generated sql query for task 12
DETAIL:  query string: "SELECT "pg_merge_job_0008.task_000012".intermediate_column_8_0 AS o_orderkey, any_value("pg_merge_job_0008.task_000012".intermediate_column_8_1) AS o_shippriority, count(*) AS count FROM (pg_merge_job_0007.task_000012 "pg_merge_job_0007.task_000012" JOIN pg_merge_job_0008.task_000012 "pg_merge_job_0008.task_000012" ON (("pg_merge_job_0007.task_000012".intermediate_column_7_0 OPERATOR(pg_catalog.=) "pg_merge_job_0008.task_000012".intermediate_column_8_1))) WHERE true GROUP BY "pg_merge_job_0008.task_000012".intermediate_column_8_0"
DEBUG:  pruning merge fetch taskId 1
DETAIL:  Creating dependency on merge taskId 3
DEBUG:  pruning merge fetch taskId 2
DETAIL:  Creating dependency on merge taskId 3
DEBUG:  pruning merge fetch taskId 4
DETAIL:  Creating dependency on merge taskId 6
DEBUG:  pruning merge fetch taskId 5
DETAIL:  Creating dependency on merge taskId 6
DEBUG:  pruning merge fetch taskId 7
DETAIL:  Creating dependency on merge taskId 9
DEBUG:  pruning merge fetch taskId 8
DETAIL:  Creating dependency on merge taskId 9
DEBUG:  pruning merge fetch taskId 10
DETAIL:  Creating dependency on merge taskId 12
DEBUG:  pruning merge fetch taskId 11
DETAIL:  Creating dependency on merge taskId 12
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  master query: SELECT o_orderkey, o_shippriority, COALESCE((pg_catalog.sum(count))::bigint, '0'::bigint) AS count FROM pg_catalog.citus_extradata_container(XXX, NULL::cstring(0), NULL::cstring(0), '(i 1 2)'::cstring(0)) remote_scan(o_orderkey bigint, o_shippriority integer, count bigint) GROUP BY o_orderkey ORDER BY o_orderkey
DEBUG:  completed cleanup query for job 9
DEBUG:  completed cleanup query for job 9
DEBUG:  completed cleanup query for job 7
DEBUG:  completed cleanup query for job 7
DEBUG:  completed cleanup query for job 8
DEBUG:  completed cleanup query for job 8
 o_orderkey | o_shippriority | count
---------------------------------------------------------------------
(0 rows)

-- Check that grouping by primary key allows o_shippriority to be in the target
-- list
-- Postgres removes o_shippriority from the group by clause here
SELECT
	o_orderkey, o_shippriority, count(*)
FROM
	lineitem, orders
WHERE
	l_suppkey = o_shippriority
GROUP BY
	o_orderkey, o_shippriority
ORDER BY
	o_orderkey;
DEBUG:  Router planner does not support append-partitioned tables.
DEBUG:  generated sql query for task 1
DETAIL:  query string: "SELECT l_suppkey FROM lineitem_290000 lineitem WHERE true"
DEBUG:  generated sql query for task 2
DETAIL:  query string: "SELECT l_suppkey FROM lineitem_290001 lineitem WHERE true"
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  generated sql query for task 1
DETAIL:  query string: "SELECT o_orderkey, o_shippriority FROM orders_290002 orders WHERE true"
DEBUG:  generated sql query for task 2
DETAIL:  query string: "SELECT o_orderkey, o_shippriority FROM orders_290003 orders WHERE true"
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  join prunable for task partitionId 0 and 1
DEBUG:  join prunable for task partitionId 0 and 2
DEBUG:  join prunable for task partitionId 0 and 3
DEBUG:  join prunable for task partitionId 1 and 0
DEBUG:  join prunable for task partitionId 1 and 2
DEBUG:  join prunable for task partitionId 1 and 3
DEBUG:  join prunable for task partitionId 2 and 0
DEBUG:  join prunable for task partitionId 2 and 1
DEBUG:  join prunable for task partitionId 2 and 3
DEBUG:  join prunable for task partitionId 3 and 0
DEBUG:  join prunable for task partitionId 3 and 1
DEBUG:  join prunable for task partitionId 3 and 2
DEBUG:  generated sql query for task 3
DETAIL:  query string: "SELECT "pg_merge_job_0011.task_000003".intermediate_column_11_0 AS o_orderkey, any_value("pg_merge_job_0011.task_000003".intermediate_column_11_1) AS o_shippriority, count(*) AS count FROM (pg_merge_job_0010.task_000003 "pg_merge_job_0010.task_000003" JOIN pg_merge_job_0011.task_000003 "pg_merge_job_0011.task_000003" ON (("pg_merge_job_0010.task_000003".intermediate_column_10_0 OPERATOR(pg_catalog.=) "pg_merge_job_0011.task_000003".intermediate_column_11_1))) WHERE true GROUP BY "pg_merge_job_0011.task_000003".intermediate_column_11_0"
DEBUG:  generated sql query for task 6
DETAIL:  query string: "SELECT "pg_merge_job_0011.task_000006".intermediate_column_11_0 AS o_orderkey, any_value("pg_merge_job_0011.task_000006".intermediate_column_11_1) AS o_shippriority, count(*) AS count FROM (pg_merge_job_0010.task_000006 "pg_merge_job_0010.task_000006" JOIN pg_merge_job_0011.task_000006 "pg_merge_job_0011.task_000006" ON (("pg_merge_job_0010.task_000006".intermediate_column_10_0 OPERATOR(pg_catalog.=) "pg_merge_job_0011.task_000006".intermediate_column_11_1))) WHERE true GROUP BY "pg_merge_job_0011.task_000006".intermediate_column_11_0"
DEBUG:  generated sql query for task 9
DETAIL:  query string: "SELECT "pg_merge_job_0011.task_000009".intermediate_column_11_0 AS o_orderkey, any_value("pg_merge_job_0011.task_000009".intermediate_column_11_1) AS o_shippriority, count(*) AS count FROM (pg_merge_job_0010.task_000009 "pg_merge_job_0010.task_000009" JOIN pg_merge_job_0011.task_000009 "pg_merge_job_0011.task_000009" ON (("pg_merge_job_0010.task_000009".intermediate_column_10_0 OPERATOR(pg_catalog.=) "pg_merge_job_0011.task_000009".intermediate_column_11_1))) WHERE true GROUP BY "pg_merge_job_0011.task_000009".intermediate_column_11_0"
DEBUG:  generated sql query for task 12
DETAIL:  query string: "SELECT "pg_merge_job_0011.task_000012".intermediate_column_11_0 AS o_orderkey, any_value("pg_merge_job_0011.task_000012".intermediate_column_11_1) AS o_shippriority, count(*) AS count FROM (pg_merge_job_0010.task_000012 "pg_merge_job_0010.task_000012" JOIN pg_merge_job_0011.task_000012 "pg_merge_job_0011.task_000012" ON (("pg_merge_job_0010.task_000012".intermediate_column_10_0 OPERATOR(pg_catalog.=) "pg_merge_job_0011.task_000012".intermediate_column_11_1))) WHERE true GROUP BY "pg_merge_job_0011.task_000012".intermediate_column_11_0"
DEBUG:  pruning merge fetch taskId 1
DETAIL:  Creating dependency on merge taskId 3
DEBUG:  pruning merge fetch taskId 2
DETAIL:  Creating dependency on merge taskId 3
DEBUG:  pruning merge fetch taskId 4
DETAIL:  Creating dependency on merge taskId 6
DEBUG:  pruning merge fetch taskId 5
DETAIL:  Creating dependency on merge taskId 6
DEBUG:  pruning merge fetch taskId 7
DETAIL:  Creating dependency on merge taskId 9
DEBUG:  pruning merge fetch taskId 8
DETAIL:  Creating dependency on merge taskId 9
DEBUG:  pruning merge fetch taskId 10
DETAIL:  Creating dependency on merge taskId 12
DEBUG:  pruning merge fetch taskId 11
DETAIL:  Creating dependency on merge taskId 12
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  master query: SELECT o_orderkey, o_shippriority, COALESCE((pg_catalog.sum(count))::bigint, '0'::bigint) AS count FROM pg_catalog.citus_extradata_container(XXX, NULL::cstring(0), NULL::cstring(0), '(i 1 2)'::cstring(0)) remote_scan(o_orderkey bigint, o_shippriority integer, count bigint) GROUP BY o_orderkey ORDER BY o_orderkey
DEBUG:  completed cleanup query for job 12
DEBUG:  completed cleanup query for job 12
DEBUG:  completed cleanup query for job 10
DEBUG:  completed cleanup query for job 10
DEBUG:  completed cleanup query for job 11
DEBUG:  completed cleanup query for job 11
 o_orderkey | o_shippriority | count
---------------------------------------------------------------------
(0 rows)

-- Check that calling any_value manually works as well
SELECT
	o_orderkey, any_value(o_shippriority)
FROM
	lineitem, orders
WHERE
	l_suppkey = o_shippriority
GROUP BY
	o_orderkey, o_shippriority
ORDER BY
	o_orderkey;
DEBUG:  Router planner does not support append-partitioned tables.
DEBUG:  generated sql query for task 1
DETAIL:  query string: "SELECT l_suppkey FROM lineitem_290000 lineitem WHERE true"
DEBUG:  generated sql query for task 2
DETAIL:  query string: "SELECT l_suppkey FROM lineitem_290001 lineitem WHERE true"
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  generated sql query for task 1
DETAIL:  query string: "SELECT o_orderkey, o_shippriority FROM orders_290002 orders WHERE true"
DEBUG:  generated sql query for task 2
DETAIL:  query string: "SELECT o_orderkey, o_shippriority FROM orders_290003 orders WHERE true"
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  join prunable for task partitionId 0 and 1
DEBUG:  join prunable for task partitionId 0 and 2
DEBUG:  join prunable for task partitionId 0 and 3
DEBUG:  join prunable for task partitionId 1 and 0
DEBUG:  join prunable for task partitionId 1 and 2
DEBUG:  join prunable for task partitionId 1 and 3
DEBUG:  join prunable for task partitionId 2 and 0
DEBUG:  join prunable for task partitionId 2 and 1
DEBUG:  join prunable for task partitionId 2 and 3
DEBUG:  join prunable for task partitionId 3 and 0
DEBUG:  join prunable for task partitionId 3 and 1
DEBUG:  join prunable for task partitionId 3 and 2
DEBUG:  generated sql query for task 3
DETAIL:  query string: "SELECT "pg_merge_job_0014.task_000003".intermediate_column_14_0 AS o_orderkey, any_value("pg_merge_job_0014.task_000003".intermediate_column_14_1) AS any_value, any_value("pg_merge_job_0014.task_000003".intermediate_column_14_1) AS worker_column_3 FROM (pg_merge_job_0013.task_000003 "pg_merge_job_0013.task_000003" JOIN pg_merge_job_0014.task_000003 "pg_merge_job_0014.task_000003" ON (("pg_merge_job_0013.task_000003".intermediate_column_13_0 OPERATOR(pg_catalog.=) "pg_merge_job_0014.task_000003".intermediate_column_14_1))) WHERE true GROUP BY "pg_merge_job_0014.task_000003".intermediate_column_14_0"
DEBUG:  generated sql query for task 6
DETAIL:  query string: "SELECT "pg_merge_job_0014.task_000006".intermediate_column_14_0 AS o_orderkey, any_value("pg_merge_job_0014.task_000006".intermediate_column_14_1) AS any_value, any_value("pg_merge_job_0014.task_000006".intermediate_column_14_1) AS worker_column_3 FROM (pg_merge_job_0013.task_000006 "pg_merge_job_0013.task_000006" JOIN pg_merge_job_0014.task_000006 "pg_merge_job_0014.task_000006" ON (("pg_merge_job_0013.task_000006".intermediate_column_13_0 OPERATOR(pg_catalog.=) "pg_merge_job_0014.task_000006".intermediate_column_14_1))) WHERE true GROUP BY "pg_merge_job_0014.task_000006".intermediate_column_14_0"
DEBUG:  generated sql query for task 9
DETAIL:  query string: "SELECT "pg_merge_job_0014.task_000009".intermediate_column_14_0 AS o_orderkey, any_value("pg_merge_job_0014.task_000009".intermediate_column_14_1) AS any_value, any_value("pg_merge_job_0014.task_000009".intermediate_column_14_1) AS worker_column_3 FROM (pg_merge_job_0013.task_000009 "pg_merge_job_0013.task_000009" JOIN pg_merge_job_0014.task_000009 "pg_merge_job_0014.task_000009" ON (("pg_merge_job_0013.task_000009".intermediate_column_13_0 OPERATOR(pg_catalog.=) "pg_merge_job_0014.task_000009".intermediate_column_14_1))) WHERE true GROUP BY "pg_merge_job_0014.task_000009".intermediate_column_14_0"
DEBUG:  generated sql query for task 12
DETAIL:  query string: "SELECT "pg_merge_job_0014.task_000012".intermediate_column_14_0 AS o_orderkey, any_value("pg_merge_job_0014.task_000012".intermediate_column_14_1) AS any_value, any_value("pg_merge_job_0014.task_000012".intermediate_column_14_1) AS worker_column_3 FROM (pg_merge_job_0013.task_000012 "pg_merge_job_0013.task_000012" JOIN pg_merge_job_0014.task_000012 "pg_merge_job_0014.task_000012" ON (("pg_merge_job_0013.task_000012".intermediate_column_13_0 OPERATOR(pg_catalog.=) "pg_merge_job_0014.task_000012".intermediate_column_14_1))) WHERE true GROUP BY "pg_merge_job_0014.task_000012".intermediate_column_14_0"
DEBUG:  pruning merge fetch taskId 1
DETAIL:  Creating dependency on merge taskId 3
DEBUG:  pruning merge fetch taskId 2
DETAIL:  Creating dependency on merge taskId 3
DEBUG:  pruning merge fetch taskId 4
DETAIL:  Creating dependency on merge taskId 6
DEBUG:  pruning merge fetch taskId 5
DETAIL:  Creating dependency on merge taskId 6
DEBUG:  pruning merge fetch taskId 7
DETAIL:  Creating dependency on merge taskId 9
DEBUG:  pruning merge fetch taskId 8
DETAIL:  Creating dependency on merge taskId 9
DEBUG:  pruning merge fetch taskId 10
DETAIL:  Creating dependency on merge taskId 12
DEBUG:  pruning merge fetch taskId 11
DETAIL:  Creating dependency on merge taskId 12
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  master query: SELECT o_orderkey, any_value(any_value) AS any_value FROM pg_catalog.citus_extradata_container(XXX, NULL::cstring(0), NULL::cstring(0), '(i 1 2)'::cstring(0)) remote_scan(o_orderkey bigint, any_value integer, worker_column_3 integer) GROUP BY o_orderkey ORDER BY o_orderkey
DEBUG:  completed cleanup query for job 15
DEBUG:  completed cleanup query for job 15
DEBUG:  completed cleanup query for job 13
DEBUG:  completed cleanup query for job 13
DEBUG:  completed cleanup query for job 14
DEBUG:  completed cleanup query for job 14
 o_orderkey | any_value
---------------------------------------------------------------------
(0 rows)

-- Check that grouping by primary key allows s_quantity to be in the having
-- list
-- Postgres removes s_quantity from the group by clause here
select  s_i_id
    from  stock, order_line
    where ol_i_id=s_i_id
    group by s_i_id, s_w_id, s_quantity
    having   s_quantity > random()
;
DEBUG:  Router planner cannot handle multi-shard select queries
DEBUG:  generated sql query for task 1
DETAIL:  query string: "SELECT s_i_id, s_w_id, s_quantity FROM stock_690004 stock WHERE true"
DEBUG:  generated sql query for task 2
DETAIL:  query string: "SELECT s_i_id, s_w_id, s_quantity FROM stock_690005 stock WHERE true"
DEBUG:  generated sql query for task 3
DETAIL:  query string: "SELECT s_i_id, s_w_id, s_quantity FROM stock_690006 stock WHERE true"
DEBUG:  generated sql query for task 4
DETAIL:  query string: "SELECT s_i_id, s_w_id, s_quantity FROM stock_690007 stock WHERE true"
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  generated sql query for task 1
DETAIL:  query string: "SELECT ol_i_id FROM order_line_690000 order_line WHERE true"
DEBUG:  generated sql query for task 2
DETAIL:  query string: "SELECT ol_i_id FROM order_line_690001 order_line WHERE true"
DEBUG:  generated sql query for task 3
DETAIL:  query string: "SELECT ol_i_id FROM order_line_690002 order_line WHERE true"
DEBUG:  generated sql query for task 4
DETAIL:  query string: "SELECT ol_i_id FROM order_line_690003 order_line WHERE true"
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  join prunable for task partitionId 0 and 1
DEBUG:  join prunable for task partitionId 0 and 2
DEBUG:  join prunable for task partitionId 0 and 3
DEBUG:  join prunable for task partitionId 1 and 0
DEBUG:  join prunable for task partitionId 1 and 2
DEBUG:  join prunable for task partitionId 1 and 3
DEBUG:  join prunable for task partitionId 2 and 0
DEBUG:  join prunable for task partitionId 2 and 1
DEBUG:  join prunable for task partitionId 2 and 3
DEBUG:  join prunable for task partitionId 3 and 0
DEBUG:  join prunable for task partitionId 3 and 1
DEBUG:  join prunable for task partitionId 3 and 2
DEBUG:  generated sql query for task 3
DETAIL:  query string: "SELECT "pg_merge_job_0016.task_000005".intermediate_column_16_0 AS s_i_id, "pg_merge_job_0016.task_000005".intermediate_column_16_1 AS worker_column_2, any_value("pg_merge_job_0016.task_000005".intermediate_column_16_2) AS worker_column_3 FROM (pg_merge_job_0016.task_000005 "pg_merge_job_0016.task_000005" JOIN pg_merge_job_0017.task_000005 "pg_merge_job_0017.task_000005" ON (("pg_merge_job_0017.task_000005".intermediate_column_17_0 OPERATOR(pg_catalog.=) "pg_merge_job_0016.task_000005".intermediate_column_16_0))) WHERE true GROUP BY "pg_merge_job_0016.task_000005".intermediate_column_16_0, "pg_merge_job_0016.task_000005".intermediate_column_16_1 HAVING ((any_value("pg_merge_job_0016.task_000005".intermediate_column_16_2))::double precision OPERATOR(pg_catalog.>) random())"
DEBUG:  generated sql query for task 6
DETAIL:  query string: "SELECT "pg_merge_job_0016.task_000010".intermediate_column_16_0 AS s_i_id, "pg_merge_job_0016.task_000010".intermediate_column_16_1 AS worker_column_2, any_value("pg_merge_job_0016.task_000010".intermediate_column_16_2) AS worker_column_3 FROM (pg_merge_job_0016.task_000010 "pg_merge_job_0016.task_000010" JOIN pg_merge_job_0017.task_000010 "pg_merge_job_0017.task_000010" ON (("pg_merge_job_0017.task_000010".intermediate_column_17_0 OPERATOR(pg_catalog.=) "pg_merge_job_0016.task_000010".intermediate_column_16_0))) WHERE true GROUP BY "pg_merge_job_0016.task_000010".intermediate_column_16_0, "pg_merge_job_0016.task_000010".intermediate_column_16_1 HAVING ((any_value("pg_merge_job_0016.task_000010".intermediate_column_16_2))::double precision OPERATOR(pg_catalog.>) random())"
DEBUG:  generated sql query for task 9
DETAIL:  query string: "SELECT "pg_merge_job_0016.task_000015".intermediate_column_16_0 AS s_i_id, "pg_merge_job_0016.task_000015".intermediate_column_16_1 AS worker_column_2, any_value("pg_merge_job_0016.task_000015".intermediate_column_16_2) AS worker_column_3 FROM (pg_merge_job_0016.task_000015 "pg_merge_job_0016.task_000015" JOIN pg_merge_job_0017.task_000015 "pg_merge_job_0017.task_000015" ON (("pg_merge_job_0017.task_000015".intermediate_column_17_0 OPERATOR(pg_catalog.=) "pg_merge_job_0016.task_000015".intermediate_column_16_0))) WHERE true GROUP BY "pg_merge_job_0016.task_000015".intermediate_column_16_0, "pg_merge_job_0016.task_000015".intermediate_column_16_1 HAVING ((any_value("pg_merge_job_0016.task_000015".intermediate_column_16_2))::double precision OPERATOR(pg_catalog.>) random())"
DEBUG:  generated sql query for task 12
DETAIL:  query string: "SELECT "pg_merge_job_0016.task_000020".intermediate_column_16_0 AS s_i_id, "pg_merge_job_0016.task_000020".intermediate_column_16_1 AS worker_column_2, any_value("pg_merge_job_0016.task_000020".intermediate_column_16_2) AS worker_column_3 FROM (pg_merge_job_0016.task_000020 "pg_merge_job_0016.task_000020" JOIN pg_merge_job_0017.task_000020 "pg_merge_job_0017.task_000020" ON (("pg_merge_job_0017.task_000020".intermediate_column_17_0 OPERATOR(pg_catalog.=) "pg_merge_job_0016.task_000020".intermediate_column_16_0))) WHERE true GROUP BY "pg_merge_job_0016.task_000020".intermediate_column_16_0, "pg_merge_job_0016.task_000020".intermediate_column_16_1 HAVING ((any_value("pg_merge_job_0016.task_000020".intermediate_column_16_2))::double precision OPERATOR(pg_catalog.>) random())"
DEBUG:  pruning merge fetch taskId 1
DETAIL:  Creating dependency on merge taskId 5
DEBUG:  pruning merge fetch taskId 2
DETAIL:  Creating dependency on merge taskId 5
DEBUG:  pruning merge fetch taskId 4
DETAIL:  Creating dependency on merge taskId 10
DEBUG:  pruning merge fetch taskId 5
DETAIL:  Creating dependency on merge taskId 10
DEBUG:  pruning merge fetch taskId 7
DETAIL:  Creating dependency on merge taskId 15
DEBUG:  pruning merge fetch taskId 8
DETAIL:  Creating dependency on merge taskId 15
DEBUG:  pruning merge fetch taskId 10
DETAIL:  Creating dependency on merge taskId 20
DEBUG:  pruning merge fetch taskId 11
DETAIL:  Creating dependency on merge taskId 20
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  master query: SELECT s_i_id FROM pg_catalog.citus_extradata_container(XXX, NULL::cstring(0), NULL::cstring(0), '(i 1 2)'::cstring(0)) remote_scan(s_i_id integer, worker_column_2 integer, worker_column_3 numeric)
DEBUG:  completed cleanup query for job 18
DEBUG:  completed cleanup query for job 18
DEBUG:  completed cleanup query for job 16
DEBUG:  completed cleanup query for job 16
DEBUG:  completed cleanup query for job 17
DEBUG:  completed cleanup query for job 17
 s_i_id
---------------------------------------------------------------------
(0 rows)

-- Reset client logging level to its previous value
SET client_min_messages TO NOTICE;
COMMIT;
drop schema repartition_join;
