Skip to content

Commit 90e5288

Browse files
committed
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
1 parent 5045c79 commit 90e5288

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

src/backend/parser/analyze.c

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,7 +1336,7 @@ static Query *
13361336
transformValuesClause(ParseState *pstate, SelectStmt *stmt)
13371337
{
13381338
Query *qry = makeNode(Query);
1339-
List *exprsLists;
1339+
List *exprsLists = NIL;
13401340
List *coltypes = NIL;
13411341
List *coltypmods = NIL;
13421342
List *colcollations = NIL;
@@ -1421,6 +1421,9 @@ transformValuesClause(ParseState *pstate, SelectStmt *stmt)
14211421

14221422
/* Release sub-list's cells to save memory */
14231423
list_free(sublist);
1424+
1425+
/* Prepare an exprsLists element for this row */
1426+
exprsLists = lappend(exprsLists, NIL);
14241427
}
14251428

14261429
/*
@@ -1475,25 +1478,15 @@ transformValuesClause(ParseState *pstate, SelectStmt *stmt)
14751478
/*
14761479
* Finally, rearrange the coerced expressions into row-organized lists.
14771480
*/
1478-
exprsLists = NIL;
1479-
foreach(lc, colexprs[0])
1480-
{
1481-
Node *col = (Node *) lfirst(lc);
1482-
List *sublist;
1483-
1484-
sublist = list_make1(col);
1485-
exprsLists = lappend(exprsLists, sublist);
1486-
}
1487-
list_free(colexprs[0]);
1488-
for (i = 1; i < sublist_length; i++)
1481+
for (i = 0; i < sublist_length; i++)
14891482
{
14901483
forboth(lc, colexprs[i], lc2, exprsLists)
14911484
{
14921485
Node *col = (Node *) lfirst(lc);
14931486
List *sublist = lfirst(lc2);
14941487

1495-
/* sublist pointer in exprsLists won't need adjustment */
1496-
(void) lappend(sublist, col);
1488+
sublist = lappend(sublist, col);
1489+
lfirst(lc2) = sublist;
14971490
}
14981491
list_free(colexprs[i]);
14991492
}

src/test/regress/expected/select.out

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,13 @@ TABLE int8_tbl;
517517
4567890123456789 | -4567890123456789
518518
(9 rows)
519519

520+
-- corner case: VALUES with no columns
521+
CREATE TEMP TABLE nocols();
522+
INSERT INTO nocols DEFAULT VALUES;
523+
SELECT * FROM nocols n, LATERAL (VALUES(n.*)) v;
524+
--
525+
(1 row)
526+
520527
--
521528
-- Test ORDER BY options
522529
--

src/test/regress/sql/select.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@ SELECT 2+2, 57
148148
UNION ALL
149149
TABLE int8_tbl;
150150

151+
-- corner case: VALUES with no columns
152+
CREATE TEMP TABLE nocols();
153+
INSERT INTO nocols DEFAULT VALUES;
154+
SELECT * FROM nocols n, LATERAL (VALUES(n.*)) v;
155+
151156
--
152157
-- Test ORDER BY options
153158
--

0 commit comments

Comments
 (0)