Skip to content

Commit 6698782

Browse files
committed
Fix crash in assign_collations_walker for EXISTS with empty SELECT list.
We (I think I, actually) forgot about this corner case while coding collation resolution. Per bug #8648 from Arjen Nienhuis.
1 parent 8b47c9d commit 6698782

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

src/backend/parser/parse_collate.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -564,16 +564,22 @@ assign_collations_walker(Node *node, assign_collations_context *context)
564564
* SubLink. Act as though the Query returns its first output
565565
* column, which indeed is what it does for EXPR_SUBLINK and
566566
* ARRAY_SUBLINK cases. In the cases where the SubLink
567-
* returns boolean, this info will be ignored.
567+
* returns boolean, this info will be ignored. Special case:
568+
* in EXISTS, the Query might return no columns, in which case
569+
* we need do nothing.
568570
*
569571
* We needn't recurse, since the Query is already processed.
570572
*/
571573
Query *qtree = (Query *) node;
572574
TargetEntry *tent;
573575

576+
if (qtree->targetList == NIL)
577+
return false;
574578
tent = (TargetEntry *) linitial(qtree->targetList);
575579
Assert(IsA(tent, TargetEntry));
576-
Assert(!tent->resjunk);
580+
if (tent->resjunk)
581+
return false;
582+
577583
collation = exprCollation((Node *) tent->expr);
578584
/* collation doesn't change if it's converted to array */
579585
strength = COLLATE_IMPLICIT;

src/test/regress/expected/subselect.out

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,3 +703,13 @@ explain (verbose, costs off)
703703
One-Time Filter: ("*VALUES*".column1 = "*VALUES*".column1)
704704
(8 rows)
705705

706+
--
707+
-- Check we behave sanely in corner case of empty SELECT list (bug #8648)
708+
--
709+
create temp table nocolumns();
710+
select exists(select * from nocolumns);
711+
exists
712+
--------
713+
f
714+
(1 row)
715+

src/test/regress/sql/subselect.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,3 +405,9 @@ explain (verbose, costs off)
405405
explain (verbose, costs off)
406406
select x, x from
407407
(select (select random() where y=y) as x from (values(1),(2)) v(y)) ss;
408+
409+
--
410+
-- Check we behave sanely in corner case of empty SELECT list (bug #8648)
411+
--
412+
create temp table nocolumns();
413+
select exists(select * from nocolumns);

0 commit comments

Comments
 (0)