LCOV - code coverage report
Current view: top level - src/backend/nodes - nodeFuncs.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13beta1 Lines: 1759 2179 80.7 %
Date: 2020-05-29 01:06:25 Functions: 31 31 100.0 %
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-2020, 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/makefuncs.h"
      22             : #include "nodes/nodeFuncs.h"
      23             : #include "nodes/pathnodes.h"
      24             : #include "utils/builtins.h"
      25             : #include "utils/lsyscache.h"
      26             : 
      27             : static bool expression_returns_set_walker(Node *node, void *context);
      28             : static int  leftmostLoc(int loc1, int loc2);
      29             : static bool fix_opfuncids_walker(Node *node, void *context);
      30             : static bool planstate_walk_subplans(List *plans, bool (*walker) (),
      31             :                                     void *context);
      32             : static bool planstate_walk_members(PlanState **planstates, int nplans,
      33             :                                    bool (*walker) (), void *context);
      34             : 
      35             : 
      36             : /*
      37             :  *  exprType -
      38             :  *    returns the Oid of the type of the expression's result.
      39             :  */
      40             : Oid
      41    21794026 : exprType(const Node *expr)
      42             : {
      43             :     Oid         type;
      44             : 
      45    21794026 :     if (!expr)
      46           0 :         return InvalidOid;
      47             : 
      48    21794026 :     switch (nodeTag(expr))
      49             :     {
      50     9674740 :         case T_Var:
      51     9674740 :             type = ((const Var *) expr)->vartype;
      52     9674740 :             break;
      53     3381220 :         case T_Const:
      54     3381220 :             type = ((const Const *) expr)->consttype;
      55     3381220 :             break;
      56     1269130 :         case T_Param:
      57     1269130 :             type = ((const Param *) expr)->paramtype;
      58     1269130 :             break;
      59      201638 :         case T_Aggref:
      60      201638 :             type = ((const Aggref *) expr)->aggtype;
      61      201638 :             break;
      62        1192 :         case T_GroupingFunc:
      63        1192 :             type = INT4OID;
      64        1192 :             break;
      65        8452 :         case T_WindowFunc:
      66        8452 :             type = ((const WindowFunc *) expr)->wintype;
      67        8452 :             break;
      68       25318 :         case T_SubscriptingRef:
      69             :             {
      70       25318 :                 const SubscriptingRef *sbsref = (const SubscriptingRef *) expr;
      71             : 
      72             :                 /* slice and/or store operations yield the container type */
      73       25318 :                 if (sbsref->reflowerindexpr || sbsref->refassgnexpr)
      74        2672 :                     type = sbsref->refcontainertype;
      75             :                 else
      76       22646 :                     type = sbsref->refelemtype;
      77             :             }
      78       25318 :             break;
      79     3167654 :         case T_FuncExpr:
      80     3167654 :             type = ((const FuncExpr *) expr)->funcresulttype;
      81     3167654 :             break;
      82        1576 :         case T_NamedArgExpr:
      83        1576 :             type = exprType((Node *) ((const NamedArgExpr *) expr)->arg);
      84        1576 :             break;
      85     1317130 :         case T_OpExpr:
      86     1317130 :             type = ((const OpExpr *) expr)->opresulttype;
      87     1317130 :             break;
      88        1160 :         case T_DistinctExpr:
      89        1160 :             type = ((const DistinctExpr *) expr)->opresulttype;
      90        1160 :             break;
      91        1308 :         case T_NullIfExpr:
      92        1308 :             type = ((const NullIfExpr *) expr)->opresulttype;
      93        1308 :             break;
      94      100180 :         case T_ScalarArrayOpExpr:
      95      100180 :             type = BOOLOID;
      96      100180 :             break;
      97      341850 :         case T_BoolExpr:
      98      341850 :             type = BOOLOID;
      99      341850 :             break;
     100      144338 :         case T_SubLink:
     101             :             {
     102      144338 :                 const SubLink *sublink = (const SubLink *) expr;
     103             : 
     104      144338 :                 if (sublink->subLinkType == EXPR_SUBLINK ||
     105       64950 :                     sublink->subLinkType == ARRAY_SUBLINK)
     106      112456 :                 {
     107             :                     /* get the type of the subselect's first target column */
     108      112456 :                     Query      *qtree = (Query *) sublink->subselect;
     109             :                     TargetEntry *tent;
     110             : 
     111      112456 :                     if (!qtree || !IsA(qtree, Query))
     112           0 :                         elog(ERROR, "cannot get type for untransformed sublink");
     113      112456 :                     tent = linitial_node(TargetEntry, qtree->targetList);
     114             :                     Assert(!tent->resjunk);
     115      112456 :                     type = exprType((Node *) tent->expr);
     116      112456 :                     if (sublink->subLinkType == ARRAY_SUBLINK)
     117             :                     {
     118       33068 :                         type = get_promoted_array_type(type);
     119       33068 :                         if (!OidIsValid(type))
     120           0 :                             ereport(ERROR,
     121             :                                     (errcode(ERRCODE_UNDEFINED_OBJECT),
     122             :                                      errmsg("could not find array type for data type %s",
     123             :                                             format_type_be(exprType((Node *) tent->expr)))));
     124             :                     }
     125             :                 }
     126       31882 :                 else if (sublink->subLinkType == MULTIEXPR_SUBLINK)
     127             :                 {
     128             :                     /* MULTIEXPR is always considered to return RECORD */
     129          62 :                     type = RECORDOID;
     130             :                 }
     131             :                 else
     132             :                 {
     133             :                     /* for all other sublink types, result is boolean */
     134       31820 :                     type = BOOLOID;
     135             :                 }
     136             :             }
     137      144338 :             break;
     138       77592 :         case T_SubPlan:
     139             :             {
     140       77592 :                 const SubPlan *subplan = (const SubPlan *) expr;
     141             : 
     142       77592 :                 if (subplan->subLinkType == EXPR_SUBLINK ||
     143        2608 :                     subplan->subLinkType == ARRAY_SUBLINK)
     144             :                 {
     145             :                     /* get the type of the subselect's first target column */
     146       75196 :                     type = subplan->firstColType;
     147       75408 :                     if (subplan->subLinkType == ARRAY_SUBLINK)
     148             :                     {
     149         212 :                         type = get_promoted_array_type(type);
     150         212 :                         if (!OidIsValid(type))
     151           0 :                             ereport(ERROR,
     152             :                                     (errcode(ERRCODE_UNDEFINED_OBJECT),
     153             :                                      errmsg("could not find array type for data type %s",
     154             :                                             format_type_be(subplan->firstColType))));
     155             :                     }
     156             :                 }
     157        2396 :                 else if (subplan->subLinkType == MULTIEXPR_SUBLINK)
     158             :                 {
     159             :                     /* MULTIEXPR is always considered to return RECORD */
     160         102 :                     type = RECORDOID;
     161             :                 }
     162             :                 else
     163             :                 {
     164             :                     /* for all other subplan types, result is boolean */
     165        2294 :                     type = BOOLOID;
     166             :                 }
     167             :             }
     168       77592 :             break;
     169        1012 :         case T_AlternativeSubPlan:
     170             :             {
     171        1012 :                 const AlternativeSubPlan *asplan = (const AlternativeSubPlan *) expr;
     172             : 
     173             :                 /* subplans should all return the same thing */
     174        1012 :                 type = exprType((Node *) linitial(asplan->subplans));
     175             :             }
     176        1012 :             break;
     177       48184 :         case T_FieldSelect:
     178       48184 :             type = ((const FieldSelect *) expr)->resulttype;
     179       48184 :             break;
     180         600 :         case T_FieldStore:
     181         600 :             type = ((const FieldStore *) expr)->resulttype;
     182         600 :             break;
     183      280006 :         case T_RelabelType:
     184      280006 :             type = ((const RelabelType *) expr)->resulttype;
     185      280006 :             break;
     186       57656 :         case T_CoerceViaIO:
     187       57656 :             type = ((const CoerceViaIO *) expr)->resulttype;
     188       57656 :             break;
     189        4564 :         case T_ArrayCoerceExpr:
     190        4564 :             type = ((const ArrayCoerceExpr *) expr)->resulttype;
     191        4564 :             break;
     192        1796 :         case T_ConvertRowtypeExpr:
     193        1796 :             type = ((const ConvertRowtypeExpr *) expr)->resulttype;
     194        1796 :             break;
     195        3158 :         case T_CollateExpr:
     196        3158 :             type = exprType((Node *) ((const CollateExpr *) expr)->arg);
     197        3158 :             break;
     198      272392 :         case T_CaseExpr:
     199      272392 :             type = ((const CaseExpr *) expr)->casetype;
     200      272392 :             break;
     201       51324 :         case T_CaseTestExpr:
     202       51324 :             type = ((const CaseTestExpr *) expr)->typeId;
     203       51324 :             break;
     204       83736 :         case T_ArrayExpr:
     205       83736 :             type = ((const ArrayExpr *) expr)->array_typeid;
     206       83736 :             break;
     207        8410 :         case T_RowExpr:
     208        8410 :             type = ((const RowExpr *) expr)->row_typeid;
     209        8410 :             break;
     210         220 :         case T_RowCompareExpr:
     211         220 :             type = BOOLOID;
     212         220 :             break;
     213       56874 :         case T_CoalesceExpr:
     214       56874 :             type = ((const CoalesceExpr *) expr)->coalescetype;
     215       56874 :             break;
     216        5180 :         case T_MinMaxExpr:
     217        5180 :             type = ((const MinMaxExpr *) expr)->minmaxtype;
     218        5180 :             break;
     219        7540 :         case T_SQLValueFunction:
     220        7540 :             type = ((const SQLValueFunction *) expr)->type;
     221        7540 :             break;
     222       14870 :         case T_XmlExpr:
     223       14870 :             if (((const XmlExpr *) expr)->op == IS_DOCUMENT)
     224          56 :                 type = BOOLOID;
     225       14814 :             else if (((const XmlExpr *) expr)->op == IS_XMLSERIALIZE)
     226          72 :                 type = TEXTOID;
     227             :             else
     228       14742 :                 type = XMLOID;
     229       14870 :             break;
     230       37540 :         case T_NullTest:
     231       37540 :             type = BOOLOID;
     232       37540 :             break;
     233         428 :         case T_BooleanTest:
     234         428 :             type = BOOLOID;
     235         428 :             break;
     236      984752 :         case T_CoerceToDomain:
     237      984752 :             type = ((const CoerceToDomain *) expr)->resulttype;
     238      984752 :             break;
     239        2296 :         case T_CoerceToDomainValue:
     240        2296 :             type = ((const CoerceToDomainValue *) expr)->typeId;
     241        2296 :             break;
     242      150978 :         case T_SetToDefault:
     243      150978 :             type = ((const SetToDefault *) expr)->typeId;
     244      150978 :             break;
     245         158 :         case T_CurrentOfExpr:
     246         158 :             type = BOOLOID;
     247         158 :             break;
     248         612 :         case T_NextValueExpr:
     249         612 :             type = ((const NextValueExpr *) expr)->typeId;
     250         612 :             break;
     251           0 :         case T_InferenceElem:
     252             :             {
     253           0 :                 const InferenceElem *n = (const InferenceElem *) expr;
     254             : 
     255           0 :                 type = exprType((Node *) n->expr);
     256             :             }
     257           0 :             break;
     258        5262 :         case T_PlaceHolderVar:
     259        5262 :             type = exprType((Node *) ((const PlaceHolderVar *) expr)->phexpr);
     260        5262 :             break;
     261           0 :         default:
     262           0 :             elog(ERROR, "unrecognized node type: %d", (int) nodeTag(expr));
     263             :             type = InvalidOid;  /* keep compiler quiet */
     264             :             break;
     265             :     }
     266    21794026 :     return type;
     267             : }
     268             : 
     269             : /*
     270             :  *  exprTypmod -
     271             :  *    returns the type-specific modifier of the expression's result type,
     272             :  *    if it can be determined.  In many cases, it can't and we return -1.
     273             :  */
     274             : int32
     275     6746712 : exprTypmod(const Node *expr)
     276             : {
     277     6746712 :     if (!expr)
     278           0 :         return -1;
     279             : 
     280     6746712 :     switch (nodeTag(expr))
     281             :     {
     282     3936276 :         case T_Var:
     283     3936276 :             return ((const Var *) expr)->vartypmod;
     284      959968 :         case T_Const:
     285      959968 :             return ((const Const *) expr)->consttypmod;
     286       86748 :         case T_Param:
     287       86748 :             return ((const Param *) expr)->paramtypmod;
     288        3694 :         case T_SubscriptingRef:
     289             :             /* typmod is the same for container or element */
     290        3694 :             return ((const SubscriptingRef *) expr)->reftypmod;
     291      734768 :         case T_FuncExpr:
     292      716680 :             {
     293             :                 int32       coercedTypmod;
     294             : 
     295             :                 /* Be smart about length-coercion functions... */
     296      734768 :                 if (exprIsLengthCoercion(expr, &coercedTypmod))
     297       18088 :                     return coercedTypmod;
     298             :             }
     299      716680 :             break;
     300           0 :         case T_NamedArgExpr:
     301           0 :             return exprTypmod((Node *) ((const NamedArgExpr *) expr)->arg);
     302          68 :         case T_NullIfExpr:
     303             :             {
     304             :                 /*
     305             :                  * Result is either first argument or NULL, so we can report
     306             :                  * first argument's typmod if known.
     307             :                  */
     308          68 :                 const NullIfExpr *nexpr = (const NullIfExpr *) expr;
     309             : 
     310          68 :                 return exprTypmod((Node *) linitial(nexpr->args));
     311             :             }
     312             :             break;
     313       10192 :         case T_SubLink:
     314          48 :             {
     315       10192 :                 const SubLink *sublink = (const SubLink *) expr;
     316             : 
     317       10192 :                 if (sublink->subLinkType == EXPR_SUBLINK ||
     318        1888 :                     sublink->subLinkType == ARRAY_SUBLINK)
     319             :                 {
     320             :                     /* get the typmod of the subselect's first target column */
     321       10144 :                     Query      *qtree = (Query *) sublink->subselect;
     322             :                     TargetEntry *tent;
     323             : 
     324       10144 :                     if (!qtree || !IsA(qtree, Query))
     325           0 :                         elog(ERROR, "cannot get type for untransformed sublink");
     326       10144 :                     tent = linitial_node(TargetEntry, qtree->targetList);
     327             :                     Assert(!tent->resjunk);
     328       10144 :                     return exprTypmod((Node *) tent->expr);
     329             :                     /* note we don't need to care if it's an array */
     330             :                 }
     331             :                 /* otherwise, result is RECORD or BOOLEAN, typmod is -1 */
     332             :             }
     333          48 :             break;
     334       53564 :         case T_SubPlan:
     335        1310 :             {
     336       53564 :                 const SubPlan *subplan = (const SubPlan *) expr;
     337             : 
     338       53564 :                 if (subplan->subLinkType == EXPR_SUBLINK ||
     339        1454 :                     subplan->subLinkType == ARRAY_SUBLINK)
     340             :                 {
     341             :                     /* get the typmod of the subselect's first target column */
     342             :                     /* note we don't need to care if it's an array */
     343       52254 :                     return subplan->firstColTypmod;
     344             :                 }
     345             :                 /* otherwise, result is RECORD or BOOLEAN, typmod is -1 */
     346             :             }
     347        1310 :             break;
     348         508 :         case T_AlternativeSubPlan:
     349             :             {
     350         508 :                 const AlternativeSubPlan *asplan = (const AlternativeSubPlan *) expr;
     351             : 
     352             :                 /* subplans should all return the same thing */
     353         508 :                 return exprTypmod((Node *) linitial(asplan->subplans));
     354             :             }
     355             :             break;
     356       18214 :         case T_FieldSelect:
     357       18214 :             return ((const FieldSelect *) expr)->resulttypmod;
     358       54042 :         case T_RelabelType:
     359       54042 :             return ((const RelabelType *) expr)->resulttypmod;
     360         954 :         case T_ArrayCoerceExpr:
     361         954 :             return ((const ArrayCoerceExpr *) expr)->resulttypmod;
     362          28 :         case T_CollateExpr:
     363          28 :             return exprTypmod((Node *) ((const CollateExpr *) expr)->arg);
     364       90562 :         case T_CaseExpr:
     365             :             {
     366             :                 /*
     367             :                  * If all the alternatives agree on type/typmod, return that
     368             :                  * typmod, else use -1
     369             :                  */
     370       90562 :                 const CaseExpr *cexpr = (const CaseExpr *) expr;
     371       90562 :                 Oid         casetype = cexpr->casetype;
     372             :                 int32       typmod;
     373             :                 ListCell   *arg;
     374             : 
     375       90562 :                 if (!cexpr->defresult)
     376           0 :                     return -1;
     377       90562 :                 if (exprType((Node *) cexpr->defresult) != casetype)
     378           0 :                     return -1;
     379       90562 :                 typmod = exprTypmod((Node *) cexpr->defresult);
     380       90562 :                 if (typmod < 0)
     381       90562 :                     return -1;  /* no point in trying harder */
     382           0 :                 foreach(arg, cexpr->args)
     383             :                 {
     384           0 :                     CaseWhen   *w = lfirst_node(CaseWhen, arg);
     385             : 
     386           0 :                     if (exprType((Node *) w->result) != casetype)
     387           0 :                         return -1;
     388           0 :                     if (exprTypmod((Node *) w->result) != typmod)
     389           0 :                         return -1;
     390             :                 }
     391           0 :                 return typmod;
     392             :             }
     393             :             break;
     394        7836 :         case T_CaseTestExpr:
     395        7836 :             return ((const CaseTestExpr *) expr)->typeMod;
     396       14754 :         case T_ArrayExpr:
     397             :             {
     398             :                 /*
     399             :                  * If all the elements agree on type/typmod, return that
     400             :                  * typmod, else use -1
     401             :                  */
     402       14754 :                 const ArrayExpr *arrayexpr = (const ArrayExpr *) expr;
     403             :                 Oid         commontype;
     404             :                 int32       typmod;
     405             :                 ListCell   *elem;
     406             : 
     407       14754 :                 if (arrayexpr->elements == NIL)
     408          62 :                     return -1;
     409       14692 :                 typmod = exprTypmod((Node *) linitial(arrayexpr->elements));
     410       14692 :                 if (typmod < 0)
     411       14680 :                     return -1;  /* no point in trying harder */
     412          12 :                 if (arrayexpr->multidims)
     413           0 :                     commontype = arrayexpr->array_typeid;
     414             :                 else
     415          12 :                     commontype = arrayexpr->element_typeid;
     416          36 :                 foreach(elem, arrayexpr->elements)
     417             :                 {
     418          24 :                     Node       *e = (Node *) lfirst(elem);
     419             : 
     420          24 :                     if (exprType(e) != commontype)
     421           0 :                         return -1;
     422          24 :                     if (exprTypmod(e) != typmod)
     423           0 :                         return -1;
     424             :                 }
     425          12 :                 return typmod;
     426             :             }
     427             :             break;
     428        3292 :         case T_CoalesceExpr:
     429             :             {
     430             :                 /*
     431             :                  * If all the alternatives agree on type/typmod, return that
     432             :                  * typmod, else use -1
     433             :                  */
     434        3292 :                 const CoalesceExpr *cexpr = (const CoalesceExpr *) expr;
     435        3292 :                 Oid         coalescetype = cexpr->coalescetype;
     436             :                 int32       typmod;
     437             :                 ListCell   *arg;
     438             : 
     439        3292 :                 if (exprType((Node *) linitial(cexpr->args)) != coalescetype)
     440           0 :                     return -1;
     441        3292 :                 typmod = exprTypmod((Node *) linitial(cexpr->args));
     442        3292 :                 if (typmod < 0)
     443        3292 :                     return -1;  /* no point in trying harder */
     444           0 :                 for_each_cell(arg, cexpr->args, list_second_cell(cexpr->args))
     445             :                 {
     446           0 :                     Node       *e = (Node *) lfirst(arg);
     447             : 
     448           0 :                     if (exprType(e) != coalescetype)
     449           0 :                         return -1;
     450           0 :                     if (exprTypmod(e) != typmod)
     451           0 :                         return -1;
     452             :                 }
     453           0 :                 return typmod;
     454             :             }
     455             :             break;
     456        2556 :         case T_MinMaxExpr:
     457             :             {
     458             :                 /*
     459             :                  * If all the alternatives agree on type/typmod, return that
     460             :                  * typmod, else use -1
     461             :                  */
     462        2556 :                 const MinMaxExpr *mexpr = (const MinMaxExpr *) expr;
     463        2556 :                 Oid         minmaxtype = mexpr->minmaxtype;
     464             :                 int32       typmod;
     465             :                 ListCell   *arg;
     466             : 
     467        2556 :                 if (exprType((Node *) linitial(mexpr->args)) != minmaxtype)
     468           0 :                     return -1;
     469        2556 :                 typmod = exprTypmod((Node *) linitial(mexpr->args));
     470        2556 :                 if (typmod < 0)
     471        2556 :                     return -1;  /* no point in trying harder */
     472           0 :                 for_each_cell(arg, mexpr->args, list_second_cell(mexpr->args))
     473             :                 {
     474           0 :                     Node       *e = (Node *) lfirst(arg);
     475             : 
     476           0 :                     if (exprType(e) != minmaxtype)
     477           0 :                         return -1;
     478           0 :                     if (exprTypmod(e) != typmod)
     479           0 :                         return -1;
     480             :                 }
     481           0 :                 return typmod;
     482             :             }
     483             :             break;
     484        1120 :         case T_SQLValueFunction:
     485        1120 :             return ((const SQLValueFunction *) expr)->typmod;
     486      489740 :         case T_CoerceToDomain:
     487      489740 :             return ((const CoerceToDomain *) expr)->resulttypmod;
     488          48 :         case T_CoerceToDomainValue:
     489          48 :             return ((const CoerceToDomainValue *) expr)->typeMod;
     490       50194 :         case T_SetToDefault:
     491       50194 :             return ((const SetToDefault *) expr)->typeMod;
     492        3442 :         case T_PlaceHolderVar:
     493        3442 :             return exprTypmod((Node *) ((const PlaceHolderVar *) expr)->phexpr);
     494      224144 :         default:
     495      224144 :             break;
     496             :     }
     497      942182 :     return -1;
     498             : }
     499             : 
     500             : /*
     501             :  * exprIsLengthCoercion
     502             :  *      Detect whether an expression tree is an application of a datatype's
     503             :  *      typmod-coercion function.  Optionally extract the result's typmod.
     504             :  *
     505             :  * If coercedTypmod is not NULL, the typmod is stored there if the expression
     506             :  * is a length-coercion function, else -1 is stored there.
     507             :  *
     508             :  * Note that a combined type-and-length coercion will be treated as a
     509             :  * length coercion by this routine.
     510             :  */
     511             : bool
     512      735490 : exprIsLengthCoercion(const Node *expr, int32 *coercedTypmod)
     513             : {
     514      735490 :     if (coercedTypmod != NULL)
     515      735490 :         *coercedTypmod = -1;    /* default result on failure */
     516             : 
     517             :     /*
     518             :      * Scalar-type length coercions are FuncExprs, array-type length coercions
     519             :      * are ArrayCoerceExprs
     520             :      */
     521      735490 :     if (expr && IsA(expr, FuncExpr))
     522             :     {
     523      735490 :         const FuncExpr *func = (const FuncExpr *) expr;
     524             :         int         nargs;
     525             :         Const      *second_arg;
     526             : 
     527             :         /*
     528             :          * If it didn't come from a coercion context, reject.
     529             :          */
     530      735490 :         if (func->funcformat != COERCE_EXPLICIT_CAST &&
     531      713916 :             func->funcformat != COERCE_IMPLICIT_CAST)
     532      661838 :             return false;
     533             : 
     534             :         /*
     535             :          * If it's not a two-argument or three-argument function with the
     536             :          * second argument being an int4 constant, it can't have been created
     537             :          * from a length coercion (it must be a type coercion, instead).
     538             :          */
     539       73652 :         nargs = list_length(func->args);
     540       73652 :         if (nargs < 2 || nargs > 3)
     541       55538 :             return false;
     542             : 
     543       18114 :         second_arg = (Const *) lsecond(func->args);
     544       18114 :         if (!IsA(second_arg, Const) ||
     545       18114 :             second_arg->consttype != INT4OID ||
     546       18114 :             second_arg->constisnull)
     547           0 :             return false;
     548             : 
     549             :         /*
     550             :          * OK, it is indeed a length-coercion function.
     551             :          */
     552       18114 :         if (coercedTypmod != NULL)
     553       18114 :             *coercedTypmod = DatumGetInt32(second_arg->constvalue);
     554             : 
     555       18114 :         return true;
     556             :     }
     557             : 
     558           0 :     if (expr && IsA(expr, ArrayCoerceExpr))
     559             :     {
     560           0 :         const ArrayCoerceExpr *acoerce = (const ArrayCoerceExpr *) expr;
     561             : 
     562             :         /* It's not a length coercion unless there's a nondefault typmod */
     563           0 :         if (acoerce->resulttypmod < 0)
     564           0 :             return false;
     565             : 
     566             :         /*
     567             :          * OK, it is indeed a length-coercion expression.
     568             :          */
     569           0 :         if (coercedTypmod != NULL)
     570           0 :             *coercedTypmod = acoerce->resulttypmod;
     571             : 
     572           0 :         return true;
     573             :     }
     574             : 
     575           0 :     return false;
     576             : }
     577             : 
     578             : /*
     579             :  * relabel_to_typmod
     580             :  *      Add a RelabelType node that changes just the typmod of the expression.
     581             :  *
     582             :  * This is primarily intended to be used during planning.  Therefore, it
     583             :  * strips any existing RelabelType nodes to maintain the planner's invariant
     584             :  * that there are not adjacent RelabelTypes.
     585             :  */
     586             : Node *
     587          24 : relabel_to_typmod(Node *expr, int32 typmod)
     588             : {
     589          24 :     Oid         type = exprType(expr);
     590          24 :     Oid         coll = exprCollation(expr);
     591             : 
     592             :     /* Strip any existing RelabelType node(s) */
     593          24 :     while (expr && IsA(expr, RelabelType))
     594           0 :         expr = (Node *) ((RelabelType *) expr)->arg;
     595             : 
     596             :     /* Apply new typmod, preserving the previous exposed type and collation */
     597          24 :     return (Node *) makeRelabelType((Expr *) expr, type, typmod, coll,
     598             :                                     COERCE_EXPLICIT_CAST);
     599             : }
     600             : 
     601             : /*
     602             :  * strip_implicit_coercions: remove implicit coercions at top level of tree
     603             :  *
     604             :  * This doesn't modify or copy the input expression tree, just return a
     605             :  * pointer to a suitable place within it.
     606             :  *
     607             :  * Note: there isn't any useful thing we can do with a RowExpr here, so
     608             :  * just return it unchanged, even if it's marked as an implicit coercion.
     609             :  */
     610             : Node *
     611      476032 : strip_implicit_coercions(Node *node)
     612             : {
     613      476032 :     if (node == NULL)
     614           0 :         return NULL;
     615      476032 :     if (IsA(node, FuncExpr))
     616             :     {
     617       10722 :         FuncExpr   *f = (FuncExpr *) node;
     618             : 
     619       10722 :         if (f->funcformat == COERCE_IMPLICIT_CAST)
     620          24 :             return strip_implicit_coercions(linitial(f->args));
     621             :     }
     622      465310 :     else if (IsA(node, RelabelType))
     623             :     {
     624        5470 :         RelabelType *r = (RelabelType *) node;
     625             : 
     626        5470 :         if (r->relabelformat == COERCE_IMPLICIT_CAST)
     627           8 :             return strip_implicit_coercions((Node *) r->arg);
     628             :     }
     629      459840 :     else if (IsA(node, CoerceViaIO))
     630             :     {
     631         260 :         CoerceViaIO *c = (CoerceViaIO *) node;
     632             : 
     633         260 :         if (c->coerceformat == COERCE_IMPLICIT_CAST)
     634           0 :             return strip_implicit_coercions((Node *) c->arg);
     635             :     }
     636      459580 :     else if (IsA(node, ArrayCoerceExpr))
     637             :     {
     638           0 :         ArrayCoerceExpr *c = (ArrayCoerceExpr *) node;
     639             : 
     640           0 :         if (c->coerceformat == COERCE_IMPLICIT_CAST)
     641           0 :             return strip_implicit_coercions((Node *) c->arg);
     642             :     }
     643      459580 :     else if (IsA(node, ConvertRowtypeExpr))
     644             :     {
     645           0 :         ConvertRowtypeExpr *c = (ConvertRowtypeExpr *) node;
     646             : 
     647           0 :         if (c->convertformat == COERCE_IMPLICIT_CAST)
     648           0 :             return strip_implicit_coercions((Node *) c->arg);
     649             :     }
     650      459580 :     else if (IsA(node, CoerceToDomain))
     651             :     {
     652       33294 :         CoerceToDomain *c = (CoerceToDomain *) node;
     653             : 
     654       33294 :         if (c->coercionformat == COERCE_IMPLICIT_CAST)
     655           0 :             return strip_implicit_coercions((Node *) c->arg);
     656             :     }
     657      476000 :     return node;
     658             : }
     659             : 
     660             : /*
     661             :  * expression_returns_set
     662             :  *    Test whether an expression returns a set result.
     663             :  *
     664             :  * Because we use expression_tree_walker(), this can also be applied to
     665             :  * whole targetlists; it'll produce true if any one of the tlist items
     666             :  * returns a set.
     667             :  */
     668             : bool
     669      844108 : expression_returns_set(Node *clause)
     670             : {
     671      844108 :     return expression_returns_set_walker(clause, NULL);
     672             : }
     673             : 
     674             : static bool
     675     3744218 : expression_returns_set_walker(Node *node, void *context)
     676             : {
     677     3744218 :     if (node == NULL)
     678       39130 :         return false;
     679     3705088 :     if (IsA(node, FuncExpr))
     680             :     {
     681      153990 :         FuncExpr   *expr = (FuncExpr *) node;
     682             : 
     683      153990 :         if (expr->funcretset)
     684        4436 :             return true;
     685             :         /* else fall through to check args */
     686             :     }
     687     3700652 :     if (IsA(node, OpExpr))
     688             :     {
     689      777350 :         OpExpr     *expr = (OpExpr *) node;
     690             : 
     691      777350 :         if (expr->opretset)
     692           4 :             return true;
     693             :         /* else fall through to check args */
     694             :     }
     695             : 
     696             :     /* Avoid recursion for some cases that parser checks not to return a set */
     697     3700648 :     if (IsA(node, Aggref))
     698         704 :         return false;
     699     3699944 :     if (IsA(node, WindowFunc))
     700          20 :         return false;
     701             : 
     702     3699924 :     return expression_tree_walker(node, expression_returns_set_walker,
     703             :                                   context);
     704             : }
     705             : 
     706             : 
     707             : /*
     708             :  *  exprCollation -
     709             :  *    returns the Oid of the collation of the expression's result.
     710             :  *
     711             :  * Note: expression nodes that can invoke functions generally have an
     712             :  * "inputcollid" field, which is what the function should use as collation.
     713             :  * That is the resolved common collation of the node's inputs.  It is often
     714             :  * but not always the same as the result collation; in particular, if the
     715             :  * function produces a non-collatable result type from collatable inputs
     716             :  * or vice versa, the two are different.
     717             :  */
     718             : Oid
     719     9858854 : exprCollation(const Node *expr)
     720             : {
     721             :     Oid         coll;
     722             : 
     723     9858854 :     if (!expr)
     724           0 :         return InvalidOid;
     725             : 
     726     9858854 :     switch (nodeTag(expr))
     727             :     {
     728     6924420 :         case T_Var:
     729     6924420 :             coll = ((const Var *) expr)->varcollid;
     730     6924420 :             break;
     731     1798068 :         case T_Const:
     732     1798068 :             coll = ((const Const *) expr)->constcollid;
     733     1798068 :             break;
     734      150802 :         case T_Param:
     735      150802 :             coll = ((const Param *) expr)->paramcollid;
     736      150802 :             break;
     737       62832 :         case T_Aggref:
     738       62832 :             coll = ((const Aggref *) expr)->aggcollid;
     739       62832 :             break;
     740         396 :         case T_GroupingFunc:
     741         396 :             coll = InvalidOid;
     742         396 :             break;
     743        1940 :         case T_WindowFunc:
     744        1940 :             coll = ((const WindowFunc *) expr)->wincollid;
     745        1940 :             break;
     746        2164 :         case T_SubscriptingRef:
     747        2164 :             coll = ((const SubscriptingRef *) expr)->refcollid;
     748        2164 :             break;
     749      316732 :         case T_FuncExpr:
     750      316732 :             coll = ((const FuncExpr *) expr)->funccollid;
     751      316732 :             break;
     752           0 :         case T_NamedArgExpr:
     753           0 :             coll = exprCollation((Node *) ((const NamedArgExpr *) expr)->arg);
     754           0 :             break;
     755       75032 :         case T_OpExpr:
     756       75032 :             coll = ((const OpExpr *) expr)->opcollid;
     757       75032 :             break;
     758          82 :         case T_DistinctExpr:
     759          82 :             coll = ((const DistinctExpr *) expr)->opcollid;
     760          82 :             break;
     761          68 :         case T_NullIfExpr:
     762          68 :             coll = ((const NullIfExpr *) expr)->opcollid;
     763          68 :             break;
     764        7828 :         case T_ScalarArrayOpExpr:
     765        7828 :             coll = InvalidOid;  /* result is always boolean */
     766        7828 :             break;
     767        1828 :         case T_BoolExpr:
     768        1828 :             coll = InvalidOid;  /* result is always boolean */
     769        1828 :             break;
     770        8708 :         case T_SubLink:
     771             :             {
     772        8708 :                 const SubLink *sublink = (const SubLink *) expr;
     773             : 
     774        8708 :                 if (sublink->subLinkType == EXPR_SUBLINK ||
     775         788 :                     sublink->subLinkType == ARRAY_SUBLINK)
     776        8668 :                 {
     777             :                     /* get the collation of subselect's first target column */
     778        8668 :                     Query      *qtree = (Query *) sublink->subselect;
     779             :                     TargetEntry *tent;
     780             : 
     781        8668 :                     if (!qtree || !IsA(qtree, Query))
     782           0 :                         elog(ERROR, "cannot get collation for untransformed sublink");
     783        8668 :                     tent = linitial_node(TargetEntry, qtree->targetList);
     784             :                     Assert(!tent->resjunk);
     785        8668 :                     coll = exprCollation((Node *) tent->expr);
     786             :                     /* collation doesn't change if it's converted to array */
     787             :                 }
     788             :                 else
     789             :                 {
     790             :                     /* otherwise, result is RECORD or BOOLEAN */
     791          40 :                     coll = InvalidOid;
     792             :                 }
     793             :             }
     794        8708 :             break;
     795       30128 :         case T_SubPlan:
     796             :             {
     797       30128 :                 const SubPlan *subplan = (const SubPlan *) expr;
     798             : 
     799       30128 :                 if (subplan->subLinkType == EXPR_SUBLINK ||
     800         316 :                     subplan->subLinkType == ARRAY_SUBLINK)
     801             :                 {
     802             :                     /* get the collation of subselect's first target column */
     803       29868 :                     coll = subplan->firstColCollation;
     804             :                     /* collation doesn't change if it's converted to array */
     805             :                 }
     806             :                 else
     807             :                 {
     808             :                     /* otherwise, result is RECORD or BOOLEAN */
     809         260 :                     coll = InvalidOid;
     810             :                 }
     811             :             }
     812       30128 :             break;
     813           4 :         case T_AlternativeSubPlan:
     814             :             {
     815           4 :                 const AlternativeSubPlan *asplan = (const AlternativeSubPlan *) expr;
     816             : 
     817             :                 /* subplans should all return the same thing */
     818           4 :                 coll = exprCollation((Node *) linitial(asplan->subplans));
     819             :             }
     820           4 :             break;
     821       15956 :         case T_FieldSelect:
     822       15956 :             coll = ((const FieldSelect *) expr)->resultcollid;
     823       15956 :             break;
     824          36 :         case T_FieldStore:
     825          36 :             coll = InvalidOid;  /* result is always composite */
     826          36 :             break;
     827       36946 :         case T_RelabelType:
     828       36946 :             coll = ((const RelabelType *) expr)->resultcollid;
     829       36946 :             break;
     830       10834 :         case T_CoerceViaIO:
     831       10834 :             coll = ((const CoerceViaIO *) expr)->resultcollid;
     832       10834 :             break;
     833         930 :         case T_ArrayCoerceExpr:
     834         930 :             coll = ((const ArrayCoerceExpr *) expr)->resultcollid;
     835         930 :             break;
     836         366 :         case T_ConvertRowtypeExpr:
     837         366 :             coll = InvalidOid;  /* result is always composite */
     838         366 :             break;
     839          52 :         case T_CollateExpr:
     840          52 :             coll = ((const CollateExpr *) expr)->collOid;
     841          52 :             break;
     842       45762 :         case T_CaseExpr:
     843       45762 :             coll = ((const CaseExpr *) expr)->casecollid;
     844       45762 :             break;
     845       43336 :         case T_CaseTestExpr:
     846       43336 :             coll = ((const CaseTestExpr *) expr)->collation;
     847       43336 :             break;
     848       13754 :         case T_ArrayExpr:
     849       13754 :             coll = ((const ArrayExpr *) expr)->array_collid;
     850       13754 :             break;
     851        2376 :         case T_RowExpr:
     852        2376 :             coll = InvalidOid;  /* result is always composite */
     853        2376 :             break;
     854          40 :         case T_RowCompareExpr:
     855          40 :             coll = InvalidOid;  /* result is always boolean */
     856          40 :             break;
     857        1832 :         case T_CoalesceExpr:
     858        1832 :             coll = ((const CoalesceExpr *) expr)->coalescecollid;
     859        1832 :             break;
     860        2482 :         case T_MinMaxExpr:
     861        2482 :             coll = ((const MinMaxExpr *) expr)->minmaxcollid;
     862        2482 :             break;
     863         570 :         case T_SQLValueFunction:
     864             :             /* Returns either NAME or a non-collatable type */
     865         570 :             if (((const SQLValueFunction *) expr)->type == NAMEOID)
     866         542 :                 coll = C_COLLATION_OID;
     867             :             else
     868          28 :                 coll = InvalidOid;
     869         570 :             break;
     870         350 :         case T_XmlExpr:
     871             : 
     872             :             /*
     873             :              * XMLSERIALIZE returns text from non-collatable inputs, so its
     874             :              * collation is always default.  The other cases return boolean or
     875             :              * XML, which are non-collatable.
     876             :              */
     877         350 :             if (((const XmlExpr *) expr)->op == IS_XMLSERIALIZE)
     878          10 :                 coll = DEFAULT_COLLATION_OID;
     879             :             else
     880         340 :                 coll = InvalidOid;
     881         350 :             break;
     882        1122 :         case T_NullTest:
     883        1122 :             coll = InvalidOid;  /* result is always boolean */
     884        1122 :             break;
     885          84 :         case T_BooleanTest:
     886          84 :             coll = InvalidOid;  /* result is always boolean */
     887          84 :             break;
     888      246672 :         case T_CoerceToDomain:
     889      246672 :             coll = ((const CoerceToDomain *) expr)->resultcollid;
     890      246672 :             break;
     891        1054 :         case T_CoerceToDomainValue:
     892        1054 :             coll = ((const CoerceToDomainValue *) expr)->collation;
     893        1054 :             break;
     894       50738 :         case T_SetToDefault:
     895       50738 :             coll = ((const SetToDefault *) expr)->collation;
     896       50738 :             break;
     897         158 :         case T_CurrentOfExpr:
     898         158 :             coll = InvalidOid;  /* result is always boolean */
     899         158 :             break;
     900         162 :         case T_NextValueExpr:
     901         162 :             coll = InvalidOid;  /* result is always an integer type */
     902         162 :             break;
     903           0 :         case T_InferenceElem:
     904           0 :             coll = exprCollation((Node *) ((const InferenceElem *) expr)->expr);
     905           0 :             break;
     906        2210 :         case T_PlaceHolderVar:
     907        2210 :             coll = exprCollation((Node *) ((const PlaceHolderVar *) expr)->phexpr);
     908        2210 :             break;
     909           0 :         default:
     910           0 :             elog(ERROR, "unrecognized node type: %d", (int) nodeTag(expr));
     911             :             coll = InvalidOid;  /* keep compiler quiet */
     912             :             break;
     913             :     }
     914     9858854 :     return coll;
     915             : }
     916             : 
     917             : /*
     918             :  *  exprInputCollation -
     919             :  *    returns the Oid of the collation a function should use, if available.
     920             :  *
     921             :  * Result is InvalidOid if the node type doesn't store this information.
     922             :  */
     923             : Oid
     924          44 : exprInputCollation(const Node *expr)
     925             : {
     926             :     Oid         coll;
     927             : 
     928          44 :     if (!expr)
     929           0 :         return InvalidOid;
     930             : 
     931          44 :     switch (nodeTag(expr))
     932             :     {
     933           0 :         case T_Aggref:
     934           0 :             coll = ((const Aggref *) expr)->inputcollid;
     935           0 :             break;
     936           0 :         case T_WindowFunc:
     937           0 :             coll = ((const WindowFunc *) expr)->inputcollid;
     938           0 :             break;
     939          44 :         case T_FuncExpr:
     940          44 :             coll = ((const FuncExpr *) expr)->inputcollid;
     941          44 :             break;
     942           0 :         case T_OpExpr:
     943           0 :             coll = ((const OpExpr *) expr)->inputcollid;
     944           0 :             break;
     945           0 :         case T_DistinctExpr:
     946           0 :             coll = ((const DistinctExpr *) expr)->inputcollid;
     947           0 :             break;
     948           0 :         case T_NullIfExpr:
     949           0 :             coll = ((const NullIfExpr *) expr)->inputcollid;
     950           0 :             break;
     951           0 :         case T_ScalarArrayOpExpr:
     952           0 :             coll = ((const ScalarArrayOpExpr *) expr)->inputcollid;
     953           0 :             break;
     954           0 :         case T_MinMaxExpr:
     955           0 :             coll = ((const MinMaxExpr *) expr)->inputcollid;
     956           0 :             break;
     957           0 :         default:
     958           0 :             coll = InvalidOid;
     959           0 :             break;
     960             :     }
     961          44 :     return coll;
     962             : }
     963             : 
     964             : /*
     965             :  *  exprSetCollation -
     966             :  *    Assign collation information to an expression tree node.
     967             :  *
     968             :  * Note: since this is only used during parse analysis, we don't need to
     969             :  * worry about subplans or PlaceHolderVars.
     970             :  */
     971             : void
     972     2107446 : exprSetCollation(Node *expr, Oid collation)
     973             : {
     974     2107446 :     switch (nodeTag(expr))
     975             :     {
     976           0 :         case T_Var:
     977           0 :             ((Var *) expr)->varcollid = collation;
     978           0 :             break;
     979           0 :         case T_Const:
     980           0 :             ((Const *) expr)->constcollid = collation;
     981           0 :             break;
     982           0 :         case T_Param:
     983           0 :             ((Param *) expr)->paramcollid = collation;
     984           0 :             break;
     985       31034 :         case T_Aggref:
     986       31034 :             ((Aggref *) expr)->aggcollid = collation;
     987       31034 :             break;
     988         176 :         case T_GroupingFunc:
     989             :             Assert(!OidIsValid(collation));
     990         176 :             break;
     991        1786 :         case T_WindowFunc:
     992        1786 :             ((WindowFunc *) expr)->wincollid = collation;
     993        1786 :             break;
     994        7584 :         case T_SubscriptingRef:
     995        7584 :             ((SubscriptingRef *) expr)->refcollid = collation;
     996        7584 :             break;
     997      558852 :         case T_FuncExpr:
     998      558852 :             ((FuncExpr *) expr)->funccollid = collation;
     999      558852 :             break;
    1000         776 :         case T_NamedArgExpr:
    1001             :             Assert(collation == exprCollation((Node *) ((NamedArgExpr *) expr)->arg));
    1002         776 :             break;
    1003      587998 :         case T_OpExpr:
    1004      587998 :             ((OpExpr *) expr)->opcollid = collation;
    1005      587998 :             break;
    1006         524 :         case T_DistinctExpr:
    1007         524 :             ((DistinctExpr *) expr)->opcollid = collation;
    1008         524 :             break;
    1009         522 :         case T_NullIfExpr:
    1010         522 :             ((NullIfExpr *) expr)->opcollid = collation;
    1011         522 :             break;
    1012       43488 :         case T_ScalarArrayOpExpr:
    1013             :             Assert(!OidIsValid(collation)); /* result is always boolean */
    1014       43488 :             break;
    1015      169268 :         case T_BoolExpr:
    1016             :             Assert(!OidIsValid(collation)); /* result is always boolean */
    1017      169268 :             break;
    1018       66882 :         case T_SubLink:
    1019             : #ifdef USE_ASSERT_CHECKING
    1020             :             {
    1021             :                 SubLink    *sublink = (SubLink *) expr;
    1022             : 
    1023             :                 if (sublink->subLinkType == EXPR_SUBLINK ||
    1024             :                     sublink->subLinkType == ARRAY_SUBLINK)
    1025             :                 {
    1026             :                     /* get the collation of subselect's first target column */
    1027             :                     Query      *qtree = (Query *) sublink->subselect;
    1028             :                     TargetEntry *tent;
    1029             : 
    1030             :                     if (!qtree || !IsA(qtree, Query))
    1031             :                         elog(ERROR, "cannot set collation for untransformed sublink");
    1032             :                     tent = linitial_node(TargetEntry, qtree->targetList);
    1033             :                     Assert(!tent->resjunk);
    1034             :                     Assert(collation == exprCollation((Node *) tent->expr));
    1035             :                 }
    1036             :                 else
    1037             :                 {
    1038             :                     /* otherwise, result is RECORD or BOOLEAN */
    1039             :                     Assert(!OidIsValid(collation));
    1040             :                 }
    1041             :             }
    1042             : #endif                          /* USE_ASSERT_CHECKING */
    1043       66882 :             break;
    1044           0 :         case T_FieldSelect:
    1045           0 :             ((FieldSelect *) expr)->resultcollid = collation;
    1046           0 :             break;
    1047         236 :         case T_FieldStore:
    1048             :             Assert(!OidIsValid(collation)); /* result is always composite */
    1049         236 :             break;
    1050      131282 :         case T_RelabelType:
    1051      131282 :             ((RelabelType *) expr)->resultcollid = collation;
    1052      131282 :             break;
    1053       17506 :         case T_CoerceViaIO:
    1054       17506 :             ((CoerceViaIO *) expr)->resultcollid = collation;
    1055       17506 :             break;
    1056        1938 :         case T_ArrayCoerceExpr:
    1057        1938 :             ((ArrayCoerceExpr *) expr)->resultcollid = collation;
    1058        1938 :             break;
    1059          40 :         case T_ConvertRowtypeExpr:
    1060             :             Assert(!OidIsValid(collation)); /* result is always composite */
    1061          40 :             break;
    1062       80058 :         case T_CaseExpr:
    1063       80058 :             ((CaseExpr *) expr)->casecollid = collation;
    1064       80058 :             break;
    1065       35136 :         case T_ArrayExpr:
    1066       35136 :             ((ArrayExpr *) expr)->array_collid = collation;
    1067       35136 :             break;
    1068           0 :         case T_RowExpr:
    1069             :             Assert(!OidIsValid(collation)); /* result is always composite */
    1070           0 :             break;
    1071           0 :         case T_RowCompareExpr:
    1072             :             Assert(!OidIsValid(collation)); /* result is always boolean */
    1073           0 :             break;
    1074       28144 :         case T_CoalesceExpr:
    1075       28144 :             ((CoalesceExpr *) expr)->coalescecollid = collation;
    1076       28144 :             break;
    1077         176 :         case T_MinMaxExpr:
    1078         176 :             ((MinMaxExpr *) expr)->minmaxcollid = collation;
    1079         176 :             break;
    1080        2492 :         case T_SQLValueFunction:
    1081             :             Assert((((SQLValueFunction *) expr)->type == NAMEOID) ?
    1082             :                    (collation == C_COLLATION_OID) :
    1083             :                    (collation == InvalidOid));
    1084        2492 :             break;
    1085         402 :         case T_XmlExpr:
    1086             :             Assert((((XmlExpr *) expr)->op == IS_XMLSERIALIZE) ?
    1087             :                    (collation == DEFAULT_COLLATION_OID) :
    1088             :                    (collation == InvalidOid));
    1089         402 :             break;
    1090       16954 :         case T_NullTest:
    1091             :             Assert(!OidIsValid(collation)); /* result is always boolean */
    1092       16954 :             break;
    1093         130 :         case T_BooleanTest:
    1094             :             Assert(!OidIsValid(collation)); /* result is always boolean */
    1095         130 :             break;
    1096      324062 :         case T_CoerceToDomain:
    1097      324062 :             ((CoerceToDomain *) expr)->resultcollid = collation;
    1098      324062 :             break;
    1099           0 :         case T_CoerceToDomainValue:
    1100           0 :             ((CoerceToDomainValue *) expr)->collation = collation;
    1101           0 :             break;
    1102           0 :         case T_SetToDefault:
    1103           0 :             ((SetToDefault *) expr)->collation = collation;
    1104           0 :             break;
    1105           0 :         case T_CurrentOfExpr:
    1106             :             Assert(!OidIsValid(collation)); /* result is always boolean */
    1107           0 :             break;
    1108           0 :         case T_NextValueExpr:
    1109             :             Assert(!OidIsValid(collation)); /* result is always an integer
    1110             :                                              * type */
    1111           0 :             break;
    1112           0 :         default:
    1113           0 :             elog(ERROR, "unrecognized node type: %d", (int) nodeTag(expr));
    1114             :             break;
    1115             :     }
    1116     2107446 : }
    1117             : 
    1118             : /*
    1119             :  *  exprSetInputCollation -
    1120             :  *    Assign input-collation information to an expression tree node.
    1121             :  *
    1122             :  * This is a no-op for node types that don't store their input collation.
    1123             :  * Note we omit RowCompareExpr, which needs special treatment since it
    1124             :  * contains multiple input collation OIDs.
    1125             :  */
    1126             : void
    1127     1783384 : exprSetInputCollation(Node *expr, Oid inputcollation)
    1128             : {
    1129     1783384 :     switch (nodeTag(expr))
    1130             :     {
    1131       31034 :         case T_Aggref:
    1132       31034 :             ((Aggref *) expr)->inputcollid = inputcollation;
    1133       31034 :             break;
    1134        1786 :         case T_WindowFunc:
    1135        1786 :             ((WindowFunc *) expr)->inputcollid = inputcollation;
    1136        1786 :             break;
    1137      558852 :         case T_FuncExpr:
    1138      558852 :             ((FuncExpr *) expr)->inputcollid = inputcollation;
    1139      558852 :             break;
    1140      587998 :         case T_OpExpr:
    1141      587998 :             ((OpExpr *) expr)->inputcollid = inputcollation;
    1142      587998 :             break;
    1143         524 :         case T_DistinctExpr:
    1144         524 :             ((DistinctExpr *) expr)->inputcollid = inputcollation;
    1145         524 :             break;
    1146         522 :         case T_NullIfExpr:
    1147         522 :             ((NullIfExpr *) expr)->inputcollid = inputcollation;
    1148         522 :             break;
    1149       43488 :         case T_ScalarArrayOpExpr:
    1150       43488 :             ((ScalarArrayOpExpr *) expr)->inputcollid = inputcollation;
    1151       43488 :             break;
    1152         176 :         case T_MinMaxExpr:
    1153         176 :             ((MinMaxExpr *) expr)->inputcollid = inputcollation;
    1154         176 :             break;
    1155      559004 :         default:
    1156      559004 :             break;
    1157             :     }
    1158     1783384 : }
    1159             : 
    1160             : 
    1161             : /*
    1162             :  *  exprLocation -
    1163             :  *    returns the parse location of an expression tree, for error reports
    1164             :  *
    1165             :  * -1 is returned if the location can't be determined.
    1166             :  *
    1167             :  * For expressions larger than a single token, the intent here is to
    1168             :  * return the location of the expression's leftmost token, not necessarily
    1169             :  * the topmost Node's location field.  For example, an OpExpr's location
    1170             :  * field will point at the operator name, but if it is not a prefix operator
    1171             :  * then we should return the location of the left-hand operand instead.
    1172             :  * The reason is that we want to reference the entire expression not just
    1173             :  * that operator, and pointing to its start seems to be the most natural way.
    1174             :  *
    1175             :  * The location is not perfect --- for example, since the grammar doesn't
    1176             :  * explicitly represent parentheses in the parsetree, given something that
    1177             :  * had been written "(a + b) * c" we are going to point at "a" not "(".
    1178             :  * But it should be plenty good enough for error reporting purposes.
    1179             :  *
    1180             :  * You might think that this code is overly general, for instance why check
    1181             :  * the operands of a FuncExpr node, when the function name can be expected
    1182             :  * to be to the left of them?  There are a couple of reasons.  The grammar
    1183             :  * sometimes builds expressions that aren't quite what the user wrote;
    1184             :  * for instance x IS NOT BETWEEN ... becomes a NOT-expression whose keyword
    1185             :  * pointer is to the right of its leftmost argument.  Also, nodes that were
    1186             :  * inserted implicitly by parse analysis (such as FuncExprs for implicit
    1187             :  * coercions) will have location -1, and so we can have odd combinations of
    1188             :  * known and unknown locations in a tree.
    1189             :  */
    1190             : int
    1191     5154302 : exprLocation(const Node *expr)
    1192             : {
    1193             :     int         loc;
    1194             : 
    1195     5154302 :     if (expr == NULL)
    1196       84824 :         return -1;
    1197     5069478 :     switch (nodeTag(expr))
    1198             :     {
    1199          20 :         case T_RangeVar:
    1200          20 :             loc = ((const RangeVar *) expr)->location;
    1201          20 :             break;
    1202           0 :         case T_TableFunc:
    1203           0 :             loc = ((const TableFunc *) expr)->location;
    1204           0 :             break;
    1205     2526662 :         case T_Var:
    1206     2526662 :             loc = ((const Var *) expr)->location;
    1207     2526662 :             break;
    1208     1418264 :         case T_Const:
    1209     1418264 :             loc = ((const Const *) expr)->location;
    1210     1418264 :             break;
    1211       94110 :         case T_Param:
    1212       94110 :             loc = ((const Param *) expr)->location;
    1213       94110 :             break;
    1214       11500 :         case T_Aggref:
    1215             :             /* function name should always be the first thing */
    1216       11500 :             loc = ((const Aggref *) expr)->location;
    1217       11500 :             break;
    1218          20 :         case T_GroupingFunc:
    1219          20 :             loc = ((const GroupingFunc *) expr)->location;
    1220          20 :             break;
    1221           8 :         case T_WindowFunc:
    1222             :             /* function name should always be the first thing */
    1223           8 :             loc = ((const WindowFunc *) expr)->location;
    1224           8 :             break;
    1225         496 :         case T_SubscriptingRef:
    1226             :             /* just use container argument's location */
    1227         496 :             loc = exprLocation((Node *) ((const SubscriptingRef *) expr)->refexpr);
    1228         496 :             break;
    1229      209928 :         case T_FuncExpr:
    1230             :             {
    1231      209928 :                 const FuncExpr *fexpr = (const FuncExpr *) expr;
    1232             : 
    1233             :                 /* consider both function name and leftmost arg */
    1234      209928 :                 loc = leftmostLoc(fexpr->location,
    1235      209928 :                                   exprLocation((Node *) fexpr->args));
    1236             :             }
    1237      209928 :             break;
    1238           4 :         case T_NamedArgExpr:
    1239             :             {
    1240           4 :                 const NamedArgExpr *na = (const NamedArgExpr *) expr;
    1241             : 
    1242             :                 /* consider both argument name and value */
    1243           4 :                 loc = leftmostLoc(na->location,
    1244           4 :                                   exprLocation((Node *) na->arg));
    1245             :             }
    1246           4 :             break;
    1247       25958 :         case T_OpExpr:
    1248             :         case T_DistinctExpr:    /* struct-equivalent to OpExpr */
    1249             :         case T_NullIfExpr:      /* struct-equivalent to OpExpr */
    1250             :             {
    1251       25958 :                 const OpExpr *opexpr = (const OpExpr *) expr;
    1252             : 
    1253             :                 /* consider both operator name and leftmost arg */
    1254       25958 :                 loc = leftmostLoc(opexpr->location,
    1255       25958 :                                   exprLocation((Node *) opexpr->args));
    1256             :             }
    1257       25958 :             break;
    1258           0 :         case T_ScalarArrayOpExpr:
    1259             :             {
    1260           0 :                 const ScalarArrayOpExpr *saopexpr = (const ScalarArrayOpExpr *) expr;
    1261             : 
    1262             :                 /* consider both operator name and leftmost arg */
    1263           0 :                 loc = leftmostLoc(saopexpr->location,
    1264           0 :                                   exprLocation((Node *) saopexpr->args));
    1265             :             }
    1266           0 :             break;
    1267           0 :         case T_BoolExpr:
    1268             :             {
    1269           0 :                 const BoolExpr *bexpr = (const BoolExpr *) expr;
    1270             : 
    1271             :                 /*
    1272             :                  * Same as above, to handle either NOT or AND/OR.  We can't
    1273             :                  * special-case NOT because of the way that it's used for
    1274             :                  * things like IS NOT BETWEEN.
    1275             :                  */
    1276           0 :                 loc = leftmostLoc(bexpr->location,
    1277           0 :                                   exprLocation((Node *) bexpr->args));
    1278             :             }
    1279           0 :             break;
    1280        4022 :         case T_SubLink:
    1281             :             {
    1282        4022 :                 const SubLink *sublink = (const SubLink *) expr;
    1283             : 
    1284             :                 /* check the testexpr, if any, and the operator/keyword */
    1285        4022 :                 loc = leftmostLoc(exprLocation(sublink->testexpr),
    1286             :                                   sublink->location);
    1287             :             }
    1288        4022 :             break;
    1289       10292 :         case T_FieldSelect:
    1290             :             /* just use argument's location */
    1291       10292 :             loc = exprLocation((Node *) ((const FieldSelect *) expr)->arg);
    1292       10292 :             break;
    1293           0 :         case T_FieldStore:
    1294             :             /* just use argument's location */
    1295           0 :             loc = exprLocation((Node *) ((const FieldStore *) expr)->arg);
    1296           0 :             break;
    1297        7580 :         case T_RelabelType:
    1298             :             {
    1299        7580 :                 const RelabelType *rexpr = (const RelabelType *) expr;
    1300             : 
    1301             :                 /* Much as above */
    1302        7580 :                 loc = leftmostLoc(rexpr->location,
    1303        7580 :                                   exprLocation((Node *) rexpr->arg));
    1304             :             }
    1305        7580 :             break;
    1306       23808 :         case T_CoerceViaIO:
    1307             :             {
    1308       23808 :                 const CoerceViaIO *cexpr = (const CoerceViaIO *) expr;
    1309             : 
    1310             :                 /* Much as above */
    1311       23808 :                 loc = leftmostLoc(cexpr->location,
    1312       23808 :                                   exprLocation((Node *) cexpr->arg));
    1313             :             }
    1314       23808 :             break;
    1315           0 :         case T_ArrayCoerceExpr:
    1316             :             {
    1317           0 :                 const ArrayCoerceExpr *cexpr = (const ArrayCoerceExpr *) expr;
    1318             : 
    1319             :                 /* Much as above */
    1320           0 :                 loc = leftmostLoc(cexpr->location,
    1321           0 :                                   exprLocation((Node *) cexpr->arg));
    1322             :             }
    1323           0 :             break;
    1324           4 :         case T_ConvertRowtypeExpr:
    1325             :             {
    1326           4 :                 const ConvertRowtypeExpr *cexpr = (const ConvertRowtypeExpr *) expr;
    1327             : 
    1328             :                 /* Much as above */
    1329           4 :                 loc = leftmostLoc(cexpr->location,
    1330           4 :                                   exprLocation((Node *) cexpr->arg));
    1331             :             }
    1332           4 :             break;
    1333          20 :         case T_CollateExpr:
    1334             :             /* just use argument's location */
    1335          20 :             loc = exprLocation((Node *) ((const CollateExpr *) expr)->arg);
    1336          20 :             break;
    1337       32296 :         case T_CaseExpr:
    1338             :             /* CASE keyword should always be the first thing */
    1339       32296 :             loc = ((const CaseExpr *) expr)->location;
    1340       32296 :             break;
    1341           0 :         case T_CaseWhen:
    1342             :             /* WHEN keyword should always be the first thing */
    1343           0 :             loc = ((const CaseWhen *) expr)->location;
    1344           0 :             break;
    1345         240 :         case T_ArrayExpr:
    1346             :             /* the location points at ARRAY or [, which must be leftmost */
    1347         240 :             loc = ((const ArrayExpr *) expr)->location;
    1348         240 :             break;
    1349         136 :         case T_RowExpr:
    1350             :             /* the location points at ROW or (, which must be leftmost */
    1351         136 :             loc = ((const RowExpr *) expr)->location;
    1352         136 :             break;
    1353           0 :         case T_RowCompareExpr:
    1354             :             /* just use leftmost argument's location */
    1355           0 :             loc = exprLocation((Node *) ((const RowCompareExpr *) expr)->largs);
    1356           0 :             break;
    1357        8266 :         case T_CoalesceExpr:
    1358             :             /* COALESCE keyword should always be the first thing */
    1359        8266 :             loc = ((const CoalesceExpr *) expr)->location;
    1360        8266 :             break;
    1361          12 :         case T_MinMaxExpr:
    1362             :             /* GREATEST/LEAST keyword should always be the first thing */
    1363          12 :             loc = ((const MinMaxExpr *) expr)->location;
    1364          12 :             break;
    1365        1872 :         case T_SQLValueFunction:
    1366             :             /* function keyword should always be the first thing */
    1367        1872 :             loc = ((const SQLValueFunction *) expr)->location;
    1368        1872 :             break;
    1369          24 :         case T_XmlExpr:
    1370             :             {
    1371          24 :                 const XmlExpr *xexpr = (const XmlExpr *) expr;
    1372             : 
    1373             :                 /* consider both function name and leftmost arg */
    1374          24 :                 loc = leftmostLoc(xexpr->location,
    1375          24 :                                   exprLocation((Node *) xexpr->args));
    1376             :             }
    1377          24 :             break;
    1378         764 :         case T_NullTest:
    1379             :             {
    1380         764 :                 const NullTest *nexpr = (const NullTest *) expr;
    1381             : 
    1382             :                 /* Much as above */
    1383         764 :                 loc = leftmostLoc(nexpr->location,
    1384         764 :                                   exprLocation((Node *) nexpr->arg));
    1385             :             }
    1386         764 :             break;
    1387           0 :         case T_BooleanTest:
    1388             :             {
    1389           0 :                 const BooleanTest *bexpr = (const BooleanTest *) expr;
    1390             : 
    1391             :                 /* Much as above */
    1392           0 :                 loc = leftmostLoc(bexpr->location,
    1393           0 :                                   exprLocation((Node *) bexpr->arg));
    1394             :             }
    1395           0 :             break;
    1396      302888 :         case T_CoerceToDomain:
    1397             :             {
    1398      302888 :                 const CoerceToDomain *cexpr = (const CoerceToDomain *) expr;
    1399             : 
    1400             :                 /* Much as above */
    1401      302888 :                 loc = leftmostLoc(cexpr->location,
    1402      302888 :                                   exprLocation((Node *) cexpr->arg));
    1403             :             }
    1404      302888 :             break;
    1405        1176 :         case T_CoerceToDomainValue:
    1406        1176 :             loc = ((const CoerceToDomainValue *) expr)->location;
    1407        1176 :             break;
    1408      100684 :         case T_SetToDefault:
    1409      100684 :             loc = ((const SetToDefault *) expr)->location;
    1410      100684 :             break;
    1411           0 :         case T_TargetEntry:
    1412             :             /* just use argument's location */
    1413           0 :             loc = exprLocation((Node *) ((const TargetEntry *) expr)->expr);
    1414           0 :             break;
    1415          12 :         case T_IntoClause:
    1416             :             /* use the contained RangeVar's location --- close enough */
    1417          12 :             loc = exprLocation((Node *) ((const IntoClause *) expr)->rel);
    1418          12 :             break;
    1419      157684 :         case T_List:
    1420      157684 :             {
    1421             :                 /* report location of first list member that has a location */
    1422             :                 ListCell   *lc;
    1423             : 
    1424      157684 :                 loc = -1;       /* just to suppress compiler warning */
    1425      158228 :                 foreach(lc, (const List *) expr)
    1426             :                 {
    1427      157796 :                     loc = exprLocation((Node *) lfirst(lc));
    1428      157796 :                     if (loc >= 0)
    1429      157252 :                         break;
    1430             :                 }
    1431             :             }
    1432      157684 :             break;
    1433        2826 :         case T_A_Expr:
    1434             :             {
    1435        2826 :                 const A_Expr *aexpr = (const A_Expr *) expr;
    1436             : 
    1437             :                 /* use leftmost of operator or left operand (if any) */
    1438             :                 /* we assume right operand can't be to left of operator */
    1439        2826 :                 loc = leftmostLoc(aexpr->location,
    1440        2826 :                                   exprLocation(aexpr->lexpr));
    1441             :             }
    1442        2826 :             break;
    1443       56798 :         case T_ColumnRef:
    1444       56798 :             loc = ((const ColumnRef *) expr)->location;
    1445       56798 :             break;
    1446           0 :         case T_ParamRef:
    1447           0 :             loc = ((const ParamRef *) expr)->location;
    1448           0 :             break;
    1449       20876 :         case T_A_Const:
    1450       20876 :             loc = ((const A_Const *) expr)->location;
    1451       20876 :             break;
    1452        1942 :         case T_FuncCall:
    1453             :             {
    1454        1942 :                 const FuncCall *fc = (const FuncCall *) expr;
    1455             : 
    1456             :                 /* consider both function name and leftmost arg */
    1457             :                 /* (we assume any ORDER BY nodes must be to right of name) */
    1458        1942 :                 loc = leftmostLoc(fc->location,
    1459        1942 :                                   exprLocation((Node *) fc->args));
    1460             :             }
    1461        1942 :             break;
    1462           8 :         case T_A_ArrayExpr:
    1463             :             /* the location points at ARRAY or [, which must be leftmost */
    1464           8 :             loc = ((const A_ArrayExpr *) expr)->location;
    1465           8 :             break;
    1466           8 :         case T_ResTarget:
    1467             :             /* we need not examine the contained expression (if any) */
    1468           8 :             loc = ((const ResTarget *) expr)->location;
    1469           8 :             break;
    1470           0 :         case T_MultiAssignRef:
    1471           0 :             loc = exprLocation(((const MultiAssignRef *) expr)->source);
    1472           0 :             break;
    1473        3656 :         case T_TypeCast:
    1474             :             {
    1475        3656 :                 const TypeCast *tc = (const TypeCast *) expr;
    1476             : 
    1477             :                 /*
    1478             :                  * This could represent CAST(), ::, or TypeName 'literal', so
    1479             :                  * any of the components might be leftmost.
    1480             :                  */
    1481        3656 :                 loc = exprLocation(tc->arg);
    1482        3656 :                 loc = leftmostLoc(loc, tc->typeName->location);
    1483        3656 :                 loc = leftmostLoc(loc, tc->location);
    1484             :             }
    1485        3656 :             break;
    1486         140 :         case T_CollateClause:
    1487             :             /* just use argument's location */
    1488         140 :             loc = exprLocation(((const CollateClause *) expr)->arg);
    1489         140 :             break;
    1490           4 :         case T_SortBy:
    1491             :             /* just use argument's location (ignore operator, if any) */
    1492           4 :             loc = exprLocation(((const SortBy *) expr)->node);
    1493           4 :             break;
    1494           0 :         case T_WindowDef:
    1495           0 :             loc = ((const WindowDef *) expr)->location;
    1496           0 :             break;
    1497           0 :         case T_RangeTableSample:
    1498           0 :             loc = ((const RangeTableSample *) expr)->location;
    1499           0 :             break;
    1500           0 :         case T_TypeName:
    1501           0 :             loc = ((const TypeName *) expr)->location;
    1502           0 :             break;
    1503           0 :         case T_ColumnDef:
    1504           0 :             loc = ((const ColumnDef *) expr)->location;
    1505           0 :             break;
    1506           0 :         case T_Constraint:
    1507           0 :             loc = ((const Constraint *) expr)->location;
    1508           0 :             break;
    1509           0 :         case T_FunctionParameter:
    1510             :             /* just use typename's location */
    1511           0 :             loc = exprLocation((Node *) ((const FunctionParameter *) expr)->argType);
    1512           0 :             break;
    1513           0 :         case T_XmlSerialize:
    1514             :             /* XMLSERIALIZE keyword should always be the first thing */
    1515           0 :             loc = ((const XmlSerialize *) expr)->location;
    1516           0 :             break;
    1517          12 :         case T_GroupingSet:
    1518          12 :             loc = ((const GroupingSet *) expr)->location;
    1519          12 :             break;
    1520           0 :         case T_WithClause:
    1521           0 :             loc = ((const WithClause *) expr)->location;
    1522           0 :             break;
    1523           0 :         case T_InferClause:
    1524           0 :             loc = ((const InferClause *) expr)->location;
    1525           0 :             break;
    1526           4 :         case T_OnConflictClause:
    1527           4 :             loc = ((const OnConflictClause *) expr)->location;
    1528           4 :             break;
    1529           0 :         case T_CommonTableExpr:
    1530           0 :             loc = ((const CommonTableExpr *) expr)->location;
    1531           0 :             break;
    1532           0 :         case T_PlaceHolderVar:
    1533             :             /* just use argument's location */
    1534           0 :             loc = exprLocation((Node *) ((const PlaceHolderVar *) expr)->phexpr);
    1535           0 :             break;
    1536           0 :         case T_InferenceElem:
    1537             :             /* just use nested expr's location */
    1538           0 :             loc = exprLocation((Node *) ((const InferenceElem *) expr)->expr);
    1539           0 :             break;
    1540           0 :         case T_PartitionElem:
    1541           0 :             loc = ((const PartitionElem *) expr)->location;
    1542           0 :             break;
    1543           0 :         case T_PartitionSpec:
    1544           0 :             loc = ((const PartitionSpec *) expr)->location;
    1545           0 :             break;
    1546          32 :         case T_PartitionBoundSpec:
    1547          32 :             loc = ((const PartitionBoundSpec *) expr)->location;
    1548          32 :             break;
    1549          12 :         case T_PartitionRangeDatum:
    1550          12 :             loc = ((const PartitionRangeDatum *) expr)->location;
    1551          12 :             break;
    1552       44410 :         default:
    1553             :             /* for any other node type it's just unknown... */
    1554       44410 :             loc = -1;
    1555       44410 :             break;
    1556             :     }
    1557     5069478 :     return loc;
    1558             : }
    1559             : 
    1560             : /*
    1561             :  * leftmostLoc - support for exprLocation
    1562             :  *
    1563             :  * Take the minimum of two parse location values, but ignore unknowns
    1564             :  */
    1565             : static int
    1566      587060 : leftmostLoc(int loc1, int loc2)
    1567             : {
    1568      587060 :     if (loc1 < 0)
    1569       70756 :         return loc2;
    1570      516304 :     else if (loc2 < 0)
    1571       80900 :         return loc1;
    1572             :     else
    1573      435404 :         return Min(loc1, loc2);
    1574             : }
    1575             : 
    1576             : 
    1577             : /*
    1578             :  * fix_opfuncids
    1579             :  *    Calculate opfuncid field from opno for each OpExpr node in given tree.
    1580             :  *    The given tree can be anything expression_tree_walker handles.
    1581             :  *
    1582             :  * The argument is modified in-place.  (This is OK since we'd want the
    1583             :  * same change for any node, even if it gets visited more than once due to
    1584             :  * shared structure.)
    1585             :  */
    1586             : void
    1587      194392 : fix_opfuncids(Node *node)
    1588             : {
    1589             :     /* This tree walk requires no special setup, so away we go... */
    1590      194392 :     fix_opfuncids_walker(node, NULL);
    1591      194392 : }
    1592             : 
    1593             : static bool
    1594      459654 : fix_opfuncids_walker(Node *node, void *context)
    1595             : {
    1596      459654 :     if (node == NULL)
    1597       77862 :         return false;
    1598      381792 :     if (IsA(node, OpExpr))
    1599       33912 :         set_opfuncid((OpExpr *) node);
    1600      347880 :     else if (IsA(node, DistinctExpr))
    1601           4 :         set_opfuncid((OpExpr *) node);  /* rely on struct equivalence */
    1602      347876 :     else if (IsA(node, NullIfExpr))
    1603          20 :         set_opfuncid((OpExpr *) node);  /* rely on struct equivalence */
    1604      347856 :     else if (IsA(node, ScalarArrayOpExpr))
    1605        1212 :         set_sa_opfuncid((ScalarArrayOpExpr *) node);
    1606      381792 :     return expression_tree_walker(node, fix_opfuncids_walker, context);
    1607             : }
    1608             : 
    1609             : /*
    1610             :  * set_opfuncid
    1611             :  *      Set the opfuncid (procedure OID) in an OpExpr node,
    1612             :  *      if it hasn't been set already.
    1613             :  *
    1614             :  * Because of struct equivalence, this can also be used for
    1615             :  * DistinctExpr and NullIfExpr nodes.
    1616             :  */
    1617             : void
    1618     2423950 : set_opfuncid(OpExpr *opexpr)
    1619             : {
    1620     2423950 :     if (opexpr->opfuncid == InvalidOid)
    1621      140242 :         opexpr->opfuncid = get_opcode(opexpr->opno);
    1622     2423950 : }
    1623             : 
    1624             : /*
    1625             :  * set_sa_opfuncid
    1626             :  *      As above, for ScalarArrayOpExpr nodes.
    1627             :  */
    1628             : void
    1629      123788 : set_sa_opfuncid(ScalarArrayOpExpr *opexpr)
    1630             : {
    1631      123788 :     if (opexpr->opfuncid == InvalidOid)
    1632         178 :         opexpr->opfuncid = get_opcode(opexpr->opno);
    1633      123788 : }
    1634             : 
    1635             : 
    1636             : /*
    1637             :  *  check_functions_in_node -
    1638             :  *    apply checker() to each function OID contained in given expression node
    1639             :  *
    1640             :  * Returns true if the checker() function does; for nodes representing more
    1641             :  * than one function call, returns true if the checker() function does so
    1642             :  * for any of those functions.  Returns false if node does not invoke any
    1643             :  * SQL-visible function.  Caller must not pass node == NULL.
    1644             :  *
    1645             :  * This function examines only the given node; it does not recurse into any
    1646             :  * sub-expressions.  Callers typically prefer to keep control of the recursion
    1647             :  * for themselves, in case additional checks should be made, or because they
    1648             :  * have special rules about which parts of the tree need to be visited.
    1649             :  *
    1650             :  * Note: we ignore MinMaxExpr, SQLValueFunction, XmlExpr, CoerceToDomain,
    1651             :  * and NextValueExpr nodes, because they do not contain SQL function OIDs.
    1652             :  * However, they can invoke SQL-visible functions, so callers should take
    1653             :  * thought about how to treat them.
    1654             :  */
    1655             : bool
    1656    14323180 : check_functions_in_node(Node *node, check_function_callback checker,
    1657             :                         void *context)
    1658             : {
    1659    14323180 :     switch (nodeTag(node))
    1660             :     {
    1661       75454 :         case T_Aggref:
    1662       74736 :             {
    1663       75454 :                 Aggref     *expr = (Aggref *) node;
    1664             : 
    1665       75454 :                 if (checker(expr->aggfnoid, context))
    1666         718 :                     return true;
    1667             :             }
    1668       74736 :             break;
    1669        3444 :         case T_WindowFunc:
    1670        3344 :             {
    1671        3444 :                 WindowFunc *expr = (WindowFunc *) node;
    1672             : 
    1673        3444 :                 if (checker(expr->winfnoid, context))
    1674         100 :                     return true;
    1675             :             }
    1676        3344 :             break;
    1677      475112 :         case T_FuncExpr:
    1678      420792 :             {
    1679      475112 :                 FuncExpr   *expr = (FuncExpr *) node;
    1680             : 
    1681      475112 :                 if (checker(expr->funcid, context))
    1682       54320 :                     return true;
    1683             :             }
    1684      420792 :             break;
    1685      966360 :         case T_OpExpr:
    1686             :         case T_DistinctExpr:    /* struct-equivalent to OpExpr */
    1687             :         case T_NullIfExpr:      /* struct-equivalent to OpExpr */
    1688      965706 :             {
    1689      966360 :                 OpExpr     *expr = (OpExpr *) node;
    1690             : 
    1691             :                 /* Set opfuncid if it wasn't set already */
    1692      966360 :                 set_opfuncid(expr);
    1693      966360 :                 if (checker(expr->opfuncid, context))
    1694         654 :                     return true;
    1695             :             }
    1696      965706 :             break;
    1697       37214 :         case T_ScalarArrayOpExpr:
    1698       37178 :             {
    1699       37214 :                 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
    1700             : 
    1701       37214 :                 set_sa_opfuncid(expr);
    1702       37214 :                 if (checker(expr->opfuncid, context))
    1703          36 :                     return true;
    1704             :             }
    1705       37178 :             break;
    1706       12860 :         case T_CoerceViaIO:
    1707       12802 :             {
    1708       12860 :                 CoerceViaIO *expr = (CoerceViaIO *) node;
    1709             :                 Oid         iofunc;
    1710             :                 Oid         typioparam;
    1711             :                 bool        typisvarlena;
    1712             : 
    1713             :                 /* check the result type's input function */
    1714       12860 :                 getTypeInputInfo(expr->resulttype,
    1715             :                                  &iofunc, &typioparam);
    1716       12860 :                 if (checker(iofunc, context))
    1717          58 :                     return true;
    1718             :                 /* check the input type's output function */
    1719       12844 :                 getTypeOutputInfo(exprType((Node *) expr->arg),
    1720             :                                   &iofunc, &typisvarlena);
    1721       12844 :                 if (checker(iofunc, context))
    1722          42 :                     return true;
    1723             :             }
    1724       12802 :             break;
    1725         152 :         case T_RowCompareExpr:
    1726         152 :             {
    1727         152 :                 RowCompareExpr *rcexpr = (RowCompareExpr *) node;
    1728             :                 ListCell   *opid;
    1729             : 
    1730         524 :                 foreach(opid, rcexpr->opnos)
    1731             :                 {
    1732         372 :                     Oid         opfuncid = get_opcode(lfirst_oid(opid));
    1733             : 
    1734         372 :                     if (checker(opfuncid, context))
    1735           0 :                         return true;
    1736             :                 }
    1737             :             }
    1738         152 :             break;
    1739    12752584 :         default:
    1740    12752584 :             break;
    1741             :     }
    1742    14267294 :     return false;
    1743             : }
    1744             : 
    1745             : 
    1746             : /*
    1747             :  * Standard expression-tree walking support
    1748             :  *
    1749             :  * We used to have near-duplicate code in many different routines that
    1750             :  * understood how to recurse through an expression node tree.  That was
    1751             :  * a pain to maintain, and we frequently had bugs due to some particular
    1752             :  * routine neglecting to support a particular node type.  In most cases,
    1753             :  * these routines only actually care about certain node types, and don't
    1754             :  * care about other types except insofar as they have to recurse through
    1755             :  * non-primitive node types.  Therefore, we now provide generic tree-walking
    1756             :  * logic to consolidate the redundant "boilerplate" code.  There are
    1757             :  * two versions: expression_tree_walker() and expression_tree_mutator().
    1758             :  */
    1759             : 
    1760             : /*
    1761             :  * expression_tree_walker() is designed to support routines that traverse
    1762             :  * a tree in a read-only fashion (although it will also work for routines
    1763             :  * that modify nodes in-place but never add/delete/replace nodes).
    1764             :  * A walker routine should look like this:
    1765             :  *
    1766             :  * bool my_walker (Node *node, my_struct *context)
    1767             :  * {
    1768             :  *      if (node == NULL)
    1769             :  *          return false;
    1770             :  *      // check for nodes that special work is required for, eg:
    1771             :  *      if (IsA(node, Var))
    1772             :  *      {
    1773             :  *          ... do special actions for Var nodes
    1774             :  *      }
    1775             :  *      else if (IsA(node, ...))
    1776             :  *      {
    1777             :  *          ... do special actions for other node types
    1778             :  *      }
    1779             :  *      // for any node type not specially processed, do:
    1780             :  *      return expression_tree_walker(node, my_walker, (void *) context);
    1781             :  * }
    1782             :  *
    1783             :  * The "context" argument points to a struct that holds whatever context
    1784             :  * information the walker routine needs --- it can be used to return data
    1785             :  * gathered by the walker, too.  This argument is not touched by
    1786             :  * expression_tree_walker, but it is passed down to recursive sub-invocations
    1787             :  * of my_walker.  The tree walk is started from a setup routine that
    1788             :  * fills in the appropriate context struct, calls my_walker with the top-level
    1789             :  * node of the tree, and then examines the results.
    1790             :  *
    1791             :  * The walker routine should return "false" to continue the tree walk, or
    1792             :  * "true" to abort the walk and immediately return "true" to the top-level
    1793             :  * caller.  This can be used to short-circuit the traversal if the walker
    1794             :  * has found what it came for.  "false" is returned to the top-level caller
    1795             :  * iff no invocation of the walker returned "true".
    1796             :  *
    1797             :  * The node types handled by expression_tree_walker include all those
    1798             :  * normally found in target lists and qualifier clauses during the planning
    1799             :  * stage.  In particular, it handles List nodes since a cnf-ified qual clause
    1800             :  * will have List structure at the top level, and it handles TargetEntry nodes
    1801             :  * so that a scan of a target list can be handled without additional code.
    1802             :  * Also, RangeTblRef, FromExpr, JoinExpr, and SetOperationStmt nodes are
    1803             :  * handled, so that query jointrees and setOperation trees can be processed
    1804             :  * without additional code.
    1805             :  *
    1806             :  * expression_tree_walker will handle SubLink nodes by recursing normally
    1807             :  * into the "testexpr" subtree (which is an expression belonging to the outer
    1808             :  * plan).  It will also call the walker on the sub-Query node; however, when
    1809             :  * expression_tree_walker itself is called on a Query node, it does nothing
    1810             :  * and returns "false".  The net effect is that unless the walker does
    1811             :  * something special at a Query node, sub-selects will not be visited during
    1812             :  * an expression tree walk. This is exactly the behavior wanted in many cases
    1813             :  * --- and for those walkers that do want to recurse into sub-selects, special
    1814             :  * behavior is typically needed anyway at the entry to a sub-select (such as
    1815             :  * incrementing a depth counter). A walker that wants to examine sub-selects
    1816             :  * should include code along the lines of:
    1817             :  *
    1818             :  *      if (IsA(node, Query))
    1819             :  *      {
    1820             :  *          adjust context for subquery;
    1821             :  *          result = query_tree_walker((Query *) node, my_walker, context,
    1822             :  *                                     0); // adjust flags as needed
    1823             :  *          restore context if needed;
    1824             :  *          return result;
    1825             :  *      }
    1826             :  *
    1827             :  * query_tree_walker is a convenience routine (see below) that calls the
    1828             :  * walker on all the expression subtrees of the given Query node.
    1829             :  *
    1830             :  * expression_tree_walker will handle SubPlan nodes by recursing normally
    1831             :  * into the "testexpr" and the "args" list (which are expressions belonging to
    1832             :  * the outer plan).  It will not touch the completed subplan, however.  Since
    1833             :  * there is no link to the original Query, it is not possible to recurse into
    1834             :  * subselects of an already-planned expression tree.  This is OK for current
    1835             :  * uses, but may need to be revisited in future.
    1836             :  */
    1837             : 
    1838             : bool
    1839    76916634 : expression_tree_walker(Node *node,
    1840             :                        bool (*walker) (),
    1841             :                        void *context)
    1842             : {
    1843             :     ListCell   *temp;
    1844             : 
    1845             :     /*
    1846             :      * The walker has already visited the current node, and so we need only
    1847             :      * recurse into any sub-nodes it has.
    1848             :      *
    1849             :      * We assume that the walker is not interested in List nodes per se, so
    1850             :      * when we expect a List we just recurse directly to self without
    1851             :      * bothering to call the walker.
    1852             :      */
    1853    76916634 :     if (node == NULL)
    1854      955294 :         return false;
    1855             : 
    1856             :     /* Guard against stack overflow due to overly complex expressions */
    1857    75961340 :     check_stack_depth();
    1858             : 
    1859    75961340 :     switch (nodeTag(node))
    1860             :     {
    1861    27853050 :         case T_Var:
    1862             :         case T_Const:
    1863             :         case T_Param:
    1864             :         case T_CaseTestExpr:
    1865             :         case T_SQLValueFunction:
    1866             :         case T_CoerceToDomainValue:
    1867             :         case T_SetToDefault:
    1868             :         case T_CurrentOfExpr:
    1869             :         case T_NextValueExpr:
    1870             :         case T_RangeTblRef:
    1871             :         case T_SortGroupClause:
    1872             :             /* primitive node types with no expression subnodes */
    1873    27853050 :             break;
    1874        3504 :         case T_WithCheckOption:
    1875        3504 :             return walker(((WithCheckOption *) node)->qual, context);
    1876      189896 :         case T_Aggref:
    1877      178790 :             {
    1878      189896 :                 Aggref     *expr = (Aggref *) node;
    1879             : 
    1880             :                 /* recurse directly on List */
    1881      189896 :                 if (expression_tree_walker((Node *) expr->aggdirectargs,
    1882             :                                            walker, context))
    1883           0 :                     return true;
    1884      189896 :                 if (expression_tree_walker((Node *) expr->args,
    1885             :                                            walker, context))
    1886       11070 :                     return true;
    1887      178826 :                 if (expression_tree_walker((Node *) expr->aggorder,
    1888             :                                            walker, context))
    1889           0 :                     return true;
    1890      178826 :                 if (expression_tree_walker((Node *) expr->aggdistinct,
    1891             :                                            walker, context))
    1892           0 :                     return true;
    1893      178826 :                 if (walker((Node *) expr->aggfilter, context))
    1894          36 :                     return true;
    1895             :             }
    1896      178790 :             break;
    1897        1312 :         case T_GroupingFunc:
    1898        1312 :             {
    1899        1312 :                 GroupingFunc *grouping = (GroupingFunc *) node;
    1900             : 
    1901        1312 :                 if (expression_tree_walker((Node *) grouping->args,
    1902             :                                            walker, context))
    1903           0 :                     return true;
    1904             :             }
    1905        1312 :             break;
    1906        7518 :         case T_WindowFunc:
    1907        7158 :             {
    1908        7518 :                 WindowFunc *expr = (WindowFunc *) node;
    1909             : 
    1910             :                 /* recurse directly on List */
    1911        7518 :                 if (expression_tree_walker((Node *) expr->args,
    1912             :                                            walker, context))
    1913         352 :                     return true;
    1914        7166 :                 if (walker((Node *) expr->aggfilter, context))
    1915           8 :                     return true;
    1916             :             }
    1917        7158 :             break;
    1918       54912 :         case T_SubscriptingRef:
    1919       51676 :             {
    1920       54912 :                 SubscriptingRef *sbsref = (SubscriptingRef *) node;
    1921             : 
    1922             :                 /* recurse directly for upper/lower container index lists */
    1923       54912 :                 if (expression_tree_walker((Node *) sbsref->refupperindexpr,
    1924             :                                            walker, context))
    1925         262 :                     return true;
    1926       54650 :                 if (expression_tree_walker((Node *) sbsref->reflowerindexpr,
    1927             :                                            walker, context))
    1928           0 :                     return true;
    1929             :                 /* walker must see the refexpr and refassgnexpr, however */
    1930       54650 :                 if (walker(sbsref->refexpr, context))
    1931        2914 :                     return true;
    1932             : 
    1933       51736 :                 if (walker(sbsref->refassgnexpr, context))
    1934          60 :                     return true;
    1935             :             }
    1936       51676 :             break;
    1937     3390262 :         case T_FuncExpr:
    1938     3314482 :             {
    1939     3390262 :                 FuncExpr   *expr = (FuncExpr *) node;
    1940             : 
    1941     3390262 :                 if (expression_tree_walker((Node *) expr->args,
    1942             :                                            walker, context))
    1943       75780 :                     return true;
    1944             :             }
    1945     3314482 :             break;
    1946        1288 :         case T_NamedArgExpr:
    1947        1288 :             return walker(((NamedArgExpr *) node)->arg, context);
    1948     5765128 :         case T_OpExpr:
    1949             :         case T_DistinctExpr:    /* struct-equivalent to OpExpr */
    1950             :         case T_NullIfExpr:      /* struct-equivalent to OpExpr */
    1951     5714034 :             {
    1952     5765128 :                 OpExpr     *expr = (OpExpr *) node;
    1953             : 
    1954     5765128 :                 if (expression_tree_walker((Node *) expr->args,
    1955             :                                            walker, context))
    1956       51042 :                     return true;
    1957             :             }
    1958     5714034 :             break;
    1959      404362 :         case T_ScalarArrayOpExpr:
    1960      375290 :             {
    1961      404362 :                 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
    1962             : 
    1963      404362 :                 if (expression_tree_walker((Node *) expr->args,
    1964             :                                            walker, context))
    1965       29072 :                     return true;
    1966             :             }
    1967      375290 :             break;
    1968     1075606 :         case T_BoolExpr:
    1969     1067836 :             {
    1970     1075606 :                 BoolExpr   *expr = (BoolExpr *) node;
    1971             : 
    1972     1075606 :                 if (expression_tree_walker((Node *) expr->args,
    1973             :                                            walker, context))
    1974        7766 :                     return true;
    1975             :             }
    1976     1067836 :             break;
    1977      302384 :         case T_SubLink:
    1978             :             {
    1979      302384 :                 SubLink    *sublink = (SubLink *) node;
    1980             : 
    1981      302384 :                 if (walker(sublink->testexpr, context))
    1982           8 :                     return true;
    1983             : 
    1984             :                 /*
    1985             :                  * Also invoke the walker on the sublink's Query node, so it
    1986             :                  * can recurse into the sub-query if it wants to.
    1987             :                  */
    1988      302376 :                 return walker(sublink->subselect, context);
    1989             :             }
    1990             :             break;
    1991      128842 :         case T_SubPlan:
    1992      128546 :             {
    1993      128842 :                 SubPlan    *subplan = (SubPlan *) node;
    1994             : 
    1995             :                 /* recurse into the testexpr, but not into the Plan */
    1996      128842 :                 if (walker(subplan->testexpr, context))
    1997          40 :                     return true;
    1998             :                 /* also examine args list */
    1999      128802 :                 if (expression_tree_walker((Node *) subplan->args,
    2000             :                                            walker, context))
    2001         256 :                     return true;
    2002             :             }
    2003      128546 :             break;
    2004        7268 :         case T_AlternativeSubPlan:
    2005        7268 :             return walker(((AlternativeSubPlan *) node)->subplans, context);
    2006      112494 :         case T_FieldSelect:
    2007      112494 :             return walker(((FieldSelect *) node)->arg, context);
    2008        1096 :         case T_FieldStore:
    2009        1092 :             {
    2010        1096 :                 FieldStore *fstore = (FieldStore *) node;
    2011             : 
    2012        1096 :                 if (walker(fstore->arg, context))
    2013           0 :                     return true;
    2014        1096 :                 if (walker(fstore->newvals, context))
    2015           4 :                     return true;
    2016             :             }
    2017        1092 :             break;
    2018      717900 :         case T_RelabelType:
    2019      717900 :             return walker(((RelabelType *) node)->arg, context);
    2020      107536 :         case T_CoerceViaIO:
    2021      107536 :             return walker(((CoerceViaIO *) node)->arg, context);
    2022       10900 :         case T_ArrayCoerceExpr:
    2023       10772 :             {
    2024       10900 :                 ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node;
    2025             : 
    2026       10900 :                 if (walker(acoerce->arg, context))
    2027         112 :                     return true;
    2028       10788 :                 if (walker(acoerce->elemexpr, context))
    2029          16 :                     return true;
    2030             :             }
    2031       10772 :             break;
    2032        2284 :         case T_ConvertRowtypeExpr:
    2033        2284 :             return walker(((ConvertRowtypeExpr *) node)->arg, context);
    2034       10704 :         case T_CollateExpr:
    2035       10704 :             return walker(((CollateExpr *) node)->arg, context);
    2036      386342 :         case T_CaseExpr:
    2037      375578 :             {
    2038      386342 :                 CaseExpr   *caseexpr = (CaseExpr *) node;
    2039             : 
    2040      386342 :                 if (walker(caseexpr->arg, context))
    2041          50 :                     return true;
    2042             :                 /* we assume walker doesn't care about CaseWhens, either */
    2043      939652 :                 foreach(temp, caseexpr->args)
    2044             :                 {
    2045      555588 :                     CaseWhen   *when = lfirst_node(CaseWhen, temp);
    2046             : 
    2047      555588 :                     if (walker(when->expr, context))
    2048        2228 :                         return true;
    2049      553688 :                     if (walker(when->result, context))
    2050         328 :                         return true;
    2051             :                 }
    2052      384064 :                 if (walker(caseexpr->defresult, context))
    2053        8486 :                     return true;
    2054             :             }
    2055      375578 :             break;
    2056      213456 :         case T_ArrayExpr:
    2057      213456 :             return walker(((ArrayExpr *) node)->elements, context);
    2058       15902 :         case T_RowExpr:
    2059             :             /* Assume colnames isn't interesting */
    2060       15902 :             return walker(((RowExpr *) node)->args, context);
    2061         940 :         case T_RowCompareExpr:
    2062         940 :             {
    2063         940 :                 RowCompareExpr *rcexpr = (RowCompareExpr *) node;
    2064             : 
    2065         940 :                 if (walker(rcexpr->largs, context))
    2066           0 :                     return true;
    2067         940 :                 if (walker(rcexpr->rargs, context))
    2068           0 :                     return true;
    2069             :             }
    2070         940 :             break;
    2071      220930 :         case T_CoalesceExpr:
    2072      220930 :             return walker(((CoalesceExpr *) node)->args, context);
    2073        3962 :         case T_MinMaxExpr:
    2074        3962 :             return walker(((MinMaxExpr *) node)->args, context);
    2075        2440 :         case T_XmlExpr:
    2076        2412 :             {
    2077        2440 :                 XmlExpr    *xexpr = (XmlExpr *) node;
    2078             : 
    2079        2440 :                 if (walker(xexpr->named_args, context))
    2080           8 :                     return true;
    2081             :                 /* we assume walker doesn't care about arg_names */
    2082        2432 :                 if (walker(xexpr->args, context))
    2083          20 :                     return true;
    2084             :             }
    2085        2412 :             break;
    2086      178486 :         case T_NullTest:
    2087      178486 :             return walker(((NullTest *) node)->arg, context);
    2088        2146 :         case T_BooleanTest:
    2089        2146 :             return walker(((BooleanTest *) node)->arg, context);
    2090     1209340 :         case T_CoerceToDomain:
    2091     1209340 :             return walker(((CoerceToDomain *) node)->arg, context);
    2092    14131882 :         case T_TargetEntry:
    2093    14131882 :             return walker(((TargetEntry *) node)->expr, context);
    2094      105750 :         case T_Query:
    2095             :             /* Do nothing with a sub-Query, per discussion above */
    2096      105750 :             break;
    2097         390 :         case T_WindowClause:
    2098         390 :             {
    2099         390 :                 WindowClause *wc = (WindowClause *) node;
    2100             : 
    2101         390 :                 if (walker(wc->partitionClause, context))
    2102           0 :                     return true;
    2103         390 :                 if (walker(wc->orderClause, context))
    2104           0 :                     return true;
    2105         390 :                 if (walker(wc->startOffset, context))
    2106           0 :                     return true;
    2107         390 :                 if (walker(wc->endOffset, context))
    2108           0 :                     return true;
    2109             :             }
    2110         390 :             break;
    2111        2150 :         case T_CommonTableExpr:
    2112             :             {
    2113        2150 :                 CommonTableExpr *cte = (CommonTableExpr *) node;
    2114             : 
    2115             :                 /*
    2116             :                  * Invoke the walker on the CTE's Query node, so it can
    2117             :                  * recurse into the sub-query if it wants to.
    2118             :                  */
    2119        2150 :                 return walker(cte->ctequery, context);
    2120             :             }
    2121             :             break;
    2122    17369890 :         case T_List:
    2123    64531784 :             foreach(temp, (List *) node)
    2124             :             {
    2125    47761170 :                 if (walker((Node *) lfirst(temp), context))
    2126      599104 :                     return true;
    2127             :             }
    2128    16770614 :             break;
    2129     1307530 :         case T_FromExpr:
    2130     1280072 :             {
    2131     1307530 :                 FromExpr   *from = (FromExpr *) node;
    2132             : 
    2133     1307530 :                 if (walker(from->fromlist, context))
    2134       23700 :                     return true;
    2135     1283830 :                 if (walker(from->quals, context))
    2136        3754 :                     return true;
    2137             :             }
    2138     1280072 :             break;
    2139        1624 :         case T_OnConflictExpr:
    2140        1624 :             {
    2141        1624 :                 OnConflictExpr *onconflict = (OnConflictExpr *) node;
    2142             : 
    2143        1624 :                 if (walker((Node *) onconflict->arbiterElems, context))
    2144           0 :                     return true;
    2145        1624 :                 if (walker(onconflict->arbiterWhere, context))
    2146           0 :                     return true;
    2147        1624 :                 if (walker(onconflict->onConflictSet, context))
    2148           0 :                     return true;
    2149        1624 :                 if (walker(onconflict->onConflictWhere, context))
    2150           0 :                     return true;
    2151        1624 :                 if (walker(onconflict->exclRelTlist, context))
    2152           0 :                     return true;
    2153             :             }
    2154        1624 :             break;
    2155           0 :         case T_PartitionPruneStepOp:
    2156           0 :             {
    2157           0 :                 PartitionPruneStepOp *opstep = (PartitionPruneStepOp *) node;
    2158             : 
    2159           0 :                 if (walker((Node *) opstep->exprs, context))
    2160           0 :                     return true;
    2161             :             }
    2162           0 :             break;
    2163           0 :         case T_PartitionPruneStepCombine:
    2164             :             /* no expression subnodes */
    2165           0 :             break;
    2166      404810 :         case T_JoinExpr:
    2167      389776 :             {
    2168      404810 :                 JoinExpr   *join = (JoinExpr *) node;
    2169             : 
    2170      404810 :                 if (walker(join->larg, context))
    2171        8018 :                     return true;
    2172      396792 :                 if (walker(join->rarg, context))
    2173        6834 :                     return true;
    2174      389958 :                 if (walker(join->quals, context))
    2175         182 :                     return true;
    2176             : 
    2177             :                 /*
    2178             :                  * alias clause, using list are deemed uninteresting.
    2179             :                  */
    2180             :             }
    2181      389776 :             break;
    2182       47616 :         case T_SetOperationStmt:
    2183       47616 :             {
    2184       47616 :                 SetOperationStmt *setop = (SetOperationStmt *) node;
    2185             : 
    2186       47616 :                 if (walker(setop->larg, context))
    2187           0 :                     return true;
    2188       47616 :                 if (walker(setop->rarg, context))
    2189           0 :                     return true;
    2190             : 
    2191             :                 /* groupClauses are deemed uninteresting */
    2192             :             }
    2193       47616 :             break;
    2194           0 :         case T_IndexClause:
    2195           0 :             {
    2196           0 :                 IndexClause *iclause = (IndexClause *) node;
    2197             : 
    2198           0 :                 if (walker(iclause->rinfo, context))
    2199           0 :                     return true;
    2200           0 :                 if (expression_tree_walker((Node *) iclause->indexquals,
    2201             :                                            walker, context))
    2202           0 :                     return true;
    2203             :             }
    2204           0 :             break;
    2205        8062 :         case T_PlaceHolderVar:
    2206        8062 :             return walker(((PlaceHolderVar *) node)->phexpr, context);
    2207        1632 :         case T_InferenceElem:
    2208        1632 :             return walker(((InferenceElem *) node)->expr, context);
    2209         776 :         case T_AppendRelInfo:
    2210         776 :             {
    2211         776 :                 AppendRelInfo *appinfo = (AppendRelInfo *) node;
    2212             : 
    2213         776 :                 if (expression_tree_walker((Node *) appinfo->translated_vars,
    2214             :                                            walker, context))
    2215           0 :                     return true;
    2216             :             }
    2217         776 :             break;
    2218           0 :         case T_PlaceHolderInfo:
    2219           0 :             return walker(((PlaceHolderInfo *) node)->ph_var, context);
    2220      195746 :         case T_RangeTblFunction:
    2221      195746 :             return walker(((RangeTblFunction *) node)->funcexpr, context);
    2222         466 :         case T_TableSampleClause:
    2223         466 :             {
    2224         466 :                 TableSampleClause *tsc = (TableSampleClause *) node;
    2225             : 
    2226         466 :                 if (expression_tree_walker((Node *) tsc->args,
    2227             :                                            walker, context))
    2228           0 :                     return true;
    2229         466 :                 if (walker((Node *) tsc->repeatable, context))
    2230           0 :                     return true;
    2231             :             }
    2232         466 :             break;
    2233         826 :         case T_TableFunc:
    2234         826 :             {
    2235         826 :                 TableFunc  *tf = (TableFunc *) node;
    2236             : 
    2237         826 :                 if (walker(tf->ns_uris, context))
    2238           0 :                     return true;
    2239         826 :                 if (walker(tf->docexpr, context))
    2240           0 :                     return true;
    2241         826 :                 if (walker(tf->rowexpr, context))
    2242           0 :                     return true;
    2243         826 :                 if (walker(tf->colexprs, context))
    2244           0 :                     return true;
    2245         826 :                 if (walker(tf->coldefexprs, context))
    2246           0 :                     return true;
    2247             :             }
    2248         826 :             break;
    2249           0 :         default:
    2250           0 :             elog(ERROR, "unrecognized node type: %d",
    2251             :                  (int) nodeTag(node));
    2252             :             break;
    2253             :     }
    2254    57680878 :     return false;
    2255             : }
    2256             : 
    2257             : /*
    2258             :  * query_tree_walker --- initiate a walk of a Query's expressions
    2259             :  *
    2260             :  * This routine exists just to reduce the number of places that need to know
    2261             :  * where all the expression subtrees of a Query are.  Note it can be used
    2262             :  * for starting a walk at top level of a Query regardless of whether the
    2263             :  * walker intends to descend into subqueries.  It is also useful for
    2264             :  * descending into subqueries within a walker.
    2265             :  *
    2266             :  * Some callers want to suppress visitation of certain items in the sub-Query,
    2267             :  * typically because they need to process them specially, or don't actually
    2268             :  * want to recurse into subqueries.  This is supported by the flags argument,
    2269             :  * which is the bitwise OR of flag values to add or suppress visitation of
    2270             :  * indicated items.  (More flag bits may be added as needed.)
    2271             :  */
    2272             : bool
    2273     1520792 : query_tree_walker(Query *query,
    2274             :                   bool (*walker) (),
    2275             :                   void *context,
    2276             :                   int flags)
    2277             : {
    2278             :     Assert(query != NULL && IsA(query, Query));
    2279             : 
    2280             :     /*
    2281             :      * We don't walk any utilityStmt here. However, we can't easily assert
    2282             :      * that it is absent, since there are at least two code paths by which
    2283             :      * action statements from CREATE RULE end up here, and NOTIFY is allowed
    2284             :      * in a rule action.
    2285             :      */
    2286             : 
    2287     1520792 :     if (walker((Node *) query->targetList, context))
    2288      227824 :         return true;
    2289     1292952 :     if (walker((Node *) query->withCheckOptions, context))
    2290           0 :         return true;
    2291     1292952 :     if (walker((Node *) query->onConflict, context))
    2292           0 :         return true;
    2293     1292952 :     if (walker((Node *) query->returningList, context))
    2294          54 :         return true;
    2295     1292898 :     if (walker((Node *) query->jointree, context))
    2296       27194 :         return true;
    2297     1265700 :     if (walker(query->setOperations, context))
    2298           0 :         return true;
    2299     1265700 :     if (walker(query->havingQual, context))
    2300           0 :         return true;
    2301     1265700 :     if (walker(query->limitOffset, context))
    2302           4 :         return true;
    2303     1265696 :     if (walker(query->limitCount, context))
    2304           0 :         return true;
    2305             : 
    2306             :     /*
    2307             :      * Most callers aren't interested in SortGroupClause nodes since those
    2308             :      * don't contain actual expressions. However they do contain OIDs which
    2309             :      * may be needed by dependency walkers etc.
    2310             :      */
    2311     1265696 :     if ((flags & QTW_EXAMINE_SORTGROUP))
    2312             :     {
    2313       87988 :         if (walker((Node *) query->groupClause, context))
    2314           0 :             return true;
    2315       87988 :         if (walker((Node *) query->windowClause, context))
    2316           0 :             return true;
    2317       87988 :         if (walker((Node *) query->sortClause, context))
    2318           0 :             return true;
    2319       87988 :         if (walker((Node *) query->distinctClause, context))
    2320           0 :             return true;
    2321             :     }
    2322             :     else
    2323             :     {
    2324             :         /*
    2325             :          * But we need to walk the expressions under WindowClause nodes even
    2326             :          * if we're not interested in SortGroupClause nodes.
    2327             :          */
    2328             :         ListCell   *lc;
    2329             : 
    2330     1181498 :         foreach(lc, query->windowClause)
    2331             :         {
    2332        3790 :             WindowClause *wc = lfirst_node(WindowClause, lc);
    2333             : 
    2334        3790 :             if (walker(wc->startOffset, context))
    2335           0 :                 return true;
    2336        3790 :             if (walker(wc->endOffset, context))
    2337           0 :                 return true;
    2338             :         }
    2339             :     }
    2340             : 
    2341             :     /*
    2342             :      * groupingSets and rowMarks are not walked:
    2343             :      *
    2344             :      * groupingSets contain only ressortgrouprefs (integers) which are
    2345             :      * meaningless without the corresponding groupClause or tlist.
    2346             :      * Accordingly, any walker that needs to care about them needs to handle
    2347             :      * them itself in its Query processing.
    2348             :      *
    2349             :      * rowMarks is not walked because it contains only rangetable indexes (and
    2350             :      * flags etc.) and therefore should be handled at Query level similarly.
    2351             :      */
    2352             : 
    2353     1265696 :     if (!(flags & QTW_IGNORE_CTE_SUBQUERIES))
    2354             :     {
    2355      764244 :         if (walker((Node *) query->cteList, context))
    2356          42 :             return true;
    2357             :     }
    2358     1265654 :     if (!(flags & QTW_IGNORE_RANGE_TABLE))
    2359             :     {
    2360      816656 :         if (range_table_walker(query->rtable, walker, context, flags))
    2361        9546 :             return true;
    2362             :     }
    2363     1256108 :     return false;
    2364             : }
    2365             : 
    2366             : /*
    2367             :  * range_table_walker is just the part of query_tree_walker that scans
    2368             :  * a query's rangetable.  This is split out since it can be useful on
    2369             :  * its own.
    2370             :  */
    2371             : bool
    2372      817386 : range_table_walker(List *rtable,
    2373             :                    bool (*walker) (),
    2374             :                    void *context,
    2375             :                    int flags)
    2376             : {
    2377             :     ListCell   *rt;
    2378             : 
    2379     2720002 :     foreach(rt, rtable)
    2380             :     {
    2381     1912162 :         RangeTblEntry *rte = lfirst_node(RangeTblEntry, rt);
    2382             : 
    2383     1912162 :         if (range_table_entry_walker(rte, walker, context, flags))
    2384        9546 :             return true;
    2385             :     }
    2386      807840 :     return false;
    2387             : }
    2388             : 
    2389             : /*
    2390             :  * Some callers even want to scan the expressions in individual RTEs.
    2391             :  */
    2392             : bool
    2393     1912170 : range_table_entry_walker(RangeTblEntry *rte,
    2394             :                          bool (*walker) (),
    2395             :                          void *context,
    2396             :                          int flags)
    2397             : {
    2398             :     /*
    2399             :      * Walkers might need to examine the RTE node itself either before or
    2400             :      * after visiting its contents (or, conceivably, both).  Note that if you
    2401             :      * specify neither flag, the walker won't be called on the RTE at all.
    2402             :      */
    2403     1912170 :     if (flags & QTW_EXAMINE_RTES_BEFORE)
    2404      128836 :         if (walker(rte, context))
    2405           8 :             return true;
    2406             : 
    2407     1912162 :     switch (rte->rtekind)
    2408             :     {
    2409     1318584 :         case RTE_RELATION:
    2410     1318584 :             if (walker(rte->tablesample, context))
    2411           0 :                 return true;
    2412     1318584 :             break;
    2413      154536 :         case RTE_SUBQUERY:
    2414      154536 :             if (!(flags & QTW_IGNORE_RT_SUBQUERIES))
    2415      153358 :                 if (walker(rte->subquery, context))
    2416        2884 :                     return true;
    2417      151652 :             break;
    2418      285830 :         case RTE_JOIN:
    2419      285830 :             if (!(flags & QTW_IGNORE_JOINALIASES))
    2420      185926 :                 if (walker(rte->joinaliasvars, context))
    2421           0 :                     return true;
    2422      285830 :             break;
    2423      132860 :         case RTE_FUNCTION:
    2424      132860 :             if (walker(rte->functions, context))
    2425        6618 :                 return true;
    2426      126242 :             break;
    2427         304 :         case RTE_TABLEFUNC:
    2428         304 :             if (walker(rte->tablefunc, context))
    2429           0 :                 return true;
    2430         304 :             break;
    2431        8994 :         case RTE_VALUES:
    2432        8994 :             if (walker(rte->values_lists, context))
    2433          44 :                 return true;
    2434        8950 :             break;
    2435       11054 :         case RTE_CTE:
    2436             :         case RTE_NAMEDTUPLESTORE:
    2437             :         case RTE_RESULT:
    2438             :             /* nothing to do */
    2439       11054 :             break;
    2440             :     }
    2441             : 
    2442     1902616 :     if (walker(rte->securityQuals, context))
    2443           0 :         return true;
    2444             : 
    2445     1902616 :     if (flags & QTW_EXAMINE_RTES_AFTER)
    2446        4948 :         if (walker(rte, context))
    2447           0 :             return true;
    2448             : 
    2449     1902616 :     return false;
    2450             : }
    2451             : 
    2452             : 
    2453             : /*
    2454             :  * expression_tree_mutator() is designed to support routines that make a
    2455             :  * modified copy of an expression tree, with some nodes being added,
    2456             :  * removed, or replaced by new subtrees.  The original tree is (normally)
    2457             :  * not changed.  Each recursion level is responsible for returning a copy of
    2458             :  * (or appropriately modified substitute for) the subtree it is handed.
    2459             :  * A mutator routine should look like this:
    2460             :  *
    2461             :  * Node * my_mutator (Node *node, my_struct *context)
    2462             :  * {
    2463             :  *      if (node == NULL)
    2464             :  *          return NULL;
    2465             :  *      // check for nodes that special work is required for, eg:
    2466             :  *      if (IsA(node, Var))
    2467             :  *      {
    2468             :  *          ... create and return modified copy of Var node
    2469             :  *      }
    2470             :  *      else if (IsA(node, ...))
    2471             :  *      {
    2472             :  *          ... do special transformations of other node types
    2473             :  *      }
    2474             :  *      // for any node type not specially processed, do:
    2475             :  *      return expression_tree_mutator(node, my_mutator, (void *) context);
    2476             :  * }
    2477             :  *
    2478             :  * The "context" argument points to a struct that holds whatever context
    2479             :  * information the mutator routine needs --- it can be used to return extra
    2480             :  * data gathered by the mutator, too.  This argument is not touched by
    2481             :  * expression_tree_mutator, but it is passed down to recursive sub-invocations
    2482             :  * of my_mutator.  The tree walk is started from a setup routine that
    2483             :  * fills in the appropriate context struct, calls my_mutator with the
    2484             :  * top-level node of the tree, and does any required post-processing.
    2485             :  *
    2486             :  * Each level of recursion must return an appropriately modified Node.
    2487             :  * If expression_tree_mutator() is called, it will make an exact copy
    2488             :  * of the given Node, but invoke my_mutator() to copy the sub-node(s)
    2489             :  * of that Node.  In this way, my_mutator() has full control over the
    2490             :  * copying process but need not directly deal with expression trees
    2491             :  * that it has no interest in.
    2492             :  *
    2493             :  * Just as for expression_tree_walker, the node types handled by
    2494             :  * expression_tree_mutator include all those normally found in target lists
    2495             :  * and qualifier clauses during the planning stage.
    2496             :  *
    2497             :  * expression_tree_mutator will handle SubLink nodes by recursing normally
    2498             :  * into the "testexpr" subtree (which is an expression belonging to the outer
    2499             :  * plan).  It will also call the mutator on the sub-Query node; however, when
    2500             :  * expression_tree_mutator itself is called on a Query node, it does nothing
    2501             :  * and returns the unmodified Query node.  The net effect is that unless the
    2502             :  * mutator does something special at a Query node, sub-selects will not be
    2503             :  * visited or modified; the original sub-select will be linked to by the new
    2504             :  * SubLink node.  Mutators that want to descend into sub-selects will usually
    2505             :  * do so by recognizing Query nodes and calling query_tree_mutator (below).
    2506             :  *
    2507             :  * expression_tree_mutator will handle a SubPlan node by recursing into the
    2508             :  * "testexpr" and the "args" list (which belong to the outer plan), but it
    2509             :  * will simply copy the link to the inner plan, since that's typically what
    2510             :  * expression tree mutators want.  A mutator that wants to modify the subplan
    2511             :  * can force appropriate behavior by recognizing SubPlan expression nodes
    2512             :  * and doing the right thing.
    2513             :  */
    2514             : 
    2515             : Node *
    2516    12654826 : expression_tree_mutator(Node *node,
    2517             :                         Node *(*mutator) (),
    2518             :                         void *context)
    2519             : {
    2520             :     /*
    2521             :      * The mutator has already decided not to modify the current node, but we
    2522             :      * must call the mutator for any sub-nodes.
    2523             :      */
    2524             : 
    2525             : #define FLATCOPY(newnode, node, nodetype)  \
    2526             :     ( (newnode) = (nodetype *) palloc(sizeof(nodetype)), \
    2527             :       memcpy((newnode), (node), sizeof(nodetype)) )
    2528             : 
    2529             : #define CHECKFLATCOPY(newnode, node, nodetype)  \
    2530             :     ( AssertMacro(IsA((node), nodetype)), \
    2531             :       (newnode) = (nodetype *) palloc(sizeof(nodetype)), \
    2532             :       memcpy((newnode), (node), sizeof(nodetype)) )
    2533             : 
    2534             : #define MUTATE(newfield, oldfield, fieldtype)  \
    2535             :         ( (newfield) = (fieldtype) mutator((Node *) (oldfield), context) )
    2536             : 
    2537    12654826 :     if (node == NULL)
    2538       26704 :         return NULL;
    2539             : 
    2540             :     /* Guard against stack overflow due to overly complex expressions */
    2541    12628122 :     check_stack_depth();
    2542             : 
    2543    12628122 :     switch (nodeTag(node))
    2544             :     {
    2545             :             /*
    2546             :              * Primitive node types with no expression subnodes.  Var and
    2547             :              * Const are frequent enough to deserve special cases, the others
    2548             :              * we just use copyObject for.
    2549             :              */
    2550     2095786 :         case T_Var:
    2551             :             {
    2552     2095786 :                 Var        *var = (Var *) node;
    2553             :                 Var        *newnode;
    2554             : 
    2555     2095786 :                 FLATCOPY(newnode, var, Var);
    2556     2095786 :                 return (Node *) newnode;
    2557             :             }
    2558             :             break;
    2559     1745026 :         case T_Const:
    2560             :             {
    2561     1745026 :                 Const      *oldnode = (Const *) node;
    2562             :                 Const      *newnode;
    2563             : 
    2564     1745026 :                 FLATCOPY(newnode, oldnode, Const);
    2565             :                 /* XXX we don't bother with datumCopy; should we? */
    2566     1745026 :                 return (Node *) newnode;
    2567             :             }
    2568             :             break;
    2569      121022 :         case T_Param:
    2570             :         case T_CaseTestExpr:
    2571             :         case T_SQLValueFunction:
    2572             :         case T_CoerceToDomainValue:
    2573             :         case T_SetToDefault:
    2574             :         case T_CurrentOfExpr:
    2575             :         case T_NextValueExpr:
    2576             :         case T_RangeTblRef:
    2577             :         case T_SortGroupClause:
    2578      121022 :             return (Node *) copyObject(node);
    2579         552 :         case T_WithCheckOption:
    2580             :             {
    2581         552 :                 WithCheckOption *wco = (WithCheckOption *) node;
    2582             :                 WithCheckOption *newnode;
    2583             : 
    2584         552 :                 FLATCOPY(newnode, wco, WithCheckOption);
    2585         552 :                 MUTATE(newnode->qual, wco->qual, Node *);
    2586         552 :                 return (Node *) newnode;
    2587             :             }
    2588      102862 :         case T_Aggref:
    2589             :             {
    2590      102862 :                 Aggref     *aggref = (Aggref *) node;
    2591             :                 Aggref     *newnode;
    2592             : 
    2593      102862 :                 FLATCOPY(newnode, aggref, Aggref);
    2594             :                 /* assume mutation doesn't change types of arguments */
    2595      102862 :                 newnode->aggargtypes = list_copy(aggref->aggargtypes);
    2596      102862 :                 MUTATE(newnode->aggdirectargs, aggref->aggdirectargs, List *);
    2597      102862 :                 MUTATE(newnode->args, aggref->args, List *);
    2598      102862 :                 MUTATE(newnode->aggorder, aggref->aggorder, List *);
    2599      102862 :                 MUTATE(newnode->aggdistinct, aggref->aggdistinct, List *);
    2600      102862 :                 MUTATE(newnode->aggfilter, aggref->aggfilter, Expr *);
    2601      102862 :                 return (Node *) newnode;
    2602             :             }
    2603             :             break;
    2604         496 :         case T_GroupingFunc:
    2605             :             {
    2606         496 :                 GroupingFunc *grouping = (GroupingFunc *) node;
    2607             :                 GroupingFunc *newnode;
    2608             : 
    2609         496 :                 FLATCOPY(newnode, grouping, GroupingFunc);
    2610         496 :                 MUTATE(newnode->args, grouping->args, List *);
    2611             : 
    2612             :                 /*
    2613             :                  * We assume here that mutating the arguments does not change
    2614             :                  * the semantics, i.e. that the arguments are not mutated in a
    2615             :                  * way that makes them semantically different from their
    2616             :                  * previously matching expressions in the GROUP BY clause.
    2617             :                  *
    2618             :                  * If a mutator somehow wanted to do this, it would have to
    2619             :                  * handle the refs and cols lists itself as appropriate.
    2620             :                  */
    2621         496 :                 newnode->refs = list_copy(grouping->refs);
    2622         496 :                 newnode->cols = list_copy(grouping->cols);
    2623             : 
    2624         496 :                 return (Node *) newnode;
    2625             :             }
    2626             :             break;
    2627        1992 :         case T_WindowFunc:
    2628             :             {
    2629        1992 :                 WindowFunc *wfunc = (WindowFunc *) node;
    2630             :                 WindowFunc *newnode;
    2631             : 
    2632        1992 :                 FLATCOPY(newnode, wfunc, WindowFunc);
    2633        1992 :                 MUTATE(newnode->args, wfunc->args, List *);
    2634        1992 :                 MUTATE(newnode->aggfilter, wfunc->aggfilter, Expr *);
    2635        1992 :                 return (Node *) newnode;
    2636             :             }
    2637             :             break;
    2638        4658 :         case T_SubscriptingRef:
    2639             :             {
    2640        4658 :                 SubscriptingRef *sbsref = (SubscriptingRef *) node;
    2641             :                 SubscriptingRef *newnode;
    2642             : 
    2643        4658 :                 FLATCOPY(newnode, sbsref, SubscriptingRef);
    2644        4658 :                 MUTATE(newnode->refupperindexpr, sbsref->refupperindexpr,
    2645             :                        List *);
    2646        4658 :                 MUTATE(newnode->reflowerindexpr, sbsref->reflowerindexpr,
    2647             :                        List *);
    2648        4658 :                 MUTATE(newnode->refexpr, sbsref->refexpr,
    2649             :                        Expr *);
    2650        4658 :                 MUTATE(newnode->refassgnexpr, sbsref->refassgnexpr,
    2651             :                        Expr *);
    2652             : 
    2653        4658 :                 return (Node *) newnode;
    2654             :             }
    2655             :             break;
    2656      407352 :         case T_FuncExpr:
    2657             :             {
    2658      407352 :                 FuncExpr   *expr = (FuncExpr *) node;
    2659             :                 FuncExpr   *newnode;
    2660             : 
    2661      407352 :                 FLATCOPY(newnode, expr, FuncExpr);
    2662      407352 :                 MUTATE(newnode->args, expr->args, List *);
    2663      407352 :                 return (Node *) newnode;
    2664             :             }
    2665             :             break;
    2666           0 :         case T_NamedArgExpr:
    2667             :             {
    2668           0 :                 NamedArgExpr *nexpr = (NamedArgExpr *) node;
    2669             :                 NamedArgExpr *newnode;
    2670             : 
    2671           0 :                 FLATCOPY(newnode, nexpr, NamedArgExpr);
    2672           0 :                 MUTATE(newnode->arg, nexpr->arg, Expr *);
    2673           0 :                 return (Node *) newnode;
    2674             :             }
    2675             :             break;
    2676      755534 :         case T_OpExpr:
    2677             :             {
    2678      755534 :                 OpExpr     *expr = (OpExpr *) node;
    2679             :                 OpExpr     *newnode;
    2680             : 
    2681      755534 :                 FLATCOPY(newnode, expr, OpExpr);
    2682      755534 :                 MUTATE(newnode->args, expr->args, List *);
    2683      755534 :                 return (Node *) newnode;
    2684             :             }
    2685             :             break;
    2686        1092 :         case T_DistinctExpr:
    2687             :             {
    2688        1092 :                 DistinctExpr *expr = (DistinctExpr *) node;
    2689             :                 DistinctExpr *newnode;
    2690             : 
    2691        1092 :                 FLATCOPY(newnode, expr, DistinctExpr);
    2692        1092 :                 MUTATE(newnode->args, expr->args, List *);
    2693        1092 :                 return (Node *) newnode;
    2694             :             }
    2695             :             break;
    2696         440 :         case T_NullIfExpr:
    2697             :             {
    2698         440 :                 NullIfExpr *expr = (NullIfExpr *) node;
    2699             :                 NullIfExpr *newnode;
    2700             : 
    2701         440 :                 FLATCOPY(newnode, expr, NullIfExpr);
    2702         440 :                 MUTATE(newnode->args, expr->args, List *);
    2703         440 :                 return (Node *) newnode;
    2704             :             }
    2705             :             break;
    2706       68756 :         case T_ScalarArrayOpExpr:
    2707             :             {
    2708       68756 :                 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
    2709             :                 ScalarArrayOpExpr *newnode;
    2710             : 
    2711       68756 :                 FLATCOPY(newnode, expr, ScalarArrayOpExpr);
    2712       68756 :                 MUTATE(newnode->args, expr->args, List *);
    2713       68756 :                 return (Node *) newnode;
    2714             :             }
    2715             :             break;
    2716      131004 :         case T_BoolExpr:
    2717             :             {
    2718      131004 :                 BoolExpr   *expr = (BoolExpr *) node;
    2719             :                 BoolExpr   *newnode;
    2720             : 
    2721      131004 :                 FLATCOPY(newnode, expr, BoolExpr);
    2722      131004 :                 MUTATE(newnode->args, expr->args, List *);
    2723      131000 :                 return (Node *) newnode;
    2724             :             }
    2725             :             break;
    2726       78366 :         case T_SubLink:
    2727             :             {
    2728       78366 :                 SubLink    *sublink = (SubLink *) node;
    2729             :                 SubLink    *newnode;
    2730             : 
    2731       78366 :                 FLATCOPY(newnode, sublink, SubLink);
    2732       78366 :                 MUTATE(newnode->testexpr, sublink->testexpr, Node *);
    2733             : 
    2734             :                 /*
    2735             :                  * Also invoke the mutator on the sublink's Query node, so it
    2736             :                  * can recurse into the sub-query if it wants to.
    2737             :                  */
    2738       78366 :                 MUTATE(newnode->subselect, sublink->subselect, Node *);
    2739       78366 :                 return (Node *) newnode;
    2740             :             }
    2741             :             break;
    2742       23460 :         case T_SubPlan:
    2743             :             {
    2744       23460 :                 SubPlan    *subplan = (SubPlan *) node;
    2745             :                 SubPlan    *newnode;
    2746             : 
    2747       23460 :                 FLATCOPY(newnode, subplan, SubPlan);
    2748             :                 /* transform testexpr */
    2749       23460 :                 MUTATE(newnode->testexpr, subplan->testexpr, Node *);
    2750             :                 /* transform args list (params to be passed to subplan) */
    2751       23460 :                 MUTATE(newnode->args, subplan->args, List *);
    2752             :                 /* but not the sub-Plan itself, which is referenced as-is */
    2753       23460 :                 return (Node *) newnode;
    2754             :             }
    2755             :             break;
    2756        1138 :         case T_AlternativeSubPlan:
    2757             :             {
    2758        1138 :                 AlternativeSubPlan *asplan = (AlternativeSubPlan *) node;
    2759             :                 AlternativeSubPlan *newnode;
    2760             : 
    2761        1138 :                 FLATCOPY(newnode, asplan, AlternativeSubPlan);
    2762        1138 :                 MUTATE(newnode->subplans, asplan->subplans, List *);
    2763        1138 :                 return (Node *) newnode;
    2764             :             }
    2765             :             break;
    2766        1054 :         case T_FieldSelect:
    2767             :             {
    2768        1054 :                 FieldSelect *fselect = (FieldSelect *) node;
    2769             :                 FieldSelect *newnode;
    2770             : 
    2771        1054 :                 FLATCOPY(newnode, fselect, FieldSelect);
    2772        1054 :                 MUTATE(newnode->arg, fselect->arg, Expr *);
    2773        1054 :                 return (Node *) newnode;
    2774             :             }
    2775             :             break;
    2776         168 :         case T_FieldStore:
    2777             :             {
    2778         168 :                 FieldStore *fstore = (FieldStore *) node;
    2779             :                 FieldStore *newnode;
    2780             : 
    2781         168 :                 FLATCOPY(newnode, fstore, FieldStore);
    2782         168 :                 MUTATE(newnode->arg, fstore->arg, Expr *);
    2783         168 :                 MUTATE(newnode->newvals, fstore->newvals, List *);
    2784         168 :                 newnode->fieldnums = list_copy(fstore->fieldnums);
    2785         168 :                 return (Node *) newnode;
    2786             :             }
    2787             :             break;
    2788       89114 :         case T_RelabelType:
    2789             :             {
    2790       89114 :                 RelabelType *relabel = (RelabelType *) node;
    2791             :                 RelabelType *newnode;
    2792             : 
    2793       89114 :                 FLATCOPY(newnode, relabel, RelabelType);
    2794       89114 :                 MUTATE(newnode->arg, relabel->arg, Expr *);
    2795       89114 :                 return (Node *) newnode;
    2796             :             }
    2797             :             break;
    2798       11294 :         case T_CoerceViaIO:
    2799             :             {
    2800       11294 :                 CoerceViaIO *iocoerce = (CoerceViaIO *) node;
    2801             :                 CoerceViaIO *newnode;
    2802             : 
    2803       11294 :                 FLATCOPY(newnode, iocoerce, CoerceViaIO);
    2804       11294 :                 MUTATE(newnode->arg, iocoerce->arg, Expr *);
    2805       11294 :                 return (Node *) newnode;
    2806             :             }
    2807             :             break;
    2808         280 :         case T_ArrayCoerceExpr:
    2809             :             {
    2810         280 :                 ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node;
    2811             :                 ArrayCoerceExpr *newnode;
    2812             : 
    2813         280 :                 FLATCOPY(newnode, acoerce, ArrayCoerceExpr);
    2814         280 :                 MUTATE(newnode->arg, acoerce->arg, Expr *);
    2815         280 :                 MUTATE(newnode->elemexpr, acoerce->elemexpr, Expr *);
    2816         280 :                 return (Node *) newnode;
    2817             :             }
    2818             :             break;
    2819         100 :         case T_ConvertRowtypeExpr:
    2820             :             {
    2821         100 :                 ConvertRowtypeExpr *convexpr = (ConvertRowtypeExpr *) node;
    2822             :                 ConvertRowtypeExpr *newnode;
    2823             : 
    2824         100 :                 FLATCOPY(newnode, convexpr, ConvertRowtypeExpr);
    2825         100 :                 MUTATE(newnode->arg, convexpr->arg, Expr *);
    2826         100 :                 return (Node *) newnode;
    2827             :             }
    2828             :             break;
    2829        2396 :         case T_CollateExpr:
    2830             :             {
    2831        2396 :                 CollateExpr *collate = (CollateExpr *) node;
    2832             :                 CollateExpr *newnode;
    2833             : 
    2834        2396 :                 FLATCOPY(newnode, collate, CollateExpr);
    2835        2396 :                 MUTATE(newnode->arg, collate->arg, Expr *);
    2836        2396 :                 return (Node *) newnode;
    2837             :             }
    2838             :             break;
    2839       62480 :         case T_CaseExpr:
    2840             :             {
    2841       62480 :                 CaseExpr   *caseexpr = (CaseExpr *) node;
    2842             :                 CaseExpr   *newnode;
    2843             : 
    2844       62480 :                 FLATCOPY(newnode, caseexpr, CaseExpr);
    2845       62480 :                 MUTATE(newnode->arg, caseexpr->arg, Expr *);
    2846       62480 :                 MUTATE(newnode->args, caseexpr->args, List *);
    2847       62480 :                 MUTATE(newnode->defresult, caseexpr->defresult, Expr *);
    2848       62480 :                 return (Node *) newnode;
    2849             :             }
    2850             :             break;
    2851       68952 :         case T_CaseWhen:
    2852             :             {
    2853       68952 :                 CaseWhen   *casewhen = (CaseWhen *) node;
    2854             :                 CaseWhen   *newnode;
    2855             : 
    2856       68952 :                 FLATCOPY(newnode, casewhen, CaseWhen);
    2857       68952 :                 MUTATE(newnode->expr, casewhen->expr, Expr *);
    2858       68952 :                 MUTATE(newnode->result, casewhen->result, Expr *);
    2859       68952 :                 return (Node *) newnode;
    2860             :             }
    2861             :             break;
    2862       19214 :         case T_ArrayExpr:
    2863             :             {
    2864       19214 :                 ArrayExpr  *arrayexpr = (ArrayExpr *) node;
    2865             :                 ArrayExpr  *newnode;
    2866             : 
    2867       19214 :                 FLATCOPY(newnode, arrayexpr, ArrayExpr);
    2868       19214 :                 MUTATE(newnode->elements, arrayexpr->elements, List *);
    2869       19214 :                 return (Node *) newnode;
    2870             :             }
    2871             :             break;
    2872        4180 :         case T_RowExpr:
    2873             :             {
    2874        4180 :                 RowExpr    *rowexpr = (RowExpr *) node;
    2875             :                 RowExpr    *newnode;
    2876             : 
    2877        4180 :                 FLATCOPY(newnode, rowexpr, RowExpr);
    2878        4180 :                 MUTATE(newnode->args, rowexpr->args, List *);
    2879             :                 /* Assume colnames needn't be duplicated */
    2880        4180 :                 return (Node *) newnode;
    2881             :             }
    2882             :             break;
    2883         176 :         case T_RowCompareExpr:
    2884             :             {
    2885         176 :                 RowCompareExpr *rcexpr = (RowCompareExpr *) node;
    2886             :                 RowCompareExpr *newnode;
    2887             : 
    2888         176 :                 FLATCOPY(newnode, rcexpr, RowCompareExpr);
    2889         176 :                 MUTATE(newnode->largs, rcexpr->largs, List *);
    2890         176 :                 MUTATE(newnode->rargs, rcexpr->rargs, List *);
    2891         176 :                 return (Node *) newnode;
    2892             :             }
    2893             :             break;
    2894       52002 :         case T_CoalesceExpr:
    2895             :             {
    2896       52002 :                 CoalesceExpr *coalesceexpr = (CoalesceExpr *) node;
    2897             :                 CoalesceExpr *newnode;
    2898             : 
    2899       52002 :                 FLATCOPY(newnode, coalesceexpr, CoalesceExpr);
    2900       52002 :                 MUTATE(newnode->args, coalesceexpr->args, List *);
    2901       52002 :                 return (Node *) newnode;
    2902             :             }
    2903             :             break;
    2904         910 :         case T_MinMaxExpr:
    2905             :             {
    2906         910 :                 MinMaxExpr *minmaxexpr = (MinMaxExpr *) node;
    2907             :                 MinMaxExpr *newnode;
    2908             : 
    2909         910 :                 FLATCOPY(newnode, minmaxexpr, MinMaxExpr);
    2910         910 :                 MUTATE(newnode->args, minmaxexpr->args, List *);
    2911         910 :                 return (Node *) newnode;
    2912             :             }
    2913             :             break;
    2914         428 :         case T_XmlExpr:
    2915             :             {
    2916         428 :                 XmlExpr    *xexpr = (XmlExpr *) node;
    2917             :                 XmlExpr    *newnode;
    2918             : 
    2919         428 :                 FLATCOPY(newnode, xexpr, XmlExpr);
    2920         428 :                 MUTATE(newnode->named_args, xexpr->named_args, List *);
    2921             :                 /* assume mutator does not care about arg_names */
    2922         428 :                 MUTATE(newnode->args, xexpr->args, List *);
    2923         428 :                 return (Node *) newnode;
    2924             :             }
    2925             :             break;
    2926       25110 :         case T_NullTest:
    2927             :             {
    2928       25110 :                 NullTest   *ntest = (NullTest *) node;
    2929             :                 NullTest   *newnode;
    2930             : 
    2931       25110 :                 FLATCOPY(newnode, ntest, NullTest);
    2932       25110 :                 MUTATE(newnode->arg, ntest->arg, Expr *);
    2933       25110 :                 return (Node *) newnode;
    2934             :             }
    2935             :             break;
    2936         168 :         case T_BooleanTest:
    2937             :             {
    2938         168 :                 BooleanTest *btest = (BooleanTest *) node;
    2939             :                 BooleanTest *newnode;
    2940             : 
    2941         168 :                 FLATCOPY(newnode, btest, BooleanTest);
    2942         168 :                 MUTATE(newnode->arg, btest->arg, Expr *);
    2943         168 :                 return (Node *) newnode;
    2944             :             }
    2945             :             break;
    2946       11352 :         case T_CoerceToDomain:
    2947             :             {
    2948       11352 :                 CoerceToDomain *ctest = (CoerceToDomain *) node;
    2949             :                 CoerceToDomain *newnode;
    2950             : 
    2951       11352 :                 FLATCOPY(newnode, ctest, CoerceToDomain);
    2952       11352 :                 MUTATE(newnode->arg, ctest->arg, Expr *);
    2953       11352 :                 return (Node *) newnode;
    2954             :             }
    2955             :             break;
    2956     3018790 :         case T_TargetEntry:
    2957             :             {
    2958     3018790 :                 TargetEntry *targetentry = (TargetEntry *) node;
    2959             :                 TargetEntry *newnode;
    2960             : 
    2961     3018790 :                 FLATCOPY(newnode, targetentry, TargetEntry);
    2962     3018790 :                 MUTATE(newnode->expr, targetentry->expr, Expr *);
    2963     3017314 :                 return (Node *) newnode;
    2964             :             }
    2965             :             break;
    2966       46652 :         case T_Query:
    2967             :             /* Do nothing with a sub-Query, per discussion above */
    2968       46652 :             return node;
    2969           0 :         case T_WindowClause:
    2970             :             {
    2971           0 :                 WindowClause *wc = (WindowClause *) node;
    2972             :                 WindowClause *newnode;
    2973             : 
    2974           0 :                 FLATCOPY(newnode, wc, WindowClause);
    2975           0 :                 MUTATE(newnode->partitionClause, wc->partitionClause, List *);
    2976           0 :                 MUTATE(newnode->orderClause, wc->orderClause, List *);
    2977           0 :                 MUTATE(newnode->startOffset, wc->startOffset, Node *);
    2978           0 :                 MUTATE(newnode->endOffset, wc->endOffset, Node *);
    2979           0 :                 return (Node *) newnode;
    2980             :             }
    2981             :             break;
    2982          20 :         case T_CommonTableExpr:
    2983             :             {
    2984          20 :                 CommonTableExpr *cte = (CommonTableExpr *) node;
    2985             :                 CommonTableExpr *newnode;
    2986             : 
    2987          20 :                 FLATCOPY(newnode, cte, CommonTableExpr);
    2988             : 
    2989             :                 /*
    2990             :                  * Also invoke the mutator on the CTE's Query node, so it can
    2991             :                  * recurse into the sub-query if it wants to.
    2992             :                  */
    2993          20 :                 MUTATE(newnode->ctequery, cte->ctequery, Node *);
    2994          20 :                 return (Node *) newnode;
    2995             :             }
    2996             :             break;
    2997     3507142 :         case T_List:
    2998             :             {
    2999             :                 /*
    3000             :                  * We assume the mutator isn't interested in the list nodes
    3001             :                  * per se, so just invoke it on each list element. NOTE: this
    3002             :                  * would fail badly on a list with integer elements!
    3003             :                  */
    3004             :                 List       *resultlist;
    3005             :                 ListCell   *temp;
    3006             : 
    3007     3507142 :                 resultlist = NIL;
    3008    11185336 :                 foreach(temp, (List *) node)
    3009             :                 {
    3010     7678194 :                     resultlist = lappend(resultlist,
    3011     7679704 :                                          mutator((Node *) lfirst(temp),
    3012             :                                                  context));
    3013             :                 }
    3014     3505632 :                 return (Node *) resultlist;
    3015             :             }
    3016             :             break;
    3017       45664 :         case T_FromExpr:
    3018             :             {
    3019       45664 :                 FromExpr   *from = (FromExpr *) node;
    3020             :                 FromExpr   *newnode;
    3021             : 
    3022       45664 :                 FLATCOPY(newnode, from, FromExpr);
    3023       45664 :                 MUTATE(newnode->fromlist, from->fromlist, List *);
    3024       45664 :                 MUTATE(newnode->quals, from->quals, Node *);
    3025       45664 :                 return (Node *) newnode;
    3026             :             }
    3027             :             break;
    3028         232 :         case T_OnConflictExpr:
    3029             :             {
    3030         232 :                 OnConflictExpr *oc = (OnConflictExpr *) node;
    3031             :                 OnConflictExpr *newnode;
    3032             : 
    3033         232 :                 FLATCOPY(newnode, oc, OnConflictExpr);
    3034         232 :                 MUTATE(newnode->arbiterElems, oc->arbiterElems, List *);
    3035         232 :                 MUTATE(newnode->arbiterWhere, oc->arbiterWhere, Node *);
    3036         232 :                 MUTATE(newnode->onConflictSet, oc->onConflictSet, List *);
    3037         232 :                 MUTATE(newnode->onConflictWhere, oc->onConflictWhere, Node *);
    3038         232 :                 MUTATE(newnode->exclRelTlist, oc->exclRelTlist, List *);
    3039             : 
    3040         232 :                 return (Node *) newnode;
    3041             :             }
    3042             :             break;
    3043           0 :         case T_PartitionPruneStepOp:
    3044             :             {
    3045           0 :                 PartitionPruneStepOp *opstep = (PartitionPruneStepOp *) node;
    3046             :                 PartitionPruneStepOp *newnode;
    3047             : 
    3048           0 :                 FLATCOPY(newnode, opstep, PartitionPruneStepOp);
    3049           0 :                 MUTATE(newnode->exprs, opstep->exprs, List *);
    3050             : 
    3051           0 :                 return (Node *) newnode;
    3052             :             }
    3053             :             break;
    3054           0 :         case T_PartitionPruneStepCombine:
    3055             :             /* no expression sub-nodes */
    3056           0 :             return (Node *) copyObject(node);
    3057        1934 :         case T_JoinExpr:
    3058             :             {
    3059        1934 :                 JoinExpr   *join = (JoinExpr *) node;
    3060             :                 JoinExpr   *newnode;
    3061             : 
    3062        1934 :                 FLATCOPY(newnode, join, JoinExpr);
    3063        1934 :                 MUTATE(newnode->larg, join->larg, Node *);
    3064        1934 :                 MUTATE(newnode->rarg, join->rarg, Node *);
    3065        1934 :                 MUTATE(newnode->quals, join->quals, Node *);
    3066             :                 /* We do not mutate alias or using by default */
    3067        1934 :                 return (Node *) newnode;
    3068             :             }
    3069             :             break;
    3070         120 :         case T_SetOperationStmt:
    3071             :             {
    3072         120 :                 SetOperationStmt *setop = (SetOperationStmt *) node;
    3073             :                 SetOperationStmt *newnode;
    3074             : 
    3075         120 :                 FLATCOPY(newnode, setop, SetOperationStmt);
    3076         120 :                 MUTATE(newnode->larg, setop->larg, Node *);
    3077         120 :                 MUTATE(newnode->rarg, setop->rarg, Node *);
    3078             :                 /* We do not mutate groupClauses by default */
    3079         120 :                 return (Node *) newnode;
    3080             :             }
    3081             :             break;
    3082        2000 :         case T_IndexClause:
    3083             :             {
    3084        2000 :                 IndexClause *iclause = (IndexClause *) node;
    3085             :                 IndexClause *newnode;
    3086             : 
    3087        2000 :                 FLATCOPY(newnode, iclause, IndexClause);
    3088        2000 :                 MUTATE(newnode->rinfo, iclause->rinfo, RestrictInfo *);
    3089        2000 :                 MUTATE(newnode->indexquals, iclause->indexquals, List *);
    3090        2000 :                 return (Node *) newnode;
    3091             :             }
    3092             :             break;
    3093        4614 :         case T_PlaceHolderVar:
    3094             :             {
    3095        4614 :                 PlaceHolderVar *phv = (PlaceHolderVar *) node;
    3096             :                 PlaceHolderVar *newnode;
    3097             : 
    3098        4614 :                 FLATCOPY(newnode, phv, PlaceHolderVar);
    3099        4614 :                 MUTATE(newnode->phexpr, phv->phexpr, Expr *);
    3100             :                 /* Assume we need not copy the relids bitmapset */
    3101        4614 :                 return (Node *) newnode;
    3102             :             }
    3103             :             break;
    3104        1382 :         case T_InferenceElem:
    3105             :             {
    3106        1382 :                 InferenceElem *inferenceelemdexpr = (InferenceElem *) node;
    3107             :                 InferenceElem *newnode;
    3108             : 
    3109        1382 :                 FLATCOPY(newnode, inferenceelemdexpr, InferenceElem);
    3110        1382 :                 MUTATE(newnode->expr, newnode->expr, Node *);
    3111        1382 :                 return (Node *) newnode;
    3112             :             }
    3113             :             break;
    3114        5464 :         case T_AppendRelInfo:
    3115             :             {
    3116        5464 :                 AppendRelInfo *appinfo = (AppendRelInfo *) node;
    3117             :                 AppendRelInfo *newnode;
    3118             : 
    3119        5464 :                 FLATCOPY(newnode, appinfo, AppendRelInfo);
    3120        5464 :                 MUTATE(newnode->translated_vars, appinfo->translated_vars, List *);
    3121             :                 /* Assume nothing need be done with parent_colnos[] */
    3122        5464 :                 return (Node *) newnode;
    3123             :             }
    3124             :             break;
    3125           0 :         case T_PlaceHolderInfo:
    3126             :             {
    3127           0 :                 PlaceHolderInfo *phinfo = (PlaceHolderInfo *) node;
    3128             :                 PlaceHolderInfo *newnode;
    3129             : 
    3130           0 :                 FLATCOPY(newnode, phinfo, PlaceHolderInfo);
    3131           0 :                 MUTATE(newnode->ph_var, phinfo->ph_var, PlaceHolderVar *);
    3132             :                 /* Assume we need not copy the relids bitmapsets */
    3133           0 :                 return (Node *) newnode;
    3134             :             }
    3135             :             break;
    3136      104646 :         case T_RangeTblFunction:
    3137             :             {
    3138      104646 :                 RangeTblFunction *rtfunc = (RangeTblFunction *) node;
    3139             :                 RangeTblFunction *newnode;
    3140             : 
    3141      104646 :                 FLATCOPY(newnode, rtfunc, RangeTblFunction);
    3142      104646 :                 MUTATE(newnode->funcexpr, rtfunc->funcexpr, Node *);
    3143             :                 /* Assume we need not copy the coldef info lists */
    3144      104646 :                 return (Node *) newnode;
    3145             :             }
    3146             :             break;
    3147         248 :         case T_TableSampleClause:
    3148             :             {
    3149         248 :                 TableSampleClause *tsc = (TableSampleClause *) node;
    3150             :                 TableSampleClause *newnode;
    3151             : 
    3152         248 :                 FLATCOPY(newnode, tsc, TableSampleClause);
    3153         248 :                 MUTATE(newnode->args, tsc->args, List *);
    3154         248 :                 MUTATE(newnode->repeatable, tsc->repeatable, Expr *);
    3155         248 :                 return (Node *) newnode;
    3156             :             }
    3157             :             break;
    3158         300 :         case T_TableFunc:
    3159             :             {
    3160         300 :                 TableFunc  *tf = (TableFunc *) node;
    3161             :                 TableFunc  *newnode;
    3162             : 
    3163         300 :                 FLATCOPY(newnode, tf, TableFunc);
    3164         300 :                 MUTATE(newnode->ns_uris, tf->ns_uris, List *);
    3165         300 :                 MUTATE(newnode->docexpr, tf->docexpr, Node *);
    3166         300 :                 MUTATE(newnode->rowexpr, tf->rowexpr, Node *);
    3167         300 :                 MUTATE(newnode->colexprs, tf->colexprs, List *);
    3168         300 :                 MUTATE(newnode->coldefexprs, tf->coldefexprs, List *);
    3169         300 :                 return (Node *) newnode;
    3170             :             }
    3171             :             break;
    3172           0 :         default:
    3173           0 :             elog(ERROR, "unrecognized node type: %d",
    3174             :                  (int) nodeTag(node));
    3175             :             break;
    3176             :     }
    3177             :     /* can't get here, but keep compiler happy */
    3178             :     return NULL;
    3179             : }
    3180             : 
    3181             : 
    3182             : /*
    3183             :  * query_tree_mutator --- initiate modification of a Query's expressions
    3184             :  *
    3185             :  * This routine exists just to reduce the number of places that need to know
    3186             :  * where all the expression subtrees of a Query are.  Note it can be used
    3187             :  * for starting a walk at top level of a Query regardless of whether the
    3188             :  * mutator intends to descend into subqueries.  It is also useful for
    3189             :  * descending into subqueries within a mutator.
    3190             :  *
    3191             :  * Some callers want to suppress mutating of certain items in the Query,
    3192             :  * typically because they need to process them specially, or don't actually
    3193             :  * want to recurse into subqueries.  This is supported by the flags argument,
    3194             :  * which is the bitwise OR of flag values to suppress mutating of
    3195             :  * indicated items.  (More flag bits may be added as needed.)
    3196             :  *
    3197             :  * Normally the Query node itself is copied, but some callers want it to be
    3198             :  * modified in-place; they must pass QTW_DONT_COPY_QUERY in flags.  All
    3199             :  * modified substructure is safely copied in any case.
    3200             :  */
    3201             : Query *
    3202       45596 : query_tree_mutator(Query *query,
    3203             :                    Node *(*mutator) (),
    3204             :                    void *context,
    3205             :                    int flags)
    3206             : {
    3207             :     Assert(query != NULL && IsA(query, Query));
    3208             : 
    3209       45596 :     if (!(flags & QTW_DONT_COPY_QUERY))
    3210             :     {
    3211             :         Query      *newquery;
    3212             : 
    3213       45596 :         FLATCOPY(newquery, query, Query);
    3214       45596 :         query = newquery;
    3215             :     }
    3216             : 
    3217       45596 :     MUTATE(query->targetList, query->targetList, List *);
    3218       45596 :     MUTATE(query->withCheckOptions, query->withCheckOptions, List *);
    3219       45596 :     MUTATE(query->onConflict, query->onConflict, OnConflictExpr *);
    3220       45596 :     MUTATE(query->returningList, query->returningList, List *);
    3221       45596 :     MUTATE(query->jointree, query->jointree, FromExpr *);
    3222       45596 :     MUTATE(query->setOperations, query->setOperations, Node *);
    3223       45596 :     MUTATE(query->havingQual, query->havingQual, Node *);
    3224       45596 :     MUTATE(query->limitOffset, query->limitOffset, Node *);
    3225       45596 :     MUTATE(query->limitCount, query->limitCount, Node *);
    3226             : 
    3227             :     /*
    3228             :      * Most callers aren't interested in SortGroupClause nodes since those
    3229             :      * don't contain actual expressions. However they do contain OIDs, which
    3230             :      * may be of interest to some mutators.
    3231             :      */
    3232             : 
    3233       45596 :     if ((flags & QTW_EXAMINE_SORTGROUP))
    3234             :     {
    3235           0 :         MUTATE(query->groupClause, query->groupClause, List *);
    3236           0 :         MUTATE(query->windowClause, query->windowClause, List *);
    3237           0 :         MUTATE(query->sortClause, query->sortClause, List *);
    3238           0 :         MUTATE(query->distinctClause, query->distinctClause, List *);
    3239             :     }
    3240             :     else
    3241             :     {
    3242             :         /*
    3243             :          * But we need to mutate the expressions under WindowClause nodes even
    3244             :          * if we're not interested in SortGroupClause nodes.
    3245             :          */
    3246             :         List       *resultlist;
    3247             :         ListCell   *temp;
    3248             : 
    3249       45596 :         resultlist = NIL;
    3250       45608 :         foreach(temp, query->windowClause)
    3251             :         {
    3252          12 :             WindowClause *wc = lfirst_node(WindowClause, temp);
    3253             :             WindowClause *newnode;
    3254             : 
    3255          12 :             FLATCOPY(newnode, wc, WindowClause);
    3256          12 :             MUTATE(newnode->startOffset, wc->startOffset, Node *);
    3257          12 :             MUTATE(newnode->endOffset, wc->endOffset, Node *);
    3258             : 
    3259          12 :             resultlist = lappend(resultlist, (Node *) newnode);
    3260             :         }
    3261       45596 :         query->windowClause = resultlist;
    3262             :     }
    3263             : 
    3264             :     /*
    3265             :      * groupingSets and rowMarks are not mutated:
    3266             :      *
    3267             :      * groupingSets contain only ressortgroup refs (integers) which are
    3268             :      * meaningless without the groupClause or tlist. Accordingly, any mutator
    3269             :      * that needs to care about them needs to handle them itself in its Query
    3270             :      * processing.
    3271             :      *
    3272             :      * rowMarks contains only rangetable indexes (and flags etc.) and
    3273             :      * therefore should be handled at Query level similarly.
    3274             :      */
    3275             : 
    3276       45596 :     if (!(flags & QTW_IGNORE_CTE_SUBQUERIES))
    3277       42730 :         MUTATE(query->cteList, query->cteList, List *);
    3278             :     else                        /* else copy CTE list as-is */
    3279        2866 :         query->cteList = copyObject(query->cteList);
    3280       45596 :     query->rtable = range_table_mutator(query->rtable,
    3281             :                                         mutator, context, flags);
    3282       45596 :     return query;
    3283             : }
    3284             : 
    3285             : /*
    3286             :  * range_table_mutator is just the part of query_tree_mutator that processes
    3287             :  * a query's rangetable.  This is split out since it can be useful on
    3288             :  * its own.
    3289             :  */
    3290             : List *
    3291       45596 : range_table_mutator(List *rtable,
    3292             :                     Node *(*mutator) (),
    3293             :                     void *context,
    3294             :                     int flags)
    3295             : {
    3296       45596 :     List       *newrt = NIL;
    3297             :     ListCell   *rt;
    3298             : 
    3299      113908 :     foreach(rt, rtable)
    3300             :     {
    3301       68312 :         RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
    3302             :         RangeTblEntry *newrte;
    3303             : 
    3304       68312 :         FLATCOPY(newrte, rte, RangeTblEntry);
    3305       68312 :         switch (rte->rtekind)
    3306             :         {
    3307       37458 :             case RTE_RELATION:
    3308       37458 :                 MUTATE(newrte->tablesample, rte->tablesample,
    3309             :                        TableSampleClause *);
    3310             :                 /* we don't bother to copy eref, aliases, etc; OK? */
    3311       37458 :                 break;
    3312        9552 :             case RTE_SUBQUERY:
    3313        9552 :                 if (!(flags & QTW_IGNORE_RT_SUBQUERIES))
    3314             :                 {
    3315        8464 :                     CHECKFLATCOPY(newrte->subquery, rte->subquery, Query);
    3316        8464 :                     MUTATE(newrte->subquery, newrte->subquery, Query *);
    3317             :                 }
    3318             :                 else
    3319             :                 {
    3320             :                     /* else, copy RT subqueries as-is */
    3321        1088 :                     newrte->subquery = copyObject(rte->subquery);
    3322             :                 }
    3323        9552 :                 break;
    3324        1886 :             case RTE_JOIN:
    3325        1886 :                 if (!(flags & QTW_IGNORE_JOINALIASES))
    3326          48 :                     MUTATE(newrte->joinaliasvars, rte->joinaliasvars, List *);
    3327             :                 else
    3328             :                 {
    3329             :                     /* else, copy join aliases as-is */
    3330        1838 :                     newrte->joinaliasvars = copyObject(rte->joinaliasvars);
    3331             :                 }
    3332        1886 :                 break;
    3333       19096 :             case RTE_FUNCTION:
    3334       19096 :                 MUTATE(newrte->functions, rte->functions, List *);
    3335       19096 :                 break;
    3336           0 :             case RTE_TABLEFUNC:
    3337           0 :                 MUTATE(newrte->tablefunc, rte->tablefunc, TableFunc *);
    3338           0 :                 break;
    3339         232 :             case RTE_VALUES:
    3340         232 :                 MUTATE(newrte->values_lists, rte->values_lists, List *);
    3341         232 :                 break;
    3342          88 :             case RTE_CTE:
    3343             :             case RTE_NAMEDTUPLESTORE:
    3344             :             case RTE_RESULT:
    3345             :                 /* nothing to do */
    3346          88 :                 break;
    3347             :         }
    3348       68312 :         MUTATE(newrte->securityQuals, rte->securityQuals, List *);
    3349       68312 :         newrt = lappend(newrt, newrte);
    3350             :     }
    3351       45596 :     return newrt;
    3352             : }
    3353             : 
    3354             : /*
    3355             :  * query_or_expression_tree_walker --- hybrid form
    3356             :  *
    3357             :  * This routine will invoke query_tree_walker if called on a Query node,
    3358             :  * else will invoke the walker directly.  This is a useful way of starting
    3359             :  * the recursion when the walker's normal change of state is not appropriate
    3360             :  * for the outermost Query node.
    3361             :  */
    3362             : bool
    3363     3207818 : query_or_expression_tree_walker(Node *node,
    3364             :                                 bool (*walker) (),
    3365             :                                 void *context,
    3366             :                                 int flags)
    3367             : {
    3368     3207818 :     if (node && IsA(node, Query))
    3369      333430 :         return query_tree_walker((Query *) node,
    3370             :                                  walker,
    3371             :                                  context,
    3372             :                                  flags);
    3373             :     else
    3374     2874388 :         return walker(node, context);
    3375             : }
    3376             : 
    3377             : /*
    3378             :  * query_or_expression_tree_mutator --- hybrid form
    3379             :  *
    3380             :  * This routine will invoke query_tree_mutator if called on a Query node,
    3381             :  * else will invoke the mutator directly.  This is a useful way of starting
    3382             :  * the recursion when the mutator's normal change of state is not appropriate
    3383             :  * for the outermost Query node.
    3384             :  */
    3385             : Node *
    3386      181892 : query_or_expression_tree_mutator(Node *node,
    3387             :                                  Node *(*mutator) (),
    3388             :                                  void *context,
    3389             :                                  int flags)
    3390             : {
    3391      181892 :     if (node && IsA(node, Query))
    3392        2112 :         return (Node *) query_tree_mutator((Query *) node,
    3393             :                                            mutator,
    3394             :                                            context,
    3395             :                                            flags);
    3396             :     else
    3397      179780 :         return mutator(node, context);
    3398             : }
    3399             : 
    3400             : 
    3401             : /*
    3402             :  * raw_expression_tree_walker --- walk raw parse trees
    3403             :  *
    3404             :  * This has exactly the same API as expression_tree_walker, but instead of
    3405             :  * walking post-analysis parse trees, it knows how to walk the node types
    3406             :  * found in raw grammar output.  (There is not currently any need for a
    3407             :  * combined walker, so we keep them separate in the name of efficiency.)
    3408             :  * Unlike expression_tree_walker, there is no special rule about query
    3409             :  * boundaries: we descend to everything that's possibly interesting.
    3410             :  *
    3411             :  * Currently, the node type coverage here extends only to DML statements
    3412             :  * (SELECT/INSERT/UPDATE/DELETE) and nodes that can appear in them, because
    3413             :  * this is used mainly during analysis of CTEs, and only DML statements can
    3414             :  * appear in CTEs.
    3415             :  */
    3416             : bool
    3417    12776454 : raw_expression_tree_walker(Node *node,
    3418             :                            bool (*walker) (),
    3419             :                            void *context)
    3420             : {
    3421             :     ListCell   *temp;
    3422             : 
    3423             :     /*
    3424             :      * The walker has already visited the current node, and so we need only
    3425             :      * recurse into any sub-nodes it has.
    3426             :      */
    3427    12776454 :     if (node == NULL)
    3428           0 :         return false;
    3429             : 
    3430             :     /* Guard against stack overflow due to overly complex expressions */
    3431    12776454 :     check_stack_depth();
    3432             : 
    3433    12776454 :     switch (nodeTag(node))
    3434             :     {
    3435     1448160 :         case T_SetToDefault:
    3436             :         case T_CurrentOfExpr:
    3437             :         case T_SQLValueFunction:
    3438             :         case T_Integer:
    3439             :         case T_Float:
    3440             :         case T_String:
    3441             :         case T_BitString:
    3442             :         case T_Null:
    3443             :         case T_ParamRef:
    3444             :         case T_A_Const:
    3445             :         case T_A_Star:
    3446             :             /* primitive node types with no subnodes */
    3447     1448160 :             break;
    3448      391484 :         case T_Alias:
    3449             :             /* we assume the colnames list isn't interesting */
    3450      391484 :             break;
    3451      568794 :         case T_RangeVar:
    3452      568794 :             return walker(((RangeVar *) node)->alias, context);
    3453         220 :         case T_GroupingFunc:
    3454         220 :             return walker(((GroupingFunc *) node)->args, context);
    3455       82860 :         case T_SubLink:
    3456       82860 :             {
    3457       82860 :                 SubLink    *sublink = (SubLink *) node;
    3458             : 
    3459       82860 :                 if (walker(sublink->testexpr, context))
    3460           0 :                     return true;
    3461             :                 /* we assume the operName is not interesting */
    3462       82860 :                 if (walker(sublink->subselect, context))
    3463           0 :                     return true;
    3464             :             }
    3465       82860 :             break;
    3466       80576 :         case T_CaseExpr:
    3467       80576 :             {
    3468       80576 :                 CaseExpr   *caseexpr = (CaseExpr *) node;
    3469             : 
    3470       80576 :                 if (walker(caseexpr->arg, context))
    3471           0 :                     return true;
    3472             :                 /* we assume walker doesn't care about CaseWhens, either */
    3473      211700 :                 foreach(temp, caseexpr->args)
    3474             :                 {
    3475      131124 :                     CaseWhen   *when = lfirst_node(CaseWhen, temp);
    3476             : 
    3477      131124 :                     if (walker(when->expr, context))
    3478           0 :                         return true;
    3479      131124 :                     if (walker(when->result, context))
    3480           0 :                         return true;
    3481             :                 }
    3482       80576 :                 if (walker(caseexpr->defresult, context))
    3483           0 :                     return true;
    3484             :             }
    3485       80576 :             break;
    3486        7070 :         case T_RowExpr:
    3487             :             /* Assume colnames isn't interesting */
    3488        7070 :             return walker(((RowExpr *) node)->args, context);
    3489       53822 :         case T_CoalesceExpr:
    3490       53822 :             return walker(((CoalesceExpr *) node)->args, context);
    3491         214 :         case T_MinMaxExpr:
    3492         214 :             return walker(((MinMaxExpr *) node)->args, context);
    3493         418 :         case T_XmlExpr:
    3494         418 :             {
    3495         418 :                 XmlExpr    *xexpr = (XmlExpr *) node;
    3496             : 
    3497         418 :                 if (walker(xexpr->named_args, context))
    3498           0 :                     return true;
    3499             :                 /* we assume walker doesn't care about arg_names */
    3500         418 :                 if (walker(xexpr->args, context))
    3501           0 :                     return true;
    3502             :             }
    3503         418 :             break;
    3504       21692 :         case T_NullTest:
    3505       21692 :             return walker(((NullTest *) node)->arg, context);
    3506         118 :         case T_BooleanTest:
    3507         118 :             return walker(((BooleanTest *) node)->arg, context);
    3508      106818 :         case T_JoinExpr:
    3509      106818 :             {
    3510      106818 :                 JoinExpr   *join = (JoinExpr *) node;
    3511             : 
    3512      106818 :                 if (walker(join->larg, context))
    3513           0 :                     return true;
    3514      106818 :                 if (walker(join->rarg, context))
    3515           0 :                     return true;
    3516      106818 :                 if (walker(join->quals, context))
    3517           0 :                     return true;
    3518      106818 :                 if (walker(join->alias, context))
    3519           0 :                     return true;
    3520             :                 /* using list is deemed uninteresting */
    3521             :             }
    3522      106818 :             break;
    3523          20 :         case T_IntoClause:
    3524          20 :             {
    3525          20 :                 IntoClause *into = (IntoClause *) node;
    3526             : 
    3527          20 :                 if (walker(into->rel, context))
    3528           0 :                     return true;
    3529             :                 /* colNames, options are deemed uninteresting */
    3530             :                 /* viewQuery should be null in raw parsetree, but check it */
    3531          20 :                 if (walker(into->viewQuery, context))
    3532           0 :                     return true;
    3533             :             }
    3534          20 :             break;
    3535     2239464 :         case T_List:
    3536     6675024 :             foreach(temp, (List *) node)
    3537             :             {
    3538     4435616 :                 if (walker((Node *) lfirst(temp), context))
    3539           0 :                     return true;
    3540             :             }
    3541     2239408 :             break;
    3542       61036 :         case T_InsertStmt:
    3543       61036 :             {
    3544       61036 :                 InsertStmt *stmt = (InsertStmt *) node;
    3545             : 
    3546       61036 :                 if (walker(stmt->relation, context))
    3547           0 :                     return true;
    3548       61036 :                 if (walker(stmt->cols, context))
    3549           0 :                     return true;
    3550       61036 :                 if (walker(stmt->selectStmt, context))
    3551           0 :                     return true;
    3552       61036 :                 if (walker(stmt->onConflictClause, context))
    3553           0 :                     return true;
    3554       61036 :                 if (walker(stmt->returningList, context))
    3555           0 :                     return true;
    3556       61036 :                 if (walker(stmt->withClause, context))
    3557           0 :                     return true;
    3558             :             }
    3559       61036 :             break;
    3560        3168 :         case T_DeleteStmt:
    3561        3168 :             {
    3562        3168 :                 DeleteStmt *stmt = (DeleteStmt *) node;
    3563             : 
    3564        3168 :                 if (walker(stmt->relation, context))
    3565           0 :                     return true;
    3566        3168 :                 if (walker(stmt->usingClause, context))
    3567           0 :                     return true;
    3568        3168 :                 if (walker(stmt->whereClause, context))
    3569           0 :                     return true;
    3570        3168 :                 if (walker(stmt->returningList, context))
    3571           0 :                     return true;
    3572        3168 :                 if (walker(stmt->withClause, context))
    3573           0 :                     return true;
    3574             :             }
    3575        3168 :             break;
    3576       10032 :         case T_UpdateStmt:
    3577       10032 :             {
    3578       10032 :                 UpdateStmt *stmt = (UpdateStmt *) node;
    3579             : 
    3580       10032 :                 if (walker(stmt->relation, context))
    3581           0 :                     return true;
    3582       10032 :                 if (walker(stmt->targetList, context))
    3583           0 :                     return true;
    3584       10032 :                 if (walker(stmt->whereClause, context))
    3585           0 :                     return true;
    3586       10032 :                 if (walker(stmt->fromClause, context))
    3587           0 :                     return true;
    3588       10032 :                 if (walker(stmt->returningList, context))
    3589           0 :                     return true;
    3590       10032 :                 if (walker(stmt->withClause, context))
    3591           0 :                     return true;
    3592             :             }
    3593       10032 :             break;
    3594      614912 :         case T_SelectStmt:
    3595      614848 :             {
    3596      614912 :                 SelectStmt *stmt = (SelectStmt *) node;
    3597             : 
    3598      614912 :                 if (walker(stmt->distinctClause, context))
    3599           0 :                     return true;
    3600      614912 :                 if (walker(stmt->intoClause, context))
    3601           0 :                     return true;
    3602      614912 :                 if (walker(stmt->targetList, context))
    3603           0 :                     return true;
    3604      614908 :                 if (walker(stmt->fromClause, context))
    3605           0 :                     return true;
    3606      614860 :                 if (walker(stmt->whereClause, context))
    3607           0 :                     return true;
    3608      614856 :                 if (walker(stmt->groupClause, context))
    3609           0 :                     return true;
    3610      614856 :                 if (walker(stmt->havingClause, context))
    3611           0 :                     return true;
    3612      614856 :                 if (walker(stmt->windowClause, context))
    3613           0 :                     return true;
    3614      614856 :                 if (walker(stmt->valuesLists, context))
    3615           0 :                     return true;
    3616      614856 :                 if (walker(stmt->sortClause, context))
    3617           0 :                     return true;
    3618      614856 :                 if (walker(stmt->limitOffset, context))
    3619           0 :                     return true;
    3620      614856 :                 if (walker(stmt->limitCount, context))
    3621           0 :                     return true;
    3622      614856 :                 if (walker(stmt->lockingClause, context))
    3623           0 :                     return true;
    3624      614856 :                 if (walker(stmt->withClause, context))
    3625           0 :                     return true;
    3626      614856 :                 if (walker(stmt->larg, context))
    3627           0 :                     return true;
    3628      614856 :                 if (walker(stmt->rarg, context))
    3629           0 :                     return true;
    3630             :             }
    3631      614848 :             break;
    3632      742476 :         case T_A_Expr:
    3633      742476 :             {
    3634      742476 :                 A_Expr     *expr = (A_Expr *) node;
    3635             : 
    3636      742476 :                 if (walker(expr->lexpr, context))
    3637           0 :                     return true;
    3638      742476 :                 if (walker(expr->rexpr, context))
    3639           0 :                     return true;
    3640             :                 /* operator name is deemed uninteresting */
    3641             :             }
    3642      742476 :             break;
    3643      223638 :         case T_BoolExpr:
    3644      223638 :             {
    3645      223638 :                 BoolExpr   *expr = (BoolExpr *) node;
    3646             : 
    3647      223638 :                 if (walker(expr->args, context))
    3648           0 :                     return true;
    3649             :             }
    3650      223638 :             break;
    3651     2595390 :         case T_ColumnRef:
    3652             :             /* we assume the fields contain nothing interesting */
    3653     2595390 :             break;
    3654      579950 :         case T_FuncCall:
    3655      579950 :             {
    3656      579950 :                 FuncCall   *fcall = (FuncCall *) node;
    3657             : 
    3658      579950 :                 if (walker(fcall->args, context))
    3659           0 :                     return true;
    3660      579950 :                 if (walker(fcall->agg_order, context))
    3661           0 :                     return true;
    3662      579950 :                 if (walker(fcall->agg_filter, context))
    3663           0 :                     return true;
    3664      579950 :                 if (walker(fcall->over, context))
    3665           0 :                     return true;
    3666             :                 /* function name is deemed uninteresting */
    3667             :             }
    3668      579950 :             break;
    3669         740 :         case T_NamedArgExpr:
    3670         740 :             return walker(((NamedArgExpr *) node)->arg, context);
    3671        7598 :         case T_A_Indices:
    3672        7598 :             {
    3673        7598 :                 A_Indices  *indices = (A_Indices *) node;
    3674             : 
    3675        7598 :                 if (walker(indices->lidx, context))
    3676           0 :                     return true;
    3677        7598 :                 if (walker(indices->uidx, context))
    3678           0 :                     return true;
    3679             :             }
    3680        7598 :             break;
    3681       30696 :         case T_A_Indirection:
    3682       30696 :             {
    3683       30696 :                 A_Indirection *indir = (A_Indirection *) node;
    3684             : 
    3685       30696 :                 if (walker(indir->arg, context))
    3686           0 :                     return true;
    3687       30696 :                 if (walker(indir->indirection, context))
    3688           0 :                     return true;
    3689             :             }
    3690       30696 :             break;
    3691        4732 :         case T_A_ArrayExpr:
    3692        4732 :             return walker(((A_ArrayExpr *) node)->elements, context);
    3693     1733076 :         case T_ResTarget:
    3694     1733072 :             {
    3695     1733076 :                 ResTarget  *rt = (ResTarget *) node;
    3696             : 
    3697     1733076 :                 if (walker(rt->indirection, context))
    3698           0 :                     return true;
    3699     1733076 :                 if (walker(rt->val, context))
    3700           0 :                     return true;
    3701             :             }
    3702     1733072 :             break;
    3703         206 :         case T_MultiAssignRef:
    3704         206 :             return walker(((MultiAssignRef *) node)->source, context);
    3705      471776 :         case T_TypeCast:
    3706      471776 :             {
    3707      471776 :                 TypeCast   *tc = (TypeCast *) node;
    3708             : 
    3709      471776 :                 if (walker(tc->arg, context))
    3710           0 :                     return true;
    3711      471776 :                 if (walker(tc->typeName, context))
    3712           0 :                     return true;
    3713             :             }
    3714      471776 :             break;
    3715        2860 :         case T_CollateClause:
    3716        2860 :             return walker(((CollateClause *) node)->arg, context);
    3717       83636 :         case T_SortBy:
    3718       83636 :             return walker(((SortBy *) node)->node, context);
    3719        2202 :         case T_WindowDef:
    3720        2202 :             {
    3721        2202 :                 WindowDef  *wd = (WindowDef *) node;
    3722             : 
    3723        2202 :                 if (walker(wd->partitionClause, context))
    3724           0 :                     return true;
    3725        2202 :                 if (walker(wd->orderClause, context))
    3726           0 :                     return true;
    3727        2202 :                 if (walker(wd->startOffset, context))
    3728           0 :                     return true;
    3729        2202 :                 if (walker(wd->endOffset, context))
    3730           0 :                     return true;
    3731             :             }
    3732        2202 :             break;
    3733       34546 :         case T_RangeSubselect:
    3734       34542 :             {
    3735       34546 :                 RangeSubselect *rs = (RangeSubselect *) node;
    3736             : 
    3737       34546 :                 if (walker(rs->subquery, context))
    3738           0 :                     return true;
    3739       34542 :                 if (walker(rs->alias, context))
    3740           0 :                     return true;
    3741             :             }
    3742       34542 :             break;
    3743       85706 :         case T_RangeFunction:
    3744       85706 :             {
    3745       85706 :                 RangeFunction *rf = (RangeFunction *) node;
    3746             : 
    3747       85706 :                 if (walker(rf->functions, context))
    3748           0 :                     return true;
    3749       85706 :                 if (walker(rf->alias, context))
    3750           0 :                     return true;
    3751       85706 :                 if (walker(rf->coldeflist, context))
    3752           0 :                     return true;
    3753             :             }
    3754       85706 :             break;
    3755         208 :         case T_RangeTableSample:
    3756         208 :             {
    3757         208 :                 RangeTableSample *rts = (RangeTableSample *) node;
    3758             : 
    3759         208 :                 if (walker(rts->relation, context))
    3760           0 :                     return true;
    3761             :                 /* method name is deemed uninteresting */
    3762         208 :                 if (walker(rts->args, context))
    3763           0 :                     return true;
    3764         208 :                 if (walker(rts->repeatable, context))
    3765           0 :                     return true;
    3766             :             }
    3767         208 :             break;
    3768         148 :         case T_RangeTableFunc:
    3769         148 :             {
    3770         148 :                 RangeTableFunc *rtf = (RangeTableFunc *) node;
    3771             : 
    3772         148 :                 if (walker(rtf->docexpr, context))
    3773           0 :                     return true;
    3774         148 :                 if (walker(rtf->rowexpr, context))
    3775           0 :                     return true;
    3776         148 :                 if (walker(rtf->namespaces, context))
    3777           0 :                     return true;
    3778         148 :                 if (walker(rtf->columns, context))
    3779           0 :                     return true;
    3780         148 :                 if (walker(rtf->alias, context))
    3781           0 :                     return true;
    3782             :             }
    3783         148 :             break;
    3784         542 :         case T_RangeTableFuncCol:
    3785         542 :             {
    3786         542 :                 RangeTableFuncCol *rtfc = (RangeTableFuncCol *) node;
    3787             : 
    3788         542 :                 if (walker(rtfc->colexpr, context))
    3789           0 :                     return true;
    3790         542 :                 if (walker(rtfc->coldefexpr, context))
    3791           0 :                     return true;
    3792             :             }
    3793         542 :             break;
    3794      473190 :         case T_TypeName:
    3795      473190 :             {
    3796      473190 :                 TypeName   *tn = (TypeName *) node;
    3797             : 
    3798      473190 :                 if (walker(tn->typmods, context))
    3799           0 :                     return true;
    3800      473190 :                 if (walker(tn->arrayBounds, context))
    3801           0 :                     return true;
    3802             :                 /* type name itself is deemed uninteresting */
    3803             :             }
    3804      473190 :             break;
    3805        1358 :         case T_ColumnDef:
    3806        1358 :             {
    3807        1358 :                 ColumnDef  *coldef = (ColumnDef *) node;
    3808             : 
    3809        1358 :                 if (walker(coldef->typeName, context))
    3810           0 :                     return true;
    3811        1358 :                 if (walker(coldef->raw_default, context))
    3812           0 :                     return true;
    3813        1358 :                 if (walker(coldef->collClause, context))
    3814           0 :                     return true;
    3815             :                 /* for now, constraints are ignored */
    3816             :             }
    3817        1358 :             break;
    3818        1050 :         case T_IndexElem:
    3819        1050 :             {
    3820        1050 :                 IndexElem  *indelem = (IndexElem *) node;
    3821             : 
    3822        1050 :                 if (walker(indelem->expr, context))
    3823           0 :                     return true;
    3824             :                 /* collation and opclass names are deemed uninteresting */
    3825             :             }
    3826        1050 :             break;
    3827         756 :         case T_GroupingSet:
    3828         756 :             return walker(((GroupingSet *) node)->content, context);
    3829        3900 :         case T_LockingClause:
    3830        3900 :             return walker(((LockingClause *) node)->lockedRels, context);
    3831          24 :         case T_XmlSerialize:
    3832          24 :             {
    3833          24 :                 XmlSerialize *xs = (XmlSerialize *) node;
    3834             : 
    3835          24 :                 if (walker(xs->expr, context))
    3836           0 :                     return true;
    3837          24 :                 if (walker(xs->typeName, context))
    3838           0 :                     return true;
    3839             :             }
    3840          24 :             break;
    3841        1526 :         case T_WithClause:
    3842        1526 :             return walker(((WithClause *) node)->ctes, context);
    3843         886 :         case T_InferClause:
    3844         886 :             {
    3845         886 :                 InferClause *stmt = (InferClause *) node;
    3846             : 
    3847         886 :                 if (walker(stmt->indexElems, context))
    3848           0 :                     return true;
    3849         886 :                 if (walker(stmt->whereClause, context))
    3850           0 :                     return true;
    3851             :             }
    3852         886 :             break;
    3853        1022 :         case T_OnConflictClause:
    3854        1022 :             {
    3855        1022 :                 OnConflictClause *stmt = (OnConflictClause *) node;
    3856             : 
    3857        1022 :                 if (walker(stmt->infer, context))
    3858           0 :                     return true;
    3859        1022 :                 if (walker(stmt->targetList, context))
    3860           0 :                     return true;
    3861        1022 :                 if (walker(stmt->whereClause, context))
    3862           0 :                     return true;
    3863             :             }
    3864        1022 :             break;
    3865        1738 :         case T_CommonTableExpr:
    3866        1738 :             return walker(((CommonTableExpr *) node)->ctequery, context);
    3867           0 :         default:
    3868           0 :             elog(ERROR, "unrecognized node type: %d",
    3869             :                  (int) nodeTag(node));
    3870             :             break;
    3871             :     }
    3872    12024302 :     return false;
    3873             : }
    3874             : 
    3875             : /*
    3876             :  * planstate_tree_walker --- walk plan state trees
    3877             :  *
    3878             :  * The walker has already visited the current node, and so we need only
    3879             :  * recurse into any sub-nodes it has.
    3880             :  */
    3881             : bool
    3882      805800 : planstate_tree_walker(PlanState *planstate,
    3883             :                       bool (*walker) (),
    3884             :                       void *context)
    3885             : {
    3886      805800 :     Plan       *plan = planstate->plan;
    3887             :     ListCell   *lc;
    3888             : 
    3889             :     /* Guard against stack overflow due to overly complex plan trees */
    3890      805800 :     check_stack_depth();
    3891             : 
    3892             :     /* initPlan-s */
    3893      805800 :     if (planstate_walk_subplans(planstate->initPlan, walker, context))
    3894           0 :         return true;
    3895             : 
    3896             :     /* lefttree */
    3897      805800 :     if (outerPlanState(planstate))
    3898             :     {
    3899      209684 :         if (walker(outerPlanState(planstate), context))
    3900           0 :             return true;
    3901             :     }
    3902             : 
    3903             :     /* righttree */
    3904      805800 :     if (innerPlanState(planstate))
    3905             :     {
    3906       66682 :         if (walker(innerPlanState(planstate), context))
    3907           0 :             return true;
    3908             :     }
    3909             : 
    3910             :     /* special child plans */
    3911      805800 :     switch (nodeTag(plan))
    3912             :     {
    3913       86054 :         case T_ModifyTable:
    3914       86054 :             if (planstate_walk_members(((ModifyTableState *) planstate)->mt_plans,
    3915             :                                        ((ModifyTableState *) planstate)->mt_nplans,
    3916             :                                        walker, context))
    3917           0 :                 return true;
    3918       86054 :             break;
    3919        7542 :         case T_Append:
    3920        7542 :             if (planstate_walk_members(((AppendState *) planstate)->appendplans,
    3921             :                                        ((AppendState *) planstate)->as_nplans,
    3922             :                                        walker, context))
    3923           0 :                 return true;
    3924        7542 :             break;
    3925         282 :         case T_MergeAppend:
    3926         282 :             if (planstate_walk_members(((MergeAppendState *) planstate)->mergeplans,
    3927             :                                        ((MergeAppendState *) planstate)->ms_nplans,
    3928             :                                        walker, context))
    3929           0 :                 return true;
    3930         282 :             break;
    3931          40 :         case T_BitmapAnd:
    3932          40 :             if (planstate_walk_members(((BitmapAndState *) planstate)->bitmapplans,
    3933             :                                        ((BitmapAndState *) planstate)->nplans,
    3934             :                                        walker, context))
    3935           0 :                 return true;
    3936          40 :             break;
    3937         164 :         case T_BitmapOr:
    3938         164 :             if (planstate_walk_members(((BitmapOrState *) planstate)->bitmapplans,
    3939             :                                        ((BitmapOrState *) planstate)->nplans,
    3940             :                                        walker, context))
    3941           0 :                 return true;
    3942         164 :             break;
    3943        2376 :         case T_SubqueryScan:
    3944        2376 :             if (walker(((SubqueryScanState *) planstate)->subplan, context))
    3945           0 :                 return true;
    3946        2376 :             break;
    3947           0 :         case T_CustomScan:
    3948           0 :             foreach(lc, ((CustomScanState *) planstate)->custom_ps)
    3949             :             {
    3950           0 :                 if (walker((PlanState *) lfirst(lc), context))
    3951           0 :                     return true;
    3952             :             }
    3953           0 :             break;
    3954      709342 :         default:
    3955      709342 :             break;
    3956             :     }
    3957             : 
    3958             :     /* subPlan-s */
    3959      805800 :     if (planstate_walk_subplans(planstate->subPlan, walker, context))
    3960           0 :         return true;
    3961             : 
    3962      805800 :     return false;
    3963             : }
    3964             : 
    3965             : /*
    3966             :  * Walk a list of SubPlans (or initPlans, which also use SubPlan nodes).
    3967             :  */
    3968             : static bool
    3969     1611600 : planstate_walk_subplans(List *plans,
    3970             :                         bool (*walker) (),
    3971             :                         void *context)
    3972             : {
    3973             :     ListCell   *lc;
    3974             : 
    3975     1663728 :     foreach(lc, plans)
    3976             :     {
    3977       52128 :         SubPlanState *sps = lfirst_node(SubPlanState, lc);
    3978             : 
    3979       52128 :         if (walker(sps->planstate, context))
    3980           0 :             return true;
    3981             :     }
    3982             : 
    3983     1611600 :     return false;
    3984             : }
    3985             : 
    3986             : /*
    3987             :  * Walk the constituent plans of a ModifyTable, Append, MergeAppend,
    3988             :  * BitmapAnd, or BitmapOr node.
    3989             :  */
    3990             : static bool
    3991       94082 : planstate_walk_members(PlanState **planstates, int nplans,
    3992             :                        bool (*walker) (), void *context)
    3993             : {
    3994             :     int         j;
    3995             : 
    3996      204524 :     for (j = 0; j < nplans; j++)
    3997             :     {
    3998      110442 :         if (walker(planstates[j], context))
    3999           0 :             return true;
    4000             :     }
    4001             : 
    4002       94082 :     return false;
    4003             : }

Generated by: LCOV version 1.13