Skip to content

Commit 94e4778

Browse files
committed
The result of a FULL or RIGHT join can't be assumed to be sorted by the
left input's sorting, because null rows may be inserted at various points. Per report from Ferenc Lutischá¸n.
1 parent d08889a commit 94e4778

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

src/backend/optimizer/path/joinpath.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/optimizer/path/joinpath.c,v 1.91 2004/12/31 22:00:04 pgsql Exp $
11+
* $PostgreSQL: pgsql/src/backend/optimizer/path/joinpath.c,v 1.92 2005/01/23 02:21:26 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -271,7 +271,8 @@ sort_inner_and_outer(Query *root,
271271
cur_mergeclauses,
272272
innerrel);
273273
/* Build pathkeys representing output sort order. */
274-
merge_pathkeys = build_join_pathkeys(root, joinrel, outerkeys);
274+
merge_pathkeys = build_join_pathkeys(root, joinrel, jointype,
275+
outerkeys);
275276

276277
/*
277278
* And now we can make the path.
@@ -431,7 +432,7 @@ match_unsorted_outer(Query *root,
431432
* as a nestloop, and even if some of the mergeclauses are
432433
* implemented by qpquals rather than as true mergeclauses):
433434
*/
434-
merge_pathkeys = build_join_pathkeys(root, joinrel,
435+
merge_pathkeys = build_join_pathkeys(root, joinrel, jointype,
435436
outerpath->pathkeys);
436437

437438
if (nestjoinOK)

src/backend/optimizer/path/pathkeys.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* Portions Copyright (c) 1994, Regents of the University of California
1212
*
1313
* IDENTIFICATION
14-
* $PostgreSQL: pgsql/src/backend/optimizer/path/pathkeys.c,v 1.63 2004/12/31 22:00:04 pgsql Exp $
14+
* $PostgreSQL: pgsql/src/backend/optimizer/path/pathkeys.c,v 1.64 2005/01/23 02:21:26 tgl Exp $
1515
*
1616
*-------------------------------------------------------------------------
1717
*/
@@ -858,16 +858,25 @@ build_subquery_pathkeys(Query *root, RelOptInfo *rel, Query *subquery)
858858
* vars they were joined with; furthermore, it doesn't matter what kind
859859
* of join algorithm is actually used.
860860
*
861+
* EXCEPTION: in a FULL or RIGHT join, we cannot treat the result as
862+
* having the outer path's path keys, because null lefthand rows may be
863+
* inserted at random points. It must be treated as unsorted.
864+
*
861865
* 'joinrel' is the join relation that paths are being formed for
866+
* 'jointype' is the join type (inner, left, full, etc)
862867
* 'outer_pathkeys' is the list of the current outer path's path keys
863868
*
864869
* Returns the list of new path keys.
865870
*/
866871
List *
867872
build_join_pathkeys(Query *root,
868873
RelOptInfo *joinrel,
874+
JoinType jointype,
869875
List *outer_pathkeys)
870876
{
877+
if (jointype == JOIN_FULL || jointype == JOIN_RIGHT)
878+
return NIL;
879+
871880
/*
872881
* This used to be quite a complex bit of code, but now that all
873882
* pathkey sublists start out life canonicalized, we don't have to do

src/include/optimizer/paths.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
99
* Portions Copyright (c) 1994, Regents of the University of California
1010
*
11-
* $PostgreSQL: pgsql/src/include/optimizer/paths.h,v 1.77 2004/12/31 22:03:36 pgsql Exp $
11+
* $PostgreSQL: pgsql/src/include/optimizer/paths.h,v 1.78 2005/01/23 02:21:36 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -114,6 +114,7 @@ extern List *build_subquery_pathkeys(Query *root, RelOptInfo *rel,
114114
Query *subquery);
115115
extern List *build_join_pathkeys(Query *root,
116116
RelOptInfo *joinrel,
117+
JoinType jointype,
117118
List *outer_pathkeys);
118119
extern List *make_pathkeys_for_sortclauses(List *sortclauses,
119120
List *tlist);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy