3
3
* back to source text
4
4
*
5
5
* 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 $
7
7
*
8
8
* This software is copyrighted by Jan Wieck - Hamburg.
9
9
*
@@ -120,6 +120,9 @@ static void get_basic_select_query(Query *query, deparse_context *context);
120
120
static void get_setop_query (Node * setOp , Query * query ,
121
121
deparse_context * context , bool toplevel );
122
122
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 );
123
126
static void get_names_for_var (Var * var , deparse_context * context ,
124
127
char * * refname , char * * attname );
125
128
static bool get_alias_for_case (CaseExpr * caseexpr , deparse_context * context ,
@@ -925,7 +928,7 @@ static void
925
928
get_select_query_def (Query * query , deparse_context * context )
926
929
{
927
930
StringInfo buf = context -> buf ;
928
- bool shortform_orderby ;
931
+ bool force_colno ;
929
932
char * sep ;
930
933
List * l ;
931
934
@@ -938,12 +941,12 @@ get_select_query_def(Query *query, deparse_context *context)
938
941
{
939
942
get_setop_query (query -> setOperations , query , context , true);
940
943
/* ORDER BY clauses must be simple in this case */
941
- shortform_orderby = true;
944
+ force_colno = true;
942
945
}
943
946
else
944
947
{
945
948
get_basic_select_query (query , context );
946
- shortform_orderby = false;
949
+ force_colno = false;
947
950
}
948
951
949
952
/* Add the ORDER BY clause if given */
@@ -954,16 +957,11 @@ get_select_query_def(Query *query, deparse_context *context)
954
957
foreach (l , query -> sortClause )
955
958
{
956
959
SortClause * srt = (SortClause * ) lfirst (l );
957
- TargetEntry * sorttle ;
958
960
char * opname ;
959
961
960
- sorttle = get_sortgroupclause_tle (srt ,
961
- query -> targetList );
962
962
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 );
967
965
opname = get_opname (srt -> sortop );
968
966
if (strcmp (opname , "<" ) != 0 )
969
967
{
@@ -1017,12 +1015,10 @@ get_basic_select_query(Query *query, deparse_context *context)
1017
1015
foreach (l , query -> distinctClause )
1018
1016
{
1019
1017
SortClause * srt = (SortClause * ) lfirst (l );
1020
- Node * sortexpr ;
1021
1018
1022
- sortexpr = get_sortgroupclause_expr (srt ,
1023
- query -> targetList );
1024
1019
appendStringInfo (buf , sep );
1025
- get_rule_expr (sortexpr , context );
1020
+ get_rule_sortgroupclause (srt , query -> targetList ,
1021
+ false, context );
1026
1022
sep = ", " ;
1027
1023
}
1028
1024
appendStringInfo (buf , ")" );
@@ -1082,12 +1078,10 @@ get_basic_select_query(Query *query, deparse_context *context)
1082
1078
foreach (l , query -> groupClause )
1083
1079
{
1084
1080
GroupClause * grp = (GroupClause * ) lfirst (l );
1085
- Node * groupexpr ;
1086
1081
1087
- groupexpr = get_sortgroupclause_expr (grp ,
1088
- query -> targetList );
1089
1082
appendStringInfo (buf , sep );
1090
- get_rule_expr (groupexpr , context );
1083
+ get_rule_sortgroupclause (grp , query -> targetList ,
1084
+ false, context );
1091
1085
sep = ", " ;
1092
1086
}
1093
1087
}
@@ -1182,6 +1176,32 @@ simple_distinct(List *distinctClause, List *targetList)
1182
1176
return true;
1183
1177
}
1184
1178
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
+ }
1185
1205
1186
1206
/* ----------
1187
1207
* get_insert_query_def - Parse back an INSERT parsetree
0 commit comments