Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ffbeafb

Browse files
committed
Constant expressions that appear in ORDER BY, GROUP BY, DISTINCT ON
lists should be reverse-compiled into targetlist index numbers, because that's the only interpretation the parser allows for a constant in these clauses. (Ergo, the only way they could have gotten into the list in the first place is to have come from the targetlist; so this should always work.) Per problem report from Peter E.
1 parent 7f2de49 commit ffbeafb

File tree

1 file changed

+39
-19
lines changed

1 file changed

+39
-19
lines changed

src/backend/utils/adt/ruleutils.c

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* back to source text
44
*
55
* IDENTIFICATION
6-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.75 2001/03/22 06:16:18 momjian Exp $
6+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.76 2001/04/15 03:14:18 tgl Exp $
77
*
88
* This software is copyrighted by Jan Wieck - Hamburg.
99
*
@@ -120,6 +120,9 @@ static void get_basic_select_query(Query *query, deparse_context *context);
120120
static void get_setop_query(Node *setOp, Query *query,
121121
deparse_context *context, bool toplevel);
122122
static bool simple_distinct(List *distinctClause, List *targetList);
123+
static void get_rule_sortgroupclause(SortClause *srt, List *tlist,
124+
bool force_colno,
125+
deparse_context *context);
123126
static void get_names_for_var(Var *var, deparse_context *context,
124127
char **refname, char **attname);
125128
static bool get_alias_for_case(CaseExpr *caseexpr, deparse_context *context,
@@ -925,7 +928,7 @@ static void
925928
get_select_query_def(Query *query, deparse_context *context)
926929
{
927930
StringInfo buf = context->buf;
928-
bool shortform_orderby;
931+
bool force_colno;
929932
char *sep;
930933
List *l;
931934

@@ -938,12 +941,12 @@ get_select_query_def(Query *query, deparse_context *context)
938941
{
939942
get_setop_query(query->setOperations, query, context, true);
940943
/* ORDER BY clauses must be simple in this case */
941-
shortform_orderby = true;
944+
force_colno = true;
942945
}
943946
else
944947
{
945948
get_basic_select_query(query, context);
946-
shortform_orderby = false;
949+
force_colno = false;
947950
}
948951

949952
/* Add the ORDER BY clause if given */
@@ -954,16 +957,11 @@ get_select_query_def(Query *query, deparse_context *context)
954957
foreach(l, query->sortClause)
955958
{
956959
SortClause *srt = (SortClause *) lfirst(l);
957-
TargetEntry *sorttle;
958960
char *opname;
959961

960-
sorttle = get_sortgroupclause_tle(srt,
961-
query->targetList);
962962
appendStringInfo(buf, sep);
963-
if (shortform_orderby)
964-
appendStringInfo(buf, "%d", sorttle->resdom->resno);
965-
else
966-
get_rule_expr(sorttle->expr, context);
963+
get_rule_sortgroupclause(srt, query->targetList,
964+
force_colno, context);
967965
opname = get_opname(srt->sortop);
968966
if (strcmp(opname, "<") != 0)
969967
{
@@ -1017,12 +1015,10 @@ get_basic_select_query(Query *query, deparse_context *context)
10171015
foreach(l, query->distinctClause)
10181016
{
10191017
SortClause *srt = (SortClause *) lfirst(l);
1020-
Node *sortexpr;
10211018

1022-
sortexpr = get_sortgroupclause_expr(srt,
1023-
query->targetList);
10241019
appendStringInfo(buf, sep);
1025-
get_rule_expr(sortexpr, context);
1020+
get_rule_sortgroupclause(srt, query->targetList,
1021+
false, context);
10261022
sep = ", ";
10271023
}
10281024
appendStringInfo(buf, ")");
@@ -1082,12 +1078,10 @@ get_basic_select_query(Query *query, deparse_context *context)
10821078
foreach(l, query->groupClause)
10831079
{
10841080
GroupClause *grp = (GroupClause *) lfirst(l);
1085-
Node *groupexpr;
10861081

1087-
groupexpr = get_sortgroupclause_expr(grp,
1088-
query->targetList);
10891082
appendStringInfo(buf, sep);
1090-
get_rule_expr(groupexpr, context);
1083+
get_rule_sortgroupclause(grp, query->targetList,
1084+
false, context);
10911085
sep = ", ";
10921086
}
10931087
}
@@ -1182,6 +1176,32 @@ simple_distinct(List *distinctClause, List *targetList)
11821176
return true;
11831177
}
11841178

1179+
/*
1180+
* Display a sort/group clause.
1181+
*/
1182+
static void
1183+
get_rule_sortgroupclause(SortClause *srt, List *tlist, bool force_colno,
1184+
deparse_context *context)
1185+
{
1186+
StringInfo buf = context->buf;
1187+
TargetEntry *tle;
1188+
Node *expr;
1189+
1190+
tle = get_sortgroupclause_tle(srt, tlist);
1191+
expr = tle->expr;
1192+
/*
1193+
* Use column-number form if requested by caller or if expression is a
1194+
* constant --- a constant is ambiguous (and will be misinterpreted
1195+
* by findTargetlistEntry()) if we dump it explicitly.
1196+
*/
1197+
if (force_colno || (expr && IsA(expr, Const)))
1198+
{
1199+
Assert(!tle->resdom->resjunk);
1200+
appendStringInfo(buf, "%d", tle->resdom->resno);
1201+
}
1202+
else
1203+
get_rule_expr(expr, context);
1204+
}
11851205

11861206
/* ----------
11871207
* get_insert_query_def - Parse back an INSERT parsetree

0 commit comments

Comments
 (0)