CREATE SCHEMA worker_split_binary_copy_test;
SET search_path TO worker_split_binary_copy_test;
SET citus.shard_count TO 1;
SET citus.shard_replication_factor TO 1;
SET citus.next_shard_id TO 81060000;
-- BEGIN: Create distributed table and insert data.
CREATE TABLE worker_split_binary_copy_test.shard_to_split_copy (
	l_orderkey bigint not null,
	l_partkey integer not null,
	l_suppkey integer not null,
	l_linenumber integer not null,
	l_quantity decimal(15, 2) not null,
	l_extendedprice decimal(15, 2) not null,
	l_discount decimal(15, 2) not null,
	l_tax decimal(15, 2) not null,
	l_returnflag char(1) not null,
	l_linestatus char(1) not null,
	l_shipdate date not null,
	l_commitdate date not null,
	l_receiptdate date not null,
	l_shipinstruct char(25) not null,
	l_shipmode char(10) not null,
	l_comment varchar(44) not null);
SELECT create_distributed_table('shard_to_split_copy', 'l_orderkey');
 create_distributed_table
---------------------------------------------------------------------

(1 row)

\COPY shard_to_split_copy FROM STDIN WITH DELIMITER '|'
-- END: Create distributed table and insert data.
-- BEGIN: Switch to Worker1, Create target shards in worker for local 2-way split copy.
\c - - - :worker_1_port
CREATE TABLE worker_split_binary_copy_test.shard_to_split_copy_81060015 (
	l_orderkey bigint not null,
	l_partkey integer not null,
	l_suppkey integer not null,
	l_linenumber integer not null,
	l_quantity decimal(15, 2) not null,
	l_extendedprice decimal(15, 2) not null,
	l_discount decimal(15, 2) not null,
	l_tax decimal(15, 2) not null,
	l_returnflag char(1) not null,
	l_linestatus char(1) not null,
	l_shipdate date not null,
	l_commitdate date not null,
	l_receiptdate date not null,
	l_shipinstruct char(25) not null,
	l_shipmode char(10) not null,
	l_comment varchar(44) not null);
CREATE TABLE worker_split_binary_copy_test.shard_to_split_copy_81060016 (
	l_orderkey bigint not null,
	l_partkey integer not null,
	l_suppkey integer not null,
	l_linenumber integer not null,
	l_quantity decimal(15, 2) not null,
	l_extendedprice decimal(15, 2) not null,
	l_discount decimal(15, 2) not null,
	l_tax decimal(15, 2) not null,
	l_returnflag char(1) not null,
	l_linestatus char(1) not null,
	l_shipdate date not null,
	l_commitdate date not null,
	l_receiptdate date not null,
	l_shipinstruct char(25) not null,
	l_shipmode char(10) not null,
	l_comment varchar(44) not null);
-- End: Switch to Worker1, Create target shards in worker for local 2-way split copy.
-- BEGIN: Switch to Worker2, Create target shards in worker for remote 2-way split copy.
\c - - - :worker_2_port
CREATE TABLE worker_split_binary_copy_test.shard_to_split_copy_81060015 (
	l_orderkey bigint not null,
	l_partkey integer not null,
	l_suppkey integer not null,
	l_linenumber integer not null,
	l_quantity decimal(15, 2) not null,
	l_extendedprice decimal(15, 2) not null,
	l_discount decimal(15, 2) not null,
	l_tax decimal(15, 2) not null,
	l_returnflag char(1) not null,
	l_linestatus char(1) not null,
	l_shipdate date not null,
	l_commitdate date not null,
	l_receiptdate date not null,
	l_shipinstruct char(25) not null,
	l_shipmode char(10) not null,
	l_comment varchar(44) not null);
CREATE TABLE worker_split_binary_copy_test.shard_to_split_copy_81060016 (
	l_orderkey bigint not null,
	l_partkey integer not null,
	l_suppkey integer not null,
	l_linenumber integer not null,
	l_quantity decimal(15, 2) not null,
	l_extendedprice decimal(15, 2) not null,
	l_discount decimal(15, 2) not null,
	l_tax decimal(15, 2) not null,
	l_returnflag char(1) not null,
	l_linestatus char(1) not null,
	l_shipdate date not null,
	l_commitdate date not null,
	l_receiptdate date not null,
	l_shipinstruct char(25) not null,
	l_shipmode char(10) not null,
	l_comment varchar(44) not null);
