Improve error messages for INSERT queries that have subqueries

pull/2038/head
Onder Kalaci 2018-03-05 14:46:47 +02:00
parent e7b28dd469
commit 40b898b59f
4 changed files with 29 additions and 19 deletions

View File

@ -562,27 +562,15 @@ ModifyQuerySupported(Query *queryTree, Query *originalQuery, bool multiShardQuer
*/ */
if (queryTree->hasSubLinks == true) if (queryTree->hasSubLinks == true)
{ {
/* /* we support subqueries for INSERTs only via INSERT INTO ... SELECT */
* We support UPDATE and DELETE with subqueries unless they are multi
* shard queries.
*/
if (!UpdateOrDeleteQuery(queryTree)) if (!UpdateOrDeleteQuery(queryTree))
{ {
StringInfo errorHint = makeStringInfo(); Assert(queryTree->commandType == CMD_INSERT);
DistTableCacheEntry *cacheEntry = DistributedTableCacheEntry(
distributedTableId);
char *partitionKeyString = cacheEntry->partitionKeyString;
char *partitionColumnName = ColumnNameToColumn(distributedTableId,
partitionKeyString);
appendStringInfo(errorHint,
"Consider using an equality filter on partition column \"%s\" to target a single shard.",
partitionColumnName);
return DeferredError(ERRCODE_FEATURE_NOT_SUPPORTED, return DeferredError(ERRCODE_FEATURE_NOT_SUPPORTED,
"subqueries are not supported in modifications across " "subqueries are not supported within INSERT queries",
"multiple shards", NULL, "Try rewriting your queries with 'INSERT "
errorHint->data, NULL); "INTO ... SELECT' syntax.");
} }
} }

View File

@ -1275,6 +1275,20 @@ SELECT * FROM summary_table ORDER BY id;
----+-----------+---------------+-------+--------- ----+-----------+---------------+-------+---------
(0 rows) (0 rows)
-- we don't support subqueries in VALUES clause
INSERT INTO summary_table (id) VALUES ((SELECT id FROM summary_table));
ERROR: subqueries are not supported within INSERT queries
HINT: Try rewriting your queries with 'INSERT INTO ... SELECT' syntax.
INSERT INTO summary_table (id) VALUES (5), ((SELECT id FROM summary_table));
ERROR: subqueries are not supported within INSERT queries
HINT: Try rewriting your queries with 'INSERT INTO ... SELECT' syntax.
-- similar queries with reference tables
INSERT INTO reference_summary_table (id) VALUES ((SELECT id FROM summary_table));
ERROR: subqueries are not supported within INSERT queries
HINT: Try rewriting your queries with 'INSERT INTO ... SELECT' syntax.
INSERT INTO summary_table (id) VALUES ((SELECT id FROM reference_summary_table));
ERROR: subqueries are not supported within INSERT queries
HINT: Try rewriting your queries with 'INSERT INTO ... SELECT' syntax.
DROP TABLE raw_table; DROP TABLE raw_table;
DROP TABLE summary_table; DROP TABLE summary_table;
DROP TABLE reference_raw_table; DROP TABLE reference_raw_table;

View File

@ -253,8 +253,8 @@ INSERT INTO dropcol_distributed AS dropcol (key, keep1) VALUES (1, '5') ON CONFL
-- subquery in the SET clause -- subquery in the SET clause
INSERT INTO upsert_test (part_key, other_col) VALUES (1, 1) ON CONFLICT (part_key) DO INSERT INTO upsert_test (part_key, other_col) VALUES (1, 1) ON CONFLICT (part_key) DO
UPDATE SET other_col = (SELECT count(*) from upsert_test); UPDATE SET other_col = (SELECT count(*) from upsert_test);
ERROR: subqueries are not supported in modifications across multiple shards ERROR: subqueries are not supported within INSERT queries
DETAIL: Consider using an equality filter on partition column "part_key" to target a single shard. HINT: Try rewriting your queries with 'INSERT INTO ... SELECT' syntax.
-- non mutable function call in the SET -- non mutable function call in the SET
INSERT INTO upsert_test (part_key, other_col) VALUES (1, 1) ON CONFLICT (part_key) DO INSERT INTO upsert_test (part_key, other_col) VALUES (1, 1) ON CONFLICT (part_key) DO
UPDATE SET other_col = random()::int; UPDATE SET other_col = random()::int;

View File

@ -821,6 +821,14 @@ EXECUTE prepared_delete_with_join(6);
SELECT * FROM summary_table ORDER BY id; SELECT * FROM summary_table ORDER BY id;
-- we don't support subqueries in VALUES clause
INSERT INTO summary_table (id) VALUES ((SELECT id FROM summary_table));
INSERT INTO summary_table (id) VALUES (5), ((SELECT id FROM summary_table));
-- similar queries with reference tables
INSERT INTO reference_summary_table (id) VALUES ((SELECT id FROM summary_table));
INSERT INTO summary_table (id) VALUES ((SELECT id FROM reference_summary_table));
DROP TABLE raw_table; DROP TABLE raw_table;
DROP TABLE summary_table; DROP TABLE summary_table;
DROP TABLE reference_raw_table; DROP TABLE reference_raw_table;