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 :18momjian Exp $
6+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.76 2001/04/15 03:14 :18tgl 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);
120120static void get_setop_query (Node * setOp ,Query * query ,
121121deparse_context * context ,bool toplevel );
122122static 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 );
123126static void get_names_for_var (Var * var ,deparse_context * context ,
124127char * * refname ,char * * attname );
125128static bool get_alias_for_case (CaseExpr * caseexpr ,deparse_context * context ,
@@ -925,7 +928,7 @@ static void
925928get_select_query_def (Query * query ,deparse_context * context )
926929{
927930StringInfo buf = context -> buf ;
928- bool shortform_orderby ;
931+ bool force_colno ;
929932char * sep ;
930933List * l ;
931934
@@ -938,12 +941,12 @@ get_select_query_def(Query *query, deparse_context *context)
938941{
939942get_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}
943946else
944947{
945948get_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)
954957foreach (l ,query -> sortClause )
955958{
956959SortClause * srt = (SortClause * )lfirst (l );
957- TargetEntry * sorttle ;
958960char * opname ;
959961
960- sorttle = get_sortgroupclause_tle (srt ,
961- query -> targetList );
962962appendStringInfo (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 );
967965opname = get_opname (srt -> sortop );
968966if (strcmp (opname ,"<" )!= 0 )
969967{
@@ -1017,12 +1015,10 @@ get_basic_select_query(Query *query, deparse_context *context)
10171015foreach (l ,query -> distinctClause )
10181016{
10191017SortClause * srt = (SortClause * )lfirst (l );
1020- Node * sortexpr ;
10211018
1022- sortexpr = get_sortgroupclause_expr (srt ,
1023- query -> targetList );
10241019appendStringInfo (buf ,sep );
1025- get_rule_expr (sortexpr ,context );
1020+ get_rule_sortgroupclause (srt ,query -> targetList ,
1021+ false,context );
10261022sep = ", " ;
10271023}
10281024appendStringInfo (buf ,")" );
@@ -1082,12 +1078,10 @@ get_basic_select_query(Query *query, deparse_context *context)
10821078foreach (l ,query -> groupClause )
10831079{
10841080GroupClause * grp = (GroupClause * )lfirst (l );
1085- Node * groupexpr ;
10861081
1087- groupexpr = get_sortgroupclause_expr (grp ,
1088- query -> targetList );
10891082appendStringInfo (buf ,sep );
1090- get_rule_expr (groupexpr ,context );
1083+ get_rule_sortgroupclause (grp ,query -> targetList ,
1084+ false,context );
10911085sep = ", " ;
10921086}
10931087}
@@ -1182,6 +1176,32 @@ simple_distinct(List *distinctClause, List *targetList)
11821176return 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