OSDN Git Service

Carry column aliases from the parser frontend. Enables queries like
[pg-rex/syncrep.git] / 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.68 2000/02/15 03:37:47 thomas 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                         {
520                                 result = (Node *) expr;
521                                 break;
522                         }
523                 default:
524                         /* should not reach here */
525                         elog(ERROR, "transformExpr: does not know how to transform node %d"
526                                  " (internal error)", nodeTag(expr));
527                         break;
528         }
529
530         return result;
531 }
532
533 static Node *
534 transformIndirection(ParseState *pstate, Node *basenode, List *indirection)
535 {
536         if (indirection == NIL)
537                 return basenode;
538         return (Node *) transformArraySubscripts(pstate, basenode,
539                                                                                          indirection, false, NULL);
540 }
541
542 static Node *
543 transformAttr(ParseState *pstate, Attr *att, int precedence)
544 {
545         Node       *basenode;
546
547         basenode = ParseNestedFuncOrColumn(pstate, att, &pstate->p_last_resno,
548                                                                            precedence);
549         return transformIndirection(pstate, basenode, att->indirection);
550 }
551
552 static Node *
553 transformIdent(ParseState *pstate, Ident *ident, int precedence)
554 {
555         Node       *result = NULL;
556         RangeTblEntry *rte;
557
558         /* try to find the ident as a relation ... but not if subscripts appear */
559         if (ident->indirection == NIL &&
560                 refnameRangeTableEntry(pstate, ident->name) != NULL)
561         {
562                 ident->isRel = TRUE;
563                 result = (Node *) ident;
564         }
565
566         if (result == NULL || precedence == EXPR_COLUMN_FIRST)
567         {
568                 /* try to find the ident as a column */
569                 if ((rte = colnameRangeTableEntry(pstate, ident->name)) != NULL)
570                 {
571                         /* Convert it to a fully qualified Attr, and transform that */
572 #ifndef DISABLE_JOIN_SYNTAX
573                         Attr       *att = makeAttr(rte->ref->relname, ident->name);
574 #else
575                         Attr       *att = makeNode(Attr);
576
577                         att->relname = rte->refname;
578                         att->paramNo = NULL;
579                         att->attrs = lcons(makeString(ident->name), NIL);
580 #endif
581                         att->indirection = ident->indirection;
582                         return transformAttr(pstate, att, precedence);
583                 }
584         }
585
586         if (result == NULL)
587                 elog(ERROR, "Attribute '%s' not found", ident->name);
588
589         return result;
590 }
591
592 /*
593  *      exprType -
594  *        returns the Oid of the type of the expression. (Used for typechecking.)
595  */
596 Oid
597 exprType(Node *expr)
598 {
599         Oid                     type = (Oid) InvalidOid;
600
601         if (!expr)
602                 return type;
603
604         switch (nodeTag(expr))
605         {
606                 case T_Func:
607                         type = ((Func *) expr)->functype;
608                         break;
609                 case T_Iter:
610                         type = ((Iter *) expr)->itertype;
611                         break;
612                 case T_Var:
613                         type = ((Var *) expr)->vartype;
614                         break;
615                 case T_Expr:
616                         type = ((Expr *) expr)->typeOid;
617                         break;
618                 case T_Const:
619                         type = ((Const *) expr)->consttype;
620                         break;
621                 case T_ArrayRef:
622                         type = ((ArrayRef *) expr)->refelemtype;
623                         break;
624                 case T_Aggref:
625                         type = ((Aggref *) expr)->aggtype;
626                         break;
627                 case T_Param:
628                         type = ((Param *) expr)->paramtype;
629                         break;
630                 case T_SubLink:
631                         {
632                                 SubLink    *sublink = (SubLink *) expr;
633
634                                 if (sublink->subLinkType == EXPR_SUBLINK)
635                                 {
636                                         /* get the type of the subselect's first target column */
637                                         Query      *qtree = (Query *) sublink->subselect;
638                                         TargetEntry *tent;
639
640                                         if (! qtree || ! IsA(qtree, Query))
641                                                 elog(ERROR, "Cannot get type for untransformed sublink");
642                                         tent = (TargetEntry *) lfirst(qtree->targetList);
643                                         type = tent->resdom->restype;
644                                 }
645                                 else
646                                 {
647                                         /* for all other sublink types, result is boolean */
648                                         type = BOOLOID;
649                                 }
650                         }
651                         break;
652                 case T_CaseExpr:
653                         type = ((CaseExpr *) expr)->casetype;
654                         break;
655                 case T_CaseWhen:
656                         type = exprType(((CaseWhen *) expr)->result);
657                         break;
658                 case T_Ident:
659                         /* is this right? */
660                         type = UNKNOWNOID;
661                         break;
662                 default:
663                         elog(ERROR, "Do not know how to get type for %d node",
664                                  nodeTag(expr));
665                         break;
666         }
667         return type;
668 }
669
670 /*
671  *      exprTypmod -
672  *        returns the type-specific attrmod of the expression, if it can be
673  *        determined.  In most cases, it can't and we return -1.
674  */
675 int32
676 exprTypmod(Node *expr)
677 {
678         if (!expr)
679                 return -1;
680
681         switch (nodeTag(expr))
682         {
683                 case T_Var:
684                         return ((Var *) expr)->vartypmod;
685                 case T_Const:
686                         {
687                                 /* Be smart about string constants... */
688                                 Const  *con = (Const *) expr;
689                                 switch (con->consttype)
690                                 {
691                                         case BPCHAROID:
692                                                 if (! con->constisnull)
693                                                         return VARSIZE(DatumGetPointer(con->constvalue));
694                                                 break;
695                                         default:
696                                                 break;
697                                 }
698                         }
699                         break;
700                 default:
701                         break;
702         }
703         return -1;
704 }
705
706 /*
707  * Produce an appropriate Const node from a constant value produced
708  * by the parser and an explicit type name to cast to.
709  */
710 static Node *
711 parser_typecast_constant(Value *expr, TypeName *typename)
712 {
713         Const      *con;
714         Type            tp;
715         Datum           datum;
716         char       *const_string = NULL;
717         bool            string_palloced = false;
718         bool            isNull = false;
719
720         switch (nodeTag(expr))
721         {
722                 case T_String:
723                         const_string = DatumGetPointer(expr->val.str);
724                         break;
725                 case T_Integer:
726                         string_palloced = true;
727                         const_string = int4out(expr->val.ival);
728                         break;
729                 case T_Float:
730                         string_palloced = true;
731                         const_string = float8out(&expr->val.dval);
732                         break;
733                 case T_Null:
734                         isNull = true;
735                         break;
736                 default:
737                         elog(ERROR,
738                                  "Cannot cast this expression to type '%s'",
739                                  typename->name);
740         }
741
742         if (typename->arrayBounds != NIL)
743         {
744                 char            type_string[NAMEDATALEN+2];
745
746                 sprintf(type_string, "_%s", typename->name);
747                 tp = (Type) typenameType(type_string);
748         }
749         else
750                 tp = (Type) typenameType(typename->name);
751
752         if (isNull)
753                 datum = (Datum) NULL;
754         else
755                 datum = stringTypeDatum(tp, const_string, typename->typmod);
756
757         con = makeConst(typeTypeId(tp),
758                                         typeLen(tp),
759                                         datum,
760                                         isNull,
761                                         typeByVal(tp),
762                                         false,          /* not a set */
763                                         true /* is cast */ );
764
765         if (string_palloced)
766                 pfree(const_string);
767
768         return (Node *) con;
769 }
770
771 /*
772  * Handle an explicit CAST applied to a non-constant expression.
773  * (Actually, this works for constants too, but gram.y won't generate
774  * a TypeCast node if the argument is just a constant.)
775  *
776  * The given expr has already been transformed, but we need to lookup
777  * the type name and then apply any necessary coercion function(s).
778  */
779 static Node *
780 parser_typecast_expression(ParseState *pstate,
781                                                    Node *expr, TypeName *typename)
782 {
783         Oid                     inputType = exprType(expr);
784         Type            tp;
785         Oid                     targetType;
786
787         if (typename->arrayBounds != NIL)
788         {
789                 char            type_string[NAMEDATALEN+2];
790
791                 sprintf(type_string, "_%s", typename->name);
792                 tp = (Type) typenameType(type_string);
793         }
794         else
795                 tp = (Type) typenameType(typename->name);
796         targetType = typeTypeId(tp);
797
798         if (inputType == InvalidOid)
799                 return expr;                    /* do nothing if NULL input */
800
801         if (inputType != targetType)
802         {
803                 expr = CoerceTargetExpr(pstate, expr, inputType,
804                                                                 targetType, typename->typmod);
805                 if (expr == NULL)
806                         elog(ERROR, "Cannot cast type '%s' to '%s'",
807                                  typeidTypeName(inputType),
808                                  typeidTypeName(targetType));
809         }
810         /*
811          * If the target is a fixed-length type, it may need a length
812          * coercion as well as a type coercion.
813          */
814         expr = coerce_type_typmod(pstate, expr,
815                                                           targetType, typename->typmod);
816
817         return expr;
818 }