@@ -371,26 +371,29 @@ static void make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
371
371
static void make_viewdef (StringInfo buf , HeapTuple ruletup , TupleDesc rulettc ,
372
372
int prettyFlags , int wrapColumn );
373
373
static void get_query_def (Query * query , StringInfo buf , List * parentnamespace ,
374
- TupleDesc resultDesc ,
374
+ TupleDesc resultDesc , bool colNamesVisible ,
375
375
int prettyFlags , int wrapColumn , int startIndent );
376
376
static void get_values_def (List * values_lists , deparse_context * context );
377
377
static void get_with_clause (Query * query , deparse_context * context );
378
378
static void get_select_query_def (Query * query , deparse_context * context ,
379
- TupleDesc resultDesc );
380
- static void get_insert_query_def (Query * query , deparse_context * context );
381
- static void get_update_query_def (Query * query , deparse_context * context );
379
+ TupleDesc resultDesc , bool colNamesVisible );
380
+ static void get_insert_query_def (Query * query , deparse_context * context ,
381
+ bool colNamesVisible );
382
+ static void get_update_query_def (Query * query , deparse_context * context ,
383
+ bool colNamesVisible );
382
384
static void get_update_query_targetlist_def (Query * query , List * targetList ,
383
385
deparse_context * context ,
384
386
RangeTblEntry * rte );
385
- static void get_delete_query_def (Query * query , deparse_context * context );
387
+ static void get_delete_query_def (Query * query , deparse_context * context ,
388
+ bool colNamesVisible );
386
389
static void get_utility_query_def (Query * query , deparse_context * context );
387
390
static void get_basic_select_query (Query * query , deparse_context * context ,
388
- TupleDesc resultDesc );
391
+ TupleDesc resultDesc , bool colNamesVisible );
389
392
static void get_target_list (List * targetList , deparse_context * context ,
390
- TupleDesc resultDesc );
393
+ TupleDesc resultDesc , bool colNamesVisible );
391<
8000
/code>
394
static void get_setop_query (Node * setOp , Query * query ,
392
395
deparse_context * context ,
393
- TupleDesc resultDesc );
396
+ TupleDesc resultDesc , bool colNamesVisible );
394
397
static Node * get_rule_sortgroupclause (Index ref , List * tlist ,
395
398
bool force_colno ,
396
399
deparse_context * context );
@@ -4899,7 +4902,7 @@ make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
4899
4902
foreach (action , actions )
4900
4903
{
4901
4904
query = (Query * ) lfirst (action );
4902
- get_query_def (query , buf , NIL , viewResultDesc ,
4905
+ get_query_def (query , buf , NIL , viewResultDesc , true,
4903
4906
prettyFlags , WRAP_COLUMN_DEFAULT , 0 );
4904
4907
if (prettyFlags )
4905
4908
appendStringInfoString (buf , ";\n" );
@@ -4917,7 +4920,7 @@ make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
4917
4920
Query * query ;
4918
4921
4919
4922
query = (Query * ) linitial (actions );
4920
- get_query_def (query , buf , NIL , viewResultDesc ,
4923
+ get_query_def (query , buf , NIL , viewResultDesc , true,
4921
4924
prettyFlags , WRAP_COLUMN_DEFAULT , 0 );
4922
4925
appendStringInfoChar (buf , ';' );
4923
4926
}
@@ -4991,7 +4994,7 @@ make_viewdef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
4991
4994
4992
4995
ev_relation = heap_open (ev_class , AccessShareLock );
4993
4996
4994
- get_query_def (query , buf , NIL , RelationGetDescr (ev_relation ),
4997
+ get_query_def (query , buf , NIL , RelationGetDescr (ev_relation ), true,
4995
4998
prettyFlags , wrapColumn , 0 );
4996
4999
appendStringInfoChar (buf , ';' );
4997
5000
@@ -5002,13 +5005,23 @@ make_viewdef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
5002
5005
/* ----------
5003
5006
* get_query_def - Parse back one query parsetree
5004
5007
*
5005
- * If resultDesc is not NULL, then it is the output tuple descriptor for
5006
- * the view represented by a SELECT query.
5008
+ * query: parsetree to be displayed
5009
+ * buf: output text is appended to buf
5010
+ * parentnamespace: list (initially empty) of outer-level deparse_namespace's
5011
+ * resultDesc: if not NULL, the output tuple descriptor for the view
5012
+ * represented by a SELECT query. We use the column names from it
5013
+ * to label SELECT output columns, in preference to names in the query
5014
+ * colNamesVisible: true if the surrounding context cares about the output
5015
+ * column names at all (as, for example, an EXISTS() context does not);
5016
+ * when false, we can suppress dummy column labels such as "?column?"
5017
+ * prettyFlags: bitmask of PRETTYFLAG_XXX options
5018
+ * wrapColumn: maximum line length, or -1 to disable wrapping
5019
+ * startIndent: initial indentation amount
5007
5020
* ----------
5008
5021
*/
5009
5022
static void
5010
5023
get_query_def (Query * query , StringInfo buf , List * parentnamespace ,
5011
- TupleDesc resultDesc ,
5024
+ TupleDesc resultDesc , bool colNamesVisible ,
5012
5025
int prettyFlags , int wrapColumn , int startIndent )
5013
5026
{
5014
5027
deparse_context context ;
@@ -5045,19 +5058,19 @@ get_query_def(Query *query, StringInfo buf, List *parentnamespace,
5045
5058
switch (query -> commandType )
5046
5059
{
5047
5060
case CMD_SELECT :
5048
- get_select_query_def (query , & context , resultDesc );
5061
+ get_select_query_def (query , & context , resultDesc , colNamesVisible );
5049
5062
break ;
5050
5063
5051
5064
case CMD_UPDATE :
5052
- get_update_query_def (query , & context );
5065
+ get_update_query_def (query , & context , colNamesVisible );
5053
5066
break ;
5054
5067
5055
5068
case CMD_INSERT :
5056
- get_insert_query_def (query , & context );
5069
+ get_insert_query_def (query , & context , colNamesVisible );
5057
5070
break ;
5058
5071
5059
5072
case CMD_DELETE :
5060
- get_delete_query_def (query , & context );
5073
+ get_delete_query_def (query , & context , colNamesVisible );
5061
5074
break ;
5062
5075
5063
5076
case CMD_NOTHING :
@@ -5169,6 +5182,7 @@ get_with_clause(Query *query, deparse_context *context)
5169
5182
if (PRETTY_INDENT (context ))
5170
5183
appendContextKeyword (context , "" , 0 , 0 , 0 );
5171
5184
get_query_def ((Query * ) cte -> ctequery , buf , context -> namespaces , NULL ,
5185
+ true,
5172
5186
context -> prettyFlags , context -> wrapColumn ,
5173
5187
context -> indentLevel );
5174
5188
if (PRETTY_INDENT (context ))
@@ -5192,7 +5206,7 @@ get_with_clause(Query *query, deparse_context *context)
5192
5206
*/
5193
5207
static void
5194
5208
get_select_query_def (Query * query , deparse_context * context ,
5195
- TupleDesc resultDesc )
5209
+ TupleDesc resultDesc , bool colNamesVisible )
5196
5210
{
5197
5211
StringInfo buf = context -> buf ;
5198
5212
List * save_windowclause ;
@@ -5216,13 +5230,14 @@ get_select_query_def(Query *query, deparse_context *context,
5216
5230
*/
5217
5231
if (query -> setOperations )
5218
5232
{
5219
- get_setop_query (query -> setOperations , query , context , resultDesc );
5233
+ get_setop_query (query -> setOperations , query , context , resultDesc ,
5234
+ colNamesVisible );
5220
5235
/* ORDER BY clauses must be simple in this case */
5221
5236
force_colno = true;
5222
5237
}
5223
5238
else
5224
5239
{
5225
- get_basic_select_query (query , context , resultDesc );
5240
+ get_basic_select_query (query , context , resultDesc , colNamesVisible );
5226
5241
force_colno = false;
5227
5242
}
5228
5243
@@ -5379,7 +5394,7 @@ get_simple_values_rte(Query *query, TupleDesc resultDesc)
5379
5394
5380
5395
static void
5381
5396
get_basic_select_query (Query * query , deparse_context * context ,
5382
- TupleDesc resultDesc )
5397
+ TupleDesc resultDesc , bool colNamesVisible )
5383
5398
{
5384
5399
StringInfo buf = context -> buf ;
5385
5400
RangeTblEntry * values_rte ;
@@ -5432,7 +5447,7 @@ get_basic_select_query(Query *query, deparse_context *context,
5432
5447
}
5433
5448
5434
5449
/* Then we tell what to select (the targetlist) */
5435
- get_target_list (query -> targetList , context , resultDesc );
5450
+ get_target_list (query -> targetList , context , resultDesc , colNamesVisible );
5436
5451
5437
5452
/* Add the FROM clause if needed */
5438
5453
get_from_clause (query , " FROM " , context );
@@ -5502,11 +5517,13 @@ get_basic_select_query(Query *query, deparse_context *context,
5502
5517
* get_target_list - Parse back a SELECT target list
5503
5518
*
5504
5519
* This is also used for RETURNING lists in INSERT/UPDATE/DELETE.
5520
+ *
5521
+ * resultDesc and colNamesVisible are as for get_query_def()
5505
5522
* ----------
5506
5523
*/
5507
5524
static void
5508
5525
get_target_list (List * targetList , deparse_context * context ,
5509
- TupleDesc resultDesc )
5526
+ TupleDesc resultDesc , bool colNamesVisible )
5510
5527
{
5511
5528
StringInfo buf = context -> buf ;
5512
5529
StringInfoData targetbuf ;
@@ -5557,8 +5574,13 @@ get_target_list(List *targetList, deparse_context *context,
5557
5574
else
5558
5575
{
5559
5576
get_rule_expr ((Node * ) tle -> expr , context , true);
5560
- /* We'll show the AS name unless it's this: */
5561
- attname = "?column?" ;
5577
+
5578
+ /*
5579
+ * When colNamesVisible is true, we should always show the
5580
+ * assigned column name explicitly. Otherwise, show it only if
5581
+ * it's not FigureColname's fallback.
5582
+ */
5583
+ attname = colNamesVisible ? NULL : "?column?" ;
5562
5584
}
5563
5585
5564
5586
/*
@@ -5637,7 +5659,7 @@ get_target_list(List *targetList, deparse_context *context,
5637
5659
5638
5660
static void
5639
5661
get_setop_query (Node * setOp , Query * query , deparse_context * context ,
5640
- TupleDesc resultDesc )
5662
+ TupleDesc resultDesc , bool colNamesVisible )
5641
5663
{
5642
5664
StringInfo buf = context -> buf ;
5643
5665
bool need_paren ;
@@ -5663,6 +5685,7 @@ get_setop_query(Node *setOp, Query *query, deparse_context *context,
5663
5685
if (need_paren )
5664
5686
appendStringInfoChar (buf , '(' );
5665
5687
get_query_def (subquery , buf , context -> namespaces , resultDesc ,
5688
+ colNamesVisible ,
5666
5689
context -> prettyFlags , context -> wrapColumn ,
5667
5690
context -> indentLevel );
5668
5691
if (need_paren )
@@ -5705,7 +5728,7 @@ get_setop_query(Node *setOp, Query *query, deparse_context *context,
5705
5728
else
5706
5729
subindent = 0 ;
5707
5730
5708
- get_setop_query (op -> larg , query , context , resultDesc );
5731
+ get_setop_query (op -> larg , query , context , resultDesc , colNamesVisible );
5709
5732
5710
5733
if (need_paren )
5711
5734
appendContextKeyword (context , ") " , - subindent , 0 , 0 );
@@ -5749,7 +5772,7 @@ get_setop_query(Node *setOp, Query *query, deparse_context *context,
5749
5772
subindent = 0 ;
5750
5773
appendContextKeyword (context , "" , subindent , 0 , 0 );
5751
5774
5752
- get_setop_query (op -> rarg , query , context , resultDesc );
5775
+ get_setop_query (op -> rarg , query , context , resultDesc , false );
5753
5776
5754
5777
if (PRETTY_INDENT (context ))
5755
5778
context -> indentLevel -= subindent ;
@@ -6084,7 +6107,8 @@ get_rule_windowspec(WindowClause *wc, List *targetList,
6084
6107
* ----------
6085
6108
*/
6086
6109
static void
6087
- get_insert_query_def (Query * query , deparse_context * context )
6110
+ get_insert_query_def (Query * query , deparse_context * context ,
6111
+ bool colNamesVisible )
6088
6112
{
6089
6113
StringInfo buf = context -> buf ;
6090
6114
RangeTblEntry * select_rte = NULL ;
@@ -6194,6 +6218,7 @@ get_insert_query_def(Query *query, deparse_context *context)
6194
6218
{
6195
6219
/* Add the SELECT */
6196
6220
get_query_def (select_rte -> subquery , buf , NIL , NULL ,
6221
+ false,
6197
6222
context -> prettyFlags , context -> wrapColumn ,
6198
6223
context -> indentLevel );
6199
6224
}
@@ -6287,7 +6312,7 @@ get_insert_query_def(Query *query, deparse_context *context)
6287
6312
{
6288
6313
appendContextKeyword (context , " RETURNING" ,
6289
6314
- PRETTYINDENT_STD , PRETTYINDENT_STD , 1 );
6290
- get_target_list (query -> returningList , context , NULL );
6315
+ get_target_list (query -> returningList , context , NULL , colNamesVisible );
6291
6316
}
6292
6317
}
6293
6318
@@ -6297,7 +6322,8 @@ get_insert_query_def(Query *query, deparse_context *context)
6297
6322
* ----------
6298
6323
*/
6299
6324
static void
6300
- get_update_query_def (Query * query , deparse_context * context )
6325
+ get_update_query_def (Query * query , deparse_context * context ,
6326
+ bool colNamesVisible )
6301
6327
{
6302
6328
StringInfo buf = context -> buf ;
6303
6329
RangeTblEntry * rte ;
@@ -6342,7 +6368,7 @@ get_update_query_def(Query *query, deparse_context *context)
6342
6368
{
6343
6369
appendContextKeyword (context , " RETURNING" ,
6344
6370
- PRETTYINDENT_STD , PRETTYINDENT_STD , 1 );
6345
- get_target_list (query -> returningList , context , NULL );
6371
+ get_target_list (query -> returningList , context , NULL , colNamesVisible );
6346
6372
}
6347
6373
}
6348
6374
@@ -6504,7 +6530,8 @@ get_update_query_targetlist_def(Query *query, List *targetList,
6504
6530
* ----------
6505
6531
*/
6506
6532
static void
6507
- get_delete_query_def (Query * query , deparse_context * context )
6533
+ get_delete_query_def (Query * query , deparse_context * context ,
6534
+ bool colNamesVisible )
6508
6535
{
6509
6536
StringInfo buf = context -> buf ;
6510
6537
RangeTblEntry * rte ;
@@ -6545,7 +6572,7 @@ get_delete_query_def(Query *query, deparse_context *context)
6545
6572
{
6546
6573
appendContextKeyword (context , " RETURNING" ,
6547
6574
- PRETTYINDENT_STD , PRETTYINDENT_STD , 1 );
6548
- get_target_list (query -> returningList , context , NULL );
6575
+ get_target_list (query -> returningList , context , NULL , colNamesVisible );
6549
6576
}
6550
6577
}
6551
6578
@@ -9745,7 +9772,7 @@ get_sublink_expr(SubLink *sublink, deparse_context *context)
9745
9772
if (need_paren )
9746
9773
appendStringInfoChar (buf , '(' );
9747
9774
9748
- get_query_def (query , buf , context -> namespaces , NULL ,
9775
+ get_query_def (query , buf , context -> namespaces , NULL , false,
9749
9776
context -> prettyFlags , context -> wrapColumn ,
9750
9777
context -> indentLevel );
9751
9778
@@ -10004,6 +10031,7 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
10004
10031
/* Subquery RTE */
10005
10032
appendStringInfoChar (buf , '(' );
10006
10033
get_query_def (rte -> subquery , buf , context -> namespaces , NULL ,
10034
+ true,
10007
10035
context -> prettyFlags , context -> wrapColumn ,
10008
10036
context -> indentLevel );
10009
10037
appendStringInfoCha
B3A7
r (buf , ')' );
0 commit comments