Skip to content

Commit 4eabaff

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 86a2180 commit 4eabaff

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
@@ -1346,7 +1346,7 @@ static Query *
13461346
transformValuesClause(ParseState *pstate, SelectStmt *stmt)
13471347
{
13481348
Query *qry = makeNode(Query);
1349-
List *exprsLists;
1349+
List *exprsLists = NIL;
13501350
List *coltypes = NIL;
13511351
List *coltypmods = NIL;
13521352
List *colcollations = NIL;
@@ -1431,6 +1431,9 @@ transformValuesClause(ParseState *pstate, SelectStmt *stmt)
14311431

14321432
/* Release sub-list's cells to save memory */
14331433
list_free(sublist);
1434+
1435+
/* Prepare an exprsLists element for this row */
1436+
exprsLists = lappend(exprsLists, NIL);
14341437
}
14351438

14361439
/*
@@ -1485,25 +1488,15 @@ transformValuesClause(ParseState *pstate, SelectStmt *stmt)
14851488
/*
14861489
* Finally, rearrange the coerced expressions into row-organized lists.
14871490
*/
1488-
exprsLists = NIL;
1489-
foreach(lc, colexprs[0])
1490-
{
1491-
Node *col = (Node *) lfirst(lc);
1492-
List *sublist;
1493-
1494-
sublist = list_make1(col);
1495-
exprsLists = lappend(exprsLists, sublist);
1496-
}
1497-
list_free(colexprs[0]);
1498-
for (i = 1; i < sublist_length; i++)
1491+
for (i = 0; i < sublist_length; i++)
14991492
{
15001493
forboth(lc, colexprs[i], lc2, exprsLists)
15011494
{
15021495
Node *col = (Node *) lfirst(lc);
15031496
List *sublist = lfirst(lc2);
15041497

1505-
/* sublist pointer in exprsLists won't need adjustment */
1506-
(void) lappend(sublist, col);
1498+
sublist = lappend(sublist, col);
1499+
lfirst(lc2) = sublist;
15071500
}
15081501
list_free(colexprs[i]);
15091502
}

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)