--
-- MULTI_HASH_PRUNING
--
-- Tests for shard and join pruning logic on hash partitioned tables.
SET citus.next_shard_id TO 630000;
SET citus.shard_count to 4;
SET citus.shard_replication_factor to 1;
-- many of the tests in this file is intended for testing non-fast-path
-- router planner, so we're explicitly disabling it in this file.
-- We've bunch of other tests that triggers fast-path-router
SET citus.enable_fast_path_router_planner TO false;
-- Create a table partitioned on integer column and update partition type to
-- hash. Then load data into this table and update shard min max values with
-- hashed ones. Hash value of 1, 2, 3  and 4 are consecutively -1905060026,
-- 1134484726, -28094569 and -1011077333.
CREATE TABLE orders_hash_partitioned (
	o_orderkey integer,
	o_custkey integer,
	o_orderstatus char(1),
	o_totalprice decimal(15,2),
	o_orderdate date,
	o_orderpriority char(15),
	o_clerk char(15),
	o_shippriority integer,
	o_comment varchar(79) );
SELECT create_distributed_table('orders_hash_partitioned', 'o_orderkey');
 create_distributed_table
---------------------------------------------------------------------

(1 row)

SET client_min_messages TO DEBUG2;
-- Check that we can prune shards for simple cases, boolean expressions and
-- immutable functions.
SELECT count(*) FROM orders_hash_partitioned;
DEBUG:  Router planner cannot handle multi-shard select queries
 count
---------------------------------------------------------------------
     0
(1 row)

SELECT count(*) FROM orders_hash_partitioned WHERE o_orderkey = 1;
DEBUG:  Creating router plan
DEBUG:  Plan is router executable
DETAIL:  distribution column value: 1
 count
---------------------------------------------------------------------
     0
(1 row)

SELECT count(*) FROM orders_hash_partitioned WHERE o_orderkey = 2;
DEBUG:  Creating router plan
DEBUG:  Plan is router executable
DETAIL:  distribution column value: 2
 count
---------------------------------------------------------------------
     0
(1 row)

SELECT count(*) FROM orders_hash_partitioned WHERE o_orderkey = 3;
DEBUG:  Creating router plan
DEBUG:  Plan is router executable
DETAIL:  distribution column value: 3
 count
---------------------------------------------------------------------
     0
(1 row)

SELECT count(*) FROM orders_hash_partitioned WHERE o_orderkey = 4;
DEBUG:  Creating router plan
DEBUG:  Plan is router executable
DETAIL:  distribution column value: 4
 count
---------------------------------------------------------------------
     0
(1 row)

SELECT count(*) FROM orders_hash_partitioned
	WHERE o_orderkey = 1 AND o_clerk = 'aaa';
DEBUG:  Creating router plan
DEBUG:  Plan is router executable
DETAIL:  distribution column value: 1
 count
---------------------------------------------------------------------
     0
(1 row)

SELECT count(*) FROM orders_hash_partitioned WHERE o_orderkey = abs(-1);
DEBUG:  Creating router plan
DEBUG:  Plan is router executable
DETAIL:  distribution column value: 1
 count
---------------------------------------------------------------------
     0
(1 row)

-- disable router planning
SET citus.enable_router_execution TO 'false';
SELECT count(*) FROM orders_hash_partitioned;
DEBUG:  Router planner not enabled.
 count
---------------------------------------------------------------------
     0
(1 row)

SELECT count(*) FROM orders_hash_partitioned WHERE o_orderkey = 1;
DEBUG:  Router planner not enabled.
 count
---------------------------------------------------------------------
     0
(1 row)

SELECT count(*) FROM orders_hash_partitioned WHERE o_orderkey = 2;
DEBUG:  Router planner not enabled.
 count
---------------------------------------------------------------------
     0
(1 row)

SELECT count(*) FROM orders_hash_partitioned WHERE o_orderkey = 3;
DEBUG:  Router planner not enabled.
 count
---------------------------------------------------------------------
     0
(1 row)

SELECT count(*) FROM orders_hash_partitioned WHERE o_orderkey = 4;
DEBUG:  Router planner not enabled.
 count
---------------------------------------------------------------------
     0
(1 row)

SELECT count(*) FROM orders_hash_partitioned
	WHERE o_orderkey = 1 AND o_clerk = 'aaa';
