summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Lane2022-05-09 18:15:37 +0000
committerTom Lane2022-05-09 18:15:37 +0000
commit4eabaffcada25206c3f8f89f7cb0b27ffb9372bc (patch)
tree6e743287106d29f782bb092e9436cc8c3c7ca68a
parent86a21803c7d860bd0cbd8c81c34cc9250d4cc173 (diff)
Fix core dump in transformValuesClause when there are no columns.
The parser code that transformed VALUES from row-oriented to column-oriented lists failed if there were zero columns. You can't write that straightforwardly (though probably you should be able to), but the case can be reached by expanding a "tab.*" reference to a zero-column table. Per bug #17477 from Wang Ke. Back-patch to all supported branches. Discussion: https://postgr.es/m/17477-0af3c6ac6b0a6ae0@postgresql.org
-rw-r--r--src/backend/parser/analyze.c21
-rw-r--r--src/test/regress/expected/select.out7
-rw-r--r--src/test/regress/sql/select.sql5
3 files changed, 19 insertions, 14 deletions
diff --git a/src/backend/parser/analyze.c b/src/backend/parser/analyze.c
index 3678dcfc9da..14be0882578 100644
--- a/src/backend/parser/analyze.c
+++ b/src/backend/parser/analyze.c
@@ -1346,7 +1346,7 @@ static Query *
transformValuesClause(ParseState *pstate, SelectStmt *stmt)
{
Query *qry = makeNode(Query);
- List *exprsLists;
+ List *exprsLists = NIL;
List *coltypes = NIL;
List *coltypmods = NIL;
List *colcollations = NIL;
@@ -1431,6 +1431,9 @@ transformValuesClause(ParseState *pstate, SelectStmt *stmt)
/* Release sub-list's cells to save memory */
list_free(sublist);
+
+ /* Prepare an exprsLists element for this row */
+ exprsLists = lappend(exprsLists, NIL);
}
/*
@@ -1485,25 +1488,15 @@ transformValuesClause(ParseState *pstate, SelectStmt *stmt)
/*
* Finally, rearrange the coerced expressions into row-organized lists.
*/
- exprsLists = NIL;
- foreach(lc, colexprs[0])
- {
- Node *col = (Node *) lfirst(lc);
- List *sublist;
-
- sublist = list_make1(col);
- exprsLists = lappend(exprsLists, sublist);
- }
- list_free(colexprs[0]);
- for (i = 1; i < sublist_length; i++)
+ for (i = 0; i < sublist_length; i++)
{
forboth(lc, colexprs[i], lc2, exprsLists)
{
Node *col = (Node *) lfirst(lc);
List *sublist = lfirst(lc2);
- /* sublist pointer in exprsLists won't need adjustment */
- (void) lappend(sublist, col);
+ sublist = lappend(sublist, col);
+ lfirst(lc2) = sublist;
}
list_free(colexprs[i]);
}
diff --git a/src/test/regress/expected/select.out b/src/test/regress/expected/select.out
index 1fab5136d29..0df53f0f619 100644
--- a/src/test/regress/expected/select.out
+++ b/src/test/regress/expected/select.out
@@ -517,6 +517,13 @@ TABLE int8_tbl;
4567890123456789 | -4567890123456789
(9 rows)
+-- corner case: VALUES with no columns
+CREATE TEMP TABLE nocols();
+INSERT INTO nocols DEFAULT VALUES;
+SELECT * FROM nocols n, LATERAL (VALUES(n.*)) v;
+--
+(1 row)
+
--
-- Test ORDER BY options
--
diff --git a/src/test/regress/sql/select.sql b/src/test/regress/sql/select.sql
index c80429e7d03..3f8cecc3f3b 100644
--- a/src/test/regress/sql/select.sql
+++ b/src/test/regress/sql/select.sql
@@ -148,6 +148,11 @@ SELECT 2+2, 57
UNION ALL
TABLE int8_tbl;
+-- corner case: VALUES with no columns
+CREATE TEMP TABLE nocols();
+INSERT INTO nocols DEFAULT VALUES;
+SELECT * FROM nocols n, LATERAL (VALUES(n.*)) v;
+
--
-- Test ORDER BY options
--