--
-- MULTI_JOIN_PRUNING
--
-- Check that join-pruning works for joins between two relations. For now
-- we only check for join-pruning between locally partitioned relations. In the
-- future we want to check for pruning between re-partitioned relations as well.
SET citus.explain_distributed_queries TO off;
SET client_min_messages TO DEBUG2;
SELECT sum(l_linenumber), avg(l_linenumber) FROM lineitem, orders
	WHERE l_orderkey = o_orderkey;
DEBUG:  Router planner cannot handle multi-shard select queries
DEBUG:  join prunable for intervals [-2147483648,-1] and [0,2147483647]
DEBUG:  join prunable for intervals [0,2147483647] and [-2147483648,-1]
  sum  |        avg
---------------------------------------------------------------------
 36089 | 3.0074166666666667
(1 row)

SELECT sum(l_linenumber), avg(l_linenumber) FROM lineitem, orders
	WHERE l_orderkey = o_orderkey AND l_orderkey > 9030;
DEBUG:  Router planner cannot handle multi-shard select queries
DEBUG:  join prunable for intervals [-2147483648,-1] and [0,2147483647]
DEBUG:  join prunable for intervals [0,2147483647] and [-2147483648,-1]
  sum  |        avg
---------------------------------------------------------------------
 17999 | 3.0189533713518953
(1 row)

-- Shards for the lineitem table have been pruned away. Check that join pruning
-- works as expected in this case.
SELECT sum(l_linenumber), avg(l_linenumber) FROM lineitem, orders
	WHERE l_orderkey = o_orderkey AND l_orderkey > 20000;
DEBUG:  Router planner cannot handle multi-shard select queries
DEBUG:  join prunable for intervals [-2147483648,-1] and [0,2147483647]
DEBUG:  join prunable for intervals [0,2147483647] and [-2147483648,-1]
 sum | avg
---------------------------------------------------------------------
     |
(1 row)

-- Partition pruning left three shards for the lineitem and one shard for the
-- orders table. These shard sets don't overlap, so join pruning should prune
-- out all the shards, and leave us with an empty task list.
select * from pg_dist_shard
where logicalrelid='lineitem'::regclass or
	  logicalrelid='orders'::regclass
order by shardid;
 logicalrelid | shardid | shardstorage | shardminvalue | shardmaxvalue
---------------------------------------------------------------------
 lineitem     |  360000 | t            | -2147483648   | -1
 lineitem     |  360001 | t            | 0             | 2147483647
 orders       |  360002 | t            | -2147483648   | -1
 orders       |  360003 | t            | 0             | 2147483647
(4 rows)

set client_min_messages to debug3;
SELECT sum(l_linenumber), avg(l_linenumber) FROM lineitem, orders
	WHERE l_orderkey = o_orderkey AND l_orderkey > 6000 AND o_orderkey < 6000;
DEBUG:  no shard pruning constraints on lineitem found
DEBUG:  shard count after pruning for lineitem: 2
DEBUG:  no shard pruning constraints on orders found
DEBUG:  shard count after pruning for orders: 2
DEBUG:  Router planner cannot handle multi-shard select queries
DEBUG:  no shard pruning constraints on lineitem found
DEBUG:  shard count after pruning for lineitem: 2
DEBUG:  no shard pruning constraints on orders found
DEBUG:  shard count after pruning for orders: 2
DEBUG:  join prunable for intervals [-2147483648,-1] and [0,2147483647]
DEBUG:  join prunable for intervals [0,2147483647] and [-2147483648,-1]
DEBUG:  assigned task to node localhost:xxxxx
DEBUG:  assigned task to node localhost:xxxxx
 sum | avg
---------------------------------------------------------------------
     |
(1 row)

set client_min_messages to debug2;
-- Make sure that we can handle filters without a column
SELECT sum(l_linenumber), avg(l_linenumber) FROM lineitem, orders
	WHERE l_orderkey = o_orderkey AND false;
DEBUG:  Creating router plan
 sum | avg
---------------------------------------------------------------------
     |
(1 row)

SELECT sum(l_linenumber), avg(l_linenumber)
    FROM lineitem INNER JOIN orders ON (l_orderkey = o_orderkey)
	WHERE false;
DEBUG:  Creating router plan
 sum | avg
---------------------------------------------------------------------
     |
(1 row)

-- These tests check that we can do join pruning for tables partitioned over
-- different type of columns including varchar, array types, composite types
-- etc. This is in response to a bug we had where we were not able to resolve
-- correct operator types for some kind of column types.
EXPLAIN (COSTS OFF)
SELECT count(*)
	FROM array_partitioned_table table1, array_partitioned_table table2
	WHERE table1.array_column = table2.array_column;
DEBUG:  Router planner cannot handle multi-shard select queries
DEBUG:  join prunable for intervals [{},{AZZXSP27F21T6,AZZXSP27F21T6}] and [{BA1000U2AMO4ZGX,BZZXSP27F21T6},{CA1000U2AMO4ZGX,CZZXSP27F21T6}]
DEBUG:  join prunable for intervals [{BA1000U2AMO4ZGX,BZZXSP27F21T6},{CA1000U2AMO4ZGX,CZZXSP27F21T6}] and [{},{AZZXSP27F21T6,AZZXSP27F21T6}]
                    QUERY PLAN
---------------------------------------------------------------------
 Aggregate
   ->  Custom Scan (Citus Adaptive)
         citus.explain_distributed_queries: false
(3 rows)

EXPLAIN (COSTS OFF)
SELECT count(*)
	FROM composite_partitioned_table table1, composite_partitioned_table table2
	WHERE table1.composite_column = table2.composite_column;
DEBUG:  Router planner cannot handle multi-shard select queries
DEBUG:  join prunable for intervals [(a,3,b),(b,4,c)] and [(c,5,d),(d,6,e)]
DEBUG:  join prunable for intervals [(c,5,d),(d,6,e)] and [(a,3,b),(b,4,c)]
                    QUERY PLAN
---------------------------------------------------------------------
 Aggregate
   ->  Custom Scan (Citus Adaptive)
         citus.explain_distributed_queries: false
(3 rows)

-- Test that large table joins on partition varchar columns work
EXPLAIN (COSTS OFF)
SELECT count(*)
	FROM varchar_partitioned_table table1, varchar_partitioned_table table2
	WHERE table1.varchar_column = table2.varchar_column;
DEBUG:  Router planner cannot handle multi-shard select queries
DEBUG:  join prunable for intervals [AA1000U2AMO4ZGX,AZZXSP27F21T6] and [BA1000U2AMO4ZGX,BZZXSP27F21T6]
DEBUG:  join prunable for intervals [BA1000U2AMO4ZGX,BZZXSP27F21T6] and [AA1000U2AMO4ZGX,AZZXSP27F21T6]
                    QUERY PLAN
---------------------------------------------------------------------
 Aggregate
   ->  Custom Scan (Citus Adaptive)
         citus.explain_distributed_queries: false
(3 rows)

SET client_min_messages TO WARNING;
DROP TABLE varchar_partitioned_table;
DROP TABLE array_partitioned_table;
DROP TABLE composite_partitioned_table;
