LCOV - code coverage report
Current view: top level - src/backend/nodes - nodeFuncs.c (source / functions) Coverage Total Hit
Test: PostgreSQL 19devel Lines: 78.5 % 2787 2187
Test Date: 2026-05-07 21:16:36 Functions: 100.0 % 32 32
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /*-------------------------------------------------------------------------
       2              :  *
       3              :  * nodeFuncs.c
       4              :  *      Various general-purpose manipulations of Node trees
       5              :  *
       6              :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
       7              :  * Portions Copyright (c) 1994, Regents of the University of California
       8              :  *
       9              :  *
      10              :  * IDENTIFICATION
      11              :  *    src/backend/nodes/nodeFuncs.c
      12              :  *
      13              :  *-------------------------------------------------------------------------
      14              :  */
      15              : #include "postgres.h"
      16              : 
      17              : #include "catalog/pg_collation.h"
      18              : #include "catalog/pg_type.h"
      19              : #include "miscadmin.h"
      20              : #include "nodes/execnodes.h"
      21              : #include "nodes/nodeFuncs.h"
      22              : #include "nodes/pathnodes.h"
      23              : #include "utils/builtins.h"
      24              : #include "utils/lsyscache.h"
      25              : 
      26              : static bool expression_returns_set_walker(Node *node, void *context);
      27              : static int  leftmostLoc(int loc1, int loc2);
      28              : static bool fix_opfuncids_walker(Node *node, void *context);
      29              : static bool planstate_walk_subplans(List *plans,
      30              :                                     planstate_tree_walker_callback walker,
      31              :                                     void *context);
      32              : static bool planstate_walk_members(PlanState **planstates, int nplans,
      33              :                                    planstate_tree_walker_callback walker,
      34              :                                    void *context);
      35              : 
      36              : 
      37              : /*
      38              :  *  exprType -
      39              :  *    returns the Oid of the type of the expression's result.
      40              :  */
      41              : Oid
      42     22269724 : exprType(const Node *expr)
      43              : {
      44              :     Oid         type;
      45              : 
      46     22269724 :     if (!expr)
      47          308 :         return InvalidOid;
      48              : 
      49     22269416 :     switch (nodeTag(expr))
      50              :     {
      51     12612867 :         case T_Var:
      52     12612867 :             type = ((const Var *) expr)->vartype;
      53     12612867 :             break;
      54      3178782 :         case T_Const:
      55      3178782 :             type = ((const Const *) expr)->consttype;
      56      3178782 :             break;
      57      1841100 :         case T_Param:
      58      1841100 :             type = ((const Param *) expr)->paramtype;
      59      1841100 :             break;
      60       223493 :         case T_Aggref:
      61       223493 :             type = ((const Aggref *) expr)->aggtype;
      62       223493 :             break;
      63         1759 :         case T_GroupingFunc:
      64         1759 :             type = INT4OID;
      65         1759 :             break;
      66        15258 :         case T_WindowFunc:
      67        15258 :             type = ((const WindowFunc *) expr)->wintype;
      68        15258 :             break;
      69          716 :         case T_MergeSupportFunc:
      70          716 :             type = ((const MergeSupportFunc *) expr)->msftype;
      71          716 :             break;
      72       104791 :         case T_SubscriptingRef:
      73       104791 :             type = ((const SubscriptingRef *) expr)->refrestype;
      74       104791 :             break;
      75      1382632 :         case T_FuncExpr:
      76      1382632 :             type = ((const FuncExpr *) expr)->funcresulttype;
      77      1382632 :             break;
      78        52912 :         case T_NamedArgExpr:
      79        52912 :             type = exprType((Node *) ((const NamedArgExpr *) expr)->arg);
      80        52912 :             break;
      81      1016627 :         case T_OpExpr:
      82      1016627 :             type = ((const OpExpr *) expr)->opresulttype;
      83      1016627 :             break;
      84         1734 :         case T_DistinctExpr:
      85         1734 :             type = ((const DistinctExpr *) expr)->opresulttype;
      86         1734 :             break;
      87         1787 :         case T_NullIfExpr:
      88         1787 :             type = ((const NullIfExpr *) expr)->opresulttype;
      89         1787 :             break;
      90        76659 :         case T_ScalarArrayOpExpr:
      91        76659 :             type = BOOLOID;
      92        76659 :             break;
      93       242319 :         case T_BoolExpr:
      94       242319 :             type = BOOLOID;
      95       242319 :             break;
      96        76824 :         case T_SubLink:
      97              :             {
      98        76824 :                 const SubLink *sublink = (const SubLink *) expr;
      99              : 
     100        76824 :                 if (sublink->subLinkType == EXPR_SUBLINK ||
     101        32394 :                     sublink->subLinkType == ARRAY_SUBLINK)
     102        56415 :                 {
     103              :                     /* get the type of the subselect's first target column */
     104        56415 :                     Query      *qtree = (Query *) sublink->subselect;
     105              :                     TargetEntry *tent;
     106              : 
     107        56415 :                     if (!qtree || !IsA(qtree, Query))
     108            0 :                         elog(ERROR, "cannot get type for untransformed sublink");
     109        56415 :                     tent = linitial_node(TargetEntry, qtree->targetList);
     110              :                     Assert(!tent->resjunk);
     111        56415 :                     type = exprType((Node *) tent->expr);
     112        56415 :                     if (sublink->subLinkType == ARRAY_SUBLINK)
     113              :                     {
     114        11985 :                         type = get_promoted_array_type(type);
     115        11985 :                         if (!OidIsValid(type))
     116            0 :                             ereport(ERROR,
     117              :                                     (errcode(ERRCODE_UNDEFINED_OBJECT),
     118              :                                      errmsg("could not find array type for data type %s",
     119              :                                             format_type_be(exprType((Node *) tent->expr)))));
     120              :                     }
     121              :                 }
     122        20409 :                 else if (sublink->subLinkType == MULTIEXPR_SUBLINK)
     123              :                 {
     124              :                     /* MULTIEXPR is always considered to return RECORD */
     125           91 :                     type = RECORDOID;
     126              :                 }
     127              :                 else
     128              :                 {
     129              :                     /* for all other sublink types, result is boolean */
     130        20318 :                     type = BOOLOID;
     131              :                 }
     132              :             }
     133        76824 :             break;
     134        37462 :         case T_SubPlan:
     135              :             {
     136        37462 :                 const SubPlan *subplan = (const SubPlan *) expr;
     137              : 
     138        37462 :                 if (subplan->subLinkType == EXPR_SUBLINK ||
     139         3211 :                     subplan->subLinkType == ARRAY_SUBLINK)
     140              :                 {
     141              :                     /* get the type of the subselect's first target column */
     142        34499 :                     type = subplan->firstColType;
     143        34499 :                     if (subplan->subLinkType == ARRAY_SUBLINK)
     144              :                     {
     145          248 :                         type = get_promoted_array_type(type);
     146          248 :                         if (!OidIsValid(type))
     147            0 :                             ereport(ERROR,
     148              :                                     (errcode(ERRCODE_UNDEFINED_OBJECT),
     149              :                                      errmsg("could not find array type for data type %s",
     150              :                                             format_type_be(subplan->firstColType))));
     151              :                     }
     152              :                 }
     153         2963 :                 else if (subplan->subLinkType == MULTIEXPR_SUBLINK)
     154              :                 {
     155              :                     /* MULTIEXPR is always considered to return RECORD */
     156          126 :                     type = RECORDOID;
     157              :                 }
     158              :                 else
     159              :                 {
     160              :                     /* for all other subplan types, result is boolean */
     161         2837 :                     type = BOOLOID;
     162              :                 }
     163              :             }
     164        37462 :             break;
     165         1094 :         case T_AlternativeSubPlan:
     166              :             {
     167         1094 :                 const AlternativeSubPlan *asplan = (const AlternativeSubPlan *) expr;
     168              : 
     169              :                 /* subplans should all return the same thing */
     170         1094 :                 type = exprType((Node *) linitial(asplan->subplans));
     171              :             }
     172         1094 :             break;
     173        32781 :         case T_FieldSelect:
     174        32781 :             type = ((const FieldSelect *) expr)->resulttype;
     175        32781 :             break;
     176          760 :         case T_FieldStore:
     177          760 :             type = ((const FieldStore *) expr)->resulttype;
     178          760 :             break;
     179       460934 :         case T_RelabelType:
     180       460934 :             type = ((const RelabelType *) expr)->resulttype;
     181       460934 :             break;
     182       103343 :         case T_CoerceViaIO:
     183       103343 :             type = ((const CoerceViaIO *) expr)->resulttype;
     184       103343 :             break;
     185        11814 :         case T_ArrayCoerceExpr:
     186        11814 :             type = ((const ArrayCoerceExpr *) expr)->resulttype;
     187        11814 :             break;
     188         1703 :         case T_ConvertRowtypeExpr:
     189         1703 :             type = ((const ConvertRowtypeExpr *) expr)->resulttype;
     190         1703 :             break;
     191         7797 :         case T_CollateExpr:
     192         7797 :             type = exprType((Node *) ((const CollateExpr *) expr)->arg);
     193         7797 :             break;
     194       276828 :         case T_CaseExpr:
     195       276828 :             type = ((const CaseExpr *) expr)->casetype;
     196       276828 :             break;
     197        29823 :         case T_CaseTestExpr:
     198        29823 :             type = ((const CaseTestExpr *) expr)->typeId;
     199        29823 :             break;
     200        72337 :         case T_ArrayExpr:
     201        72337 :             type = ((const ArrayExpr *) expr)->array_typeid;
     202        72337 :             break;
     203        11326 :         case T_RowExpr:
     204        11326 :             type = ((const RowExpr *) expr)->row_typeid;
     205        11326 :             break;
     206          314 :         case T_RowCompareExpr:
     207          314 :             type = BOOLOID;
     208          314 :             break;
     209        19154 :         case T_CoalesceExpr:
     210        19154 :             type = ((const CoalesceExpr *) expr)->coalescetype;
     211        19154 :             break;
     212         3670 :         case T_MinMaxExpr:
     213         3670 :             type = ((const MinMaxExpr *) expr)->minmaxtype;
     214         3670 :             break;
     215         7850 :         case T_SQLValueFunction:
     216         7850 :             type = ((const SQLValueFunction *) expr)->type;
     217         7850 :             break;
     218        17441 :         case T_XmlExpr:
     219        17441 :             if (((const XmlExpr *) expr)->op == IS_DOCUMENT)
     220           70 :                 type = BOOLOID;
     221        17371 :             else if (((const XmlExpr *) expr)->op == IS_XMLSERIALIZE)
     222          635 :                 type = TEXTOID;
     223              :             else
     224        16736 :                 type = XMLOID;
     225        17441 :             break;
     226          681 :         case T_JsonValueExpr:
     227              :             {
     228          681 :                 const JsonValueExpr *jve = (const JsonValueExpr *) expr;
     229              : 
     230          681 :                 type = exprType((Node *) jve->formatted_expr);
     231              :             }
     232          681 :             break;
     233         5484 :         case T_JsonConstructorExpr:
     234         5484 :             type = ((const JsonConstructorExpr *) expr)->returning->typid;
     235         5484 :             break;
     236         1262 :         case T_JsonIsPredicate:
     237         1262 :             type = BOOLOID;
     238         1262 :             break;
     239         6960 :         case T_JsonExpr:
     240              :             {
     241         6960 :                 const JsonExpr *jexpr = (const JsonExpr *) expr;
     242              : 
     243         6960 :                 type = jexpr->returning->typid;
     244         6960 :                 break;
     245              :             }
     246         3204 :         case T_JsonBehavior:
     247              :             {
     248         3204 :                 const JsonBehavior *behavior = (const JsonBehavior *) expr;
     249              : 
     250         3204 :                 type = exprType(behavior->expr);
     251         3204 :                 break;
     252              :             }
     253        30397 :         case T_NullTest:
     254        30397 :             type = BOOLOID;
     255        30397 :             break;
     256         2408 :         case T_BooleanTest:
     257         2408 :             type = BOOLOID;
     258         2408 :             break;
     259       188130 :         case T_CoerceToDomain:
     260       188130 :             type = ((const CoerceToDomain *) expr)->resulttype;
     261       188130 :             break;
     262         1546 :         case T_CoerceToDomainValue:
     263         1546 :             type = ((const CoerceToDomainValue *) expr)->typeId;
     264         1546 :             break;
     265        80994 :         case T_SetToDefault:
     266        80994 :             type = ((const SetToDefault *) expr)->typeId;
     267        80994 :             break;
     268          172 :         case T_CurrentOfExpr:
     269          172 :             type = BOOLOID;
     270          172 :             break;
     271         1462 :         case T_NextValueExpr:
     272         1462 :             type = ((const NextValueExpr *) expr)->typeId;
     273         1462 :             break;
     274            0 :         case T_InferenceElem:
     275              :             {
     276            0 :                 const InferenceElem *n = (const InferenceElem *) expr;
     277              : 
     278            0 :                 type = exprType((Node *) n->expr);
     279              :             }
     280            0 :             break;
     281          640 :         case T_ReturningExpr:
     282          640 :             type = exprType((Node *) ((const ReturningExpr *) expr)->retexpr);
     283          640 :             break;
     284        17757 :         case T_PlaceHolderVar:
     285        17757 :             type = exprType((Node *) ((const PlaceHolderVar *) expr)->phexpr);
     286        17757 :             break;
     287         1628 :         case T_GraphPropertyRef:
     288         1628 :             type = ((const GraphPropertyRef *) expr)->typeId;
     289         1628 :             break;
     290            0 :         default:
     291            0 :             elog(ERROR, "unrecognized node type: %d", (int) nodeTag(expr));
     292              :             type = InvalidOid;  /* keep compiler quiet */
     293              :             break;
     294              :     }
     295     22269416 :     return type;
     296              : }
     297              : 
     298              : /*
     299              :  *  exprTypmod -
     300              :  *    returns the type-specific modifier of the expression's result type,
     301              :  *    if it can be determined.  In many cases, it can't and we return -1.
     302              :  */
     303              : int32
     304      7697534 : exprTypmod(const Node *expr)
     305              : {
     306      7697534 :     if (!expr)
     307            0 :         return -1;
     308              : 
     309      7697534 :     switch (nodeTag(expr))
     310              :     {
     311      4597769 :         case T_Var:
     312      4597769 :             return ((const Var *) expr)->vartypmod;
     313      1189090 :         case T_Const:
     314      1189090 :             return ((const Const *) expr)->consttypmod;
     315       105215 :         case T_Param:
     316       105215 :             return ((const Param *) expr)->paramtypmod;
     317        31849 :         case T_SubscriptingRef:
     318        31849 :             return ((const SubscriptingRef *) expr)->reftypmod;
     319       874520 :         case T_FuncExpr:
     320              :             {
     321              :                 int32       coercedTypmod;
     322              : 
     323              :                 /* Be smart about length-coercion functions... */
     324       874520 :                 if (exprIsLengthCoercion(expr, &coercedTypmod))
     325        19654 :                     return coercedTypmod;
     326              :             }
     327       854866 :             break;
     328            0 :         case T_NamedArgExpr:
     329            0 :             return exprTypmod((Node *) ((const NamedArgExpr *) expr)->arg);
     330          321 :         case T_NullIfExpr:
     331              :             {
     332              :                 /*
     333              :                  * Result is either first argument or NULL, so we can report
     334              :                  * first argument's typmod if known.
     335              :                  */
     336          321 :                 const NullIfExpr *nexpr = (const NullIfExpr *) expr;
     337              : 
     338          321 :                 return exprTypmod((Node *) linitial(nexpr->args));
     339              :             }
     340              :             break;
     341         4457 :         case T_SubLink:
     342              :             {
     343         4457 :                 const SubLink *sublink = (const SubLink *) expr;
     344              : 
     345         4457 :                 if (sublink->subLinkType == EXPR_SUBLINK ||
     346          422 :                     sublink->subLinkType == ARRAY_SUBLINK)
     347              :                 {
     348              :                     /* get the typmod of the subselect's first target column */
     349         4399 :                     Query      *qtree = (Query *) sublink->subselect;
     350              :                     TargetEntry *tent;
     351              : 
     352         4399 :                     if (!qtree || !IsA(qtree, Query))
     353            0 :                         elog(ERROR, "cannot get type for untransformed sublink");
     354         4399 :                     tent = linitial_node(TargetEntry, qtree->targetList);
     355              :                     Assert(!tent->resjunk);
     356         4399 :                     return exprTypmod((Node *) tent->expr);
     357              :                     /* note we don't need to care if it's an array */
     358              :                 }
     359              :                 /* otherwise, result is RECORD or BOOLEAN, typmod is -1 */
     360              :             }
     361           58 :             break;
     362        26513 :         case T_SubPlan:
     363              :             {
     364        26513 :                 const SubPlan *subplan = (const SubPlan *) expr;
     365              : 
     366        26513 :                 if (subplan->subLinkType == EXPR_SUBLINK ||
     367         1732 :                     subplan->subLinkType == ARRAY_SUBLINK)
     368              :                 {
     369              :                     /* get the typmod of the subselect's first target column */
     370              :                     /* note we don't need to care if it's an array */
     371        24958 :                     return subplan->firstColTypmod;
     372              :                 }
     373              :                 /* otherwise, result is RECORD or BOOLEAN, typmod is -1 */
     374              :             }
     375         1555 :             break;
     376          552 :         case T_AlternativeSubPlan:
     377              :             {
     378          552 :                 const AlternativeSubPlan *asplan = (const AlternativeSubPlan *) expr;
     379              : 
     380              :                 /* subplans should all return the same thing */
     381          552 :                 return exprTypmod((Node *) linitial(asplan->subplans));
     382              :             }
     383              :             break;
     384        14367 :         case T_FieldSelect:
     385        14367 :             return ((const FieldSelect *) expr)->resulttypmod;
     386       155340 :         case T_RelabelType:
     387       155340 :             return ((const RelabelType *) expr)->resulttypmod;
     388         5627 :         case T_ArrayCoerceExpr:
     389         5627 :             return ((const ArrayCoerceExpr *) expr)->resulttypmod;
     390          151 :         case T_CollateExpr:
     391          151 :             return exprTypmod((Node *) ((const CollateExpr *) expr)->arg);
     392       131851 :         case T_CaseExpr:
     393              :             {
     394              :                 /*
     395              :                  * If all the alternatives agree on type/typmod, return that
     396              :                  * typmod, else use -1
     397              :                  */
     398       131851 :                 const CaseExpr *cexpr = (const CaseExpr *) expr;
     399       131851 :                 Oid         casetype = cexpr->casetype;
     400              :                 int32       typmod;
     401              :                 ListCell   *arg;
     402              : 
     403       131851 :                 if (!cexpr->defresult)
     404            0 :                     return -1;
     405       131851 :                 if (exprType((Node *) cexpr->defresult) != casetype)
     406            0 :                     return -1;
     407       131851 :                 typmod = exprTypmod((Node *) cexpr->defresult);
     408       131851 :                 if (typmod < 0)
     409       131851 :                     return -1;  /* no point in trying harder */
     410            0 :                 foreach(arg, cexpr->args)
     411              :                 {
     412            0 :                     CaseWhen   *w = lfirst_node(CaseWhen, arg);
     413              : 
     414            0 :                     if (exprType((Node *) w->result) != casetype)
     415            0 :                         return -1;
     416            0 :                     if (exprTypmod((Node *) w->result) != typmod)
     417            0 :                         return -1;
     418              :                 }
     419            0 :                 return typmod;
     420              :             }
     421              :             break;
     422         9781 :         case T_CaseTestExpr:
     423         9781 :             return ((const CaseTestExpr *) expr)->typeMod;
     424        29218 :         case T_ArrayExpr:
     425              :             {
     426              :                 /*
     427              :                  * If all the elements agree on type/typmod, return that
     428              :                  * typmod, else use -1
     429              :                  */
     430        29218 :                 const ArrayExpr *arrayexpr = (const ArrayExpr *) expr;
     431              :                 Oid         commontype;
     432              :                 int32       typmod;
     433              :                 ListCell   *elem;
     434              : 
     435        29218 :                 if (arrayexpr->elements == NIL)
     436          164 :                     return -1;
     437        29054 :                 typmod = exprTypmod((Node *) linitial(arrayexpr->elements));
     438        29054 :                 if (typmod < 0)
     439        29030 :                     return -1;  /* no point in trying harder */
     440           24 :                 if (arrayexpr->multidims)
     441            0 :                     commontype = arrayexpr->array_typeid;
     442              :                 else
     443           24 :                     commontype = arrayexpr->element_typeid;
     444           84 :                 foreach(elem, arrayexpr->elements)
     445              :                 {
     446           60 :                     Node       *e = (Node *) lfirst(elem);
     447              : 
     448           60 :                     if (exprType(e) != commontype)
     449            0 :                         return -1;
     450           60 :                     if (exprTypmod(e) != typmod)
     451            0 :                         return -1;
     452              :                 }
     453           24 :                 return typmod;
     454              :             }
     455              :             break;
     456         5870 :         case T_CoalesceExpr:
     457              :             {
     458              :                 /*
     459              :                  * If all the alternatives agree on type/typmod, return that
     460              :                  * typmod, else use -1
     461              :                  */
     462         5870 :                 const CoalesceExpr *cexpr = (const CoalesceExpr *) expr;
     463         5870 :                 Oid         coalescetype = cexpr->coalescetype;
     464              :                 int32       typmod;
     465              :                 ListCell   *arg;
     466              : 
     467         5870 :                 if (exprType((Node *) linitial(cexpr->args)) != coalescetype)
     468            0 :                     return -1;
     469         5870 :                 typmod = exprTypmod((Node *) linitial(cexpr->args));
     470         5870 :                 if (typmod < 0)
     471         5870 :                     return -1;  /* no point in trying harder */
     472            0 :                 for_each_from(arg, cexpr->args, 1)
     473              :                 {
     474            0 :                     Node       *e = (Node *) lfirst(arg);
     475              : 
     476            0 :                     if (exprType(e) != coalescetype)
     477            0 :                         return -1;
     478            0 :                     if (exprTypmod(e) != typmod)
     479            0 :                         return -1;
     480              :                 }
     481            0 :                 return typmod;
     482              :             }
     483              :             break;
     484         1895 :         case T_MinMaxExpr:
     485              :             {
     486              :                 /*
     487              :                  * If all the alternatives agree on type/typmod, return that
     488              :                  * typmod, else use -1
     489              :                  */
     490         1895 :                 const MinMaxExpr *mexpr = (const MinMaxExpr *) expr;
     491         1895 :                 Oid         minmaxtype = mexpr->minmaxtype;
     492              :                 int32       typmod;
     493              :                 ListCell   *arg;
     494              : 
     495         1895 :                 if (exprType((Node *) linitial(mexpr->args)) != minmaxtype)
     496            0 :                     return -1;
     497         1895 :                 typmod = exprTypmod((Node *) linitial(mexpr->args));
     498         1895 :                 if (typmod < 0)
     499         1895 :                     return -1;  /* no point in trying harder */
     500            0 :                 for_each_from(arg, mexpr->args, 1)
     501              :                 {
     502            0 :                     Node       *e = (Node *) lfirst(arg);
     503              : 
     504            0 :                     if (exprType(e) != minmaxtype)
     505            0 :                         return -1;
     506            0 :                     if (exprTypmod(e) != typmod)
     507            0 :                         return -1;
     508              :                 }
     509            0 :                 return typmod;
     510              :             }
     511              :             break;
     512         1673 :         case T_SQLValueFunction:
     513         1673 :             return ((const SQLValueFunction *) expr)->typmod;
     514           44 :         case T_JsonValueExpr:
     515           44 :             return exprTypmod((Node *) ((const JsonValueExpr *) expr)->formatted_expr);
     516         2046 :         case T_JsonConstructorExpr:
     517         2046 :             return ((const JsonConstructorExpr *) expr)->returning->typmod;
     518         2683 :         case T_JsonExpr:
     519              :             {
     520         2683 :                 const JsonExpr *jexpr = (const JsonExpr *) expr;
     521              : 
     522         2683 :                 return jexpr->returning->typmod;
     523              :             }
     524              :             break;
     525            0 :         case T_JsonBehavior:
     526              :             {
     527            0 :                 const JsonBehavior *behavior = (const JsonBehavior *) expr;
     528              : 
     529            0 :                 return exprTypmod(behavior->expr);
     530              :             }
     531              :             break;
     532       143144 :         case T_CoerceToDomain:
     533       143144 :             return ((const CoerceToDomain *) expr)->resulttypmod;
     534           52 :         case T_CoerceToDomainValue:
     535           52 :             return ((const CoerceToDomainValue *) expr)->typeMod;
     536        21185 :         case T_SetToDefault:
     537        21185 :             return ((const SetToDefault *) expr)->typeMod;
     538          352 :         case T_ReturningExpr:
     539          352 :             return exprTypmod((Node *) ((const ReturningExpr *) expr)->retexpr);
     540         9991 :         case T_PlaceHolderVar:
     541         9991 :             return exprTypmod((Node *) ((const PlaceHolderVar *) expr)->phexpr);
     542         1262 :         case T_GraphPropertyRef:
     543         1262 :             return ((const GraphPropertyRef *) expr)->typmod;
     544       330716 :         default:
     545       330716 :             break;
     546              :     }
     547      1187195 :     return -1;
     548              : }
     549              : 
     550              : /*
     551              :  * exprIsLengthCoercion
     552              :  *      Detect whether an expression tree is an application of a datatype's
     553              :  *      typmod-coercion function.  Optionally extract the result's typmod.
     554              :  *
     555              :  * If coercedTypmod is not NULL, the typmod is stored there if the expression
     556              :  * is a length-coercion function, else -1 is stored there.
     557              :  *
     558              :  * Note that a combined type-and-length coercion will be treated as a
     559              :  * length coercion by this routine.
     560              :  */
     561              : bool
     562       875660 : exprIsLengthCoercion(const Node *expr, int32 *coercedTypmod)
     563              : {
     564       875660 :     if (coercedTypmod != NULL)
     565       875660 :         *coercedTypmod = -1;    /* default result on failure */
     566              : 
     567              :     /*
     568              :      * Scalar-type length coercions are FuncExprs, array-type length coercions
     569              :      * are ArrayCoerceExprs
     570              :      */
     571       875660 :     if (expr && IsA(expr, FuncExpr))
     572              :     {
     573       875660 :         const FuncExpr *func = (const FuncExpr *) expr;
     574              :         int         nargs;
     575              :         Const      *second_arg;
     576              : 
     577              :         /*
     578              :          * If it didn't come from a coercion context, reject.
     579              :          */
     580       875660 :         if (func->funcformat != COERCE_EXPLICIT_CAST &&
     581       850706 :             func->funcformat != COERCE_IMPLICIT_CAST)
     582       704564 :             return false;
     583              : 
     584              :         /*
     585              :          * If it's not a two-argument or three-argument function with the
     586              :          * second argument being an int4 constant, it can't have been created
     587              :          * from a length coercion (it must be a type coercion, instead).
     588              :          */
     589       171096 :         nargs = list_length(func->args);
     590       171096 :         if (nargs < 2 || nargs > 3)
     591       151355 :             return false;
     592              : 
     593        19741 :         second_arg = (Const *) lsecond(func->args);
     594        19741 :         if (!IsA(second_arg, Const) ||
     595        19741 :             second_arg->consttype != INT4OID ||
     596        19741 :             second_arg->constisnull)
     597            0 :             return false;
     598              : 
     599              :         /*
     600              :          * OK, it is indeed a length-coercion function.
     601              :          */
     602        19741 :         if (coercedTypmod != NULL)
     603        19741 :             *coercedTypmod = DatumGetInt32(second_arg->constvalue);
     604              : 
     605        19741 :         return true;
     606              :     }
     607              : 
     608            0 :     if (expr && IsA(expr, ArrayCoerceExpr))
     609              :     {
     610            0 :         const ArrayCoerceExpr *acoerce = (const ArrayCoerceExpr *) expr;
     611              : 
     612              :         /* It's not a length coercion unless there's a nondefault typmod */
     613            0 :         if (acoerce->resulttypmod < 0)
     614            0 :             return false;
     615              : 
     616              :         /*
     617              :          * OK, it is indeed a length-coercion expression.
     618              :          */
     619            0 :         if (coercedTypmod != NULL)
     620            0 :             *coercedTypmod = acoerce->resulttypmod;
     621              : 
     622            0 :         return true;
     623              :     }
     624              : 
     625            0 :     return false;
     626              : }
     627              : 
     628              : /*
     629              :  * applyRelabelType
     630              :  *      Add a RelabelType node if needed to make the expression expose
     631              :  *      the specified type, typmod, and collation.
     632              :  *
     633              :  * This is primarily intended to be used during planning.  Therefore, it must
     634              :  * maintain the post-eval_const_expressions invariants that there are not
     635              :  * adjacent RelabelTypes, and that the tree is fully const-folded (hence,
     636              :  * we mustn't return a RelabelType atop a Const).  If we do find a Const,
     637              :  * we'll modify it in-place if "overwrite_ok" is true; that should only be
     638              :  * passed as true if caller knows the Const is newly generated.
     639              :  */
     640              : Node *
     641       225806 : applyRelabelType(Node *arg, Oid rtype, int32 rtypmod, Oid rcollid,
     642              :                  CoercionForm rformat, int rlocation, bool overwrite_ok)
     643              : {
     644              :     /*
     645              :      * If we find stacked RelabelTypes (eg, from foo::int::oid) we can discard
     646              :      * all but the top one, and must do so to ensure that semantically
     647              :      * equivalent expressions are equal().
     648              :      */
     649       228724 :     while (arg && IsA(arg, RelabelType))
     650         2918 :         arg = (Node *) ((RelabelType *) arg)->arg;
     651              : 
     652       225806 :     if (arg && IsA(arg, Const))
     653              :     {
     654              :         /* Modify the Const directly to preserve const-flatness. */
     655        88467 :         Const      *con = (Const *) arg;
     656              : 
     657        88467 :         if (!overwrite_ok)
     658        11559 :             con = copyObject(con);
     659        88467 :         con->consttype = rtype;
     660        88467 :         con->consttypmod = rtypmod;
     661        88467 :         con->constcollid = rcollid;
     662              :         /* We keep the Const's original location. */
     663        88467 :         return (Node *) con;
     664              :     }
     665       143270 :     else if (exprType(arg) == rtype &&
     666        11756 :              exprTypmod(arg) == rtypmod &&
     667         5825 :              exprCollation(arg) == rcollid)
     668              :     {
     669              :         /* Sometimes we find a nest of relabels that net out to nothing. */
     670         2765 :         return arg;
     671              :     }
     672              :     else
     673              :     {
     674              :         /* Nope, gotta have a RelabelType. */
     675       134574 :         RelabelType *newrelabel = makeNode(RelabelType);
     676              : 
     677       134574 :         newrelabel->arg = (Expr *) arg;
     678       134574 :         newrelabel->resulttype = rtype;
     679       134574 :         newrelabel->resulttypmod = rtypmod;
     680       134574 :         newrelabel->resultcollid = rcollid;
     681       134574 :         newrelabel->relabelformat = rformat;
     682       134574 :         newrelabel->location = rlocation;
     683       134574 :         return (Node *) newrelabel;
     684              :     }
     685              : }
     686              : 
     687              : /*
     688              :  * relabel_to_typmod
     689              :  *      Add a RelabelType node that changes just the typmod of the expression.
     690              :  *
     691              :  * Convenience function for a common usage of applyRelabelType.
     692              :  */
     693              : Node *
     694           24 : relabel_to_typmod(Node *expr, int32 typmod)
     695              : {
     696           24 :     return applyRelabelType(expr, exprType(expr), typmod, exprCollation(expr),
     697              :                             COERCE_EXPLICIT_CAST, -1, false);
     698              : }
     699              : 
     700              : /*
     701              :  * strip_implicit_coercions: remove implicit coercions at top level of tree
     702              :  *
     703              :  * This doesn't modify or copy the input expression tree, just return a
     704              :  * pointer to a suitable place within it.
     705              :  *
     706              :  * Note: there isn't any useful thing we can do with a RowExpr here, so
     707              :  * just return it unchanged, even if it's marked as an implicit coercion.
     708              :  */
     709              : Node *
     710       710875 : strip_implicit_coercions(Node *node)
     711              : {
     712       710875 :     if (node == NULL)
     713            0 :         return NULL;
     714       710875 :     if (IsA(node, FuncExpr))
     715              :     {
     716         9732 :         FuncExpr   *f = (FuncExpr *) node;
     717              : 
     718         9732 :         if (f->funcformat == COERCE_IMPLICIT_CAST)
     719           36 :             return strip_implicit_coercions(linitial(f->args));
     720              :     }
     721       701143 :     else if (IsA(node, RelabelType))
     722              :     {
     723         8059 :         RelabelType *r = (RelabelType *) node;
     724              : 
     725         8059 :         if (r->relabelformat == COERCE_IMPLICIT_CAST)
     726            9 :             return strip_implicit_coercions((Node *) r->arg);
     727              :     }
     728       693084 :     else if (IsA(node, CoerceViaIO))
     729              :     {
     730          413 :         CoerceViaIO *c = (CoerceViaIO *) node;
     731              : 
     732          413 :         if (c->coerceformat == COERCE_IMPLICIT_CAST)
     733            0 :             return strip_implicit_coercions((Node *) c->arg);
     734              :     }
     735       692671 :     else if (IsA(node, ArrayCoerceExpr))
     736              :     {
     737            0 :         ArrayCoerceExpr *c = (ArrayCoerceExpr *) node;
     738              : 
     739            0 :         if (c->coerceformat == COERCE_IMPLICIT_CAST)
     740            0 :             return strip_implicit_coercions((Node *) c->arg);
     741              :     }
     742       692671 :     else if (IsA(node, ConvertRowtypeExpr))
     743              :     {
     744            0 :         ConvertRowtypeExpr *c = (ConvertRowtypeExpr *) node;
     745              : 
     746            0 :         if (c->convertformat == COERCE_IMPLICIT_CAST)
     747            0 :             return strip_implicit_coercions((Node *) c->arg);
     748              :     }
     749       692671 :     else if (IsA(node, CoerceToDomain))
     750              :     {
     751         5115 :         CoerceToDomain *c = (CoerceToDomain *) node;
     752              : 
     753         5115 :         if (c->coercionformat == COERCE_IMPLICIT_CAST)
     754            0 :             return strip_implicit_coercions((Node *) c->arg);
     755              :     }
     756       710830 :     return node;
     757              : }
     758              : 
     759              : /*
     760              :  * expression_returns_set
     761              :  *    Test whether an expression returns a set result.
     762              :  *
     763              :  * Because we use expression_tree_walker(), this can also be applied to
     764              :  * whole targetlists; it'll produce true if any one of the tlist items
     765              :  * returns a set.
     766              :  */
     767              : bool
     768       598860 : expression_returns_set(Node *clause)
     769              : {
     770       598860 :     return expression_returns_set_walker(clause, NULL);
     771              : }
     772              : 
     773              : static bool
     774      2587433 : expression_returns_set_walker(Node *node, void *context)
     775              : {
     776      2587433 :     if (node == NULL)
     777        31134 :         return false;
     778      2556299 :     if (IsA(node, FuncExpr))
     779              :     {
     780        84233 :         FuncExpr   *expr = (FuncExpr *) node;
     781              : 
     782        84233 :         if (expr->funcretset)
     783        12052 :             return true;
     784              :         /* else fall through to check args */
     785              :     }
     786      2544247 :     if (IsA(node, OpExpr))
     787              :     {
     788       574300 :         OpExpr     *expr = (OpExpr *) node;
     789              : 
     790       574300 :         if (expr->opretset)
     791            5 :             return true;
     792              :         /* else fall through to check args */
     793              :     }
     794              : 
     795              :     /*
     796              :      * If you add any more cases that return sets, also fix
     797              :      * expression_returns_set_rows() in clauses.c and IS_SRF_CALL() in
     798              :      * tlist.c.
     799              :      */
     800              : 
     801              :     /* Avoid recursion for some cases that parser checks not to return a set */
     802      2544242 :     if (IsA(node, Aggref))
     803          799 :         return false;
     804      2543443 :     if (IsA(node, GroupingFunc))
     805           40 :         return false;
     806      2543403 :     if (IsA(node, WindowFunc))
     807           24 :         return false;
     808              : 
     809      2543379 :     return expression_tree_walker(node, expression_returns_set_walker,
     810              :                                   context);
     811              : }
     812              : 
     813              : 
     814              : /*
     815              :  *  exprCollation -
     816              :  *    returns the Oid of the collation of the expression's result.
     817              :  *
     818              :  * Note: expression nodes that can invoke functions generally have an
     819              :  * "inputcollid" field, which is what the function should use as collation.
     820              :  * That is the resolved common collation of the node's inputs.  It is often
     821              :  * but not always the same as the result collation; in particular, if the
     822              :  * function produces a non-collatable result type from collatable inputs
     823              :  * or vice versa, the two are different.
     824              :  */
     825              : Oid
     826     10043704 : exprCollation(const Node *expr)
     827              : {
     828              :     Oid         coll;
     829              : 
     830     10043704 :     if (!expr)
     831            0 :         return InvalidOid;
     832              : 
     833     10043704 :     switch (nodeTag(expr))
     834              :     {
     835      7776416 :         case T_Var:
     836      7776416 :             coll = ((const Var *) expr)->varcollid;
     837      7776416 :             break;
     838      1299516 :         case T_Const:
     839      1299516 :             coll = ((const Const *) expr)->constcollid;
     840      1299516 :             break;
     841       123147 :         case T_Param:
     842       123147 :             coll = ((const Param *) expr)->paramcollid;
     843       123147 :             break;
     844        66153 :         case T_Aggref:
     845        66153 :             coll = ((const Aggref *) expr)->aggcollid;
     846        66153 :             break;
     847          555 :         case T_GroupingFunc:
     848          555 :             coll = InvalidOid;
     849          555 :             break;
     850         4028 :         case T_WindowFunc:
     851         4028 :             coll = ((const WindowFunc *) expr)->wincollid;
     852         4028 :             break;
     853          249 :         case T_MergeSupportFunc:
     854          249 :             coll = ((const MergeSupportFunc *) expr)->msfcollid;
     855          249 :             break;
     856         6864 :         case T_SubscriptingRef:
     857         6864 :             coll = ((const SubscriptingRef *) expr)->refcollid;
     858         6864 :             break;
     859       277126 :         case T_FuncExpr:
     860       277126 :             coll = ((const FuncExpr *) expr)->funccollid;
     861       277126 :             break;
     862            0 :         case T_NamedArgExpr:
     863            0 :             coll = exprCollation((Node *) ((const NamedArgExpr *) expr)->arg);
     864            0 :             break;
     865        79822 :         case T_OpExpr:
     866        79822 :             coll = ((const OpExpr *) expr)->opcollid;
     867        79822 :             break;
     868           54 :         case T_DistinctExpr:
     869           54 :             coll = ((const DistinctExpr *) expr)->opcollid;
     870           54 :             break;
     871          173 :         case T_NullIfExpr:
     872          173 :             coll = ((const NullIfExpr *) expr)->opcollid;
     873          173 :             break;
     874        15106 :         case T_ScalarArrayOpExpr:
     875              :             /* ScalarArrayOpExpr's result is boolean ... */
     876        15106 :             coll = InvalidOid;  /* ... so it has no collation */
     877        15106 :             break;
     878         2840 :         case T_BoolExpr:
     879              :             /* BoolExpr's result is boolean ... */
     880         2840 :             coll = InvalidOid;  /* ... so it has no collation */
     881         2840 :             break;
     882         2523 :         case T_SubLink:
     883              :             {
     884         2523 :                 const SubLink *sublink = (const SubLink *) expr;
     885              : 
     886         2523 :                 if (sublink->subLinkType == EXPR_SUBLINK ||
     887          179 :                     sublink->subLinkType == ARRAY_SUBLINK)
     888         2473 :                 {
     889              :                     /* get the collation of subselect's first target column */
     890         2473 :                     Query      *qtree = (Query *) sublink->subselect;
     891              :                     TargetEntry *tent;
     892              : 
     893         2473 :                     if (!qtree || !IsA(qtree, Query))
     894            0 :                         elog(ERROR, "cannot get collation for untransformed sublink");
     895         2473 :                     tent = linitial_node(TargetEntry, qtree->targetList);
     896              :                     Assert(!tent->resjunk);
     897         2473 :                     coll = exprCollation((Node *) tent->expr);
     898              :                     /* collation doesn't change if it's converted to array */
     899              :                 }
     900              :                 else
     901              :                 {
     902              :                     /* otherwise, SubLink's result is RECORD or BOOLEAN */
     903           50 :                     coll = InvalidOid;  /* ... so it has no collation */
     904              :                 }
     905              :             }
     906         2523 :             break;
     907        14772 :         case T_SubPlan:
     908              :             {
     909        14772 :                 const SubPlan *subplan = (const SubPlan *) expr;
     910              : 
     911        14772 :                 if (subplan->subLinkType == EXPR_SUBLINK ||
     912          207 :                     subplan->subLinkType == ARRAY_SUBLINK)
     913              :                 {
     914              :                     /* get the collation of subselect's first target column */
     915        14629 :                     coll = subplan->firstColCollation;
     916              :                     /* collation doesn't change if it's converted to array */
     917              :                 }
     918              :                 else
     919              :                 {
     920              :                     /* otherwise, SubPlan's result is RECORD or BOOLEAN */
     921          143 :                     coll = InvalidOid;  /* ... so it has no collation */
     922              :                 }
     923              :             }
     924        14772 :             break;
     925            0 :         case T_AlternativeSubPlan:
     926              :             {
     927            0 :                 const AlternativeSubPlan *asplan = (const AlternativeSubPlan *) expr;
     928              : 
     929              :                 /* subplans should all return the same thing */
     930            0 :                 coll = exprCollation((Node *) linitial(asplan->subplans));
     931              :             }
     932            0 :             break;
     933         8513 :         case T_FieldSelect:
     934         8513 :             coll = ((const FieldSelect *) expr)->resultcollid;
     935         8513 :             break;
     936           42 :         case T_FieldStore:
     937              :             /* FieldStore's result is composite ... */
     938           42 :             coll = InvalidOid;  /* ... so it has no collation */
     939           42 :             break;
     940        88178 :         case T_RelabelType:
     941        88178 :             coll = ((const RelabelType *) expr)->resultcollid;
     942        88178 :             break;
     943        31307 :         case T_CoerceViaIO:
     944        31307 :             coll = ((const CoerceViaIO *) expr)->resultcollid;
     945        31307 :             break;
     946         1261 :         case T_ArrayCoerceExpr:
     947         1261 :             coll = ((const ArrayCoerceExpr *) expr)->resultcollid;
     948         1261 :             break;
     949          348 :         case T_ConvertRowtypeExpr:
     950              :             /* ConvertRowtypeExpr's result is composite ... */
     951          348 :             coll = InvalidOid;  /* ... so it has no collation */
     952          348 :             break;
     953          161 :         case T_CollateExpr:
     954          161 :             coll = ((const CollateExpr *) expr)->collOid;
     955          161 :             break;
     956       103718 :         case T_CaseExpr:
     957       103718 :             coll = ((const CaseExpr *) expr)->casecollid;
     958       103718 :             break;
     959        24326 :         case T_CaseTestExpr:
     960        24326 :             coll = ((const CaseTestExpr *) expr)->collation;
     961        24326 :             break;
     962        21980 :         case T_ArrayExpr:
     963        21980 :             coll = ((const ArrayExpr *) expr)->array_collid;
     964        21980 :             break;
     965         3799 :         case T_RowExpr:
     966              :             /* RowExpr's result is composite ... */
     967         3799 :             coll = InvalidOid;  /* ... so it has no collation */
     968         3799 :             break;
     969           44 :         case T_RowCompareExpr:
     970              :             /* RowCompareExpr's result is boolean ... */
     971           44 :             coll = InvalidOid;  /* ... so it has no collation */
     972           44 :             break;
     973         2817 :         case T_CoalesceExpr:
     974         2817 :             coll = ((const CoalesceExpr *) expr)->coalescecollid;
     975         2817 :             break;
     976         1706 :         case T_MinMaxExpr:
     977         1706 :             coll = ((const MinMaxExpr *) expr)->minmaxcollid;
     978         1706 :             break;
     979         1117 :         case T_SQLValueFunction:
     980              :             /* Returns either NAME or a non-collatable type */
     981         1117 :             if (((const SQLValueFunction *) expr)->type == NAMEOID)
     982         1014 :                 coll = C_COLLATION_OID;
     983              :             else
     984          103 :                 coll = InvalidOid;
     985         1117 :             break;
     986          451 :         case T_XmlExpr:
     987              : 
     988              :             /*
     989              :              * XMLSERIALIZE returns text from non-collatable inputs, so its
     990              :              * collation is always default.  The other cases return boolean or
     991              :              * XML, which are non-collatable.
     992              :              */
     993          451 :             if (((const XmlExpr *) expr)->op == IS_XMLSERIALIZE)
     994          110 :                 coll = DEFAULT_COLLATION_OID;
     995              :             else
     996          341 :                 coll = InvalidOid;
     997          451 :             break;
     998            8 :         case T_JsonValueExpr:
     999            8 :             coll = exprCollation((Node *) ((const JsonValueExpr *) expr)->formatted_expr);
    1000            8 :             break;
    1001         1244 :         case T_JsonConstructorExpr:
    1002              :             {
    1003         1244 :                 const JsonConstructorExpr *ctor = (const JsonConstructorExpr *) expr;
    1004              : 
    1005              :                 /*
    1006              :                  * Collation comes from coercion if present, otherwise from
    1007              :                  * func.  The func fallback is needed in cases where func
    1008              :                  * already produces the final output type and no coercion is
    1009              :                  * needed (cf. the JSCTOR_JSON_ARRAY_QUERY case).
    1010              :                  */
    1011         1244 :                 if (ctor->coercion)
    1012          190 :                     coll = exprCollation((Node *) ctor->coercion);
    1013         1054 :                 else if (ctor->func)
    1014          516 :                     coll = exprCollation((Node *) ctor->func);
    1015              :                 else
    1016          538 :                     coll = InvalidOid;
    1017              :             }
    1018         1244 :             break;
    1019          222 :         case T_JsonIsPredicate:
    1020              :             /* IS JSON's result is boolean ... */
    1021          222 :             coll = InvalidOid;  /* ... so it has no collation */
    1022          222 :             break;
    1023         1652 :         case T_JsonExpr:
    1024              :             {
    1025         1652 :                 const JsonExpr *jsexpr = (const JsonExpr *) expr;
    1026              : 
    1027         1652 :                 coll = jsexpr->collation;
    1028              :             }
    1029         1652 :             break;
    1030            0 :         case T_JsonBehavior:
    1031              :             {
    1032            0 :                 const JsonBehavior *behavior = (const JsonBehavior *) expr;
    1033              : 
    1034            0 :                 if (behavior->expr)
    1035            0 :                     coll = exprCollation(behavior->expr);
    1036              :                 else
    1037            0 :                     coll = InvalidOid;
    1038              :             }
    1039            0 :             break;
    1040         1447 :         case T_NullTest:
    1041              :             /* NullTest's result is boolean ... */
    1042         1447 :             coll = InvalidOid;  /* ... so it has no collation */
    1043         1447 :             break;
    1044          338 :         case T_BooleanTest:
    1045              :             /* BooleanTest's result is boolean ... */
    1046          338 :             coll = InvalidOid;  /* ... so it has no collation */
    1047          338 :             break;
    1048        45817 :         case T_CoerceToDomain:
    1049        45817 :             coll = ((const CoerceToDomain *) expr)->resultcollid;
    1050        45817 :             break;
    1051          567 :         case T_CoerceToDomainValue:
    1052          567 :             coll = ((const CoerceToDomainValue *) expr)->collation;
    1053          567 :             break;
    1054        20943 :         case T_SetToDefault:
    1055        20943 :             coll = ((const SetToDefault *) expr)->collation;
    1056        20943 :             break;
    1057          172 :         case T_CurrentOfExpr:
    1058              :             /* CurrentOfExpr's result is boolean ... */
    1059          172 :             coll = InvalidOid;  /* ... so it has no collation */
    1060          172 :             break;
    1061          380 :         case T_NextValueExpr:
    1062              :             /* NextValueExpr's result is an integer type ... */
    1063          380 :             coll = InvalidOid;  /* ... so it has no collation */
    1064          380 :             break;
    1065            0 :         case T_InferenceElem:
    1066            0 :             coll = exprCollation((Node *) ((const InferenceElem *) expr)->expr);
    1067            0 :             break;
    1068          352 :         case T_ReturningExpr:
    1069          352 :             coll = exprCollation((Node *) ((const ReturningExpr *) expr)->retexpr);
    1070          352 :             break;
    1071         8565 :         case T_PlaceHolderVar:
    1072         8565 :             coll = exprCollation((Node *) ((const PlaceHolderVar *) expr)->phexpr);
    1073         8565 :             break;
    1074         2885 :         case T_GraphPropertyRef:
    1075         2885 :             coll = ((const GraphPropertyRef *) expr)->collation;
    1076         2885 :             break;
    1077            0 :         default:
    1078            0 :             elog(ERROR, "unrecognized node type: %d", (int) nodeTag(expr));
    1079              :             coll = InvalidOid;  /* keep compiler quiet */
    1080              :             break;
    1081              :     }
    1082     10043704 :     return coll;
    1083              : }
    1084              : 
    1085              : /*
    1086              :  *  exprInputCollation -
    1087              :  *    returns the Oid of the collation a function should use, if available.
    1088              :  *
    1089              :  * Result is InvalidOid if the node type doesn't store this information.
    1090              :  */
    1091              : Oid
    1092       364386 : exprInputCollation(const Node *expr)
    1093              : {
    1094              :     Oid         coll;
    1095              : 
    1096       364386 :     if (!expr)
    1097            0 :         return InvalidOid;
    1098              : 
    1099       364386 :     switch (nodeTag(expr))
    1100              :     {
    1101          439 :         case T_Aggref:
    1102          439 :             coll = ((const Aggref *) expr)->inputcollid;
    1103          439 :             break;
    1104            0 :         case T_WindowFunc:
    1105            0 :             coll = ((const WindowFunc *) expr)->inputcollid;
    1106            0 :             break;
    1107          135 :         case T_FuncExpr:
    1108          135 :             coll = ((const FuncExpr *) expr)->inputcollid;
    1109          135 :             break;
    1110       361693 :         case T_OpExpr:
    1111       361693 :             coll = ((const OpExpr *) expr)->inputcollid;
    1112       361693 :             break;
    1113           14 :         case T_DistinctExpr:
    1114           14 :             coll = ((const DistinctExpr *) expr)->inputcollid;
    1115           14 :             break;
    1116            8 :         case T_NullIfExpr:
    1117            8 :             coll = ((const NullIfExpr *) expr)->inputcollid;
    1118            8 :             break;
    1119           79 :         case T_ScalarArrayOpExpr:
    1120           79 :             coll = ((const ScalarArrayOpExpr *) expr)->inputcollid;
    1121           79 :             break;
    1122            4 :         case T_MinMaxExpr:
    1123            4 :             coll = ((const MinMaxExpr *) expr)->inputcollid;
    1124            4 :             break;
    1125         2014 :         default:
    1126         2014 :             coll = InvalidOid;
    1127         2014 :             break;
    1128              :     }
    1129       364386 :     return coll;
    1130              : }
    1131              : 
    1132              : /*
    1133              :  *  exprSetCollation -
    1134              :  *    Assign collation information to an expression tree node.
    1135              :  *
    1136              :  * Note: since this is only used during parse analysis, we don't need to
    1137              :  * worry about subplans, PlaceHolderVars, or ReturningExprs.
    1138              :  */
    1139              : void
    1140      1214968 : exprSetCollation(Node *expr, Oid collation)
    1141              : {
    1142      1214968 :     switch (nodeTag(expr))
    1143              :     {
    1144            0 :         case T_Var:
    1145            0 :             ((Var *) expr)->varcollid = collation;
    1146            0 :             break;
    1147            0 :         case T_Const:
    1148            0 :             ((Const *) expr)->constcollid = collation;
    1149            0 :             break;
    1150            0 :         case T_Param:
    1151            0 :             ((Param *) expr)->paramcollid = collation;
    1152            0 :             break;
    1153        31425 :         case T_Aggref:
    1154        31425 :             ((Aggref *) expr)->aggcollid = collation;
    1155        31425 :             break;
    1156          252 :         case T_GroupingFunc:
    1157              :             Assert(!OidIsValid(collation));
    1158          252 :             break;
    1159         2636 :         case T_WindowFunc:
    1160         2636 :             ((WindowFunc *) expr)->wincollid = collation;
    1161         2636 :             break;
    1162          150 :         case T_MergeSupportFunc:
    1163          150 :             ((MergeSupportFunc *) expr)->msfcollid = collation;
    1164          150 :             break;
    1165         8944 :         case T_SubscriptingRef:
    1166         8944 :             ((SubscriptingRef *) expr)->refcollid = collation;
    1167         8944 :             break;
    1168       288331 :         case T_FuncExpr:
    1169       288331 :             ((FuncExpr *) expr)->funccollid = collation;
    1170       288331 :             break;
    1171        26414 :         case T_NamedArgExpr:
    1172              :             Assert(collation == exprCollation((Node *) ((NamedArgExpr *) expr)->arg));
    1173        26414 :             break;
    1174       415378 :         case T_OpExpr:
    1175       415378 :             ((OpExpr *) expr)->opcollid = collation;
    1176       415378 :             break;
    1177          864 :         case T_DistinctExpr:
    1178          864 :             ((DistinctExpr *) expr)->opcollid = collation;
    1179          864 :             break;
    1180          467 :         case T_NullIfExpr:
    1181          467 :             ((NullIfExpr *) expr)->opcollid = collation;
    1182          467 :             break;
    1183        23231 :         case T_ScalarArrayOpExpr:
    1184              :             /* ScalarArrayOpExpr's result is boolean ... */
    1185              :             Assert(!OidIsValid(collation)); /* ... so never set a collation */
    1186        23231 :             break;
    1187       117194 :         case T_BoolExpr:
    1188              :             /* BoolExpr's result is boolean ... */
    1189              :             Assert(!OidIsValid(collation)); /* ... so never set a collation */
    1190       117194 :             break;
    1191        35899 :         case T_SubLink:
    1192              : #ifdef USE_ASSERT_CHECKING
    1193              :             {
    1194              :                 SubLink    *sublink = (SubLink *) expr;
    1195              : 
    1196              :                 if (sublink->subLinkType == EXPR_SUBLINK ||
    1197              :                     sublink->subLinkType == ARRAY_SUBLINK)
    1198              :                 {
    1199              :                     /* get the collation of subselect's first target column */
    1200              :                     Query      *qtree = (Query *) sublink->subselect;
    1201              :                     TargetEntry *tent;
    1202              : 
    1203              :                     if (!qtree || !IsA(qtree, Query))
    1204              :                         elog(ERROR, "cannot set collation for untransformed sublink");
    1205              :                     tent = linitial_node(TargetEntry, qtree->targetList);
    1206              :                     Assert(!tent->resjunk);
    1207              :                     Assert(collation == exprCollation((Node *) tent->expr));
    1208              :                 }
    1209              :                 else
    1210              :                 {
    1211              :                     /* otherwise, result is RECORD or BOOLEAN */
    1212              :                     Assert(!OidIsValid(collation));
    1213              :                 }
    1214              :             }
    1215              : #endif                          /* USE_ASSERT_CHECKING */
    1216        35899 :             break;
    1217            0 :         case T_FieldSelect:
    1218            0 :             ((FieldSelect *) expr)->resultcollid = collation;
    1219            0 :             break;
    1220          401 :         case T_FieldStore:
    1221              :             /* FieldStore's result is composite ... */
    1222              :             Assert(!OidIsValid(collation)); /* ... so never set a collation */
    1223          401 :             break;
    1224       107592 :         case T_RelabelType:
    1225       107592 :             ((RelabelType *) expr)->resultcollid = collation;
    1226       107592 :             break;
    1227        17894 :         case T_CoerceViaIO:
    1228        17894 :             ((CoerceViaIO *) expr)->resultcollid = collation;
    1229        17894 :             break;
    1230         3773 :         case T_ArrayCoerceExpr:
    1231         3773 :             ((ArrayCoerceExpr *) expr)->resultcollid = collation;
    1232         3773 :             break;
    1233           40 :         case T_ConvertRowtypeExpr:
    1234              :             /* ConvertRowtypeExpr's result is composite ... */
    1235              :             Assert(!OidIsValid(collation)); /* ... so never set a collation */
    1236           40 :             break;
    1237        30465 :         case T_CaseExpr:
    1238        30465 :             ((CaseExpr *) expr)->casecollid = collation;
    1239        30465 :             break;
    1240        18598 :         case T_ArrayExpr:
    1241        18598 :             ((ArrayExpr *) expr)->array_collid = collation;
    1242        18598 :             break;
    1243            0 :         case T_RowExpr:
    1244              :             /* RowExpr's result is composite ... */
    1245              :             Assert(!OidIsValid(collation)); /* ... so never set a collation */
    1246            0 :             break;
    1247            0 :         case T_RowCompareExpr:
    1248              :             /* RowCompareExpr's result is boolean ... */
    1249              :             Assert(!OidIsValid(collation)); /* ... so never set a collation */
    1250            0 :             break;
    1251         4337 :         case T_CoalesceExpr:
    1252         4337 :             ((CoalesceExpr *) expr)->coalescecollid = collation;
    1253         4337 :             break;
    1254          225 :         case T_MinMaxExpr:
    1255          225 :             ((MinMaxExpr *) expr)->minmaxcollid = collation;
    1256          225 :             break;
    1257         1810 :         case T_SQLValueFunction:
    1258              :             Assert((((SQLValueFunction *) expr)->type == NAMEOID) ?
    1259              :                    (collation == C_COLLATION_OID) :
    1260              :                    (collation == InvalidOid));
    1261         1810 :             break;
    1262          521 :         case T_XmlExpr:
    1263              :             Assert((((XmlExpr *) expr)->op == IS_XMLSERIALIZE) ?
    1264              :                    (collation == DEFAULT_COLLATION_OID) :
    1265              :                    (collation == InvalidOid));
    1266          521 :             break;
    1267          466 :         case T_JsonValueExpr:
    1268          466 :             exprSetCollation((Node *) ((JsonValueExpr *) expr)->formatted_expr,
    1269              :                              collation);
    1270          466 :             break;
    1271         1118 :         case T_JsonConstructorExpr:
    1272              :             {
    1273         1118 :                 JsonConstructorExpr *ctor = (JsonConstructorExpr *) expr;
    1274              : 
    1275              :                 /* See comment in exprCollation() */
    1276         1118 :                 if (ctor->coercion)
    1277          260 :                     exprSetCollation((Node *) ctor->coercion, collation);
    1278          858 :                 else if (ctor->func)
    1279          304 :                     exprSetCollation((Node *) ctor->func, collation);
    1280              :                 else
    1281              :                     Assert(!OidIsValid(collation)); /* result is always a
    1282              :                                                      * json[b] type */
    1283              :             }
    1284         1118 :             break;
    1285          260 :         case T_JsonIsPredicate:
    1286              :             Assert(!OidIsValid(collation)); /* result is always boolean */
    1287          260 :             break;
    1288         1700 :         case T_JsonExpr:
    1289              :             {
    1290         1700 :                 JsonExpr   *jexpr = (JsonExpr *) expr;
    1291              : 
    1292         1700 :                 jexpr->collation = collation;
    1293              :             }
    1294         1700 :             break;
    1295         3204 :         case T_JsonBehavior:
    1296              :             Assert(((JsonBehavior *) expr)->expr == NULL ||
    1297              :                    exprCollation(((JsonBehavior *) expr)->expr) == collation);
    1298         3204 :             break;
    1299        14375 :         case T_NullTest:
    1300              :             /* NullTest's result is boolean ... */
    1301              :             Assert(!OidIsValid(collation)); /* ... so never set a collation */
    1302        14375 :             break;
    1303          741 :         case T_BooleanTest:
    1304              :             /* BooleanTest's result is boolean ... */
    1305              :             Assert(!OidIsValid(collation)); /* ... so never set a collation */
    1306          741 :             break;
    1307        56263 :         case T_CoerceToDomain:
    1308        56263 :             ((CoerceToDomain *) expr)->resultcollid = collation;
    1309        56263 :             break;
    1310            0 :         case T_CoerceToDomainValue:
    1311            0 :             ((CoerceToDomainValue *) expr)->collation = collation;
    1312            0 :             break;
    1313            0 :         case T_SetToDefault:
    1314            0 :             ((SetToDefault *) expr)->collation = collation;
    1315            0 :             break;
    1316            0 :         case T_CurrentOfExpr:
    1317              :             /* CurrentOfExpr's result is boolean ... */
    1318              :             Assert(!OidIsValid(collation)); /* ... so never set a collation */
    1319            0 :             break;
    1320            0 :         case T_NextValueExpr:
    1321              :             /* NextValueExpr's result is an integer type ... */
    1322              :             Assert(!OidIsValid(collation)); /* ... so never set a collation */
    1323            0 :             break;
    1324            0 :         default:
    1325            0 :             elog(ERROR, "unrecognized node type: %d", (int) nodeTag(expr));
    1326              :             break;
    1327              :     }
    1328      1214968 : }
    1329              : 
    1330              : /*
    1331              :  *  exprSetInputCollation -
    1332              :  *    Assign input-collation information to an expression tree node.
    1333              :  *
    1334              :  * This is a no-op for node types that don't store their input collation.
    1335              :  * Note we omit RowCompareExpr, which needs special treatment since it
    1336              :  * contains multiple input collation OIDs.
    1337              :  */
    1338              : void
    1339      1157683 : exprSetInputCollation(Node *expr, Oid inputcollation)
    1340              : {
    1341      1157683 :     switch (nodeTag(expr))
    1342              :     {
    1343        31209 :         case T_Aggref:
    1344        31209 :             ((Aggref *) expr)->inputcollid = inputcollation;
    1345        31209 :             break;
    1346         2612 :         case T_WindowFunc:
    1347         2612 :             ((WindowFunc *) expr)->inputcollid = inputcollation;
    1348         2612 :             break;
    1349       288203 :         case T_FuncExpr:
    1350       288203 :             ((FuncExpr *) expr)->inputcollid = inputcollation;
    1351       288203 :             break;
    1352       415378 :         case T_OpExpr:
    1353       415378 :             ((OpExpr *) expr)->inputcollid = inputcollation;
    1354       415378 :             break;
    1355          864 :         case T_DistinctExpr:
    1356          864 :             ((DistinctExpr *) expr)->inputcollid = inputcollation;
    1357          864 :             break;
    1358          467 :         case T_NullIfExpr:
    1359          467 :             ((NullIfExpr *) expr)->inputcollid = inputcollation;
    1360          467 :             break;
    1361        23231 :         case T_ScalarArrayOpExpr:
    1362        23231 :             ((ScalarArrayOpExpr *) expr)->inputcollid = inputcollation;
    1363        23231 :             break;
    1364          225 :         case T_MinMaxExpr:
    1365          225 :             ((MinMaxExpr *) expr)->inputcollid = inputcollation;
    1366          225 :             break;
    1367       395494 :         default:
    1368       395494 :             break;
    1369              :     }
    1370      1157683 : }
    1371              : 
    1372              : 
    1373              : /*
    1374              :  *  exprLocation -
    1375              :  *    returns the parse location of an expression tree, for error reports
    1376              :  *
    1377              :  * -1 is returned if the location can't be determined.
    1378              :  *
    1379              :  * For expressions larger than a single token, the intent here is to
    1380              :  * return the location of the expression's leftmost token, not necessarily
    1381              :  * the topmost Node's location field.  For example, an OpExpr's location
    1382              :  * field will point at the operator name, but if it is not a prefix operator
    1383              :  * then we should return the location of the left-hand operand instead.
    1384              :  * The reason is that we want to reference the entire expression not just
    1385              :  * that operator, and pointing to its start seems to be the most natural way.
    1386              :  *
    1387              :  * The location is not perfect --- for example, since the grammar doesn't
    1388              :  * explicitly represent parentheses in the parsetree, given something that
    1389              :  * had been written "(a + b) * c" we are going to point at "a" not "(".
    1390              :  * But it should be plenty good enough for error reporting purposes.
    1391              :  *
    1392              :  * You might think that this code is overly general, for instance why check
    1393              :  * the operands of a FuncExpr node, when the function name can be expected
    1394              :  * to be to the left of them?  There are a couple of reasons.  The grammar
    1395              :  * sometimes builds expressions that aren't quite what the user wrote;
    1396              :  * for instance x IS NOT BETWEEN ... becomes a NOT-expression whose keyword
    1397              :  * pointer is to the right of its leftmost argument.  Also, nodes that were
    1398              :  * inserted implicitly by parse analysis (such as FuncExprs for implicit
    1399              :  * coercions) will have location -1, and so we can have odd combinations of
    1400              :  * known and unknown locations in a tree.
    1401              :  */
    1402              : int
    1403      2968052 : exprLocation(const Node *expr)
    1404              : {
    1405              :     int         loc;
    1406              : 
    1407      2968052 :     if (expr == NULL)
    1408        17963 :         return -1;
    1409      2950089 :     switch (nodeTag(expr))
    1410              :     {
    1411           20 :         case T_RangeVar:
    1412           20 :             loc = ((const RangeVar *) expr)->location;
    1413           20 :             break;
    1414            0 :         case T_TableFunc:
    1415            0 :             loc = ((const TableFunc *) expr)->location;
    1416            0 :             break;
    1417      1544672 :         case T_Var:
    1418      1544672 :             loc = ((const Var *) expr)->location;
    1419      1544672 :             break;
    1420       905760 :         case T_Const:
    1421       905760 :             loc = ((const Const *) expr)->location;
    1422       905760 :             break;
    1423        68645 :         case T_Param:
    1424        68645 :             loc = ((const Param *) expr)->location;
    1425        68645 :             break;
    1426         6744 :         case T_Aggref:
    1427              :             /* function name should always be the first thing */
    1428         6744 :             loc = ((const Aggref *) expr)->location;
    1429         6744 :             break;
    1430           50 :         case T_GroupingFunc:
    1431           50 :             loc = ((const GroupingFunc *) expr)->location;
    1432           50 :             break;
    1433           36 :         case T_WindowFunc:
    1434              :             /* function name should always be the first thing */
    1435           36 :             loc = ((const WindowFunc *) expr)->location;
    1436           36 :             break;
    1437          150 :         case T_MergeSupportFunc:
    1438          150 :             loc = ((const MergeSupportFunc *) expr)->location;
    1439          150 :             break;
    1440          196 :         case T_SubscriptingRef:
    1441              :             /* just use container argument's location */
    1442          196 :             loc = exprLocation((Node *) ((const SubscriptingRef *) expr)->refexpr);
    1443          196 :             break;
    1444        68278 :         case T_FuncExpr:
    1445              :             {
    1446        68278 :                 const FuncExpr *fexpr = (const FuncExpr *) expr;
    1447              : 
    1448              :                 /* consider both function name and leftmost arg */
    1449        68278 :                 loc = leftmostLoc(fexpr->location,
    1450        68278 :                                   exprLocation((Node *) fexpr->args));
    1451              :             }
    1452        68278 :             break;
    1453            4 :         case T_NamedArgExpr:
    1454              :             {
    1455            4 :                 const NamedArgExpr *na = (const NamedArgExpr *) expr;
    1456              : 
    1457              :                 /* consider both argument name and value */
    1458            4 :                 loc = leftmostLoc(na->location,
    1459            4 :                                   exprLocation((Node *) na->arg));
    1460              :             }
    1461            4 :             break;
    1462         8730 :         case T_OpExpr:
    1463              :         case T_DistinctExpr:    /* struct-equivalent to OpExpr */
    1464              :         case T_NullIfExpr:      /* struct-equivalent to OpExpr */
    1465              :             {
    1466         8730 :                 const OpExpr *opexpr = (const OpExpr *) expr;
    1467              : 
    1468              :                 /* consider both operator name and leftmost arg */
    1469         8730 :                 loc = leftmostLoc(opexpr->location,
    1470         8730 :                                   exprLocation((Node *) opexpr->args));
    1471              :             }
    1472         8730 :             break;
    1473            0 :         case T_ScalarArrayOpExpr:
    1474              :             {
    1475            0 :                 const ScalarArrayOpExpr *saopexpr = (const ScalarArrayOpExpr *) expr;
    1476              : 
    1477              :                 /* consider both operator name and leftmost arg */
    1478            0 :                 loc = leftmostLoc(saopexpr->location,
    1479            0 :                                   exprLocation((Node *) saopexpr->args));
    1480              :             }
    1481            0 :             break;
    1482           89 :         case T_BoolExpr:
    1483              :             {
    1484           89 :                 const BoolExpr *bexpr = (const BoolExpr *) expr;
    1485              : 
    1486              :                 /*
    1487              :                  * Same as above, to handle either NOT or AND/OR.  We can't
    1488              :                  * special-case NOT because of the way that it's used for
    1489              :                  * things like IS NOT BETWEEN.
    1490              :                  */
    1491           89 :                 loc = leftmostLoc(bexpr->location,
    1492           89 :                                   exprLocation((Node *) bexpr->args));
    1493              :             }
    1494           89 :             break;
    1495          757 :         case T_SubLink:
    1496              :             {
    1497          757 :                 const SubLink *sublink = (const SubLink *) expr;
    1498              : 
    1499              :                 /* check the testexpr, if any, and the operator/keyword */
    1500          757 :                 loc = leftmostLoc(exprLocation(sublink->testexpr),
    1501          757 :                                   sublink->location);
    1502              :             }
    1503          757 :             break;
    1504         2656 :         case T_FieldSelect:
    1505              :             /* just use argument's location */
    1506         2656 :             loc = exprLocation((Node *) ((const FieldSelect *) expr)->arg);
    1507         2656 :             break;
    1508            0 :         case T_FieldStore:
    1509              :             /* just use argument's location */
    1510            0 :             loc = exprLocation((Node *) ((const FieldStore *) expr)->arg);
    1511            0 :             break;
    1512         9639 :         case T_RelabelType:
    1513              :             {
    1514         9639 :                 const RelabelType *rexpr = (const RelabelType *) expr;
    1515              : 
    1516              :                 /* Much as above */
    1517         9639 :                 loc = leftmostLoc(rexpr->location,
    1518         9639 :                                   exprLocation((Node *) rexpr->arg));
    1519              :             }
    1520         9639 :             break;
    1521        16336 :         case T_CoerceViaIO:
    1522              :             {
    1523        16336 :                 const CoerceViaIO *cexpr = (const CoerceViaIO *) expr;
    1524              : 
    1525              :                 /* Much as above */
    1526        16336 :                 loc = leftmostLoc(cexpr->location,
    1527        16336 :                                   exprLocation((Node *) cexpr->arg));
    1528              :             }
    1529        16336 :             break;
    1530            6 :         case T_ArrayCoerceExpr:
    1531              :             {
    1532            6 :                 const ArrayCoerceExpr *cexpr = (const ArrayCoerceExpr *) expr;
    1533              : 
    1534              :                 /* Much as above */
    1535            6 :                 loc = leftmostLoc(cexpr->location,
    1536            6 :                                   exprLocation((Node *) cexpr->arg));
    1537              :             }
    1538            6 :             break;
    1539            8 :         case T_ConvertRowtypeExpr:
    1540              :             {
    1541            8 :                 const ConvertRowtypeExpr *cexpr = (const ConvertRowtypeExpr *) expr;
    1542              : 
    1543              :                 /* Much as above */
    1544            8 :                 loc = leftmostLoc(cexpr->location,
    1545            8 :                                   exprLocation((Node *) cexpr->arg));
    1546              :             }
    1547            8 :             break;
    1548           80 :         case T_CollateExpr:
    1549              :             /* just use argument's location */
    1550           80 :             loc = exprLocation((Node *) ((const CollateExpr *) expr)->arg);
    1551           80 :             break;
    1552         7181 :         case T_CaseExpr:
    1553              :             /* CASE keyword should always be the first thing */
    1554         7181 :             loc = ((const CaseExpr *) expr)->location;
    1555         7181 :             break;
    1556            0 :         case T_CaseWhen:
    1557              :             /* WHEN keyword should always be the first thing */
    1558            0 :             loc = ((const CaseWhen *) expr)->location;
    1559            0 :             break;
    1560          278 :         case T_ArrayExpr:
    1561              :             /* the location points at ARRAY or [, which must be leftmost */
    1562          278 :             loc = ((const ArrayExpr *) expr)->location;
    1563          278 :             break;
    1564          233 :         case T_RowExpr:
    1565              :             /* the location points at ROW or (, which must be leftmost */
    1566          233 :             loc = ((const RowExpr *) expr)->location;
    1567          233 :             break;
    1568            0 :         case T_RowCompareExpr:
    1569              :             /* just use leftmost argument's location */
    1570            0 :             loc = exprLocation((Node *) ((const RowCompareExpr *) expr)->largs);
    1571            0 :             break;
    1572         1509 :         case T_CoalesceExpr:
    1573              :             /* COALESCE keyword should always be the first thing */
    1574         1509 :             loc = ((const CoalesceExpr *) expr)->location;
    1575         1509 :             break;
    1576           19 :         case T_MinMaxExpr:
    1577              :             /* GREATEST/LEAST keyword should always be the first thing */
    1578           19 :             loc = ((const MinMaxExpr *) expr)->location;
    1579           19 :             break;
    1580         1137 :         case T_SQLValueFunction:
    1581              :             /* function keyword should always be the first thing */
    1582         1137 :             loc = ((const SQLValueFunction *) expr)->location;
    1583         1137 :             break;
    1584          144 :         case T_XmlExpr:
    1585              :             {
    1586          144 :                 const XmlExpr *xexpr = (const XmlExpr *) expr;
    1587              : 
    1588              :                 /* consider both function name and leftmost arg */
    1589          144 :                 loc = leftmostLoc(xexpr->location,
    1590          144 :                                   exprLocation((Node *) xexpr->args));
    1591              :             }
    1592          144 :             break;
    1593            0 :         case T_JsonFormat:
    1594            0 :             loc = ((const JsonFormat *) expr)->location;
    1595            0 :             break;
    1596            0 :         case T_JsonValueExpr:
    1597            0 :             loc = exprLocation((Node *) ((const JsonValueExpr *) expr)->raw_expr);
    1598            0 :             break;
    1599          150 :         case T_JsonConstructorExpr:
    1600          150 :             loc = ((const JsonConstructorExpr *) expr)->location;
    1601          150 :             break;
    1602            0 :         case T_JsonIsPredicate:
    1603            0 :             loc = ((const JsonIsPredicate *) expr)->location;
    1604            0 :             break;
    1605          340 :         case T_JsonExpr:
    1606              :             {
    1607          340 :                 const JsonExpr *jsexpr = (const JsonExpr *) expr;
    1608              : 
    1609              :                 /* consider both function name and leftmost arg */
    1610          340 :                 loc = leftmostLoc(jsexpr->location,
    1611          340 :                                   exprLocation(jsexpr->formatted_expr));
    1612              :             }
    1613          340 :             break;
    1614          140 :         case T_JsonBehavior:
    1615          140 :             loc = exprLocation(((const JsonBehavior *) expr)->expr);
    1616          140 :             break;
    1617          110 :         case T_NullTest:
    1618              :             {
    1619          110 :                 const NullTest *nexpr = (const NullTest *) expr;
    1620              : 
    1621              :                 /* Much as above */
    1622          110 :                 loc = leftmostLoc(nexpr->location,
    1623          110 :                                   exprLocation((Node *) nexpr->arg));
    1624              :             }
    1625          110 :             break;
    1626            0 :         case T_BooleanTest:
    1627              :             {
    1628            0 :                 const BooleanTest *bexpr = (const BooleanTest *) expr;
    1629              : 
    1630              :                 /* Much as above */
    1631            0 :                 loc = leftmostLoc(bexpr->location,
    1632            0 :                                   exprLocation((Node *) bexpr->arg));
    1633              :             }
    1634            0 :             break;
    1635        53018 :         case T_CoerceToDomain:
    1636              :             {
    1637        53018 :                 const CoerceToDomain *cexpr = (const CoerceToDomain *) expr;
    1638              : 
    1639              :                 /* Much as above */
    1640        53018 :                 loc = leftmostLoc(cexpr->location,
    1641        53018 :                                   exprLocation((Node *) cexpr->arg));
    1642              :             }
    1643        53018 :             break;
    1644          708 :         case T_CoerceToDomainValue:
    1645          708 :             loc = ((const CoerceToDomainValue *) expr)->location;
    1646          708 :             break;
    1647        40134 :         case T_SetToDefault:
    1648        40134 :             loc = ((const SetToDefault *) expr)->location;
    1649        40134 :             break;
    1650            0 :         case T_ReturningExpr:
    1651            0 :             loc = exprLocation((Node *) ((const ReturningExpr *) expr)->retexpr);
    1652            0 :             break;
    1653            0 :         case T_TargetEntry:
    1654              :             /* just use argument's location */
    1655            0 :             loc = exprLocation((Node *) ((const TargetEntry *) expr)->expr);
    1656            0 :             break;
    1657           12 :         case T_IntoClause:
    1658              :             /* use the contained RangeVar's location --- close enough */
    1659           12 :             loc = exprLocation((Node *) ((const IntoClause *) expr)->rel);
    1660           12 :             break;
    1661        64381 :         case T_List:
    1662              :             {
    1663              :                 /* report location of first list member that has a location */
    1664              :                 ListCell   *lc;
    1665              : 
    1666        64381 :                 loc = -1;       /* just to suppress compiler warning */
    1667        65061 :                 foreach(lc, (const List *) expr)
    1668              :                 {
    1669        64723 :                     loc = exprLocation((Node *) lfirst(lc));
    1670        64723 :                     if (loc >= 0)
    1671        64043 :                         break;
    1672              :                 }
    1673              :             }
    1674        64381 :             break;
    1675         3562 :         case T_A_Expr:
    1676              :             {
    1677         3562 :                 const A_Expr *aexpr = (const A_Expr *) expr;
    1678              : 
    1679              :                 /* use leftmost of operator or left operand (if any) */
    1680              :                 /* we assume right operand can't be to left of operator */
    1681         3562 :                 loc = leftmostLoc(aexpr->location,
    1682         3562 :                                   exprLocation(aexpr->lexpr));
    1683              :             }
    1684         3562 :             break;
    1685        56050 :         case T_ColumnRef:
    1686        56050 :             loc = ((const ColumnRef *) expr)->location;
    1687        56050 :             break;
    1688            0 :         case T_ParamRef:
    1689            0 :             loc = ((const ParamRef *) expr)->location;
    1690            0 :             break;
    1691        51366 :         case T_A_Const:
    1692        51366 :             loc = ((const A_Const *) expr)->location;
    1693        51366 :             break;
    1694         2676 :         case T_FuncCall:
    1695              :             {
    1696         2676 :                 const FuncCall *fc = (const FuncCall *) expr;
    1697              : 
    1698              :                 /* consider both function name and leftmost arg */
    1699              :                 /* (we assume any ORDER BY nodes must be to right of name) */
    1700         2676 :                 loc = leftmostLoc(fc->location,
    1701         2676 :                                   exprLocation((Node *) fc->args));
    1702              :             }
    1703         2676 :             break;
    1704            0 :         case T_A_ArrayExpr:
    1705              :             /* the location points at ARRAY or [, which must be leftmost */
    1706            0 :             loc = ((const A_ArrayExpr *) expr)->location;
    1707            0 :             break;
    1708           12 :         case T_ResTarget:
    1709              :             /* we need not examine the contained expression (if any) */
    1710           12 :             loc = ((const ResTarget *) expr)->location;
    1711           12 :             break;
    1712            0 :         case T_MultiAssignRef:
    1713            0 :             loc = exprLocation(((const MultiAssignRef *) expr)->source);
    1714            0 :             break;
    1715         5623 :         case T_TypeCast:
    1716              :             {
    1717         5623 :                 const TypeCast *tc = (const TypeCast *) expr;
    1718              : 
    1719              :                 /*
    1720              :                  * This could represent CAST(), ::, or TypeName 'literal', so
    1721              :                  * any of the components might be leftmost.
    1722              :                  */
    1723         5623 :                 loc = exprLocation(tc->arg);
    1724         5623 :                 loc = leftmostLoc(loc, tc->typeName->location);
    1725         5623 :                 loc = leftmostLoc(loc, tc->location);
    1726              :             }
    1727         5623 :             break;
    1728          680 :         case T_CollateClause:
    1729              :             /* just use argument's location */
    1730          680 :             loc = exprLocation(((const CollateClause *) expr)->arg);
    1731          680 :             break;
    1732            8 :         case T_SortBy:
    1733              :             /* just use argument's location (ignore operator, if any) */
    1734            8 :             loc = exprLocation(((const SortBy *) expr)->node);
    1735            8 :             break;
    1736            0 :         case T_WindowDef:
    1737            0 :             loc = ((const WindowDef *) expr)->location;
    1738            0 :             break;
    1739            0 :         case T_RangeTableSample:
    1740            0 :             loc = ((const RangeTableSample *) expr)->location;
    1741            0 :             break;
    1742            0 :         case T_TypeName:
    1743            0 :             loc = ((const TypeName *) expr)->location;
    1744            0 :             break;
    1745           12 :         case T_ColumnDef:
    1746           12 :             loc = ((const ColumnDef *) expr)->location;
    1747           12 :             break;
    1748            0 :         case T_IndexElem:
    1749            0 :             loc = ((const IndexElem *) expr)->location;
    1750            0 :             break;
    1751            0 :         case T_Constraint:
    1752            0 :             loc = ((const Constraint *) expr)->location;
    1753            0 :             break;
    1754            0 :         case T_FunctionParameter:
    1755            0 :             loc = ((const FunctionParameter *) expr)->location;
    1756            0 :             break;
    1757            0 :         case T_XmlSerialize:
    1758              :             /* XMLSERIALIZE keyword should always be the first thing */
    1759            0 :             loc = ((const XmlSerialize *) expr)->location;
    1760            0 :             break;
    1761           24 :         case T_GroupingSet:
    1762           24 :             loc = ((const GroupingSet *) expr)->location;
    1763           24 :             break;
    1764            0 :         case T_WithClause:
    1765            0 :             loc = ((const WithClause *) expr)->location;
    1766            0 :             break;
    1767            0 :         case T_InferClause:
    1768            0 :             loc = ((const InferClause *) expr)->location;
    1769            0 :             break;
    1770            4 :         case T_OnConflictClause:
    1771            4 :             loc = ((const OnConflictClause *) expr)->location;
    1772            4 :             break;
    1773            0 :         case T_CTESearchClause:
    1774            0 :             loc = ((const CTESearchClause *) expr)->location;
    1775            0 :             break;
    1776            0 :         case T_CTECycleClause:
    1777            0 :             loc = ((const CTECycleClause *) expr)->location;
    1778            0 :             break;
    1779            0 :         case T_CommonTableExpr:
    1780            0 :             loc = ((const CommonTableExpr *) expr)->location;
    1781            0 :             break;
    1782            0 :         case T_JsonKeyValue:
    1783              :             /* just use the key's location */
    1784            0 :             loc = exprLocation((Node *) ((const JsonKeyValue *) expr)->key);
    1785            0 :             break;
    1786            0 :         case T_JsonObjectConstructor:
    1787            0 :             loc = ((const JsonObjectConstructor *) expr)->location;
    1788            0 :             break;
    1789            0 :         case T_JsonArrayConstructor:
    1790            0 :             loc = ((const JsonArrayConstructor *) expr)->location;
    1791            0 :             break;
    1792            0 :         case T_JsonArrayQueryConstructor:
    1793            0 :             loc = ((const JsonArrayQueryConstructor *) expr)->location;
    1794            0 :             break;
    1795            0 :         case T_JsonAggConstructor:
    1796            0 :             loc = ((const JsonAggConstructor *) expr)->location;
    1797            0 :             break;
    1798            0 :         case T_JsonObjectAgg:
    1799            0 :             loc = exprLocation((Node *) ((const JsonObjectAgg *) expr)->constructor);
    1800            0 :             break;
    1801            0 :         case T_JsonArrayAgg:
    1802            0 :             loc = exprLocation((Node *) ((const JsonArrayAgg *) expr)->constructor);
    1803            0 :             break;
    1804            0 :         case T_PlaceHolderVar:
    1805              :             /* just use argument's location */
    1806            0 :             loc = exprLocation((Node *) ((const PlaceHolderVar *) expr)->phexpr);
    1807            0 :             break;
    1808            0 :         case T_InferenceElem:
    1809              :             /* just use nested expr's location */
    1810            0 :             loc = exprLocation((Node *) ((const InferenceElem *) expr)->expr);
    1811            0 :             break;
    1812            0 :         case T_PartitionElem:
    1813            0 :             loc = ((const PartitionElem *) expr)->location;
    1814            0 :             break;
    1815            0 :         case T_PartitionSpec:
    1816            0 :             loc = ((const PartitionSpec *) expr)->location;
    1817            0 :             break;
    1818           36 :         case T_PartitionBoundSpec:
    1819           36 :             loc = ((const PartitionBoundSpec *) expr)->location;
    1820           36 :             break;
    1821           32 :         case T_PartitionRangeDatum:
    1822           32 :             loc = ((const PartitionRangeDatum *) expr)->location;
    1823           32 :             break;
    1824        27654 :         default:
    1825              :             /* for any other node type it's just unknown... */
    1826        27654 :             loc = -1;
    1827        27654 :             break;
    1828              :     }
    1829      2950089 :     return loc;
    1830              : }
    1831              : 
    1832              : /*
    1833              :  * leftmostLoc - support for exprLocation
    1834              :  *
    1835              :  * Take the minimum of two parse location values, but ignore unknowns
    1836              :  */
    1837              : static int
    1838       174943 : leftmostLoc(int loc1, int loc2)
    1839              : {
    1840       174943 :     if (loc1 < 0)
    1841        13517 :         return loc2;
    1842       161426 :     else if (loc2 < 0)
    1843        16937 :         return loc1;
    1844              :     else
    1845       144489 :         return Min(loc1, loc2);
    1846              : }
    1847              : 
    1848              : 
    1849              : /*
    1850              :  * fix_opfuncids
    1851              :  *    Calculate opfuncid field from opno for each OpExpr node in given tree.
    1852              :  *    The given tree can be anything expression_tree_walker handles.
    1853              :  *
    1854              :  * The argument is modified in-place.  (This is OK since we'd want the
    1855              :  * same change for any node, even if it gets visited more than once due to
    1856              :  * shared structure.)
    1857              :  */
    1858              : void
    1859       319175 : fix_opfuncids(Node *node)
    1860              : {
    1861              :     /* This tree walk requires no special setup, so away we go... */
    1862       319175 :     fix_opfuncids_walker(node, NULL);
    1863       319175 : }
    1864              : 
    1865              : static bool
    1866       769383 : fix_opfuncids_walker(Node *node, void *context)
    1867              : {
    1868       769383 :     if (node == NULL)
    1869        38476 :         return false;
    1870       730907 :     if (IsA(node, OpExpr))
    1871        47049 :         set_opfuncid((OpExpr *) node);
    1872       683858 :     else if (IsA(node, DistinctExpr))
    1873            4 :         set_opfuncid((OpExpr *) node);  /* rely on struct equivalence */
    1874       683854 :     else if (IsA(node, NullIfExpr))
    1875          671 :         set_opfuncid((OpExpr *) node);  /* rely on struct equivalence */
    1876       683183 :     else if (IsA(node, ScalarArrayOpExpr))
    1877         1778 :         set_sa_opfuncid((ScalarArrayOpExpr *) node);
    1878       730907 :     return expression_tree_walker(node, fix_opfuncids_walker, context);
    1879              : }
    1880              : 
    1881              : /*
    1882              :  * set_opfuncid
    1883              :  *      Set the opfuncid (procedure OID) in an OpExpr node,
    1884              :  *      if it hasn't been set already.
    1885              :  *
    1886              :  * Because of struct equivalence, this can also be used for
    1887              :  * DistinctExpr and NullIfExpr nodes.
    1888              :  */
    1889              : void
    1890      3162553 : set_opfuncid(OpExpr *opexpr)
    1891              : {
    1892      3162553 :     if (opexpr->opfuncid == InvalidOid)
    1893       190918 :         opexpr->opfuncid = get_opcode(opexpr->opno);
    1894      3162553 : }
    1895              : 
    1896              : /*
    1897              :  * set_sa_opfuncid
    1898              :  *      As above, for ScalarArrayOpExpr nodes.
    1899              :  */
    1900              : void
    1901       154711 : set_sa_opfuncid(ScalarArrayOpExpr *opexpr)
    1902              : {
    1903       154711 :     if (opexpr->opfuncid == InvalidOid)
    1904          420 :         opexpr->opfuncid = get_opcode(opexpr->opno);
    1905       154711 : }
    1906              : 
    1907              : 
    1908              : /*
    1909              :  *  check_functions_in_node -
    1910              :  *    apply checker() to each function OID contained in given expression node
    1911              :  *
    1912              :  * Returns true if the checker() function does; for nodes representing more
    1913              :  * than one function call, returns true if the checker() function does so
    1914              :  * for any of those functions.  Returns false if node does not invoke any
    1915              :  * SQL-visible function.  Caller must not pass node == NULL.
    1916              :  *
    1917              :  * This function examines only the given node; it does not recurse into any
    1918              :  * sub-expressions.  Callers typically prefer to keep control of the recursion
    1919              :  * for themselves, in case additional checks should be made, or because they
    1920              :  * have special rules about which parts of the tree need to be visited.
    1921              :  *
    1922              :  * Note: we ignore MinMaxExpr, SQLValueFunction, XmlExpr, CoerceToDomain,
    1923              :  * and NextValueExpr nodes, because they do not contain SQL function OIDs.
    1924              :  * However, they can invoke SQL-visible functions, so callers should take
    1925              :  * thought about how to treat them.
    1926              :  */
    1927              : bool
    1928     18445656 : check_functions_in_node(Node *node, check_function_callback checker,
    1929              :                         void *context)
    1930              : {
    1931     18445656 :     switch (nodeTag(node))
    1932              :     {
    1933        94625 :         case T_Aggref:
    1934              :             {
    1935        94625 :                 Aggref     *expr = (Aggref *) node;
    1936              : 
    1937        94625 :                 if (checker(expr->aggfnoid, context))
    1938          860 :                     return true;
    1939              :             }
    1940        93765 :             break;
    1941         6508 :         case T_WindowFunc:
    1942              :             {
    1943         6508 :                 WindowFunc *expr = (WindowFunc *) node;
    1944              : 
    1945         6508 :                 if (checker(expr->winfnoid, context))
    1946          125 :                     return true;
    1947              :             }
    1948         6383 :             break;
    1949       550598 :         case T_FuncExpr:
    1950              :             {
    1951       550598 :                 FuncExpr   *expr = (FuncExpr *) node;
    1952              : 
    1953       550598 :                 if (checker(expr->funcid, context))
    1954        74290 :                     return true;
    1955              :             }
    1956       476308 :             break;
    1957      1245663 :         case T_OpExpr:
    1958              :         case T_DistinctExpr:    /* struct-equivalent to OpExpr */
    1959              :         case T_NullIfExpr:      /* struct-equivalent to OpExpr */
    1960              :             {
    1961      1245663 :                 OpExpr     *expr = (OpExpr *) node;
    1962              : 
    1963              :                 /* Set opfuncid if it wasn't set already */
    1964      1245663 :                 set_opfuncid(expr);
    1965      1245663 :                 if (checker(expr->opfuncid, context))
    1966         1388 :                     return true;
    1967              :             }
    1968      1244275 :             break;
    1969        52954 :         case T_ScalarArrayOpExpr:
    1970              :             {
    1971        52954 :                 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
    1972              : 
    1973        52954 :                 set_sa_opfuncid(expr);
    1974        52954 :                 if (checker(expr->opfuncid, context))
    1975          167 :                     return true;
    1976              :             }
    1977        52787 :             break;
    1978        39883 :         case T_CoerceViaIO:
    1979              :             {
    1980        39883 :                 CoerceViaIO *expr = (CoerceViaIO *) node;
    1981              :                 Oid         iofunc;
    1982              :                 Oid         typioparam;
    1983              :                 bool        typisvarlena;
    1984              : 
    1985              :                 /* check the result type's input function */
    1986        39883 :                 getTypeInputInfo(expr->resulttype,
    1987              :                                  &iofunc, &typioparam);
    1988        39883 :                 if (checker(iofunc, context))
    1989          375 :                     return true;
    1990              :                 /* check the input type's output function */
    1991        39852 :                 getTypeOutputInfo(exprType((Node *) expr->arg),
    1992              :                                   &iofunc, &typisvarlena);
    1993        39852 :                 if (checker(iofunc, context))
    1994          344 :                     return true;
    1995              :             }
    1996        39508 :             break;
    1997          304 :         case T_RowCompareExpr:
    1998              :             {
    1999          304 :                 RowCompareExpr *rcexpr = (RowCompareExpr *) node;
    2000              :                 ListCell   *opid;
    2001              : 
    2002          997 :                 foreach(opid, rcexpr->opnos)
    2003              :                 {
    2004          693 :                     Oid         opfuncid = get_opcode(lfirst_oid(opid));
    2005              : 
    2006          693 :                     if (checker(opfuncid, context))
    2007            0 :                         return true;
    2008              :                 }
    2009              :             }
    2010          304 :             break;
    2011     16455121 :         default:
    2012     16455121 :             break;
    2013              :     }
    2014     18368451 :     return false;
    2015              : }
    2016              : 
    2017              : 
    2018              : /*
    2019              :  * Standard expression-tree walking support
    2020              :  *
    2021              :  * We used to have near-duplicate code in many different routines that
    2022              :  * understood how to recurse through an expression node tree.  That was
    2023              :  * a pain to maintain, and we frequently had bugs due to some particular
    2024              :  * routine neglecting to support a particular node type.  In most cases,
    2025              :  * these routines only actually care about certain node types, and don't
    2026              :  * care about other types except insofar as they have to recurse through
    2027              :  * non-primitive node types.  Therefore, we now provide generic tree-walking
    2028              :  * logic to consolidate the redundant "boilerplate" code.  There are
    2029              :  * two versions: expression_tree_walker() and expression_tree_mutator().
    2030              :  */
    2031              : 
    2032              : /*
    2033              :  * expression_tree_walker() is designed to support routines that traverse
    2034              :  * a tree in a read-only fashion (although it will also work for routines
    2035              :  * that modify nodes in-place but never add/delete/replace nodes).
    2036              :  * A walker routine should look like this:
    2037              :  *
    2038              :  * bool my_walker (Node *node, my_struct *context)
    2039              :  * {
    2040              :  *      if (node == NULL)
    2041              :  *          return false;
    2042              :  *      // check for nodes that special work is required for, eg:
    2043              :  *      if (IsA(node, Var))
    2044              :  *      {
    2045              :  *          ... do special actions for Var nodes
    2046              :  *      }
    2047              :  *      else if (IsA(node, ...))
    2048              :  *      {
    2049              :  *          ... do special actions for other node types
    2050              :  *      }
    2051              :  *      // for any node type not specially processed, do:
    2052              :  *      return expression_tree_walker(node, my_walker, context);
    2053              :  * }
    2054              :  *
    2055              :  * The "context" argument points to a struct that holds whatever context
    2056              :  * information the walker routine needs --- it can be used to return data
    2057              :  * gathered by the walker, too.  This argument is not touched by
    2058              :  * expression_tree_walker, but it is passed down to recursive sub-invocations
    2059              :  * of my_walker.  The tree walk is started from a setup routine that
    2060              :  * fills in the appropriate context struct, calls my_walker with the top-level
    2061              :  * node of the tree, and then examines the results.
    2062              :  *
    2063              :  * The walker routine should return "false" to continue the tree walk, or
    2064              :  * "true" to abort the walk and immediately return "true" to the top-level
    2065              :  * caller.  This can be used to short-circuit the traversal if the walker
    2066              :  * has found what it came for.  "false" is returned to the top-level caller
    2067              :  * iff no invocation of the walker returned "true".
    2068              :  *
    2069              :  * The node types handled by expression_tree_walker include all those
    2070              :  * normally found in target lists and qualifier clauses during the planning
    2071              :  * stage.  In particular, it handles List nodes since a cnf-ified qual clause
    2072              :  * will have List structure at the top level, and it handles TargetEntry nodes
    2073              :  * so that a scan of a target list can be handled without additional code.
    2074              :  * Also, RangeTblRef, FromExpr, JoinExpr, and SetOperationStmt nodes are
    2075              :  * handled, so that query jointrees and setOperation trees can be processed
    2076              :  * without additional code.
    2077              :  *
    2078              :  * expression_tree_walker will handle SubLink nodes by recursing normally
    2079              :  * into the "testexpr" subtree (which is an expression belonging to the outer
    2080              :  * plan).  It will also call the walker on the sub-Query node; however, when
    2081              :  * expression_tree_walker itself is called on a Query node, it does nothing
    2082              :  * and returns "false".  The net effect is that unless the walker does
    2083              :  * something special at a Query node, sub-selects will not be visited during
    2084              :  * an expression tree walk. This is exactly the behavior wanted in many cases
    2085              :  * --- and for those walkers that do want to recurse into sub-selects, special
    2086              :  * behavior is typically needed anyway at the entry to a sub-select (such as
    2087              :  * incrementing a depth counter). A walker that wants to examine sub-selects
    2088              :  * should include code along the lines of:
    2089              :  *
    2090              :  *      if (IsA(node, Query))
    2091              :  *      {
    2092              :  *          adjust context for subquery;
    2093              :  *          result = query_tree_walker((Query *) node, my_walker, context,
    2094              :  *                                     0); // adjust flags as needed
    2095              :  *          restore context if needed;
    2096              :  *          return result;
    2097              :  *      }
    2098              :  *
    2099              :  * query_tree_walker is a convenience routine (see below) that calls the
    2100              :  * walker on all the expression subtrees of the given Query node.
    2101              :  *
    2102              :  * expression_tree_walker will handle SubPlan nodes by recursing normally
    2103              :  * into the "testexpr" and the "args" list (which are expressions belonging to
    2104              :  * the outer plan).  It will not touch the completed subplan, however.  Since
    2105              :  * there is no link to the original Query, it is not possible to recurse into
    2106              :  * subselects of an already-planned expression tree.  This is OK for current
    2107              :  * uses, but may need to be revisited in future.
    2108              :  */
    2109              : 
    2110              : bool
    2111     82683968 : expression_tree_walker_impl(Node *node,
    2112              :                             tree_walker_callback walker,
    2113              :                             void *context)
    2114              : {
    2115              :     ListCell   *temp;
    2116              : 
    2117              :     /*
    2118              :      * The walker has already visited the current node, and so we need only
    2119              :      * recurse into any sub-nodes it has.
    2120              :      *
    2121              :      * We assume that the walker is not interested in List nodes per se, so
    2122              :      * when we expect a List we just recurse directly to self without
    2123              :      * bothering to call the walker.
    2124              :      */
    2125              : #define WALK(n) walker((Node *) (n), context)
    2126              : 
    2127              : #define LIST_WALK(l) expression_tree_walker_impl((Node *) (l), walker, context)
    2128              : 
    2129     82683968 :     if (node == NULL)
    2130      1594426 :         return false;
    2131              : 
    2132              :     /* Guard against stack overflow due to overly complex expressions */
    2133     81089542 :     check_stack_depth();
    2134              : 
    2135     81089538 :     switch (nodeTag(node))
    2136              :     {
    2137     34753645 :         case T_Var:
    2138              :         case T_Const:
    2139              :         case T_Param:
    2140              :         case T_CaseTestExpr:
    2141              :         case T_SQLValueFunction:
    2142              :         case T_CoerceToDomainValue:
    2143              :         case T_SetToDefault:
    2144              :         case T_CurrentOfExpr:
    2145              :         case T_NextValueExpr:
    2146              :         case T_RangeTblRef:
    2147              :         case T_SortGroupClause:
    2148              :         case T_CTESearchClause:
    2149              :         case T_GraphLabelRef:
    2150              :         case T_GraphPropertyRef:
    2151              :         case T_MergeSupportFunc:
    2152              :             /* primitive node types with no expression subnodes */
    2153     34753645 :             break;
    2154         6691 :         case T_WithCheckOption:
    2155         6691 :             return WALK(((WithCheckOption *) node)->qual);
    2156       277602 :         case T_Aggref:
    2157              :             {
    2158       277602 :                 Aggref     *expr = (Aggref *) node;
    2159              : 
    2160              :                 /* recurse directly on Lists */
    2161       277602 :                 if (LIST_WALK(expr->aggdirectargs))
    2162            0 :                     return true;
    2163       277602 :                 if (LIST_WALK(expr->args))
    2164        14742 :                     return true;
    2165       262860 :                 if (LIST_WALK(expr->aggorder))
    2166            0 :                     return true;
    2167       262860 :                 if (LIST_WALK(expr->aggdistinct))
    2168            0 :                     return true;
    2169       262860 :                 if (WALK(expr->aggfilter))
    2170           70 :                     return true;
    2171              :             }
    2172       262790 :             break;
    2173         3136 :         case T_GroupingFunc:
    2174              :             {
    2175         3136 :                 GroupingFunc *grouping = (GroupingFunc *) node;
    2176              : 
    2177         3136 :                 if (LIST_WALK(grouping->args))
    2178          190 :                     return true;
    2179              :             }
    2180         2946 :             break;
    2181        16118 :         case T_WindowFunc:
    2182              :             {
    2183        16118 :                 WindowFunc *expr = (WindowFunc *) node;
    2184              : 
    2185              :                 /* recurse directly on List */
    2186        16118 :                 if (LIST_WALK(expr->args))
    2187          471 :                     return true;
    2188        15647 :                 if (WALK(expr->aggfilter))
    2189            9 :                     return true;
    2190        15638 :                 if (WALK(expr->runCondition))
    2191            0 :                     return true;
    2192              :             }
    2193        15638 :             break;
    2194          458 :         case T_WindowFuncRunCondition:
    2195              :             {
    2196          458 :                 WindowFuncRunCondition *expr = (WindowFuncRunCondition *) node;
    2197              : 
    2198          458 :                 if (WALK(expr->arg))
    2199            0 :                     return true;
    2200              :             }
    2201          458 :             break;
    2202       207833 :         case T_SubscriptingRef:
    2203              :             {
    2204       207833 :                 SubscriptingRef *sbsref = (SubscriptingRef *) node;
    2205              : 
    2206              :                 /* recurse directly for upper/lower container index lists */
    2207       207833 :                 if (LIST_WALK(sbsref->refupperindexpr))
    2208         9961 :                     return true;
    2209       197872 :                 if (LIST_WALK(sbsref->reflowerindexpr))
    2210            0 :                     return true;
    2211              :                 /* walker must see the refexpr and refassgnexpr, however */
    2212       197872 :                 if (WALK(sbsref->refexpr))
    2213        11619 :                     return true;
    2214              : 
    2215       186253 :                 if (WALK(sbsref->refassgnexpr))
    2216          126 :                     return true;
    2217              :             }
    2218       186127 :             break;
    2219      2716489 :         case T_FuncExpr:
    2220              :             {
    2221      2716489 :                 FuncExpr   *expr = (FuncExpr *) node;
    2222              : 
    2223      2716489 :                 if (LIST_WALK(expr->args))
    2224        61384 :                     return true;
    2225              :             }
    2226      2655097 :             break;
    2227        55928 :         case T_NamedArgExpr:
    2228        55928 :             return WALK(((NamedArgExpr *) node)->arg);
    2229      6478431 :         case T_OpExpr:
    2230              :         case T_DistinctExpr:    /* struct-equivalent to OpExpr */
    2231              :         case T_NullIfExpr:      /* struct-equivalent to OpExpr */
    2232              :             {
    2233      6478431 :                 OpExpr     *expr = (OpExpr *) node;
    2234              : 
    2235      6478431 :                 if (LIST_WALK(expr->args))
    2236        63890 :                     return true;
    2237              :             }
    2238      6414457 :             break;
    2239       347469 :         case T_ScalarArrayOpExpr:
    2240              :             {
    2241       347469 :                 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
    2242              : 
    2243       347469 :                 if (LIST_WALK(expr->args))
    2244        33705 :                     return true;
    2245              :             }
    2246       313764 :             break;
    2247       955796 :         case T_BoolExpr:
    2248              :             {
    2249       955796 :                 BoolExpr   *expr = (BoolExpr *) node;
    2250              : 
    2251       955796 :                 if (LIST_WALK(expr->args))
    2252        10429 :                     return true;
    2253              :             }
    2254       945359 :             break;
    2255       200138 :         case T_SubLink:
    2256              :             {
    2257       200138 :                 SubLink    *sublink = (SubLink *) node;
    2258              : 
    2259       200138 :                 if (WALK(sublink->testexpr))
    2260           56 :                     return true;
    2261              : 
    2262              :                 /*
    2263              :                  * Also invoke the walker on the sublink's Query node, so it
    2264              :                  * can recurse into the sub-query if it wants to.
    2265              :                  */
    2266       200082 :                 return WALK(sublink->subselect);
    2267              :             }
    2268              :             break;
    2269        86508 :         case T_SubPlan:
    2270              :             {
    2271        86508 :                 SubPlan    *subplan = (SubPlan *) node;
    2272              : 
    2273              :                 /* recurse into the testexpr, but not into the Plan */
    2274        86508 :                 if (WALK(subplan->testexpr))
    2275           58 :                     return true;
    2276              :                 /* also examine args list */
    2277        86450 :                 if (LIST_WALK(subplan->args))
    2278          350 :                     return true;
    2279              :             }
    2280        86100 :             break;
    2281         6243 :         case T_AlternativeSubPlan:
    2282         6243 :             return LIST_WALK(((AlternativeSubPlan *) node)->subplans);
    2283       247133 :         case T_FieldSelect:
    2284       247133 :             return WALK(((FieldSelect *) node)->arg);
    2285         2347 :         case T_FieldStore:
    2286              :             {
    2287         2347 :                 FieldStore *fstore = (FieldStore *) node;
    2288              : 
    2289         2347 :                 if (WALK(fstore->arg))
    2290            0 :                     return true;
    2291         2347 :                 if (WALK(fstore->newvals))
    2292            8 :                     return true;
    2293              :             }
    2294         2339 :             break;
    2295      1004813 :         case T_RelabelType:
    2296      1004813 :             return WALK(((RelabelType *) node)->arg);
    2297       205852 :         case T_CoerceViaIO:
    2298       205852 :             return WALK(((CoerceViaIO *) node)->arg);
    2299        40122 :         case T_ArrayCoerceExpr:
    2300              :             {
    2301        40122 :                 ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node;
    2302              : 
    2303        40122 :                 if (WALK(acoerce->arg))
    2304         3386 :                     return true;
    2305        36736 :                 if (WALK(acoerce->elemexpr))
    2306           20 :                     return true;
    2307              :             }
    2308        36716 :             break;
    2309         2309 :         case T_ConvertRowtypeExpr:
    2310         2309 :             return WALK(((ConvertRowtypeExpr *) node)->arg);
    2311        26492 :         case T_CollateExpr:
    2312        26492 :             return WALK(((CollateExpr *) node)->arg);
    2313       355587 :         case T_CaseExpr:
    2314              :             {
    2315       355587 :                 CaseExpr   *caseexpr = (CaseExpr *) node;
    2316              : 
    2317       355587 :                 if (WALK(caseexpr->arg))
    2318          319 :                     return true;
    2319              :                 /* we assume walker doesn't care about CaseWhens, either */
    2320      1045664 :                 foreach(temp, caseexpr->args)
    2321              :                 {
    2322       698120 :                     CaseWhen   *when = lfirst_node(CaseWhen, temp);
    2323              : 
    2324       698120 :                     if (WALK(when->expr))
    2325         7724 :                         return true;
    2326       696045 :                     if (WALK(when->result))
    2327         5649 :                         return true;
    2328              :                 }
    2329       347544 :                 if (WALK(caseexpr->defresult))
    2330         7318 :                     return true;
    2331              :             }
    2332       340226 :             break;
    2333       164984 :         case T_ArrayExpr:
    2334       164984 :             return WALK(((ArrayExpr *) node)->elements);
    2335        26991 :         case T_RowExpr:
    2336              :             /* Assume colnames isn't interesting */
    2337        26991 :             return WALK(((RowExpr *) node)->args);
    2338         2283 :         case T_RowCompareExpr:
    2339              :             {
    2340         2283 :                 RowCompareExpr *rcexpr = (RowCompareExpr *) node;
    2341              : 
    2342         2283 :                 if (WALK(rcexpr->largs))
    2343            0 :                     return true;
    2344         2283 :                 if (WALK(rcexpr->rargs))
    2345            0 :                     return true;
    2346              :             }
    2347         2283 :             break;
    2348        42982 :         case T_CoalesceExpr:
    2349        42982 :             return WALK(((CoalesceExpr *) node)->args);
    2350         4465 :         case T_MinMaxExpr:
    2351         4465 :             return WALK(((MinMaxExpr *) node)->args);
    2352         3957 :         case T_XmlExpr:
    2353              :             {
    2354         3957 :                 XmlExpr    *xexpr = (XmlExpr *) node;
    2355              : 
    2356         3957 :                 if (WALK(xexpr->named_args))
    2357            8 :                     return true;
    2358              :                 /* we assume walker doesn't care about arg_names */
    2359         3949 :                 if (WALK(xexpr->args))
    2360           16 :                     return true;
    2361              :             }
    2362         3933 :             break;
    2363         2977 :         case T_JsonValueExpr:
    2364              :             {
    2365         2977 :                 JsonValueExpr *jve = (JsonValueExpr *) node;
    2366              : 
    2367         2977 :                 if (WALK(jve->raw_expr))
    2368           40 :                     return true;
    2369         2937 :                 if (WALK(jve->formatted_expr))
    2370            0 :                     return true;
    2371              :             }
    2372         2937 :             break;
    2373         8633 :         case T_JsonConstructorExpr:
    2374              :             {
    2375         8633 :                 JsonConstructorExpr *ctor = (JsonConstructorExpr *) node;
    2376              : 
    2377         8633 :                 if (WALK(ctor->args))
    2378           58 :                     return true;
    2379         8575 :                 if (WALK(ctor->func))
    2380           80 :                     return true;
    2381         8495 :                 if (WALK(ctor->coercion))
    2382           10 :                     return true;
    2383              :             }
    2384         8485 :             break;
    2385         2141 :         case T_JsonIsPredicate:
    2386         2141 :             return WALK(((JsonIsPredicate *) node)->expr);
    2387        14049 :         case T_JsonExpr:
    2388              :             {
    2389        14049 :                 JsonExpr   *jexpr = (JsonExpr *) node;
    2390              : 
    2391        14049 :                 if (WALK(jexpr->formatted_expr))
    2392           56 :                     return true;
    2393        13993 :                 if (WALK(jexpr->path_spec))
    2394            4 :                     return true;
    2395        13989 :                 if (WALK(jexpr->passing_values))
    2396            4 :                     return true;
    2397              :                 /* we assume walker doesn't care about passing_names */
    2398        13985 :                 if (WALK(jexpr->on_empty))
    2399           30 :                     return true;
    2400        13955 :                 if (WALK(jexpr->on_error))
    2401           24 :                     return true;
    2402              :             }
    2403        13931 :             break;
    2404        24688 :         case T_JsonBehavior:
    2405              :             {
    2406        24688 :                 JsonBehavior *behavior = (JsonBehavior *) node;
    2407              : 
    2408        24688 :                 if (WALK(behavior->expr))
    2409           54 :                     return true;
    2410              :             }
    2411        24634 :             break;
    2412       194158 :         case T_NullTest:
    2413       194158 :             return WALK(((NullTest *) node)->arg);
    2414        10983 :         case T_BooleanTest:
    2415        10983 :             return WALK(((BooleanTest *) node)->arg);
    2416       263399 :         case T_CoerceToDomain:
    2417       263399 :             return WALK(((CoerceToDomain *) node)->arg);
    2418     12828061 :         case T_TargetEntry:
    2419     12828061 :             return WALK(((TargetEntry *) node)->expr);
    2420       100610 :         case T_Query:
    2421              :             /* Do nothing with a sub-Query, per discussion above */
    2422       100610 :             break;
    2423          162 :         case T_WindowClause:
    2424              :             {
    2425          162 :                 WindowClause *wc = (WindowClause *) node;
    2426              : 
    2427          162 :                 if (WALK(wc->partitionClause))
    2428            0 :                     return true;
    2429          162 :                 if (WALK(wc->orderClause))
    2430            0 :                     return true;
    2431          162 :                 if (WALK(wc->startOffset))
    2432            0 :                     return true;
    2433          162 :                 if (WALK(wc->endOffset))
    2434            0 :                     return true;
    2435              :             }
    2436          162 :             break;
    2437           68 :         case T_CTECycleClause:
    2438              :             {
    2439           68 :                 CTECycleClause *cc = (CTECycleClause *) node;
    2440              : 
    2441           68 :                 if (WALK(cc->cycle_mark_value))
    2442            0 :                     return true;
    2443           68 :                 if (WALK(cc->cycle_mark_default))
    2444            0 :                     return true;
    2445              :             }
    2446           68 :             break;
    2447         5999 :         case T_CommonTableExpr:
    2448              :             {
    2449         5999 :                 CommonTableExpr *cte = (CommonTableExpr *) node;
    2450              : 
    2451              :                 /*
    2452              :                  * Invoke the walker on the CTE's Query node, so it can
    2453              :                  * recurse into the sub-query if it wants to.
    2454              :                  */
    2455         5999 :                 if (WALK(cte->ctequery))
    2456          154 :                     return true;
    2457              : 
    2458         5845 :                 if (WALK(cte->search_clause))
    2459            0 :                     return true;
    2460         5845 :                 if (WALK(cte->cycle_clause))
    2461            0 :                     return true;
    2462              :             }
    2463         5845 :             break;
    2464            0 :         case T_JsonKeyValue:
    2465              :             {
    2466            0 :                 JsonKeyValue *kv = (JsonKeyValue *) node;
    2467              : 
    2468            0 :                 if (WALK(kv->key))
    2469            0 :                     return true;
    2470            0 :                 if (WALK(kv->value))
    2471            0 :                     return true;
    2472              :             }
    2473            0 :             break;
    2474            0 :         case T_JsonObjectConstructor:
    2475              :             {
    2476            0 :                 JsonObjectConstructor *ctor = (JsonObjectConstructor *) node;
    2477              : 
    2478            0 :                 if (LIST_WALK(ctor->exprs))
    2479            0 :                     return true;
    2480              :             }
    2481            0 :             break;
    2482            0 :         case T_JsonArrayConstructor:
    2483              :             {
    2484            0 :                 JsonArrayConstructor *ctor = (JsonArrayConstructor *) node;
    2485              : 
    2486            0 :                 if (LIST_WALK(ctor->exprs))
    2487            0 :                     return true;
    2488              :             }
    2489            0 :             break;
    2490            0 :         case T_JsonArrayQueryConstructor:
    2491              :             {
    2492            0 :                 JsonArrayQueryConstructor *ctor = (JsonArrayQueryConstructor *) node;
    2493              : 
    2494            0 :                 if (WALK(ctor->query))
    2495            0 :                     return true;
    2496              :             }
    2497            0 :             break;
    2498            0 :         case T_JsonAggConstructor:
    2499              :             {
    2500            0 :                 JsonAggConstructor *ctor = (JsonAggConstructor *) node;
    2501              : 
    2502            0 :                 if (WALK(ctor->agg_filter))
    2503            0 :                     return true;
    2504            0 :                 if (WALK(ctor->agg_order))
    2505            0 :                     return true;
    2506            0 :                 if (WALK(ctor->over))
    2507            0 :                     return true;
    2508              :             }
    2509            0 :             break;
    2510            0 :         case T_JsonObjectAgg:
    2511              :             {
    2512            0 :                 JsonObjectAgg *ctor = (JsonObjectAgg *) node;
    2513              : 
    2514            0 :                 if (WALK(ctor->constructor))
    2515            0 :                     return true;
    2516            0 :                 if (WALK(ctor->arg))
    2517            0 :                     return true;
    2518              :             }
    2519            0 :             break;
    2520            0 :         case T_JsonArrayAgg:
    2521              :             {
    2522            0 :                 JsonArrayAgg *ctor = (JsonArrayAgg *) node;
    2523              : 
    2524            0 :                 if (WALK(ctor->constructor))
    2525            0 :                     return true;
    2526            0 :                 if (WALK(ctor->arg))
    2527            0 :                     return true;
    2528              :             }
    2529            0 :             break;
    2530              : 
    2531         2746 :         case T_PartitionBoundSpec:
    2532              :             {
    2533         2746 :                 PartitionBoundSpec *pbs = (PartitionBoundSpec *) node;
    2534              : 
    2535         2746 :                 if (WALK(pbs->listdatums))
    2536            0 :                     return true;
    2537         2746 :                 if (WALK(pbs->lowerdatums))
    2538            0 :                     return true;
    2539         2746 :                 if (WALK(pbs->upperdatums))
    2540            0 :                     return true;
    2541              :             }
    2542         2746 :             break;
    2543         3596 :         case T_PartitionRangeDatum:
    2544              :             {
    2545         3596 :                 PartitionRangeDatum *prd = (PartitionRangeDatum *) node;
    2546              : 
    2547         3596 :                 if (WALK(prd->value))
    2548            0 :                     return true;
    2549              :             }
    2550         3596 :             break;
    2551     17905713 :         case T_List:
    2552     61725391 :             foreach(temp, (List *) node)
    2553              :             {
    2554     44427386 :                 if (WALK(lfirst(temp)))
    2555       607544 :                     return true;
    2556              :             }
    2557     17298005 :             break;
    2558      1031349 :         case T_FromExpr:
    2559              :             {
    2560      1031349 :                 FromExpr   *from = (FromExpr *) node;
    2561              : 
    2562      1031349 :                 if (LIST_WALK(from->fromlist))
    2563        47295 :                     return true;
    2564       984054 :                 if (WALK(from->quals))
    2565         2432 :                     return true;
    2566              :             }
    2567       981614 :             break;
    2568         2467 :         case T_OnConflictExpr:
    2569              :             {
    2570         2467 :                 OnConflictExpr *onconflict = (OnConflictExpr *) node;
    2571              : 
    2572         2467 :                 if (WALK(onconflict->arbiterElems))
    2573            0 :                     return true;
    2574         2467 :                 if (WALK(onconflict->arbiterWhere))
    2575            0 :                     return true;
    2576         2467 :                 if (WALK(onconflict->onConflictSet))
    2577            0 :                     return true;
    2578         2467 :                 if (WALK(onconflict->onConflictWhere))
    2579            0 :                     return true;
    2580         2467 :                 if (WALK(onconflict->exclRelTlist))
    2581            0 :                     return true;
    2582              :             }
    2583         2467 :             break;
    2584         4550 :         case T_MergeAction:
    2585              :             {
    2586         4550 :                 MergeAction *action = (MergeAction *) node;
    2587              : 
    2588         4550 :                 if (WALK(action->qual))
    2589           93 :                     return true;
    2590         4457 :                 if (WALK(action->targetList))
    2591          233 :                     return true;
    2592              :             }
    2593         4224 :             break;
    2594         1091 :         case T_ForPortionOfExpr:
    2595              :             {
    2596         1091 :                 ForPortionOfExpr *forPortionOf = (ForPortionOfExpr *) node;
    2597              : 
    2598         1091 :                 if (WALK(forPortionOf->rangeVar))
    2599            0 :                     return true;
    2600         1091 :                 if (WALK(forPortionOf->targetFrom))
    2601            0 :                     return true;
    2602         1091 :                 if (WALK(forPortionOf->targetTo))
    2603            0 :                     return true;
    2604         1091 :                 if (WALK(forPortionOf->targetRange))
    2605            0 :                     return true;
    2606         1091 :                 if (WALK(forPortionOf->overlapsExpr))
    2607            0 :                     return true;
    2608         1091 :                 if (WALK(forPortionOf->rangeTargetList))
    2609            0 :                     return true;
    2610              :             }
    2611         1091 :             break;
    2612          644 :         case T_PartitionPruneStepOp:
    2613              :             {
    2614          644 :                 PartitionPruneStepOp *opstep = (PartitionPruneStepOp *) node;
    2615              : 
    2616          644 :                 if (WALK(opstep->exprs))
    2617            0 :                     return true;
    2618              :             }
    2619          644 :             break;
    2620          121 :         case T_PartitionPruneStepCombine:
    2621              :             /* no expression subnodes */
    2622          121 :             break;
    2623       235111 :         case T_JoinExpr:
    2624              :             {
    2625       235111 :                 JoinExpr   *join = (JoinExpr *) node;
    2626              : 
    2627       235111 :                 if (WALK(join->larg))
    2628        11921 :                     return true;
    2629       223190 :                 if (WALK(join->rarg))
    2630        15947 :                     return true;
    2631       207243 :                 if (WALK(join->quals))
    2632           48 :                     return true;
    2633              : 
    2634              :                 /*
    2635              :                  * alias clause, using list are deemed uninteresting.
    2636              :                  */
    2637              :             }
    2638       207195 :             break;
    2639        28382 :         case T_SetOperationStmt:
    2640              :             {
    2641        28382 :                 SetOperationStmt *setop = (SetOperationStmt *) node;
    2642              : 
    2643        28382 :                 if (WALK(setop->larg))
    2644            0 :                     return true;
    2645        28382 :                 if (WALK(setop->rarg))
    2646            0 :                     return true;
    2647              : 
    2648              :                 /* groupClauses are deemed uninteresting */
    2649              :             }
    2650        28382 :             break;
    2651            0 :         case T_IndexClause:
    2652              :             {
    2653            0 :                 IndexClause *iclause = (IndexClause *) node;
    2654              : 
    2655            0 :                 if (WALK(iclause->rinfo))
    2656            0 :                     return true;
    2657            0 :                 if (LIST_WALK(iclause->indexquals))
    2658            0 :                     return true;
    2659              :             }
    2660            0 :             break;
    2661        26147 :         case T_PlaceHolderVar:
    2662        26147 :             return WALK(((PlaceHolderVar *) node)->phexpr);
    2663         2489 :         case T_InferenceElem:
    2664         2489 :             return WALK(((InferenceElem *) node)->expr);
    2665         2836 :         case T_ReturningExpr:
    2666         2836 :             return WALK(((ReturningExpr *) node)->retexpr);
    2667         1482 :         case T_AppendRelInfo:
    2668              :             {
    2669         1482 :                 AppendRelInfo *appinfo = (AppendRelInfo *) node;
    2670              : 
    2671         1482 :                 if (LIST_WALK(appinfo->translated_vars))
    2672            0 :                     return true;
    2673              :             }
    2674         1482 :             break;
    2675            0 :         case T_PlaceHolderInfo:
    2676            0 :             return WALK(((PlaceHolderInfo *) node)->ph_var);
    2677       138684 :         case T_RangeTblFunction:
    2678       138684 :             return WALK(((RangeTblFunction *) node)->funcexpr);
    2679          622 :         case T_TableSampleClause:
    2680              :             {
    2681          622 :                 TableSampleClause *tsc = (TableSampleClause *) node;
    2682              : 
    2683          622 :                 if (LIST_WALK(tsc->args))
    2684            0 :                     return true;
    2685          622 :                 if (WALK(tsc->repeatable))
    2686            0 :                     return true;
    2687              :             }
    2688          622 :             break;
    2689         2558 :         case T_TableFunc:
    2690              :             {
    2691         2558 :                 TableFunc  *tf = (TableFunc *) node;
    2692              : 
    2693         2558 :                 if (WALK(tf->ns_uris))
    2694            0 :                     return true;
    2695         2558 :                 if (WALK(tf->docexpr))
    2696           60 :                     return true;
    2697         2498 :                 if (WALK(tf->rowexpr))
    2698            0 :                     return true;
    2699         2498 :                 if (WALK(tf->colexprs))
    2700            0 :                     return true;
    2701         2498 :                 if (WALK(tf->coldefexprs))
    2702            0 :                     return true;
    2703         2498 :                 if (WALK(tf->colvalexprs))
    2704            0 :                     return true;
    2705         2498 :                 if (WALK(tf->passingvalexprs))
    2706            0 :                     return true;
    2707              :             }
    2708         2498 :             break;
    2709          156 :         case T_GraphElementPattern:
    2710              :             {
    2711          156 :                 GraphElementPattern *gep = (GraphElementPattern *) node;
    2712              : 
    2713          156 :                 if (WALK(gep->labelexpr))
    2714            0 :                     return true;
    2715          156 :                 if (WALK(gep->subexpr))
    2716            0 :                     return true;
    2717          156 :                 if (WALK(gep->whereClause))
    2718            0 :                     return true;
    2719              :             }
    2720          156 :             break;
    2721           64 :         case T_GraphPattern:
    2722              :             {
    2723           64 :                 GraphPattern *gp = (GraphPattern *) node;
    2724              : 
    2725           64 :                 if (LIST_WALK(gp->path_pattern_list))
    2726            0 :                     return true;
    2727           64 :                 if (WALK(gp->whereClause))
    2728            0 :                     return true;
    2729              :             }
    2730           64 :             break;
    2731            0 :         default:
    2732            0 :             elog(ERROR, "unrecognized node type: %d",
    2733              :                  (int) nodeTag(node));
    2734              :             break;
    2735              :     }
    2736     64713457 :     return false;
    2737              : 
    2738              :     /* The WALK() macro can be re-used below, but LIST_WALK() not so much */
    2739              : #undef LIST_WALK
    2740              : }
    2741              : 
    2742              : /*
    2743              :  * query_tree_walker --- initiate a walk of a Query's expressions
    2744              :  *
    2745              :  * This routine exists just to reduce the number of places that need to know
    2746              :  * where all the expression subtrees of a Query are.  Note it can be used
    2747              :  * for starting a walk at top level of a Query regardless of whether the
    2748              :  * walker intends to descend into subqueries.  It is also useful for
    2749              :  * descending into subqueries within a walker.
    2750              :  *
    2751              :  * Some callers want to suppress visitation of certain items in the sub-Query,
    2752              :  * typically because they need to process them specially, or don't actually
    2753              :  * want to recurse into subqueries.  This is supported by the flags argument,
    2754              :  * which is the bitwise OR of flag values to add or suppress visitation of
    2755              :  * indicated items.  (More flag bits may be added as needed.)
    2756              :  */
    2757              : bool
    2758      1262878 : query_tree_walker_impl(Query *query,
    2759              :                        tree_walker_callback walker,
    2760              :                        void *context,
    2761              :                        int flags)
    2762              : {
    2763              :     Assert(query != NULL && IsA(query, Query));
    2764              : 
    2765              :     /*
    2766              :      * We don't walk any utilityStmt here. However, we can't easily assert
    2767              :      * that it is absent, since there are at least two code paths by which
    2768              :      * action statements from CREATE RULE end up here, and NOTIFY is allowed
    2769              :      * in a rule action.
    2770              :      */
    2771              : 
    2772      1262878 :     if (WALK(query->targetList))
    2773       233250 :         return true;
    2774      1029608 :     if (WALK(query->withCheckOptions))
    2775            0 :         return true;
    2776      1029608 :     if (WALK(query->onConflict))
    2777            0 :         return true;
    2778      1029608 :     if (WALK(query->mergeActionList))
    2779          326 :         return true;
    2780      1029282 :     if (WALK(query->mergeJoinCondition))
    2781          227 :         return true;
    2782      1029055 :     if (WALK(query->forPortionOf))
    2783            0 :         return true;
    2784      1029055 :     if (WALK(query->returningList))
    2785           57 :         return true;
    2786      1028998 :     if (WALK(query->jointree))
    2787        49415 :         return true;
    2788       979575 :     if (WALK(query->setOperations))
    2789            0 :         return true;
    2790       979575 :     if (WALK(query->havingQual))
    2791            0 :         return true;
    2792       979575 :     if (WALK(query->limitOffset))
    2793            4 :         return true;
    2794       979571 :     if (WALK(query->limitCount))
    2795            0 :         return true;
    2796              : 
    2797              :     /*
    2798              :      * Most callers aren't interested in SortGroupClause nodes since those
    2799              :      * don't contain actual expressions. However they do contain OIDs which
    2800              :      * may be needed by dependency walkers etc.
    2801              :      */
    2802       979571 :     if ((flags & QTW_EXAMINE_SORTGROUP))
    2803              :     {
    2804        42760 :         if (WALK(query->groupClause))
    2805            0 :             return true;
    2806        42760 :         if (WALK(query->windowClause))
    2807            0 :             return true;
    2808        42760 :         if (WALK(query->sortClause))
    2809            0 :             return true;
    2810        42760 :         if (WALK(query->distinctClause))
    2811            0 :             return true;
    2812              :     }
    2813              :     else
    2814              :     {
    2815              :         /*
    2816              :          * But we need to walk the expressions under WindowClause nodes even
    2817              :          * if we're not interested in SortGroupClause nodes.
    2818              :          */
    2819              :         ListCell   *lc;
    2820              : 
    2821       941544 :         foreach(lc, query->windowClause)
    2822              :         {
    2823         4738 :             WindowClause *wc = lfirst_node(WindowClause, lc);
    2824              : 
    2825         4738 :             if (WALK(wc->startOffset))
    2826            5 :                 return true;
    2827         4733 :             if (WALK(wc->endOffset))
    2828            0 :                 return true;
    2829              :         }
    2830              :     }
    2831              : 
    2832              :     /*
    2833              :      * groupingSets and rowMarks are not walked:
    2834              :      *
    2835              :      * groupingSets contain only ressortgrouprefs (integers) which are
    2836              :      * meaningless without the corresponding groupClause or tlist.
    2837              :      * Accordingly, any walker that needs to care about them needs to handle
    2838              :      * them itself in its Query processing.
    2839              :      *
    2840              :      * rowMarks is not walked because it contains only rangetable indexes (and
    2841              :      * flags etc.) and therefore should be handled at Query level similarly.
    2842              :      */
    2843              : 
    2844       979566 :     if (!(flags & QTW_IGNORE_CTE_SUBQUERIES))
    2845              :     {
    2846       565871 :         if (WALK(query->cteList))
    2847          150 :             return true;
    2848              :     }
    2849       979416 :     if (!(flags & QTW_IGNORE_RANGE_TABLE))
    2850              :     {
    2851       600632 :         if (range_table_walker(query->rtable, walker, context, flags))
    2852        16454 :             return true;
    2853              :     }
    2854       962962 :     return false;
    2855              : }
    2856              : 
    2857              : /*
    2858              :  * range_table_walker is just the part of query_tree_walker that scans
    2859              :  * a query's rangetable.  This is split out since it can be useful on
    2860              :  * its own.
    2861              :  */
    2862              : bool
    2863       604687 : range_table_walker_impl(List *rtable,
    2864              :                         tree_walker_callback walker,
    2865              :                         void *context,
    2866              :                         int flags)
    2867              : {
    2868              :     ListCell   *rt;
    2869              : 
    2870      1509459 :     foreach(rt, rtable)
    2871              :     {
    2872       921226 :         RangeTblEntry *rte = lfirst_node(RangeTblEntry, rt);
    2873              : 
    2874       921226 :         if (range_table_entry_walker(rte, walker, context, flags))
    2875        16454 :             return true;
    2876              :     }
    2877       588233 :     return false;
    2878              : }
    2879              : 
    2880              : /*
    2881              :  * Some callers even want to scan the expressions in individual RTEs.
    2882              :  */
    2883              : bool
    2884       921246 : range_table_entry_walker_impl(RangeTblEntry *rte,
    2885              :                               tree_walker_callback walker,
    2886              :                               void *context,
    2887              :                               int flags)
    2888              : {
    2889              :     /*
    2890              :      * Walkers might need to examine the RTE node itself either before or
    2891              :      * after visiting its contents (or, conceivably, both).  Note that if you
    2892              :      * specify neither flag, the walker won't be called on the RTE at all.
    2893              :      */
    2894       921246 :     if (flags & QTW_EXAMINE_RTES_BEFORE)
    2895       102562 :         if (WALK(rte))
    2896           10 :             return true;
    2897              : 
    2898       921236 :     switch (rte->rtekind)
    2899              :     {
    2900       555323 :         case RTE_RELATION:
    2901       555323 :             if (WALK(rte->tablesample))
    2902            0 :                 return true;
    2903       555323 :             break;
    2904       104139 :         case RTE_SUBQUERY:
    2905       104139 :             if (!(flags & QTW_IGNORE_RT_SUBQUERIES))
    2906       102402 :                 if (WALK(rte->subquery))
    2907         3615 :                     return true;
    2908       100524 :             break;
    2909       143757 :         case RTE_JOIN:
    2910       143757 :             if (!(flags & QTW_IGNORE_JOINALIASES))
    2911       124817 :                 if (WALK(rte->joinaliasvars))
    2912            0 :                     return true;
    2913       143757 :             break;
    2914        59903 :         case RTE_FUNCTION:
    2915        59903 :             if (WALK(rte->functions))
    2916        12772 :                 return true;
    2917        47131 :             break;
    2918          791 :         case RTE_TABLEFUNC:
    2919          791 :             if (WALK(rte->tablefunc))
    2920            0 :                 return true;
    2921          791 :             break;
    2922        21324 :         case RTE_VALUES:
    2923        21324 :             if (WALK(rte->values_lists))
    2924           41 :                 return true;
    2925        21283 :             break;
    2926           64 :         case RTE_GRAPH_TABLE:
    2927           64 :             if (WALK(rte->graph_pattern))
    2928            0 :                 return true;
    2929           64 :             if (WALK(rte->graph_table_columns))
    2930            0 :                 return true;
    2931           64 :             break;
    2932        30241 :         case RTE_CTE:
    2933              :         case RTE_NAMEDTUPLESTORE:
    2934              :         case RTE_RESULT:
    2935              :             /* nothing to do */
    2936        30241 :             break;
    2937         5694 :         case RTE_GROUP:
    2938         5694 :             if (!(flags & QTW_IGNORE_GROUPEXPRS))
    2939         5694 :                 if (WALK(rte->groupexprs))
    2940           21 :                     return true;
    2941         5673 :             break;
    2942              :     }
    2943              : 
    2944       904787 :     if (WALK(rte->securityQuals))
    2945           15 :         return true;
    2946              : 
    2947       904772 :     if (flags & QTW_EXAMINE_RTES_AFTER)
    2948        12196 :         if (WALK(rte))
    2949            0 :             return true;
    2950              : 
    2951       904772 :     return false;
    2952              : }
    2953              : 
    2954              : 
    2955              : /*
    2956              :  * expression_tree_mutator() is designed to support routines that make a
    2957              :  * modified copy of an expression tree, with some nodes being added,
    2958              :  * removed, or replaced by new subtrees.  The original tree is (normally)
    2959              :  * not changed.  Each recursion level is responsible for returning a copy of
    2960              :  * (or appropriately modified substitute for) the subtree it is handed.
    2961              :  * A mutator routine should look like this:
    2962              :  *
    2963              :  * Node * my_mutator (Node *node, my_struct *context)
    2964              :  * {
    2965              :  *      if (node == NULL)
    2966              :  *          return NULL;
    2967              :  *      // check for nodes that special work is required for, eg:
    2968              :  *      if (IsA(node, Var))
    2969              :  *      {
    2970              :  *          ... create and return modified copy of Var node
    2971              :  *      }
    2972              :  *      else if (IsA(node, ...))
    2973              :  *      {
    2974              :  *          ... do special transformations of other node types
    2975              :  *      }
    2976              :  *      // for any node type not specially processed, do:
    2977              :  *      return expression_tree_mutator(node, my_mutator, context);
    2978              :  * }
    2979              :  *
    2980              :  * The "context" argument points to a struct that holds whatever context
    2981              :  * information the mutator routine needs --- it can be used to return extra
    2982              :  * data gathered by the mutator, too.  This argument is not touched by
    2983              :  * expression_tree_mutator, but it is passed down to recursive sub-invocations
    2984              :  * of my_mutator.  The tree walk is started from a setup routine that
    2985              :  * fills in the appropriate context struct, calls my_mutator with the
    2986              :  * top-level node of the tree, and does any required post-processing.
    2987              :  *
    2988              :  * Each level of recursion must return an appropriately modified Node.
    2989              :  * If expression_tree_mutator() is called, it will make an exact copy
    2990              :  * of the given Node, but invoke my_mutator() to copy the sub-node(s)
    2991              :  * of that Node.  In this way, my_mutator() has full control over the
    2992              :  * copying process but need not directly deal with expression trees
    2993              :  * that it has no interest in.
    2994              :  *
    2995              :  * Just as for expression_tree_walker, the node types handled by
    2996              :  * expression_tree_mutator include all those normally found in target lists
    2997              :  * and qualifier clauses during the planning stage.
    2998              :  *
    2999              :  * expression_tree_mutator will handle SubLink nodes by recursing normally
    3000              :  * into the "testexpr" subtree (which is an expression belonging to the outer
    3001              :  * plan).  It will also call the mutator on the sub-Query node; however, when
    3002              :  * expression_tree_mutator itself is called on a Query node, it does nothing
    3003              :  * and returns the unmodified Query node.  The net effect is that unless the
    3004              :  * mutator does something special at a Query node, sub-selects will not be
    3005              :  * visited or modified; the original sub-select will be linked to by the new
    3006              :  * SubLink node.  Mutators that want to descend into sub-selects will usually
    3007              :  * do so by recognizing Query nodes and calling query_tree_mutator (below).
    3008              :  *
    3009              :  * expression_tree_mutator will handle a SubPlan node by recursing into the
    3010              :  * "testexpr" and the "args" list (which belong to the outer plan), but it
    3011              :  * will simply copy the link to the inner plan, since that's typically what
    3012              :  * expression tree mutators want.  A mutator that wants to modify the subplan
    3013              :  * can force appropriate behavior by recognizing SubPlan expression nodes
    3014              :  * and doing the right thing.
    3015              :  */
    3016              : 
    3017              : Node *
    3018     16136557 : expression_tree_mutator_impl(Node *node,
    3019              :                              tree_mutator_callback mutator,
    3020              :                              void *context)
    3021              : {
    3022              :     /*
    3023              :      * The mutator has already decided not to modify the current node, but we
    3024              :      * must call the mutator for any sub-nodes.
    3025              :      */
    3026              : 
    3027              : #define FLATCOPY(newnode, node, nodetype)  \
    3028              :     ( (newnode) = palloc_object(nodetype), \
    3029              :       memcpy((newnode), (node), sizeof(nodetype)) )
    3030              : 
    3031              : #define MUTATE(newfield, oldfield, fieldtype)  \
    3032              :         ( (newfield) = (fieldtype) mutator((Node *) (oldfield), context) )
    3033              : 
    3034     16136557 :     if (node == NULL)
    3035        52302 :         return NULL;
    3036              : 
    3037              :     /* Guard against stack overflow due to overly complex expressions */
    3038     16084255 :     check_stack_depth();
    3039              : 
    3040     16084255 :     switch (nodeTag(node))
    3041              :     {
    3042              :             /*
    3043              :              * Primitive node types with no expression subnodes.  Var and
    3044              :              * Const are frequent enough to deserve special cases, the others
    3045              :              * we just use copyObject for.
    3046              :              */
    3047      3072147 :         case T_Var:
    3048              :             {
    3049      3072147 :                 Var        *var = (Var *) node;
    3050              :                 Var        *newnode;
    3051              : 
    3052      3072147 :                 FLATCOPY(newnode, var, Var);
    3053              :                 /* Assume we need not copy the varnullingrels bitmapset */
    3054      3072147 :                 return (Node *) newnode;
    3055              :             }
    3056              :             break;
    3057      2544711 :         case T_Const:
    3058              :             {
    3059      2544711 :                 Const      *oldnode = (Const *) node;
    3060              :                 Const      *newnode;
    3061              : 
    3062      2544711 :                 FLATCOPY(newnode, oldnode, Const);
    3063              :                 /* XXX we don't bother with datumCopy; should we? */
    3064      2544711 :                 return (Node *) newnode;
    3065              :             }
    3066              :             break;
    3067       108600 :         case T_Param:
    3068              :         case T_CaseTestExpr:
    3069              :         case T_SQLValueFunction:
    3070              :         case T_JsonFormat:
    3071              :         case T_CoerceToDomainValue:
    3072              :         case T_SetToDefault:
    3073              :         case T_CurrentOfExpr:
    3074              :         case T_NextValueExpr:
    3075              :         case T_RangeTblRef:
    3076              :         case T_SortGroupClause:
    3077              :         case T_CTESearchClause:
    3078              :         case T_GraphLabelRef:
    3079              :         case T_GraphPropertyRef:
    3080              :         case T_MergeSupportFunc:
    3081       108600 :             return copyObject(node);
    3082         1105 :         case T_WithCheckOption:
    3083              :             {
    3084         1105 :                 WithCheckOption *wco = (WithCheckOption *) node;
    3085              :                 WithCheckOption *newnode;
    3086              : 
    3087         1105 :                 FLATCOPY(newnode, wco, WithCheckOption);
    3088         1105 :                 MUTATE(newnode->qual, wco->qual, Node *);
    3089         1105 :                 return (Node *) newnode;
    3090              :             }
    3091       141582 :         case T_Aggref:
    3092              :             {
    3093       141582 :                 Aggref     *aggref = (Aggref *) node;
    3094              :                 Aggref     *newnode;
    3095              : 
    3096       141582 :                 FLATCOPY(newnode, aggref, Aggref);
    3097              :                 /* assume mutation doesn't change types of arguments */
    3098       141582 :                 newnode->aggargtypes = list_copy(aggref->aggargtypes);
    3099       141582 :                 MUTATE(newnode->aggdirectargs, aggref->aggdirectargs, List *);
    3100       141582 :                 MUTATE(newnode->args, aggref->args, List *);
    3101       141582 :                 MUTATE(newnode->aggorder, aggref->aggorder, List *);
    3102       141582 :                 MUTATE(newnode->aggdistinct, aggref->aggdistinct, List *);
    3103       141582 :                 MUTATE(newnode->aggfilter, aggref->aggfilter, Expr *);
    3104       141582 :                 return (Node *) newnode;
    3105              :             }
    3106              :             break;
    3107         1273 :         case T_GroupingFunc:
    3108              :             {
    3109         1273 :                 GroupingFunc *grouping = (GroupingFunc *) node;
    3110              :                 GroupingFunc *newnode;
    3111              : 
    3112         1273 :                 FLATCOPY(newnode, grouping, GroupingFunc);
    3113         1273 :                 MUTATE(newnode->args, grouping->args, List *);
    3114              : 
    3115              :                 /*
    3116              :                  * We assume here that mutating the arguments does not change
    3117              :                  * the semantics, i.e. that the arguments are not mutated in a
    3118              :                  * way that makes them semantically different from their
    3119              :                  * previously matching expressions in the GROUP BY clause.
    3120              :                  *
    3121              :                  * If a mutator somehow wanted to do this, it would have to
    3122              :                  * handle the refs and cols lists itself as appropriate.
    3123              :                  */
    3124         1273 :                 newnode->refs = list_copy(grouping->refs);
    3125         1273 :                 newnode->cols = list_copy(grouping->cols);
    3126              : 
    3127         1273 :                 return (Node *) newnode;
    3128              :             }
    3129              :             break;
    3130         4726 :         case T_WindowFunc:
    3131              :             {
    3132         4726 :                 WindowFunc *wfunc = (WindowFunc *) node;
    3133              :                 WindowFunc *newnode;
    3134              : 
    3135         4726 :                 FLATCOPY(newnode, wfunc, WindowFunc);
    3136         4726 :                 MUTATE(newnode->args, wfunc->args, List *);
    3137         4726 :                 MUTATE(newnode->aggfilter, wfunc->aggfilter, Expr *);
    3138         4726 :                 return (Node *) newnode;
    3139              :             }
    3140              :             break;
    3141            0 :         case T_WindowFuncRunCondition:
    3142              :             {
    3143            0 :                 WindowFuncRunCondition *wfuncrc = (WindowFuncRunCondition *) node;
    3144              :                 WindowFuncRunCondition *newnode;
    3145              : 
    3146            0 :                 FLATCOPY(newnode, wfuncrc, WindowFuncRunCondition);
    3147            0 :                 MUTATE(newnode->arg, wfuncrc->arg, Expr *);
    3148            0 :                 return (Node *) newnode;
    3149              :             }
    3150              :             break;
    3151        55844 :         case T_SubscriptingRef:
    3152              :             {
    3153        55844 :                 SubscriptingRef *sbsref = (SubscriptingRef *) node;
    3154              :                 SubscriptingRef *newnode;
    3155              : 
    3156        55844 :                 FLATCOPY(newnode, sbsref, SubscriptingRef);
    3157        55844 :                 MUTATE(newnode->refupperindexpr, sbsref->refupperindexpr,
    3158              :                        List *);
    3159        55844 :                 MUTATE(newnode->reflowerindexpr, sbsref->reflowerindexpr,
    3160              :                        List *);
    3161        55844 :                 MUTATE(newnode->refexpr, sbsref->refexpr,
    3162              :                        Expr *);
    3163        55844 :                 MUTATE(newnode->refassgnexpr, sbsref->refassgnexpr,
    3164              :                        Expr *);
    3165              : 
    3166        55844 :                 return (Node *) newnode;
    3167              :             }
    3168              :             break;
    3169       272804 :         case T_FuncExpr:
    3170              :             {
    3171       272804 :                 FuncExpr   *expr = (FuncExpr *) node;
    3172              :                 FuncExpr   *newnode;
    3173              : 
    3174       272804 :                 FLATCOPY(newnode, expr, FuncExpr);
    3175       272804 :                 MUTATE(newnode->args, expr->args, List *);
    3176       272804 :                 return (Node *) newnode;
    3177              :             }
    3178              :             break;
    3179            0 :         case T_NamedArgExpr:
    3180              :             {
    3181            0 :                 NamedArgExpr *nexpr = (NamedArgExpr *) node;
    3182              :                 NamedArgExpr *newnode;
    3183              : 
    3184            0 :                 FLATCOPY(newnode, nexpr, NamedArgExpr);
    3185            0 :                 MUTATE(newnode->arg, nexpr->arg, Expr *);
    3186            0 :                 return (Node *) newnode;
    3187              :             }
    3188              :             break;
    3189      1072032 :         case T_OpExpr:
    3190              :             {
    3191      1072032 :                 OpExpr     *expr = (OpExpr *) node;
    3192              :                 OpExpr     *newnode;
    3193              : 
    3194      1072032 :                 FLATCOPY(newnode, expr, OpExpr);
    3195      1072032 :                 MUTATE(newnode->args, expr->args, List *);
    3196      1072024 :                 return (Node *) newnode;
    3197              :             }
    3198              :             break;
    3199         2191 :         case T_DistinctExpr:
    3200              :             {
    3201         2191 :                 DistinctExpr *expr = (DistinctExpr *) node;
    3202              :                 DistinctExpr *newnode;
    3203              : 
    3204         2191 :                 FLATCOPY(newnode, expr, DistinctExpr);
    3205         2191 :                 MUTATE(newnode->args, expr->args, List *);
    3206         2191 :                 return (Node *) newnode;
    3207              :             }
    3208              :             break;
    3209         1459 :         case T_NullIfExpr:
    3210              :             {
    3211         1459 :                 NullIfExpr *expr = (NullIfExpr *) node;
    3212              :                 NullIfExpr *newnode;
    3213              : 
    3214         1459 :                 FLATCOPY(newnode, expr, NullIfExpr);
    3215         1459 :                 MUTATE(newnode->args, expr->args, List *);
    3216         1459 :                 return (Node *) newnode;
    3217              :             }
    3218              :             break;
    3219        69274 :         case T_ScalarArrayOpExpr:
    3220              :             {
    3221        69274 :                 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
    3222              :                 ScalarArrayOpExpr *newnode;
    3223              : 
    3224        69274 :                 FLATCOPY(newnode, expr, ScalarArrayOpExpr);
    3225        69274 :                 MUTATE(newnode->args, expr->args, List *);
    3226        69274 :                 return (Node *) newnode;
    3227              :             }
    3228              :             break;
    3229       155812 :         case T_BoolExpr:
    3230              :             {
    3231       155812 :                 BoolExpr   *expr = (BoolExpr *) node;
    3232              :                 BoolExpr   *newnode;
    3233              : 
    3234       155812 :                 FLATCOPY(newnode, expr, BoolExpr);
    3235       155812 :                 MUTATE(newnode->args, expr->args, List *);
    3236       155812 :                 return (Node *) newnode;
    3237              :             }
    3238              :             break;
    3239        43043 :         case T_SubLink:
    3240              :             {
    3241        43043 :                 SubLink    *sublink = (SubLink *) node;
    3242              :                 SubLink    *newnode;
    3243              : 
    3244        43043 :                 FLATCOPY(newnode, sublink, SubLink);
    3245        43043 :                 MUTATE(newnode->testexpr, sublink->testexpr, Node *);
    3246              : 
    3247              :                 /*
    3248              :                  * Also invoke the mutator on the sublink's Query node, so it
    3249              :                  * can recurse into the sub-query if it wants to.
    3250              :                  */
    3251        43043 :                 MUTATE(newnode->subselect, sublink->subselect, Node *);
    3252        43043 :                 return (Node *) newnode;
    3253              :             }
    3254              :             break;
    3255        14745 :         case T_SubPlan:
    3256              :             {
    3257        14745 :                 SubPlan    *subplan = (SubPlan *) node;
    3258              :                 SubPlan    *newnode;
    3259              : 
    3260        14745 :                 FLATCOPY(newnode, subplan, SubPlan);
    3261              :                 /* transform testexpr */
    3262        14745 :                 MUTATE(newnode->testexpr, subplan->testexpr, Node *);
    3263              :                 /* transform args list (params to be passed to subplan) */
    3264        14745 :                 MUTATE(newnode->args, subplan->args, List *);
    3265              :                 /* but not the sub-Plan itself, which is referenced as-is */
    3266        14745 :                 return (Node *) newnode;
    3267              :             }
    3268              :             break;
    3269          180 :         case T_AlternativeSubPlan:
    3270              :             {
    3271          180 :                 AlternativeSubPlan *asplan = (AlternativeSubPlan *) node;
    3272              :                 AlternativeSubPlan *newnode;
    3273              : 
    3274          180 :                 FLATCOPY(newnode, asplan, AlternativeSubPlan);
    3275          180 :                 MUTATE(newnode->subplans, asplan->subplans, List *);
    3276          180 :                 return (Node *) newnode;
    3277              :             }
    3278              :             break;
    3279        86733 :         case T_FieldSelect:
    3280              :             {
    3281        86733 :                 FieldSelect *fselect = (FieldSelect *) node;
    3282              :                 FieldSelect *newnode;
    3283              : 
    3284        86733 :                 FLATCOPY(newnode, fselect, FieldSelect);
    3285        86733 :                 MUTATE(newnode->arg, fselect->arg, Expr *);
    3286        86733 :                 return (Node *) newnode;
    3287              :             }
    3288              :             break;
    3289          360 :         case T_FieldStore:
    3290              :             {
    3291          360 :                 FieldStore *fstore = (FieldStore *) node;
    3292              :                 FieldStore *newnode;
    3293              : 
    3294          360 :                 FLATCOPY(newnode, fstore, FieldStore);
    3295          360 :                 MUTATE(newnode->arg, fstore->arg, Expr *);
    3296          360 :                 MUTATE(newnode->newvals, fstore->newvals, List *);
    3297          360 :                 newnode->fieldnums = list_copy(fstore->fieldnums);
    3298          360 :                 return (Node *) newnode;
    3299              :             }
    3300              :             break;
    3301       125802 :         case T_RelabelType:
    3302              :             {
    3303       125802 :                 RelabelType *relabel = (RelabelType *) node;
    3304              :                 RelabelType *newnode;
    3305              : 
    3306       125802 :                 FLATCOPY(newnode, relabel, RelabelType);
    3307       125802 :                 MUTATE(newnode->arg, relabel->arg, Expr *);
    3308       125802 :                 return (Node *) newnode;
    3309              :             }
    3310              :             break;
    3311        31842 :         case T_CoerceViaIO:
    3312              :             {
    3313        31842 :                 CoerceViaIO *iocoerce = (CoerceViaIO *) node;
    3314              :                 CoerceViaIO *newnode;
    3315              : 
    3316        31842 :                 FLATCOPY(newnode, iocoerce, CoerceViaIO);
    3317        31842 :                 MUTATE(newnode->arg, iocoerce->arg, Expr *);
    3318        31842 :                 return (Node *) newnode;
    3319              :             }
    3320              :             break;
    3321        10608 :         case T_ArrayCoerceExpr:
    3322              :             {
    3323        10608 :                 ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node;
    3324              :                 ArrayCoerceExpr *newnode;
    3325              : 
    3326        10608 :                 FLATCOPY(newnode, acoerce, ArrayCoerceExpr);
    3327        10608 :                 MUTATE(newnode->arg, acoerce->arg, Expr *);
    3328        10608 :                 MUTATE(newnode->elemexpr, acoerce->elemexpr, Expr *);
    3329        10608 :                 return (Node *) newnode;
    3330              :             }
    3331              :             break;
    3332          278 :         case T_ConvertRowtypeExpr:
    3333              :             {
    3334          278 :                 ConvertRowtypeExpr *convexpr = (ConvertRowtypeExpr *) node;
    3335              :                 ConvertRowtypeExpr *newnode;
    3336              : 
    3337          278 :                 FLATCOPY(newnode, convexpr, ConvertRowtypeExpr);
    3338          278 :                 MUTATE(newnode->arg, convexpr->arg, Expr *);
    3339          278 :                 return (Node *) newnode;
    3340              :             }
    3341              :             break;
    3342         6188 :         case T_CollateExpr:
    3343              :             {
    3344         6188 :                 CollateExpr *collate = (CollateExpr *) node;
    3345              :                 CollateExpr *newnode;
    3346              : 
    3347         6188 :                 FLATCOPY(newnode, collate, CollateExpr);
    3348         6188 :                 MUTATE(newnode->arg, collate->arg, Expr *);
    3349         6188 :                 return (Node *) newnode;
    3350              :             }
    3351              :             break;
    3352        66419 :         case T_CaseExpr:
    3353              :             {
    3354        66419 :                 CaseExpr   *caseexpr = (CaseExpr *) node;
    3355              :                 CaseExpr   *newnode;
    3356              : 
    3357        66419 :                 FLATCOPY(newnode, caseexpr, CaseExpr);
    3358        66419 :                 MUTATE(newnode->arg, caseexpr->arg, Expr *);
    3359        66419 :                 MUTATE(newnode->args, caseexpr->args, List *);
    3360        66419 :                 MUTATE(newnode->defresult, caseexpr->defresult, Expr *);
    3361        66419 :                 return (Node *) newnode;
    3362              :             }
    3363              :             break;
    3364       150468 :         case T_CaseWhen:
    3365              :             {
    3366       150468 :                 CaseWhen   *casewhen = (CaseWhen *) node;
    3367              :                 CaseWhen   *newnode;
    3368              : 
    3369       150468 :                 FLATCOPY(newnode, casewhen, CaseWhen);
    3370       150468 :                 MUTATE(newnode->expr, casewhen->expr, Expr *);
    3371       150468 :                 MUTATE(newnode->result, casewhen->result, Expr *);
    3372       150468 :                 return (Node *) newnode;
    3373              :             }
    3374              :             break;
    3375        32729 :         case T_ArrayExpr:
    3376              :             {
    3377        32729 :                 ArrayExpr  *arrayexpr = (ArrayExpr *) node;
    3378              :                 ArrayExpr  *newnode;
    3379              : 
    3380        32729 :                 FLATCOPY(newnode, arrayexpr, ArrayExpr);
    3381        32729 :                 MUTATE(newnode->elements, arrayexpr->elements, List *);
    3382        32729 :                 return (Node *) newnode;
    3383              :             }
    3384              :             break;
    3385         7219 :         case T_RowExpr:
    3386              :             {
    3387         7219 :                 RowExpr    *rowexpr = (RowExpr *) node;
    3388              :                 RowExpr    *newnode;
    3389              : 
    3390         7219 :                 FLATCOPY(newnode, rowexpr, RowExpr);
    3391         7219 :                 MUTATE(newnode->args, rowexpr->args, List *);
    3392              :                 /* Assume colnames needn't be duplicated */
    3393         7219 :                 return (Node *) newnode;
    3394              :             }
    3395              :             break;
    3396          553 :         case T_RowCompareExpr:
    3397              :             {
    3398          553 :                 RowCompareExpr *rcexpr = (RowCompareExpr *) node;
    3399              :                 RowCompareExpr *newnode;
    3400              : 
    3401          553 :                 FLATCOPY(newnode, rcexpr, RowCompareExpr);
    3402          553 :                 MUTATE(newnode->largs, rcexpr->largs, List *);
    3403          553 :                 MUTATE(newnode->rargs, rcexpr->rargs, List *);
    3404          553 :                 return (Node *) newnode;
    3405              :             }
    3406              :             break;
    3407         9534 :         case T_CoalesceExpr:
    3408              :             {
    3409         9534 :                 CoalesceExpr *coalesceexpr = (CoalesceExpr *) node;
    3410              :                 CoalesceExpr *newnode;
    3411              : 
    3412         9534 :                 FLATCOPY(newnode, coalesceexpr, CoalesceExpr);
    3413         9534 :                 MUTATE(newnode->args, coalesceexpr->args, List *);
    3414         9534 :                 return (Node *) newnode;
    3415              :             }
    3416              :             break;
    3417         1130 :         case T_MinMaxExpr:
    3418              :             {
    3419         1130 :                 MinMaxExpr *minmaxexpr = (MinMaxExpr *) node;
    3420              :                 MinMaxExpr *newnode;
    3421              : 
    3422         1130 :                 FLATCOPY(newnode, minmaxexpr, MinMaxExpr);
    3423         1130 :                 MUTATE(newnode->args, minmaxexpr->args, List *);
    3424         1130 :                 return (Node *) newnode;
    3425              :             }
    3426              :             break;
    3427          683 :         case T_XmlExpr:
    3428              :             {
    3429          683 :                 XmlExpr    *xexpr = (XmlExpr *) node;
    3430              :                 XmlExpr    *newnode;
    3431              : 
    3432          683 :                 FLATCOPY(newnode, xexpr, XmlExpr);
    3433          683 :                 MUTATE(newnode->named_args, xexpr->named_args, List *);
    3434              :                 /* assume mutator does not care about arg_names */
    3435          683 :                 MUTATE(newnode->args, xexpr->args, List *);
    3436          683 :                 return (Node *) newnode;
    3437              :             }
    3438              :             break;
    3439         2378 :         case T_JsonReturning:
    3440              :             {
    3441         2378 :                 JsonReturning *jr = (JsonReturning *) node;
    3442              :                 JsonReturning *newnode;
    3443              : 
    3444         2378 :                 FLATCOPY(newnode, jr, JsonReturning);
    3445         2378 :                 MUTATE(newnode->format, jr->format, JsonFormat *);
    3446              : 
    3447         2378 :                 return (Node *) newnode;
    3448              :             }
    3449          149 :         case T_JsonValueExpr:
    3450              :             {
    3451          149 :                 JsonValueExpr *jve = (JsonValueExpr *) node;
    3452              :                 JsonValueExpr *newnode;
    3453              : 
    3454          149 :                 FLATCOPY(newnode, jve, JsonValueExpr);
    3455          149 :                 MUTATE(newnode->raw_expr, jve->raw_expr, Expr *);
    3456          149 :                 MUTATE(newnode->formatted_expr, jve->formatted_expr, Expr *);
    3457          149 :                 MUTATE(newnode->format, jve->format, JsonFormat *);
    3458              : 
    3459          149 :                 return (Node *) newnode;
    3460              :             }
    3461         2378 :         case T_JsonConstructorExpr:
    3462              :             {
    3463         2378 :                 JsonConstructorExpr *jce = (JsonConstructorExpr *) node;
    3464              :                 JsonConstructorExpr *newnode;
    3465              : 
    3466         2378 :                 FLATCOPY(newnode, jce, JsonConstructorExpr);
    3467         2378 :                 MUTATE(newnode->args, jce->args, List *);
    3468         2378 :                 MUTATE(newnode->func, jce->func, Expr *);
    3469         2378 :                 MUTATE(newnode->coercion, jce->coercion, Expr *);
    3470         2378 :                 MUTATE(newnode->returning, jce->returning, JsonReturning *);
    3471              : 
    3472         2378 :                 return (Node *) newnode;
    3473              :             }
    3474          506 :         case T_JsonIsPredicate:
    3475              :             {
    3476          506 :                 JsonIsPredicate *pred = (JsonIsPredicate *) node;
    3477              :                 JsonIsPredicate *newnode;
    3478              : 
    3479          506 :                 FLATCOPY(newnode, pred, JsonIsPredicate);
    3480          506 :                 MUTATE(newnode->expr, pred->expr, Node *);
    3481          506 :                 MUTATE(newnode->format, pred->format, JsonFormat *);
    3482              : 
    3483          506 :                 return (Node *) newnode;
    3484              :             }
    3485         3220 :         case T_JsonExpr:
    3486              :             {
    3487         3220 :                 JsonExpr   *jexpr = (JsonExpr *) node;
    3488              :                 JsonExpr   *newnode;
    3489              : 
    3490         3220 :                 FLATCOPY(newnode, jexpr, JsonExpr);
    3491         3220 :                 MUTATE(newnode->formatted_expr, jexpr->formatted_expr, Node *);
    3492         3220 :                 MUTATE(newnode->path_spec, jexpr->path_spec, Node *);
    3493         3216 :                 MUTATE(newnode->passing_values, jexpr->passing_values, List *);
    3494              :                 /* assume mutator does not care about passing_names */
    3495         3216 :                 MUTATE(newnode->on_empty, jexpr->on_empty, JsonBehavior *);
    3496         3212 :                 MUTATE(newnode->on_error, jexpr->on_error, JsonBehavior *);
    3497         3208 :                 return (Node *) newnode;
    3498              :             }
    3499              :             break;
    3500         5655 :         case T_JsonBehavior:
    3501              :             {
    3502         5655 :                 JsonBehavior *behavior = (JsonBehavior *) node;
    3503              :                 JsonBehavior *newnode;
    3504              : 
    3505         5655 :                 FLATCOPY(newnode, behavior, JsonBehavior);
    3506         5655 :                 MUTATE(newnode->expr, behavior->expr, Node *);
    3507         5647 :                 return (Node *) newnode;
    3508              :             }
    3509              :             break;
    3510        36217 :         case T_NullTest:
    3511              :             {
    3512        36217 :                 NullTest   *ntest = (NullTest *) node;
    3513              :                 NullTest   *newnode;
    3514              : 
    3515        36217 :                 FLATCOPY(newnode, ntest, NullTest);
    3516        36217 :                 MUTATE(newnode->arg, ntest->arg, Expr *);
    3517        36217 :                 return (Node *) newnode;
    3518              :             }
    3519              :             break;
    3520         1957 :         case T_BooleanTest:
    3521              :             {
    3522         1957 :                 BooleanTest *btest = (BooleanTest *) node;
    3523              :                 BooleanTest *newnode;
    3524              : 
    3525         1957 :                 FLATCOPY(newnode, btest, BooleanTest);
    3526         1957 :                 MUTATE(newnode->arg, btest->arg, Expr *);
    3527         1957 :                 return (Node *) newnode;
    3528              :             }
    3529              :             break;
    3530        14779 :         case T_CoerceToDomain:
    3531              :             {
    3532        14779 :                 CoerceToDomain *ctest = (CoerceToDomain *) node;
    3533              :                 CoerceToDomain *newnode;
    3534              : 
    3535        14779 :                 FLATCOPY(newnode, ctest, CoerceToDomain);
    3536        14779 :                 MUTATE(newnode->arg, ctest->arg, Expr *);
    3537        14779 :                 return (Node *) newnode;
    3538              :             }
    3539              :             break;
    3540         1633 :         case T_ReturningExpr:
    3541              :             {
    3542         1633 :                 ReturningExpr *rexpr = (ReturningExpr *) node;
    3543              :                 ReturningExpr *newnode;
    3544              : 
    3545         1633 :                 FLATCOPY(newnode, rexpr, ReturningExpr);
    3546         1633 :                 MUTATE(newnode->retexpr, rexpr->retexpr, Expr *);
    3547         1633 :                 return (Node *) newnode;
    3548              :             }
    3549              :             break;
    3550      3398478 :         case T_TargetEntry:
    3551              :             {
    3552      3398478 :                 TargetEntry *targetentry = (TargetEntry *) node;
    3553              :                 TargetEntry *newnode;
    3554              : 
    3555      3398478 :                 FLATCOPY(newnode, targetentry, TargetEntry);
    3556      3398478 :                 MUTATE(newnode->expr, targetentry->expr, Expr *);
    3557      3395765 :                 return (Node *) newnode;
    3558              :             }
    3559              :             break;
    3560        30542 :         case T_Query:
    3561              :             /* Do nothing with a sub-Query, per discussion above */
    3562        30542 :             return node;
    3563            0 :         case T_WindowClause:
    3564              :             {
    3565            0 :                 WindowClause *wc = (WindowClause *) node;
    3566              :                 WindowClause *newnode;
    3567              : 
    3568            0 :                 FLATCOPY(newnode, wc, WindowClause);
    3569            0 :                 MUTATE(newnode->partitionClause, wc->partitionClause, List *);
    3570            0 :                 MUTATE(newnode->orderClause, wc->orderClause, List *);
    3571            0 :                 MUTATE(newnode->startOffset, wc->startOffset, Node *);
    3572            0 :                 MUTATE(newnode->endOffset, wc->endOffset, Node *);
    3573            0 :                 return (Node *) newnode;
    3574              :             }
    3575              :             break;
    3576            0 :         case T_CTECycleClause:
    3577              :             {
    3578            0 :                 CTECycleClause *cc = (CTECycleClause *) node;
    3579              :                 CTECycleClause *newnode;
    3580              : 
    3581            0 :                 FLATCOPY(newnode, cc, CTECycleClause);
    3582            0 :                 MUTATE(newnode->cycle_mark_value, cc->cycle_mark_value, Node *);
    3583            0 :                 MUTATE(newnode->cycle_mark_default, cc->cycle_mark_default, Node *);
    3584            0 :                 return (Node *) newnode;
    3585              :             }
    3586              :             break;
    3587          164 :         case T_CommonTableExpr:
    3588              :             {
    3589          164 :                 CommonTableExpr *cte = (CommonTableExpr *) node;
    3590              :                 CommonTableExpr *newnode;
    3591              : 
    3592          164 :                 FLATCOPY(newnode, cte, CommonTableExpr);
    3593              : 
    3594              :                 /*
    3595              :                  * Also invoke the mutator on the CTE's Query node, so it can
    3596              :                  * recurse into the sub-query if it wants to.
    3597              :                  */
    3598          164 :                 MUTATE(newnode->ctequery, cte->ctequery, Node *);
    3599              : 
    3600          164 :                 MUTATE(newnode->search_clause, cte->search_clause, CTESearchClause *);
    3601          164 :                 MUTATE(newnode->cycle_clause, cte->cycle_clause, CTECycleClause *);
    3602              : 
    3603          164 :                 return (Node *) newnode;
    3604              :             }
    3605              :             break;
    3606            0 :         case T_PartitionBoundSpec:
    3607              :             {
    3608            0 :                 PartitionBoundSpec *pbs = (PartitionBoundSpec *) node;
    3609              :                 PartitionBoundSpec *newnode;
    3610              : 
    3611            0 :                 FLATCOPY(newnode, pbs, PartitionBoundSpec);
    3612            0 :                 MUTATE(newnode->listdatums, pbs->listdatums, List *);
    3613            0 :                 MUTATE(newnode->lowerdatums, pbs->lowerdatums, List *);
    3614            0 :                 MUTATE(newnode->upperdatums, pbs->upperdatums, List *);
    3615            0 :                 return (Node *) newnode;
    3616              :             }
    3617              :             break;
    3618            0 :         case T_PartitionRangeDatum:
    3619              :             {
    3620            0 :                 PartitionRangeDatum *prd = (PartitionRangeDatum *) node;
    3621              :                 PartitionRangeDatum *newnode;
    3622              : 
    3623            0 :                 FLATCOPY(newnode, prd, PartitionRangeDatum);
    3624            0 :                 MUTATE(newnode->value, prd->value, Node *);
    3625            0 :                 return (Node *) newnode;
    3626              :             }
    3627              :             break;
    3628      4355873 :         case T_List:
    3629              :             {
    3630              :                 /*
    3631              :                  * We assume the mutator isn't interested in the list nodes
    3632              :                  * per se, so just invoke it on each list element. NOTE: this
    3633              :                  * would fail badly on a list with integer elements!
    3634              :                  */
    3635              :                 List       *resultlist;
    3636              :                 ListCell   *temp;
    3637              : 
    3638      4355873 :                 resultlist = NIL;
    3639     14088450 :                 foreach(temp, (List *) node)
    3640              :                 {
    3641      9732577 :                     resultlist = lappend(resultlist,
    3642      9735379 :                                          mutator((Node *) lfirst(temp),
    3643              :                                                  context));
    3644              :                 }
    3645      4353071 :                 return (Node *) resultlist;
    3646              :             }
    3647              :             break;
    3648        23263 :         case T_FromExpr:
    3649              :             {
    3650        23263 :                 FromExpr   *from = (FromExpr *) node;
    3651              :                 FromExpr   *newnode;
    3652              : 
    3653        23263 :                 FLATCOPY(newnode, from, FromExpr);
    3654        23263 :                 MUTATE(newnode->fromlist, from->fromlist, List *);
    3655        23263 :                 MUTATE(newnode->quals, from->quals, Node *);
    3656        23263 :                 return (Node *) newnode;
    3657              :             }
    3658              :             break;
    3659          414 :         case T_OnConflictExpr:
    3660              :             {
    3661          414 :                 OnConflictExpr *oc = (OnConflictExpr *) node;
    3662              :                 OnConflictExpr *newnode;
    3663              : 
    3664          414 :                 FLATCOPY(newnode, oc, OnConflictExpr);
    3665          414 :                 MUTATE(newnode->arbiterElems, oc->arbiterElems, List *);
    3666          414 :                 MUTATE(newnode->arbiterWhere, oc->arbiterWhere, Node *);
    3667          414 :                 MUTATE(newnode->onConflictSet, oc->onConflictSet, List *);
    3668          414 :                 MUTATE(newnode->onConflictWhere, oc->onConflictWhere, Node *);
    3669          414 :                 MUTATE(newnode->exclRelTlist, oc->exclRelTlist, List *);
    3670              : 
    3671          414 :                 return (Node *) newnode;
    3672              :             }
    3673              :             break;
    3674          712 :         case T_MergeAction:
    3675              :             {
    3676          712 :                 MergeAction *action = (MergeAction *) node;
    3677              :                 MergeAction *newnode;
    3678              : 
    3679          712 :                 FLATCOPY(newnode, action, MergeAction);
    3680          712 :                 MUTATE(newnode->qual, action->qual, Node *);
    3681          712 :                 MUTATE(newnode->targetList, action->targetList, List *);
    3682              : 
    3683          712 :                 return (Node *) newnode;
    3684              :             }
    3685              :             break;
    3686           28 :         case T_ForPortionOfExpr:
    3687              :             {
    3688           28 :                 ForPortionOfExpr *fpo = (ForPortionOfExpr *) node;
    3689              :                 ForPortionOfExpr *newnode;
    3690              : 
    3691           28 :                 FLATCOPY(newnode, fpo, ForPortionOfExpr);
    3692           28 :                 MUTATE(newnode->rangeVar, fpo->rangeVar, Var *);
    3693           28 :                 MUTATE(newnode->targetFrom, fpo->targetFrom, Node *);
    3694           28 :                 MUTATE(newnode->targetTo, fpo->targetTo, Node *);
    3695           28 :                 MUTATE(newnode->targetRange, fpo->targetRange, Node *);
    3696           28 :                 MUTATE(newnode->overlapsExpr, fpo->overlapsExpr, Node *);
    3697           28 :                 MUTATE(newnode->rangeTargetList, fpo->rangeTargetList, List *);
    3698              : 
    3699           28 :                 return (Node *) newnode;
    3700              :             }
    3701              :             break;
    3702          130 :         case T_PartitionPruneStepOp:
    3703              :             {
    3704          130 :                 PartitionPruneStepOp *opstep = (PartitionPruneStepOp *) node;
    3705              :                 PartitionPruneStepOp *newnode;
    3706              : 
    3707          130 :                 FLATCOPY(newnode, opstep, PartitionPruneStepOp);
    3708          130 :                 MUTATE(newnode->exprs, opstep->exprs, List *);
    3709              : 
    3710          130 :                 return (Node *) newnode;
    3711              :             }
    3712              :             break;
    3713           10 :         case T_PartitionPruneStepCombine:
    3714              :             /* no expression sub-nodes */
    3715           10 :             return copyObject(node);
    3716         6141 :         case T_JoinExpr:
    3717              :             {
    3718         6141 :                 JoinExpr   *join = (JoinExpr *) node;
    3719              :                 JoinExpr   *newnode;
    3720              : 
    3721         6141 :                 FLATCOPY(newnode, join, JoinExpr);
    3722         6141 :                 MUTATE(newnode->larg, join->larg, Node *);
    3723         6141 :                 MUTATE(newnode->rarg, join->rarg, Node *);
    3724         6141 :                 MUTATE(newnode->quals, join->quals, Node *);
    3725              :                 /* We do not mutate alias or using by default */
    3726         6141 :                 return (Node *) newnode;
    3727              :             }
    3728              :             break;
    3729          169 :         case T_SetOperationStmt:
    3730              :             {
    3731          169 :                 SetOperationStmt *setop = (SetOperationStmt *) node;
    3732              :                 SetOperationStmt *newnode;
    3733              : 
    3734          169 :                 FLATCOPY(newnode, setop, SetOperationStmt);
    3735          169 :                 MUTATE(newnode->larg, setop->larg, Node *);
    3736          169 :                 MUTATE(newnode->rarg, setop->rarg, Node *);
    3737              :                 /* We do not mutate groupClauses by default */
    3738          169 :                 return (Node *) newnode;
    3739              :             }
    3740              :             break;
    3741          447 :         case T_IndexClause:
    3742              :             {
    3743          447 :                 IndexClause *iclause = (IndexClause *) node;
    3744              :                 IndexClause *newnode;
    3745              : 
    3746          447 :                 FLATCOPY(newnode, iclause, IndexClause);
    3747          447 :                 MUTATE(newnode->rinfo, iclause->rinfo, RestrictInfo *);
    3748          447 :                 MUTATE(newnode->indexquals, iclause->indexquals, List *);
    3749          447 :                 return (Node *) newnode;
    3750              :             }
    3751              :             break;
    3752        12779 :         case T_PlaceHolderVar:
    3753              :             {
    3754        12779 :                 PlaceHolderVar *phv = (PlaceHolderVar *) node;
    3755              :                 PlaceHolderVar *newnode;
    3756              : 
    3757        12779 :                 FLATCOPY(newnode, phv, PlaceHolderVar);
    3758        12779 :                 MUTATE(newnode->phexpr, phv->phexpr, Expr *);
    3759              :                 /* Assume we need not copy the relids bitmapsets */
    3760        12779 :                 return (Node *) newnode;
    3761              :             }
    3762              :             break;
    3763         2429 :         case T_InferenceElem:
    3764              :             {
    3765         2429 :                 InferenceElem *inferenceelemdexpr = (InferenceElem *) node;
    3766              :                 InferenceElem *newnode;
    3767              : 
    3768         2429 :                 FLATCOPY(newnode, inferenceelemdexpr, InferenceElem);
    3769         2429 :                 MUTATE(newnode->expr, newnode->expr, Node *);
    3770         2429 :                 return (Node *) newnode;
    3771              :             }
    3772              :             break;
    3773        20063 :         case T_AppendRelInfo:
    3774              :             {
    3775        20063 :                 AppendRelInfo *appinfo = (AppendRelInfo *) node;
    3776              :                 AppendRelInfo *newnode;
    3777              : 
    3778        20063 :                 FLATCOPY(newnode, appinfo, AppendRelInfo);
    3779        20063 :                 MUTATE(newnode->translated_vars, appinfo->translated_vars, List *);
    3780              :                 /* Assume nothing need be done with parent_colnos[] */
    3781        20063 :                 return (Node *) newnode;
    3782              :             }
    3783              :             break;
    3784            0 :         case T_PlaceHolderInfo:
    3785              :             {
    3786            0 :                 PlaceHolderInfo *phinfo = (PlaceHolderInfo *) node;
    3787              :                 PlaceHolderInfo *newnode;
    3788              : 
    3789            0 :                 FLATCOPY(newnode, phinfo, PlaceHolderInfo);
    3790            0 :                 MUTATE(newnode->ph_var, phinfo->ph_var, PlaceHolderVar *);
    3791              :                 /* Assume we need not copy the relids bitmapsets */
    3792            0 :                 return (Node *) newnode;
    3793              :             }
    3794              :             break;
    3795        70430 :         case T_RangeTblFunction:
    3796              :             {
    3797        70430 :                 RangeTblFunction *rtfunc = (RangeTblFunction *) node;
    3798              :                 RangeTblFunction *newnode;
    3799              : 
    3800        70430 :                 FLATCOPY(newnode, rtfunc, RangeTblFunction);
    3801        70430 :                 MUTATE(newnode->funcexpr, rtfunc->funcexpr, Node *);
    3802              :                 /* Assume we need not copy the coldef info lists */
    3803        70430 :                 return (Node *) newnode;
    3804              :             }
    3805              :             break;
    3806          375 :         case T_TableSampleClause:
    3807              :             {
    3808          375 :                 TableSampleClause *tsc = (TableSampleClause *) node;
    3809              :                 TableSampleClause *newnode;
    3810              : 
    3811          375 :                 FLATCOPY(newnode, tsc, TableSampleClause);
    3812          375 :                 MUTATE(newnode->args, tsc->args, List *);
    3813          375 :                 MUTATE(newnode->repeatable, tsc->repeatable, Expr *);
    3814          375 :                 return (Node *) newnode;
    3815              :             }
    3816              :             break;
    3817          854 :         case T_TableFunc:
    3818              :             {
    3819          854 :                 TableFunc  *tf = (TableFunc *) node;
    3820              :                 TableFunc  *newnode;
    3821              : 
    3822          854 :                 FLATCOPY(newnode, tf, TableFunc);
    3823          854 :                 MUTATE(newnode->ns_uris, tf->ns_uris, List *);
    3824          854 :                 MUTATE(newnode->docexpr, tf->docexpr, Node *);
    3825          854 :                 MUTATE(newnode->rowexpr, tf->rowexpr, Node *);
    3826          854 :                 MUTATE(newnode->colexprs, tf->colexprs, List *);
    3827          854 :                 MUTATE(newnode->coldefexprs, tf->coldefexprs, List *);
    3828          854 :                 MUTATE(newnode->colvalexprs, tf->colvalexprs, List *);
    3829          854 :                 MUTATE(newnode->passingvalexprs, tf->passingvalexprs, List *);
    3830          854 :                 return (Node *) newnode;
    3831              :             }
    3832              :             break;
    3833            4 :         case T_GraphElementPattern:
    3834              :             {
    3835            4 :                 GraphElementPattern *gep = (GraphElementPattern *) node;
    3836              :                 GraphElementPattern *newnode;
    3837              : 
    3838            4 :                 FLATCOPY(newnode, gep, GraphElementPattern);
    3839            4 :                 MUTATE(newnode->labelexpr, gep->labelexpr, Node *);
    3840            4 :                 MUTATE(newnode->subexpr, gep->subexpr, List *);
    3841            4 :                 MUTATE(newnode->whereClause, gep->whereClause, Node *);
    3842            4 :                 newnode->quantifier = list_copy(gep->quantifier);
    3843            4 :                 return (Node *) newnode;
    3844              :             }
    3845              :             break;
    3846            4 :         case T_GraphPattern:
    3847              :             {
    3848            4 :                 GraphPattern *gp = (GraphPattern *) node;
    3849              :                 GraphPattern *newnode;
    3850              : 
    3851            4 :                 FLATCOPY(newnode, gp, GraphPattern);
    3852            4 :                 MUTATE(newnode->path_pattern_list, gp->path_pattern_list, List *);
    3853            4 :                 MUTATE(newnode->whereClause, gp->whereClause, Node *);
    3854            4 :                 return (Node *) newnode;
    3855              :             }
    3856              :             break;
    3857            0 :         default:
    3858            0 :             elog(ERROR, "unrecognized node type: %d",
    3859              :                  (int) nodeTag(node));
    3860              :             break;
    3861              :     }
    3862              :     /* can't get here, but keep compiler happy */
    3863              :     return NULL;
    3864              : }
    3865              : 
    3866              : 
    3867              : /*
    3868              :  * query_tree_mutator --- initiate modification of a Query's expressions
    3869              :  *
    3870              :  * This routine exists just to reduce the number of places that need to know
    3871              :  * where all the expression subtrees of a Query are.  Note it can be used
    3872              :  * for starting a walk at top level of a Query regardless of whether the
    3873              :  * mutator intends to descend into subqueries.  It is also useful for
    3874              :  * descending into subqueries within a mutator.
    3875              :  *
    3876              :  * Some callers want to suppress mutating of certain items in the Query,
    3877              :  * typically because they need to process them specially, or don't actually
    3878              :  * want to recurse into subqueries.  This is supported by the flags argument,
    3879              :  * which is the bitwise OR of flag values to suppress mutating of
    3880              :  * indicated items.  (More flag bits may be added as needed.)
    3881              :  *
    3882              :  * Normally the top-level Query node itself is copied, but some callers want
    3883              :  * it to be modified in-place; they must pass QTW_DONT_COPY_QUERY in flags.
    3884              :  * All modified substructure is safely copied in any case.
    3885              :  */
    3886              : Query *
    3887        23021 : query_tree_mutator_impl(Query *query,
    3888              :                         tree_mutator_callback mutator,
    3889              :                         void *context,
    3890              :                         int flags)
    3891              : {
    3892              :     Assert(query != NULL && IsA(query, Query));
    3893              : 
    3894        23021 :     if (!(flags & QTW_DONT_COPY_QUERY))
    3895              :     {
    3896              :         Query      *newquery;
    3897              : 
    3898        23021 :         FLATCOPY(newquery, query, Query);
    3899        23021 :         query = newquery;
    3900              :     }
    3901              : 
    3902        23021 :     MUTATE(query->targetList, query->targetList, List *);
    3903        23021 :     MUTATE(query->withCheckOptions, query->withCheckOptions, List *);
    3904        23021 :     MUTATE(query->onConflict, query->onConflict, OnConflictExpr *);
    3905        23021 :     MUTATE(query->mergeActionList, query->mergeActionList, List *);
    3906        23021 :     MUTATE(query->mergeJoinCondition, query->mergeJoinCondition, Node *);
    3907        23021 :     MUTATE(query->forPortionOf, query->forPortionOf, ForPortionOfExpr *);
    3908        23021 :     MUTATE(query->returningList, query->returningList, List *);
    3909        23021 :     MUTATE(query->jointree, query->jointree, FromExpr *);
    3910        23021 :     MUTATE(query->setOperations, query->setOperations, Node *);
    3911        23021 :     MUTATE(query->havingQual, query->havingQual, Node *);
    3912        23021 :     MUTATE(query->limitOffset, query->limitOffset, Node *);
    3913        23021 :     MUTATE(query->limitCount, query->limitCount, Node *);
    3914              : 
    3915              :     /*
    3916              :      * Most callers aren't interested in SortGroupClause nodes since those
    3917              :      * don't contain actual expressions. However they do contain OIDs, which
    3918              :      * may be of interest to some mutators.
    3919              :      */
    3920              : 
    3921        23021 :     if ((flags & QTW_EXAMINE_SORTGROUP))
    3922              :     {
    3923            0 :         MUTATE(query->groupClause, query->groupClause, List *);
    3924            0 :         MUTATE(query->windowClause, query->windowClause, List *);
    3925            0 :         MUTATE(query->sortClause, query->sortClause, List *);
    3926            0 :         MUTATE(query->distinctClause, query->distinctClause, List *);
    3927              :     }
    3928              :     else
    3929              :     {
    3930              :         /*
    3931              :          * But we need to mutate the expressions under WindowClause nodes even
    3932              :          * if we're not interested in SortGroupClause nodes.
    3933              :          */
    3934              :         List       *resultlist;
    3935              :         ListCell   *temp;
    3936              : 
    3937        23021 :         resultlist = NIL;
    3938        23165 :         foreach(temp, query->windowClause)
    3939              :         {
    3940          144 :             WindowClause *wc = lfirst_node(WindowClause, temp);
    3941              :             WindowClause *newnode;
    3942              : 
    3943          144 :             FLATCOPY(newnode, wc, WindowClause);
    3944          144 :             MUTATE(newnode->startOffset, wc->startOffset, Node *);
    3945          144 :             MUTATE(newnode->endOffset, wc->endOffset, Node *);
    3946              : 
    3947          144 :             resultlist = lappend(resultlist, (Node *) newnode);
    3948              :         }
    3949        23021 :         query->windowClause = resultlist;
    3950              :     }
    3951              : 
    3952              :     /*
    3953              :      * groupingSets and rowMarks are not mutated:
    3954              :      *
    3955              :      * groupingSets contain only ressortgroup refs (integers) which are
    3956              :      * meaningless without the groupClause or tlist. Accordingly, any mutator
    3957              :      * that needs to care about them needs to handle them itself in its Query
    3958              :      * processing.
    3959              :      *
    3960              :      * rowMarks contains only rangetable indexes (and flags etc.) and
    3961              :      * therefore should be handled at Query level similarly.
    3962              :      */
    3963              : 
    3964        23021 :     if (!(flags & QTW_IGNORE_CTE_SUBQUERIES))
    3965        23021 :         MUTATE(query->cteList, query->cteList, List *);
    3966              :     else                        /* else copy CTE list as-is */
    3967            0 :         query->cteList = copyObject(query->cteList);
    3968        23021 :     query->rtable = range_table_mutator(query->rtable,
    3969              :                                         mutator, context, flags);
    3970        23021 :     return query;
    3971              : }
    3972              : 
    3973              : /*
    3974              :  * range_table_mutator is just the part of query_tree_mutator that processes
    3975              :  * a query's rangetable.  This is split out since it can be useful on
    3976              :  * its own.
    3977              :  */
    3978              : List *
    3979        23021 : range_table_mutator_impl(List *rtable,
    3980              :                          tree_mutator_callback mutator,
    3981              :                          void *context,
    3982              :                          int flags)
    3983              : {
    3984        23021 :     List       *newrt = NIL;
    3985              :     ListCell   *rt;
    3986              : 
    3987        67778 :     foreach(rt, rtable)
    3988              :     {
    3989        44757 :         RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
    3990              :         RangeTblEntry *newrte;
    3991              : 
    3992        44757 :         FLATCOPY(newrte, rte, RangeTblEntry);
    3993        44757 :         switch (rte->rtekind)
    3994              :         {
    3995        27739 :             case RTE_RELATION:
    3996        27739 :                 MUTATE(newrte->tablesample, rte->tablesample,
    3997              :                        TableSampleClause *);
    3998              :                 /* we don't bother to copy eref, aliases, etc; OK? */
    3999        27739 :                 break;
    4000         3489 :             case RTE_SUBQUERY:
    4001         3489 :                 if (!(flags & QTW_IGNORE_RT_SUBQUERIES))
    4002         3489 :                     MUTATE(newrte->subquery, rte->subquery, Query *);
    4003              :                 else
    4004              :                 {
    4005              :                     /* else, copy RT subqueries as-is */
    4006            0 :                     newrte->subquery = copyObject(rte->subquery);
    4007              :                 }
    4008         3489 :                 break;
    4009         6144 :             case RTE_JOIN:
    4010         6144 :                 if (!(flags & QTW_IGNORE_JOINALIASES))
    4011         5750 :                     MUTATE(newrte->joinaliasvars, rte->joinaliasvars, List *);
    4012              :                 else
    4013              :                 {
    4014              :                     /* else, copy join aliases as-is */
    4015          394 :                     newrte->joinaliasvars = copyObject(rte->joinaliasvars);
    4016              :                 }
    4017         6144 :                 break;
    4018         5719 :             case RTE_FUNCTION:
    4019         5719 :                 MUTATE(newrte->functions, rte->functions, List *);
    4020         5719 :                 break;
    4021            0 :             case RTE_TABLEFUNC:
    4022            0 :                 MUTATE(newrte->tablefunc, rte->tablefunc, TableFunc *);
    4023            0 :                 break;
    4024         1033 :             case RTE_VALUES:
    4025         1033 :                 MUTATE(newrte->values_lists, rte->values_lists, List *);
    4026         1033 :                 break;
    4027            4 :             case RTE_GRAPH_TABLE:
    4028            4 :                 MUTATE(newrte->graph_pattern, rte->graph_pattern, GraphPattern *);
    4029            4 :                 MUTATE(newrte->graph_table_columns, rte->graph_table_columns, List *);
    4030            4 :                 break;
    4031          427 :             case RTE_CTE:
    4032              :             case RTE_NAMEDTUPLESTORE:
    4033              :             case RTE_RESULT:
    4034              :                 /* nothing to do */
    4035          427 :                 break;
    4036          202 :             case RTE_GROUP:
    4037          202 :                 if (!(flags & QTW_IGNORE_GROUPEXPRS))
    4038          202 :                     MUTATE(newrte->groupexprs, rte->groupexprs, List *);
    4039              :                 else
    4040              :                 {
    4041              :                     /* else, copy grouping exprs as-is */
    4042            0 :                     newrte->groupexprs = copyObject(rte->groupexprs);
    4043              :                 }
    4044          202 :                 break;
    4045              :         }
    4046        44757 :         MUTATE(newrte->securityQuals, rte->securityQuals, List *);
    4047        44757 :         newrt = lappend(newrt, newrte);
    4048              :     }
    4049        23021 :     return newrt;
    4050              : }
    4051              : 
    4052              : /*
    4053              :  * query_or_expression_tree_walker --- hybrid form
    4054              :  *
    4055              :  * This routine will invoke query_tree_walker if called on a Query node,
    4056              :  * else will invoke the walker directly.  This is a useful way of starting
    4057              :  * the recursion when the walker's normal change of state is not appropriate
    4058              :  * for the outermost Query node.
    4059              :  */
    4060              : bool
    4061      3247524 : query_or_expression_tree_walker_impl(Node *node,
    4062              :                                      tree_walker_callback walker,
    4063              :                                      void *context,
    4064              :                                      int flags)
    4065              : {
    4066      3247524 :     if (node && IsA(node, Query))
    4067       318540 :         return query_tree_walker((Query *) node,
    4068              :                                  walker,
    4069              :                                  context,
    4070              :                                  flags);
    4071              :     else
    4072      2928984 :         return WALK(node);
    4073              : }
    4074              : 
    4075              : /*
    4076              :  * query_or_expression_tree_mutator --- hybrid form
    4077              :  *
    4078              :  * This routine will invoke query_tree_mutator if called on a Query node,
    4079              :  * else will invoke the mutator directly.  This is a useful way of starting
    4080              :  * the recursion when the mutator's normal change of state is not appropriate
    4081              :  * for the outermost Query node.
    4082              :  */
    4083              : Node *
    4084       482642 : query_or_expression_tree_mutator_impl(Node *node,
    4085              :                                       tree_mutator_callback mutator,
    4086              :                                       void *context,
    4087              :                                       int flags)
    4088              : {
    4089       482642 :     if (node && IsA(node, Query))
    4090         6713 :         return (Node *) query_tree_mutator((Query *) node,
    4091              :                                            mutator,
    4092              :                                            context,
    4093              :                                            flags);
    4094              :     else
    4095       475929 :         return mutator(node, context);
    4096              : }
    4097              : 
    4098              : 
    4099              : /*
    4100              :  * raw_expression_tree_walker --- walk raw parse trees
    4101              :  *
    4102              :  * This has exactly the same API as expression_tree_walker, but instead of
    4103              :  * walking post-analysis parse trees, it knows how to walk the node types
    4104              :  * found in raw grammar output.  (There is not currently any need for a
    4105              :  * combined walker, so we keep them separate in the name of efficiency.)
    4106              :  * Unlike expression_tree_walker, there is no special rule about query
    4107              :  * boundaries: we descend to everything that's possibly interesting.
    4108              :  *
    4109              :  * Currently, the node type coverage here extends only to DML statements
    4110              :  * (SELECT/INSERT/UPDATE/DELETE/MERGE) and nodes that can appear in them,
    4111              :  * because this is used mainly during analysis of CTEs, and only DML
    4112              :  * statements can appear in CTEs.
    4113              :  */
    4114              : bool
    4115      8117397 : raw_expression_tree_walker_impl(Node *node,
    4116              :                                 tree_walker_callback walker,
    4117              :                                 void *context)
    4118              : {
    4119              :     ListCell   *temp;
    4120              : 
    4121              :     /*
    4122              :      * The walker has already visited the current node, and so we need only
    4123              :      * recurse into any sub-nodes it has.
    4124              :      */
    4125      8117397 :     if (node == NULL)
    4126            0 :         return false;
    4127              : 
    4128              :     /* Guard against stack overflow due to overly complex expressions */
    4129      8117397 :     check_stack_depth();
    4130              : 
    4131      8117397 :     switch (nodeTag(node))
    4132              :     {
    4133      1007352 :         case T_JsonFormat:
    4134              :         case T_SetToDefault:
    4135              :         case T_CurrentOfExpr:
    4136              :         case T_SQLValueFunction:
    4137              :         case T_Integer:
    4138              :         case T_Float:
    4139              :         case T_Boolean:
    4140              :         case T_String:
    4141              :         case T_BitString:
    4142              :         case T_ParamRef:
    4143              :         case T_A_Const:
    4144              :         case T_A_Star:
    4145              :         case T_MergeSupportFunc:
    4146              :         case T_ReturningOption:
    4147              :             /* primitive node types with no subnodes */
    4148      1007352 :             break;
    4149       259961 :         case T_Alias:
    4150              :             /* we assume the colnames list isn't interesting */
    4151       259961 :             break;
    4152       416138 :         case T_RangeVar:
    4153       416138 :             return WALK(((RangeVar *) node)->alias);
    4154          328 :         case T_GroupingFunc:
    4155          328 :             return WALK(((GroupingFunc *) node)->args);
    4156        39278 :         case T_SubLink:
    4157              :             {
    4158        39278 :                 SubLink    *sublink = (SubLink *) node;
    4159              : 
    4160        39278 :                 if (WALK(sublink->testexpr))
    4161            0 :                     return true;
    4162              :                 /* we assume the operName is not interesting */
    4163        39278 :                 if (WALK(sublink->subselect))
    4164            0 :                     return true;
    4165              :             }
    4166        39278 :             break;
    4167        29193 :         case T_CaseExpr:
    4168              :             {
    4169        29193 :                 CaseExpr   *caseexpr = (CaseExpr *) node;
    4170              : 
    4171        29193 :                 if (WALK(caseexpr->arg))
    4172            0 :                     return true;
    4173              :                 /* we assume walker doesn't care about CaseWhens, either */
    4174        78532 :                 foreach(temp, caseexpr->args)
    4175              :                 {
    4176        49339 :                     CaseWhen   *when = lfirst_node(CaseWhen, temp);
    4177              : 
    4178        49339 :                     if (WALK(when->expr))
    4179            0 :                         return true;
    4180        49339 :                     if (WALK(when->result))
    4181            0 :                         return true;
    4182              :                 }
    4183        29193 :                 if (WALK(caseexpr->defresult))
    4184            0 :                     return true;
    4185              :             }
    4186        29193 :             break;
    4187         5089 :         case T_RowExpr:
    4188              :             /* Assume colnames isn't interesting */
    4189         5089 :             return WALK(((RowExpr *) node)->args);
    4190         3792 :         case T_CoalesceExpr:
    4191         3792 :             return WALK(((CoalesceExpr *) node)->args);
    4192          262 :         case T_MinMaxExpr:
    4193          262 :             return WALK(((MinMaxExpr *) node)->args);
    4194          413 :         case T_XmlExpr:
    4195              :             {
    4196          413 :                 XmlExpr    *xexpr = (XmlExpr *) node;
    4197              : 
    4198          413 :                 if (WALK(xexpr->named_args))
    4199            0 :                     return true;
    4200              :                 /* we assume walker doesn't care about arg_names */
    4201          413 :                 if (WALK(xexpr->args))
    4202            0 :                     return true;
    4203              :             }
    4204          413 :             break;
    4205         1054 :         case T_JsonReturning:
    4206         1054 :             return WALK(((JsonReturning *) node)->format);
    4207         2678 :         case T_JsonValueExpr:
    4208              :             {
    4209         2678 :                 JsonValueExpr *jve = (JsonValueExpr *) node;
    4210              : 
    4211         2678 :                 if (WALK(jve->raw_expr))
    4212            0 :                     return true;
    4213         2678 :                 if (WALK(jve->formatted_expr))
    4214            0 :                     return true;
    4215         2678 :                 if (WALK(jve->format))
    4216            0 :                     return true;
    4217              :             }
    4218         2678 :             break;
    4219          104 :         case T_JsonParseExpr:
    4220              :             {
    4221          104 :                 JsonParseExpr *jpe = (JsonParseExpr *) node;
    4222              : 
    4223          104 :                 if (WALK(jpe->expr))
    4224            0 :                     return true;
    4225          104 :                 if (WALK(jpe->output))
    4226            0 :                     return true;
    4227              :             }
    4228          104 :             break;
    4229           70 :         case T_JsonScalarExpr:
    4230              :             {
    4231           70 :                 JsonScalarExpr *jse = (JsonScalarExpr *) node;
    4232              : 
    4233           70 :                 if (WALK(jse->expr))
    4234            0 :                     return true;
    4235           70 :                 if (WALK(jse->output))
    4236            0 :                     return true;
    4237              :             }
    4238           70 :             break;
    4239           68 :         case T_JsonSerializeExpr:
    4240              :             {
    4241           68 :                 JsonSerializeExpr *jse = (JsonSerializeExpr *) node;
    4242              : 
    4243           68 :                 if (WALK(jse->expr))
    4244            0 :                     return true;
    4245           68 :                 if (WALK(jse->output))
    4246            0 :                     return true;
    4247              :             }
    4248           68 :             break;
    4249            0 :         case T_JsonConstructorExpr:
    4250              :             {
    4251            0 :                 JsonConstructorExpr *ctor = (JsonConstructorExpr *) node;
    4252              : 
    4253            0 :                 if (WALK(ctor->args))
    4254            0 :                     return true;
    4255            0 :                 if (WALK(ctor->func))
    4256            0 :                     return true;
    4257            0 :                 if (WALK(ctor->coercion))
    4258            0 :                     return true;
    4259            0 :                 if (WALK(ctor->returning))
    4260            0 :                     return true;
    4261              :             }
    4262            0 :             break;
    4263          280 :         case T_JsonIsPredicate:
    4264          280 :             return WALK(((JsonIsPredicate *) node)->expr);
    4265          252 :         case T_JsonArgument:
    4266          252 :             return WALK(((JsonArgument *) node)->val);
    4267         1016 :         case T_JsonFuncExpr:
    4268              :             {
    4269         1016 :                 JsonFuncExpr *jfe = (JsonFuncExpr *) node;
    4270              : 
    4271         1016 :                 if (WALK(jfe->context_item))
    4272            0 :                     return true;
    4273         1016 :                 if (WALK(jfe->pathspec))
    4274            0 :                     return true;
    4275         1016 :                 if (WALK(jfe->passing))
    4276            0 :                     return true;
    4277         1016 :                 if (WALK(jfe->output))
    4278            0 :                     return true;
    4279         1016 :                 if (WALK(jfe->on_empty))
    4280            0 :                     return true;
    4281         1016 :                 if (WALK(jfe->on_error))
    4282            0 :                     return true;
    4283              :             }
    4284         1016 :             break;
    4285          732 :         case T_JsonBehavior:
    4286              :             {
    4287          732 :                 JsonBehavior *jb = (JsonBehavior *) node;
    4288              : 
    4289          732 :                 if (WALK(jb->expr))
    4290            0 :                     return true;
    4291              :             }
    4292          732 :             break;
    4293          348 :         case T_JsonTable:
    4294              :             {
    4295          348 :                 JsonTable  *jt = (JsonTable *) node;
    4296              : 
    4297          348 :                 if (WALK(jt->context_item))
    4298            0 :                     return true;
    4299          348 :                 if (WALK(jt->pathspec))
    4300            0 :                     return true;
    4301          348 :                 if (WALK(jt->passing))
    4302            0 :                     return true;
    4303          348 :                 if (WALK(jt->columns))
    4304            0 :                     return true;
    4305          348 :                 if (WALK(jt->on_error))
    4306            0 :                     return true;
    4307              :             }
    4308          348 :             break;
    4309          892 :         case T_JsonTableColumn:
    4310              :             {
    4311          892 :                 JsonTableColumn *jtc = (JsonTableColumn *) node;
    4312              : 
    4313          892 :                 if (WALK(jtc->typeName))
    4314            0 :                     return true;
    4315          892 :                 if (WALK(jtc->on_empty))
    4316            0 :                     return true;
    4317          892 :                 if (WALK(jtc->on_error))
    4318            0 :                     return true;
    4319          892 :                 if (WALK(jtc->columns))
    4320            0 :                     return true;
    4321              :             }
    4322          892 :             break;
    4323          348 :         case T_JsonTablePathSpec:
    4324          348 :             return WALK(((JsonTablePathSpec *) node)->string);
    4325        15615 :         case T_NullTest:
    4326        15615 :             return WALK(((NullTest *) node)->arg);
    4327          779 :         case T_BooleanTest:
    4328          779 :             return WALK(((BooleanTest *) node)->arg);
    4329        74796 :         case T_JoinExpr:
    4330              :             {
    4331        74796 :                 JoinExpr   *join = (JoinExpr *) node;
    4332              : 
    4333        74796 :                 if (WALK(join->larg))
    4334            0 :                     return true;
    4335        74796 :                 if (WALK(join->rarg))
    4336            0 :                     return true;
    4337        74796 :                 if (WALK(join->quals))
    4338            0 :                     return true;
    4339        74796 :                 if (WALK(join->alias))
    4340            0 :                     return true;
    4341              :                 /* using list is deemed uninteresting */
    4342              :             }
    4343        74796 :             break;
    4344           20 :         case T_IntoClause:
    4345              :             {
    4346           20 :                 IntoClause *into = (IntoClause *) node;
    4347              : 
    4348           20 :                 if (WALK(into->rel))
    4349            0 :                     return true;
    4350              :                 /* colNames, options are deemed uninteresting */
    4351              :                 /* viewQuery should be null in raw parsetree, but check it */
    4352           20 :                 if (WALK(into->viewQuery))
    4353            0 :                     return true;
    4354              :             }
    4355           20 :             break;
    4356      1511669 :         case T_List:
    4357      4264899 :             foreach(temp, (List *) node)
    4358              :             {
    4359      2753298 :                 if (WALK((Node *) lfirst(temp)))
    4360            0 :                     return true;
    4361              :             }
    4362      1511601 :             break;
    4363        45303 :         case T_InsertStmt:
    4364              :             {
    4365        45303 :                 InsertStmt *stmt = (InsertStmt *) node;
    4366              : 
    4367        45303 :                 if (WALK(stmt->relation))
    4368            0 :                     return true;
    4369        45303 :                 if (WALK(stmt->cols))
    4370            0 :                     return true;
    4371        45303 :                 if (WALK(stmt->selectStmt))
    4372            0 :                     return true;
    4373        45303 :                 if (WALK(stmt->onConflictClause))
    4374            0 :                     return true;
    4375        45303 :                 if (WALK(stmt->returningClause))
    4376            0 :                     return true;
    4377        45303 :                 if (WALK(stmt->withClause))
    4378            0 :                     return true;
    4379              :             }
    4380        45303 :             break;
    4381         3390 :         case T_DeleteStmt:
    4382              :             {
    4383         3390 :                 DeleteStmt *stmt = (DeleteStmt *) node;
    4384              : 
    4385         3390 :                 if (WALK(stmt->relation))
    4386            0 :                     return true;
    4387         3390 :                 if (WALK(stmt->usingClause))
    4388            0 :                     return true;
    4389         3390 :                 if (WALK(stmt->whereClause))
    4390            0 :                     return true;
    4391         3390 :                 if (WALK(stmt->returningClause))
    4392            0 :                     return true;
    4393         3390 :                 if (WALK(stmt->withClause))
    4394            0 :                     return true;
    4395              :             }
    4396         3390 :             break;
    4397         9351 :         case T_UpdateStmt:
    4398              :             {
    4399         9351 :                 UpdateStmt *stmt = (UpdateStmt *) node;
    4400              : 
    4401         9351 :                 if (WALK(stmt->relation))
    4402            0 :                     return true;
    4403         9351 :                 if (WALK(stmt->targetList))
    4404            0 :                     return true;
    4405         9351 :                 if (WALK(stmt->whereClause))
    4406            0 :                     return true;
    4407         9351 :                 if (WALK(stmt->fromClause))
    4408            0 :                     return true;
    4409         9351 :                 if (WALK(stmt->returningClause))
    4410            0 :                     return true;
    4411         9351 :                 if (WALK(stmt->withClause))
    4412            0 :                     return true;
    4413              :             }
    4414         9351 :             break;
    4415         1412 :         case T_MergeStmt:
    4416              :             {
    4417         1412 :                 MergeStmt  *stmt = (MergeStmt *) node;
    4418              : 
    4419         1412 :                 if (WALK(stmt->relation))
    4420            0 :                     return true;
    4421         1412 :                 if (WALK(stmt->sourceRelation))
    4422            0 :                     return true;
    4423         1412 :                 if (WALK(stmt->joinCondition))
    4424            0 :                     return true;
    4425         1412 :                 if (WALK(stmt->mergeWhenClauses))
    4426            0 :                     return true;
    4427         1412 :                 if (WALK(stmt->returningClause))
    4428            0 :                     return true;
    4429         1412 :                 if (WALK(stmt->withClause))
    4430            0 :                     return true;
    4431              :             }
    4432         1412 :             break;
    4433         2153 :         case T_MergeWhenClause:
    4434              :             {
    4435         2153 :                 MergeWhenClause *mergeWhenClause = (MergeWhenClause *) node;
    4436              : 
    4437         2153 :                 if (WALK(mergeWhenClause->condition))
    4438            0 :                     return true;
    4439         2153 :                 if (WALK(mergeWhenClause->targetList))
    4440            0 :                     return true;
    4441         2153 :                 if (WALK(mergeWhenClause->values))
    4442            0 :                     return true;
    4443              :             }
    4444         2153 :             break;
    4445         2616 :         case T_ReturningClause:
    4446              :             {
    4447         2616 :                 ReturningClause *returning = (ReturningClause *) node;
    4448              : 
    4449         2616 :                 if (WALK(returning->options))
    4450            0 :                     return true;
    4451         2616 :                 if (WALK(returning->exprs))
    4452            0 :                     return true;
    4453              :             }
    4454         2616 :             break;
    4455       465207 :         case T_SelectStmt:
    4456              :             {
    4457       465207 :                 SelectStmt *stmt = (SelectStmt *) node;
    4458              : 
    4459       465207 :                 if (WALK(stmt->distinctClause))
    4460            0 :                     return true;
    4461       465207 :                 if (WALK(stmt->intoClause))
    4462            0 :                     return true;
    4463       465207 :                 if (WALK(stmt->targetList))
    4464            0 :                     return true;
    4465       465203 :                 if (WALK(stmt->fromClause))
    4466            0 :                     return true;
    4467       465147 :                 if (WALK(stmt->whereClause))
    4468            0 :                     return true;
    4469       465143 :                 if (WALK(stmt->groupClause))
    4470            0 :                     return true;
    4471       465143 :                 if (WALK(stmt->havingClause))
    4472            0 :                     return true;
    4473       465143 :                 if (WALK(stmt->windowClause))
    4474            0 :                     return true;
    4475       465143 :                 if (WALK(stmt->valuesLists))
    4476            0 :                     return true;
    4477       465143 :                 if (WALK(stmt->sortClause))
    4478            0 :                     return true;
    4479       465143 :                 if (WALK(stmt->limitOffset))
    4480            0 :                     return true;
    4481       465143 :                 if (WALK(stmt->limitCount))
    4482            0 :                     return true;
    4483       465143 :                 if (WALK(stmt->lockingClause))
    4484            0 :                     return true;
    4485       465143 :                 if (WALK(stmt->withClause))
    4486            0 :                     return true;
    4487       465143 :                 if (WALK(stmt->larg))
    4488            0 :                     return true;
    4489       465143 :                 if (WALK(stmt->rarg))
    4490            0 :                     return true;
    4491              :             }
    4492       465135 :             break;
    4493            0 :         case T_PLAssignStmt:
    4494              :             {
    4495            0 :                 PLAssignStmt *stmt = (PLAssignStmt *) node;
    4496              : 
    4497            0 :                 if (WALK(stmt->indirection))
    4498            0 :                     return true;
    4499            0 :                 if (WALK(stmt->val))
    4500            0 :                     return true;
    4501              :             }
    4502            0 :             break;
    4503       522704 :         case T_A_Expr:
    4504              :             {
    4505       522704 :                 A_Expr     *expr = (A_Expr *) node;
    4506              : 
    4507       522704 :                 if (WALK(expr->lexpr))
    4508            0 :                     return true;
    4509       522704 :                 if (WALK(expr->rexpr))
    4510            0 :                     return true;
    4511              :                 /* operator name is deemed uninteresting */
    4512              :             }
    4513       522704 :             break;
    4514       164298 :         case T_BoolExpr:
    4515              :             {
    4516       164298 :                 BoolExpr   *expr = (BoolExpr *) node;
    4517              : 
    4518       164298 :                 if (WALK(expr->args))
    4519            0 :                     return true;
    4520              :             }
    4521       164298 :             break;
    4522      1573342 :         case T_ColumnRef:
    4523              :             /* we assume the fields contain nothing interesting */
    4524      1573342 :             break;
    4525       299317 :         case T_FuncCall:
    4526              :             {
    4527       299317 :                 FuncCall   *fcall = (FuncCall *) node;
    4528              : 
    4529       299317 :                 if (WALK(fcall->args))
    4530            0 :                     return true;
    4531       299317 :                 if (WALK(fcall->agg_order))
    4532            0 :                     return true;
    4533       299317 :                 if (WALK(fcall->agg_filter))
    4534            0 :                     return true;
    4535       299317 :                 if (WALK(fcall->over))
    4536            0 :                     return true;
    4537              :                 /* function name is deemed uninteresting */
    4538              :             }
    4539       299317 :             break;
    4540        26361 :         case T_NamedArgExpr:
    4541        26361 :             return WALK(((NamedArgExpr *) node)->arg);
    4542        13747 :         case T_A_Indices:
    4543              :             {
    4544        13747 :                 A_Indices  *indices = (A_Indices *) node;
    4545              : 
    4546        13747 :                 if (WALK(indices->lidx))
    4547            0 :                     return true;
    4548        13747 :                 if (WALK(indices->uidx))
    4549            0 :                     return true;
    4550              :             }
    4551        13747 :             break;
    4552        22472 :         case T_A_Indirection:
    4553              :             {
    4554        22472 :                 A_Indirection *indir = (A_Indirection *) node;
    4555              : 
    4556        22472 :                 if (WALK(indir->arg))
    4557            0 :                     return true;
    4558        22472 :                 if (WALK(indir->indirection))
    4559            0 :                     return true;
    4560              :             }
    4561        22472 :             break;
    4562         6615 :         case T_A_ArrayExpr:
    4563         6615 :             return WALK(((A_ArrayExpr *) node)->elements);
    4564       938563 :         case T_ResTarget:
    4565              :             {
    4566       938563 :                 ResTarget  *rt = (ResTarget *) node;
    4567              : 
    4568       938563 :                 if (WALK(rt->indirection))
    4569            0 :                     return true;
    4570       938563 :                 if (WALK(rt->val))
    4571            0 :                     return true;
    4572              :             }
    4573       938559 :             break;
    4574          261 :         case T_MultiAssignRef:
    4575          261 :             return WALK(((MultiAssignRef *) node)->source);
    4576       232245 :         case T_TypeCast:
    4577              :             {
    4578       232245 :                 TypeCast   *tc = (TypeCast *) node;
    4579              : 
    4580       232245 :                 if (WALK(tc->arg))
    4581            0 :                     return true;
    4582       232245 :                 if (WALK(tc->typeName))
    4583            0 :                     return true;
    4584              :             }
    4585       232245 :             break;
    4586         7035 :         case T_CollateClause:
    4587         7035 :             return WALK(((CollateClause *) node)->arg);
    4588        82298 :         case T_SortBy:
    4589        82298 :             return WALK(((SortBy *) node)->node);
    4590         3480 :         case T_WindowDef:
    4591              :             {
    4592         3480 :                 WindowDef  *wd = (WindowDef *) node;
    4593              : 
    4594         3480 :                 if (WALK(wd->partitionClause))
    4595            0 :                     return true;
    4596         3480 :                 if (WALK(wd->orderClause))
    4597            0 :                     return true;
    4598         3480 :                 if (WALK(wd->startOffset))
    4599            0 :                     return true;
    4600         3480 :                 if (WALK(wd->endOffset))
    4601            0 :                     return true;
    4602              :             }
    4603         3480 :             break;
    4604        16821 :         case T_RangeSubselect:
    4605              :             {
    4606        16821 :                 RangeSubselect *rs = (RangeSubselect *) node;
    4607              : 
    4608        16821 :                 if (WALK(rs->subquery))
    4609            0 :                     return true;
    4610        16817 :                 if (WALK(rs->alias))
    4611            0 :                     return true;
    4612              :             }
    4613        16817 :             break;
    4614        42089 :         case T_RangeFunction:
    4615              :             {
    4616        42089 :                 RangeFunction *rf = (RangeFunction *) node;
    4617              : 
    4618        42089 :                 if (WALK(rf->functions))
    4619            0 :                     return true;
    4620        42089 :                 if (WALK(rf->alias))
    4621            0 :                     return true;
    4622        42089 :                 if (WALK(rf->coldeflist))
    4623            0 :                     return true;
    4624              :             }
    4625        42089 :             break;
    4626          208 :         case T_RangeTableSample:
    4627              :             {
    4628          208 :                 RangeTableSample *rts = (RangeTableSample *) node;
    4629              : 
    4630          208 :                 if (WALK(rts->relation))
    4631            0 :                     return true;
    4632              :                 /* method name is deemed uninteresting */
    4633          208 :                 if (WALK(rts->args))
    4634            0 :                     return true;
    4635          208 :                 if (WALK(rts->repeatable))
    4636            0 :                     return true;
    4637              :             }
    4638          208 :             break;
    4639          154 :         case T_RangeTableFunc:
    4640              :             {
    4641          154 :                 RangeTableFunc *rtf = (RangeTableFunc *) node;
    4642              : 
    4643          154 :                 if (WALK(rtf->docexpr))
    4644            0 :                     return true;
    4645          154 :                 if (WALK(rtf->rowexpr))
    4646            0 :                     return true;
    4647          154 :                 if (WALK(rtf->namespaces))
    4648            0 :                     return true;
    4649          154 :                 if (WALK(rtf->columns))
    4650            0 :                     return true;
    4651          154 :                 if (WALK(rtf->alias))
    4652            0 :                     return true;
    4653              :             }
    4654          154 :             break;
    4655          545 :         case T_RangeTableFuncCol:
    4656              :             {
    4657          545 :                 RangeTableFuncCol *rtfc = (RangeTableFuncCol *) node;
    4658              : 
    4659          545 :                 if (WALK(rtfc->colexpr))
    4660            0 :                     return true;
    4661          545 :                 if (WALK(rtfc->coldefexpr))
    4662            0 :                     return true;
    4663              :             }
    4664          545 :             break;
    4665          594 :         case T_RangeGraphTable:
    4666              :             {
    4667          594 :                 RangeGraphTable *rgt = (RangeGraphTable *) node;
    4668              : 
    4669          594 :                 if (WALK(rgt->graph_pattern))
    4670            0 :                     return true;
    4671          594 :                 if (WALK(rgt->columns))
    4672            0 :                     return true;
    4673          594 :                 if (WALK(rgt->alias))
    4674            0 :                     return true;
    4675              :             }
    4676          594 :             break;
    4677       235264 :         case T_TypeName:
    4678              :             {
    4679       235264 :                 TypeName   *tn = (TypeName *) node;
    4680              : 
    4681       235264 :                 if (WALK(tn->typmods))
    4682            0 :                     return true;
    4683       235264 :                 if (WALK(tn->arrayBounds))
    4684            0 :                     return true;
    4685              :                 /* type name itself is deemed uninteresting */
    4686              :             }
    4687       235264 :             break;
    4688         1177 :         case T_ColumnDef:
    4689              :             {
    4690         1177 :                 ColumnDef  *coldef = (ColumnDef *) node;
    4691              : 
    4692         1177 :                 if (WALK(coldef->typeName))
    4693            0 :                     return true;
    4694         1177 :                 if (WALK(coldef->raw_default))
    4695            0 :                     return true;
    4696         1177 :                 if (WALK(coldef->collClause))
    4697            0 :                     return true;
    4698              :                 /* for now, constraints are ignored */
    4699              :             }
    4700         1177 :             break;
    4701         1588 :         case T_IndexElem:
    4702              :             {
    4703         1588 :                 IndexElem  *indelem = (IndexElem *) node;
    4704              : 
    4705         1588 :                 if (WALK(indelem->expr))
    4706            0 :                     return true;
    4707              :                 /* collation and opclass names are deemed uninteresting */
    4708              :             }
    4709         1588 :             break;
    4710         1224 :         case T_GroupingSet:
    4711         1224 :             return WALK(((GroupingSet *) node)->content);
    4712         8062 :         case T_LockingClause:
    4713         8062 :             return WALK(((LockingClause *) node)->lockedRels);
    4714          144 :         case T_XmlSerialize:
    4715              :             {
    4716          144 :                 XmlSerialize *xs = (XmlSerialize *) node;
    4717              : 
    4718          144 :                 if (WALK(xs->expr))
    4719            0 :                     return true;
    4720          144 :                 if (WALK(xs->typeName))
    4721            0 :                     return true;
    4722              :             }
    4723          144 :             break;
    4724         2988 :         case T_WithClause:
    4725         2988 :             return WALK(((WithClause *) node)->ctes);
    4726         1420 :         case T_InferClause:
    4727              :             {
    4728         1420 :                 InferClause *stmt = (InferClause *) node;
    4729              : 
    4730         1420 :                 if (WALK(stmt->indexElems))
    4731            0 :                     return true;
    4732         1420 :                 if (WALK(stmt->whereClause))
    4733            0 :                     return true;
    4734              :             }
    4735         1420 :             break;
    4736         1572 :         case T_OnConflictClause:
    4737              :             {
    4738         1572 :                 OnConflictClause *stmt = (OnConflictClause *) node;
    4739              : 
    4740         1572 :                 if (WALK(stmt->infer))
    4741            0 :                     return true;
    4742         1572 :                 if (WALK(stmt->targetList))
    4743            0 :                     return true;
    4744         1572 :                 if (WALK(stmt->whereClause))
    4745            0 :                     return true;
    4746              :             }
    4747         1572 :             break;
    4748         3752 :         case T_CommonTableExpr:
    4749              :             /* search_clause and cycle_clause are not interesting here */
    4750         3752 :             return WALK(((CommonTableExpr *) node)->ctequery);
    4751         1054 :         case T_JsonOutput:
    4752              :             {
    4753         1054 :                 JsonOutput *out = (JsonOutput *) node;
    4754              : 
    4755         1054 :                 if (WALK(out->typeName))
    4756            0 :                     return true;
    4757         1054 :                 if (WALK(out->returning))
    4758            0 :                     return true;
    4759              :             }
    4760         1054 :             break;
    4761          534 :         case T_JsonKeyValue:
    4762              :             {
    4763          534 :                 JsonKeyValue *jkv = (JsonKeyValue *) node;
    4764              : 
    4765          534 :                 if (WALK(jkv->key))
    4766            0 :                     return true;
    4767          534 :                 if (WALK(jkv->value))
    4768            0 :                     return true;
    4769              :             }
    4770          534 :             break;
    4771          290 :         case T_JsonObjectConstructor:
    4772              :             {
    4773          290 :                 JsonObjectConstructor *joc = (JsonObjectConstructor *) node;
    4774              : 
    4775          290 :                 if (WALK(joc->output))
    4776            0 :                     return true;
    4777          290 :                 if (WALK(joc->exprs))
    4778            0 :                     return true;
    4779              :             }
    4780          290 :             break;
    4781          144 :         case T_JsonArrayConstructor:
    4782              :             {
    4783          144 :                 JsonArrayConstructor *jac = (JsonArrayConstructor *) node;
    4784              : 
    4785          144 :                 if (WALK(jac->output))
    4786            0 :                     return true;
    4787          144 :                 if (WALK(jac->exprs))
    4788            0 :                     return true;
    4789              :             }
    4790          144 :             break;
    4791          304 :         case T_JsonAggConstructor:
    4792              :             {
    4793          304 :                 JsonAggConstructor *ctor = (JsonAggConstructor *) node;
    4794              : 
    4795          304 :                 if (WALK(ctor->output))
    4796            0 :                     return true;
    4797          304 :                 if (WALK(ctor->agg_order))
    4798            0 :                     return true;
    4799          304 :                 if (WALK(ctor->agg_filter))
    4800            0 :                     return true;
    4801          304 :                 if (WALK(ctor->over))
    4802            0 :                     return true;
    4803              :             }
    4804          304 :             break;
    4805          136 :         case T_JsonObjectAgg:
    4806              :             {
    4807          136 :                 JsonObjectAgg *joa = (JsonObjectAgg *) node;
    4808              : 
    4809          136 :                 if (WALK(joa->constructor))
    4810            0 :                     return true;
    4811          136 :                 if (WALK(joa->arg))
    4812            0 :                     return true;
    4813              :             }
    4814          136 :             break;
    4815          168 :         case T_JsonArrayAgg:
    4816              :             {
    4817          168 :                 JsonArrayAgg *jaa = (JsonArrayAgg *) node;
    4818              : 
    4819          168 :                 if (WALK(jaa->constructor))
    4820            0 :                     return true;
    4821          168 :                 if (WALK(jaa->arg))
    4822            0 :                     return true;
    4823              :             }
    4824          168 :             break;
    4825           76 :         case T_JsonArrayQueryConstructor:
    4826              :             {
    4827           76 :                 JsonArrayQueryConstructor *jaqc = (JsonArrayQueryConstructor *) node;
    4828              : 
    4829           76 :                 if (WALK(jaqc->output))
    4830            0 :                     return true;
    4831           76 :                 if (WALK(jaqc->query))
    4832            0 :                     return true;
    4833              :             }
    4834           76 :             break;
    4835         1798 :         case T_GraphElementPattern:
    4836              :             {
    4837         1798 :                 GraphElementPattern *gep = (GraphElementPattern *) node;
    4838              : 
    4839         1798 :                 if (WALK(gep->labelexpr))
    4840            0 :                     return true;
    4841         1798 :                 if (WALK(gep->subexpr))
    4842            0 :                     return true;
    4843         1798 :                 if (WALK(gep->whereClause))
    4844            0 :                     return true;
    4845              :             }
    4846         1798 :             break;
    4847          594 :         case T_GraphPattern:
    4848              :             {
    4849          594 :                 GraphPattern *gp = (GraphPattern *) node;
    4850              : 
    4851          594 :                 if (WALK(gp->path_pattern_list))
    4852            0 :                     return true;
    4853          594 :                 if (WALK(gp->whereClause))
    4854            0 :                     return true;
    4855              :             }
    4856          594 :             break;
    4857            0 :         default:
    4858            0 :             elog(ERROR, "unrecognized node type: %d",
    4859              :                  (int) nodeTag(node));
    4860              :             break;
    4861              :     }
    4862      7534716 :     return false;
    4863              : }
    4864              : 
    4865              : /*
    4866              :  * planstate_tree_walker --- walk plan state trees
    4867              :  *
    4868              :  * The walker has already visited the current node, and so we need only
    4869              :  * recurse into any sub-nodes it has.
    4870              :  */
    4871              : bool
    4872       916205 : planstate_tree_walker_impl(PlanState *planstate,
    4873              :                            planstate_tree_walker_callback walker,
    4874              :                            void *context)
    4875              : {
    4876       916205 :     Plan       *plan = planstate->plan;
    4877              :     ListCell   *lc;
    4878              : 
    4879              :     /* We don't need implicit coercions to Node here */
    4880              : #define PSWALK(n) walker(n, context)
    4881              : 
    4882              :     /* Guard against stack overflow due to overly complex plan trees */
    4883       916205 :     check_stack_depth();
    4884              : 
    4885              :     /* initPlan-s */
    4886       916205 :     if (planstate_walk_subplans(planstate->initPlan, walker, context))
    4887            0 :         return true;
    4888              : 
    4889              :     /* lefttree */
    4890       916205 :     if (outerPlanState(planstate))
    4891              :     {
    4892       358999 :         if (PSWALK(outerPlanState(planstate)))
    4893            0 :             return true;
    4894              :     }
    4895              : 
    4896              :     /* righttree */
    4897       916205 :     if (innerPlanState(planstate))
    4898              :     {
    4899       106397 :         if (PSWALK(innerPlanState(planstate)))
    4900            0 :             return true;
    4901              :     }
    4902              : 
    4903              :     /* special child plans */
    4904       916205 :     switch (nodeTag(plan))
    4905              :     {
    4906        13461 :         case T_Append:
    4907        13461 :             if (planstate_walk_members(((AppendState *) planstate)->appendplans,
    4908              :                                        ((AppendState *) planstate)->as_nplans,
    4909              :                                        walker, context))
    4910            0 :                 return true;
    4911        13461 :             break;
    4912          430 :         case T_MergeAppend:
    4913          430 :             if (planstate_walk_members(((MergeAppendState *) planstate)->mergeplans,
    4914              :                                        ((MergeAppendState *) planstate)->ms_nplans,
    4915              :                                        walker, context))
    4916            0 :                 return true;
    4917          430 :             break;
    4918          148 :         case T_BitmapAnd:
    4919          148 :             if (planstate_walk_members(((BitmapAndState *) planstate)->bitmapplans,
    4920              :                                        ((BitmapAndState *) planstate)->nplans,
    4921              :                                        walker, context))
    4922            0 :                 return true;
    4923          148 :             break;
    4924          261 :         case T_BitmapOr:
    4925          261 :             if (planstate_walk_members(((BitmapOrState *) planstate)->bitmapplans,
    4926              :                                        ((BitmapOrState *) planstate)->nplans,
    4927              :                                        walker, context))
    4928            0 :                 return true;
    4929          261 :             break;
    4930        14765 :         case T_SubqueryScan:
    4931        14765 :             if (PSWALK(((SubqueryScanState *) planstate)->subplan))
    4932            0 :                 return true;
    4933        14765 :             break;
    4934            0 :         case T_CustomScan:
    4935            0 :             foreach(lc, ((CustomScanState *) planstate)->custom_ps)
    4936              :             {
    4937            0 :                 if (PSWALK(lfirst(lc)))
    4938            0 :                     return true;
    4939              :             }
    4940            0 :             break;
    4941       887140 :         default:
    4942       887140 :             break;
    4943              :     }
    4944              : 
    4945              :     /* subPlan-s */
    4946       916205 :     if (planstate_walk_subplans(planstate->subPlan, walker, context))
    4947            0 :         return true;
    4948              : 
    4949       916205 :     return false;
    4950              : }
    4951              : 
    4952              : /*
    4953              :  * Walk a list of SubPlans (or initPlans, which also use SubPlan nodes).
    4954              :  */
    4955              : static bool
    4956      1832410 : planstate_walk_subplans(List *plans,
    4957              :                         planstate_tree_walker_callback walker,
    4958              :                         void *context)
    4959              : {
    4960              :     ListCell   *lc;
    4961              : 
    4962      1861921 :     foreach(lc, plans)
    4963              :     {
    4964        29511 :         SubPlanState *sps = lfirst_node(SubPlanState, lc);
    4965              : 
    4966        29511 :         if (PSWALK(sps->planstate))
    4967            0 :             return true;
    4968              :     }
    4969              : 
    4970      1832410 :     return false;
    4971              : }
    4972              : 
    4973              : /*
    4974              :  * Walk the constituent plans of a ModifyTable, Append, MergeAppend,
    4975              :  * BitmapAnd, or BitmapOr node.
    4976              :  */
    4977              : static bool
    4978        14300 : planstate_walk_members(PlanState **planstates, int nplans,
    4979              :                        planstate_tree_walker_callback walker,
    4980              :                        void *context)
    4981              : {
    4982        61795 :     for (int j = 0; j < nplans; j++)
    4983              :     {
    4984        47495 :         if (PSWALK(planstates[j]))
    4985            0 :             return true;
    4986              :     }
    4987              : 
    4988        14300 :     return false;
    4989              : }
        

Generated by: LCOV version 2.0-1