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

Generated by: LCOV version 1.14