1
0
mirror of https://github.com/postgres/postgres.git synced 2025-06-14 18:42:34 +03:00

Fix optimizer to not try to push WHERE clauses down into a sub-SELECT that

has a DISTINCT ON clause, per bug report from Anthony Wood.  While at it,
improve the DISTINCT-ON-clause recognizer routine to not be fooled by out-
of-order DISTINCT lists.
This commit is contained in:
Tom Lane
2001-07-31 17:56:31 +00:00
parent 6fbf442d5a
commit 421467cdc8
4 changed files with 70 additions and 40 deletions

View File

@ -3,7 +3,7 @@
* back to source text
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.80 2001/07/16 05:06:59 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.81 2001/07/31 17:56:31 tgl Exp $
*
* This software is copyrighted by Jan Wieck - Hamburg.
*
@ -119,7 +119,6 @@ static void get_utility_query_def(Query *query, deparse_context *context);
static void get_basic_select_query(Query *query, deparse_context *context);
static void get_setop_query(Node *setOp, Query *query,
deparse_context *context, bool toplevel);
static bool simple_distinct(List *distinctClause, List *targetList);
static void get_rule_sortgroupclause(SortClause *srt, List *tlist,
bool force_colno,
deparse_context *context);
@ -1064,9 +1063,7 @@ get_basic_select_query(Query *query, deparse_context *context)
/* Add the DISTINCT clause if given */
if (query->distinctClause != NIL)
{
if (simple_distinct(query->distinctClause, query->targetList))
appendStringInfo(buf, " DISTINCT");
else
if (has_distinct_on_clause(query))
{
appendStringInfo(buf, " DISTINCT ON (");
sep = "";
@ -1081,6 +1078,8 @@ get_basic_select_query(Query *query, deparse_context *context)
}
appendStringInfo(buf, ")");
}
else
appendStringInfo(buf, " DISTINCT");
}
/* Then we tell what to select (the targetlist) */
@ -1207,34 +1206,6 @@ get_setop_query(Node *setOp, Query *query, deparse_context *context,
}
}
/*
* Detect whether a DISTINCT list can be represented as just DISTINCT
* or needs DISTINCT ON. It's simple if it contains exactly the nonjunk
* targetlist items.
*/
static bool
simple_distinct(List *distinctClause, List *targetList)
{
while (targetList)
{
TargetEntry *tle = (TargetEntry *) lfirst(targetList);
if (!tle->resdom->resjunk)
{
if (distinctClause == NIL)
return false;
if (((SortClause *) lfirst(distinctClause))->tleSortGroupRef !=
tle->resdom->ressortgroupref)
return false;
distinctClause = lnext(distinctClause);
}
targetList = lnext(targetList);
}
if (distinctClause != NIL)
return false;
return true;
}
/*
* Display a sort/group clause.
*/