--
-- WORKER_RANGE_PARTITION
--
\set JobId 201010
\set TaskId 101101
\set Partition_Column l_orderkey
\set Partition_Column_Text '\'l_orderkey\''
\set Partition_Column_Type 20
\set Select_Query_Text '\'SELECT * FROM lineitem\''
\set Select_All 'SELECT *'
\set Table_Part_00 lineitem_range_part_00
\set Table_Part_01 lineitem_range_part_01
\set Table_Part_02 lineitem_range_part_02
\set Table_Part_03 lineitem_range_part_03
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
\set Table_File_03 :File_Basedir/job_:JobId/task_:TaskId/p_00003.:userid
-- 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[1, 3000, 12000]::_int8);
 worker_range_partition_table
---------------------------------------------------------------------

(1 row)

COPY :Table_Part_00 FROM :'Table_File_00';
COPY :Table_Part_01 FROM :'Table_File_01';
COPY :Table_Part_02 FROM :'Table_File_02';
COPY :Table_Part_03 FROM :'Table_File_03';
SELECT COUNT(*) FROM :Table_Part_00;
 count
---------------------------------------------------------------------
     0
(1 row)

SELECT COUNT(*) FROM :Table_Part_03;
 count
---------------------------------------------------------------------
  3047
(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 lineitem WHERE :Partition_Column < 1 ) 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 lineitem WHERE :Partition_Column >= 1 AND
       		   		       :Partition_Column < 3000 ) 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 lineitem WHERE :Partition_Column >= 3000 AND
       		   		       :Partition_Column < 12000 ) diff;
 diff_lhs_02
---------------------------------------------------------------------
           0
(1 row)

SELECT COUNT(*) AS diff_lhs_03 FROM (
       :Select_All FROM :Table_Part_03 EXCEPT ALL
       :Select_All FROM lineitem WHERE :Partition_Column >= 12000 ) diff;
 diff_lhs_03
---------------------------------------------------------------------
           0
(1 row)

SELECT COUNT(*) AS diff_rhs_00 FROM (
       :Select_All FROM lineitem WHERE :Partition_Column < 1 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 lineitem WHERE :Partition_Column >= 1 AND
       		   		       :Partition_Column < 3000 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 lineitem WHERE :Partition_Column >= 3000 AND
       		   		       :Partition_Column < 12000 EXCEPT ALL
       :Select_All FROM :Table_Part_02 ) diff;
 diff_rhs_02
---------------------------------------------------------------------
           0
(1 row)

SELECT COUNT(*) AS diff_rhs_03 FROM (
       :Select_All FROM lineitem WHERE :Partition_Column >= 12000 EXCEPT ALL
       :Select_All FROM :Table_Part_03 ) diff;
 diff_rhs_03
---------------------------------------------------------------------
           0
(1 row)

