]> granicus.if.org Git - postgresql/blob - src/backend/parser/parse_expr.c
Create a new expression node type RelabelType, which exists solely to
[postgresql] / src / backend / parser / parse_expr.c
1 /*-------------------------------------------------------------------------
2  *
3  * parse_expr.c
4  *        handle expressions in parser
5  *
6  * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $Header: /cvsroot/pgsql/src/backend/parser/parse_expr.c,v 1.69 2000/02/20 21:32:10 tgl Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15
16 #include "postgres.h"
17
18 #include "catalog/pg_operator.h"
19 #include "nodes/makefuncs.h"
20 #include "nodes/params.h"
21 #include "nodes/relation.h"
22 #include "parse.h"
23 #include "parser/analyze.h"
24 #include "parser/gramparse.h"
25 #include "parser/parse_coerce.h"
26 #include "parser/parse_expr.h"
27 #include "parser/parse_func.h"
28 #include "parser/parse_oper.h"
29 #include "parser/parse_relation.h"
30 #include "parser/parse_target.h"
31 #include "utils/builtins.h"
32
33 static Node *parser_typecast_constant(Value *expr, TypeName *typename);
34 static Node *parser_typecast_expression(ParseState *pstate,
35                                                                                 Node *expr, TypeName *typename);
36 static Node *transformAttr(ParseState *pstate, Attr *att, int precedence);
37 static Node *transformIdent(ParseState *pstate, Ident *ident, int precedence);
38 static Node *transformIndirection(ParseState *pstate, Node *basenode,
39                                                                   List *indirection);
40
41 /*
42  * transformExpr -
43  *        analyze and transform expressions. Type checking and type casting is
44  *        done here. The optimizer and the executor cannot handle the original
45  *        (raw) expressions collected by the parse tree. Hence the transformation
46  *        here.
47  */
48 Node *
49 transformExpr(ParseState *pstate, Node *expr, int precedence)
50 {
51         Node       *result = NULL;
52
53         if (expr == NULL)
54                 return NULL;
55
56         switch (nodeTag(expr))
57         {
58                 case T_Attr:
59                         {
60                                 result = transformAttr(pstate, (Attr *) expr, precedence);
61                                 break;
62                         }
63                 case T_A_Const:
64                         {
65                                 A_Const    *con = (A_Const *) expr;
66                                 Value      *val = &con->val;
67
68                                 if (con->typename != NULL)
69                                         result = parser_typecast_constant(val, con->typename);
70                                 else
71                                         result = (Node *) make_const(val);
72                                 break;
73                         }
74                 case T_ParamNo:
75                         {
76                                 ParamNo    *pno = (ParamNo *) expr;
77                                 int                     paramno = pno->number;
78                                 Oid                     toid = param_type(paramno);
79                                 Param      *param;
80
81                                 if (!OidIsValid(toid))
82                                         elog(ERROR, "Parameter '$%d' is out of range", paramno);
83                                 param = makeNode(Param);
84                                 param->paramkind = PARAM_NUM;
85                                 param->paramid = (AttrNumber) paramno;
86                                 param->paramname = "<unnamed>";
87                                 param->paramtype = (Oid) toid;
88                                 param->param_tlist = (List *) NULL;
89                                 result = transformIndirection(pstate, (Node *) param,
90                                                                                           pno->indirection);
91                                 /* XXX what about cast (typename) applied to Param ??? */
92                                 break;
93                         }
94                 case T_TypeCast:
95                         {
96                                 TypeCast   *tc = (TypeCast *) expr;
97                                 Node       *arg = transformExpr(pstate, tc->arg, precedence);
98
99                                 result = parser_typecast_expression(pstate, arg, tc->typename);
100                                 break;
101                         }
102                 case T_A_Expr:
103                         {
104                                 A_Expr     *a = (A_Expr *) expr;
105
106                                 switch (a->oper)
107                                 {
108                                         case OP:
109                                                 {
110                                                         Node       *lexpr = transformExpr(pstate, a->lexpr, precedence);
111                                                         Node       *rexpr = transformExpr(pstate, a->rexpr, precedence);
112
113                                                         result = (Node *) make_op(a->opname, lexpr, rexpr);
114                                                 }
115                                                 break;
116                                         case ISNULL:
117                                                 {
118                                                         Node       *lexpr = transformExpr(pstate, a->lexpr, precedence);
119
120                                                         result = ParseFuncOrColumn(pstate,
121                                                                                                            "nullvalue",
122                                                                                                            lcons(lexpr, NIL),
123                                                                                                            false, false,
124                                                                                                            &pstate->p_last_resno,
125                                                                                                            precedence);
126                                                 }
127                                                 break;
128                                         case NOTNULL:
129                                                 {
130                                                         Node       *lexpr = transformExpr(pstate, a->lexpr, precedence);
131
132                                                         result = ParseFuncOrColumn(pstate,
133                                                                                                            "nonnullvalue",
134                                                                                                            lcons(lexpr, NIL),
135                                                                                                            false, false,
136                                                                                                            &pstate->p_last_resno,
137                                                                                                            precedence);
138                                                 }
139                                                 break;
140                                         case AND:
141                                                 {
142                                                         Expr       *expr = makeNode(Expr);
143                                                         Node       *lexpr = transformExpr(pstate, a->lexpr, precedence);
144                                                         Node       *rexpr = transformExpr(pstate, a->rexpr, precedence);
145
146                                                         if (exprType(lexpr) != BOOLOID)
147                                                                 elog(ERROR, "left-hand side of AND is type '%s', not '%s'",
148                                                                          typeidTypeName(exprType(lexpr)), typeidTypeName(BOOLOID));
149
150                                                         if (exprType(rexpr) != BOOLOID)
151                                                                 elog(ERROR, "right-hand side of AND is type '%s', not '%s'",
152                                                                          typeidTypeName(exprType(rexpr)), typeidTypeName(BOOLOID));
153
154                                                         expr->typeOid = BOOLOID;
155                                                         expr->opType = AND_EXPR;
156                                                         expr->args = makeList(lexpr, rexpr, -1);
157                                                         result = (Node *) expr;
158                                                 }
159                                                 break;
160                                         case OR:
161                                                 {
162                                                         Expr       *expr = makeNode(Expr);
163                                                         Node       *lexpr = transformExpr(pstate, a->lexpr, precedence);
164                                                         Node       *rexpr = transformExpr(pstate, a->rexpr, precedence);
165
166                                                         if (exprType(lexpr) != BOOLOID)
167                                                                 elog(ERROR, "left-hand side of OR is type '%s', not '%s'",
168                                                                          typeidTypeName(exprType(lexpr)), typeidTypeName(BOOLOID));
169                                                         if (exprType(rexpr) != BOOLOID)
170                                                                 elog(ERROR, "right-hand side of OR is type '%s', not '%s'",
171                                                                          typeidTypeName(exprType(rexpr)), typeidTypeName(BOOLOID));
172                                                         expr->typeOid = BOOLOID;
173                                                         expr->opType = OR_EXPR;
174                                                         expr->args = makeList(lexpr, rexpr, -1);
175                                                         result = (Node *) expr;
176                                                 }
177                                                 break;
178                                         case NOT:
179                                                 {
180                                                         Expr       *expr = makeNode(Expr);
181                                                         Node       *rexpr = transformExpr(pstate, a->rexpr, precedence);
182
183                                                         if (exprType(rexpr) != BOOLOID)
184                                                                 elog(ERROR, "argument to NOT is type '%s', not '%s'",
185                                                                          typeidTypeName(exprType(rexpr)), typeidTypeName(BOOLOID));
186                                                         expr->typeOid = BOOLOID;
187                                                         expr->opType = NOT_EXPR;
188                                                         expr->args = makeList(rexpr, -1);
189                                                         result = (Node *) expr;
190                                                 }
191                                                 break;
192                                 }
193                                 break;
194                         }
195                 case T_Ident:
196                         {
197                                 result = transformIdent(pstate, (Ident *) expr, precedence);
198                                 break;
199                         }
200                 case T_FuncCall:
201                         {
202                                 FuncCall   *fn = (FuncCall *) expr;
203                                 List       *args;
204
205                                 /* transform the list of arguments */
206                                 foreach(args, fn->args)
207                                         lfirst(args) = transformExpr(pstate, (Node *) lfirst(args), precedence);
208                                 result = ParseFuncOrColumn(pstate,
209                                                                                    fn->funcname,
210                                                                                    fn->args,
211                                                                                    fn->agg_star,
212                                                                                    fn->agg_distinct,
213                                                                                    &pstate->p_last_resno,
214                                                                                    precedence);
215                                 break;
216                         }
217                 case T_SubLink:
218                         {
219                                 SubLink    *sublink = (SubLink *) expr;
220                                 List       *qtrees;
221                                 Query      *qtree;
222
223                                 pstate->p_hasSubLinks = true;
224                                 qtrees = parse_analyze(lcons(sublink->subselect, NIL), pstate);
225                                 if (length(qtrees) != 1)
226                                         elog(ERROR, "Bad query in subselect");
227                                 qtree = (Query *) lfirst(qtrees);
228                                 if (qtree->commandType != CMD_SELECT ||
229                                         qtree->resultRelation != 0)
230                                         elog(ERROR, "Bad query in subselect");
231                                 sublink->subselect = (Node *) qtree;
232
233                                 if (sublink->subLinkType == EXISTS_SUBLINK)
234                                 {
235                                         /* EXISTS needs no lefthand or combining operator.
236                                          * These fields should be NIL already, but make sure.
237                                          */
238                                         sublink->lefthand = NIL;
239                                         sublink->oper = NIL;
240                                 }
241                                 else if (sublink->subLinkType == EXPR_SUBLINK)
242                                 {
243                                         List       *tlist = qtree->targetList;
244
245                                         /* Make sure the subselect delivers a single column
246                                          * (ignoring resjunk targets).
247                                          */
248                                         if (tlist == NIL ||
249                                                 ((TargetEntry *) lfirst(tlist))->resdom->resjunk)
250                                                 elog(ERROR, "Subselect must have a field");
251                                         while ((tlist = lnext(tlist)) != NIL)
252                                         {
253                                                 if (! ((TargetEntry *) lfirst(tlist))->resdom->resjunk)
254                                                         elog(ERROR, "Subselect must have only one field");
255                                         }
256                                         /* EXPR needs no lefthand or combining operator.
257                                          * These fields should be NIL already, but make sure.
258                                          */
259                                         sublink->lefthand = NIL;
260                                         sublink->oper = NIL;
261                                 }
262                                 else
263                                 {
264                                         /* ALL, ANY, or MULTIEXPR: generate operator list */
265                                         char       *op = lfirst(sublink->oper);
266                                         List       *left_list = sublink->lefthand;
267                                         List       *right_list = qtree->targetList;
268                                         List       *elist;
269
270                                         foreach(elist, left_list)
271                                                 lfirst(elist) = transformExpr(pstate, lfirst(elist),
272                                                                                                           precedence);
273
274                                         /* Combining operators other than =/<> is dubious... */
275                                         if (length(left_list) != 1 &&
276                                                 strcmp(op, "=") != 0 && strcmp(op, "<>") != 0)
277                                                 elog(ERROR, "Row comparison cannot use '%s'",
278                                                          op);
279
280                                         sublink->oper = NIL;
281
282                                         /* Scan subquery's targetlist to find values that will be
283                                          * matched against lefthand values.  We need to ignore
284                                          * resjunk targets, so doing the outer iteration over
285                                          * right_list is easier than doing it over left_list.
286                                          */
287                                         while (right_list != NIL)
288                                         {
289                                                 TargetEntry *tent = (TargetEntry *) lfirst(right_list);
290                                                 Node       *lexpr;
291                                                 Operator        optup;
292                                                 Form_pg_operator opform;
293                                                 Oper       *newop;
294
295                                                 right_list = lnext(right_list);
296                                                 if (tent->resdom->resjunk)
297                                                         continue;
298
299                                                 if (left_list == NIL)
300                                                         elog(ERROR, "Subselect has too many fields");
301                                                 lexpr = lfirst(left_list);
302                                                 left_list = lnext(left_list);
303
304                                                 optup = oper(op,
305                                                                          exprType(lexpr),
306                                                                          exprType(tent->expr),
307                                                                          FALSE);
308                                                 opform = (Form_pg_operator) GETSTRUCT(optup);
309
310                                                 if (opform->oprresult != BOOLOID)
311                                                         elog(ERROR, "'%s' result type of '%s' must return '%s'"
312                                                                  " to be used with quantified predicate subquery",
313                                                                  op, typeidTypeName(opform->oprresult),
314                                                                  typeidTypeName(BOOLOID));
315
316                                                 newop = makeOper(oprid(optup),/* opno */
317                                                                                  InvalidOid, /* opid */
318                                                                                  opform->oprresult,
319                                                                                  0,
320                                                                                  NULL);
321                                                 sublink->oper = lappend(sublink->oper, newop);
322                                         }
323                                         if (left_list != NIL)
324                                                 elog(ERROR, "Subselect has too few fields");
325                                 }
326                                 result = (Node *) expr;
327                                 break;
328                         }
329
330                 case T_CaseExpr:
331                         {
332                                 CaseExpr   *c = (CaseExpr *) expr;
333                                 CaseWhen   *w;
334                                 List       *args;
335                                 Oid                     ptype;
336                                 CATEGORY        pcategory;
337
338                                 /* transform the list of arguments */
339                                 foreach(args, c->args)
340                                 {
341                                         w = lfirst(args);
342                                         if (c->arg != NULL)
343                                         {
344                                                 /* shorthand form was specified, so expand... */
345                                                 A_Expr     *a = makeNode(A_Expr);
346
347                                                 a->oper = OP;
348                                                 a->opname = "=";
349                                                 a->lexpr = c->arg;
350                                                 a->rexpr = w->expr;
351                                                 w->expr = (Node *) a;
352                                         }
353                                         lfirst(args) = transformExpr(pstate, (Node *) w, precedence);
354                                 }
355
356                                 /*
357                                  * It's not shorthand anymore, so drop the implicit
358                                  * argument. This is necessary to keep the executor from
359                                  * seeing an untransformed expression...
360                                  */
361                                 c->arg = NULL;
362
363                                 /* transform the default clause */
364                                 if (c->defresult == NULL)
365                                 {
366                                         A_Const    *n = makeNode(A_Const);
367
368                                         n->val.type = T_Null;
369                                         c->defresult = (Node *) n;
370                                 }
371                                 c->defresult = transformExpr(pstate, c->defresult, precedence);
372
373                                 /* now check types across result clauses... */
374                                 c->casetype = exprType(c->defresult);
375                                 ptype = c->casetype;
376                                 pcategory = TypeCategory(ptype);
377                                 foreach(args, c->args)
378                                 {
379                                         Oid                     wtype;
380
381                                         w = lfirst(args);
382                                         wtype = exprType(w->result);
383                                         /* move on to next one if no new information... */
384                                         if (wtype && (wtype != UNKNOWNOID)
385                                                 && (wtype != ptype))
386                                         {
387                                                 if (!ptype || ptype == UNKNOWNOID)
388                                                 {
389                                                         /* so far, only nulls so take anything... */
390                                                         ptype = wtype;
391                                                         pcategory = TypeCategory(ptype);
392                                                 }
393                                                 else if ((TypeCategory(wtype) != pcategory)
394                                                                  || ((TypeCategory(wtype) == USER_TYPE)
395                                                         && (TypeCategory(c->casetype) == USER_TYPE)))
396                                                 {
397                                                         /*
398                                                          * both types in different categories?
399                                                          * then not much hope...
400                                                          */
401                                                         elog(ERROR, "CASE/WHEN types '%s' and '%s' not matched",
402                                                                  typeidTypeName(c->casetype), typeidTypeName(wtype));
403                                                 }
404                                                 else if (IsPreferredType(pcategory, wtype)
405                                                                  && can_coerce_type(1, &ptype, &wtype))
406                                                 {
407                                                         /*
408                                                          * new one is preferred and can convert?
409                                                          * then take it...
410                                                          */
411                                                         ptype = wtype;
412                                                         pcategory = TypeCategory(ptype);
413                                                 }
414                                         }
415                                 }
416
417                                 /* Convert default result clause, if necessary */
418                                 if (c->casetype != ptype)
419                                 {
420                                         if (!c->casetype || c->casetype == UNKNOWNOID)
421                                         {
422                                                 /*
423                                                  * default clause is NULL, so assign preferred
424                                                  * type from WHEN clauses...
425                                                  */
426                                                 c->casetype = ptype;
427                                         }
428                                         else if (can_coerce_type(1, &c->casetype, &ptype))
429                                         {
430                                                 c->defresult = coerce_type(pstate, c->defresult,
431                                                                                                  c->casetype, ptype, -1);
432                                                 c->casetype = ptype;
433                                         }
434                                         else
435                                         {
436                                                 elog(ERROR, "CASE/ELSE unable to convert to type '%s'",
437                                                          typeidTypeName(ptype));
438                                         }
439                                 }
440
441                                 /* Convert when clauses, if not null and if necessary */
442                                 foreach(args, c->args)
443                                 {
444                                         Oid                     wtype;
445
446                                         w = lfirst(args);
447                                         wtype = exprType(w->result);
448
449                                         /*
450                                          * only bother with conversion if not NULL and
451                                          * different type...
452                                          */
453                                         if (wtype && (wtype != UNKNOWNOID)
454                                                 && (wtype != ptype))
455                                         {
456                                                 if (can_coerce_type(1, &wtype, &ptype))
457                                                 {
458                                                         w->result = coerce_type(pstate, w->result, wtype,
459                                                                                                         ptype, -1);
460                                                 }
461                                                 else
462                                                 {
463                                                         elog(ERROR, "CASE/WHEN unable to convert to type '%s'",
464                                                                  typeidTypeName(ptype));
465                                                 }
466                                         }
467                                 }
468
469                                 result = expr;
470                                 break;
471                         }
472
473                 case T_CaseWhen:
474                         {
475                                 CaseWhen   *w = (CaseWhen *) expr;
476
477                                 w->expr = transformExpr(pstate, (Node *) w->expr, precedence);
478                                 if (exprType(w->expr) != BOOLOID)
479                                         elog(ERROR, "WHEN clause must have a boolean result");
480
481                                 /*
482                                  * result is NULL for NULLIF() construct - thomas
483                                  * 1998-11-11
484                                  */
485                                 if (w->result == NULL)
486                                 {
487                                         A_Const    *n = makeNode(A_Const);
488
489                                         n->val.type = T_Null;
490                                         w->result = (Node *) n;
491                                 }
492                                 w->result = transformExpr(pstate, (Node *) w->result, precedence);
493                                 result = expr;
494                                 break;
495                         }
496
497 /* Some nodes do _not_ come from the original parse tree,
498  *      but result from parser transformation in this phase.
499  * At least one construct (BETWEEN/AND) puts the same nodes
500  *      into two branches of the parse tree; hence, some nodes
501  *      are transformed twice.
502  * Another way it can happen is that coercion of an operator or
503  *      function argument to the required type (via coerce_type())
504  *      can apply transformExpr to an already-transformed subexpression.
505  *      An example here is "SELECT count(*) + 1.0 FROM table".
506  * Thus, we can see node types in this routine that do not appear in the
507  *      original parse tree.  Assume they are already transformed, and just
508  *      pass them through.
509  * Do any other node types need to be accepted?  For now we are taking
510  *      a conservative approach, and only accepting node types that are
511  *      demonstrably necessary to accept.
512  */
513                 case T_Expr:
514                 case T_Var:
515                 case T_Const:
516                 case T_Param:
517                 case T_Aggref:
518                 case T_ArrayRef:
519                 case T_RelabelType:
520                         {
521                                 result = (Node *) expr;
522                                 break;
523                         }
524                 default:
525                         /* should not reach here */
526                         elog(ERROR, "transformExpr: does not know how to transform node %d"
527                                  " (internal error)", nodeTag(expr));
528                         break;
529         }
530
531         return result;
532 }
533
534 static Node *
535 transformIndirection(ParseState *pstate, Node *basenode, List *indirection)
536 {
537         if (indirection == NIL)
538                 return basenode;
539         return (Node *) transformArraySubscripts(pstate, basenode,
540                                                                                          indirection, false, NULL);
541 }
542
543 static Node *
544 transformAttr(ParseState *pstate, Attr *att, int precedence)
545 {
546         Node       *basenode;
547
548         basenode = ParseNestedFuncOrColumn(pstate, att, &pstate->p_last_resno,
549                                                                            precedence);
550         return transformIndirection(pstate, basenode, att->indirection);
551 }
552
553 static Node *
554 transformIdent(ParseState *pstate, Ident *ident, int precedence)
555 {
556         Node       *result = NULL;
557         RangeTblEntry *rte;
558
559         /* try to find the ident as a relation ... but not if subscripts appear */
560         if (ident->indirection == NIL &&
561                 refnameRangeTableEntry(pstate, ident->name) != NULL)
562         {
563                 ident->isRel = TRUE;
564                 result = (Node *) ident;
565         }
566
567         if (result == NULL || precedence == EXPR_COLUMN_FIRST)
568         {
569                 /* try to find the ident as a column */
570                 if ((rte = colnameRangeTableEntry(pstate, ident->name)) != NULL)
571                 {
572                         /* Convert it to a fully qualified Attr, and transform that */
573 #ifndef DISABLE_JOIN_SYNTAX
574                         Attr       *att = makeAttr(rte->ref->relname, ident->name);
575 #else
576                         Attr       *att = makeNode(Attr);
577
578                         att->relname = rte->refname;
579                         att->paramNo = NULL;
580                         att->attrs = lcons(makeString(ident->name), NIL);
581 #endif
582                         att->indirection = ident->indirection;
583                         return transformAttr(pstate, att, precedence);
584                 }
585         }
586
587         if (result == NULL)
588                 elog(ERROR, "Attribute '%s' not found", ident->name);
589
590         return result;
591 }
592
593 /*
594  *      exprType -
595  *        returns the Oid of the type of the expression. (Used for typechecking.)
596  */
597 Oid
598 exprType(Node *expr)
599 {
600         Oid                     type = (Oid) InvalidOid;
601
602         if (!expr)
603                 return type;
604
605         switch (nodeTag(expr))
606         {
607                 case T_Func:
608                         type = ((Func *) expr)->functype;
609                         break;
610                 case T_Iter:
611                         type = ((Iter *) expr)->itertype;
612                         break;
613                 case T_Var:
614                         type = ((Var *) expr)->vartype;
615                         break;
616                 case T_Expr:
617                         type = ((Expr *) expr)->typeOid;
618                         break;
619                 case T_Const:
620                         type = ((Const *) expr)->consttype;
621                         break;
622                 case T_ArrayRef:
623                         type = ((ArrayRef *) expr)->refelemtype;
624                         break;
625                 case T_Aggref:
626                         type = ((Aggref *) expr)->aggtype;
627                         break;
628                 case T_Param:
629                         type = ((Param *) expr)->paramtype;
630                         break;
631                 case T_RelabelType:
632                         type = ((RelabelType *) expr)->resulttype;
633                         break;
634                 case T_SubLink:
635                         {
636                                 SubLink    *sublink = (SubLink *) expr;
637
638                                 if (sublink->subLinkType == EXPR_SUBLINK)
639                                 {
640                                         /* get the type of the subselect's first target column */
641                                         Query      *qtree = (Query *) sublink->subselect;
642                                         TargetEntry *tent;
643
644                                         if (! qtree || ! IsA(qtree, Query))
645                                                 elog(ERROR, "Cannot get type for untransformed sublink");
646                                         tent = (TargetEntry *) lfirst(qtree->targetList);
647                                         type = tent->resdom->restype;
648                                 }
649                                 else
650                                 {
651                                         /* for all other sublink types, result is boolean */
652                                         type = BOOLOID;
653                                 }
654                         }
655                         break;
656                 case T_CaseExpr:
657                         type = ((CaseExpr *) expr)->casetype;
658                         break;
659                 case T_CaseWhen:
660                         type = exprType(((CaseWhen *) expr)->result);
661                         break;
662                 case T_Ident:
663                         /* is this right? */
664                         type = UNKNOWNOID;
665                         break;
666                 default:
667                         elog(ERROR, "Do not know how to get type for %d node",
668                                  nodeTag(expr));
669                         break;
670         }
671         return type;
672 }
673
674 /*
675  *      exprTypmod -
676  *        returns the type-specific attrmod of the expression, if it can be
677  *        determined.  In most cases, it can't and we return -1.
678  */
679 int32
680 exprTypmod(Node *expr)
681 {
682         if (!expr)
683                 return -1;
684
685         switch (nodeTag(expr))
686         {
687                 case T_Var:
688                         return ((Var *) expr)->vartypmod;
689                 case T_Const:
690                         {
691                                 /* Be smart about string constants... */
692                                 Const  *con = (Const *) expr;
693                                 switch (con->consttype)
694                                 {
695                                         case BPCHAROID:
696                                                 if (! con->constisnull)
697                                                         return VARSIZE(DatumGetPointer(con->constvalue));
698                                                 break;
699                                         default:
700                                                 break;
701                                 }
702                         }
703                         break;
704                 case T_RelabelType:
705                         return ((RelabelType *) expr)->resulttypmod;
706                         break;
707                 default:
708                         break;
709         }
710         return -1;
711 }
712
713 /*
714  * Produce an appropriate Const node from a constant value produced
715  * by the parser and an explicit type name to cast to.
716  */
717 static Node *
718 parser_typecast_constant(Value *expr, TypeName *typename)
719 {
720         Const      *con;
721         Type            tp;
722         Datum           datum;
723         char       *const_string = NULL;
724         bool            string_palloced = false;
725         bool            isNull = false;
726
727         switch (nodeTag(expr))
728         {
729                 case T_String:
730                         const_string = DatumGetPointer(expr->val.str);
731                         break;
732                 case T_Integer:
733                         string_palloced = true;
734                         const_string = int4out(expr->val.ival);
735                         break;
736                 case T_Float:
737                         string_palloced = true;
738                         const_string = float8out(&expr->val.dval);
739                         break;
740                 case T_Null:
741                         isNull = true;
742                         break;
743                 default:
744                         elog(ERROR,
745                                  "Cannot cast this expression to type '%s'",
746                                  typename->name);
747         }
748
749         if (typename->arrayBounds != NIL)
750         {
751                 char            type_string[NAMEDATALEN+2];
752
753                 sprintf(type_string, "_%s", typename->name);
754                 tp = (Type) typenameType(type_string);
755         }
756         else
757                 tp = (Type) typenameType(typename->name);
758
759         if (isNull)
760                 datum = (Datum) NULL;
761         else
762                 datum = stringTypeDatum(tp, const_string, typename->typmod);
763
764         con = makeConst(typeTypeId(tp),
765                                         typeLen(tp),
766                                         datum,
767                                         isNull,
768                                         typeByVal(tp),
769                                         false,          /* not a set */
770                                         true /* is cast */ );
771
772         if (string_palloced)
773                 pfree(const_string);
774
775         return (Node *) con;
776 }
777
778 /*
779  * Handle an explicit CAST applied to a non-constant expression.
780  * (Actually, this works for constants too, but gram.y won't generate
781  * a TypeCast node if the argument is just a constant.)
782  *
783  * The given expr has already been transformed, but we need to lookup
784  * the type name and then apply any necessary coercion function(s).
785  */
786 static Node *
787 parser_typecast_expression(ParseState *pstate,
788                                                    Node *expr, TypeName *typename)
789 {
790         Oid                     inputType = exprType(expr);
791         Type            tp;
792         Oid                     targetType;
793
794         if (typename->arrayBounds != NIL)
795         {
796                 char            type_string[NAMEDATALEN+2];
797
798                 sprintf(type_string, "_%s", typename->name);
799                 tp = (Type) typenameType(type_string);
800         }
801         else
802                 tp = (Type) typenameType(typename->name);
803         targetType = typeTypeId(tp);
804
805         if (inputType == InvalidOid)
806                 return expr;                    /* do nothing if NULL input */
807
808         if (inputType != targetType)
809         {
810                 expr = CoerceTargetExpr(pstate, expr, inputType,
811                                                                 targetType, typename->typmod);
812                 if (expr == NULL)
813                         elog(ERROR, "Cannot cast type '%s' to '%s'",
814                                  typeidTypeName(inputType),
815                                  typeidTypeName(targetType));
816         }
817         /*
818          * If the target is a fixed-length type, it may need a length
819          * coercion as well as a type coercion.
820          */
821         expr = coerce_type_typmod(pstate, expr,
822                                                           targetType, typename->typmod);
823
824         return expr;
825 }