mirror of https://github.com/citusdata/citus.git
Enhance test data for issue 7891 by adding sample entries and implementing DELETE and MERGE operations to validate reference table handling
parent
c779325c93
commit
5fcacfed6e
|
@ -41,7 +41,10 @@ SELECT create_reference_table('t2_ref');
|
||||||
(1 row)
|
(1 row)
|
||||||
|
|
||||||
-- Insert sample data
|
-- Insert sample data
|
||||||
INSERT INTO t6_pg (vkey, pkey, c26) VALUES (2, 12000, '');
|
INSERT INTO t6_pg (vkey, pkey, c26) VALUES
|
||||||
|
(2, 12000, 'initial'),
|
||||||
|
(3, 13000, 'will_be_deleted'),
|
||||||
|
(4, 14000, 'to_merge');
|
||||||
INSERT INTO t4_pg (vkey, pkey, c22, c23, c24)
|
INSERT INTO t4_pg (vkey, pkey, c22, c23, c24)
|
||||||
VALUES (5, 15000, 0.0, ']]?', MAKE_TIMESTAMP(2071, 10, 26, 16, 20, 5));
|
VALUES (5, 15000, 0.0, ']]?', MAKE_TIMESTAMP(2071, 10, 26, 16, 20, 5));
|
||||||
INSERT INTO t2_ref (vkey, pkey, c15)
|
INSERT INTO t2_ref (vkey, pkey, c15)
|
||||||
|
@ -50,8 +53,10 @@ INSERT INTO t2_ref (vkey, pkey, c15)
|
||||||
SELECT 't6_pg before' AS label, * FROM t6_pg;
|
SELECT 't6_pg before' AS label, * FROM t6_pg;
|
||||||
label | vkey | pkey | c26
|
label | vkey | pkey | c26
|
||||||
---------------------------------------------------------------------
|
---------------------------------------------------------------------
|
||||||
t6_pg before | 2 | 12000 |
|
t6_pg before | 2 | 12000 | initial
|
||||||
(1 row)
|
t6_pg before | 3 | 13000 | will_be_deleted
|
||||||
|
t6_pg before | 4 | 14000 | to_merge
|
||||||
|
(3 rows)
|
||||||
|
|
||||||
SELECT 't4_pg data' AS label, * FROM t4_pg;
|
SELECT 't4_pg data' AS label, * FROM t4_pg;
|
||||||
label | vkey | pkey | c22 | c23 | c24
|
label | vkey | pkey | c22 | c23 | c24
|
||||||
|
@ -77,11 +82,83 @@ UPDATE t6_pg
|
||||||
SELECT (SELECT c15 FROM t2_ref)
|
SELECT (SELECT c15 FROM t2_ref)
|
||||||
FROM t4_pg
|
FROM t4_pg
|
||||||
);
|
);
|
||||||
-- Show final data
|
|
||||||
SELECT 't6_pg after' AS label, * FROM t6_pg;
|
SELECT 't6_pg after' AS label, * FROM t6_pg;
|
||||||
label | vkey | pkey | c26
|
label | vkey | pkey | c26
|
||||||
---------------------------------------------------------------------
|
---------------------------------------------------------------------
|
||||||
t6_pg after | 43 | 12000 |
|
t6_pg after | 43 | 12000 | initial
|
||||||
|
t6_pg after | 43 | 13000 | will_be_deleted
|
||||||
|
t6_pg after | 43 | 14000 | to_merge
|
||||||
|
(3 rows)
|
||||||
|
|
||||||
|
--
|
||||||
|
-- DELETE with a similar nested subquery approach
|
||||||
|
-- Here, let's delete any rows for which t4_pg is non-empty (like a trivial check).
|
||||||
|
-- We'll specifically target the row with c26='will_be_deleted' to confirm it's removed.
|
||||||
|
--
|
||||||
|
DELETE FROM t6_pg
|
||||||
|
WHERE EXISTS (
|
||||||
|
SELECT (SELECT c15 FROM t2_ref)
|
||||||
|
FROM t4_pg
|
||||||
|
)
|
||||||
|
AND c26 = 'will_be_deleted';
|
||||||
|
SELECT 't6_pg after DELETE' AS label, * FROM t6_pg;
|
||||||
|
label | vkey | pkey | c26
|
||||||
|
---------------------------------------------------------------------
|
||||||
|
t6_pg after DELETE | 43 | 12000 | initial
|
||||||
|
t6_pg after DELETE | 43 | 14000 | to_merge
|
||||||
|
(2 rows)
|
||||||
|
|
||||||
|
--
|
||||||
|
-- We'll merge from t4_pg into t6_pg. The merge will update c26 for pkey=14000.
|
||||||
|
--
|
||||||
|
MERGE INTO t6_pg AS tgt
|
||||||
|
USING t4_pg AS src
|
||||||
|
ON (tgt.pkey = 14000) -- trivial condition to "match" row pkey=14000
|
||||||
|
WHEN MATCHED THEN
|
||||||
|
UPDATE SET c26 = 'merged_' || (SELECT pkey FROM t2_ref WHERE pkey=24000 LIMIT 1)
|
||||||
|
WHEN NOT MATCHED THEN
|
||||||
|
INSERT (vkey, pkey, c26)
|
||||||
|
VALUES (99, src.pkey, 'inserted_via_merge');
|
||||||
|
ERROR: non-IMMUTABLE functions are not yet supported in MERGE sql with distributed tables
|
||||||
|
MERGE INTO t6_pg AS tgt
|
||||||
|
USING t4_pg AS src
|
||||||
|
ON (tgt.pkey = src.pkey)
|
||||||
|
WHEN MATCHED THEN
|
||||||
|
UPDATE SET c26 = 'merged_value'
|
||||||
|
WHEN NOT MATCHED THEN
|
||||||
|
INSERT (vkey, pkey, c26)
|
||||||
|
VALUES (src.vkey, src.pkey, 'inserted_via_merge');
|
||||||
|
SELECT 't6_pg after MERGE' AS label, * FROM t6_pg;
|
||||||
|
label | vkey | pkey | c26
|
||||||
|
---------------------------------------------------------------------
|
||||||
|
t6_pg after MERGE | 43 | 12000 | initial
|
||||||
|
t6_pg after MERGE | 43 | 14000 | to_merge
|
||||||
|
t6_pg after MERGE | 5 | 15000 | inserted_via_merge
|
||||||
|
(3 rows)
|
||||||
|
|
||||||
|
--
|
||||||
|
-- Update the REFERENCE table itself and verify the change
|
||||||
|
-- This is to ensure that the reference table is correctly handled.
|
||||||
|
UPDATE t2_ref
|
||||||
|
SET c15 = '2099-01-01 00:00:00'::timestamp
|
||||||
|
WHERE pkey = 24000;
|
||||||
|
SELECT 't2_ref after self-update' AS label, * FROM t2_ref;
|
||||||
|
label | vkey | pkey | c15
|
||||||
|
---------------------------------------------------------------------
|
||||||
|
t2_ref after self-update | 14 | 24000 | Thu Jan 01 00:00:00 2099
|
||||||
|
(1 row)
|
||||||
|
|
||||||
|
UPDATE t2_ref
|
||||||
|
SET c15 = '2099-01-01 00:00:00'::timestamp
|
||||||
|
WHERE EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM t4_pg
|
||||||
|
);
|
||||||
|
ERROR: relation t4_pg is not distributed
|
||||||
|
SELECT 't2_ref after UPDATE' AS label, * FROM t2_ref;
|
||||||
|
label | vkey | pkey | c15
|
||||||
|
---------------------------------------------------------------------
|
||||||
|
t2_ref after UPDATE | 14 | 24000 | Thu Jan 01 00:00:00 2099
|
||||||
(1 row)
|
(1 row)
|
||||||
|
|
||||||
-- Cleanup
|
-- Cleanup
|
||||||
|
|
|
@ -41,7 +41,10 @@ CREATE TABLE t6_pg (
|
||||||
SELECT create_reference_table('t2_ref');
|
SELECT create_reference_table('t2_ref');
|
||||||
|
|
||||||
-- Insert sample data
|
-- Insert sample data
|
||||||
INSERT INTO t6_pg (vkey, pkey, c26) VALUES (2, 12000, '');
|
INSERT INTO t6_pg (vkey, pkey, c26) VALUES
|
||||||
|
(2, 12000, 'initial'),
|
||||||
|
(3, 13000, 'will_be_deleted'),
|
||||||
|
(4, 14000, 'to_merge');
|
||||||
INSERT INTO t4_pg (vkey, pkey, c22, c23, c24)
|
INSERT INTO t4_pg (vkey, pkey, c22, c23, c24)
|
||||||
VALUES (5, 15000, 0.0, ']]?', MAKE_TIMESTAMP(2071, 10, 26, 16, 20, 5));
|
VALUES (5, 15000, 0.0, ']]?', MAKE_TIMESTAMP(2071, 10, 26, 16, 20, 5));
|
||||||
INSERT INTO t2_ref (vkey, pkey, c15)
|
INSERT INTO t2_ref (vkey, pkey, c15)
|
||||||
|
@ -66,9 +69,67 @@ UPDATE t6_pg
|
||||||
FROM t4_pg
|
FROM t4_pg
|
||||||
);
|
);
|
||||||
|
|
||||||
-- Show final data
|
|
||||||
SELECT 't6_pg after' AS label, * FROM t6_pg;
|
SELECT 't6_pg after' AS label, * FROM t6_pg;
|
||||||
|
|
||||||
|
--
|
||||||
|
-- DELETE with a similar nested subquery approach
|
||||||
|
-- Here, let's delete any rows for which t4_pg is non-empty (like a trivial check).
|
||||||
|
-- We'll specifically target the row with c26='will_be_deleted' to confirm it's removed.
|
||||||
|
--
|
||||||
|
DELETE FROM t6_pg
|
||||||
|
WHERE EXISTS (
|
||||||
|
SELECT (SELECT c15 FROM t2_ref)
|
||||||
|
FROM t4_pg
|
||||||
|
)
|
||||||
|
AND c26 = 'will_be_deleted';
|
||||||
|
|
||||||
|
SELECT 't6_pg after DELETE' AS label, * FROM t6_pg;
|
||||||
|
|
||||||
|
--
|
||||||
|
-- We'll merge from t4_pg into t6_pg. The merge will update c26 for pkey=14000.
|
||||||
|
--
|
||||||
|
MERGE INTO t6_pg AS tgt
|
||||||
|
USING t4_pg AS src
|
||||||
|
ON (tgt.pkey = 14000)
|
||||||
|
WHEN MATCHED THEN
|
||||||
|
UPDATE SET c26 = 'merged_' || (SELECT pkey FROM t2_ref WHERE pkey=24000 LIMIT 1)
|
||||||
|
WHEN NOT MATCHED THEN
|
||||||
|
INSERT (vkey, pkey, c26)
|
||||||
|
VALUES (99, src.pkey, 'inserted_via_merge');
|
||||||
|
|
||||||
|
MERGE INTO t6_pg AS tgt
|
||||||
|
USING t4_pg AS src
|
||||||
|
ON (tgt.pkey = src.pkey)
|
||||||
|
WHEN MATCHED THEN
|
||||||
|
UPDATE SET c26 = 'merged_value'
|
||||||
|
WHEN NOT MATCHED THEN
|
||||||
|
INSERT (vkey, pkey, c26)
|
||||||
|
VALUES (src.vkey, src.pkey, 'inserted_via_merge');
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
SELECT 't6_pg after MERGE' AS label, * FROM t6_pg;
|
||||||
|
|
||||||
|
--
|
||||||
|
-- Update the REFERENCE table itself and verify the change
|
||||||
|
-- This is to ensure that the reference table is correctly handled.
|
||||||
|
|
||||||
|
UPDATE t2_ref
|
||||||
|
SET c15 = '2099-01-01 00:00:00'::timestamp
|
||||||
|
WHERE pkey = 24000;
|
||||||
|
|
||||||
|
SELECT 't2_ref after self-update' AS label, * FROM t2_ref;
|
||||||
|
|
||||||
|
|
||||||
|
UPDATE t2_ref
|
||||||
|
SET c15 = '2099-01-01 00:00:00'::timestamp
|
||||||
|
WHERE EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM t4_pg
|
||||||
|
);
|
||||||
|
|
||||||
|
SELECT 't2_ref after UPDATE' AS label, * FROM t2_ref;
|
||||||
|
|
||||||
-- Cleanup
|
-- Cleanup
|
||||||
SET client_min_messages TO WARNING;
|
SET client_min_messages TO WARNING;
|
||||||
DROP SCHEMA issue_7891 CASCADE;
|
DROP SCHEMA issue_7891 CASCADE;
|
||||||
|
|
Loading…
Reference in New Issue