]> granicus.if.org Git - postgresql/blob - src/backend/nodes/nodeFuncs.c
Per-column collation support
[postgresql] / src / backend / nodes / nodeFuncs.c
1 /*-------------------------------------------------------------------------
2  *
3  * nodeFuncs.c
4  *              Various general-purpose manipulations of Node trees
5  *
6  * Portions Copyright (c) 1996-2011, 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/nodeFuncs.h"
21 #include "nodes/relation.h"
22 #include "utils/builtins.h"
23 #include "utils/lsyscache.h"
24
25
26 static bool expression_returns_set_walker(Node *node, void *context);
27 static int      leftmostLoc(int loc1, int loc2);
28
29
30 /*
31  *      exprType -
32  *        returns the Oid of the type of the expression's result.
33  */
34 Oid
35 exprType(Node *expr)
36 {
37         Oid                     type;
38
39         if (!expr)
40                 return InvalidOid;
41
42         switch (nodeTag(expr))
43         {
44                 case T_Var:
45                         type = ((Var *) expr)->vartype;
46                         break;
47                 case T_Const:
48                         type = ((Const *) expr)->consttype;
49                         break;
50                 case T_Param:
51                         type = ((Param *) expr)->paramtype;
52                         break;
53                 case T_Aggref:
54                         type = ((Aggref *) expr)->aggtype;
55                         break;
56                 case T_WindowFunc:
57                         type = ((WindowFunc *) expr)->wintype;
58                         break;
59                 case T_ArrayRef:
60                         {
61                                 ArrayRef   *arrayref = (ArrayRef *) expr;
62
63                                 /* slice and/or store operations yield the array type */
64                                 if (arrayref->reflowerindexpr || arrayref->refassgnexpr)
65                                         type = arrayref->refarraytype;
66                                 else
67                                         type = arrayref->refelemtype;
68                         }
69                         break;
70                 case T_FuncExpr:
71                         type = ((FuncExpr *) expr)->funcresulttype;
72                         break;
73                 case T_NamedArgExpr:
74                         type = exprType((Node *) ((NamedArgExpr *) expr)->arg);
75                         break;
76                 case T_OpExpr:
77                         type = ((OpExpr *) expr)->opresulttype;
78                         break;
79                 case T_DistinctExpr:
80                         type = ((DistinctExpr *) expr)->opresulttype;
81                         break;
82                 case T_ScalarArrayOpExpr:
83                         type = BOOLOID;
84                         break;
85                 case T_BoolExpr:
86                         type = BOOLOID;
87                         break;
88                 case T_SubLink:
89                         {
90                                 SubLink    *sublink = (SubLink *) expr;
91
92                                 if (sublink->subLinkType == EXPR_SUBLINK ||
93                                         sublink->subLinkType == ARRAY_SUBLINK)
94                                 {
95                                         /* get the type of the subselect's first target column */
96                                         Query      *qtree = (Query *) sublink->subselect;
97                                         TargetEntry *tent;
98
99                                         if (!qtree || !IsA(qtree, Query))
100                                                 elog(ERROR, "cannot get type for untransformed sublink");
101                                         tent = (TargetEntry *) linitial(qtree->targetList);
102                                         Assert(IsA(tent, TargetEntry));
103                                         Assert(!tent->resjunk);
104                                         type = exprType((Node *) tent->expr);
105                                         if (sublink->subLinkType == ARRAY_SUBLINK)
106                                         {
107                                                 type = get_array_type(type);
108                                                 if (!OidIsValid(type))
109                                                         ereport(ERROR,
110                                                                         (errcode(ERRCODE_UNDEFINED_OBJECT),
111                                                                          errmsg("could not find array type for data type %s",
112                                                         format_type_be(exprType((Node *) tent->expr)))));
113                                         }
114                                 }
115                                 else
116                                 {
117                                         /* for all other sublink types, result is boolean */
118                                         type = BOOLOID;
119                                 }
120                         }
121                         break;
122                 case T_SubPlan:
123                         {
124                                 SubPlan    *subplan = (SubPlan *) expr;
125
126                                 if (subplan->subLinkType == EXPR_SUBLINK ||
127                                         subplan->subLinkType == ARRAY_SUBLINK)
128                                 {
129                                         /* get the type of the subselect's first target column */
130                                         type = subplan->firstColType;
131                                         if (subplan->subLinkType == ARRAY_SUBLINK)
132                                         {
133                                                 type = get_array_type(type);
134                                                 if (!OidIsValid(type))
135                                                         ereport(ERROR,
136                                                                         (errcode(ERRCODE_UNDEFINED_OBJECT),
137                                                                          errmsg("could not find array type for data type %s",
138                                                                         format_type_be(subplan->firstColType))));
139                                         }
140                                 }
141                                 else
142                                 {
143                                         /* for all other subplan types, result is boolean */
144                                         type = BOOLOID;
145                                 }
146                         }
147                         break;
148                 case T_AlternativeSubPlan:
149                         {
150                                 AlternativeSubPlan *asplan = (AlternativeSubPlan *) expr;
151
152                                 /* subplans should all return the same thing */
153                                 type = exprType((Node *) linitial(asplan->subplans));
154                         }
155                         break;
156                 case T_FieldSelect:
157                         type = ((FieldSelect *) expr)->resulttype;
158                         break;
159                 case T_FieldStore:
160                         type = ((FieldStore *) expr)->resulttype;
161                         break;
162                 case T_RelabelType:
163                         type = ((RelabelType *) expr)->resulttype;
164                         break;
165                 case T_CollateClause:
166                         type = exprType((Node *) ((CollateClause *) expr)->arg);
167                         break;
168                 case T_CoerceViaIO:
169                         type = ((CoerceViaIO *) expr)->resulttype;
170                         break;
171                 case T_ArrayCoerceExpr:
172                         type = ((ArrayCoerceExpr *) expr)->resulttype;
173                         break;
174                 case T_ConvertRowtypeExpr:
175                         type = ((ConvertRowtypeExpr *) expr)->resulttype;
176                         break;
177                 case T_CaseExpr:
178                         type = ((CaseExpr *) expr)->casetype;
179                         break;
180                 case T_CaseTestExpr:
181                         type = ((CaseTestExpr *) expr)->typeId;
182                         break;
183                 case T_ArrayExpr:
184                         type = ((ArrayExpr *) expr)->array_typeid;
185                         break;
186                 case T_RowExpr:
187                         type = ((RowExpr *) expr)->row_typeid;
188                         break;
189                 case T_RowCompareExpr:
190                         type = BOOLOID;
191                         break;
192                 case T_CoalesceExpr:
193                         type = ((CoalesceExpr *) expr)->coalescetype;
194                         break;
195                 case T_MinMaxExpr:
196                         type = ((MinMaxExpr *) expr)->minmaxtype;
197                         break;
198                 case T_XmlExpr:
199                         if (((XmlExpr *) expr)->op == IS_DOCUMENT)
200                                 type = BOOLOID;
201                         else if (((XmlExpr *) expr)->op == IS_XMLSERIALIZE)
202                                 type = TEXTOID;
203                         else
204                                 type = XMLOID;
205                         break;
206                 case T_NullIfExpr:
207                         type = exprType((Node *) linitial(((NullIfExpr *) expr)->args));
208                         break;
209                 case T_NullTest:
210                         type = BOOLOID;
211                         break;
212                 case T_BooleanTest:
213                         type = BOOLOID;
214                         break;
215                 case T_CoerceToDomain:
216                         type = ((CoerceToDomain *) expr)->resulttype;
217                         break;
218                 case T_CoerceToDomainValue:
219                         type = ((CoerceToDomainValue *) expr)->typeId;
220                         break;
221                 case T_SetToDefault:
222                         type = ((SetToDefault *) expr)->typeId;
223                         break;
224                 case T_CurrentOfExpr:
225                         type = BOOLOID;
226                         break;
227                 case T_PlaceHolderVar:
228                         type = exprType((Node *) ((PlaceHolderVar *) expr)->phexpr);
229                         break;
230                 default:
231                         elog(ERROR, "unrecognized node type: %d", (int) nodeTag(expr));
232                         type = InvalidOid;      /* keep compiler quiet */
233                         break;
234         }
235         return type;
236 }
237
238 /*
239  *      exprTypmod -
240  *        returns the type-specific modifier of the expression's result type,
241  *        if it can be determined.      In many cases, it can't and we return -1.
242  */
243 int32
244 exprTypmod(Node *expr)
245 {
246         if (!expr)
247                 return -1;
248
249         switch (nodeTag(expr))
250         {
251                 case T_Var:
252                         return ((Var *) expr)->vartypmod;
253                 case T_Const:
254                         return ((Const *) expr)->consttypmod;
255                 case T_Param:
256                         return ((Param *) expr)->paramtypmod;
257                 case T_ArrayRef:
258                         /* typmod is the same for array or element */
259                         return ((ArrayRef *) expr)->reftypmod;
260                 case T_FuncExpr:
261                         {
262                                 int32           coercedTypmod;
263
264                                 /* Be smart about length-coercion functions... */
265                                 if (exprIsLengthCoercion(expr, &coercedTypmod))
266                                         return coercedTypmod;
267                         }
268                         break;
269                 case T_NamedArgExpr:
270                         return exprTypmod((Node *) ((NamedArgExpr *) expr)->arg);
271                 case T_SubLink:
272                         {
273                                 SubLink    *sublink = (SubLink *) expr;
274
275                                 if (sublink->subLinkType == EXPR_SUBLINK ||
276                                         sublink->subLinkType == ARRAY_SUBLINK)
277                                 {
278                                         /* get the typmod of the subselect's first target column */
279                                         Query      *qtree = (Query *) sublink->subselect;
280                                         TargetEntry *tent;
281
282                                         if (!qtree || !IsA(qtree, Query))
283                                                 elog(ERROR, "cannot get type for untransformed sublink");
284                                         tent = (TargetEntry *) linitial(qtree->targetList);
285                                         Assert(IsA(tent, TargetEntry));
286                                         Assert(!tent->resjunk);
287                                         return exprTypmod((Node *) tent->expr);
288                                         /* note we don't need to care if it's an array */
289                                 }
290                         }
291                         break;
292                 case T_SubPlan:
293                         {
294                                 SubPlan    *subplan = (SubPlan *) expr;
295
296                                 if (subplan->subLinkType == EXPR_SUBLINK ||
297                                         subplan->subLinkType == ARRAY_SUBLINK)
298                                 {
299                                         /* get the typmod of the subselect's first target column */
300                                         /* note we don't need to care if it's an array */
301                                         return subplan->firstColTypmod;
302                                 }
303                                 else
304                                 {
305                                         /* for all other subplan types, result is boolean */
306                                         return -1;
307                                 }
308                         }
309                         break;
310                 case T_AlternativeSubPlan:
311                         {
312                                 AlternativeSubPlan *asplan = (AlternativeSubPlan *) expr;
313
314                                 /* subplans should all return the same thing */
315                                 return exprTypmod((Node *) linitial(asplan->subplans));
316                         }
317                         break;
318                 case T_FieldSelect:
319                         return ((FieldSelect *) expr)->resulttypmod;
320                 case T_RelabelType:
321                         return ((RelabelType *) expr)->resulttypmod;
322                 case T_ArrayCoerceExpr:
323                         return ((ArrayCoerceExpr *) expr)->resulttypmod;
324                 case T_CaseExpr:
325                         {
326                                 /*
327                                  * If all the alternatives agree on type/typmod, return that
328                                  * typmod, else use -1
329                                  */
330                                 CaseExpr   *cexpr = (CaseExpr *) expr;
331                                 Oid                     casetype = cexpr->casetype;
332                                 int32           typmod;
333                                 ListCell   *arg;
334
335                                 if (!cexpr->defresult)
336                                         return -1;
337                                 if (exprType((Node *) cexpr->defresult) != casetype)
338                                         return -1;
339                                 typmod = exprTypmod((Node *) cexpr->defresult);
340                                 if (typmod < 0)
341                                         return -1;      /* no point in trying harder */
342                                 foreach(arg, cexpr->args)
343                                 {
344                                         CaseWhen   *w = (CaseWhen *) lfirst(arg);
345
346                                         Assert(IsA(w, CaseWhen));
347                                         if (exprType((Node *) w->result) != casetype)
348                                                 return -1;
349                                         if (exprTypmod((Node *) w->result) != typmod)
350                                                 return -1;
351                                 }
352                                 return typmod;
353                         }
354                         break;
355                 case T_CaseTestExpr:
356                         return ((CaseTestExpr *) expr)->typeMod;
357                 case T_ArrayExpr:
358                         {
359                                 /*
360                                  * If all the elements agree on type/typmod, return that
361                                  * typmod, else use -1
362                                  */
363                                 ArrayExpr  *arrayexpr = (ArrayExpr *) expr;
364                                 Oid                     commontype;
365                                 int32           typmod;
366                                 ListCell   *elem;
367
368                                 if (arrayexpr->elements == NIL)
369                                         return -1;
370                                 typmod = exprTypmod((Node *) linitial(arrayexpr->elements));
371                                 if (typmod < 0)
372                                         return -1;      /* no point in trying harder */
373                                 if (arrayexpr->multidims)
374                                         commontype = arrayexpr->array_typeid;
375                                 else
376                                         commontype = arrayexpr->element_typeid;
377                                 foreach(elem, arrayexpr->elements)
378                                 {
379                                         Node       *e = (Node *) lfirst(elem);
380
381                                         if (exprType(e) != commontype)
382                                                 return -1;
383                                         if (exprTypmod(e) != typmod)
384                                                 return -1;
385                                 }
386                                 return typmod;
387                         }
388                         break;
389                 case T_CoalesceExpr:
390                         {
391                                 /*
392                                  * If all the alternatives agree on type/typmod, return that
393                                  * typmod, else use -1
394                                  */
395                                 CoalesceExpr *cexpr = (CoalesceExpr *) expr;
396                                 Oid                     coalescetype = cexpr->coalescetype;
397                                 int32           typmod;
398                                 ListCell   *arg;
399
400                                 if (exprType((Node *) linitial(cexpr->args)) != coalescetype)
401                                         return -1;
402                                 typmod = exprTypmod((Node *) linitial(cexpr->args));
403                                 if (typmod < 0)
404                                         return -1;      /* no point in trying harder */
405                                 for_each_cell(arg, lnext(list_head(cexpr->args)))
406                                 {
407                                         Node       *e = (Node *) lfirst(arg);
408
409                                         if (exprType(e) != coalescetype)
410                                                 return -1;
411                                         if (exprTypmod(e) != typmod)
412                                                 return -1;
413                                 }
414                                 return typmod;
415                         }
416                         break;
417                 case T_MinMaxExpr:
418                         {
419                                 /*
420                                  * If all the alternatives agree on type/typmod, return that
421                                  * typmod, else use -1
422                                  */
423                                 MinMaxExpr *mexpr = (MinMaxExpr *) expr;
424                                 Oid                     minmaxtype = mexpr->minmaxtype;
425                                 int32           typmod;
426                                 ListCell   *arg;
427
428                                 if (exprType((Node *) linitial(mexpr->args)) != minmaxtype)
429                                         return -1;
430                                 typmod = exprTypmod((Node *) linitial(mexpr->args));
431                                 if (typmod < 0)
432                                         return -1;      /* no point in trying harder */
433                                 for_each_cell(arg, lnext(list_head(mexpr->args)))
434                                 {
435                                         Node       *e = (Node *) lfirst(arg);
436
437                                         if (exprType(e) != minmaxtype)
438                                                 return -1;
439                                         if (exprTypmod(e) != typmod)
440                                                 return -1;
441                                 }
442                                 return typmod;
443                         }
444                         break;
445                 case T_NullIfExpr:
446                         {
447                                 NullIfExpr *nexpr = (NullIfExpr *) expr;
448
449                                 return exprTypmod((Node *) linitial(nexpr->args));
450                         }
451                         break;
452                 case T_CoerceToDomain:
453                         return ((CoerceToDomain *) expr)->resulttypmod;
454                 case T_CoerceToDomainValue:
455                         return ((CoerceToDomainValue *) expr)->typeMod;
456                 case T_SetToDefault:
457                         return ((SetToDefault *) expr)->typeMod;
458                 case T_PlaceHolderVar:
459                         return exprTypmod((Node *) ((PlaceHolderVar *) expr)->phexpr);
460                 default:
461                         break;
462         }
463         return -1;
464 }
465
466 /*
467  *      exprCollation -
468  *        returns the Oid of the collation of the expression's result.
469  */
470 Oid
471 exprCollation(Node *expr)
472 {
473         Oid                     coll;
474
475         if (!expr)
476                 return InvalidOid;
477
478         switch (nodeTag(expr))
479         {
480                 case T_Var:
481                         coll = ((Var *) expr)->varcollid;
482                         break;
483                 case T_Const:
484                         coll = ((Const *) expr)->constcollid;
485                         break;
486                 case T_Param:
487                         coll = ((Param *) expr)->paramcollation;
488                         break;
489                 case T_Aggref:
490                         coll = ((Aggref *) expr)->collid;
491                         break;
492                 case T_WindowFunc:
493                         coll = ((WindowFunc *) expr)->collid;
494                         break;
495                 case T_ArrayRef:
496                         coll = ((ArrayRef *) expr)->refcollid;
497                         break;
498                 case T_FuncExpr:
499                         coll = ((FuncExpr *) expr)->collid;
500                         break;
501                 case T_NamedArgExpr:
502                         coll = exprCollation((Node *) ((NamedArgExpr *) expr)->arg);
503                         break;
504                 case T_OpExpr:
505                         coll = ((OpExpr *) expr)->collid;
506                         break;
507                 case T_DistinctExpr:
508                         coll = ((DistinctExpr *) expr)->collid;
509                         break;
510                 case T_ScalarArrayOpExpr:
511                         coll = ((ScalarArrayOpExpr *) expr)->collid;
512                         break;
513                 case T_BoolExpr:
514                         coll = InvalidOid; /* not applicable */
515                         break;
516                 case T_SubLink:
517                         {
518                                 SubLink    *sublink = (SubLink *) expr;
519
520                                 if (sublink->subLinkType == EXPR_SUBLINK ||
521                                         sublink->subLinkType == ARRAY_SUBLINK)
522                                 {
523                                         /* get the collation of the subselect's first target column */
524                                         Query      *qtree = (Query *) sublink->subselect;
525                                         TargetEntry *tent;
526
527                                         if (!qtree || !IsA(qtree, Query))
528                                                 elog(ERROR, "cannot get collation for untransformed sublink");
529                                         tent = (TargetEntry *) linitial(qtree->targetList);
530                                         Assert(IsA(tent, TargetEntry));
531                                         Assert(!tent->resjunk);
532                                         coll = exprCollation((Node *) tent->expr);
533                                         /* note we don't need to care if it's an array */
534                                 }
535                                 else
536                                         coll = InvalidOid;
537                         }
538                         break;
539                 case T_SubPlan:
540                         {
541                                 SubPlan    *subplan = (SubPlan *) expr;
542
543                                 if (subplan->subLinkType == EXPR_SUBLINK ||
544                                         subplan->subLinkType == ARRAY_SUBLINK)
545                                 {
546                                         /* get the collation of the subselect's first target column */
547                                         /* note we don't need to care if it's an array */
548                                         coll = subplan->firstColCollation;
549                                 }
550                                 else
551                                 {
552                                         /* for all other subplan types, result is boolean */
553                                         coll = InvalidOid;
554                                 }
555                         }
556                         break;
557                 case T_AlternativeSubPlan:
558                         {
559                                 AlternativeSubPlan *asplan = (AlternativeSubPlan *) expr;
560
561                                 /* subplans should all return the same thing */
562                                 coll = exprCollation((Node *) linitial(asplan->subplans));
563                         }
564                         break;
565                 case T_FieldSelect:
566                         coll = ((FieldSelect *) expr)->resultcollation;
567                         break;
568                 case T_FieldStore:
569                         coll = InvalidOid; /* not applicable */
570                         break;
571                 case T_RelabelType:
572                         coll = exprCollation((Node *) ((RelabelType *) expr)->arg);
573                         break;
574                 case T_CollateClause:
575                         coll = ((CollateClause *) expr)->collOid;
576                         break;
577                 case T_CoerceViaIO:
578                 {
579                         CoerceViaIO *cvio = (CoerceViaIO *) expr;
580                         coll = coercion_expression_result_collation(cvio->resulttype, (Node *) cvio->arg);
581                         break;
582                 }
583                 case T_ArrayCoerceExpr:
584                 {
585                         ArrayCoerceExpr *ace = (ArrayCoerceExpr *) expr;
586                         coll = coercion_expression_result_collation(ace->resulttype, (Node *) ace->arg);
587                         break;
588                 }
589                 case T_ConvertRowtypeExpr:
590                 {
591                         ConvertRowtypeExpr *cre = (ConvertRowtypeExpr *) expr;
592                         coll = coercion_expression_result_collation(cre->resulttype, (Node *) cre->arg);
593                         break;
594                 }
595                 case T_CaseExpr:
596                         coll = ((CaseExpr *) expr)->casecollation;
597                         break;
598                 case T_CaseTestExpr:
599                         coll = ((CaseTestExpr *) expr)->collation;
600                         break;
601                 case T_ArrayExpr:
602                         coll = get_typcollation(((ArrayExpr *) expr)->array_typeid);
603                         break;
604                 case T_RowExpr:
605                         coll = InvalidOid; /* not applicable */
606                         break;
607                 case T_RowCompareExpr:
608                         coll = InvalidOid; /* not applicable */
609                         break;
610                 case T_CoalesceExpr:
611                         coll = ((CoalesceExpr *) expr)->coalescecollation;
612                         break;
613                 case T_MinMaxExpr:
614                         coll = ((MinMaxExpr *) expr)->collid;
615                         break;
616                 case T_XmlExpr:
617                         if (((XmlExpr *) expr)->op == IS_XMLSERIALIZE)
618                                 coll = DEFAULT_COLLATION_OID;
619                         else
620                                 coll = InvalidOid;
621                         break;
622                 case T_NullIfExpr:
623                         coll = exprCollation((Node *) linitial(((NullIfExpr *) expr)->args));
624                         break;
625                 case T_NullTest:
626                         coll = InvalidOid; /* not applicable */
627                         break;
628                 case T_BooleanTest:
629                         coll = InvalidOid; /* not applicable */
630                         break;
631                 case T_CoerceToDomain:
632                         coll = get_typcollation(((CoerceToDomain *) expr)->resulttype);
633                         if (coll == DEFAULT_COLLATION_OID)
634                                 coll = exprCollation((Node *) ((CoerceToDomain *) expr)->arg);
635                         break;
636                 case T_CoerceToDomainValue:
637                         coll = get_typcollation(((CoerceToDomainValue *) expr)->typeId);
638                         break;
639                 case T_SetToDefault:
640                         coll = ((SetToDefault *) expr)->collid;
641                         break;
642                 case T_CurrentOfExpr:
643                         coll = InvalidOid; /* not applicable */
644                         break;
645                 case T_PlaceHolderVar:
646                         coll = exprCollation((Node *) ((PlaceHolderVar *) expr)->phexpr);
647                         break;
648                 default:
649                         elog(ERROR, "unrecognized node type: %d", (int) nodeTag(expr));
650                         coll = InvalidOid;      /* keep compiler quiet */
651                         break;
652         }
653
654         return coll;
655 }
656
657 /*
658  * Compute the result collation of a coercion-like expression that
659  * converts arg to resulttype.
660  */
661 Oid
662 coercion_expression_result_collation(Oid resulttype, Node *arg)
663 {
664         if (type_is_collatable(resulttype))
665         {
666                 if (type_is_collatable(exprType(arg)))
667                         return exprCollation(arg);
668                 else
669                         return DEFAULT_COLLATION_OID;
670         }
671         else
672                 return InvalidOid;
673 }
674
675 /*
676  * exprIsLengthCoercion
677  *              Detect whether an expression tree is an application of a datatype's
678  *              typmod-coercion function.  Optionally extract the result's typmod.
679  *
680  * If coercedTypmod is not NULL, the typmod is stored there if the expression
681  * is a length-coercion function, else -1 is stored there.
682  *
683  * Note that a combined type-and-length coercion will be treated as a
684  * length coercion by this routine.
685  */
686 bool
687 exprIsLengthCoercion(Node *expr, int32 *coercedTypmod)
688 {
689         if (coercedTypmod != NULL)
690                 *coercedTypmod = -1;    /* default result on failure */
691
692         /*
693          * Scalar-type length coercions are FuncExprs, array-type length coercions
694          * are ArrayCoerceExprs
695          */
696         if (expr && IsA(expr, FuncExpr))
697         {
698                 FuncExpr   *func = (FuncExpr *) expr;
699                 int                     nargs;
700                 Const      *second_arg;
701
702                 /*
703                  * If it didn't come from a coercion context, reject.
704                  */
705                 if (func->funcformat != COERCE_EXPLICIT_CAST &&
706                         func->funcformat != COERCE_IMPLICIT_CAST)
707                         return false;
708
709                 /*
710                  * If it's not a two-argument or three-argument function with the
711                  * second argument being an int4 constant, it can't have been created
712                  * from a length coercion (it must be a type coercion, instead).
713                  */
714                 nargs = list_length(func->args);
715                 if (nargs < 2 || nargs > 3)
716                         return false;
717
718                 second_arg = (Const *) lsecond(func->args);
719                 if (!IsA(second_arg, Const) ||
720                         second_arg->consttype != INT4OID ||
721                         second_arg->constisnull)
722                         return false;
723
724                 /*
725                  * OK, it is indeed a length-coercion function.
726                  */
727                 if (coercedTypmod != NULL)
728                         *coercedTypmod = DatumGetInt32(second_arg->constvalue);
729
730                 return true;
731         }
732
733         if (expr && IsA(expr, ArrayCoerceExpr))
734         {
735                 ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) expr;
736
737                 /* It's not a length coercion unless there's a nondefault typmod */
738                 if (acoerce->resulttypmod < 0)
739                         return false;
740
741                 /*
742                  * OK, it is indeed a length-coercion expression.
743                  */
744                 if (coercedTypmod != NULL)
745                         *coercedTypmod = acoerce->resulttypmod;
746
747                 return true;
748         }
749
750         return false;
751 }
752
753 /*
754  * expression_returns_set
755  *        Test whether an expression returns a set result.
756  *
757  * Because we use expression_tree_walker(), this can also be applied to
758  * whole targetlists; it'll produce TRUE if any one of the tlist items
759  * returns a set.
760  */
761 bool
762 expression_returns_set(Node *clause)
763 {
764         return expression_returns_set_walker(clause, NULL);
765 }
766
767 static bool
768 expression_returns_set_walker(Node *node, void *context)
769 {
770         if (node == NULL)
771                 return false;
772         if (IsA(node, FuncExpr))
773         {
774                 FuncExpr   *expr = (FuncExpr *) node;
775
776                 if (expr->funcretset)
777                         return true;
778                 /* else fall through to check args */
779         }
780         if (IsA(node, OpExpr))
781         {
782                 OpExpr     *expr = (OpExpr *) node;
783
784                 if (expr->opretset)
785                         return true;
786                 /* else fall through to check args */
787         }
788
789         /* Avoid recursion for some cases that can't return a set */
790         if (IsA(node, Aggref))
791                 return false;
792         if (IsA(node, WindowFunc))
793                 return false;
794         if (IsA(node, DistinctExpr))
795                 return false;
796         if (IsA(node, ScalarArrayOpExpr))
797                 return false;
798         if (IsA(node, BoolExpr))
799                 return false;
800         if (IsA(node, SubLink))
801                 return false;
802         if (IsA(node, SubPlan))
803                 return false;
804         if (IsA(node, AlternativeSubPlan))
805                 return false;
806         if (IsA(node, ArrayExpr))
807                 return false;
808         if (IsA(node, RowExpr))
809                 return false;
810         if (IsA(node, RowCompareExpr))
811                 return false;
812         if (IsA(node, CoalesceExpr))
813                 return false;
814         if (IsA(node, MinMaxExpr))
815                 return false;
816         if (IsA(node, XmlExpr))
817                 return false;
818         if (IsA(node, NullIfExpr))
819                 return false;
820
821         return expression_tree_walker(node, expression_returns_set_walker,
822                                                                   context);
823 }
824
825
826 /*
827  *      exprLocation -
828  *        returns the parse location of an expression tree, for error reports
829  *
830  * -1 is returned if the location can't be determined.
831  *
832  * For expressions larger than a single token, the intent here is to
833  * return the location of the expression's leftmost token, not necessarily
834  * the topmost Node's location field.  For example, an OpExpr's location
835  * field will point at the operator name, but if it is not a prefix operator
836  * then we should return the location of the left-hand operand instead.
837  * The reason is that we want to reference the entire expression not just
838  * that operator, and pointing to its start seems to be the most natural way.
839  *
840  * The location is not perfect --- for example, since the grammar doesn't
841  * explicitly represent parentheses in the parsetree, given something that
842  * had been written "(a + b) * c" we are going to point at "a" not "(".
843  * But it should be plenty good enough for error reporting purposes.
844  *
845  * You might think that this code is overly general, for instance why check
846  * the operands of a FuncExpr node, when the function name can be expected
847  * to be to the left of them?  There are a couple of reasons.  The grammar
848  * sometimes builds expressions that aren't quite what the user wrote;
849  * for instance x IS NOT BETWEEN ... becomes a NOT-expression whose keyword
850  * pointer is to the right of its leftmost argument.  Also, nodes that were
851  * inserted implicitly by parse analysis (such as FuncExprs for implicit
852  * coercions) will have location -1, and so we can have odd combinations of
853  * known and unknown locations in a tree.
854  */
855 int
856 exprLocation(Node *expr)
857 {
858         int                     loc;
859
860         if (expr == NULL)
861                 return -1;
862         switch (nodeTag(expr))
863         {
864                 case T_RangeVar:
865                         loc = ((RangeVar *) expr)->location;
866                         break;
867                 case T_Var:
868                         loc = ((Var *) expr)->location;
869                         break;
870                 case T_Const:
871                         loc = ((Const *) expr)->location;
872                         break;
873                 case T_Param:
874                         loc = ((Param *) expr)->location;
875                         break;
876                 case T_Aggref:
877                         /* function name should always be the first thing */
878                         loc = ((Aggref *) expr)->location;
879                         break;
880                 case T_WindowFunc:
881                         /* function name should always be the first thing */
882                         loc = ((WindowFunc *) expr)->location;
883                         break;
884                 case T_ArrayRef:
885                         /* just use array argument's location */
886                         loc = exprLocation((Node *) ((ArrayRef *) expr)->refexpr);
887                         break;
888                 case T_FuncExpr:
889                         {
890                                 FuncExpr   *fexpr = (FuncExpr *) expr;
891
892                                 /* consider both function name and leftmost arg */
893                                 loc = leftmostLoc(fexpr->location,
894                                                                   exprLocation((Node *) fexpr->args));
895                         }
896                         break;
897                 case T_NamedArgExpr:
898                         {
899                                 NamedArgExpr *na = (NamedArgExpr *) expr;
900
901                                 /* consider both argument name and value */
902                                 loc = leftmostLoc(na->location,
903                                                                   exprLocation((Node *) na->arg));
904                         }
905                         break;
906                 case T_OpExpr:
907                 case T_DistinctExpr:    /* struct-equivalent to OpExpr */
908                 case T_NullIfExpr:              /* struct-equivalent to OpExpr */
909                         {
910                                 OpExpr     *opexpr = (OpExpr *) expr;
911
912                                 /* consider both operator name and leftmost arg */
913                                 loc = leftmostLoc(opexpr->location,
914                                                                   exprLocation((Node *) opexpr->args));
915                         }
916                         break;
917                 case T_ScalarArrayOpExpr:
918                         {
919                                 ScalarArrayOpExpr *saopexpr = (ScalarArrayOpExpr *) expr;
920
921                                 /* consider both operator name and leftmost arg */
922                                 loc = leftmostLoc(saopexpr->location,
923                                                                   exprLocation((Node *) saopexpr->args));
924                         }
925                         break;
926                 case T_BoolExpr:
927                         {
928                                 BoolExpr   *bexpr = (BoolExpr *) expr;
929
930                                 /*
931                                  * Same as above, to handle either NOT or AND/OR.  We can't
932                                  * special-case NOT because of the way that it's used for
933                                  * things like IS NOT BETWEEN.
934                                  */
935                                 loc = leftmostLoc(bexpr->location,
936                                                                   exprLocation((Node *) bexpr->args));
937                         }
938                         break;
939                 case T_SubLink:
940                         {
941                                 SubLink    *sublink = (SubLink *) expr;
942
943                                 /* check the testexpr, if any, and the operator/keyword */
944                                 loc = leftmostLoc(exprLocation(sublink->testexpr),
945                                                                   sublink->location);
946                         }
947                         break;
948                 case T_FieldSelect:
949                         /* just use argument's location */
950                         loc = exprLocation((Node *) ((FieldSelect *) expr)->arg);
951                         break;
952                 case T_FieldStore:
953                         /* just use argument's location */
954                         loc = exprLocation((Node *) ((FieldStore *) expr)->arg);
955                         break;
956                 case T_RelabelType:
957                         {
958                                 RelabelType *rexpr = (RelabelType *) expr;
959
960                                 /* Much as above */
961                                 loc = leftmostLoc(rexpr->location,
962                                                                   exprLocation((Node *) rexpr->arg));
963                         }
964                         break;
965                 case T_CoerceViaIO:
966                         {
967                                 CoerceViaIO *cexpr = (CoerceViaIO *) expr;
968
969                                 /* Much as above */
970                                 loc = leftmostLoc(cexpr->location,
971                                                                   exprLocation((Node *) cexpr->arg));
972                         }
973                         break;
974                 case T_ArrayCoerceExpr:
975                         {
976                                 ArrayCoerceExpr *cexpr = (ArrayCoerceExpr *) expr;
977
978                                 /* Much as above */
979                                 loc = leftmostLoc(cexpr->location,
980                                                                   exprLocation((Node *) cexpr->arg));
981                         }
982                         break;
983                 case T_ConvertRowtypeExpr:
984                         {
985                                 ConvertRowtypeExpr *cexpr = (ConvertRowtypeExpr *) expr;
986
987                                 /* Much as above */
988                                 loc = leftmostLoc(cexpr->location,
989                                                                   exprLocation((Node *) cexpr->arg));
990                         }
991                         break;
992                 case T_CaseExpr:
993                         /* CASE keyword should always be the first thing */
994                         loc = ((CaseExpr *) expr)->location;
995                         break;
996                 case T_CaseWhen:
997                         /* WHEN keyword should always be the first thing */
998                         loc = ((CaseWhen *) expr)->location;
999                         break;
1000                 case T_ArrayExpr:
1001                         /* the location points at ARRAY or [, which must be leftmost */
1002                         loc = ((ArrayExpr *) expr)->location;
1003                         break;
1004                 case T_RowExpr:
1005                         /* the location points at ROW or (, which must be leftmost */
1006                         loc = ((RowExpr *) expr)->location;
1007                         break;
1008                 case T_RowCompareExpr:
1009                         /* just use leftmost argument's location */
1010                         loc = exprLocation((Node *) ((RowCompareExpr *) expr)->largs);
1011                         break;
1012                 case T_CoalesceExpr:
1013                         /* COALESCE keyword should always be the first thing */
1014                         loc = ((CoalesceExpr *) expr)->location;
1015                         break;
1016                 case T_MinMaxExpr:
1017                         /* GREATEST/LEAST keyword should always be the first thing */
1018                         loc = ((MinMaxExpr *) expr)->location;
1019                         break;
1020                 case T_XmlExpr:
1021                         {
1022                                 XmlExpr    *xexpr = (XmlExpr *) expr;
1023
1024                                 /* consider both function name and leftmost arg */
1025                                 loc = leftmostLoc(xexpr->location,
1026                                                                   exprLocation((Node *) xexpr->args));
1027                         }
1028                         break;
1029                 case T_NullTest:
1030                         /* just use argument's location */
1031                         loc = exprLocation((Node *) ((NullTest *) expr)->arg);
1032                         break;
1033                 case T_BooleanTest:
1034                         /* just use argument's location */
1035                         loc = exprLocation((Node *) ((BooleanTest *) expr)->arg);
1036                         break;
1037                 case T_CoerceToDomain:
1038                         {
1039                                 CoerceToDomain *cexpr = (CoerceToDomain *) expr;
1040
1041                                 /* Much as above */
1042                                 loc = leftmostLoc(cexpr->location,
1043                                                                   exprLocation((Node *) cexpr->arg));
1044                         }
1045                         break;
1046                 case T_CoerceToDomainValue:
1047                         loc = ((CoerceToDomainValue *) expr)->location;
1048                         break;
1049                 case T_SetToDefault:
1050                         loc = ((SetToDefault *) expr)->location;
1051                         break;
1052                 case T_TargetEntry:
1053                         /* just use argument's location */
1054                         loc = exprLocation((Node *) ((TargetEntry *) expr)->expr);
1055                         break;
1056                 case T_IntoClause:
1057                         /* use the contained RangeVar's location --- close enough */
1058                         loc = exprLocation((Node *) ((IntoClause *) expr)->rel);
1059                         break;
1060                 case T_List:
1061                         {
1062                                 /* report location of first list member that has a location */
1063                                 ListCell   *lc;
1064
1065                                 loc = -1;               /* just to suppress compiler warning */
1066                                 foreach(lc, (List *) expr)
1067                                 {
1068                                         loc = exprLocation((Node *) lfirst(lc));
1069                                         if (loc >= 0)
1070                                                 break;
1071                                 }
1072                         }
1073                         break;
1074                 case T_A_Expr:
1075                         {
1076                                 A_Expr     *aexpr = (A_Expr *) expr;
1077
1078                                 /* use leftmost of operator or left operand (if any) */
1079                                 /* we assume right operand can't be to left of operator */
1080                                 loc = leftmostLoc(aexpr->location,
1081                                                                   exprLocation(aexpr->lexpr));
1082                         }
1083                         break;
1084                 case T_ColumnRef:
1085                         loc = ((ColumnRef *) expr)->location;
1086                         break;
1087                 case T_ParamRef:
1088                         loc = ((ParamRef *) expr)->location;
1089                         break;
1090                 case T_A_Const:
1091                         loc = ((A_Const *) expr)->location;
1092                         break;
1093                 case T_FuncCall:
1094                         {
1095                                 FuncCall   *fc = (FuncCall *) expr;
1096
1097                                 /* consider both function name and leftmost arg */
1098                                 /* (we assume any ORDER BY nodes must be to right of name) */
1099                                 loc = leftmostLoc(fc->location,
1100                                                                   exprLocation((Node *) fc->args));
1101                         }
1102                         break;
1103                 case T_A_ArrayExpr:
1104                         /* the location points at ARRAY or [, which must be leftmost */
1105                         loc = ((A_ArrayExpr *) expr)->location;
1106                         break;
1107                 case T_ResTarget:
1108                         /* we need not examine the contained expression (if any) */
1109                         loc = ((ResTarget *) expr)->location;
1110                         break;
1111                 case T_TypeCast:
1112                         {
1113                                 TypeCast   *tc = (TypeCast *) expr;
1114
1115                                 /*
1116                                  * This could represent CAST(), ::, or TypeName 'literal', so
1117                                  * any of the components might be leftmost.
1118                                  */
1119                                 loc = exprLocation(tc->arg);
1120                                 loc = leftmostLoc(loc, tc->typeName->location);
1121                                 loc = leftmostLoc(loc, tc->location);
1122                         }
1123                         break;
1124                 case T_CollateClause:
1125                         loc = ((CollateClause *) expr)->location;
1126                         break;
1127                 case T_SortBy:
1128                         /* just use argument's location (ignore operator, if any) */
1129                         loc = exprLocation(((SortBy *) expr)->node);
1130                         break;
1131                 case T_WindowDef:
1132                         loc = ((WindowDef *) expr)->location;
1133                         break;
1134                 case T_TypeName:
1135                         loc = ((TypeName *) expr)->location;
1136                         break;
1137                 case T_Constraint:
1138                         loc = ((Constraint *) expr)->location;
1139                         break;
1140                 case T_XmlSerialize:
1141                         /* XMLSERIALIZE keyword should always be the first thing */
1142                         loc = ((XmlSerialize *) expr)->location;
1143                         break;
1144                 case T_WithClause:
1145                         loc = ((WithClause *) expr)->location;
1146                         break;
1147                 case T_CommonTableExpr:
1148                         loc = ((CommonTableExpr *) expr)->location;
1149                         break;
1150                 case T_PlaceHolderVar:
1151                         /* just use argument's location */
1152                         loc = exprLocation((Node *) ((PlaceHolderVar *) expr)->phexpr);
1153                         break;
1154                 default:
1155                         /* for any other node type it's just unknown... */
1156                         loc = -1;
1157                         break;
1158         }
1159         return loc;
1160 }
1161
1162 /*
1163  * leftmostLoc - support for exprLocation
1164  *
1165  * Take the minimum of two parse location values, but ignore unknowns
1166  */
1167 static int
1168 leftmostLoc(int loc1, int loc2)
1169 {
1170         if (loc1 < 0)
1171                 return loc2;
1172         else if (loc2 < 0)
1173                 return loc1;
1174         else
1175                 return Min(loc1, loc2);
1176 }
1177
1178
1179 /*
1180  * Standard expression-tree walking support
1181  *
1182  * We used to have near-duplicate code in many different routines that
1183  * understood how to recurse through an expression node tree.  That was
1184  * a pain to maintain, and we frequently had bugs due to some particular
1185  * routine neglecting to support a particular node type.  In most cases,
1186  * these routines only actually care about certain node types, and don't
1187  * care about other types except insofar as they have to recurse through
1188  * non-primitive node types.  Therefore, we now provide generic tree-walking
1189  * logic to consolidate the redundant "boilerplate" code.  There are
1190  * two versions: expression_tree_walker() and expression_tree_mutator().
1191  */
1192
1193 /*
1194  * expression_tree_walker() is designed to support routines that traverse
1195  * a tree in a read-only fashion (although it will also work for routines
1196  * that modify nodes in-place but never add/delete/replace nodes).
1197  * A walker routine should look like this:
1198  *
1199  * bool my_walker (Node *node, my_struct *context)
1200  * {
1201  *              if (node == NULL)
1202  *                      return false;
1203  *              // check for nodes that special work is required for, eg:
1204  *              if (IsA(node, Var))
1205  *              {
1206  *                      ... do special actions for Var nodes
1207  *              }
1208  *              else if (IsA(node, ...))
1209  *              {
1210  *                      ... do special actions for other node types
1211  *              }
1212  *              // for any node type not specially processed, do:
1213  *              return expression_tree_walker(node, my_walker, (void *) context);
1214  * }
1215  *
1216  * The "context" argument points to a struct that holds whatever context
1217  * information the walker routine needs --- it can be used to return data
1218  * gathered by the walker, too.  This argument is not touched by
1219  * expression_tree_walker, but it is passed down to recursive sub-invocations
1220  * of my_walker.  The tree walk is started from a setup routine that
1221  * fills in the appropriate context struct, calls my_walker with the top-level
1222  * node of the tree, and then examines the results.
1223  *
1224  * The walker routine should return "false" to continue the tree walk, or
1225  * "true" to abort the walk and immediately return "true" to the top-level
1226  * caller.      This can be used to short-circuit the traversal if the walker
1227  * has found what it came for.  "false" is returned to the top-level caller
1228  * iff no invocation of the walker returned "true".
1229  *
1230  * The node types handled by expression_tree_walker include all those
1231  * normally found in target lists and qualifier clauses during the planning
1232  * stage.  In particular, it handles List nodes since a cnf-ified qual clause
1233  * will have List structure at the top level, and it handles TargetEntry nodes
1234  * so that a scan of a target list can be handled without additional code.
1235  * Also, RangeTblRef, FromExpr, JoinExpr, and SetOperationStmt nodes are
1236  * handled, so that query jointrees and setOperation trees can be processed
1237  * without additional code.
1238  *
1239  * expression_tree_walker will handle SubLink nodes by recursing normally
1240  * into the "testexpr" subtree (which is an expression belonging to the outer
1241  * plan).  It will also call the walker on the sub-Query node; however, when
1242  * expression_tree_walker itself is called on a Query node, it does nothing
1243  * and returns "false".  The net effect is that unless the walker does
1244  * something special at a Query node, sub-selects will not be visited during
1245  * an expression tree walk. This is exactly the behavior wanted in many cases
1246  * --- and for those walkers that do want to recurse into sub-selects, special
1247  * behavior is typically needed anyway at the entry to a sub-select (such as
1248  * incrementing a depth counter). A walker that wants to examine sub-selects
1249  * should include code along the lines of:
1250  *
1251  *              if (IsA(node, Query))
1252  *              {
1253  *                      adjust context for subquery;
1254  *                      result = query_tree_walker((Query *) node, my_walker, context,
1255  *                                                                         0); // adjust flags as needed
1256  *                      restore context if needed;
1257  *                      return result;
1258  *              }
1259  *
1260  * query_tree_walker is a convenience routine (see below) that calls the
1261  * walker on all the expression subtrees of the given Query node.
1262  *
1263  * expression_tree_walker will handle SubPlan nodes by recursing normally
1264  * into the "testexpr" and the "args" list (which are expressions belonging to
1265  * the outer plan).  It will not touch the completed subplan, however.  Since
1266  * there is no link to the original Query, it is not possible to recurse into
1267  * subselects of an already-planned expression tree.  This is OK for current
1268  * uses, but may need to be revisited in future.
1269  */
1270
1271 bool
1272 expression_tree_walker(Node *node,
1273                                            bool (*walker) (),
1274                                            void *context)
1275 {
1276         ListCell   *temp;
1277
1278         /*
1279          * The walker has already visited the current node, and so we need only
1280          * recurse into any sub-nodes it has.
1281          *
1282          * We assume that the walker is not interested in List nodes per se, so
1283          * when we expect a List we just recurse directly to self without
1284          * bothering to call the walker.
1285          */
1286         if (node == NULL)
1287                 return false;
1288
1289         /* Guard against stack overflow due to overly complex expressions */
1290         check_stack_depth();
1291
1292         switch (nodeTag(node))
1293         {
1294                 case T_Var:
1295                 case T_Const:
1296                 case T_Param:
1297                 case T_CoerceToDomainValue:
1298                 case T_CaseTestExpr:
1299                 case T_SetToDefault:
1300                 case T_CurrentOfExpr:
1301                 case T_RangeTblRef:
1302                 case T_SortGroupClause:
1303                         /* primitive node types with no expression subnodes */
1304                         break;
1305                 case T_Aggref:
1306                         {
1307                                 Aggref     *expr = (Aggref *) node;
1308
1309                                 /* recurse directly on List */
1310                                 if (expression_tree_walker((Node *) expr->args,
1311                                                                                    walker, context))
1312                                         return true;
1313                                 if (expression_tree_walker((Node *) expr->aggorder,
1314                                                                                    walker, context))
1315                                         return true;
1316                                 if (expression_tree_walker((Node *) expr->aggdistinct,
1317                                                                                    walker, context))
1318                                         return true;
1319                         }
1320                         break;
1321                 case T_WindowFunc:
1322                         {
1323                                 WindowFunc *expr = (WindowFunc *) node;
1324
1325                                 /* recurse directly on List */
1326                                 if (expression_tree_walker((Node *) expr->args,
1327                                                                                    walker, context))
1328                                         return true;
1329                         }
1330                         break;
1331                 case T_ArrayRef:
1332                         {
1333                                 ArrayRef   *aref = (ArrayRef *) node;
1334
1335                                 /* recurse directly for upper/lower array index lists */
1336                                 if (expression_tree_walker((Node *) aref->refupperindexpr,
1337                                                                                    walker, context))
1338                                         return true;
1339                                 if (expression_tree_walker((Node *) aref->reflowerindexpr,
1340                                                                                    walker, context))
1341                                         return true;
1342                                 /* walker must see the refexpr and refassgnexpr, however */
1343                                 if (walker(aref->refexpr, context))
1344                                         return true;
1345                                 if (walker(aref->refassgnexpr, context))
1346                                         return true;
1347                         }
1348                         break;
1349                 case T_FuncExpr:
1350                         {
1351                                 FuncExpr   *expr = (FuncExpr *) node;
1352
1353                                 if (expression_tree_walker((Node *) expr->args,
1354                                                                                    walker, context))
1355                                         return true;
1356                         }
1357                         break;
1358                 case T_NamedArgExpr:
1359                         return walker(((NamedArgExpr *) node)->arg, context);
1360                 case T_OpExpr:
1361                         {
1362                                 OpExpr     *expr = (OpExpr *) node;
1363
1364                                 if (expression_tree_walker((Node *) expr->args,
1365                                                                                    walker, context))
1366                                         return true;
1367                         }
1368                         break;
1369                 case T_DistinctExpr:
1370                         {
1371                                 DistinctExpr *expr = (DistinctExpr *) node;
1372
1373                                 if (expression_tree_walker((Node *) expr->args,
1374                                                                                    walker, context))
1375                                         return true;
1376                         }
1377                         break;
1378                 case T_ScalarArrayOpExpr:
1379                         {
1380                                 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
1381
1382                                 if (expression_tree_walker((Node *) expr->args,
1383                                                                                    walker, context))
1384                                         return true;
1385                         }
1386                         break;
1387                 case T_BoolExpr:
1388                         {
1389                                 BoolExpr   *expr = (BoolExpr *) node;
1390
1391                                 if (expression_tree_walker((Node *) expr->args,
1392                                                                                    walker, context))
1393                                         return true;
1394                         }
1395                         break;
1396                 case T_SubLink:
1397                         {
1398                                 SubLink    *sublink = (SubLink *) node;
1399
1400                                 if (walker(sublink->testexpr, context))
1401                                         return true;
1402
1403                                 /*
1404                                  * Also invoke the walker on the sublink's Query node, so it
1405                                  * can recurse into the sub-query if it wants to.
1406                                  */
1407                                 return walker(sublink->subselect, context);
1408                         }
1409                         break;
1410                 case T_SubPlan:
1411                         {
1412                                 SubPlan    *subplan = (SubPlan *) node;
1413
1414                                 /* recurse into the testexpr, but not into the Plan */
1415                                 if (walker(subplan->testexpr, context))
1416                                         return true;
1417                                 /* also examine args list */
1418                                 if (expression_tree_walker((Node *) subplan->args,
1419                                                                                    walker, context))
1420                                         return true;
1421                         }
1422                         break;
1423                 case T_AlternativeSubPlan:
1424                         return walker(((AlternativeSubPlan *) node)->subplans, context);
1425                 case T_FieldSelect:
1426                         return walker(((FieldSelect *) node)->arg, context);
1427                 case T_FieldStore:
1428                         {
1429                                 FieldStore *fstore = (FieldStore *) node;
1430
1431                                 if (walker(fstore->arg, context))
1432                                         return true;
1433                                 if (walker(fstore->newvals, context))
1434                                         return true;
1435                         }
1436                         break;
1437                 case T_RelabelType:
1438                         return walker(((RelabelType *) node)->arg, context);
1439                 case T_CollateClause:
1440                         return walker(((CollateClause *) node)->arg, context);
1441                 case T_CoerceViaIO:
1442                         return walker(((CoerceViaIO *) node)->arg, context);
1443                 case T_ArrayCoerceExpr:
1444                         return walker(((ArrayCoerceExpr *) node)->arg, context);
1445                 case T_ConvertRowtypeExpr:
1446                         return walker(((ConvertRowtypeExpr *) node)->arg, context);
1447                 case T_CaseExpr:
1448                         {
1449                                 CaseExpr   *caseexpr = (CaseExpr *) node;
1450
1451                                 if (walker(caseexpr->arg, context))
1452                                         return true;
1453                                 /* we assume walker doesn't care about CaseWhens, either */
1454                                 foreach(temp, caseexpr->args)
1455                                 {
1456                                         CaseWhen   *when = (CaseWhen *) lfirst(temp);
1457
1458                                         Assert(IsA(when, CaseWhen));
1459                                         if (walker(when->expr, context))
1460                                                 return true;
1461                                         if (walker(when->result, context))
1462                                                 return true;
1463                                 }
1464                                 if (walker(caseexpr->defresult, context))
1465                                         return true;
1466                         }
1467                         break;
1468                 case T_ArrayExpr:
1469                         return walker(((ArrayExpr *) node)->elements, context);
1470                 case T_RowExpr:
1471                         /* Assume colnames isn't interesting */
1472                         return walker(((RowExpr *) node)->args, context);
1473                 case T_RowCompareExpr:
1474                         {
1475                                 RowCompareExpr *rcexpr = (RowCompareExpr *) node;
1476
1477                                 if (walker(rcexpr->largs, context))
1478                                         return true;
1479                                 if (walker(rcexpr->rargs, context))
1480                                         return true;
1481                         }
1482                         break;
1483                 case T_CoalesceExpr:
1484                         return walker(((CoalesceExpr *) node)->args, context);
1485                 case T_MinMaxExpr:
1486                         return walker(((MinMaxExpr *) node)->args, context);
1487                 case T_XmlExpr:
1488                         {
1489                                 XmlExpr    *xexpr = (XmlExpr *) node;
1490
1491                                 if (walker(xexpr->named_args, context))
1492                                         return true;
1493                                 /* we assume walker doesn't care about arg_names */
1494                                 if (walker(xexpr->args, context))
1495                                         return true;
1496                         }
1497                         break;
1498                 case T_NullIfExpr:
1499                         return walker(((NullIfExpr *) node)->args, context);
1500                 case T_NullTest:
1501                         return walker(((NullTest *) node)->arg, context);
1502                 case T_BooleanTest:
1503                         return walker(((BooleanTest *) node)->arg, context);
1504                 case T_CoerceToDomain:
1505                         return walker(((CoerceToDomain *) node)->arg, context);
1506                 case T_TargetEntry:
1507                         return walker(((TargetEntry *) node)->expr, context);
1508                 case T_Query:
1509                         /* Do nothing with a sub-Query, per discussion above */
1510                         break;
1511                 case T_WindowClause:
1512                         {
1513                                 WindowClause *wc = (WindowClause *) node;
1514
1515                                 if (walker(wc->partitionClause, context))
1516                                         return true;
1517                                 if (walker(wc->orderClause, context))
1518                                         return true;
1519                                 if (walker(wc->startOffset, context))
1520                                         return true;
1521                                 if (walker(wc->endOffset, context))
1522                                         return true;
1523                         }
1524                         break;
1525                 case T_CommonTableExpr:
1526                         {
1527                                 CommonTableExpr *cte = (CommonTableExpr *) node;
1528
1529                                 /*
1530                                  * Invoke the walker on the CTE's Query node, so it can
1531                                  * recurse into the sub-query if it wants to.
1532                                  */
1533                                 return walker(cte->ctequery, context);
1534                         }
1535                         break;
1536                 case T_List:
1537                         foreach(temp, (List *) node)
1538                         {
1539                                 if (walker((Node *) lfirst(temp), context))
1540                                         return true;
1541                         }
1542                         break;
1543                 case T_FromExpr:
1544                         {
1545                                 FromExpr   *from = (FromExpr *) node;
1546
1547                                 if (walker(from->fromlist, context))
1548                                         return true;
1549                                 if (walker(from->quals, context))
1550                                         return true;
1551                         }
1552                         break;
1553                 case T_JoinExpr:
1554                         {
1555                                 JoinExpr   *join = (JoinExpr *) node;
1556
1557                                 if (walker(join->larg, context))
1558                                         return true;
1559                                 if (walker(join->rarg, context))
1560                                         return true;
1561                                 if (walker(join->quals, context))
1562                                         return true;
1563
1564                                 /*
1565                                  * alias clause, using list are deemed uninteresting.
1566                                  */
1567                         }
1568                         break;
1569                 case T_SetOperationStmt:
1570                         {
1571                                 SetOperationStmt *setop = (SetOperationStmt *) node;
1572
1573                                 if (walker(setop->larg, context))
1574                                         return true;
1575                                 if (walker(setop->rarg, context))
1576                                         return true;
1577
1578                                 /* groupClauses are deemed uninteresting */
1579                         }
1580                         break;
1581                 case T_PlaceHolderVar:
1582                         return walker(((PlaceHolderVar *) node)->phexpr, context);
1583                 case T_AppendRelInfo:
1584                         {
1585                                 AppendRelInfo *appinfo = (AppendRelInfo *) node;
1586
1587                                 if (expression_tree_walker((Node *) appinfo->translated_vars,
1588                                                                                    walker, context))
1589                                         return true;
1590                         }
1591                         break;
1592                 case T_PlaceHolderInfo:
1593                         return walker(((PlaceHolderInfo *) node)->ph_var, context);
1594                 default:
1595                         elog(ERROR, "unrecognized node type: %d",
1596                                  (int) nodeTag(node));
1597                         break;
1598         }
1599         return false;
1600 }
1601
1602 /*
1603  * query_tree_walker --- initiate a walk of a Query's expressions
1604  *
1605  * This routine exists just to reduce the number of places that need to know
1606  * where all the expression subtrees of a Query are.  Note it can be used
1607  * for starting a walk at top level of a Query regardless of whether the
1608  * walker intends to descend into subqueries.  It is also useful for
1609  * descending into subqueries within a walker.
1610  *
1611  * Some callers want to suppress visitation of certain items in the sub-Query,
1612  * typically because they need to process them specially, or don't actually
1613  * want to recurse into subqueries.  This is supported by the flags argument,
1614  * which is the bitwise OR of flag values to suppress visitation of
1615  * indicated items.  (More flag bits may be added as needed.)
1616  */
1617 bool
1618 query_tree_walker(Query *query,
1619                                   bool (*walker) (),
1620                                   void *context,
1621                                   int flags)
1622 {
1623         Assert(query != NULL && IsA(query, Query));
1624
1625         if (walker((Node *) query->targetList, context))
1626                 return true;
1627         if (walker((Node *) query->returningList, context))
1628                 return true;
1629         if (walker((Node *) query->jointree, context))
1630                 return true;
1631         if (walker(query->setOperations, context))
1632                 return true;
1633         if (walker(query->havingQual, context))
1634                 return true;
1635         if (walker(query->limitOffset, context))
1636                 return true;
1637         if (walker(query->limitCount, context))
1638                 return true;
1639         if (!(flags & QTW_IGNORE_CTE_SUBQUERIES))
1640         {
1641                 if (walker((Node *) query->cteList, context))
1642                         return true;
1643         }
1644         if (range_table_walker(query->rtable, walker, context, flags))
1645                 return true;
1646         return false;
1647 }
1648
1649 /*
1650  * range_table_walker is just the part of query_tree_walker that scans
1651  * a query's rangetable.  This is split out since it can be useful on
1652  * its own.
1653  */
1654 bool
1655 range_table_walker(List *rtable,
1656                                    bool (*walker) (),
1657                                    void *context,
1658                                    int flags)
1659 {
1660         ListCell   *rt;
1661
1662         foreach(rt, rtable)
1663         {
1664                 RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
1665
1666                 /* For historical reasons, visiting RTEs is not the default */
1667                 if (flags & QTW_EXAMINE_RTES)
1668                         if (walker(rte, context))
1669                                 return true;
1670
1671                 switch (rte->rtekind)
1672                 {
1673                         case RTE_RELATION:
1674                         case RTE_SPECIAL:
1675                         case RTE_CTE:
1676                                 /* nothing to do */
1677                                 break;
1678                         case RTE_SUBQUERY:
1679                                 if (!(flags & QTW_IGNORE_RT_SUBQUERIES))
1680                                         if (walker(rte->subquery, context))
1681                                                 return true;
1682                                 break;
1683                         case RTE_JOIN:
1684                                 if (!(flags & QTW_IGNORE_JOINALIASES))
1685                                         if (walker(rte->joinaliasvars, context))
1686                                                 return true;
1687                                 break;
1688                         case RTE_FUNCTION:
1689                                 if (walker(rte->funcexpr, context))
1690                                         return true;
1691                                 break;
1692                         case RTE_VALUES:
1693                                 if (walker(rte->values_lists, context))
1694                                         return true;
1695                                 break;
1696                 }
1697         }
1698         return false;
1699 }
1700
1701
1702 /*
1703  * expression_tree_mutator() is designed to support routines that make a
1704  * modified copy of an expression tree, with some nodes being added,
1705  * removed, or replaced by new subtrees.  The original tree is (normally)
1706  * not changed.  Each recursion level is responsible for returning a copy of
1707  * (or appropriately modified substitute for) the subtree it is handed.
1708  * A mutator routine should look like this:
1709  *
1710  * Node * my_mutator (Node *node, my_struct *context)
1711  * {
1712  *              if (node == NULL)
1713  *                      return NULL;
1714  *              // check for nodes that special work is required for, eg:
1715  *              if (IsA(node, Var))
1716  *              {
1717  *                      ... create and return modified copy of Var node
1718  *              }
1719  *              else if (IsA(node, ...))
1720  *              {
1721  *                      ... do special transformations of other node types
1722  *              }
1723  *              // for any node type not specially processed, do:
1724  *              return expression_tree_mutator(node, my_mutator, (void *) context);
1725  * }
1726  *
1727  * The "context" argument points to a struct that holds whatever context
1728  * information the mutator routine needs --- it can be used to return extra
1729  * data gathered by the mutator, too.  This argument is not touched by
1730  * expression_tree_mutator, but it is passed down to recursive sub-invocations
1731  * of my_mutator.  The tree walk is started from a setup routine that
1732  * fills in the appropriate context struct, calls my_mutator with the
1733  * top-level node of the tree, and does any required post-processing.
1734  *
1735  * Each level of recursion must return an appropriately modified Node.
1736  * If expression_tree_mutator() is called, it will make an exact copy
1737  * of the given Node, but invoke my_mutator() to copy the sub-node(s)
1738  * of that Node.  In this way, my_mutator() has full control over the
1739  * copying process but need not directly deal with expression trees
1740  * that it has no interest in.
1741  *
1742  * Just as for expression_tree_walker, the node types handled by
1743  * expression_tree_mutator include all those normally found in target lists
1744  * and qualifier clauses during the planning stage.
1745  *
1746  * expression_tree_mutator will handle SubLink nodes by recursing normally
1747  * into the "testexpr" subtree (which is an expression belonging to the outer
1748  * plan).  It will also call the mutator on the sub-Query node; however, when
1749  * expression_tree_mutator itself is called on a Query node, it does nothing
1750  * and returns the unmodified Query node.  The net effect is that unless the
1751  * mutator does something special at a Query node, sub-selects will not be
1752  * visited or modified; the original sub-select will be linked to by the new
1753  * SubLink node.  Mutators that want to descend into sub-selects will usually
1754  * do so by recognizing Query nodes and calling query_tree_mutator (below).
1755  *
1756  * expression_tree_mutator will handle a SubPlan node by recursing into the
1757  * "testexpr" and the "args" list (which belong to the outer plan), but it
1758  * will simply copy the link to the inner plan, since that's typically what
1759  * expression tree mutators want.  A mutator that wants to modify the subplan
1760  * can force appropriate behavior by recognizing SubPlan expression nodes
1761  * and doing the right thing.
1762  */
1763
1764 Node *
1765 expression_tree_mutator(Node *node,
1766                                                 Node *(*mutator) (),
1767                                                 void *context)
1768 {
1769         /*
1770          * The mutator has already decided not to modify the current node, but we
1771          * must call the mutator for any sub-nodes.
1772          */
1773
1774 #define FLATCOPY(newnode, node, nodetype)  \
1775         ( (newnode) = (nodetype *) palloc(sizeof(nodetype)), \
1776           memcpy((newnode), (node), sizeof(nodetype)) )
1777
1778 #define CHECKFLATCOPY(newnode, node, nodetype)  \
1779         ( AssertMacro(IsA((node), nodetype)), \
1780           (newnode) = (nodetype *) palloc(sizeof(nodetype)), \
1781           memcpy((newnode), (node), sizeof(nodetype)) )
1782
1783 #define MUTATE(newfield, oldfield, fieldtype)  \
1784                 ( (newfield) = (fieldtype) mutator((Node *) (oldfield), context) )
1785
1786         if (node == NULL)
1787                 return NULL;
1788
1789         /* Guard against stack overflow due to overly complex expressions */
1790         check_stack_depth();
1791
1792         switch (nodeTag(node))
1793         {
1794                         /*
1795                          * Primitive node types with no expression subnodes.  Var and
1796                          * Const are frequent enough to deserve special cases, the others
1797                          * we just use copyObject for.
1798                          */
1799                 case T_Var:
1800                         {
1801                                 Var                *var = (Var *) node;
1802                                 Var                *newnode;
1803
1804                                 FLATCOPY(newnode, var, Var);
1805                                 return (Node *) newnode;
1806                         }
1807                         break;
1808                 case T_Const:
1809                         {
1810                                 Const      *oldnode = (Const *) node;
1811                                 Const      *newnode;
1812
1813                                 FLATCOPY(newnode, oldnode, Const);
1814                                 /* XXX we don't bother with datumCopy; should we? */
1815                                 return (Node *) newnode;
1816                         }
1817                         break;
1818                 case T_Param:
1819                 case T_CoerceToDomainValue:
1820                 case T_CaseTestExpr:
1821                 case T_SetToDefault:
1822                 case T_CurrentOfExpr:
1823                 case T_RangeTblRef:
1824                 case T_SortGroupClause:
1825                         return (Node *) copyObject(node);
1826                 case T_Aggref:
1827                         {
1828                                 Aggref     *aggref = (Aggref *) node;
1829                                 Aggref     *newnode;
1830
1831                                 FLATCOPY(newnode, aggref, Aggref);
1832                                 MUTATE(newnode->args, aggref->args, List *);
1833                                 MUTATE(newnode->aggorder, aggref->aggorder, List *);
1834                                 MUTATE(newnode->aggdistinct, aggref->aggdistinct, List *);
1835                                 return (Node *) newnode;
1836                         }
1837                         break;
1838                 case T_WindowFunc:
1839                         {
1840                                 WindowFunc *wfunc = (WindowFunc *) node;
1841                                 WindowFunc *newnode;
1842
1843                                 FLATCOPY(newnode, wfunc, WindowFunc);
1844                                 MUTATE(newnode->args, wfunc->args, List *);
1845                                 return (Node *) newnode;
1846                         }
1847                         break;
1848                 case T_ArrayRef:
1849                         {
1850                                 ArrayRef   *arrayref = (ArrayRef *) node;
1851                                 ArrayRef   *newnode;
1852
1853                                 FLATCOPY(newnode, arrayref, ArrayRef);
1854                                 MUTATE(newnode->refupperindexpr, arrayref->refupperindexpr,
1855                                            List *);
1856                                 MUTATE(newnode->reflowerindexpr, arrayref->reflowerindexpr,
1857                                            List *);
1858                                 MUTATE(newnode->refexpr, arrayref->refexpr,
1859                                            Expr *);
1860                                 MUTATE(newnode->refassgnexpr, arrayref->refassgnexpr,
1861                                            Expr *);
1862                                 return (Node *) newnode;
1863                         }
1864                         break;
1865                 case T_FuncExpr:
1866                         {
1867                                 FuncExpr   *expr = (FuncExpr *) node;
1868                                 FuncExpr   *newnode;
1869
1870                                 FLATCOPY(newnode, expr, FuncExpr);
1871                                 MUTATE(newnode->args, expr->args, List *);
1872                                 return (Node *) newnode;
1873                         }
1874                         break;
1875                 case T_NamedArgExpr:
1876                         {
1877                                 NamedArgExpr *nexpr = (NamedArgExpr *) node;
1878                                 NamedArgExpr *newnode;
1879
1880                                 FLATCOPY(newnode, nexpr, NamedArgExpr);
1881                                 MUTATE(newnode->arg, nexpr->arg, Expr *);
1882                                 return (Node *) newnode;
1883                         }
1884                         break;
1885                 case T_OpExpr:
1886                         {
1887                                 OpExpr     *expr = (OpExpr *) node;
1888                                 OpExpr     *newnode;
1889
1890                                 FLATCOPY(newnode, expr, OpExpr);
1891                                 MUTATE(newnode->args, expr->args, List *);
1892                                 return (Node *) newnode;
1893                         }
1894                         break;
1895                 case T_DistinctExpr:
1896                         {
1897                                 DistinctExpr *expr = (DistinctExpr *) node;
1898                                 DistinctExpr *newnode;
1899
1900                                 FLATCOPY(newnode, expr, DistinctExpr);
1901                                 MUTATE(newnode->args, expr->args, List *);
1902                                 return (Node *) newnode;
1903                         }
1904                         break;
1905                 case T_ScalarArrayOpExpr:
1906                         {
1907                                 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
1908                                 ScalarArrayOpExpr *newnode;
1909
1910                                 FLATCOPY(newnode, expr, ScalarArrayOpExpr);
1911                                 MUTATE(newnode->args, expr->args, List *);
1912                                 return (Node *) newnode;
1913                         }
1914                         break;
1915                 case T_BoolExpr:
1916                         {
1917                                 BoolExpr   *expr = (BoolExpr *) node;
1918                                 BoolExpr   *newnode;
1919
1920                                 FLATCOPY(newnode, expr, BoolExpr);
1921                                 MUTATE(newnode->args, expr->args, List *);
1922                                 return (Node *) newnode;
1923                         }
1924                         break;
1925                 case T_SubLink:
1926                         {
1927                                 SubLink    *sublink = (SubLink *) node;
1928                                 SubLink    *newnode;
1929
1930                                 FLATCOPY(newnode, sublink, SubLink);
1931                                 MUTATE(newnode->testexpr, sublink->testexpr, Node *);
1932
1933                                 /*
1934                                  * Also invoke the mutator on the sublink's Query node, so it
1935                                  * can recurse into the sub-query if it wants to.
1936                                  */
1937                                 MUTATE(newnode->subselect, sublink->subselect, Node *);
1938                                 return (Node *) newnode;
1939                         }
1940                         break;
1941                 case T_SubPlan:
1942                         {
1943                                 SubPlan    *subplan = (SubPlan *) node;
1944                                 SubPlan    *newnode;
1945
1946                                 FLATCOPY(newnode, subplan, SubPlan);
1947                                 /* transform testexpr */
1948                                 MUTATE(newnode->testexpr, subplan->testexpr, Node *);
1949                                 /* transform args list (params to be passed to subplan) */
1950                                 MUTATE(newnode->args, subplan->args, List *);
1951                                 /* but not the sub-Plan itself, which is referenced as-is */
1952                                 return (Node *) newnode;
1953                         }
1954                         break;
1955                 case T_AlternativeSubPlan:
1956                         {
1957                                 AlternativeSubPlan *asplan = (AlternativeSubPlan *) node;
1958                                 AlternativeSubPlan *newnode;
1959
1960                                 FLATCOPY(newnode, asplan, AlternativeSubPlan);
1961                                 MUTATE(newnode->subplans, asplan->subplans, List *);
1962                                 return (Node *) newnode;
1963                         }
1964                         break;
1965                 case T_FieldSelect:
1966                         {
1967                                 FieldSelect *fselect = (FieldSelect *) node;
1968                                 FieldSelect *newnode;
1969
1970                                 FLATCOPY(newnode, fselect, FieldSelect);
1971                                 MUTATE(newnode->arg, fselect->arg, Expr *);
1972                                 return (Node *) newnode;
1973                         }
1974                         break;
1975                 case T_FieldStore:
1976                         {
1977                                 FieldStore *fstore = (FieldStore *) node;
1978                                 FieldStore *newnode;
1979
1980                                 FLATCOPY(newnode, fstore, FieldStore);
1981                                 MUTATE(newnode->arg, fstore->arg, Expr *);
1982                                 MUTATE(newnode->newvals, fstore->newvals, List *);
1983                                 newnode->fieldnums = list_copy(fstore->fieldnums);
1984                                 return (Node *) newnode;
1985                         }
1986                         break;
1987                 case T_RelabelType:
1988                         {
1989                                 RelabelType *relabel = (RelabelType *) node;
1990                                 RelabelType *newnode;
1991
1992                                 FLATCOPY(newnode, relabel, RelabelType);
1993                                 MUTATE(newnode->arg, relabel->arg, Expr *);
1994                                 return (Node *) newnode;
1995                         }
1996                         break;
1997                 case T_CollateClause:
1998                         {
1999                                 CollateClause *collate = (CollateClause *) node;
2000                                 CollateClause *newnode;
2001
2002                                 FLATCOPY(newnode, collate, CollateClause);
2003                                 MUTATE(newnode->arg, collate->arg, Expr *);
2004                                 return (Node *) newnode;
2005                         }
2006                         break;
2007                 case T_CoerceViaIO:
2008                         {
2009                                 CoerceViaIO *iocoerce = (CoerceViaIO *) node;
2010                                 CoerceViaIO *newnode;
2011
2012                                 FLATCOPY(newnode, iocoerce, CoerceViaIO);
2013                                 MUTATE(newnode->arg, iocoerce->arg, Expr *);
2014                                 return (Node *) newnode;
2015                         }
2016                         break;
2017                 case T_ArrayCoerceExpr:
2018                         {
2019                                 ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node;
2020                                 ArrayCoerceExpr *newnode;
2021
2022                                 FLATCOPY(newnode, acoerce, ArrayCoerceExpr);
2023                                 MUTATE(newnode->arg, acoerce->arg, Expr *);
2024                                 return (Node *) newnode;
2025                         }
2026                         break;
2027                 case T_ConvertRowtypeExpr:
2028                         {
2029                                 ConvertRowtypeExpr *convexpr = (ConvertRowtypeExpr *) node;
2030                                 ConvertRowtypeExpr *newnode;
2031
2032                                 FLATCOPY(newnode, convexpr, ConvertRowtypeExpr);
2033                                 MUTATE(newnode->arg, convexpr->arg, Expr *);
2034                                 return (Node *) newnode;
2035                         }
2036                         break;
2037                 case T_CaseExpr:
2038                         {
2039                                 CaseExpr   *caseexpr = (CaseExpr *) node;
2040                                 CaseExpr   *newnode;
2041
2042                                 FLATCOPY(newnode, caseexpr, CaseExpr);
2043                                 MUTATE(newnode->arg, caseexpr->arg, Expr *);
2044                                 MUTATE(newnode->args, caseexpr->args, List *);
2045                                 MUTATE(newnode->defresult, caseexpr->defresult, Expr *);
2046                                 return (Node *) newnode;
2047                         }
2048                         break;
2049                 case T_CaseWhen:
2050                         {
2051                                 CaseWhen   *casewhen = (CaseWhen *) node;
2052                                 CaseWhen   *newnode;
2053
2054                                 FLATCOPY(newnode, casewhen, CaseWhen);
2055                                 MUTATE(newnode->expr, casewhen->expr, Expr *);
2056                                 MUTATE(newnode->result, casewhen->result, Expr *);
2057                                 return (Node *) newnode;
2058                         }
2059                         break;
2060                 case T_ArrayExpr:
2061                         {
2062                                 ArrayExpr  *arrayexpr = (ArrayExpr *) node;
2063                                 ArrayExpr  *newnode;
2064
2065                                 FLATCOPY(newnode, arrayexpr, ArrayExpr);
2066                                 MUTATE(newnode->elements, arrayexpr->elements, List *);
2067                                 return (Node *) newnode;
2068                         }
2069                         break;
2070                 case T_RowExpr:
2071                         {
2072                                 RowExpr    *rowexpr = (RowExpr *) node;
2073                                 RowExpr    *newnode;
2074
2075                                 FLATCOPY(newnode, rowexpr, RowExpr);
2076                                 MUTATE(newnode->args, rowexpr->args, List *);
2077                                 /* Assume colnames needn't be duplicated */
2078                                 return (Node *) newnode;
2079                         }
2080                         break;
2081                 case T_RowCompareExpr:
2082                         {
2083                                 RowCompareExpr *rcexpr = (RowCompareExpr *) node;
2084                                 RowCompareExpr *newnode;
2085
2086                                 FLATCOPY(newnode, rcexpr, RowCompareExpr);
2087                                 MUTATE(newnode->largs, rcexpr->largs, List *);
2088                                 MUTATE(newnode->rargs, rcexpr->rargs, List *);
2089                                 return (Node *) newnode;
2090                         }
2091                         break;
2092                 case T_CoalesceExpr:
2093                         {
2094                                 CoalesceExpr *coalesceexpr = (CoalesceExpr *) node;
2095                                 CoalesceExpr *newnode;
2096
2097                                 FLATCOPY(newnode, coalesceexpr, CoalesceExpr);
2098                                 MUTATE(newnode->args, coalesceexpr->args, List *);
2099                                 return (Node *) newnode;
2100                         }
2101                         break;
2102                 case T_MinMaxExpr:
2103                         {
2104                                 MinMaxExpr *minmaxexpr = (MinMaxExpr *) node;
2105                                 MinMaxExpr *newnode;
2106
2107                                 FLATCOPY(newnode, minmaxexpr, MinMaxExpr);
2108                                 MUTATE(newnode->args, minmaxexpr->args, List *);
2109                                 return (Node *) newnode;
2110                         }
2111                         break;
2112                 case T_XmlExpr:
2113                         {
2114                                 XmlExpr    *xexpr = (XmlExpr *) node;
2115                                 XmlExpr    *newnode;
2116
2117                                 FLATCOPY(newnode, xexpr, XmlExpr);
2118                                 MUTATE(newnode->named_args, xexpr->named_args, List *);
2119                                 /* assume mutator does not care about arg_names */
2120                                 MUTATE(newnode->args, xexpr->args, List *);
2121                                 return (Node *) newnode;
2122                         }
2123                         break;
2124                 case T_NullIfExpr:
2125                         {
2126                                 NullIfExpr *expr = (NullIfExpr *) node;
2127                                 NullIfExpr *newnode;
2128
2129                                 FLATCOPY(newnode, expr, NullIfExpr);
2130                                 MUTATE(newnode->args, expr->args, List *);
2131                                 return (Node *) newnode;
2132                         }
2133                         break;
2134                 case T_NullTest:
2135                         {
2136                                 NullTest   *ntest = (NullTest *) node;
2137                                 NullTest   *newnode;
2138
2139                                 FLATCOPY(newnode, ntest, NullTest);
2140                                 MUTATE(newnode->arg, ntest->arg, Expr *);
2141                                 return (Node *) newnode;
2142                         }
2143                         break;
2144                 case T_BooleanTest:
2145                         {
2146                                 BooleanTest *btest = (BooleanTest *) node;
2147                                 BooleanTest *newnode;
2148
2149                                 FLATCOPY(newnode, btest, BooleanTest);
2150                                 MUTATE(newnode->arg, btest->arg, Expr *);
2151                                 return (Node *) newnode;
2152                         }
2153                         break;
2154                 case T_CoerceToDomain:
2155                         {
2156                                 CoerceToDomain *ctest = (CoerceToDomain *) node;
2157                                 CoerceToDomain *newnode;
2158
2159                                 FLATCOPY(newnode, ctest, CoerceToDomain);
2160                                 MUTATE(newnode->arg, ctest->arg, Expr *);
2161                                 return (Node *) newnode;
2162                         }
2163                         break;
2164                 case T_TargetEntry:
2165                         {
2166                                 TargetEntry *targetentry = (TargetEntry *) node;
2167                                 TargetEntry *newnode;
2168
2169                                 FLATCOPY(newnode, targetentry, TargetEntry);
2170                                 MUTATE(newnode->expr, targetentry->expr, Expr *);
2171                                 return (Node *) newnode;
2172                         }
2173                         break;
2174                 case T_Query:
2175                         /* Do nothing with a sub-Query, per discussion above */
2176                         return node;
2177                 case T_WindowClause:
2178                         {
2179                                 WindowClause *wc = (WindowClause *) node;
2180                                 WindowClause *newnode;
2181
2182                                 FLATCOPY(newnode, wc, WindowClause);
2183                                 MUTATE(newnode->partitionClause, wc->partitionClause, List *);
2184                                 MUTATE(newnode->orderClause, wc->orderClause, List *);
2185                                 MUTATE(newnode->startOffset, wc->startOffset, Node *);
2186                                 MUTATE(newnode->endOffset, wc->endOffset, Node *);
2187                                 return (Node *) newnode;
2188                         }
2189                         break;
2190                 case T_CommonTableExpr:
2191                         {
2192                                 CommonTableExpr *cte = (CommonTableExpr *) node;
2193                                 CommonTableExpr *newnode;
2194
2195                                 FLATCOPY(newnode, cte, CommonTableExpr);
2196
2197                                 /*
2198                                  * Also invoke the mutator on the CTE's Query node, so it can
2199                                  * recurse into the sub-query if it wants to.
2200                                  */
2201                                 MUTATE(newnode->ctequery, cte->ctequery, Node *);
2202                                 return (Node *) newnode;
2203                         }
2204                         break;
2205                 case T_List:
2206                         {
2207                                 /*
2208                                  * We assume the mutator isn't interested in the list nodes
2209                                  * per se, so just invoke it on each list element. NOTE: this
2210                                  * would fail badly on a list with integer elements!
2211                                  */
2212                                 List       *resultlist;
2213                                 ListCell   *temp;
2214
2215                                 resultlist = NIL;
2216                                 foreach(temp, (List *) node)
2217                                 {
2218                                         resultlist = lappend(resultlist,
2219                                                                                  mutator((Node *) lfirst(temp),
2220                                                                                                  context));
2221                                 }
2222                                 return (Node *) resultlist;
2223                         }
2224                         break;
2225                 case T_FromExpr:
2226                         {
2227                                 FromExpr   *from = (FromExpr *) node;
2228                                 FromExpr   *newnode;
2229
2230                                 FLATCOPY(newnode, from, FromExpr);
2231                                 MUTATE(newnode->fromlist, from->fromlist, List *);
2232                                 MUTATE(newnode->quals, from->quals, Node *);
2233                                 return (Node *) newnode;
2234                         }
2235                         break;
2236                 case T_JoinExpr:
2237                         {
2238                                 JoinExpr   *join = (JoinExpr *) node;
2239                                 JoinExpr   *newnode;
2240
2241                                 FLATCOPY(newnode, join, JoinExpr);
2242                                 MUTATE(newnode->larg, join->larg, Node *);
2243                                 MUTATE(newnode->rarg, join->rarg, Node *);
2244                                 MUTATE(newnode->quals, join->quals, Node *);
2245                                 /* We do not mutate alias or using by default */
2246                                 return (Node *) newnode;
2247                         }
2248                         break;
2249                 case T_SetOperationStmt:
2250                         {
2251                                 SetOperationStmt *setop = (SetOperationStmt *) node;
2252                                 SetOperationStmt *newnode;
2253
2254                                 FLATCOPY(newnode, setop, SetOperationStmt);
2255                                 MUTATE(newnode->larg, setop->larg, Node *);
2256                                 MUTATE(newnode->rarg, setop->rarg, Node *);
2257                                 /* We do not mutate groupClauses by default */
2258                                 return (Node *) newnode;
2259                         }
2260                         break;
2261                 case T_PlaceHolderVar:
2262                         {
2263                                 PlaceHolderVar *phv = (PlaceHolderVar *) node;
2264                                 PlaceHolderVar *newnode;
2265
2266                                 FLATCOPY(newnode, phv, PlaceHolderVar);
2267                                 MUTATE(newnode->phexpr, phv->phexpr, Expr *);
2268                                 /* Assume we need not copy the relids bitmapset */
2269                                 return (Node *) newnode;
2270                         }
2271                         break;
2272                 case T_AppendRelInfo:
2273                         {
2274                                 AppendRelInfo *appinfo = (AppendRelInfo *) node;
2275                                 AppendRelInfo *newnode;
2276
2277                                 FLATCOPY(newnode, appinfo, AppendRelInfo);
2278                                 MUTATE(newnode->translated_vars, appinfo->translated_vars, List *);
2279                                 return (Node *) newnode;
2280                         }
2281                         break;
2282                 case T_PlaceHolderInfo:
2283                         {
2284                                 PlaceHolderInfo *phinfo = (PlaceHolderInfo *) node;
2285                                 PlaceHolderInfo *newnode;
2286
2287                                 FLATCOPY(newnode, phinfo, PlaceHolderInfo);
2288                                 MUTATE(newnode->ph_var, phinfo->ph_var, PlaceHolderVar *);
2289                                 /* Assume we need not copy the relids bitmapsets */
2290                                 return (Node *) newnode;
2291                         }
2292                         break;
2293                 default:
2294                         elog(ERROR, "unrecognized node type: %d",
2295                                  (int) nodeTag(node));
2296                         break;
2297         }
2298         /* can't get here, but keep compiler happy */
2299         return NULL;
2300 }
2301
2302
2303 /*
2304  * query_tree_mutator --- initiate modification of a Query's expressions
2305  *
2306  * This routine exists just to reduce the number of places that need to know
2307  * where all the expression subtrees of a Query are.  Note it can be used
2308  * for starting a walk at top level of a Query regardless of whether the
2309  * mutator intends to descend into subqueries.  It is also useful for
2310  * descending into subqueries within a mutator.
2311  *
2312  * Some callers want to suppress mutating of certain items in the Query,
2313  * typically because they need to process them specially, or don't actually
2314  * want to recurse into subqueries.  This is supported by the flags argument,
2315  * which is the bitwise OR of flag values to suppress mutating of
2316  * indicated items.  (More flag bits may be added as needed.)
2317  *
2318  * Normally the Query node itself is copied, but some callers want it to be
2319  * modified in-place; they must pass QTW_DONT_COPY_QUERY in flags.      All
2320  * modified substructure is safely copied in any case.
2321  */
2322 Query *
2323 query_tree_mutator(Query *query,
2324                                    Node *(*mutator) (),
2325                                    void *context,
2326                                    int flags)
2327 {
2328         Assert(query != NULL && IsA(query, Query));
2329
2330         if (!(flags & QTW_DONT_COPY_QUERY))
2331         {
2332                 Query      *newquery;
2333
2334                 FLATCOPY(newquery, query, Query);
2335                 query = newquery;
2336         }
2337
2338         MUTATE(query->targetList, query->targetList, List *);
2339         MUTATE(query->returningList, query->returningList, List *);
2340         MUTATE(query->jointree, query->jointree, FromExpr *);
2341         MUTATE(query->setOperations, query->setOperations, Node *);
2342         MUTATE(query->havingQual, query->havingQual, Node *);
2343         MUTATE(query->limitOffset, query->limitOffset, Node *);
2344         MUTATE(query->limitCount, query->limitCount, Node *);
2345         if (!(flags & QTW_IGNORE_CTE_SUBQUERIES))
2346                 MUTATE(query->cteList, query->cteList, List *);
2347         else    /* else copy CTE list as-is */
2348                 query->cteList = copyObject(query->cteList);
2349         query->rtable = range_table_mutator(query->rtable,
2350                                                                                 mutator, context, flags);
2351         return query;
2352 }
2353
2354 /*
2355  * range_table_mutator is just the part of query_tree_mutator that processes
2356  * a query's rangetable.  This is split out since it can be useful on
2357  * its own.
2358  */
2359 List *
2360 range_table_mutator(List *rtable,
2361                                         Node *(*mutator) (),
2362                                         void *context,
2363                                         int flags)
2364 {
2365         List       *newrt = NIL;
2366         ListCell   *rt;
2367
2368         foreach(rt, rtable)
2369         {
2370                 RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
2371                 RangeTblEntry *newrte;
2372
2373                 FLATCOPY(newrte, rte, RangeTblEntry);
2374                 switch (rte->rtekind)
2375                 {
2376                         case RTE_RELATION:
2377                         case RTE_SPECIAL:
2378                         case RTE_CTE:
2379                                 /* we don't bother to copy eref, aliases, etc; OK? */
2380                                 break;
2381                         case RTE_SUBQUERY:
2382                                 if (!(flags & QTW_IGNORE_RT_SUBQUERIES))
2383                                 {
2384                                         CHECKFLATCOPY(newrte->subquery, rte->subquery, Query);
2385                                         MUTATE(newrte->subquery, newrte->subquery, Query *);
2386                                 }
2387                                 else
2388                                 {
2389                                         /* else, copy RT subqueries as-is */
2390                                         newrte->subquery = copyObject(rte->subquery);
2391                                 }
2392                                 break;
2393                         case RTE_JOIN:
2394                                 if (!(flags & QTW_IGNORE_JOINALIASES))
2395                                         MUTATE(newrte->joinaliasvars, rte->joinaliasvars, List *);
2396                                 else
2397                                 {
2398                                         /* else, copy join aliases as-is */
2399                                         newrte->joinaliasvars = copyObject(rte->joinaliasvars);
2400                                 }
2401                                 break;
2402                         case RTE_FUNCTION:
2403                                 MUTATE(newrte->funcexpr, rte->funcexpr, Node *);
2404                                 break;
2405                         case RTE_VALUES:
2406                                 MUTATE(newrte->values_lists, rte->values_lists, List *);
2407                                 break;
2408                 }
2409                 newrt = lappend(newrt, newrte);
2410         }
2411         return newrt;
2412 }
2413
2414 /*
2415  * query_or_expression_tree_walker --- hybrid form
2416  *
2417  * This routine will invoke query_tree_walker if called on a Query node,
2418  * else will invoke the walker directly.  This is a useful way of starting
2419  * the recursion when the walker's normal change of state is not appropriate
2420  * for the outermost Query node.
2421  */
2422 bool
2423 query_or_expression_tree_walker(Node *node,
2424                                                                 bool (*walker) (),
2425                                                                 void *context,
2426                                                                 int flags)
2427 {
2428         if (node && IsA(node, Query))
2429                 return query_tree_walker((Query *) node,
2430                                                                  walker,
2431                                                                  context,
2432                                                                  flags);
2433         else
2434                 return walker(node, context);
2435 }
2436
2437 /*
2438  * query_or_expression_tree_mutator --- hybrid form
2439  *
2440  * This routine will invoke query_tree_mutator if called on a Query node,
2441  * else will invoke the mutator directly.  This is a useful way of starting
2442  * the recursion when the mutator's normal change of state is not appropriate
2443  * for the outermost Query node.
2444  */
2445 Node *
2446 query_or_expression_tree_mutator(Node *node,
2447                                                                  Node *(*mutator) (),
2448                                                                  void *context,
2449                                                                  int flags)
2450 {
2451         if (node && IsA(node, Query))
2452                 return (Node *) query_tree_mutator((Query *) node,
2453                                                                                    mutator,
2454                                                                                    context,
2455                                                                                    flags);
2456         else
2457                 return mutator(node, context);
2458 }
2459
2460
2461 /*
2462  * raw_expression_tree_walker --- walk raw parse trees
2463  *
2464  * This has exactly the same API as expression_tree_walker, but instead of
2465  * walking post-analysis parse trees, it knows how to walk the node types
2466  * found in raw grammar output.  (There is not currently any need for a
2467  * combined walker, so we keep them separate in the name of efficiency.)
2468  * Unlike expression_tree_walker, there is no special rule about query
2469  * boundaries: we descend to everything that's possibly interesting.
2470  *
2471  * Currently, the node type coverage extends to SelectStmt and everything
2472  * that could appear under it, but not other statement types.
2473  */
2474 bool
2475                         raw_expression_tree_walker(Node *node, bool (*walker) (), void *context)
2476 {
2477         ListCell   *temp;
2478
2479         /*
2480          * The walker has already visited the current node, and so we need only
2481          * recurse into any sub-nodes it has.
2482          */
2483         if (node == NULL)
2484                 return false;
2485
2486         /* Guard against stack overflow due to overly complex expressions */
2487         check_stack_depth();
2488
2489         switch (nodeTag(node))
2490         {
2491                 case T_SetToDefault:
2492                 case T_CurrentOfExpr:
2493                 case T_Integer:
2494                 case T_Float:
2495                 case T_String:
2496                 case T_BitString:
2497                 case T_Null:
2498                 case T_ParamRef:
2499                 case T_A_Const:
2500                 case T_A_Star:
2501                         /* primitive node types with no subnodes */
2502                         break;
2503                 case T_Alias:
2504                         /* we assume the colnames list isn't interesting */
2505                         break;
2506                 case T_RangeVar:
2507                         return walker(((RangeVar *) node)->alias, context);
2508                 case T_SubLink:
2509                         {
2510                                 SubLink    *sublink = (SubLink *) node;
2511
2512                                 if (walker(sublink->testexpr, context))
2513                                         return true;
2514                                 /* we assume the operName is not interesting */
2515                                 if (walker(sublink->subselect, context))
2516                                         return true;
2517                         }
2518                         break;
2519                 case T_CaseExpr:
2520                         {
2521                                 CaseExpr   *caseexpr = (CaseExpr *) node;
2522
2523                                 if (walker(caseexpr->arg, context))
2524                                         return true;
2525                                 /* we assume walker doesn't care about CaseWhens, either */
2526                                 foreach(temp, caseexpr->args)
2527                                 {
2528                                         CaseWhen   *when = (CaseWhen *) lfirst(temp);
2529
2530                                         Assert(IsA(when, CaseWhen));
2531                                         if (walker(when->expr, context))
2532                                                 return true;
2533                                         if (walker(when->result, context))
2534                                                 return true;
2535                                 }
2536                                 if (walker(caseexpr->defresult, context))
2537                                         return true;
2538                         }
2539                         break;
2540                 case T_RowExpr:
2541                         /* Assume colnames isn't interesting */
2542                         return walker(((RowExpr *) node)->args, context);
2543                 case T_CoalesceExpr:
2544                         return walker(((CoalesceExpr *) node)->args, context);
2545                 case T_MinMaxExpr:
2546                         return walker(((MinMaxExpr *) node)->args, context);
2547                 case T_XmlExpr:
2548                         {
2549                                 XmlExpr    *xexpr = (XmlExpr *) node;
2550
2551                                 if (walker(xexpr->named_args, context))
2552                                         return true;
2553                                 /* we assume walker doesn't care about arg_names */
2554                                 if (walker(xexpr->args, context))
2555                                         return true;
2556                         }
2557                         break;
2558                 case T_NullTest:
2559                         return walker(((NullTest *) node)->arg, context);
2560                 case T_BooleanTest:
2561                         return walker(((BooleanTest *) node)->arg, context);
2562                 case T_JoinExpr:
2563                         {
2564                                 JoinExpr   *join = (JoinExpr *) node;
2565
2566                                 if (walker(join->larg, context))
2567                                         return true;
2568                                 if (walker(join->rarg, context))
2569                                         return true;
2570                                 if (walker(join->quals, context))
2571                                         return true;
2572                                 if (walker(join->alias, context))
2573                                         return true;
2574                                 /* using list is deemed uninteresting */
2575                         }
2576                         break;
2577                 case T_IntoClause:
2578                         {
2579                                 IntoClause *into = (IntoClause *) node;
2580
2581                                 if (walker(into->rel, context))
2582                                         return true;
2583                                 /* colNames, options are deemed uninteresting */
2584                         }
2585                         break;
2586                 case T_List:
2587                         foreach(temp, (List *) node)
2588                         {
2589                                 if (walker((Node *) lfirst(temp), context))
2590                                         return true;
2591                         }
2592                         break;
2593                 case T_SelectStmt:
2594                         {
2595                                 SelectStmt *stmt = (SelectStmt *) node;
2596
2597                                 if (walker(stmt->distinctClause, context))
2598                                         return true;
2599                                 if (walker(stmt->intoClause, context))
2600                                         return true;
2601                                 if (walker(stmt->targetList, context))
2602                                         return true;
2603                                 if (walker(stmt->fromClause, context))
2604                                         return true;
2605                                 if (walker(stmt->whereClause, context))
2606                                         return true;
2607                                 if (walker(stmt->groupClause, context))
2608                                         return true;
2609                                 if (walker(stmt->havingClause, context))
2610                                         return true;
2611                                 if (walker(stmt->windowClause, context))
2612                                         return true;
2613                                 if (walker(stmt->withClause, context))
2614                                         return true;
2615                                 if (walker(stmt->valuesLists, context))
2616                                         return true;
2617                                 if (walker(stmt->sortClause, context))
2618                                         return true;
2619                                 if (walker(stmt->limitOffset, context))
2620                                         return true;
2621                                 if (walker(stmt->limitCount, context))
2622                                         return true;
2623                                 if (walker(stmt->lockingClause, context))
2624                                         return true;
2625                                 if (walker(stmt->larg, context))
2626                                         return true;
2627                                 if (walker(stmt->rarg, context))
2628                                         return true;
2629                         }
2630                         break;
2631                 case T_A_Expr:
2632                         {
2633                                 A_Expr     *expr = (A_Expr *) node;
2634
2635                                 if (walker(expr->lexpr, context))
2636                                         return true;
2637                                 if (walker(expr->rexpr, context))
2638                                         return true;
2639                                 /* operator name is deemed uninteresting */
2640                         }
2641                         break;
2642                 case T_ColumnRef:
2643                         /* we assume the fields contain nothing interesting */
2644                         break;
2645                 case T_FuncCall:
2646                         {
2647                                 FuncCall   *fcall = (FuncCall *) node;
2648
2649                                 if (walker(fcall->args, context))
2650                                         return true;
2651                                 if (walker(fcall->agg_order, context))
2652                                         return true;
2653                                 if (walker(fcall->over, context))
2654                                         return true;
2655                                 /* function name is deemed uninteresting */
2656                         }
2657                         break;
2658                 case T_NamedArgExpr:
2659                         return walker(((NamedArgExpr *) node)->arg, context);
2660                 case T_A_Indices:
2661                         {
2662                                 A_Indices  *indices = (A_Indices *) node;
2663
2664                                 if (walker(indices->lidx, context))
2665                                         return true;
2666                                 if (walker(indices->uidx, context))
2667                                         return true;
2668                         }
2669                         break;
2670                 case T_A_Indirection:
2671                         {
2672                                 A_Indirection *indir = (A_Indirection *) node;
2673
2674                                 if (walker(indir->arg, context))
2675                                         return true;
2676                                 if (walker(indir->indirection, context))
2677                                         return true;
2678                         }
2679                         break;
2680                 case T_A_ArrayExpr:
2681                         return walker(((A_ArrayExpr *) node)->elements, context);
2682                 case T_ResTarget:
2683                         {
2684                                 ResTarget  *rt = (ResTarget *) node;
2685
2686                                 if (walker(rt->indirection, context))
2687                                         return true;
2688                                 if (walker(rt->val, context))
2689                                         return true;
2690                         }
2691                         break;
2692                 case T_TypeCast:
2693                         {
2694                                 TypeCast   *tc = (TypeCast *) node;
2695
2696                                 if (walker(tc->arg, context))
2697                                         return true;
2698                                 if (walker(tc->typeName, context))
2699                                         return true;
2700                         }
2701                         break;
2702                 case T_CollateClause:
2703                         return walker(((CollateClause *) node)->arg, context);
2704                 case T_SortBy:
2705                         return walker(((SortBy *) node)->node, context);
2706                 case T_WindowDef:
2707                         {
2708                                 WindowDef  *wd = (WindowDef *) node;
2709
2710                                 if (walker(wd->partitionClause, context))
2711                                         return true;
2712                                 if (walker(wd->orderClause, context))
2713                                         return true;
2714                                 if (walker(wd->startOffset, context))
2715                                         return true;
2716                                 if (walker(wd->endOffset, context))
2717                                         return true;
2718                         }
2719                         break;
2720                 case T_RangeSubselect:
2721                         {
2722                                 RangeSubselect *rs = (RangeSubselect *) node;
2723
2724                                 if (walker(rs->subquery, context))
2725                                         return true;
2726                                 if (walker(rs->alias, context))
2727                                         return true;
2728                         }
2729                         break;
2730                 case T_RangeFunction:
2731                         {
2732                                 RangeFunction *rf = (RangeFunction *) node;
2733
2734                                 if (walker(rf->funccallnode, context))
2735                                         return true;
2736                                 if (walker(rf->alias, context))
2737                                         return true;
2738                         }
2739                         break;
2740                 case T_TypeName:
2741                         {
2742                                 TypeName   *tn = (TypeName *) node;
2743
2744                                 if (walker(tn->typmods, context))
2745                                         return true;
2746                                 if (walker(tn->arrayBounds, context))
2747                                         return true;
2748                                 /* type name itself is deemed uninteresting */
2749                         }
2750                         break;
2751                 case T_ColumnDef:
2752                         {
2753                                 ColumnDef  *coldef = (ColumnDef *) node;
2754
2755                                 if (walker(coldef->typeName, context))
2756                                         return true;
2757                                 if (walker(coldef->raw_default, context))
2758                                         return true;
2759                                 /* for now, constraints are ignored */
2760                         }
2761                         break;
2762                 case T_LockingClause:
2763                         return walker(((LockingClause *) node)->lockedRels, context);
2764                 case T_XmlSerialize:
2765                         {
2766                                 XmlSerialize *xs = (XmlSerialize *) node;
2767
2768                                 if (walker(xs->expr, context))
2769                                         return true;
2770                                 if (walker(xs->typeName, context))
2771                                         return true;
2772                         }
2773                         break;
2774                 case T_WithClause:
2775                         return walker(((WithClause *) node)->ctes, context);
2776                 case T_CommonTableExpr:
2777                         return walker(((CommonTableExpr *) node)->ctequery, context);
2778                 default:
2779                         elog(ERROR, "unrecognized node type: %d",
2780                                  (int) nodeTag(node));
2781                         break;
2782         }
2783         return false;
2784 }