1 /*-------------------------------------------------------------------------
4 * various routines that make nodes for querytrees
6 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $Header: /cvsroot/pgsql/src/backend/parser/parse_node.c,v 1.76 2002/12/12 20:35:13 tgl Exp $
13 *-------------------------------------------------------------------------
17 #include "access/heapam.h"
18 #include "catalog/pg_operator.h"
19 #include "catalog/pg_type.h"
20 #include "nodes/makefuncs.h"
21 #include "parser/parsetree.h"
22 #include "parser/parse_coerce.h"
23 #include "parser/parse_expr.h"
24 #include "parser/parse_node.h"
25 #include "parser/parse_oper.h"
26 #include "parser/parse_relation.h"
27 #include "utils/builtins.h"
28 #include "utils/int8.h"
29 #include "utils/lsyscache.h"
30 #include "utils/syscache.h"
31 #include "utils/varbit.h"
35 * Allocate and initialize a new ParseState.
36 * The CALLER is responsible for freeing the ParseState* returned.
39 make_parsestate(ParseState *parentParseState)
43 pstate = palloc0(sizeof(ParseState));
45 pstate->parentParseState = parentParseState;
46 pstate->p_last_resno = 1;
53 * Ensure argument type match by forcing conversion of constants.
56 make_operand(Node *tree, Oid orig_typeId, Oid target_typeId)
63 if (target_typeId != orig_typeId)
64 result = coerce_type(tree, orig_typeId, target_typeId,
65 COERCION_IMPLICIT, COERCE_IMPLICIT_CAST);
71 /* otherwise, this is a NULL value */
72 result = (Node *) makeNullConst(target_typeId);
76 } /* make_operand() */
80 * Operator construction.
82 * Transform operator expression ensuring type compatibility.
83 * This is where some type conversion happens.
86 make_op(List *opname, Node *ltree, Node *rtree)
91 Form_pg_operator opform;
96 ltypeId = (ltree == NULL) ? UNKNOWNOID : exprType(ltree);
97 rtypeId = (rtree == NULL) ? UNKNOWNOID : exprType(rtree);
102 tup = right_oper(opname, ltypeId, false);
103 opform = (Form_pg_operator) GETSTRUCT(tup);
104 left = make_operand(ltree, ltypeId, opform->oprleft);
109 else if (ltree == NULL)
111 tup = left_oper(opname, rtypeId, false);
112 opform = (Form_pg_operator) GETSTRUCT(tup);
113 right = make_operand(rtree, rtypeId, opform->oprright);
117 /* otherwise, binary operator */
120 tup = oper(opname, ltypeId, rtypeId, false);
121 opform = (Form_pg_operator) GETSTRUCT(tup);
122 left = make_operand(ltree, ltypeId, opform->oprleft);
123 right = make_operand(rtree, rtypeId, opform->oprright);
126 result = makeNode(OpExpr);
127 result->opno = oprid(tup);
128 result->opfuncid = InvalidOid;
129 result->opresulttype = opform->oprresult;
130 result->opretset = get_func_retset(opform->oprcode);
133 result->args = makeList1(right);
135 result->args = makeList1(left);
137 result->args = makeList2(left, right);
139 ReleaseSysCache(tup);
141 return (Expr *) result;
147 * Build a Var node for an attribute identified by RTE and attrno
150 make_var(ParseState *pstate, RangeTblEntry *rte, int attrno)
157 vnum = RTERangeTablePosn(pstate, rte, &sublevels_up);
158 get_rte_attribute_type(rte, attrno, &vartypeid, &type_mod);
159 return makeVar(vnum, attrno, vartypeid, type_mod, sublevels_up);
163 * transformArraySubscripts()
164 * Transform array subscripting. This is used for both
165 * array fetch and array assignment.
167 * In an array fetch, we are given a source array value and we produce an
168 * expression that represents the result of extracting a single array element
171 * In an array assignment, we are given a destination array value plus a
172 * source value that is to be assigned to a single element or a slice of
173 * that array. We produce an expression that represents the new array value
174 * with the source data inserted into the right part of the array.
177 * arrayBase Already-transformed expression for the array as a whole
178 * (may be NULL if we are handling an INSERT)
179 * arrayType OID of array's datatype
180 * arrayTypMod typmod to be applied to array elements
181 * indirection Untransformed list of subscripts (must not be NIL)
182 * forceSlice If true, treat subscript as array slice in all cases
183 * assignFrom NULL for array fetch, else transformed expression for source.
186 transformArraySubscripts(ParseState *pstate,
196 HeapTuple type_tuple_array,
198 Form_pg_type type_struct_array,
200 bool isSlice = forceSlice;
201 List *upperIndexpr = NIL;
202 List *lowerIndexpr = NIL;
206 /* Get the type tuple for the array */
207 type_tuple_array = SearchSysCache(TYPEOID,
208 ObjectIdGetDatum(arrayType),
210 if (!HeapTupleIsValid(type_tuple_array))
211 elog(ERROR, "transformArraySubscripts: Cache lookup failed for array type %u",
213 type_struct_array = (Form_pg_type) GETSTRUCT(type_tuple_array);
215 elementType = type_struct_array->typelem;
216 if (elementType == InvalidOid)
217 elog(ERROR, "transformArraySubscripts: type %s is not an array",
218 NameStr(type_struct_array->typname));
220 /* Get the type tuple for the array element type */
221 type_tuple_element = SearchSysCache(TYPEOID,
222 ObjectIdGetDatum(elementType),
224 if (!HeapTupleIsValid(type_tuple_element))
225 elog(ERROR, "transformArraySubscripts: Cache lookup failed for array element type %u",
227 type_struct_element = (Form_pg_type) GETSTRUCT(type_tuple_element);
230 * A list containing only single subscripts refers to a single array
231 * element. If any of the items are double subscripts (lower:upper),
232 * then the subscript expression means an array slice operation. In
233 * this case, we supply a default lower bound of 1 for any items that
234 * contain only a single subscript. The forceSlice parameter forces us
235 * to treat the operation as a slice, even if no lower bounds are
236 * mentioned. Otherwise, we have to prescan the indirection list to
237 * see if there are any double subscripts.
241 foreach(idx, indirection)
243 A_Indices *ai = (A_Indices *) lfirst(idx);
245 if (ai->lidx != NULL)
254 * The type represented by the subscript expression is the element
255 * type if we are fetching a single element, but it is the same as the
256 * array type if we are fetching a slice or storing.
258 if (isSlice || assignFrom != NULL)
259 resultType = arrayType;
261 resultType = elementType;
264 * Transform the subscript expressions.
266 foreach(idx, indirection)
268 A_Indices *ai = (A_Indices *) lfirst(idx);
275 subexpr = transformExpr(pstate, ai->lidx);
276 /* If it's not int4 already, try to coerce */
277 subexpr = coerce_to_target_type(subexpr, exprType(subexpr),
280 COERCE_IMPLICIT_CAST);
282 elog(ERROR, "array index expressions must be integers");
286 /* Make a constant 1 */
287 subexpr = (Node *) makeConst(INT4OID,
291 true); /* pass by value */
293 lowerIndexpr = lappend(lowerIndexpr, subexpr);
295 subexpr = transformExpr(pstate, ai->uidx);
296 /* If it's not int4 already, try to coerce */
297 subexpr = coerce_to_target_type(subexpr, exprType(subexpr),
300 COERCE_IMPLICIT_CAST);
302 elog(ERROR, "array index expressions must be integers");
303 upperIndexpr = lappend(upperIndexpr, subexpr);
307 * If doing an array store, coerce the source value to the right type.
309 if (assignFrom != NULL)
311 Oid typesource = exprType(assignFrom);
312 Oid typeneeded = isSlice ? arrayType : elementType;
314 if (typesource != InvalidOid)
316 assignFrom = coerce_to_target_type(assignFrom, typesource,
317 typeneeded, arrayTypMod,
319 COERCE_IMPLICIT_CAST);
320 if (assignFrom == NULL)
321 elog(ERROR, "Array assignment requires type %s"
322 " but expression is of type %s"
323 "\n\tYou will need to rewrite or cast the expression",
324 format_type_be(typeneeded),
325 format_type_be(typesource));
330 * Ready to build the ArrayRef node.
332 aref = makeNode(ArrayRef);
333 aref->refrestype = resultType; /* XXX should save element type
335 aref->refattrlength = type_struct_array->typlen;
336 aref->refelemlength = type_struct_element->typlen;
337 aref->refelembyval = type_struct_element->typbyval;
338 aref->refelemalign = type_struct_element->typalign;
339 aref->refupperindexpr = upperIndexpr;
340 aref->reflowerindexpr = lowerIndexpr;
341 aref->refexpr = (Expr *) arrayBase;
342 aref->refassgnexpr = (Expr *) assignFrom;
344 ReleaseSysCache(type_tuple_array);
345 ReleaseSysCache(type_tuple_element);
353 * Convert a Value node (as returned by the grammar) to a Const node
354 * of the "natural" type for the constant. Note that this routine is
355 * only used when there is no explicit cast for the constant, so we
356 * have to guess what type is wanted.
358 * For string literals we produce a constant of type UNKNOWN ---- whose
359 * representation is the same as text, but it indicates to later type
360 * resolution that we're not sure that it should be considered text.
361 * Explicit "NULL" constants are also typed as UNKNOWN.
363 * For integers and floats we produce int4, int8, or numeric depending
364 * on the value of the number. XXX This should include int2 as well,
365 * but additional cleanup is needed before we can do that; else cases
366 * like "WHERE int4var = 42" will fail to be indexable.
369 make_const(Value *value)
378 switch (nodeTag(value))
381 val = Int32GetDatum(intVal(value));
384 typelen = sizeof(int32);
389 /* could be an oversize integer as well as a float ... */
390 if (scanint8(strVal(value), true, &val64))
392 val = Int64GetDatum(val64);
395 typelen = sizeof(int64);
396 typebyval = false; /* XXX might change someday */
400 val = DirectFunctionCall3(numeric_in,
401 CStringGetDatum(strVal(value)),
402 ObjectIdGetDatum(InvalidOid),
406 typelen = -1; /* variable len */
412 val = DirectFunctionCall1(unknownin,
413 CStringGetDatum(strVal(value)));
415 typeid = UNKNOWNOID; /* will be coerced later */
416 typelen = -1; /* variable len */
421 val = DirectFunctionCall3(bit_in,
422 CStringGetDatum(strVal(value)),
423 ObjectIdGetDatum(InvalidOid),
431 elog(WARNING, "make_const: unknown type %d", nodeTag(value));
435 /* return a null const */
436 con = makeConst(UNKNOWNOID,
444 con = makeConst(typeid,