--
-- MULTI_APPEND_TABLE_TO_SHARD
--


SET citus.next_shard_id TO 230000;


-- Initialize tables to join
CREATE TABLE multi_append_table_to_shard_right_reference
(
	right_number INTEGER not null,
	right_text TEXT not null
);
SELECT create_reference_table('multi_append_table_to_shard_right_reference');

CREATE TABLE multi_append_table_to_shard_left
(
	left_number INTEGER not null,
	left_text TEXT not null
);
SELECT create_distributed_table('multi_append_table_to_shard_left', 'left_number', 'append');

CREATE TABLE multi_append_table_to_shard_right_reference_hash
(
	right_number INTEGER not null,
	right_text TEXT not null
);
SET citus.shard_count TO 1;
SET citus.shard_replication_factor TO 1;
SELECT create_distributed_table('multi_append_table_to_shard_right_reference_hash', 'right_number', 'hash');

-- Replicate 'left' table on both workers
SELECT set_config('citus.shard_replication_factor', '2', false);
\copy multi_append_table_to_shard_left FROM '@abs_srcdir@/data/agg.data'
\copy multi_append_table_to_shard_left FROM '@abs_srcdir@/data/agg.data'

-- Place 'right' table on both workers
\copy multi_append_table_to_shard_right_reference FROM '@abs_srcdir@/data/agg.data'

-- Reset shard replication factor to ensure tasks will be assigned to both workers
SELECT set_config('citus.shard_replication_factor', '2', false);

-- All 8 rows in left table match a row in right table
SELECT	COUNT(*)
FROM	multi_append_table_to_shard_left,
		multi_append_table_to_shard_right_reference
WHERE	left_number = right_number;

-- Now append more data to the 'right' table
CREATE TABLE multi_append_table_to_shard_stage
(
	number INTEGER not null,
	text TEXT not null
);

COPY multi_append_table_to_shard_stage FROM '@abs_srcdir@/data/large_records.data' with delimiter '|';

-- Check that we error out if we try to append data to a hash partitioned table.
SELECT master_create_empty_shard('multi_append_table_to_shard_right_reference_hash');

SELECT	master_append_table_to_shard(shardid, 'multi_append_table_to_shard_stage', 'localhost', 57636)
FROM
		pg_dist_shard
WHERE	'multi_append_table_to_shard_right_reference_hash'::regclass::oid = logicalrelid;

-- Clean up after test
SELECT master_apply_delete_command('DELETE FROM multi_append_table_to_shard_left');
DROP TABLE multi_append_table_to_shard_stage;
DROP TABLE multi_append_table_to_shard_right_reference;
DROP TABLE multi_append_table_to_shard_left;

-- Check partitioning by date
CREATE TABLE multi_append_table_to_shard_date
(
	event_date DATE,
	value INT
);
SELECT master_create_distributed_table('multi_append_table_to_shard_date', 'event_date', 'append');

-- Create an empty shard and check that we can query the table
SELECT master_create_empty_shard('multi_append_table_to_shard_date');

SELECT * FROM multi_append_table_to_shard_date;

-- Create an empty distributed table and check that we can query it
CREATE TABLE multi_append_table_to_shard_stage (LIKE multi_append_table_to_shard_date);
SELECT	master_append_table_to_shard(shardid, 'multi_append_table_to_shard_stage', 'localhost', 57636)
FROM
		pg_dist_shard
WHERE	'multi_append_table_to_shard_date'::regclass::oid = logicalrelid;

SELECT * FROM multi_append_table_to_shard_date;

-- INSERT NULL values and check that we can query the table
INSERT INTO multi_append_table_to_shard_stage VALUES (NULL, NULL);
SELECT	master_append_table_to_shard(shardid, 'multi_append_table_to_shard_stage', 'localhost', 57636)
FROM
		pg_dist_shard
WHERE	'multi_append_table_to_shard_date'::regclass::oid = logicalrelid;

SELECT * FROM multi_append_table_to_shard_date;

-- INSERT regular values and check that we can query the table
INSERT INTO multi_append_table_to_shard_stage VALUES ('2016-01-01', 3);
SELECT	master_append_table_to_shard(shardid, 'multi_append_table_to_shard_stage', 'localhost', 57636)
FROM
		pg_dist_shard
WHERE	'multi_append_table_to_shard_date'::regclass::oid = logicalrelid;

SELECT * FROM multi_append_table_to_shard_date;

-- When run inside aborted transaction does not persist changes
INSERT INTO multi_append_table_to_shard_stage VALUES ('2016-02-02', 4);
BEGIN;
SELECT	master_append_table_to_shard(shardid, 'multi_append_table_to_shard_stage', 'localhost', 57636)
FROM
		pg_dist_shard
WHERE	'multi_append_table_to_shard_date'::regclass::oid = logicalrelid;
ROLLBACK;

SELECT * FROM multi_append_table_to_shard_date;

DROP TABLE multi_append_table_to_shard_stage;
DROP TABLE multi_append_table_to_shard_date;
