diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2003-06-16 02:03:38 +0000 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2003-06-16 02:03:38 +0000 |
commit | a4997254693057d4587d74222881b8e03580da2c (patch) | |
tree | c022e641ec691ce3f108b28e0720444da6ed3eef /src/backend/optimizer | |
parent | cb02610e503957d7ed9b4375537fb6275c16f1fa (diff) |
Allow GROUP BY, ORDER BY, DISTINCT targets to be unknown literals,
silently resolving them to type TEXT. This is comparable to what we
do when faced with UNKNOWN in CASE, UNION, and other contexts. It gets
rid of this and related annoyances:
select distinct f1, '' from int4_tbl;
ERROR: Unable to identify an ordering operator '<' for type unknown
This was discussed many moons ago, but no one got round to fixing it.
Diffstat (limited to 'src/backend/optimizer')
-rw-r--r-- | src/backend/optimizer/plan/createplan.c | 4 | ||||
-rw-r--r-- | src/backend/optimizer/plan/planner.c | 39 | ||||
-rw-r--r-- | src/backend/optimizer/prep/prepunion.c | 6 |
3 files changed, 33 insertions, 16 deletions
diff --git a/src/backend/optimizer/plan/createplan.c b/src/backend/optimizer/plan/createplan.c index 273a80129eb..81ee7962df9 100644 --- a/src/backend/optimizer/plan/createplan.c +++ b/src/backend/optimizer/plan/createplan.c @@ -10,7 +10,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/optimizer/plan/createplan.c,v 1.145 2003/06/15 22:51:45 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/optimizer/plan/createplan.c,v 1.146 2003/06/16 02:03:37 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -583,7 +583,7 @@ create_unique_plan(Query *root, UniquePath *best_path) { List *sortList; - sortList = addAllTargetsToSortList(NIL, my_tlist); + sortList = addAllTargetsToSortList(NULL, NIL, my_tlist, false); plan = (Plan *) make_sort_from_sortclauses(root, my_tlist, subplan, sortList); plan = (Plan *) make_unique(my_tlist, plan, sortList); diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c index fdb5519862f..f55d0cad5d1 100644 --- a/src/backend/optimizer/plan/planner.c +++ b/src/backend/optimizer/plan/planner.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planner.c,v 1.154 2003/06/06 15:04:02 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planner.c,v 1.155 2003/06/16 02:03:37 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -894,6 +894,24 @@ grouping_planner(Query *parse, double tuple_fraction) if (parse->groupClause) { List *groupExprs; + double cheapest_path_rows; + int cheapest_path_width; + + /* + * Beware in this section of the possibility that + * cheapest_path->parent is NULL. This could happen if user + * does something silly like SELECT 'foo' GROUP BY 1; + */ + if (cheapest_path->parent) + { + cheapest_path_rows = cheapest_path->parent->rows; + cheapest_path_width = cheapest_path->parent->width; + } + else + { + cheapest_path_rows = 1; /* assume non-set result */ + cheapest_path_width = 100; /* arbitrary */ + } /* * Always estimate the number of groups. We can't do this until @@ -903,7 +921,7 @@ grouping_planner(Query *parse, double tuple_fraction) parse->targetList); dNumGroups = estimate_num_groups(parse, groupExprs, - cheapest_path->parent->rows); + cheapest_path_rows); /* Also want it as a long int --- but 'ware overflow! */ numGroups = (long) Min(dNumGroups, (double) LONG_MAX); @@ -936,8 +954,7 @@ grouping_planner(Query *parse, double tuple_fraction) * assume it is 100 bytes. Also set the overhead per hashtable * entry at 64 bytes. */ - int hashentrysize = cheapest_path->parent->width + 64 + - numAggs * 100; + int hashentrysize = cheapest_path_width + 64 + numAggs * 100; if (hashentrysize * dNumGroups <= SortMem * 1024L) { @@ -964,13 +981,13 @@ grouping_planner(Query *parse, double tuple_fraction) numGroupCols, dNumGroups, cheapest_path->startup_cost, cheapest_path->total_cost, - cheapest_path->parent->rows); + cheapest_path_rows); /* Result of hashed agg is always unsorted */ if (sort_pathkeys) cost_sort(&hashed_p, parse, sort_pathkeys, hashed_p.total_cost, dNumGroups, - cheapest_path->parent->width); + cheapest_path_width); if (sorted_path) { @@ -989,8 +1006,8 @@ grouping_planner(Query *parse, double tuple_fraction) { cost_sort(&sorted_p, parse, group_pathkeys, sorted_p.total_cost, - cheapest_path->parent->rows, - cheapest_path->parent->width); + cheapest_path_rows, + cheapest_path_width); current_pathkeys = group_pathkeys; } if (parse->hasAggs) @@ -999,13 +1016,13 @@ grouping_planner(Query *parse, double tuple_fraction) numGroupCols, dNumGroups, sorted_p.startup_cost, sorted_p.total_cost, - cheapest_path->parent->rows); + cheapest_path_rows); else cost_group(&sorted_p, parse, numGroupCols, dNumGroups, sorted_p.startup_cost, sorted_p.total_cost, - cheapest_path->parent->rows); + cheapest_path_rows); /* The Agg or Group node will preserve ordering */ if (sort_pathkeys && !pathkeys_contained_in(sort_pathkeys, @@ -1014,7 +1031,7 @@ grouping_planner(Query *parse, double tuple_fraction) cost_sort(&sorted_p, parse, sort_pathkeys, sorted_p.total_cost, dNumGroups, - cheapest_path->parent->width); + cheapest_path_width); } /* diff --git a/src/backend/optimizer/prep/prepunion.c b/src/backend/optimizer/prep/prepunion.c index 86a52645fe6..ac6e0f0f352 100644 --- a/src/backend/optimizer/prep/prepunion.c +++ b/src/backend/optimizer/prep/prepunion.c @@ -14,7 +14,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/optimizer/prep/prepunion.c,v 1.95 2003/05/06 00:20:32 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/optimizer/prep/prepunion.c,v 1.96 2003/06/16 02:03:37 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -239,7 +239,7 @@ generate_union_plan(SetOperationStmt *op, Query *parse, List *sortList; tlist = copyObject(tlist); - sortList = addAllTargetsToSortList(NIL, tlist); + sortList = addAllTargetsToSortList(NULL, NIL, tlist, false); plan = (Plan *) make_sort_from_sortclauses(parse, tlist, plan, sortList); plan = (Plan *) make_unique(tlist, plan, sortList); @@ -293,7 +293,7 @@ generate_nonunion_plan(SetOperationStmt *op, Query *parse, * correct output. */ tlist = copyObject(tlist); - sortList = addAllTargetsToSortList(NIL, tlist); + sortList = addAllTargetsToSortList(NULL, NIL, tlist, false); plan = (Plan *) make_sort_from_sortclauses(parse, tlist, plan, sortList); switch (op->op) { |