Skip to content

Commit ab2f783

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 9b5797c commit ab2f783

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

src/backend/parser/analyze.c

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,7 +1381,7 @@ static Query *
13811381
transformValuesClause(ParseState *pstate, SelectStmt *stmt)
13821382
{
13831383
Query *qry = makeNode(Query);
1384-
List *exprsLists;
1384+
List *exprsLists = NIL;
13851385
List *coltypes = NIL;
13861386
List *coltypmods = NIL;
13871387
List *colcollations = NIL;
@@ -1465,6 +1465,9 @@ transformValuesClause(ParseState *pstate, SelectStmt *stmt)
14651465

14661466
/* Release sub-list's cells to save memory */
14671467
list_free(sublist);
1468+
1469+
/* Prepare an exprsLists element for this row */
1470+
exprsLists = lappend(exprsLists, NIL);
14681471
}
14691472

14701473
/*
@@ -1508,24 +1511,15 @@ transformValuesClause(ParseState *pstate, SelectStmt *stmt)
15081511
/*
15091512
* Finally, rearrange the coerced expressions into row-organized lists.
15101513
*/
1511-
exprsLists = NIL;
1512-
foreach(lc, colexprs[0])
1513-
{
1514-
Node *col = (Node *) lfirst(lc);
1515-
List *sublist;
1516-
1517-
sublist = list_make1(col);
1518-
exprsLists = lappend(exprsLists, sublist);
1519-
}
1520-
list_free(colexprs[0]);
1521-
for (i = 1; i < sublist_length; i++)
1514+
for (i = 0; i < sublist_length; i++)
15221515
{
15231516
forboth(lc, colexprs[i], lc2, exprsLists)
15241517
{
15251518
Node *col = (Node *) lfirst(lc);
15261519
List *sublist = lfirst(lc2);
15271520

15281521
sublist = lappend(sublist, col);
1522+
lfirst(lc2) = sublist;
15291523
}
15301524
list_free(colexprs[i]);
15311525
}

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)