DEBUG:  Router planner not enabled.
 count
---------------------------------------------------------------------
     0
(1 row)

SELECT count(*) FROM orders_hash_partitioned WHERE o_orderkey = abs(-1);
DEBUG:  Router planner not enabled.
 count
---------------------------------------------------------------------
     0
(1 row)

SET citus.enable_router_execution TO DEFAULT;
SELECT count(*) FROM orders_hash_partitioned WHERE o_orderkey is NULL;
DEBUG:  Router planner cannot handle multi-shard select queries
 count
---------------------------------------------------------------------
     0
(1 row)

SELECT count(*) FROM orders_hash_partitioned WHERE o_orderkey is not NULL;
DEBUG:  Router planner cannot handle multi-shard select queries
 count
---------------------------------------------------------------------
     0
(1 row)

SELECT count(*) FROM orders_hash_partitioned WHERE o_orderkey > 2;
DEBUG:  Router planner cannot handle multi-shard select queries
 count
---------------------------------------------------------------------
     0
(1 row)

SELECT count(*) FROM orders_hash_partitioned
	WHERE o_orderkey = 1 OR o_orderkey = 2;
DEBUG:  Router planner cannot handle multi-shard select queries
 count
---------------------------------------------------------------------
     0
(1 row)

SELECT count(*) FROM orders_hash_partitioned
	WHERE o_orderkey = 1 OR o_clerk = 'aaa';
DEBUG:  Router planner cannot handle multi-shard select queries
 count
---------------------------------------------------------------------
     0
(1 row)

SELECT count(*) FROM orders_hash_partitioned
	WHERE o_orderkey = 1 OR (o_orderkey = 3 AND o_clerk = 'aaa');
DEBUG:  Router planner cannot handle multi-shard select queries
 count
---------------------------------------------------------------------
     0
(1 row)

SELECT count(*) FROM orders_hash_partitioned
	WHERE o_orderkey = 1 OR o_orderkey is NULL;
DEBUG:  Router planner cannot handle multi-shard select queries
 count
---------------------------------------------------------------------
     0
(1 row)

SELECT count(*) FROM
       (SELECT o_orderkey FROM orders_hash_partitioned WHERE o_orderkey = 1) AS orderkeys;
DEBUG:  Creating router plan
DEBUG:  Plan is router executable
DETAIL:  distribution column value: 1
 count
---------------------------------------------------------------------
     0
(1 row)

SET client_min_messages TO DEFAULT;
-- Check that we support runing for ANY/IN with literal.
SELECT count(*) FROM lineitem_hash_part
	WHERE l_orderkey = ANY ('{1,2,3}');
 count
---------------------------------------------------------------------
    13
(1 row)

SELECT count(*) FROM lineitem_hash_part
	WHERE l_orderkey IN (1,2,3);
 count
---------------------------------------------------------------------
    13
(1 row)

-- Check whether we can deal with null arrays
SELECT count(*) FROM lineitem_hash_part
	WHERE l_orderkey IN (NULL);
 count
---------------------------------------------------------------------
     0
(1 row)

SELECT count(*) FROM lineitem_hash_part
	WHERE l_orderkey = ANY (NULL);
 count
---------------------------------------------------------------------
     0
(1 row)

SELECT count(*) FROM lineitem_hash_part
	WHERE l_orderkey IN (NULL) OR TRUE;
 count
---------------------------------------------------------------------
 12000
(1 row)

SELECT count(*) FROM lineitem_hash_part
	WHERE l_orderkey = ANY (NULL) OR TRUE;	
 count
---------------------------------------------------------------------
 12000
(1 row)

-- Check whether we support IN/ANY in subquery
SELECT count(*) FROM lineitem_hash_part WHERE l_orderkey IN (SELECT l_orderkey FROM lineitem_hash_part);
 count
---------------------------------------------------------------------
 12000
(1 row)

SELECT count(*) FROM lineitem_hash_part WHERE l_orderkey = ANY (SELECT l_orderkey FROM lineitem_hash_part);
 count
---------------------------------------------------------------------
 12000
(1 row)

-- Check whether we support IN/ANY in subquery with append and range distributed table
SELECT count(*) FROM lineitem
	WHERE l_orderkey = ANY ('{1,2,3}');
 count
---------------------------------------------------------------------
    13
(1 row)

