--
-- WORKER_BINARY_DATA_PARTITION
--
\set JobId 201010
\set TaskId 101105
\set Partition_Column textcolumn
\set Partition_Column_Text '\'textcolumn\''
\set Partition_Column_Type 25
\set Select_Query_Text '\'SELECT * FROM binary_data_table\''
\set Select_All 'SELECT *'
\set Table_Name binary_data_table
\set Table_Part_00 binary_data_table_part_00
\set Table_Part_01 binary_data_table_part_01
\set Table_Part_02 binary_data_table_part_02
SELECT usesysid AS userid FROM pg_user WHERE usename = current_user \gset
\set File_Basedir  base/pgsql_job_cache
\set Table_File_00 :File_Basedir/job_:JobId/task_:TaskId/p_00000.:userid
\set Table_File_01 :File_Basedir/job_:JobId/task_:TaskId/p_00001.:userid
\set Table_File_02 :File_Basedir/job_:JobId/task_:TaskId/p_00002.:userid
-- Create table with special characters
CREATE TABLE :Table_Name(textcolumn text, binarycolumn bytea);
COPY :Table_Name FROM stdin;
SELECT length(binarycolumn) FROM :Table_Name;
 length
---------------------------------------------------------------------
      2
      4
      3
      2
      4
     14
     28
     16
      9
     11
     11
     24
     17
     12
(14 rows)

-- Run select query, and apply range partitioning on query results
SELECT worker_range_partition_table(:JobId, :TaskId, :Select_Query_Text,
       				    :Partition_Column_Text, :Partition_Column_Type,
				    ARRAY['aaa', 'some']::_text);
 worker_range_partition_table
---------------------------------------------------------------------

(1 row)

-- Copy range partitioned files into tables
CREATE TABLE :Table_Part_00 ( LIKE :Table_Name );
CREATE TABLE :Table_Part_01 ( LIKE :Table_Name );
CREATE TABLE :Table_Part_02 ( LIKE :Table_Name );
COPY :Table_Part_00 FROM :'Table_File_00';
COPY :Table_Part_01 FROM :'Table_File_01';
COPY :Table_Part_02 FROM :'Table_File_02';
-- The union of the three partitions should have as many rows as original table
SELECT COUNT(*) AS total_row_count FROM (
       SELECT * FROM :Table_Part_00 UNION ALL
       SELECT * FROM :Table_Part_01 UNION ALL
       SELECT * FROM :Table_Part_02 ) AS all_rows;
 total_row_count
---------------------------------------------------------------------
              14
(1 row)

-- We first compute the difference of partition tables against the base table.
-- Then, we compute the difference of the base table against partitioned tables.
SELECT COUNT(*) AS diff_lhs_00 FROM (
       :Select_All FROM :Table_Part_00 EXCEPT ALL
       :Select_All FROM :Table_Name WHERE :Partition_Column IS NULL OR
       		   		    	  :Partition_Column < 'aaa' ) diff;
 diff_lhs_00
---------------------------------------------------------------------
           0
(1 row)

SELECT COUNT(*) AS diff_lhs_01 FROM (
       :Select_All FROM :Table_Part_01 EXCEPT ALL
       :Select_All FROM :Table_Name WHERE :Partition_Column >= 'aaa' AND
       		   		       	  :Partition_Column < 'some' ) diff;
 diff_lhs_01
---------------------------------------------------------------------
           0
(1 row)

SELECT COUNT(*) AS diff_lhs_02 FROM (
       :Select_All FROM :Table_Part_02 EXCEPT ALL
       :Select_All FROM :Table_Name WHERE :Partition_Column >= 'some' ) diff;
 diff_lhs_02
---------------------------------------------------------------------
           0
(1 row)

SELECT COUNT(*) AS diff_rhs_00 FROM (
       :Select_All FROM :Table_Name WHERE :Partition_Column IS NULL OR
       		   		    	  :Partition_Column < 'aaa' EXCEPT ALL
       :Select_All FROM :Table_Part_00 ) diff;
 diff_rhs_00
---------------------------------------------------------------------
           0
(1 row)

SELECT COUNT(*) AS diff_rhs_01 FROM (
       :Select_All FROM :Table_Name WHERE :Partition_Column >= 'aaa' AND
       		   		       	  :Partition_Column < 'some' EXCEPT ALL
       :Select_All FROM :Table_Part_01 ) diff;
 diff_rhs_01
---------------------------------------------------------------------
           0
(1 row)

SELECT COUNT(*) AS diff_rhs_02 FROM (
       :Select_All FROM :Table_Name WHERE :Partition_Column >= 'some' EXCEPT ALL
       :Select_All FROM :Table_Part_02 ) diff;
 diff_rhs_02
---------------------------------------------------------------------
           0
(1 row)

