1 /*-------------------------------------------------------------------------
4 * handle expressions in parser
6 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $Header: /cvsroot/pgsql/src/backend/parser/parse_expr.c,v 1.84 2000/09/29 18:21:36 tgl Exp $
13 *-------------------------------------------------------------------------
18 #include "catalog/pg_operator.h"
19 #include "catalog/pg_proc.h"
20 #include "nodes/makefuncs.h"
21 #include "nodes/params.h"
22 #include "parser/analyze.h"
23 #include "parser/gramparse.h"
24 #include "parser/parse.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 "parser/parse_type.h"
32 #include "utils/builtins.h"
33 #include "utils/syscache.h"
36 int max_expr_depth = DEFAULT_MAX_EXPR_DEPTH;
38 static int expr_depth_counter = 0;
40 static Node *parser_typecast_constant(Value *expr, TypeName *typename);
41 static Node *parser_typecast_expression(ParseState *pstate,
42 Node *expr, TypeName *typename);
43 static Node *transformAttr(ParseState *pstate, Attr *att, int precedence);
44 static Node *transformIdent(ParseState *pstate, Ident *ident, int precedence);
45 static Node *transformIndirection(ParseState *pstate, Node *basenode,
50 * Initialize for parsing a new query.
52 * We reset the expression depth counter here, in case it was left nonzero
53 * due to elog()'ing out of the last parsing operation.
58 expr_depth_counter = 0;
64 * Analyze and transform expressions. Type checking and type casting is
65 * done here. The optimizer and the executor cannot handle the original
66 * (raw) expressions collected by the parse tree. Hence the transformation
69 * NOTE: there are various cases in which this routine will get applied to
70 * an already-transformed expression. Some examples:
71 * 1. At least one construct (BETWEEN/AND) puts the same nodes
72 * into two branches of the parse tree; hence, some nodes
73 * are transformed twice.
74 * 2. Another way it can happen is that coercion of an operator or
75 * function argument to the required type (via coerce_type())
76 * can apply transformExpr to an already-transformed subexpression.
77 * An example here is "SELECT count(*) + 1.0 FROM table".
78 * While it might be possible to eliminate these cases, the path of
79 * least resistance so far has been to ensure that transformExpr() does
80 * no damage if applied to an already-transformed tree. This is pretty
81 * easy for cases where the transformation replaces one node type with
82 * another, such as A_Const => Const; we just do nothing when handed
83 * a Const. More care is needed for node types that are used as both
84 * input and output of transformExpr; see SubLink for example.
87 transformExpr(ParseState *pstate, Node *expr, int precedence)
95 * Guard against an overly complex expression leading to coredump due
96 * to stack overflow here, or in later recursive routines that
97 * traverse expression trees. Note that this is very unlikely to
98 * happen except with pathological queries; but we don't want someone
99 * to be able to crash the backend quite that easily...
101 if (++expr_depth_counter > max_expr_depth)
102 elog(ERROR, "Expression too complex: nesting depth exceeds max_expr_depth = %d",
105 switch (nodeTag(expr))
109 result = transformAttr(pstate, (Attr *) expr, precedence);
114 A_Const *con = (A_Const *) expr;
115 Value *val = &con->val;
117 if (con->typename != NULL)
118 result = parser_typecast_constant(val, con->typename);
120 result = (Node *) make_const(val);
125 ParamNo *pno = (ParamNo *) expr;
126 int paramno = pno->number;
127 Oid toid = param_type(paramno);
128 Param *param = makeNode(Param);
130 if (!OidIsValid(toid))
131 elog(ERROR, "Parameter '$%d' is out of range", paramno);
132 param->paramkind = PARAM_NUM;
133 param->paramid = (AttrNumber) paramno;
134 param->paramname = "<unnamed>";
135 param->paramtype = toid;
136 result = transformIndirection(pstate, (Node *) param,
138 /* cope with typecast applied to param */
139 if (pno->typename != NULL)
140 result = parser_typecast_expression(pstate, result,
146 TypeCast *tc = (TypeCast *) expr;
147 Node *arg = transformExpr(pstate, tc->arg, precedence);
149 result = parser_typecast_expression(pstate, arg, tc->typename);
154 A_Expr *a = (A_Expr *) expr;
160 Node *lexpr = transformExpr(pstate,
163 Node *rexpr = transformExpr(pstate,
167 result = (Node *) make_op(a->opname, lexpr, rexpr);
172 Node *lexpr = transformExpr(pstate,
176 result = ParseFuncOrColumn(pstate,
185 Node *lexpr = transformExpr(pstate,
189 result = ParseFuncOrColumn(pstate,
198 Node *lexpr = transformExpr(pstate,
201 Node *rexpr = transformExpr(pstate,
204 Expr *expr = makeNode(Expr);
206 if (exprType(lexpr) != BOOLOID)
207 elog(ERROR, "left-hand side of AND is type '%s', not '%s'",
208 typeidTypeName(exprType(lexpr)), typeidTypeName(BOOLOID));
210 if (exprType(rexpr) != BOOLOID)
211 elog(ERROR, "right-hand side of AND is type '%s', not '%s'",
212 typeidTypeName(exprType(rexpr)), typeidTypeName(BOOLOID));
214 expr->typeOid = BOOLOID;
215 expr->opType = AND_EXPR;
216 expr->args = makeList2(lexpr, rexpr);
217 result = (Node *) expr;
222 Node *lexpr = transformExpr(pstate,
225 Node *rexpr = transformExpr(pstate,
228 Expr *expr = makeNode(Expr);
230 if (exprType(lexpr) != BOOLOID)
231 elog(ERROR, "left-hand side of OR is type '%s', not '%s'",
232 typeidTypeName(exprType(lexpr)), typeidTypeName(BOOLOID));
233 if (exprType(rexpr) != BOOLOID)
234 elog(ERROR, "right-hand side of OR is type '%s', not '%s'",
235 typeidTypeName(exprType(rexpr)), typeidTypeName(BOOLOID));
236 expr->typeOid = BOOLOID;
237 expr->opType = OR_EXPR;
238 expr->args = makeList2(lexpr, rexpr);
239 result = (Node *) expr;
244 Node *rexpr = transformExpr(pstate,
247 Expr *expr = makeNode(Expr);
249 if (exprType(rexpr) != BOOLOID)
250 elog(ERROR, "argument to NOT is type '%s', not '%s'",
251 typeidTypeName(exprType(rexpr)), typeidTypeName(BOOLOID));
252 expr->typeOid = BOOLOID;
253 expr->opType = NOT_EXPR;
254 expr->args = makeList1(rexpr);
255 result = (Node *) expr;
263 result = transformIdent(pstate, (Ident *) expr, precedence);
268 FuncCall *fn = (FuncCall *) expr;
271 /* transform the list of arguments */
272 foreach(args, fn->args)
273 lfirst(args) = transformExpr(pstate,
274 (Node *) lfirst(args),
276 result = ParseFuncOrColumn(pstate,
286 SubLink *sublink = (SubLink *) expr;
290 /* If we already transformed this node, do nothing */
291 if (IsA(sublink->subselect, Query))
296 pstate->p_hasSubLinks = true;
297 qtrees = parse_analyze(makeList1(sublink->subselect),
299 if (length(qtrees) != 1)
300 elog(ERROR, "Bad query in subselect");
301 qtree = (Query *) lfirst(qtrees);
302 if (qtree->commandType != CMD_SELECT ||
303 qtree->resultRelation != 0)
304 elog(ERROR, "Bad query in subselect");
305 sublink->subselect = (Node *) qtree;
307 if (sublink->subLinkType == EXISTS_SUBLINK)
311 * EXISTS needs no lefthand or combining operator.
312 * These fields should be NIL already, but make sure.
314 sublink->lefthand = NIL;
317 else if (sublink->subLinkType == EXPR_SUBLINK)
319 List *tlist = qtree->targetList;
322 * Make sure the subselect delivers a single column
323 * (ignoring resjunk targets).
326 ((TargetEntry *) lfirst(tlist))->resdom->resjunk)
327 elog(ERROR, "Subselect must have a field");
328 while ((tlist = lnext(tlist)) != NIL)
330 if (!((TargetEntry *) lfirst(tlist))->resdom->resjunk)
331 elog(ERROR, "Subselect must have only one field");
335 * EXPR needs no lefthand or combining operator. These
336 * fields should be NIL already, but make sure.
338 sublink->lefthand = NIL;
343 /* ALL, ANY, or MULTIEXPR: generate operator list */
344 List *left_list = sublink->lefthand;
345 List *right_list = qtree->targetList;
349 foreach(elist, left_list)
350 lfirst(elist) = transformExpr(pstate, lfirst(elist),
353 Assert(IsA(sublink->oper, A_Expr));
354 op = ((A_Expr *) sublink->oper)->opname;
357 /* Combining operators other than =/<> is dubious... */
358 if (length(left_list) != 1 &&
359 strcmp(op, "=") != 0 && strcmp(op, "<>") != 0)
360 elog(ERROR, "Row comparison cannot use '%s'",
364 * Scan subquery's targetlist to find values that will
365 * be matched against lefthand values. We need to
366 * ignore resjunk targets, so doing the outer
367 * iteration over right_list is easier than doing it
370 while (right_list != NIL)
372 TargetEntry *tent = (TargetEntry *) lfirst(right_list);
375 Form_pg_operator opform;
378 right_list = lnext(right_list);
379 if (tent->resdom->resjunk)
382 if (left_list == NIL)
383 elog(ERROR, "Subselect has too many fields");
384 lexpr = lfirst(left_list);
385 left_list = lnext(left_list);
389 exprType(tent->expr),
391 opform = (Form_pg_operator) GETSTRUCT(optup);
393 if (opform->oprresult != BOOLOID)
394 elog(ERROR, "'%s' result type of '%s' must return '%s'"
395 " to be used with quantified predicate subquery",
396 op, typeidTypeName(opform->oprresult),
397 typeidTypeName(BOOLOID));
399 newop = makeOper(oprid(optup), /* opno */
400 InvalidOid, /* opid */
402 sublink->oper = lappend(sublink->oper, newop);
404 if (left_list != NIL)
405 elog(ERROR, "Subselect has too few fields");
407 result = (Node *) expr;
413 CaseExpr *c = (CaseExpr *) expr;
419 /* transform the list of arguments */
420 foreach(args, c->args)
425 /* shorthand form was specified, so expand... */
426 A_Expr *a = makeNode(A_Expr);
432 w->expr = (Node *) a;
434 lfirst(args) = transformExpr(pstate, (Node *) w, precedence);
438 * It's not shorthand anymore, so drop the implicit
439 * argument. This is necessary to keep the executor from
440 * seeing an untransformed expression... not to mention
441 * keeping a re-application of transformExpr from doing
446 /* transform the default clause */
447 if (c->defresult == NULL)
449 A_Const *n = makeNode(A_Const);
451 n->val.type = T_Null;
452 c->defresult = (Node *) n;
454 c->defresult = transformExpr(pstate, c->defresult, precedence);
456 /* now check types across result clauses... */
457 c->casetype = exprType(c->defresult);
459 pcategory = TypeCategory(ptype);
460 foreach(args, c->args)
465 wtype = exprType(w->result);
466 /* move on to next one if no new information... */
467 if (wtype && (wtype != UNKNOWNOID)
470 if (!ptype || ptype == UNKNOWNOID)
472 /* so far, only nulls so take anything... */
474 pcategory = TypeCategory(ptype);
476 else if ((TypeCategory(wtype) != pcategory)
477 || ((TypeCategory(wtype) == USER_TYPE)
478 && (TypeCategory(c->casetype) == USER_TYPE)))
482 * both types in different categories? then
485 elog(ERROR, "CASE/WHEN types '%s' and '%s' not matched",
486 typeidTypeName(c->casetype), typeidTypeName(wtype));
488 else if (IsPreferredType(pcategory, wtype)
489 && can_coerce_type(1, &ptype, &wtype))
493 * new one is preferred and can convert? then
497 pcategory = TypeCategory(ptype);
502 /* Convert default result clause, if necessary */
503 if (c->casetype != ptype)
505 if (!c->casetype || c->casetype == UNKNOWNOID)
509 * default clause is NULL, so assign preferred
510 * type from WHEN clauses...
514 else if (can_coerce_type(1, &c->casetype, &ptype))
516 c->defresult = coerce_type(pstate, c->defresult,
517 c->casetype, ptype, -1);
522 elog(ERROR, "CASE/ELSE unable to convert to type '%s'",
523 typeidTypeName(ptype));
527 /* Convert when clauses, if not null and if necessary */
528 foreach(args, c->args)
533 wtype = exprType(w->result);
536 * only bother with conversion if not NULL and
539 if (wtype && (wtype != UNKNOWNOID)
542 if (can_coerce_type(1, &wtype, &ptype))
544 w->result = coerce_type(pstate, w->result, wtype,
549 elog(ERROR, "CASE/WHEN unable to convert to type '%s'",
550 typeidTypeName(ptype));
561 CaseWhen *w = (CaseWhen *) expr;
563 w->expr = transformExpr(pstate, (Node *) w->expr, precedence);
564 if (exprType(w->expr) != BOOLOID)
565 elog(ERROR, "WHEN clause must have a boolean result");
568 * result is NULL for NULLIF() construct - thomas
571 if (w->result == NULL)
573 A_Const *n = makeNode(A_Const);
575 n->val.type = T_Null;
576 w->result = (Node *) n;
578 w->result = transformExpr(pstate, (Node *) w->result, precedence);
584 * Quietly accept node types that may be presented when we are
585 * called on an already-transformed tree.
587 * Do any other node types need to be accepted? For now we are
588 * taking a conservative approach, and only accepting node
589 * types that are demonstrably necessary to accept.
600 result = (Node *) expr;
605 /* should not reach here */
606 elog(ERROR, "transformExpr: does not know how to transform node %d"
607 " (internal error)", nodeTag(expr));
611 expr_depth_counter--;
617 transformIndirection(ParseState *pstate, Node *basenode, List *indirection)
619 if (indirection == NIL)
621 return (Node *) transformArraySubscripts(pstate, basenode,
622 indirection, false, NULL);
626 transformAttr(ParseState *pstate, Attr *att, int precedence)
630 basenode = ParseNestedFuncOrColumn(pstate, att, precedence);
631 return transformIndirection(pstate, basenode, att->indirection);
635 transformIdent(ParseState *pstate, Ident *ident, int precedence)
640 * try to find the ident as a relation ... but not if subscripts
643 if (ident->indirection == NIL &&
644 refnameRangeTableEntry(pstate, ident->name) != NULL)
647 result = (Node *) ident;
650 if (result == NULL || precedence == EXPR_COLUMN_FIRST)
652 /* try to find the ident as a column */
653 Node *var = colnameToVar(pstate, ident->name);
656 result = transformIndirection(pstate, var, ident->indirection);
660 elog(ERROR, "Attribute '%s' not found", ident->name);
667 * returns the Oid of the type of the expression. (Used for typechecking.)
672 Oid type = (Oid) InvalidOid;
677 switch (nodeTag(expr))
680 type = ((Func *) expr)->functype;
683 type = ((Iter *) expr)->itertype;
686 type = ((Var *) expr)->vartype;
689 type = ((Expr *) expr)->typeOid;
692 type = ((Const *) expr)->consttype;
695 type = ((ArrayRef *) expr)->refelemtype;
698 type = ((Aggref *) expr)->aggtype;
701 type = ((Param *) expr)->paramtype;
704 type = ((FieldSelect *) expr)->resulttype;
707 type = ((RelabelType *) expr)->resulttype;
711 SubLink *sublink = (SubLink *) expr;
713 if (sublink->subLinkType == EXPR_SUBLINK)
715 /* get the type of the subselect's first target column */
716 Query *qtree = (Query *) sublink->subselect;
719 if (!qtree || !IsA(qtree, Query))
720 elog(ERROR, "Cannot get type for untransformed sublink");
721 tent = (TargetEntry *) lfirst(qtree->targetList);
722 type = tent->resdom->restype;
726 /* for all other sublink types, result is boolean */
732 type = ((CaseExpr *) expr)->casetype;
735 type = exprType(((CaseWhen *) expr)->result);
742 elog(ERROR, "Do not know how to get type for %d node",
751 * returns the type-specific attrmod of the expression, if it can be
752 * determined. In most cases, it can't and we return -1.
755 exprTypmod(Node *expr)
760 switch (nodeTag(expr))
763 return ((Var *) expr)->vartypmod;
766 /* Be smart about string constants... */
767 Const *con = (Const *) expr;
769 switch (con->consttype)
772 if (!con->constisnull)
773 return VARSIZE(DatumGetPointer(con->constvalue));
784 /* Be smart about length-coercion functions... */
785 if (exprIsLengthCoercion(expr, &coercedTypmod))
786 return coercedTypmod;
790 return ((FieldSelect *) expr)->resulttypmod;
793 return ((RelabelType *) expr)->resulttypmod;
802 * exprIsLengthCoercion
803 * Detect whether an expression tree is an application of a datatype's
804 * typmod-coercion function. Optionally extract the result's typmod.
806 * If coercedTypmod is not NULL, the typmod is stored there if the expression
807 * is a length-coercion function, else -1 is stored there.
809 * We assume that a two-argument function named for a datatype, whose
810 * output and first argument types are that datatype, and whose second
811 * input is an int32 constant, represents a forced length coercion.
813 * XXX It'd be better if the parsetree retained some explicit indication
814 * of the coercion, so we didn't need these heuristics.
817 exprIsLengthCoercion(Node *expr, int32 *coercedTypmod)
822 Form_pg_proc procStruct;
823 Form_pg_type typeStruct;
825 if (coercedTypmod != NULL)
826 *coercedTypmod = -1; /* default result on failure */
828 /* Is it a function-call at all? */
831 ((Expr *) expr)->opType != FUNC_EXPR)
833 func = (Func *) (((Expr *) expr)->oper);
834 Assert(IsA(func, Func));
837 * If it's not a two-argument function with the second argument being
838 * an int4 constant, it can't have been created from a length
841 if (length(((Expr *) expr)->args) != 2)
843 second_arg = (Const *) lsecond(((Expr *) expr)->args);
844 if (!IsA(second_arg, Const) ||
845 second_arg->consttype != INT4OID ||
846 second_arg->constisnull)
850 * Lookup the function in pg_proc
852 tup = SearchSysCacheTuple(PROCOID,
853 ObjectIdGetDatum(func->funcid),
855 if (!HeapTupleIsValid(tup))
856 elog(ERROR, "cache lookup for proc %u failed", func->funcid);
857 procStruct = (Form_pg_proc) GETSTRUCT(tup);
860 * It must be a function with two arguments where the first is of the
861 * same type as the return value and the second is an int4. Also, just
862 * to be sure, check return type agrees with expr node.
864 if (procStruct->pronargs != 2 ||
865 procStruct->prorettype != procStruct->proargtypes[0] ||
866 procStruct->proargtypes[1] != INT4OID ||
867 procStruct->prorettype != ((Expr *) expr)->typeOid)
871 * Furthermore, the name of the function must be the same as the
872 * argument/result type's name.
874 tup = SearchSysCacheTuple(TYPEOID,
875 ObjectIdGetDatum(procStruct->prorettype),
877 if (!HeapTupleIsValid(tup))
878 elog(ERROR, "cache lookup for type %u failed",
879 procStruct->prorettype);
880 typeStruct = (Form_pg_type) GETSTRUCT(tup);
881 if (strncmp(NameStr(procStruct->proname),
882 NameStr(typeStruct->typname),
887 * OK, it is indeed a length-coercion function.
889 if (coercedTypmod != NULL)
890 *coercedTypmod = DatumGetInt32(second_arg->constvalue);
895 * Produce an appropriate Const node from a constant value produced
896 * by the parser and an explicit type name to cast to.
899 parser_typecast_constant(Value *expr, TypeName *typename)
904 char *const_string = NULL;
905 bool string_palloced = false;
908 switch (nodeTag(expr))
911 string_palloced = true;
912 const_string = DatumGetCString(DirectFunctionCall1(int4out,
913 Int32GetDatum(expr->val.ival)));
917 const_string = expr->val.str;
923 elog(ERROR, "Cannot cast this expression to type '%s'",
927 if (typename->arrayBounds != NIL)
929 char type_string[NAMEDATALEN + 2];
931 sprintf(type_string, "_%s", typename->name);
932 tp = (Type) typenameType(type_string);
935 tp = (Type) typenameType(typename->name);
938 datum = (Datum) NULL;
940 datum = stringTypeDatum(tp, const_string, typename->typmod);
942 con = makeConst(typeTypeId(tp),
947 false, /* not a set */
948 true /* is cast */ );
957 * Handle an explicit CAST applied to a non-constant expression.
958 * (Actually, this works for constants too, but gram.y won't generate
959 * a TypeCast node if the argument is just a constant.)
961 * The given expr has already been transformed, but we need to lookup
962 * the type name and then apply any necessary coercion function(s).
965 parser_typecast_expression(ParseState *pstate,
966 Node *expr, TypeName *typename)
968 Oid inputType = exprType(expr);
972 if (typename->arrayBounds != NIL)
974 char type_string[NAMEDATALEN + 2];
976 sprintf(type_string, "_%s", typename->name);
977 tp = (Type) typenameType(type_string);
980 tp = (Type) typenameType(typename->name);
981 targetType = typeTypeId(tp);
983 if (inputType == InvalidOid)
984 return expr; /* do nothing if NULL input */
986 if (inputType != targetType)
988 expr = CoerceTargetExpr(pstate, expr, inputType,
989 targetType, typename->typmod);
991 elog(ERROR, "Cannot cast type '%s' to '%s'",
992 typeidTypeName(inputType),
993 typeidTypeName(targetType));
997 * If the target is a fixed-length type, it may need a length coercion
998 * as well as a type coercion.
1000 expr = coerce_type_typmod(pstate, expr,
1001 targetType, typename->typmod);