SELECT count(*) FROM lineitem
	WHERE l_orderkey IN (1,2,3);
 count
---------------------------------------------------------------------
    13
(1 row)

SELECT count(*) FROM lineitem
	WHERE l_orderkey = ANY(NULL) OR TRUE;	
 count
---------------------------------------------------------------------
 12000
(1 row)

SELECT count(*) FROM lineitem_range
	WHERE l_orderkey = ANY ('{1,2,3}');
 count
---------------------------------------------------------------------
    13
(1 row)

SELECT count(*) FROM lineitem_range
	WHERE l_orderkey IN (1,2,3);
 count
---------------------------------------------------------------------
    13
(1 row)

SELECT count(*) FROM lineitem_range
	WHERE l_orderkey = ANY(NULL) OR TRUE;	
 count
---------------------------------------------------------------------
 12000
(1 row)

SET client_min_messages TO DEBUG2;
-- Check that we don't show the message if the operator is not
-- equality operator
SELECT count(*) FROM orders_hash_partitioned
	WHERE o_orderkey < ALL ('{1,2,3}');
DEBUG:  Router planner cannot handle multi-shard select queries
 count
---------------------------------------------------------------------
     0
(1 row)

-- Check that we don't give a spurious hint message when non-partition
-- columns are used with ANY/IN/ALL
SELECT count(*) FROM orders_hash_partitioned
	WHERE o_orderkey = 1 OR o_totalprice IN (2, 5);
DEBUG:  Router planner cannot handle multi-shard select queries
 count
---------------------------------------------------------------------
     0
(1 row)

-- Check that we cannot prune for mutable functions.
SELECT count(*) FROM orders_hash_partitioned WHERE o_orderkey = random();
DEBUG:  Router planner cannot handle multi-shard select queries
 count
---------------------------------------------------------------------
     0
(1 row)

SELECT count(*) FROM orders_hash_partitioned
	WHERE o_orderkey = random() OR o_orderkey = 1;
DEBUG:  Router planner cannot handle multi-shard select queries
 count
---------------------------------------------------------------------
     0
(1 row)

SELECT count(*) FROM orders_hash_partitioned
	WHERE o_orderkey = random() AND o_orderkey = 1;
DEBUG:  Creating router plan
DEBUG:  Plan is router executable
DETAIL:  distribution column value: 1
 count
---------------------------------------------------------------------
     0
(1 row)

-- Check that we can do join pruning.
SELECT count(*)
	FROM orders_hash_partitioned orders1, orders_hash_partitioned orders2
	WHERE orders1.o_orderkey = orders2.o_orderkey;
DEBUG:  Router planner cannot handle multi-shard select queries
DEBUG:  join prunable for intervals [-2147483648,-1073741825] and [-1073741824,-1]
DEBUG:  join prunable for intervals [-2147483648,-1073741825] and [0,1073741823]
DEBUG:  join prunable for intervals [-2147483648,-1073741825] and [1073741824,2147483647]
DEBUG:  join prunable for intervals [-1073741824,-1] and [-2147483648,-1073741825]
DEBUG:  join prunable for intervals [-1073741824,-1] and [0,1073741823]
DEBUG:  join prunable for intervals [-1073741824,-1] and [1073741824,2147483647]
DEBUG:  join prunable for intervals [0,1073741823] and [-2147483648,-1073741825]
DEBUG:  join prunable for intervals [0,1073741823] and [-1073741824,-1]
DEBUG:  join prunable for intervals [0,1073741823] and [1073741824,2147483647]
DEBUG:  join prunable for intervals [1073741824,2147483647] and [-2147483648,-1073741825]
DEBUG:  join prunable for intervals [1073741824,2147483647] and [-1073741824,-1]
DEBUG:  join prunable for intervals [1073741824,2147483647] and [0,1073741823]
 count
---------------------------------------------------------------------
     0
(1 row)

SELECT count(*)
	FROM orders_hash_partitioned orders1, orders_hash_partitioned orders2
	WHERE orders1.o_orderkey = orders2.o_orderkey
	AND orders1.o_orderkey = 1
	AND orders2.o_orderkey is NULL;
DEBUG:  Creating router plan
DEBUG:  Plan is router executable
DETAIL:  distribution column value: 1
 count
---------------------------------------------------------------------
     0
(1 row)

