diff --git a/src/test/regress/expected/merge.out b/src/test/regress/expected/merge.out index 42ded746e..0fef8342f 100644 --- a/src/test/regress/expected/merge.out +++ b/src/test/regress/expected/merge.out @@ -1984,12 +1984,19 @@ DEBUG: Using column - index:0 from the source list to redistribute -- let's verify if we use some other column from source for value of distributed column in target. -- it should be inserted to correct shard of target. CREATE TABLE source_withdata (id integer, some_number integer); +CREATE TABLE target_table (id integer, name text); SELECT create_distributed_table('source_withdata', 'id'); create_distributed_table --------------------------------------------------------------------- (1 row) +SELECT create_distributed_table('target_table', 'id'); + create_distributed_table +--------------------------------------------------------------------- + +(1 row) + INSERT INTO source_withdata (id, some_number) VALUES (1, 3); -- we will use some_number column from source_withdata to insert into distributed column of target. -- value of some_number is 3 let's verify what shard it should go to. @@ -2000,49 +2007,116 @@ select worker_hash(3); (1 row) -- it should go to second shard of target as target has 4 shard and hash "-28094569" comes in range of second shard. -MERGE INTO target_pushdowntest t +MERGE INTO target_table t USING (SELECT id, some_number from source_withdata where id = 1) s on t.id = s.some_number WHEN NOT MATCHED THEN - INSERT (id) - VALUES (s.some_number); + INSERT (id, name) + VALUES (s.some_number, 'parag'); DEBUG: Sub-query is not pushable, try repartitioning DEBUG: MERGE command is only supported when all distributed tables are co-located and joined on their distribution columns DEBUG: Creating MERGE repartition plan DEBUG: Using column - index:1 from the source list to redistribute DEBUG: Collect source query results on coordinator DEBUG: Create a MERGE task list that needs to be routed -DEBUG: -DEBUG: -DEBUG: -DEBUG: +DEBUG: +DEBUG: +DEBUG: +DEBUG: DEBUG: Execute MERGE task list -- let's verify if data inserted to second shard of target. -EXPLAIN (analyze on, costs off, timing off, summary off) SELECT * FROM target_pushdowntest; - QUERY PLAN +EXPLAIN (analyze on, costs off, timing off, summary off) SELECT * FROM target_table; + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) (actual rows=1 loops=1) Task Count: 4 - Tuple data received from nodes: 4 bytes + Tuple data received from nodes: 9 bytes Tasks Shown: All -> Task Tuple data received from node: 0 bytes Node: host=localhost port=xxxxx dbname=regression - -> Seq Scan on target_pushdowntest_4000068 target_pushdowntest (actual rows=0 loops=1) + -> Seq Scan on target_table_4000076 target_table (actual rows=0 loops=1) -> Task - Tuple data received from node: 4 bytes + Tuple data received from node: 9 bytes Node: host=localhost port=xxxxx dbname=regression - -> Seq Scan on target_pushdowntest_4000069 target_pushdowntest (actual rows=1 loops=1) + -> Seq Scan on target_table_4000077 target_table (actual rows=1 loops=1) -> Task Tuple data received from node: 0 bytes Node: host=localhost port=xxxxx dbname=regression - -> Seq Scan on target_pushdowntest_4000070 target_pushdowntest (actual rows=0 loops=1) + -> Seq Scan on target_table_4000078 target_table (actual rows=0 loops=1) -> Task Tuple data received from node: 0 bytes Node: host=localhost port=xxxxx dbname=regression - -> Seq Scan on target_pushdowntest_4000071 target_pushdowntest (actual rows=0 loops=1) + -> Seq Scan on target_table_4000079 target_table (actual rows=0 loops=1) (20 rows) +-- let's verify target data too. +SELECT * FROM target_table; + id | name +--------------------------------------------------------------------- + 3 | parag +(1 row) + +-- test UPDATE : when source is single sharded and table are colocated +MERGE INTO target_table t +USING (SELECT id, some_number from source_withdata where id = 1) s +on t.id = s.some_number +WHEN MATCHED THEN + UPDATE SET name = 'parag jain'; +DEBUG: Sub-query is not pushable, try repartitioning +DEBUG: MERGE command is only supported when all distributed tables are co-located and joined on their distribution columns +DEBUG: Creating MERGE repartition plan +DEBUG: Using column - index:1 from the source list to redistribute +DEBUG: Collect source query results on coordinator +DEBUG: Create a MERGE task list that needs to be routed +DEBUG: +DEBUG: +DEBUG: +DEBUG: +DEBUG: Execute MERGE task list +-- let's verify if data updated properly. +SELECT * FROM target_table; + id | name +--------------------------------------------------------------------- + 3 | parag jain +(1 row) + +-- let's see what happend when we try to update distributed key of target table +MERGE INTO target_table t +USING (SELECT id, some_number from source_withdata where id = 1) s +on t.id = s.some_number +WHEN MATCHED THEN + UPDATE SET id = 1500; +ERROR: updating the distribution column is not allowed in MERGE actions +SELECT * FROM target_table; + id | name +--------------------------------------------------------------------- + 3 | parag jain +(1 row) + +-- test DELETE : when source is single sharded and table are colocated +MERGE INTO target_table t +USING (SELECT id, some_number from source_withdata where id = 1) s +on t.id = s.some_number +WHEN MATCHED THEN + DELETE; +DEBUG: Sub-query is not pushable, try repartitioning +DEBUG: MERGE command is only supported when all distributed tables are co-located and joined on their distribution columns +DEBUG: Creating MERGE repartition plan +DEBUG: Using column - index:1 from the source list to redistribute +DEBUG: Collect source query results on coordinator +DEBUG: Create a MERGE task list that needs to be routed +DEBUG: +DEBUG: +DEBUG: +DEBUG: +DEBUG: Execute MERGE task list +-- let's verify if data deleted properly. +SELECT * FROM target_table; + id | name +--------------------------------------------------------------------- +(0 rows) + RESET client_min_messages; -- This will prune shards with restriction information as NOT MATCHED is void BEGIN; @@ -3100,14 +3174,14 @@ WHEN NOT MATCHED THEN -> Limit -> Sort Sort Key: id2 - -> Seq Scan on demo_source_table_4000147 demo_source_table + -> Seq Scan on demo_source_table_4000151 demo_source_table -> Distributed Subplan XXX_2 -> Custom Scan (Citus Adaptive) Task Count: 4 Tasks Shown: One of 4 -> Task Node: host=localhost port=xxxxx dbname=regression - -> Seq Scan on demo_source_table_4000147 demo_source_table + -> Seq Scan on demo_source_table_4000151 demo_source_table Task Count: 1 Tasks Shown: All -> Task @@ -3321,10 +3395,10 @@ DEBUG: Creating MERGE repartition plan DEBUG: Using column - index:0 from the source list to redistribute DEBUG: Collect source query results on coordinator DEBUG: Create a MERGE task list that needs to be routed -DEBUG: -DEBUG: -DEBUG: -DEBUG: +DEBUG: +DEBUG: +DEBUG: +DEBUG: DEBUG: Execute MERGE task list RESET client_min_messages; SELECT * FROM target_6785 ORDER BY 1; @@ -4168,7 +4242,7 @@ CONTEXT: SQL statement "SELECT citus_drop_all_shards(v_obj.objid, v_obj.schema_ PL/pgSQL function citus_drop_trigger() line XX at PERFORM DROP FUNCTION merge_when_and_write(); DROP SCHEMA merge_schema CASCADE; -NOTICE: drop cascades to 106 other objects +NOTICE: drop cascades to 107 other objects DETAIL: drop cascades to function insert_data() drop cascades to table local_local drop cascades to table target @@ -4231,6 +4305,7 @@ drop cascades to function compare_tables() drop cascades to table source_pushdowntest drop cascades to table target_pushdowntest drop cascades to table source_withdata +drop cascades to table target_table drop cascades to view pg_source_view drop cascades to view citus_source_view drop cascades to table pg_pa_target @@ -4247,7 +4322,7 @@ drop cascades to table target_set drop cascades to table source_set drop cascades to table refsource_ref drop cascades to table pg_result -drop cascades to table refsource_ref_4000124 +drop cascades to table refsource_ref_4000128 drop cascades to table pg_ref drop cascades to table local_ref drop cascades to table reftarget_local @@ -4265,8 +4340,7 @@ drop cascades to table source_6785 drop cascades to table target_6785 drop cascades to function add_s(integer,integer) drop cascades to table pg -drop cascades to table t1_4000186 -drop cascades to table s1_4000187 +drop cascades to table t1_4000190 +drop cascades to table s1_4000191 drop cascades to table t1 -drop cascades to table s1 -and 6 other objects (see server log for list) +and 7 other objects (see server log for list) diff --git a/src/test/regress/sql/merge.sql b/src/test/regress/sql/merge.sql index 61bb06cea..c2206cf52 100644 --- a/src/test/regress/sql/merge.sql +++ b/src/test/regress/sql/merge.sql @@ -1253,7 +1253,9 @@ WHEN NOT MATCHED THEN -- let's verify if we use some other column from source for value of distributed column in target. -- it should be inserted to correct shard of target. CREATE TABLE source_withdata (id integer, some_number integer); +CREATE TABLE target_table (id integer, name text); SELECT create_distributed_table('source_withdata', 'id'); +SELECT create_distributed_table('target_table', 'id'); INSERT INTO source_withdata (id, some_number) VALUES (1, 3); @@ -1262,15 +1264,48 @@ INSERT INTO source_withdata (id, some_number) VALUES (1, 3); select worker_hash(3); -- it should go to second shard of target as target has 4 shard and hash "-28094569" comes in range of second shard. -MERGE INTO target_pushdowntest t +MERGE INTO target_table t USING (SELECT id, some_number from source_withdata where id = 1) s on t.id = s.some_number WHEN NOT MATCHED THEN - INSERT (id) - VALUES (s.some_number); + INSERT (id, name) + VALUES (s.some_number, 'parag'); -- let's verify if data inserted to second shard of target. -EXPLAIN (analyze on, costs off, timing off, summary off) SELECT * FROM target_pushdowntest; +EXPLAIN (analyze on, costs off, timing off, summary off) SELECT * FROM target_table; + +-- let's verify target data too. +SELECT * FROM target_table; + + +-- test UPDATE : when source is single sharded and table are colocated +MERGE INTO target_table t +USING (SELECT id, some_number from source_withdata where id = 1) s +on t.id = s.some_number +WHEN MATCHED THEN + UPDATE SET name = 'parag jain'; + +-- let's verify if data updated properly. +SELECT * FROM target_table; + +-- let's see what happend when we try to update distributed key of target table +MERGE INTO target_table t +USING (SELECT id, some_number from source_withdata where id = 1) s +on t.id = s.some_number +WHEN MATCHED THEN + UPDATE SET id = 1500; + +SELECT * FROM target_table; + +-- test DELETE : when source is single sharded and table are colocated +MERGE INTO target_table t +USING (SELECT id, some_number from source_withdata where id = 1) s +on t.id = s.some_number +WHEN MATCHED THEN + DELETE; + +-- let's verify if data deleted properly. +SELECT * FROM target_table; RESET client_min_messages;