Skip to content

Commit e947838

Browse files
committed
Fix dumping of outer joins with empty qual lists.
Normally, a JoinExpr would have empty "quals" only if it came from CROSS JOIN syntax. However, it's possible to get to this state by specifying NATURAL JOIN between two tables with no common column names, and there might be other ways too. The code previously printed no ON clause if "quals" was empty; that's right for CROSS JOIN but syntactically invalid if it's some type of outer join. Fix by printing ON TRUE in that case. This got broken by commit 2ffa740, which stopped using NATURAL JOIN syntax in ruleutils output due to its brittleness in the face of column renamings. Back-patch to 9.3 where that commit appeared. Per report from Tushar Ahuja. Discussion: https://postgr.es/m/98b283cd-6dda-5d3f-f8ac-87db8c76a3da@enterprisedb.com
1 parent cd969b2 commit e947838

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

src/backend/utils/adt/ruleutils.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8553,6 +8553,11 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
85538553
if (!PRETTY_PAREN(context))
85548554
appendStringInfoChar(buf, ')');
85558555
}
8556+
else if (j->jointype != JOIN_INNER)
8557+
{
8558+
/* If we didn't say CROSS JOIN above, we must provide an ON */
8559+
appendStringInfoString(buf, " ON TRUE");
8560+
}
85568561

85578562
if (!PRETTY_PAREN(context) || j->alias != NULL)
85588563
appendStringInfoChar(buf, ')');

src/test/regress/expected/create_view.out

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,6 +1590,35 @@ select pg_get_viewdef('tt20v', true);
15901590
CAST((1 + 2)::bigint AS bigint) i8(i8);
15911591
(1 row)
15921592

1593+
-- corner cases with empty join conditions
1594+
create view tt21v as
1595+
select * from tt5 natural inner join tt6;
1596+
select pg_get_viewdef('tt21v', true);
1597+
pg_get_viewdef
1598+
----------------------
1599+
SELECT tt5.a, +
1600+
tt5.b, +
1601+
tt5.cc, +
1602+
tt6.c, +
1603+
tt6.d +
1604+
FROM tt5 +
1605+
CROSS JOIN tt6;
1606+
(1 row)
1607+
1608+
create view tt22v as
1609+
select * from tt5 natural left join tt6;
1610+
select pg_get_viewdef('tt22v', true);
1611+
pg_get_viewdef
1612+
-----------------------------
1613+
SELECT tt5.a, +
1614+
tt5.b, +
1615+
tt5.cc, +
1616+
tt6.c, +
1617+
tt6.d +
1618+
FROM tt5 +
1619+
LEFT JOIN tt6 ON TRUE;
1620+
(1 row)
1621+
15931622
-- clean up all the random objects we made above
15941623
set client_min_messages = warning;
15951624
DROP SCHEMA temp_view_test CASCADE;

src/test/regress/sql/create_view.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,16 @@ select * from
531531
cast(1+2 as int8) as i8;
532532
select pg_get_viewdef('tt20v', true);
533533

534+
-- corner cases with empty join conditions
535+
536+
create view tt21v as
537+
select * from tt5 natural inner join tt6;
538+
select pg_get_viewdef('tt21v', true);
539+
540+
create view tt22v as
541+
select * from tt5 natural left join tt6;
542+
select pg_get_viewdef('tt22v', true);
543+
534544
-- clean up all the random objects we made above
535545
set client_min_messages = warning;
536546
DROP SCHEMA temp_view_test CASCADE;

0 commit comments

Comments
 (0)