From 4549e0688401168832b333239705309efa69e6a8 Mon Sep 17 00:00:00 2001 From: Andres Freund Date: Wed, 22 Jun 2016 14:36:36 -0700 Subject: [PATCH] Add regression tests for RETURNING. --- .../regress/expected/multi_modifications.out | 134 +++++++++++++++++- src/test/regress/expected/multi_upsert.out | 17 +++ src/test/regress/expected/multi_upsert_0.out | 13 ++ src/test/regress/sql/multi_modifications.sql | 46 +++++- src/test/regress/sql/multi_upsert.sql | 9 ++ 5 files changed, 211 insertions(+), 8 deletions(-) diff --git a/src/test/regress/expected/multi_modifications.out b/src/test/regress/expected/multi_modifications.out index c61b225de..f3087ed9b 100644 --- a/src/test/regress/expected/multi_modifications.out +++ b/src/test/regress/expected/multi_modifications.out @@ -97,6 +97,13 @@ SELECT COUNT(*) FROM limit_orders WHERE id = 32743; 1 (1 row) +-- basic single-row INSERT with RETURNING +INSERT INTO limit_orders VALUES (32744, 'AAPL', 9580, '2004-10-19 10:23:54', 'buy', 20.69) RETURNING *; + id | symbol | bidder_id | placed_at | kind | limit_price +-------+--------+-----------+--------------------------+------+------------- + 32744 | AAPL | 9580 | Tue Oct 19 10:23:54 2004 | buy | 20.69 +(1 row) + -- try a single-row INSERT with no shard to receive it INSERT INTO insufficient_shards VALUES (32743, 'AAPL', 9580, '2004-10-19 10:23:54', 'buy', 20.69); @@ -173,6 +180,14 @@ INSERT INTO limit_orders VALUES (32743, 'LUV', 5994, '2001-04-16 03:37:28', 'buy ERROR: duplicate key value violates unique constraint "limit_orders_pkey_750001" DETAIL: Key (id)=(32743) already exists. CONTEXT: while executing command on localhost:57638 +-- INSERT violating primary key constraint, with RETURNING specified. +INSERT INTO limit_orders VALUES (32743, 'LUV', 5994, '2001-04-16 03:37:28', 'buy', 0.58) RETURNING *; +ERROR: duplicate key value violates unique constraint "limit_orders_pkey_750001" +DETAIL: Key (id)=(32743) already exists. +CONTEXT: while executing command on localhost:57638 +-- INSERT, with RETURNING specified, failing with a non-constraint error +INSERT INTO limit_orders VALUES (34153, 'LEE', 5994, '2001-04-16 03:37:28', 'buy', 0.58) RETURNING id / 0; +ERROR: could not modify any active placements SET client_min_messages TO DEFAULT; -- commands with non-constant partition values are unsupported INSERT INTO limit_orders VALUES (random() * 100, 'ORCL', 152, '2011-08-25 11:50:45', @@ -215,6 +230,19 @@ SELECT COUNT(*) FROM limit_orders WHERE id = 246; 0 (1 row) +-- test simple DELETE with RETURNING +DELETE FROM limit_orders WHERE id = 430 RETURNING *; + id | symbol | bidder_id | placed_at | kind | limit_price +-----+--------+-----------+--------------------------+------+----------------- + 430 | IBM | 214 | Tue Jan 28 15:31:17 2003 | buy | 1.4142135623731 +(1 row) + +SELECT COUNT(*) FROM limit_orders WHERE id = 430; + count +------- + 0 +(1 row) + -- DELETE with expression in WHERE clause INSERT INTO limit_orders VALUES (246, 'TSLA', 162, '2007-07-02 16:32:15', 'sell', 20.69); SELECT COUNT(*) FROM limit_orders WHERE id = 246; @@ -256,6 +284,13 @@ SELECT symbol FROM limit_orders WHERE id = 246; GM (1 row) +-- simple UPDATE with RETURNING +UPDATE limit_orders SET symbol = 'GM' WHERE id = 246 RETURNING *; + id | symbol | bidder_id | placed_at | kind | limit_price +-----+--------+-----------+--------------------------+------+------------- + 246 | GM | 162 | Mon Jul 02 16:32:15 2007 | sell | 20.69 +(1 row) + -- expression UPDATE UPDATE limit_orders SET bidder_id = 6 * 3 WHERE id = 246; SELECT bidder_id FROM limit_orders WHERE id = 246; @@ -264,6 +299,13 @@ SELECT bidder_id FROM limit_orders WHERE id = 246; 18 (1 row) +-- expression UPDATE with RETURNING +UPDATE limit_orders SET bidder_id = 6 * 5 WHERE id = 246 RETURNING *; + id | symbol | bidder_id | placed_at | kind | limit_price +-----+--------+-----------+--------------------------+------+------------- + 246 | GM | 30 | Mon Jul 02 16:32:15 2007 | sell | 20.69 +(1 row) + -- multi-column UPDATE UPDATE limit_orders SET (kind, limit_price) = ('buy', DEFAULT) WHERE id = 246; SELECT kind, limit_price FROM limit_orders WHERE id = 246; @@ -272,6 +314,13 @@ SELECT kind, limit_price FROM limit_orders WHERE id = 246; buy | 0.00 (1 row) +-- multi-column UPDATE with RETURNING +UPDATE limit_orders SET (kind, limit_price) = ('buy', 999) WHERE id = 246 RETURNING *; + id | symbol | bidder_id | placed_at | kind | limit_price +-----+--------+-----------+--------------------------+------+------------- + 246 | GM | 30 | Mon Jul 02 16:32:15 2007 | buy | 999 +(1 row) + -- Test that on unique contraint violations, we fail fast INSERT INTO limit_orders VALUES (275, 'ADR', 140, '2007-07-02 16:32:15', 'sell', 43.67); INSERT INTO limit_orders VALUES (275, 'ADR', 140, '2007-07-02 16:32:15', 'sell', 43.67); @@ -362,7 +411,7 @@ DETAIL: Common table expressions are not supported in distributed modifications SELECT symbol, bidder_id FROM limit_orders WHERE id = 246; symbol | bidder_id --------+----------- - GM | 18 + GM | 30 (1 row) -- updates referencing just a var are supported @@ -377,12 +426,51 @@ SELECT symbol, bidder_id FROM limit_orders WHERE id = 246; gm | 247 (1 row) +-- IMMUTABLE functions are allowed -- even in returning +UPDATE limit_orders SET symbol = UPPER(symbol) WHERE id = 246 RETURNING id, LOWER(symbol), symbol; + id | lower | symbol +-----+-------+-------- + 246 | gm | GM +(1 row) + -- updates referencing non-IMMUTABLE functions are unsupported UPDATE limit_orders SET placed_at = now() WHERE id = 246; ERROR: functions used in modification queries on distributed tables must be marked IMMUTABLE +-- even in RETURNING +UPDATE limit_orders SET placed_at = placed_at WHERE id = 246 RETURNING NOW(); +ERROR: functions used in modification queries on distributed tables must be marked IMMUTABLE -- cursors are not supported UPDATE limit_orders SET symbol = 'GM' WHERE CURRENT OF cursor_name; ERROR: distributed modifications must target exactly one shard +-- check that multi-row UPDATE/DELETEs with RETURNING work +INSERT INTO multiple_hash VALUES ('0', '1'); +INSERT INTO multiple_hash VALUES ('0', '2'); +INSERT INTO multiple_hash VALUES ('0', '3'); +INSERT INTO multiple_hash VALUES ('0', '4'); +INSERT INTO multiple_hash VALUES ('0', '5'); +INSERT INTO multiple_hash VALUES ('0', '6'); +UPDATE multiple_hash SET data = data ||'-1' WHERE category = '0' RETURNING *; + category | data +----------+------ + 0 | 1-1 + 0 | 2-1 + 0 | 3-1 + 0 | 4-1 + 0 | 5-1 + 0 | 6-1 +(6 rows) + +DELETE FROM multiple_hash WHERE category = '0' RETURNING *; + category | data +----------+------ + 0 | 1-1 + 0 | 2-1 + 0 | 3-1 + 0 | 4-1 + 0 | 5-1 + 0 | 6-1 +(6 rows) + -- ensure returned row counters are correct \set QUIET off INSERT INTO multiple_hash VALUES ('1', '1'); @@ -396,6 +484,13 @@ INSERT 0 1 INSERT INTO multiple_hash VALUES ('2', '2'); INSERT 0 1 INSERT INTO multiple_hash VALUES ('2', '3'); +INSERT 0 1 +INSERT INTO multiple_hash VALUES ('2', '3') RETURNING *; + category | data +----------+------ + 2 | 3 +(1 row) + INSERT 0 1 -- check that update return the right number of rows -- one row @@ -403,14 +498,24 @@ UPDATE multiple_hash SET data = data ||'-1' WHERE category = '1' AND data = '1'; UPDATE 1 -- three rows UPDATE multiple_hash SET data = data ||'-2' WHERE category = '1'; +UPDATE 3 +-- three rows, with RETURNING +UPDATE multiple_hash SET data = data ||'-2' WHERE category = '1' RETURNING category; + category +---------- + 1 + 1 + 1 +(3 rows) + UPDATE 3 -- check SELECT * FROM multiple_hash WHERE category = '1' ORDER BY category, data; - category | data -----------+------- - 1 | 1-1-2 - 1 | 2-2 - 1 | 3-2 + category | data +----------+--------- + 1 | 1-1-2-2 + 1 | 2-2-2 + 1 | 3-2-2 (3 rows) -- check that deletes return the right number of rows @@ -419,8 +524,23 @@ DELETE FROM multiple_hash WHERE category = '2' AND data = '1'; DELETE 1 -- two rows DELETE FROM multiple_hash WHERE category = '2'; -DELETE 2 +DELETE 3 +-- three rows, with RETURNING +DELETE FROM multiple_hash WHERE category = '1' RETURNING category; + category +---------- + 1 + 1 + 1 +(3 rows) + +DELETE 3 -- check +SELECT * FROM multiple_hash WHERE category = '1' ORDER BY category, data; + category | data +----------+------ +(0 rows) + SELECT * FROM multiple_hash WHERE category = '2' ORDER BY category, data; category | data ----------+------ diff --git a/src/test/regress/expected/multi_upsert.out b/src/test/regress/expected/multi_upsert.out index ec9597171..3b3ff22c1 100644 --- a/src/test/regress/expected/multi_upsert.out +++ b/src/test/regress/expected/multi_upsert.out @@ -105,6 +105,23 @@ SELECT * FROM upsert_test; 1 | 5 | 872 (1 row) +-- Test upsert, with returning: +INSERT INTO upsert_test (part_key, other_col) VALUES (2, 2) + ON CONFLICT (part_key) DO UPDATE SET other_col = 3 + RETURNING *; + part_key | other_col | third_col +----------+-----------+----------- + 2 | 2 | +(1 row) + +INSERT INTO upsert_test (part_key, other_col) VALUES (2, 2) + ON CONFLICT (part_key) DO UPDATE SET other_col = 3 + RETURNING *; + part_key | other_col | third_col +----------+-----------+----------- + 2 | 3 | +(1 row) + -- create another table CREATE TABLE upsert_test_2 ( diff --git a/src/test/regress/expected/multi_upsert_0.out b/src/test/regress/expected/multi_upsert_0.out index 09b052c1c..eb653cf3d 100644 --- a/src/test/regress/expected/multi_upsert_0.out +++ b/src/test/regress/expected/multi_upsert_0.out @@ -138,6 +138,19 @@ SELECT * FROM upsert_test; 1 | 1 | (1 row) +-- Test upsert, with returning: +INSERT INTO upsert_test (part_key, other_col) VALUES (2, 2) + ON CONFLICT (part_key) DO UPDATE SET other_col = 3 + RETURNING *; +ERROR: syntax error at or near "ON" +LINE 2: ON CONFLICT (part_key) DO UPDATE SET other_col = 3 + ^ +INSERT INTO upsert_test (part_key, other_col) VALUES (2, 2) + ON CONFLICT (part_key) DO UPDATE SET other_col = 3 + RETURNING *; +ERROR: syntax error at or near "ON" +LINE 2: ON CONFLICT (part_key) DO UPDATE SET other_col = 3 + ^ -- create another table CREATE TABLE upsert_test_2 ( diff --git a/src/test/regress/sql/multi_modifications.sql b/src/test/regress/sql/multi_modifications.sql index 935a9228a..0ff747cb7 100644 --- a/src/test/regress/sql/multi_modifications.sql +++ b/src/test/regress/sql/multi_modifications.sql @@ -69,6 +69,9 @@ INSERT INTO limit_orders VALUES (32743, 'AAPL', 9580, '2004-10-19 10:23:54', 'bu 20.69); SELECT COUNT(*) FROM limit_orders WHERE id = 32743; +-- basic single-row INSERT with RETURNING +INSERT INTO limit_orders VALUES (32744, 'AAPL', 9580, '2004-10-19 10:23:54', 'buy', 20.69) RETURNING *; + -- try a single-row INSERT with no shard to receive it INSERT INTO insufficient_shards VALUES (32743, 'AAPL', 9580, '2004-10-19 10:23:54', 'buy', 20.69); @@ -118,10 +121,16 @@ INSERT INTO limit_orders VALUES (NULL, 'T', 975234, DEFAULT); -- INSERT violating column constraint INSERT INTO limit_orders VALUES (18811, 'BUD', 14962, '2014-04-05 08:32:16', 'sell', -5.00); - -- INSERT violating primary key constraint INSERT INTO limit_orders VALUES (32743, 'LUV', 5994, '2001-04-16 03:37:28', 'buy', 0.58); +-- INSERT violating primary key constraint, with RETURNING specified. +INSERT INTO limit_orders VALUES (32743, 'LUV', 5994, '2001-04-16 03:37:28', 'buy', 0.58) RETURNING *; + +-- INSERT, with RETURNING specified, failing with a non-constraint error +INSERT INTO limit_orders VALUES (34153, 'LEE', 5994, '2001-04-16 03:37:28', 'buy', 0.58) RETURNING id / 0; + + SET client_min_messages TO DEFAULT; -- commands with non-constant partition values are unsupported @@ -154,6 +163,10 @@ SELECT COUNT(*) FROM limit_orders WHERE id = 246; DELETE FROM limit_orders WHERE id = 246; SELECT COUNT(*) FROM limit_orders WHERE id = 246; +-- test simple DELETE with RETURNING +DELETE FROM limit_orders WHERE id = 430 RETURNING *; +SELECT COUNT(*) FROM limit_orders WHERE id = 430; + -- DELETE with expression in WHERE clause INSERT INTO limit_orders VALUES (246, 'TSLA', 162, '2007-07-02 16:32:15', 'sell', 20.69); SELECT COUNT(*) FROM limit_orders WHERE id = 246; @@ -183,14 +196,23 @@ INSERT INTO limit_orders VALUES (246, 'TSLA', 162, '2007-07-02 16:32:15', 'sell' UPDATE limit_orders SET symbol = 'GM' WHERE id = 246; SELECT symbol FROM limit_orders WHERE id = 246; +-- simple UPDATE with RETURNING +UPDATE limit_orders SET symbol = 'GM' WHERE id = 246 RETURNING *; + -- expression UPDATE UPDATE limit_orders SET bidder_id = 6 * 3 WHERE id = 246; SELECT bidder_id FROM limit_orders WHERE id = 246; +-- expression UPDATE with RETURNING +UPDATE limit_orders SET bidder_id = 6 * 5 WHERE id = 246 RETURNING *; + -- multi-column UPDATE UPDATE limit_orders SET (kind, limit_price) = ('buy', DEFAULT) WHERE id = 246; SELECT kind, limit_price FROM limit_orders WHERE id = 246; +-- multi-column UPDATE with RETURNING +UPDATE limit_orders SET (kind, limit_price) = ('buy', 999) WHERE id = 246 RETURNING *; + -- Test that on unique contraint violations, we fail fast INSERT INTO limit_orders VALUES (275, 'ADR', 140, '2007-07-02 16:32:15', 'sell', 43.67); INSERT INTO limit_orders VALUES (275, 'ADR', 140, '2007-07-02 16:32:15', 'sell', 43.67); @@ -285,12 +307,28 @@ UPDATE limit_orders SET symbol = LOWER(symbol) WHERE id = 246; SELECT symbol, bidder_id FROM limit_orders WHERE id = 246; +-- IMMUTABLE functions are allowed -- even in returning +UPDATE limit_orders SET symbol = UPPER(symbol) WHERE id = 246 RETURNING id, LOWER(symbol), symbol; + -- updates referencing non-IMMUTABLE functions are unsupported UPDATE limit_orders SET placed_at = now() WHERE id = 246; +-- even in RETURNING +UPDATE limit_orders SET placed_at = placed_at WHERE id = 246 RETURNING NOW(); + -- cursors are not supported UPDATE limit_orders SET symbol = 'GM' WHERE CURRENT OF cursor_name; +-- check that multi-row UPDATE/DELETEs with RETURNING work +INSERT INTO multiple_hash VALUES ('0', '1'); +INSERT INTO multiple_hash VALUES ('0', '2'); +INSERT INTO multiple_hash VALUES ('0', '3'); +INSERT INTO multiple_hash VALUES ('0', '4'); +INSERT INTO multiple_hash VALUES ('0', '5'); +INSERT INTO multiple_hash VALUES ('0', '6'); + +UPDATE multiple_hash SET data = data ||'-1' WHERE category = '0' RETURNING *; +DELETE FROM multiple_hash WHERE category = '0' RETURNING *; -- ensure returned row counters are correct \set QUIET off @@ -300,12 +338,15 @@ INSERT INTO multiple_hash VALUES ('1', '3'); INSERT INTO multiple_hash VALUES ('2', '1'); INSERT INTO multiple_hash VALUES ('2', '2'); INSERT INTO multiple_hash VALUES ('2', '3'); +INSERT INTO multiple_hash VALUES ('2', '3') RETURNING *; -- check that update return the right number of rows -- one row UPDATE multiple_hash SET data = data ||'-1' WHERE category = '1' AND data = '1'; -- three rows UPDATE multiple_hash SET data = data ||'-2' WHERE category = '1'; +-- three rows, with RETURNING +UPDATE multiple_hash SET data = data ||'-2' WHERE category = '1' RETURNING category; -- check SELECT * FROM multiple_hash WHERE category = '1' ORDER BY category, data; @@ -314,5 +355,8 @@ SELECT * FROM multiple_hash WHERE category = '1' ORDER BY category, data; DELETE FROM multiple_hash WHERE category = '2' AND data = '1'; -- two rows DELETE FROM multiple_hash WHERE category = '2'; +-- three rows, with RETURNING +DELETE FROM multiple_hash WHERE category = '1' RETURNING category; -- check +SELECT * FROM multiple_hash WHERE category = '1' ORDER BY category, data; SELECT * FROM multiple_hash WHERE category = '2' ORDER BY category, data; diff --git a/src/test/regress/sql/multi_upsert.sql b/src/test/regress/sql/multi_upsert.sql index 394ef0bbc..0d8de95f6 100644 --- a/src/test/regress/sql/multi_upsert.sql +++ b/src/test/regress/sql/multi_upsert.sql @@ -85,6 +85,15 @@ INSERT INTO upsert_test as ups_test (part_key, other_col) VALUES (1, 1) ON CONFL -- see the results SELECT * FROM upsert_test; +-- Test upsert, with returning: +INSERT INTO upsert_test (part_key, other_col) VALUES (2, 2) + ON CONFLICT (part_key) DO UPDATE SET other_col = 3 + RETURNING *; + +INSERT INTO upsert_test (part_key, other_col) VALUES (2, 2) + ON CONFLICT (part_key) DO UPDATE SET other_col = 3 + RETURNING *; + -- create another table CREATE TABLE upsert_test_2 (