diff --git a/src/backend/parser/analyze.c b/src/backend/parser/analyze.c index 32b3a0a8e5..d243e0284a 100644 --- a/src/backend/parser/analyze.c +++ b/src/backend/parser/analyze.c @@ -1336,7 +1336,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; @@ -1421,6 +1421,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); } /* @@ -1475,25 +1478,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 c441049f41..6dcd4fa8f3 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 b5929b2eca..d6f42aa0d4 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 --