diff --git a/src/test/regress/expected/multi_modifications.out b/src/test/regress/expected/multi_modifications.out index 25f23ad3c..eb395fdba 100644 --- a/src/test/regress/expected/multi_modifications.out +++ b/src/test/regress/expected/multi_modifications.out @@ -659,7 +659,12 @@ INSERT INTO app_analytics_events (app_id, name) VALUES (103, 'Mynt') RETURNING * DROP TABLE app_analytics_events; -- test UPDATE ... FROM CREATE TABLE raw_table (id bigint, value bigint); -CREATE TABLE summary_table (id bigint, min_value numeric, average_value numeric); +CREATE TABLE summary_table ( + id bigint, + min_value numeric, + average_value numeric, + count int, + uniques int); SELECT create_distributed_table('raw_table', 'id'); create_distributed_table -------------------------- @@ -674,11 +679,16 @@ SELECT create_distributed_table('summary_table', 'id'); INSERT INTO raw_table VALUES (1, 100); INSERT INTO raw_table VALUES (1, 200); -INSERT INTO summary_table VALUES (1, NULL); +INSERT INTO raw_table VALUES (1, 200); +INSERT INTO raw_table VALUES (1, 300); +INSERT INTO raw_table VALUES (2, 400); +INSERT INTO raw_table VALUES (2, 500); +INSERT INTO summary_table VALUES (1); +INSERT INTO summary_table VALUES (2); SELECT * FROM summary_table WHERE id = 1; - id | min_value | average_value -----+-----------+--------------- - 1 | | + id | min_value | average_value | count | uniques +----+-----------+---------------+-------+--------- + 1 | | | | (1 row) UPDATE summary_table SET average_value = average_query.average FROM ( @@ -686,19 +696,30 @@ UPDATE summary_table SET average_value = average_query.average FROM ( ) average_query WHERE id = 1; SELECT * FROM summary_table WHERE id = 1; - id | min_value | average_value -----+-----------+---------------------- - 1 | | 150.0000000000000000 + id | min_value | average_value | count | uniques +----+-----------+----------------------+-------+--------- + 1 | | 200.0000000000000000 | | +(1 row) + +-- try different syntax +UPDATE summary_table SET (min_value, average_value) = + (SELECT min(value), avg(value) FROM raw_table WHERE id = 2) +WHERE id = 2; +SELECT * FROM summary_table WHERE id = 2; + id | min_value | average_value | count | uniques +----+-----------+----------------------+-------+--------- + 2 | 400 | 450.0000000000000000 | | (1 row) UPDATE summary_table SET min_value = 100 WHERE id IN (SELECT id FROM raw_table WHERE id = 1 and value > 100) AND id = 1; SELECT * FROM summary_table WHERE id = 1; - id | min_value | average_value -----+-----------+---------------------- - 1 | 100 | 150.0000000000000000 + id | min_value | average_value | count | uniques +----+-----------+----------------------+-------+--------- + 1 | 100 | 200.0000000000000000 | | (1 row) +-- test unsupported query types UPDATE summary_table SET average_value = average_query.average FROM ( SELECT avg(value) AS average FROM raw_table WHERE id = 1 AND id = 4 ) average_query @@ -712,9 +733,9 @@ WHERE id = 1; ERROR: cannot run UPDATE command which targets no shards HINT: Make sure the value for partition column falls into a single shard. SELECT * FROM summary_table WHERE id = 1; - id | min_value | average_value -----+-----------+---------------------- - 1 | 100 | 150.0000000000000000 + id | min_value | average_value | count | uniques +----+-----------+----------------------+-------+--------- + 1 | 100 | 200.0000000000000000 | | (1 row) UPDATE summary_table SET average_value = average_query.average FROM ( @@ -727,5 +748,31 @@ UPDATE summary_table SET average_value = average_query.average FROM ( SELECT avg(value) AS average FROM raw_table) average_query; ERROR: cannot run UPDATE command which targets multiple shards HINT: Make sure the value for partition column falls into a single shard. +UPDATE summary_table SET average_value = average_value + 1 WHERE id = + (SELECT id FROM raw_table WHERE value > 100); +ERROR: cannot run UPDATE command which targets multiple shards +HINT: Make sure the value for partition column falls into a single shard. +-- test complex queries +UPDATE summary_table +SET + uniques = metrics.expensive_uniques, + count = metrics.total_count +FROM + (SELECT + id, + count(DISTINCT (CASE WHEN value > 100 then value end)) AS expensive_uniques, + count(value) AS total_count + FROM raw_table + WHERE id = 1 + GROUP BY id) metrics +WHERE + summary_table.id = metrics.id AND + summary_table.id = 1; +SELECT * FROM summary_table WHERE id = 1; + id | min_value | average_value | count | uniques +----+-----------+----------------------+-------+--------- + 1 | 100 | 200.0000000000000000 | 4 | 2 +(1 row) + DROP TABLE raw_table; DROP TABLE summary_table; diff --git a/src/test/regress/sql/multi_modifications.sql b/src/test/regress/sql/multi_modifications.sql index b75d778ff..ef48b0e55 100644 --- a/src/test/regress/sql/multi_modifications.sql +++ b/src/test/regress/sql/multi_modifications.sql @@ -432,15 +432,25 @@ DROP TABLE app_analytics_events; -- test UPDATE ... FROM CREATE TABLE raw_table (id bigint, value bigint); -CREATE TABLE summary_table (id bigint, min_value numeric, average_value numeric); +CREATE TABLE summary_table ( + id bigint, + min_value numeric, + average_value numeric, + count int, + uniques int); SELECT create_distributed_table('raw_table', 'id'); SELECT create_distributed_table('summary_table', 'id'); INSERT INTO raw_table VALUES (1, 100); INSERT INTO raw_table VALUES (1, 200); +INSERT INTO raw_table VALUES (1, 200); +INSERT INTO raw_table VALUES (1, 300); +INSERT INTO raw_table VALUES (2, 400); +INSERT INTO raw_table VALUES (2, 500); -INSERT INTO summary_table VALUES (1, NULL); +INSERT INTO summary_table VALUES (1); +INSERT INTO summary_table VALUES (2); SELECT * FROM summary_table WHERE id = 1; @@ -451,11 +461,19 @@ WHERE id = 1; SELECT * FROM summary_table WHERE id = 1; +-- try different syntax +UPDATE summary_table SET (min_value, average_value) = + (SELECT min(value), avg(value) FROM raw_table WHERE id = 2) +WHERE id = 2; + +SELECT * FROM summary_table WHERE id = 2; + UPDATE summary_table SET min_value = 100 WHERE id IN (SELECT id FROM raw_table WHERE id = 1 and value > 100) AND id = 1; SELECT * FROM summary_table WHERE id = 1; +-- test unsupported query types UPDATE summary_table SET average_value = average_query.average FROM ( SELECT avg(value) AS average FROM raw_table WHERE id = 1 AND id = 4 ) average_query @@ -476,5 +494,27 @@ WHERE id = 1 AND id = 4; UPDATE summary_table SET average_value = average_query.average FROM ( SELECT avg(value) AS average FROM raw_table) average_query; +UPDATE summary_table SET average_value = average_value + 1 WHERE id = + (SELECT id FROM raw_table WHERE value > 100); + +-- test complex queries +UPDATE summary_table +SET + uniques = metrics.expensive_uniques, + count = metrics.total_count +FROM + (SELECT + id, + count(DISTINCT (CASE WHEN value > 100 then value end)) AS expensive_uniques, + count(value) AS total_count + FROM raw_table + WHERE id = 1 + GROUP BY id) metrics +WHERE + summary_table.id = metrics.id AND + summary_table.id = 1; + +SELECT * FROM summary_table WHERE id = 1; + DROP TABLE raw_table; DROP TABLE summary_table;