--
-- NON_COLOCATED_JOIN_ORDER
--
-- Tests to check placements of shards must be equal to choose local join logic.
CREATE TABLE test_table_1(id int, value_1 int);
SELECT master_create_distributed_table('test_table_1', 'id', 'append');
 master_create_distributed_table
---------------------------------------------------------------------

(1 row)

\copy test_table_1 FROM STDIN DELIMITER ','
\copy test_table_1 FROM STDIN DELIMITER ','
CREATE TABLE test_table_2(id int, value_1 int);
SELECT master_create_distributed_table('test_table_2', 'id', 'append');
 master_create_distributed_table
---------------------------------------------------------------------

(1 row)

\copy test_table_2 FROM STDIN DELIMITER ','
\copy test_table_2 FROM STDIN DELIMITER ','
SET citus.log_multi_join_order to TRUE;
SET client_min_messages to DEBUG1;
-- Since we both have same amount of shards and they are colocated on the same node
-- local join logic will be triggered.
SELECT count(*) FROM test_table_1, test_table_2 WHERE test_table_1.id = test_table_2.id;
LOG:  join order: [ "test_table_1" ][ local partition join "test_table_2" ]
 count
---------------------------------------------------------------------
     6
(1 row)

-- Add two shards placement of interval [8,10] to test_table_1
SET citus.shard_replication_factor to 2;
\copy test_table_1 FROM STDIN DELIMITER ','
-- Add two shards placement of interval [8,10] to test_table_2
SET citus.shard_replication_factor to 1;
\copy test_table_2 FROM STDIN DELIMITER ','
-- Although shard interval of relation are same, since they have different amount of placements
-- for interval [8,10] repartition join logic will be triggered.
SET citus.enable_repartition_joins to ON;
SELECT count(*) FROM test_table_1, test_table_2 WHERE test_table_1.id = test_table_2.id;
LOG:  join order: [ "test_table_1" ][ single range partition join "test_table_2" ]
 count
---------------------------------------------------------------------
     9
(1 row)

SET client_min_messages TO default;
DROP TABLE test_table_1;
DROP TABLE test_table_2;
