|
8 | 8 | * |
9 | 9 | * |
10 | 10 | * IDENTIFICATION |
11 | | - * $PostgreSQL: pgsql/src/backend/optimizer/plan/planner.c,v 1.228 2008/03/27 19:06:14 tgl Exp $ |
| 11 | + * $PostgreSQL: pgsql/src/backend/optimizer/plan/planner.c,v 1.229 2008/03/28 02:00:11 tgl Exp $ |
12 | 12 | * |
13 | 13 | *------------------------------------------------------------------------- |
14 | 14 | */ |
@@ -835,6 +835,21 @@ grouping_planner(PlannerInfo *root, double tuple_fraction) |
835 | 835 |
|
836 | 836 | MemSet(&agg_counts,0,sizeof(AggClauseCounts)); |
837 | 837 |
|
| 838 | +/* |
| 839 | + * If the query involves ungrouped aggregation, then it can produce |
| 840 | + * at most one row, so we can ignore any ORDER BY or DISTINCT |
| 841 | + * request. This isn't all that exciting as an optimization, but it |
| 842 | + * prevents a corner case when optimize_minmax_aggregates succeeds: |
| 843 | + * if ORDER BY or DISTINCT were present we'd try, and fail, to match |
| 844 | + * the EquivalenceClasses we're about to build with the modified |
| 845 | + * targetlist entries it will create. |
| 846 | + */ |
| 847 | +if (parse->hasAggs&&parse->groupClause==NIL) |
| 848 | +{ |
| 849 | +parse->sortClause=NIL; |
| 850 | +parse->distinctClause=NIL; |
| 851 | +} |
| 852 | + |
838 | 853 | /* Preprocess targetlist */ |
839 | 854 | tlist=preprocess_targetlist(root,tlist); |
840 | 855 |
|
@@ -950,17 +965,6 @@ grouping_planner(PlannerInfo *root, double tuple_fraction) |
950 | 965 | * right tlist, and it has no sort order. |
951 | 966 | */ |
952 | 967 | current_pathkeys=NIL; |
953 | | -/* |
954 | | - * In fact, since we don't optimize grouped aggregates, it |
955 | | - * needs no sort order --- there must be exactly one output row, |
956 | | - * and so any ORDER BY or DISTINCT attached to the query is |
957 | | - * useless and can be dropped. Aside from saving useless cycles, |
958 | | - * this protects us against problems with matching the hacked-up |
959 | | - * tlist entries to sort clauses. |
960 | | - */ |
961 | | -Assert(!parse->groupClause); |
962 | | -parse->sortClause=NULL; |
963 | | -parse->distinctClause=NULL; |
964 | 968 | } |
965 | 969 | else |
966 | 970 | { |
|