-- End: Switch to Worker2, Create target shards in worker for remote 2-way split copy.
-- BEGIN: List row count for source shard and targets shard in Worker1.
\c - - - :worker_1_port
SELECT COUNT(*) FROM worker_split_binary_copy_test.shard_to_split_copy_81060000;
 count
---------------------------------------------------------------------
    22
(1 row)

SELECT COUNT(*) FROM worker_split_binary_copy_test.shard_to_split_copy_81060015;
 count
---------------------------------------------------------------------
     0
(1 row)

SELECT COUNT(*) FROM worker_split_binary_copy_test.shard_to_split_copy_81060016;
 count
---------------------------------------------------------------------
     0
(1 row)

-- END: List row count for source shard and targets shard in Worker1.
-- BEGIN: List row count for target shard in Worker2.
\c - - - :worker_2_port
SELECT COUNT(*) FROM worker_split_binary_copy_test.shard_to_split_copy_81060015;
 count
---------------------------------------------------------------------
     0
(1 row)

SELECT COUNT(*) FROM worker_split_binary_copy_test.shard_to_split_copy_81060016;
 count
---------------------------------------------------------------------
     0
(1 row)

-- END: List row count for targets shard in Worker2.
-- BEGIN: Set worker_1_node and worker_2_node
\c - - - :worker_1_port
SELECT nodeid AS worker_1_node FROM pg_dist_node WHERE nodeport=:worker_1_port \gset
SELECT nodeid AS worker_2_node FROM pg_dist_node WHERE nodeport=:worker_2_port \gset
-- END: Set worker_1_node and worker_2_node
-- BEGIN: Trigger 2-way local shard split copy.
-- Ensure we will perform binary copy.
SET citus.enable_binary_protocol = true;
SELECT * from worker_split_copy(
    81060000, -- source shard id to copy
    'l_orderkey',
    ARRAY[
         -- split copy info for split children 1
        ROW(81060015, -- destination shard id
             -2147483648, -- split range begin
            1073741823, --split range end
            :worker_1_node)::pg_catalog.split_copy_info,
        -- split copy info for split children 2
        ROW(81060016,  --destination shard id
            1073741824, --split range begin
            2147483647, --split range end
            :worker_1_node)::pg_catalog.split_copy_info
        ]
    );
 worker_split_copy
---------------------------------------------------------------------

(1 row)

-- END: Trigger 2-way local shard split copy.
-- BEGIN: Trigger 2-way remote shard split copy.
SELECT * from worker_split_copy(
    81060000, -- source shard id to copy
    'l_orderkey',
    ARRAY[
         -- split copy info for split children 1
        ROW(81060015, -- destination shard id
             -2147483648, -- split range begin
            1073741823, --split range end
            :worker_2_node)::pg_catalog.split_copy_info,
        -- split copy info for split children 2
        ROW(81060016,  --destination shard id
            1073741824, --split range begin
            2147483647, --split range end
            :worker_2_node)::pg_catalog.split_copy_info
        ]
    );
 worker_split_copy
---------------------------------------------------------------------

(1 row)

-- END: Trigger 2-way remote shard split copy.
-- BEGIN: List updated row count for local targets shard.
SELECT COUNT(*) FROM worker_split_binary_copy_test.shard_to_split_copy_81060015;
 count
---------------------------------------------------------------------
    21
(1 row)

SELECT COUNT(*) FROM worker_split_binary_copy_test.shard_to_split_copy_81060016;
 count
---------------------------------------------------------------------
     1
(1 row)

-- END: List updated row count for local targets shard.
-- BEGIN: List updated row count for remote targets shard.
\c - - - :worker_2_port
SELECT COUNT(*) FROM worker_split_binary_copy_test.shard_to_split_copy_81060015;
 count
---------------------------------------------------------------------
    21
(1 row)

SELECT COUNT(*) FROM worker_split_binary_copy_test.shard_to_split_copy_81060016;
 count
---------------------------------------------------------------------
     1
(1 row)

-- END: List updated row count for remote targets shard.
-- BEGIN: CLEANUP.
\c - - - :master_port
SET client_min_messages TO WARNING;
DROP SCHEMA worker_split_binary_copy_test CASCADE;
-- END: CLEANUP.
