--
-- MULTI_REPARTITION_JOIN_TASK_ASSIGNMENT
--
-- Tests which cover task assignment for MapMerge jobs for single range repartition
-- and dual hash repartition joins. The tests also cover task assignment propagation
-- from a sql task to its dependent tasks. Note that 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 710000;
BEGIN;
SET client_min_messages TO DEBUG3;
SET citus.enable_repartition_joins to ON;
-- Single range repartition join to test anchor-shard based task assignment and
-- assignment propagation to merge and data-fetch tasks.
SELECT
	count(*)
FROM
	orders, customer_append
WHERE
	o_custkey = c_custkey;
DEBUG:  Router planner does not support append-partitioned tables.
DEBUG:  no shard pruning constraints on orders found
DEBUG:  shard count after pruning for orders: 2
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  no shard pruning constraints on customer_append found
DEBUG:  shard count after pruning for customer_append: 3
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 0 and 4
DEBUG:  join prunable for task partitionId 0 and 5
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 1 and 4
DEBUG:  join prunable for task partitionId 1 and 5
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 2 and 4
DEBUG:  join prunable for task partitionId 2 and 5
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:  join prunable for task partitionId 3 and 4
DEBUG:  join prunable for task partitionId 3 and 5
DEBUG:  join prunable for task partitionId 4 and 0
DEBUG:  join prunable for task partitionId 4 and 1
DEBUG:  join prunable for task partitionId 4 and 2
DEBUG:  join prunable for task partitionId 4 and 3
DEBUG:  join prunable for task partitionId 4 and 5
DEBUG:  join prunable for task partitionId 5 and 0
DEBUG:  join prunable for task partitionId 5 and 1
DEBUG:  join prunable for task partitionId 5 and 2
DEBUG:  join prunable for task partitionId 5 and 3
DEBUG:  join prunable for task partitionId 5 and 4
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 4
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 8
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 12
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 16
DEBUG:  pruning merge fetch taskId 13
DETAIL:  Creating dependency on merge taskId 15
DEBUG:  pruning merge fetch taskId 14
DETAIL:  Creating dependency on merge taskId 20
DEBUG:  pruning merge fetch taskId 16
DETAIL:  Creating dependency on merge taskId 18
DEBUG:  pruning merge fetch taskId 17
DETAIL:  Creating dependency on merge taskId 24
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:  assigned task to node localhost:xxxxx
DEBUG:  assigned task to node localhost:xxxxx
 count
---------------------------------------------------------------------
  2985
(1 row)

-- Single range repartition join, along with a join with a small table containing
-- more than one shard. This situation results in multiple sql tasks depending on
-- the same merge task, and tests our constraint group creation and assignment
-- propagation.
SELECT
	count(*)
FROM
	orders_reference, customer_append, lineitem
WHERE
	o_custkey = c_custkey AND
	o_orderkey = l_orderkey;
DEBUG:  Router planner does not support append-partitioned tables.
DEBUG:  no shard pruning constraints on customer_append found
DEBUG:  shard count after pruning for customer_append: 3
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  no shard pruning constraints on lineitem found
DEBUG:  shard count after pruning for lineitem: 2
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 0 and 4
DEBUG:  join prunable for task partitionId 0 and 5
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 1 and 4
DEBUG:  join prunable for task partitionId 1 and 5
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 2 and 4
DEBUG:  join prunable for task partitionId 2 and 5
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:  join prunable for task partitionId 3 and 4
DEBUG:  join prunable for task partitionId 3 and 5
DEBUG:  join prunable for task partitionId 4 and 0
DEBUG:  join prunable for task partitionId 4 and 1
DEBUG:  join prunable for task partitionId 4 and 2
DEBUG:  join prunable for task partitionId 4 and 3
DEBUG:  join prunable for task partitionId 4 and 5
DEBUG:  join prunable for task partitionId 5 and 0
DEBUG:  join prunable for task partitionId 5 and 1
DEBUG:  join prunable for task partitionId 5 and 2
DEBUG:  join prunable for task partitionId 5 and 3
DEBUG:  join prunable for task partitionId 5 and 4
DEBUG:  pruning merge fetch taskId 1
DETAIL:  Creating dependency on merge taskId 4
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 8
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 12
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 16
DEBUG:  pruning merge fetch taskId 11
DETAIL:  Creating dependency on merge taskId 12
DEBUG:  pruning merge fetch taskId 13
DETAIL:  Creating dependency on merge taskId 20
DEBUG:  pruning merge fetch taskId 14
DETAIL:  Creating dependency on merge taskId 15
DEBUG:  pruning merge fetch taskId 16
DETAIL:  Creating dependency on merge taskId 24
DEBUG:  pruning merge fetch taskId 17
DETAIL:  Creating dependency on merge taskId 18
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:  assigned task to node localhost:xxxxx
DEBUG:  assigned task to node localhost:xxxxx
 count
---------------------------------------------------------------------
 12000
(1 row)

-- Dual hash repartition join which tests the separate hash repartition join
-- task assignment algorithm.
SELECT
	count(*)
FROM
	lineitem, customer_append
WHERE
	l_partkey = c_nationkey;
DEBUG:  Router planner does not support append-partitioned tables.
DEBUG:  no shard pruning constraints on lineitem found
DEBUG:  shard count after pruning for lineitem: 2
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  no shard pruning constraints on customer_append found
DEBUG:  shard count after pruning for customer_append: 3
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 0 and 4
DEBUG:  join prunable for task partitionId 0 and 5
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 1 and 4
DEBUG:  join prunable for task partitionId 1 and 5
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 2 and 4
DEBUG:  join prunable for task partitionId 2 and 5
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:  join prunable for task partitionId 3 and 4
DEBUG:  join prunable for task partitionId 3 and 5
DEBUG:  join prunable for task partitionId 4 and 0
DEBUG:  join prunable for task partitionId 4 and 1
DEBUG:  join prunable for task partitionId 4 and 2
DEBUG:  join prunable for task partitionId 4 and 3
DEBUG:  join prunable for task partitionId 4 and 5
DEBUG:  join prunable for task partitionId 5 and 0
DEBUG:  join prunable for task partitionId 5 and 1
DEBUG:  join prunable for task partitionId 5 and 2
DEBUG:  join prunable for task partitionId 5 and 3
DEBUG:  join prunable for task partitionId 5 and 4
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 4
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 8
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 12
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 16
DEBUG:  pruning merge fetch taskId 13
DETAIL:  Creating dependency on merge taskId 15
DEBUG:  pruning merge fetch taskId 14
DETAIL:  Creating dependency on merge taskId 20
DEBUG:  pruning merge fetch taskId 16
DETAIL:  Creating dependency on merge taskId 18
DEBUG:  pruning merge fetch taskId 17
DETAIL:  Creating dependency on merge taskId 24
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:  assigned task to node localhost:xxxxx
DEBUG:  assigned task to node localhost:xxxxx
 count
---------------------------------------------------------------------
   125
(1 row)

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