Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit63247be

Browse files
committed
Fix parser so that we don't modify the user-written ORDER BY list in order
to represent DISTINCT or DISTINCT ON. This gets rid of a longstandingannoyance that a view or rule using SELECT DISTINCT will be dumped outwith an overspecified ORDER BY list, and is one small step along the wayto decoupling DISTINCT and ORDER BY enough so that hash-based implementationof DISTINCT will be possible. In passing, improve transformDistinctClauseso that it doesn't reject duplicate DISTINCT ON items, as was reported bySteve Midgley a couple weeks ago.
1 parentb1fb3b2 commit63247be

File tree

7 files changed

+173
-124
lines changed

7 files changed

+173
-124
lines changed

‎src/backend/optimizer/plan/planmain.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*
1515
*
1616
* IDENTIFICATION
17-
* $PostgreSQL: pgsql/src/backend/optimizer/plan/planmain.c,v 1.106 2008/01/11 04:02:18 tgl Exp $
17+
* $PostgreSQL: pgsql/src/backend/optimizer/plan/planmain.c,v 1.107 2008/07/31 22:47:56 tgl Exp $
1818
*
1919
*-------------------------------------------------------------------------
2020
*/
@@ -288,7 +288,7 @@ query_planner(PlannerInfo *root, List *tlist,
288288
* levels of sort --- and, therefore, certainly need to read all the
289289
* tuples --- unless ORDER BY is a subset of GROUP BY.
290290
*/
291-
if (parse->groupClause&&parse->sortClause&&
291+
if (root->group_pathkeys&&root->sort_pathkeys&&
292292
!pathkeys_contained_in(root->sort_pathkeys,root->group_pathkeys))
293293
tuple_fraction=0.0;
294294
}

‎src/backend/optimizer/plan/planner.c

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/optimizer/plan/planner.c,v 1.234 2008/07/10 02:14:03 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/optimizer/plan/planner.c,v 1.235 2008/07/31 22:47:56 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -826,6 +826,7 @@ grouping_planner(PlannerInfo *root, double tuple_fraction)
826826
/*
827827
* Calculate pathkeys that represent result ordering requirements
828828
*/
829+
Assert(parse->distinctClause==NIL);
829830
sort_pathkeys=make_pathkeys_for_sortclauses(root,
830831
parse->sortClause,
831832
tlist,
@@ -864,17 +865,29 @@ grouping_planner(PlannerInfo *root, double tuple_fraction)
864865
* Calculate pathkeys that represent grouping/ordering requirements.
865866
* Stash them in PlannerInfo so that query_planner can canonicalize
866867
* them after EquivalenceClasses have been formed.
868+
*
869+
* Note: for the moment, DISTINCT is always implemented via sort/uniq,
870+
* and we set the sort_pathkeys to be the more rigorous of the
871+
* DISTINCT and ORDER BY requirements. This should be changed
872+
* someday, but DISTINCT ON is a bit of a problem ...
867873
*/
868874
root->group_pathkeys=
869875
make_pathkeys_for_sortclauses(root,
870876
parse->groupClause,
871877
tlist,
872878
false);
873-
root->sort_pathkeys=
874-
make_pathkeys_for_sortclauses(root,
875-
parse->sortClause,
876-
tlist,
877-
false);
879+
if (list_length(parse->distinctClause)>list_length(parse->sortClause))
880+
root->sort_pathkeys=
881+
make_pathkeys_for_sortclauses(root,
882+
parse->distinctClause,
883+
tlist,
884+
false);
885+
else
886+
root->sort_pathkeys=
887+
make_pathkeys_for_sortclauses(root,
888+
parse->sortClause,
889+
tlist,
890+
false);
878891

879892
/*
880893
* Will need actual number of aggregates for estimating costs.
@@ -903,9 +916,9 @@ grouping_planner(PlannerInfo *root, double tuple_fraction)
903916
* by ORDER BY --- but that might just leave us failing to exploit an
904917
* available sort order at all. Needs more thought...)
905918
*/
906-
if (parse->groupClause)
919+
if (root->group_pathkeys)
907920
root->query_pathkeys=root->group_pathkeys;
908-
elseif (parse->sortClause)
921+
elseif (root->sort_pathkeys)
909922
root->query_pathkeys=root->sort_pathkeys;
910923
else
911924
root->query_pathkeys=NIL;
@@ -1172,7 +1185,7 @@ grouping_planner(PlannerInfo *root, double tuple_fraction)
11721185
* If we were not able to make the plan come out in the right order, add
11731186
* an explicit sort step.
11741187
*/
1175-
if (parse->sortClause)
1188+
if (sort_pathkeys)
11761189
{
11771190
if (!pathkeys_contained_in(sort_pathkeys,current_pathkeys))
11781191
{

‎src/backend/optimizer/prep/prepunion.c

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*
2323
*
2424
* IDENTIFICATION
25-
* $PostgreSQL: pgsql/src/backend/optimizer/prep/prepunion.c,v 1.147 2008/06/19 00:46:04 alvherre Exp $
25+
* $PostgreSQL: pgsql/src/backend/optimizer/prep/prepunion.c,v 1.148 2008/07/31 22:47:56 tgl Exp $
2626
*
2727
*-------------------------------------------------------------------------
2828
*/
@@ -69,6 +69,7 @@ static List *generate_setop_tlist(List *colTypes, int flag,
6969
staticList*generate_append_tlist(List*colTypes,boolflag,
7070
List*input_plans,
7171
List*refnames_tlist);
72+
staticList*generate_setop_sortlist(List*targetlist);
7273
staticvoidexpand_inherited_rtentry(PlannerInfo*root,RangeTblEntry*rte,
7374
Indexrti);
7475
staticvoidmake_inh_translation_lists(Relationoldrelation,
@@ -319,7 +320,7 @@ generate_union_plan(SetOperationStmt *op, PlannerInfo *root,
319320
{
320321
List*sortList;
321322

322-
sortList=addAllTargetsToSortList(NULL,NIL,tlist, false);
323+
sortList=generate_setop_sortlist(tlist);
323324
if (sortList)
324325
{
325326
plan= (Plan*)make_sort_from_sortclauses(root,sortList,plan);
@@ -384,7 +385,7 @@ generate_nonunion_plan(SetOperationStmt *op, PlannerInfo *root,
384385
* Sort the child results, then add a SetOp plan node to generate the
385386
* correct output.
386387
*/
387-
sortList=addAllTargetsToSortList(NULL,NIL,tlist, false);
388+
sortList=generate_setop_sortlist(tlist);
388389

389390
if (sortList==NIL)/* nothing to sort on? */
390391
{
@@ -675,6 +676,31 @@ generate_append_tlist(List *colTypes, bool flag,
675676
returntlist;
676677
}
677678

679+
/*
680+
* generate_setop_sortlist
681+
*Build a SortClause list enumerating all the non-resjunk tlist entries,
682+
*using default ordering properties.
683+
*/
684+
staticList*
685+
generate_setop_sortlist(List*targetlist)
686+
{
687+
List*sortlist=NIL;
688+
ListCell*l;
689+
690+
foreach(l,targetlist)
691+
{
692+
TargetEntry*tle= (TargetEntry*)lfirst(l);
693+
694+
if (!tle->resjunk)
695+
sortlist=addTargetToSortList(NULL,tle,
696+
sortlist,targetlist,
697+
SORTBY_DEFAULT,
698+
SORTBY_NULLS_DEFAULT,
699+
NIL, false);
700+
}
701+
returnsortlist;
702+
}
703+
678704

679705
/*
680706
* find_all_inheritors -

‎src/backend/parser/analyze.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
1818
* Portions Copyright (c) 1994, Regents of the University of California
1919
*
20-
*$PostgreSQL: pgsql/src/backend/parser/analyze.c,v 1.373 2008/07/18 20:26:06 tgl Exp $
20+
*$PostgreSQL: pgsql/src/backend/parser/analyze.c,v 1.374 2008/07/31 22:47:56 tgl Exp $
2121
*
2222
*-------------------------------------------------------------------------
2323
*/
@@ -711,6 +711,8 @@ transformSelectStmt(ParseState *pstate, SelectStmt *stmt)
711711
/*
712712
* Transform sorting/grouping stuff. Do ORDER BY first because both
713713
* transformGroupClause and transformDistinctClause need the results.
714+
* Note that these functions can also change the targetList, so it's
715+
* passed to them by reference.
714716
*/
715717
qry->sortClause=transformSortClause(pstate,
716718
stmt->sortClause,
@@ -725,8 +727,9 @@ transformSelectStmt(ParseState *pstate, SelectStmt *stmt)
725727
qry->distinctClause=transformDistinctClause(pstate,
726728
stmt->distinctClause,
727729
&qry->targetList,
728-
&qry->sortClause);
730+
qry->sortClause);
729731

732+
/* transform LIMIT */
730733
qry->limitOffset=transformLimitClause(pstate,stmt->limitOffset,
731734
"OFFSET");
732735
qry->limitCount=transformLimitClause(pstate,stmt->limitCount,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp